1 <?php
  2 
  3 /**
  4  * @package redaxo\core
  5  */
  6 class rex_clang_service
  7 {
  8     /**
  9      * Erstellt eine Clang.
 10      *
 11      * @param string $code     Clang Code
 12      * @param string $name     Name
 13      * @param int    $priority Priority
 14      * @param bool   $status   Status
 15      */
 16     public static function addCLang($code, $name, $priority, $status = false)
 17     {
 18         $sql = rex_sql::factory();
 19         $sql->setTable(rex::getTablePrefix() . 'clang');
 20         $sql->setNewId('id');
 21         $sql->setValue('code', $code);
 22         $sql->setValue('name', $name);
 23         $sql->setValue('priority', $priority);
 24         $sql->setValue('status', $status);
 25         $sql->insert();
 26         $id = $sql->getLastId();
 27 
 28         rex_sql_util::organizePriorities(rex::getTable('clang'), 'priority', '', 'priority, id != ' . $id);
 29 
 30         rex_delete_cache();
 31 
 32         // ----- EXTENSION POINT
 33         $clang = rex_clang::get($id);
 34         rex_extension::registerPoint(new rex_extension_point('CLANG_ADDED', '', [
 35             'id' => $clang->getId(),
 36             'name' => $clang->getName(),
 37             'clang' => $clang,
 38         ]));
 39     }
 40 
 41     /**
 42      * Ändert eine Clang.
 43      *
 44      * @param int       $id       Id der Clang
 45      * @param string    $code     Clang Code
 46      * @param string    $name     Name der Clang
 47      * @param int       $priority Priority
 48      * @param bool|null $status   Status
 49      *
 50      * @return bool
 51      *
 52      * @throws rex_exception
 53      */
 54     public static function editCLang($id, $code, $name, $priority, $status = null)
 55     {
 56         if (!rex_clang::exists($id)) {
 57             throw new rex_exception('clang with id "' . $id . '" does not exist');
 58         }
 59 
 60         $oldPriority = rex_clang::get($id)->getPriority();
 61 
 62         $editLang = rex_sql::factory();
 63         $editLang->setTable(rex::getTablePrefix() . 'clang');
 64         $editLang->setWhere(['id' => $id]);
 65         $editLang->setValue('code', $code);
 66         $editLang->setValue('name', $name);
 67         $editLang->setValue('priority', $priority);
 68         if (null !== $status) {
 69             $editLang->setValue('status', $status);
 70         }
 71         $editLang->update();
 72 
 73         $comparator = $oldPriority < $priority ? '=' : '!=';
 74         rex_sql_util::organizePriorities(rex::getTable('clang'), 'priority', '', 'priority, id' . $comparator . $id);
 75 
 76         rex_delete_cache();
 77 
 78         // ----- EXTENSION POINT
 79         $clang = rex_clang::get($id);
 80         rex_extension::registerPoint(new rex_extension_point('CLANG_UPDATED', '', [
 81             'id' => $clang->getId(),
 82             'name' => $clang->getName(),
 83             'clang' => $clang,
 84         ]));
 85 
 86         return true;
 87     }
 88 
 89     /**
 90      * Löscht eine Clang.
 91      *
 92      * @param int $id Zu löschende ClangId
 93      *
 94      * @throws rex_exception
 95      */
 96     public static function deleteCLang($id)
 97     {
 98         $startClang = rex_clang::getStartId();
 99         if ($id == $startClang) {
100             throw new rex_functional_exception(rex_i18n::msg('clang_error_startidcanotbedeleted', $startClang));
101         }
102 
103         if (!rex_clang::exists($id)) {
104             throw new rex_functional_exception(rex_i18n::msg('clang_error_idcanotbedeleted', $id));
105         }
106 
107         $clang = rex_clang::get($id);
108 
109         $del = rex_sql::factory();
110         $del->setQuery('delete from ' . rex::getTablePrefix() . 'clang where id=?', [$id]);
111 
112         rex_sql_util::organizePriorities(rex::getTable('clang'), 'priority', '', 'priority');
113 
114         rex_delete_cache();
115 
116         // ----- EXTENSION POINT
117         rex_extension::registerPoint(new rex_extension_point('CLANG_DELETED', '', [
118             'id' => $clang->getId(),
119             'name' => $clang->getName(),
120             'clang' => $clang,
121         ]));
122     }
123 
124     /**
125      * Schreibt Spracheigenschaften in die Datei include/clang.php.
126      *
127      * @throws rex_exception
128      */
129     public static function generateCache()
130     {
131         $lg = rex_sql::factory();
132         $lg->setQuery('select * from ' . rex::getTablePrefix() . 'clang order by priority');
133 
134         $clangs = [];
135         foreach ($lg as $lang) {
136             $id = $lang->getValue('id');
137             foreach ($lg->getFieldnames() as $field) {
138                 $clangs[$id][$field] = $lang->getValue($field);
139             }
140         }
141 
142         $file = rex_path::coreCache('clang.cache');
143         if (rex_file::putCache($file, $clangs) === false) {
144             throw new rex_exception('Clang cache file could not be generated');
145         }
146     }
147 }
148