Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 41 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| ThemeConverter | |
0.00% |
0 / 41 |
|
0.00% |
0 / 1 |
272 | |
0.00% |
0 / 1 |
| convertFromLegacyTheme | |
0.00% |
0 / 41 |
|
0.00% |
0 / 1 |
272 | |||
| 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) 2015 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\tao\model\theme; |
| 23 | |
| 24 | use oat\oatbox\Configurable; |
| 25 | use /** @noinspection PhpDeprecationInspection */ |
| 26 | oat\tao\model\theme\DefaultTheme; |
| 27 | use oat\tao\model\theme\ConfigurablePlatformTheme; |
| 28 | use oat\tao\helpers\Template; |
| 29 | |
| 30 | /** |
| 31 | * Class ThemeConverter |
| 32 | * |
| 33 | * Class to convert legacy platform themes to ConfigurablePlatformTheme |
| 34 | * |
| 35 | * @package oat\tao\model\theme |
| 36 | */ |
| 37 | class ThemeConverter |
| 38 | { |
| 39 | /** |
| 40 | * Build an instance of ConfigurablePlatformTheme from a legacy theme |
| 41 | * |
| 42 | * @param object|array $theme |
| 43 | * @return ConfigurablePlatformTheme |
| 44 | * @throws \common_exception_MissingParameter |
| 45 | */ |
| 46 | public static function convertFromLegacyTheme($theme) |
| 47 | { |
| 48 | if ($theme instanceof ConfigurablePlatformTheme) { |
| 49 | return $theme; |
| 50 | } |
| 51 | |
| 52 | // older themes are stored as an instance, newer ones as array |
| 53 | if (is_array($theme)) { |
| 54 | if (empty($theme[ThemeServiceInterface::THEME_CLASS_OFFSET])) { |
| 55 | throw new \common_exception_MissingParameter( |
| 56 | ThemeServiceInterface::THEME_CLASS_OFFSET, |
| 57 | __METHOD__ |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | $options = !empty($theme[ThemeServiceInterface::THEME_OPTIONS_OFFSET]) |
| 62 | ? $theme[ThemeServiceInterface::THEME_OPTIONS_OFFSET] |
| 63 | : [] |
| 64 | ; |
| 65 | |
| 66 | $theme = new $theme[ThemeServiceInterface::THEME_CLASS_OFFSET]($options); |
| 67 | } |
| 68 | |
| 69 | // list of all previously used templates |
| 70 | $templates = ['footer', 'header', 'header-logo', 'login-message']; |
| 71 | |
| 72 | // all keys associated with a public getter from previously used theme classes |
| 73 | $getKeys = ['id', 'label', 'stylesheet', 'logoUrl', 'link', 'message', 'customTexts']; |
| 74 | |
| 75 | // collect options |
| 76 | $options = []; |
| 77 | if (method_exists($theme, 'getOptions')) { |
| 78 | $options = $theme->getOptions(); |
| 79 | } |
| 80 | |
| 81 | if (method_exists($theme, 'getThemeData')) { |
| 82 | $options = array_merge($options, $theme->getThemeData()); |
| 83 | } |
| 84 | unset($options['data']); |
| 85 | |
| 86 | foreach ($getKeys as $key) { |
| 87 | $method = 'get' . ucfirst($key); |
| 88 | if (method_exists($theme, $method)) { |
| 89 | $options[$key] = $theme->{$method}(); |
| 90 | } |
| 91 | } |
| 92 | // TAO default logo URL is different |
| 93 | if ($theme instanceof DefaultTheme) { |
| 94 | $options['logoUrl'] = Template::img('tao-logo.png', 'tao'); |
| 95 | } |
| 96 | |
| 97 | if (method_exists($theme, 'getTemplate')) { |
| 98 | if (empty($options[ConfigurablePlatformTheme::TEMPLATES])) { |
| 99 | $options[ConfigurablePlatformTheme::TEMPLATES] = []; |
| 100 | } |
| 101 | foreach ($templates as $id) { |
| 102 | $template = $theme->getTemplate($id); |
| 103 | if (!is_null($template)) { |
| 104 | $options[ConfigurablePlatformTheme::TEMPLATES][$id] = $template; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // example: oat\taoExtension\model\theme\MyTheme |
| 110 | $themeClass = get_class($theme); |
| 111 | //@todo: map to container id |
| 112 | if (empty($options[ConfigurablePlatformTheme::EXTENSION_ID])) { |
| 113 | strtok($themeClass, '\\'); |
| 114 | $options[ConfigurablePlatformTheme::EXTENSION_ID] = strtok('\\'); |
| 115 | } |
| 116 | |
| 117 | if (empty($options[ConfigurablePlatformTheme::LABEL])) { |
| 118 | $options[ConfigurablePlatformTheme::LABEL] = basename($themeClass); |
| 119 | } |
| 120 | return new ConfigurablePlatformTheme($options); |
| 121 | } |
| 122 | } |