/** * 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_metainfo_cleanup($epOrParams) { $params = $epOrParams instanceof rex_extension_point ? $epOrParams->getParams() : $epOrParams; // 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::getTablePrefix() . 'article`') === false && strpos($params['content'], 'CREATE TABLE ' . rex::getTablePrefix() . 'article') === false) { return; } // check wheter tables exists $tables = rex_sql::showTables(); if (!isset($tables[rex::getTablePrefix() . 'metainfo_field'])) { return false; } // since this extension may be used also when the addon is not yet installed, // require needed classes manually require_once __DIR__ . '/../lib/table_manager.php'; $sql = rex_sql::factory(); $sql->setQuery('SELECT name FROM ' . rex::getTablePrefix() . 'metainfo_field'); for ($i = 0; $i < $sql->getRows(); ++$i) { $prefix = rex_metainfo_meta_prefix($sql->getValue('name')); $table = rex_metainfo_meta_table($prefix); $tableManager = new rex_metainfo_table_manager($table); $tableManager->deleteColumn($sql->getValue('name')); $sql->next(); } // evtl reste aufräumen $tablePrefixes = ['article' => ['art_', 'cat_'], 'media' => ['med_'], 'clang' => ['clang_']]; foreach ($tablePrefixes as $table => $prefixes) { $table = rex::getTablePrefix() . $table; $tableManager = new rex_metainfo_table_manager($table); foreach (rex_sql::showColumns($table) as $column) { $column = $column['name']; if (in_array(substr($column, 0, 4), $prefixes)) { $tableManager->deleteColumn($column); } } } $sql = rex_sql::factory(); $sql->setQuery('DELETE FROM ' . rex::getTablePrefix() . 'metainfo_field'); }
function rex_metainfo_delete_field($fieldIdOrName) { // Löschen anhand der FieldId if (is_int($fieldIdOrName)) { $fieldQry = 'SELECT * FROM ' . rex::getTablePrefix() . 'metainfo_field WHERE id=:idOrName LIMIT 2'; $invalidField = rex_i18n::msg('minfo_field_error_invalid_fieldid'); } elseif (is_string($fieldIdOrName)) { $fieldQry = 'SELECT * FROM ' . rex::getTablePrefix() . 'metainfo_field WHERE name=:idOrName LIMIT 2'; $invalidField = rex_i18n::msg('minfo_field_error_invalid_name'); } else { throw new InvalidArgumentException('MetaInfos: Unexpected type for $fieldIdOrName!'); } // Feld existiert? $sql = rex_sql::factory(); $sql->setQuery($fieldQry, [':idOrName' => $fieldIdOrName]); if ($sql->getRows() != 1) { return $invalidField; } $name = $sql->getValue('name'); $field_id = $sql->getValue('id'); $prefix = rex_metainfo_meta_prefix($name); $metaTable = rex_metainfo_meta_table($prefix); // Spalte existiert? $sql->setQuery('SELECT * FROM ' . $metaTable . ' LIMIT 1'); if (!in_array($name, $sql->getFieldnames())) { return rex_i18n::msg('minfo_field_error_invalid_name'); } $sql->setTable(rex::getTablePrefix() . 'metainfo_field'); $sql->setWhere(['id' => $field_id]); $sql->delete(); $tableManager = new rex_metainfo_table_manager($metaTable); return $tableManager->deleteColumn($name); }
/** * Extension to check whether the given media is still in use. * * @param rex_extension_point $ep * * @throws rex_exception * * @return string */ public static function isMediaInUse(rex_extension_point $ep) { $params = $ep->getParams(); $warning = $ep->getSubject(); $sql = rex_sql::factory(); $sql->setQuery('SELECT `name`, `type_id` FROM `' . rex::getTablePrefix() . 'metainfo_field` WHERE `type_id` IN(6,7)'); $rows = $sql->getRows(); if ($rows == 0) { return $warning; } $where = ['articles' => [], 'media' => []]; $filename = addslashes($params['filename']); for ($i = 0; $i < $rows; ++$i) { $name = $sql->getValue('name'); if (rex_metainfo_meta_prefix($name) == self::PREFIX) { $key = 'media'; } else { $key = 'articles'; } switch ($sql->getValue('type_id')) { case '6': $where[$key][] = $name . '="' . $filename . '"'; break; case '7': $where[$key][] = 'FIND_IN_SET("' . $filename . '", ' . $name . ')'; break; default: throw new rex_exception('Unexpected fieldtype "' . $sql->getValue('type_id') . '"!'); } $sql->next(); } $articles = ''; $categories = ''; if (!empty($where['articles'])) { $sql->setQuery('SELECT id, clang_id, parent_id, name, catname, startarticle FROM ' . rex::getTablePrefix() . 'article WHERE ' . implode(' OR ', $where['articles'])); if ($sql->getRows() > 0) { foreach ($sql->getArray() as $art_arr) { $aid = $art_arr['id']; $clang = $art_arr['clang_id']; $parent_id = $art_arr['parent_id']; if ($art_arr['startarticle']) { $categories .= '<li><a href="javascript:openPage(\'' . rex_url::backendPage('structure', ['edit_id' => $aid, 'function' => 'edit_cat', 'category_id' => $parent_id, 'clang' => $clang]) . '\')">' . $art_arr['catname'] . '</a></li>'; } else { $articles .= '<li><a href="javascript:openPage(\'' . rex_url::backendPage('content', ['article_id' => $aid, 'mode' => 'meta', 'clang' => $clang]) . '\')">' . $art_arr['name'] . '</a></li>'; } } if ($articles != '') { $warning[] = rex_i18n::msg('minfo_media_in_use_art') . '<br /><ul>' . $articles . '</ul>'; } if ($categories != '') { $warning[] = rex_i18n::msg('minfo_media_in_use_cat') . '<br /><ul>' . $categories . '</ul>'; } } } $media = ''; if (!empty($where['media'])) { $sql->setQuery('SELECT id, filename, category_id FROM ' . rex::getTablePrefix() . 'media WHERE ' . implode(' OR ', $where['media'])); if ($sql->getRows() > 0) { foreach ($sql->getArray() as $med_arr) { $id = $med_arr['id']; $filename = $med_arr['filename']; $cat_id = $med_arr['category_id']; $media .= '<li><a href="' . rex_url::backendPage('mediapool/detail', ['file_id' => $id, 'rex_file_category' => $cat_id]) . '">' . $filename . '</a></li>'; } if ($media != '') { $warning[] = rex_i18n::msg('minfo_media_in_use_med') . '<br /><ul>' . $media . '</ul>'; } } } return $warning; }