16 import(
'lib.pkp.classes.plugins.LazyLoadPlugin');
18 define(
'LESS_FILENAME_SUFFIX',
'.less');
19 define(
'THEME_OPTION_PREFIX',
'themeOption_');
72 function register($category, $path, $mainContextId =
null) {
73 if (!parent::register($category, $path, $mainContextId))
return false;
102 if (defined(
'SESSION_DISABLE_INIT')) {
116 public abstract function init();
125 $this->_registerTemplates();
126 $this->_registerStyles();
140 if (defined(
'SESSION_DISABLE_INIT'))
return false;
143 if (is_a($context,
'Context')) {
144 $activeTheme = $context->getData(
'themePluginPath');
147 $activeTheme = $site->getData(
'themePluginPath');
173 public function addStyle($name, $style, $args = array()) {
176 if (substr($style, (strlen(LESS_FILENAME_SUFFIX) * -1)) === LESS_FILENAME_SUFFIX) {
180 } elseif (empty($args[
'inline'])) {
181 if (isset($args[
'baseUrl'])) {
182 $args[
'style'] = $args[
'baseUrl'] . $style;
189 $args[
'style'] = $style;
194 if (isset($args[
'addLess'])) {
195 foreach ($args[
'addLess'] as &$file) {
200 $this->styles[$name] = $args;
211 public function modifyStyle($name, $args = array()) {
219 if (isset($args[
'addLess'])) {
220 foreach ($args[
'addLess'] as &$file) {
225 if (isset($args[
'style']) && !isset($args[
'inline'])) {
226 $args[
'style'] = substr($args[
'style'], (strlen(LESS_FILENAME_SUFFIX) * -1)) == LESS_FILENAME_SUFFIX ? $this->
_getBaseDir($args[
'style']) : $this->
_getBaseUrl($args[
'style']);
229 $style = array_merge_recursive($style, $args);
240 if (isset($this->styles[$name])) {
241 unset($this->styles[$name]);
245 return $this->parent ? $this->parent->removeStyle($name) :
false;
257 if (isset($this->styles[$name])) {
258 $style = &$this->styles[$name];
263 if (!isset($this->parent)) {
268 return $this->parent->getStyle($name);
286 public function addScript($name, $script, $args = array()) {
288 if (!empty($args[
'inline'])) {
289 $args[
'script'] = $script;
290 } elseif (isset($args[
'baseUrl'])) {
291 $args[
'script'] = $args[
'baseUrl'] . $script;
296 $this->scripts[$name] = $args;
311 if (empty($script)) {
315 if (isset($args[
'path'])) {
319 $script = array_merge( $script, $args );
330 if (isset($this->scripts[$name])) {
331 unset($this->scripts[$name]);
335 return $this->parent ? $this->parent->removeScript($name) :
false;
347 if (isset($this->scripts[$name])) {
348 $style = &$this->scripts[$name];
353 if (!isset($this->parent)) {
357 return $this->parent->getScript($name);
375 public function addOption($name, $type, $args = array()) {
377 if (!empty($this->options[$name])) {
382 if (in_array($type, [
'text',
'colour',
'radio'])) {
383 if (isset($args[
'label'])) {
384 $args[
'label'] = __($args[
'label']);
386 if (isset($args[
'description'])) {
387 $args[
'description'] = __($args[
'description']);
394 $type =
'FieldColor';
397 $type =
'FieldOptions';
398 $args[
'type'] =
'radio';
399 if (!empty($args[
'options'])) {
401 foreach ($args[
'options'] as $optionValue => $optionLabel) {
402 $options[] = [
'value' => $optionValue,
'label' => __($optionLabel)];
410 $class =
'PKP\components\forms\\' . $type;
412 $this->options[$name] =
new $class($name, $args);
413 }
catch (Exception $e) {
414 $class =
'APP\components\forms\\' . $type;
416 $this->options[$name] =
new $class($name, $args);
417 }
catch (Exception $e) {
418 throw new Exception(sprintf(
419 'The %s class was not found for the theme option, %s, defined by %s or one of its parent themes.',
439 if (!isset($this->options[$name])) {
440 return $this->parent ? $this->parent->getOption($name) :
false;
444 if (is_null($this->_optionValues)) {
446 $contextId = $context ? $context->getId() : CONTEXT_ID_NONE;
450 if (isset($this->_optionValues[$name])) {
451 return $this->_optionValues[$name];
455 if (isset($this->options[$name])) {
456 $option = $this->options[$name];
457 } elseif ($this->parent) {
458 $option = $this->parent->getOption($name);
460 return isset($option->default) ? $option->default :
null;
475 if (isset($this->options[$name])) {
476 return $this->options[$name];
479 return $this->parent ? $this->parent->getOptionConfig($name) :
false;
492 if (!$this->parent) {
497 $this->parent->getOptionsConfig(),
514 foreach ($args as $key => $value) {
515 if (property_exists($option, $key)) {
516 $option->{$key} = $value;
527 public function removeOption($name) {
529 if (isset($this->options[$name])) {
530 unset($this->options[$name]);
534 return $this->parent ? $this->parent->removeOption($name) :
false;
546 public function getOptionValues($contextId) {
551 $values = $pluginSettingsDAO->getPluginSettings($contextId, $this->getName());
552 foreach ($this->options as $optionName => $optionConfig) {
553 $value = isset($values[$optionName]) ? $values[$optionName] :
null;
555 if (!is_null($optionConfig->default)) {
556 switch (gettype($optionConfig->default)) {
558 $value = !$value || $value ===
'false' ? false :
true;
561 $value = (int) $value;
564 $value = $value ===
null ? [] : unserialize($value);
568 $return[$optionName] = $value;
571 if (!$this->parent) {
576 $this->parent->getOptionValues($contextId),
601 public function validateOptions($options, $themePluginPath, $contextId, $request) {
613 public function saveOption($name, $value, $contextId =
null) {
615 $option = !empty($this->options[$name]) ? $this->options[$name] :
null;
617 if (is_null($option)) {
618 return $this->parent ? $this->parent->saveOption($name, $value, $contextId) :
false;
621 if (is_null($contextId)) {
623 $contextId = $context->getId();
630 $pluginSettingsDao->deleteSetting($contextId, $this->getName(), $name);
632 $type = $pluginSettingsDao->getType($value);
633 $value = $pluginSettingsDao->convertToDb($value, $type);
634 $this->updateSetting($contextId, $name, $value, $type);
644 public function addMenuArea($menuAreas) {
646 if (!is_array($menuAreas)) {
647 $menuAreas = array($menuAreas);
650 $this->menuAreas = array_merge($this->menuAreas, $menuAreas);
659 public function removeMenuArea($menuArea) {
661 $index = array_search($menuArea, $this->menuAreas);
662 if ($index !==
false) {
663 array_splice($this->menuAreas, $index, 1);
667 return $this->parent ? $this->parent->removeMenuArea($menuArea) :
false;
676 public function getMenuAreas($existingAreas = array()) {
678 $existingAreas = array_unique(array_merge($this->menuAreas, $existingAreas));
680 return $this->parent ? $this->parent->getMenuAreas($existingAreas) : $existingAreas;
689 public function setParent($parent) {
692 if (!is_a($parent,
'ThemePlugin')) {
696 $this->parent = $parent;
697 $this->parent->init();
705 private function _registerTemplates() {
708 if (isset($this->parent) && is_a($this->parent,
'ThemePlugin')) {
709 $this->parent->_registerTemplates();
715 $templateManager->addTemplateDir($this->_getBaseDir(
'templates'));
725 private function _registerStyles() {
727 if (isset($this->parent)) {
728 $this->parent->_registerStyles();
732 $dispatcher = $request->getDispatcher();
735 foreach($this->styles as $name => $data) {
737 if (empty($data[
'style'])) {
742 if ($dispatcher && substr($data[
'style'], (strlen(LESS_FILENAME_SUFFIX) * -1)) == LESS_FILENAME_SUFFIX) {
743 $styles = $dispatcher->url(
755 $styles = $data[
'style'];
758 unset($data[
'style']);
760 $templateManager->addStylesheet($name, $styles, $data);
771 public function _registerScripts() {
773 if (isset($this->parent)) {
774 $this->parent->_registerScripts();
780 foreach($this->scripts as $name => $data) {
781 $script = $data[
'script'];
782 unset($data[
'script']);
783 $templateManager->addJavaScript($name, $script, $data);
797 public function _getBaseUrl($path =
'') {
799 $path = empty($path) ?
'' : DIRECTORY_SEPARATOR . $path;
800 return $request->getBaseUrl() . DIRECTORY_SEPARATOR . $this->getPluginPath() . $path;
809 public function _getBaseDir($path =
'') {
810 $path = empty($path) ?
'' : DIRECTORY_SEPARATOR . $path;
811 return Core::getBaseDir() . DIRECTORY_SEPARATOR . $this->getPluginPath() . $path;
827 public function isColourDark($color, $limit = 130) {
828 $color = str_replace(
'#',
'', $color);
829 $r = hexdec(substr($color, 0, 2));
830 $g = hexdec(substr($color, 2, 2));
831 $b = hexdec(substr($color, 4, 2));
837 return $contrast < $limit;