/**
 * Alle Metafelder löschen, nicht das nach einem Import in der Parameter Tabelle
 * noch Datensätze zu Feldern stehen, welche nicht als Spalten in der
 * rex_article angelegt wurden!
 */
function rex_a62_metainfo_cleanup($params)
{
    global $REX;
    // Cleanup nur durchführen, wenn auch die rex_article Tabelle neu angelegt wird
    if (isset($params['force']) && $params['force'] != true && strpos($params['content'], 'CREATE TABLE `' . $REX['TABLE_PREFIX'] . 'article`') === false && strpos($params['content'], 'CREATE TABLE ' . $REX['TABLE_PREFIX'] . 'article') === false) {
        return;
    }
    require_once $REX['INCLUDE_PATH'] . '/addons/metainfo/classes/class.rex_table_manager.inc.php';
    $sql = new rex_sql();
    $sql->setQuery('SELECT name FROM ' . $REX['TABLE_PREFIX'] . '62_params');
    for ($i = 0; $i < $sql->getRows(); $i++) {
        if (substr($sql->getValue('name'), 0, 4) == 'med_') {
            $tableManager = new rex_a62_tableManager($REX['TABLE_PREFIX'] . 'file');
        } else {
            $tableManager = new rex_a62_tableManager($REX['TABLE_PREFIX'] . 'article');
        }
        $tableManager->deleteColumn($sql->getValue('name'));
        $sql->next();
    }
    // evtl reste aufräumen
    $tablePrefixes = array('article' => array('art_', 'cat_'), 'file' => array('med_'));
    foreach ($tablePrefixes as $table => $prefixes) {
        $table = $REX['TABLE_PREFIX'] . $table;
        $tableManager = new rex_a62_tableManager($table);
        foreach (rex_sql::showColumns($table) as $column) {
            $column = $column['name'];
            if (in_array(substr($column, 0, 4), $prefixes)) {
                $tableManager->deleteColumn($column);
            }
        }
    }
    $sql = new rex_sql();
    $sql->setQuery('DELETE FROM ' . $REX['TABLE_PREFIX'] . '62_params');
    rex_generateAll();
}
function a62_delete_field($fieldIdOrName)
{
    global $REX, $I18N;
    // Löschen anhand der FieldId
    if (is_int($fieldIdOrName)) {
        $fieldQry = 'SELECT * FROM ' . $REX['TABLE_PREFIX'] . '62_params WHERE field_id=' . $fieldIdOrName . ' LIMIT 2';
        $invalidField = $I18N->msg('minfo_field_error_invalid_fieldid');
    } else {
        if (is_string($fieldIdOrName)) {
            $fieldQry = 'SELECT * FROM ' . $REX['TABLE_PREFIX'] . '62_params WHERE name="' . addslashes($fieldIdOrName) . '" LIMIT 2';
            $invalidField = $I18N->msg('minfo_field_error_invalid_name');
        } else {
            trigger_error('MetaInfos: Unexpected type for $fieldIdOrName!', E_USER_ERROR);
        }
    }
    // Feld existiert?
    $sql = rex_sql::factory();
    $fieldInfos = $sql->getArray($fieldQry);
    if ($sql->getRows() != 1) {
        return $invalidField;
    }
    $name = $fieldInfos[0]['name'];
    $field_id = $fieldInfos[0]['field_id'];
    $prefix = a62_meta_prefix($name);
    $metaTable = a62_meta_table($prefix);
    // Spalte existiert?
    $sql->setQuery('SELECT * FROM ' . $metaTable . ' LIMIT 1');
    if (!in_array($name, $sql->getFieldnames())) {
        return $I18N->msg('minfo_field_error_invalid_name');
    }
    $sql->setTable($REX['TABLE_PREFIX'] . '62_params');
    $sql->setWhere('field_id=' . $field_id);
    if (!$sql->delete()) {
        return $sql->getError();
    }
    $tableManager = new rex_a62_tableManager($metaTable);
    return $tableManager->deleteColumn($name);
}