1 <?php
  2 
  3 /**
  4  * @package redaxo\mediapool
  5  */
  6 class rex_media_category_service
  7 {
  8     /**
  9      * @param string                  $name   The name of the new category
 10      * @param rex_media_category|null $parent The category in which the new category should be created, or null for a top/root level category
 11      *
 12      * @return string A success message
 13      */
 14     public static function addCategory($name, $parent)
 15     {
 16         $db = rex_sql::factory();
 17 
 18         // root category
 19         $parent_id = 0;
 20         $path = '|';
 21         if ($parent) {
 22             $parent_id = $parent->getId();
 23             $path = $parent->getPath() . $parent->getId().'|';
 24         }
 25 
 26         $db->setTable(rex::getTablePrefix() . 'media_category');
 27         $db->setValue('name', $name);
 28         $db->setValue('parent_id', $parent_id);
 29         $db->setValue('path', $path);
 30         $db->addGlobalCreateFields();
 31         $db->addGlobalUpdateFields();
 32 
 33         $db->insert();
 34 
 35         rex_media_cache::deleteCategoryList($parent_id);
 36 
 37         return rex_i18n::msg('pool_kat_saved', $name);
 38     }
 39 
 40     /**
 41      * @param int $categoryId
 42      *
 43      * @return string A success message
 44      *
 45      * @throws rex_functional_exception
 46      */
 47     public static function deleteCategory($categoryId)
 48     {
 49         $gf = rex_sql::factory();
 50         $gf->setQuery('SELECT * FROM ' . rex::getTablePrefix() . 'media WHERE category_id=?', [$categoryId]);
 51         $gd = rex_sql::factory();
 52         $gd->setQuery('SELECT * FROM ' . rex::getTablePrefix() . 'media_category WHERE parent_id=?', [$categoryId]);
 53         if ($gf->getRows() == 0 && $gd->getRows() == 0) {
 54             if ($uses = self::categoryIsInUse($categoryId)) {
 55                 $gf->setQuery('SELECT name FROM ' . rex::getTable('media_category') . ' WHERE id=?', [$categoryId]);
 56                 $name = "{$gf->getValue('name')} [$categoryId]";
 57                 throw new rex_functional_exception('<strong>' . rex_i18n::msg('pool_kat_delete_error', $name) . ' '
 58                     . rex_i18n::msg('pool_object_in_use_by') . '</strong><br />' . $uses);
 59             }
 60 
 61             $gf->setQuery('DELETE FROM ' . rex::getTablePrefix() . 'media_category WHERE id=?', [$categoryId]);
 62             rex_media_cache::deleteCategory($categoryId);
 63             rex_media_cache::deleteLists();
 64         } else {
 65             throw new rex_functional_exception(rex_i18n::msg('pool_kat_not_deleted'));
 66         }
 67 
 68         return rex_i18n::msg('pool_kat_deleted');
 69     }
 70 
 71     /**
 72      * @param int $categoryId
 73      *
 74      * @return bool|string false|warning-Message
 75      */
 76     public static function categoryIsInUse($categoryId)
 77     {
 78         // ----- EXTENSION POINT
 79         $warning = rex_extension::registerPoint(new rex_extension_point('MEDIA_CATEGORY_IS_IN_USE', [], [
 80             'id' => $categoryId,
 81         ]));
 82 
 83         if (!empty($warning)) {
 84             return implode('<br />', $warning);
 85         }
 86 
 87         return false;
 88     }
 89 
 90     /**
 91      * @param int   $categoryId The id of the category to edit
 92      * @param array $data       The category data
 93      *
 94      * @return string A success message
 95      */
 96     public static function editCategory($categoryId, array $data)
 97     {
 98         $cat_name = $data['name'];
 99 
100         $db = rex_sql::factory();
101         $db->setTable(rex::getTablePrefix() . 'media_category');
102         $db->setWhere(['id' => $categoryId]);
103         $db->setValue('name', $cat_name);
104         $db->addGlobalUpdateFields();
105 
106         $db->update();
107 
108         rex_media_cache::deleteCategory($categoryId);
109         return rex_i18n::msg('pool_kat_updated', $cat_name);
110     }
111 }
112