Code Coverage  | 
      ||||||||||
Lines  | 
       Functions and Methods  | 
       Classes and Traits  | 
      ||||||||
| Total |         | 
       0.00%  | 
       0 / 73  | 
               | 
       0.00%  | 
       0 / 10  | 
       CRAP |         | 
       0.00%  | 
       0 / 1  | 
      
| ItemThemeInstaller |         | 
       0.00%  | 
       0 / 73  | 
               | 
       0.00%  | 
       0 / 10  | 
       756 |         | 
       0.00%  | 
       0 / 1  | 
      
| __construct |         | 
       0.00%  | 
       0 / 2  | 
               | 
       0.00%  | 
       0 / 1  | 
       2 | |||
| remove |         | 
       0.00%  | 
       0 / 8  | 
               | 
       0.00%  | 
       0 / 1  | 
       12 | |||
| add |         | 
       0.00%  | 
       0 / 7  | 
               | 
       0.00%  | 
       0 / 1  | 
       12 | |||
| update |         | 
       0.00%  | 
       0 / 5  | 
               | 
       0.00%  | 
       0 / 1  | 
       6 | |||
| setDefault |         | 
       0.00%  | 
       0 / 7  | 
               | 
       0.00%  | 
       0 / 1  | 
       6 | |||
| reset |         | 
       0.00%  | 
       0 / 22  | 
               | 
       0.00%  | 
       0 / 1  | 
       42 | |||
| themeExists |         | 
       0.00%  | 
       0 / 8  | 
               | 
       0.00%  | 
       0 / 1  | 
       20 | |||
| getPrefixedThemeId |         | 
       0.00%  | 
       0 / 5  | 
               | 
       0.00%  | 
       0 / 1  | 
       12 | |||
| getStylesheetPath |         | 
       0.00%  | 
       0 / 3  | 
               | 
       0.00%  | 
       0 / 1  | 
       6 | |||
| register |         | 
       0.00%  | 
       0 / 6  | 
               | 
       0.00%  | 
       0 / 1  | 
       2 | |||
| 1 | <?php | 
| 2 | |
| 3 | /** | 
| 4 | * This program is free software; you can redistribute it and/or | 
| 5 | * modify it under the terms of the GNU General Public License | 
| 6 | * as published by the Free Software Foundation; under version 2 | 
| 7 | * of the License (non-upgradable). | 
| 8 | * | 
| 9 | * This program is distributed in the hope that it will be useful, | 
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
| 12 | * GNU General Public License for more details. | 
| 13 | * | 
| 14 | * You should have received a copy of the GNU General Public License | 
| 15 | * along with this program; if not, write to the Free Software | 
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
| 17 | * | 
| 18 | * Copyright (c) 2017 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); | 
| 19 | * | 
| 20 | */ | 
| 21 | |
| 22 | namespace oat\taoQtiItem\model\themes; | 
| 23 | |
| 24 | use oat\oatbox\log\LoggerAwareTrait; | 
| 25 | use oat\tao\model\ThemeRegistry; | 
| 26 | |
| 27 | /** | 
| 28 | * Class ItemThemeInstaller | 
| 29 | * | 
| 30 | * Item themes are usually stored in /extensionName/views/css/themes/items/name-of-the-theme. | 
| 31 | * For the usage of this class it's assumed that you are using this very setup. | 
| 32 | * For all other setups please use ThemeRegistry directly. * | 
| 33 | * | 
| 34 | * Let's say you start with a regular setup and 'tao' as the default theme. You want to remove 'tao' | 
| 35 | * and install two of your own themes. The registry should eventually look like this. | 
| 36 | * | 
| 37 | * <code> | 
| 38 | * 'available' => [ | 
| 39 | * [ | 
| 40 | * 'id' => 'taoFooDefault', | 
| 41 | * 'name' => 'TAO', | 
| 42 | * 'path' => '/taoFoo/views/css/themes/items/default/theme.css' | 
| 43 | * ], | 
| 44 | * [ | 
| 45 | * 'id' => 'taoFooOther', | 
| 46 | * 'name' => 'The other one', | 
| 47 | * 'path' => '/taoFoo/views/css/themes/items/other/theme.css' | 
| 48 | * ] | 
| 49 | * ], | 
| 50 | * 'default' => 'taoFooOther' // note the prefix | 
| 51 | * </code> | 
| 52 | * | 
| 53 | * The code for installs and updates is - apart from the themes - identical. All theme ids | 
| 54 | * will be prefixed with your extension id to avoid collisions. If you add the extension | 
| 55 | * prefix yourself it won't be doubled. The 'tao' theme is never prefixed. | 
| 56 | * | 
| 57 | * <code> | 
| 58 | * $themes = [ | 
| 59 | * 'default' => 'The default theme', // equivalent to 'taoFooDefault' => 'The default theme' | 
| 60 | * 'other' => 'The other one' | 
| 61 | * ]; | 
| 62 | * | 
| 63 | * $itemThemeInstaller = new ItemThemeInstaller('taoFoo'); // 'taoFoo' is the id of your extension | 
| 64 | * $itemThemeInstaller->add($themes); | 
| 65 | * </code> | 
| 66 | * | 
| 67 | * | 
| 68 | * Now you need to set a default theme, again there will be no prefix duplication | 
| 69 | * | 
| 70 | * <code> | 
| 71 | * $itemThemeInstaller->setDefault('default'); | 
| 72 | * </code> | 
| 73 | * | 
| 74 | * Eventually you may want to remove the 'tao' theme. Note that ItemThemeInstaller::remove() also accepts | 
| 75 | * an array of ids as argument. | 
| 76 | * | 
| 77 | * <code> | 
| 78 | * $itemThemeInstaller->remove('tao'); | 
| 79 | * // with array | 
| 80 | * $itemThemeInstaller->remove(['tao', 'foo', 'bar']); | 
| 81 | * </code> | 
| 82 | * | 
| 83 | * What if the label - the name under which the theme appears in the preview or during delivery - needs to be changed? | 
| 84 | * | 
| 85 | * <code> | 
| 86 | * $themes = [ | 
| 87 | * 'default' => 'The new label' | 
| 88 | * ]; | 
| 89 | * $itemThemeInstaller->update($themes); | 
| 90 | * </code> | 
| 91 | * | 
| 92 | * Finally you can also restore the defaults. This affects the current item extension only, themes from other extensions | 
| 93 | * stay registered. 'tao' however will be re-installed and set as default. | 
| 94 | * | 
| 95 | * <code> | 
| 96 | * $itemThemeInstaller->reset(); | 
| 97 | * </code> | 
| 98 | * | 
| 99 | * During development you often have situations where you want to run the installer/updater multiple times. You can do | 
| 100 | * this for all methods without risking errors. | 
| 101 | * | 
| 102 | * @package oat\taoQtiItem\model\themes | 
| 103 | */ | 
| 104 | class ItemThemeInstaller | 
| 105 | { | 
| 106 | use LoggerAwareTrait; | 
| 107 | |
| 108 | private $extensionId; | 
| 109 | |
| 110 | private $registry; | 
| 111 | |
| 112 | /** | 
| 113 | * ItemThemeInstaller constructor. | 
| 114 | * | 
| 115 | * @param $extensionId | 
| 116 | */ | 
| 117 | public function __construct($extensionId) | 
| 118 | { | 
| 119 | $this->extensionId = $extensionId; | 
| 120 | $this->registry = ThemeRegistry::getRegistry(); | 
| 121 | } | 
| 122 | |
| 123 | /** | 
| 124 | * Remove themes from the configuration | 
| 125 | * | 
| 126 | * @param array|string $themeIds | 
| 127 | * | 
| 128 | * @return bool | 
| 129 | */ | 
| 130 | public function remove($themeIds) | 
| 131 | { | 
| 132 | $themeIds = (array)$themeIds; | 
| 133 | foreach ($themeIds as $themeId) { | 
| 134 | $prefixedId = $this->getPrefixedThemeId($themeId); | 
| 135 | if (!$this->themeExists($prefixedId)) { | 
| 136 | continue; | 
| 137 | } | 
| 138 | $this->registry->unregisterTheme($prefixedId); | 
| 139 | } | 
| 140 | $this->logInfo('Item themes removed: ' . implode(',', $themeIds)); | 
| 141 | return true; | 
| 142 | } | 
| 143 | |
| 144 | |
| 145 | /** | 
| 146 | * @param array $themes | 
| 147 | * | 
| 148 | * @return bool | 
| 149 | */ | 
| 150 | public function add(array $themes) | 
| 151 | { | 
| 152 | |
| 153 | foreach ($themes as $themeId => $label) { | 
| 154 | $prefixedId = $this->getPrefixedThemeId($themeId); | 
| 155 | if ($this->themeExists($prefixedId)) { | 
| 156 | continue; | 
| 157 | } | 
| 158 | $this->register($themeId, $label); | 
| 159 | } | 
| 160 | |
| 161 | $this->logInfo('Item themes registered: ' . implode(',', array_keys($themes))); | 
| 162 | return true; | 
| 163 | } | 
| 164 | |
| 165 | |
| 166 | /** | 
| 167 | * @param array $themes | 
| 168 | * | 
| 169 | * @return bool | 
| 170 | */ | 
| 171 | public function update(array $themes) | 
| 172 | { | 
| 173 | foreach ($themes as $themeId => $label) { | 
| 174 | $this->remove($themeId); | 
| 175 | $this->register($themeId, $label); | 
| 176 | } | 
| 177 | $this->logInfo('Item themes updated: ' . implode(',', array_keys($themes))); | 
| 178 | return true; | 
| 179 | } | 
| 180 | |
| 181 | |
| 182 | /** | 
| 183 | * Set the current theme | 
| 184 | * | 
| 185 | * @param $themeId | 
| 186 | * | 
| 187 | * @return boolean|\common_report_Report | 
| 188 | */ | 
| 189 | public function setDefault($themeId) | 
| 190 | { | 
| 191 | $prefixedId = $this->getPrefixedThemeId($themeId); | 
| 192 | if (!$this->themeExists($prefixedId)) { | 
| 193 | $this->logInfo($themeId . ' not installed, could not set to default'); | 
| 194 | return false; | 
| 195 | } | 
| 196 | $this->registry->setDefaultTheme('items', $prefixedId); | 
| 197 | |
| 198 | $this->logInfo('Default item theme set to ' . $themeId); | 
| 199 | return true; | 
| 200 | } | 
| 201 | |
| 202 | |
| 203 | /** | 
| 204 | * Reset the item theme registry to its initial values | 
| 205 | * | 
| 206 | * @return bool|\common_report_Report | 
| 207 | */ | 
| 208 | public function reset() | 
| 209 | { | 
| 210 | $map = $this->registry->getMap(); | 
| 211 | if (empty($map['items']['available'])) { | 
| 212 | return false; | 
| 213 | } | 
| 214 | foreach ($map['items']['available'] as $theme) { | 
| 215 | // exclude themes that don't belong to this customer | 
| 216 | if ($theme['id'] === 'tao' || 0 !== strpos($theme['id'], $this->extensionId)) { | 
| 217 | continue; | 
| 218 | } | 
| 219 | $this->registry->unregisterTheme($theme['id']); | 
| 220 | } | 
| 221 | // get the now updated map | 
| 222 | $map = $this->registry->getMap(); | 
| 223 | |
| 224 | $taoTheme = [ | 
| 225 | 'id' => 'tao', | 
| 226 | 'name' => 'TAO', | 
| 227 | 'path' => $this->getStylesheetPath('tao') | 
| 228 | ]; | 
| 229 | |
| 230 | if (!$this->themeExists('tao')) { | 
| 231 | array_unshift($map['items']['available'], $taoTheme); | 
| 232 | } | 
| 233 | |
| 234 | $this->registry->set('items', [ | 
| 235 | 'base' => $map['items']['base'], | 
| 236 | // potential other themes have not been removed | 
| 237 | 'available' => $map['items']['available'], | 
| 238 | 'default' => 'tao' | 
| 239 | ]); | 
| 240 | |
| 241 | $this->logInfo('Removed ' . $this->extensionId . ' themes, restored TAO default'); | 
| 242 | return true; | 
| 243 | } | 
| 244 | |
| 245 | /** | 
| 246 | * Is a theme already registered? | 
| 247 | * | 
| 248 | * @param $themeId | 
| 249 | * | 
| 250 | * @return bool | 
| 251 | */ | 
| 252 | public function themeExists($themeId) | 
| 253 | { | 
| 254 | // while this seem to be obsolete in most cases | 
| 255 | // it can be useful when the function is called from the outside | 
| 256 | $prefixedId = $this->getPrefixedThemeId($themeId); | 
| 257 | $map = $this->registry->getMap(); | 
| 258 | if (empty($map['items']['available'])) { | 
| 259 | return false; | 
| 260 | } | 
| 261 | foreach ($map['items']['available'] as $theme) { | 
| 262 | if ($theme['id'] === $prefixedId) { | 
| 263 | return true; | 
| 264 | } | 
| 265 | } | 
| 266 | return false; | 
| 267 | } | 
| 268 | |
| 269 | |
| 270 | /** | 
| 271 | * Prefix theme id base on extension id from calling class. Pass through if already prefixed | 
| 272 | * | 
| 273 | * @param $themeId | 
| 274 | * | 
| 275 | * @return string | 
| 276 | */ | 
| 277 | protected function getPrefixedThemeId($themeId) | 
| 278 | { | 
| 279 | if ($themeId === 'tao') { | 
| 280 | return $themeId; | 
| 281 | } | 
| 282 | if (preg_match('~^' . $this->extensionId . '[A-Z]~', $themeId)) { | 
| 283 | return $themeId; | 
| 284 | } | 
| 285 | return $this->extensionId . ucfirst($themeId); | 
| 286 | } | 
| 287 | |
| 288 | /** | 
| 289 | * @param $themeId | 
| 290 | * | 
| 291 | * @return string | 
| 292 | */ | 
| 293 | protected function getStylesheetPath($themeId) | 
| 294 | { | 
| 295 | return $themeId === 'tao' | 
| 296 | ? 'taoQtiItem/views/css/themes/default.css' | 
| 297 | : sprintf('%s/views/css/themes/items/%s/theme.css', $this->extensionId, $themeId); | 
| 298 | } | 
| 299 | |
| 300 | /** | 
| 301 | * @param $themeId | 
| 302 | * @param $label | 
| 303 | */ | 
| 304 | protected function register($themeId, $label) | 
| 305 | { | 
| 306 | $this->registry->registerTheme( | 
| 307 | $this->getPrefixedThemeId($themeId), | 
| 308 | $label, | 
| 309 | $this->getStylesheetPath($themeId), | 
| 310 | ['items'] | 
| 311 | ); | 
| 312 | } | 
| 313 | } |