Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 86 |
|
0.00% |
0 / 19 |
CRAP | |
0.00% |
0 / 1 |
| ConfigurablePlatformTheme | |
0.00% |
0 / 86 |
|
0.00% |
0 / 19 |
1722 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
| getTemplate | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| getPortalTemplate | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| __call | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| getThemeData | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getStylesheet | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getLogoUrl | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getLink | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getMessage | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getLabel | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getOperatedBy | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| setDefaultThemePath | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getText | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| getTextFromArray | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| getCustomTexts | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getAllTexts | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setupOptions | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
20 | |||
| getDefaultemplate | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
42 | |||
| 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 InvalidArgumentException; |
| 25 | use Jig\Utils\StringUtils; |
| 26 | use oat\oatbox\Configurable; |
| 27 | use oat\tao\helpers\Template; |
| 28 | |
| 29 | /** |
| 30 | * Class ConfigurablePlatformTheme |
| 31 | * |
| 32 | * Class to easily configure a platform theme, the configuration is written to |
| 33 | * /config/tao/theming.conf |
| 34 | * |
| 35 | * @deprecated until operation issues as resolved |
| 36 | * @package oat\tao\model\theme |
| 37 | */ |
| 38 | class ConfigurablePlatformTheme extends Configurable implements Theme, SolarDesignCheckerInterface |
| 39 | { |
| 40 | use PortalTemplateFinderTrait; |
| 41 | use SolarDesignCheckerTrait; |
| 42 | |
| 43 | /** Theme extension id key */ |
| 44 | public const EXTENSION_ID = 'extensionId'; |
| 45 | |
| 46 | /** Theme label key */ |
| 47 | public const LABEL = 'label'; |
| 48 | |
| 49 | /** Theme id key */ |
| 50 | public const ID = 'id'; |
| 51 | |
| 52 | /** Theme stylesheet key */ |
| 53 | public const STYLESHEET = 'stylesheet'; |
| 54 | |
| 55 | /** Theme logo url key */ |
| 56 | public const LOGO_URL = 'logoUrl'; |
| 57 | |
| 58 | /** Theme logo link key */ |
| 59 | public const LINK = 'link'; |
| 60 | |
| 61 | /** Theme logo title key */ |
| 62 | public const MESSAGE = 'message'; |
| 63 | |
| 64 | /** Theme templates key */ |
| 65 | public const TEMPLATES = 'templates'; |
| 66 | |
| 67 | /** Use the default path for logo, stylesheet, templates etc. */ |
| 68 | public const DEFAULT_PATH = 'useDefaultThemePath'; |
| 69 | |
| 70 | /** Path to themes */ |
| 71 | public const DEFAULT_THEME_PATH = 'themes/platform'; |
| 72 | |
| 73 | /** Logo Name */ |
| 74 | public const DEFAULT_LOGO_NAME = 'logo.png'; |
| 75 | |
| 76 | /** Stylesheet Name */ |
| 77 | public const DEFAULT_STYLESHEET_NAME = 'theme.css'; |
| 78 | |
| 79 | /** Theme operated by key */ |
| 80 | public const OPERATED_BY = 'operatedBy'; |
| 81 | |
| 82 | public const TEMPLATE_STRATEGY = 'templateStrategy'; |
| 83 | |
| 84 | public const PORTAL_TEMPLATE_STRATEGY = 'portal'; |
| 85 | |
| 86 | public const DEFAULT_TEMPLATE_STRATEGY = 'default'; |
| 87 | |
| 88 | /** |
| 89 | * Default theme path |
| 90 | * |
| 91 | * @var string |
| 92 | */ |
| 93 | private $defaultThemePath = ''; |
| 94 | |
| 95 | /** |
| 96 | * Set of custom texts that can be used in the templates |
| 97 | * |
| 98 | * @var array |
| 99 | */ |
| 100 | private $customTexts = []; |
| 101 | |
| 102 | /** |
| 103 | * These options are required to build a new instance of ConfigurablePlatformTheme |
| 104 | * |
| 105 | * @var array |
| 106 | */ |
| 107 | private $mandatoryOptions = [ |
| 108 | self::LABEL |
| 109 | ]; |
| 110 | |
| 111 | |
| 112 | /** |
| 113 | * ConfigurablePlatformTheme constructor. |
| 114 | * |
| 115 | * @examples |
| 116 | * Only label is configured, this will create a default configuration. |
| 117 | * Label is the only mandatory element. |
| 118 | * |
| 119 | * $options = [ |
| 120 | * 'label' => 'Default Theme' |
| 121 | * ]; |
| 122 | * $theme = new \oat\tao\model\theme\ConfigurablePlatformTheme($options); |
| 123 | * |
| 124 | * This will end up as: |
| 125 | * $options = [ |
| 126 | * 'logoUrl' => 'http://domain/taoSomething/views/img/themes/platform/default-theme/logo.png', |
| 127 | * 'label' => 'Default Theme', |
| 128 | * 'extensionId' => 'taoSomething', |
| 129 | * 'id' => 'taoSomethingDefaultTheme' |
| 130 | * ]; |
| 131 | * |
| 132 | * If this contains anything you don't like, just add that key to your $config array to override the default. |
| 133 | * The same applies if something is missing that you would like to have - for these cases generic getter is |
| 134 | * available. |
| 135 | * |
| 136 | * // Full blown custom configuration example |
| 137 | * $options = [ |
| 138 | * 'label' => 'Default Theme', |
| 139 | * 'extensionId' => 'taoSomething', |
| 140 | * 'logoUrl' => 'http://example.com/foo.png', |
| 141 | * 'link' => 'http://example.com', |
| 142 | * 'message' => 'Tao Platform', |
| 143 | * |
| 144 | * // if stylesheet === ConfigurablePlatformTheme::DEFAULT_PATH |
| 145 | * 'stylesheet' => 'http://domain/taoSomething/views/css/themes/platform/default-theme/theme.css', |
| 146 | * // when no stylesheet is given: |
| 147 | * 'stylesheet' => 'http://example.com/tao/views/css/tao-3.css', |
| 148 | * // when stylesheet is any other url: |
| 149 | * 'stylesheet' => 'http://example.com/any-other-url.css', |
| 150 | * |
| 151 | * 'templates' => [ |
| 152 | * 'header-logo' => Template::getTemplate('blocks/header-logo.tpl', 'some-extension'), |
| 153 | * |
| 154 | * // if the value of the template === ConfigurablePlatformTheme::DEFAULT_PATH |
| 155 | * // the default theme path will be used something like: |
| 156 | * // templates/themes/platform/default-theme/login-message.tpl |
| 157 | * 'login-message' => ConfigurablePlatformTheme::DEFAULT_PATH, |
| 158 | * ], |
| 159 | * // array of translatable strings |
| 160 | * 'customTexts' => [ |
| 161 | * 'diagBrowserCheckResult' => 'Your browser %CURRENT_BROWSER% is not compatible.', |
| 162 | * 'diagOsCheckResult' => 'Your Operating System %CURRENT_OS% is not compatible.' |
| 163 | * ], |
| 164 | * 'operatedBy' => [ |
| 165 | * 'email' => 'company@example.com', |
| 166 | * 'name' => 'Big Company' |
| 167 | * ], |
| 168 | * 'whateverCustomStuff' => 'anything as long as the key is in camelCase' |
| 169 | * ]; |
| 170 | * |
| 171 | * @param array $options |
| 172 | * |
| 173 | * @throws \common_exception_MissingParameter |
| 174 | */ |
| 175 | public function __construct(array $options = []) |
| 176 | { |
| 177 | // make sure label and extension id are set |
| 178 | foreach ($this->mandatoryOptions as $required) { |
| 179 | if (empty($options[$required])) { |
| 180 | throw new \common_exception_MissingParameter($required, get_class()); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | $this->setDefaultThemePath($options[static::LABEL]); |
| 185 | |
| 186 | parent::__construct(); |
| 187 | |
| 188 | // set default options |
| 189 | $this->setupOptions($options); |
| 190 | |
| 191 | if ($this->hasOption('customTexts')) { |
| 192 | $this->customTexts = $this->getOption('customTexts'); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | |
| 197 | /** |
| 198 | * Get a template associated from a given $id |
| 199 | * |
| 200 | * @param string $id |
| 201 | * @param string $context |
| 202 | * @return string |
| 203 | */ |
| 204 | public function getTemplate($id, $context = Theme::CONTEXT_BACKOFFICE) |
| 205 | { |
| 206 | if ( |
| 207 | $this->hasOption(self::TEMPLATE_STRATEGY) |
| 208 | && $this->getOption(self::TEMPLATE_STRATEGY) == self::PORTAL_TEMPLATE_STRATEGY |
| 209 | ) { |
| 210 | return $this->getPortalTemplate($id); |
| 211 | } |
| 212 | |
| 213 | return $this->getDefaultemplate($id); |
| 214 | } |
| 215 | |
| 216 | private function getPortalTemplate($id): ?string |
| 217 | { |
| 218 | try { |
| 219 | return $this->findTemplateByIdOrFail($id); |
| 220 | } catch (InvalidArgumentException $exception) { |
| 221 | return $this->getDefaultemplate($id); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * This method is here to handle custom options |
| 227 | * |
| 228 | * @param $method |
| 229 | * @param $arguments |
| 230 | * @return mixed |
| 231 | * @throws \common_exception_NotFound |
| 232 | */ |
| 233 | public function __call($method, $arguments) |
| 234 | { |
| 235 | if (substr($method, 0, 3) !== 'get') { |
| 236 | throw new \common_exception_NotFound('Unknown method "' . $method . '"'); |
| 237 | } |
| 238 | $optionKey = strtolower($method[3]) . substr($method, 4); |
| 239 | if ($this->hasOption($optionKey)) { |
| 240 | return $this->getOption($optionKey); |
| 241 | } |
| 242 | throw new \common_exception_NotFound('Unknown option "' . $optionKey . '"'); |
| 243 | } |
| 244 | |
| 245 | |
| 246 | /** |
| 247 | * Get all options |
| 248 | * |
| 249 | * @return array |
| 250 | */ |
| 251 | public function getThemeData() |
| 252 | { |
| 253 | return $this->getOptions(); |
| 254 | } |
| 255 | |
| 256 | |
| 257 | /** |
| 258 | * Get the url of stylesheet associated to current theme configuration |
| 259 | * |
| 260 | * @param string $context |
| 261 | * @return string |
| 262 | */ |
| 263 | public function getStylesheet($context = Theme::CONTEXT_BACKOFFICE) |
| 264 | { |
| 265 | return $this->getOption(static::STYLESHEET); |
| 266 | } |
| 267 | |
| 268 | |
| 269 | /** |
| 270 | * Get the logo url of current theme |
| 271 | * If not empty, this url is used on the header logo |
| 272 | * |
| 273 | * @return string |
| 274 | */ |
| 275 | public function getLogoUrl() |
| 276 | { |
| 277 | return $this->getOption(static::LOGO_URL); |
| 278 | } |
| 279 | |
| 280 | |
| 281 | /** |
| 282 | * Get the url link of current theme |
| 283 | * URL is used in the header as a link for the logo |
| 284 | * and in the footer for the message |
| 285 | * |
| 286 | * @return string |
| 287 | */ |
| 288 | public function getLink() |
| 289 | { |
| 290 | if ($this->hasOption(static::LINK)) { |
| 291 | return \tao_helpers_Display::encodeAttrValue($this->getOption(static::LINK)); |
| 292 | } |
| 293 | |
| 294 | return ''; |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Get the message of current theme |
| 299 | * Message is used in the header as title of the logo |
| 300 | * Message is used in the footer as footer message |
| 301 | * |
| 302 | * @return string |
| 303 | */ |
| 304 | public function getMessage() |
| 305 | { |
| 306 | if ($this->hasOption(static::MESSAGE)) { |
| 307 | return $this->getOption(static::MESSAGE); |
| 308 | } |
| 309 | |
| 310 | return ''; |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Gets the label of current theme |
| 315 | * Labels are useful in situations where you can choose between multiple themes |
| 316 | * |
| 317 | * @return string |
| 318 | */ |
| 319 | public function getLabel() |
| 320 | { |
| 321 | return $this->getOption(static::LABEL); |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Gets the id of current theme |
| 326 | * IDs are used to register the theme |
| 327 | * |
| 328 | * @return string |
| 329 | */ |
| 330 | public function getId() |
| 331 | { |
| 332 | return $this->getOption(static::ID); |
| 333 | } |
| 334 | |
| 335 | |
| 336 | /** |
| 337 | * Operated by info, make sure both fields contain a string |
| 338 | * |
| 339 | * @return array |
| 340 | */ |
| 341 | public function getOperatedBy() |
| 342 | { |
| 343 | $operatedBy = $this->getOption(static::OPERATED_BY); |
| 344 | $operatedBy['name'] = empty($operatedBy['name']) ? '' : $operatedBy['name']; |
| 345 | $operatedBy['email'] = empty($operatedBy['email']) ? '' : $operatedBy['email']; |
| 346 | return $operatedBy; |
| 347 | } |
| 348 | |
| 349 | |
| 350 | /** |
| 351 | * Construct the common part of the default theme part |
| 352 | * |
| 353 | * @param string $label |
| 354 | */ |
| 355 | protected function setDefaultThemePath($label) |
| 356 | { |
| 357 | $this->defaultThemePath = static::DEFAULT_THEME_PATH . '/' . StringUtils::removeSpecChars($label); |
| 358 | } |
| 359 | |
| 360 | |
| 361 | /** |
| 362 | * Allow to retrieve a custom translatable string for a given key |
| 363 | * |
| 364 | * @param String $key |
| 365 | * @return string |
| 366 | */ |
| 367 | public function getText($key) |
| 368 | { |
| 369 | return (array_key_exists($key, $this->customTexts)) ? $this->customTexts[$key] : ''; |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Retrieve all custom translatable strings for the given keys |
| 374 | * |
| 375 | * @param array $keys |
| 376 | * @return array |
| 377 | */ |
| 378 | public function getTextFromArray(array $keys = []) |
| 379 | { |
| 380 | $values = []; |
| 381 | foreach ($keys as $key) { |
| 382 | $values[$key] = $this->getText($key); |
| 383 | } |
| 384 | return $values; |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Retrieve all existing custom translatable strings |
| 389 | * |
| 390 | * @return array |
| 391 | */ |
| 392 | public function getCustomTexts() |
| 393 | { |
| 394 | return $this->customTexts; |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * This is now just an alias to keep backward compatibility |
| 399 | * |
| 400 | * @return array |
| 401 | */ |
| 402 | public function getAllTexts() |
| 403 | { |
| 404 | return $this->getCustomTexts(); |
| 405 | } |
| 406 | |
| 407 | |
| 408 | /** |
| 409 | * This setup is used when configuring a theme for a custom extension. |
| 410 | * In multi tenancy though the tenant id might be use instead of the extension id. |
| 411 | * |
| 412 | * @param $options |
| 413 | * |
| 414 | * @return bool |
| 415 | */ |
| 416 | protected function setupOptions($options) |
| 417 | { |
| 418 | if (empty($options[static::EXTENSION_ID])) { |
| 419 | $cls = get_class($this); |
| 420 | strtok($cls, '\\'); |
| 421 | $options[static::EXTENSION_ID] = strtok('\\'); |
| 422 | } |
| 423 | $options = array_merge( |
| 424 | [ |
| 425 | static::STYLESHEET => Template::css('tao-3.css', 'tao'), |
| 426 | static::LOGO_URL => Template::img('tao-logo.png', 'tao'), |
| 427 | static::LABEL => $options[static::LABEL], |
| 428 | static::EXTENSION_ID => $options[static::EXTENSION_ID], |
| 429 | static::ID => $options[static::EXTENSION_ID] |
| 430 | . StringUtils::camelize(StringUtils::removeSpecChars($options[static::LABEL]), true) |
| 431 | ], |
| 432 | $options |
| 433 | ); |
| 434 | |
| 435 | if ($options[static::LOGO_URL] === static::DEFAULT_PATH) { |
| 436 | $options[static::LOGO_URL] = Template::img( |
| 437 | $this->defaultThemePath . '/' . static::DEFAULT_LOGO_NAME, |
| 438 | $options[static::EXTENSION_ID] |
| 439 | ); |
| 440 | } |
| 441 | |
| 442 | if ($options[static::STYLESHEET] === static::DEFAULT_PATH) { |
| 443 | $options[static::STYLESHEET] = Template::css( |
| 444 | $this->defaultThemePath . '/' . static::DEFAULT_STYLESHEET_NAME, |
| 445 | $options[static::EXTENSION_ID] |
| 446 | ); |
| 447 | } |
| 448 | |
| 449 | $this->setOptions($options); |
| 450 | |
| 451 | return true; |
| 452 | } |
| 453 | |
| 454 | private function getDefaultemplate(string $id): ?string |
| 455 | { |
| 456 | $templates = $this->getOption(static::TEMPLATES); |
| 457 | |
| 458 | if (is_null($templates) || empty($templates[$id])) { |
| 459 | $path = strpos($id, '.tpl') !== false ? $id : 'blocks/' . $id . '.tpl'; |
| 460 | $templatePath = Template::getTemplate($path, 'tao'); |
| 461 | return file_exists($templatePath) ? $templatePath : null; |
| 462 | } |
| 463 | |
| 464 | if ($templates[$id] === static::DEFAULT_PATH) { |
| 465 | return Template::getTemplate( |
| 466 | $this->defaultThemePath . '/' . $id . '.tpl', |
| 467 | $this->getOption(static::EXTENSION_ID) |
| 468 | ); |
| 469 | } |
| 470 | |
| 471 | // otherwise it will be assumed the template is already configured |
| 472 | return $templates[$id]; |
| 473 | } |
| 474 | } |