Open Monograph Press  3.3.0
PKPThemeForm.inc.php
1 <?php
21 use \PKP\components\forms\FormComponent;
22 use \PKP\components\forms\FieldSelect;
23 
24 define('FORM_THEME', 'theme');
25 
26 class PKPThemeForm extends FormComponent {
28  public $id = FORM_THEME;
29 
31  public $method = 'PUT';
32 
34  public $themeFields = [];
35 
44  public function __construct($action, $locales, $context = null) {
45  $this->action = $action;
46  $this->locales = $locales;
47 
48  if (!empty($context)) {
49  $activeTheme = $context->getData('themePluginPath');
50  $contextId = $context->getId();
51  } else {
52  $activeTheme = \Application::get()->getRequest()->getSite()->getData('themePluginPath');
53  $contextId = CONTEXT_ID_NONE;
54  }
55 
56  $themes = $themeOptions = [];
57  $plugins = \PluginRegistry::loadCategory('themes', true);
58  foreach ($plugins as $plugin) {
59  $themes[] = [
60  'value' => $plugin->getDirName(),
61  'label' => $plugin->getDisplayName(),
62  ];
63  }
64 
65  $this->addField(new FieldSelect('themePluginPath', [
66  'label' => __('manager.setup.theme'),
67  'description' => __('manager.setup.theme.description'),
68  'options' => $themes,
69  'value' => $activeTheme,
70  ]));
71 
72  // Add theme options for each theme
73  foreach ($plugins as $plugin) {
74  // Re-run the init functions for each theme so that any theme options
75  // are set up. Because this is run after PluginRegistry::loadCategory(),
76  // the scripts and styles won't actually be registered against the
77  // template manager. However, if PluginRegistry::loadCategory() is called
78  // again for the themes category, it can cause scripts and styles to be
79  // overwritten by inactive themes.
80  $plugin->init();
81  $themeOptions = $plugin->getOptionsConfig();
82  if (empty($themeOptions)) {
83  continue;
84  }
85  $themeOptionValues = $plugin->getOptionValues($contextId);
86  foreach ($themeOptions as $optionName => $optionField) {
87  $optionField->value = isset($themeOptionValues[$optionName]) ? $themeOptionValues[$optionName] : null;
88  $this->addThemeField($plugin->getDirName(), $optionField);
89  }
90  }
91  }
92 
105  public function addThemeField($theme, $field, $position = []) {
106  if (empty($position)) {
107  if (!isset($this->themeFields[$theme])) {
108  $this->themeFields[$theme] = [];
109  }
110  $this->themeFields[$theme][] = $field;
111  } else {
112  $this->themeFields[$theme] = $this->addToPosition($position[1], $this->themeFields[$theme], $field, $position[0]);
113  }
114  return $this;
115  }
116 
120  public function getConfig() {
121  // Add the active theme's option fields to the fields array
122  $activeThemeField = array_filter($this->fields, function($field) {
123  return $field->name === 'themePluginPath';
124  });
125  $activeTheme = $activeThemeField[0]->value;
126  if (!empty($this->themeFields[$activeTheme])) {
127  $this->fields = array_merge($this->fields, $this->themeFields[$activeTheme]);
128  }
129 
130  $config = parent::getConfig();
131 
132  // Set up field config for non-active fields
133  if (!$this->groups) {
134  $this->addGroup(array('id' => 'default'));
135  $this->fields = array_map(function($field) {
136  $field->groupId = 'default';
137  return $field;
138  }, $this->fields);
139  }
140  $defaultGroupId = $this->groups[0]['id'];
141  $config['themeFields'] = array_map(function($themeOptions) use ($defaultGroupId) {
142  return array_map(function($themeOption) use ($defaultGroupId) {
143  $field = $this->getFieldConfig($themeOption);
144  $field['groupId'] = $defaultGroupId;
145  return $field;
146  }, $themeOptions);
147  }, $this->themeFields);
148 
149  return $config;
150  }
151 }
PKP\components\forms\FormComponent\addGroup
addGroup($args, $position=[])
Definition: FormComponent.inc.php:145
PKP\components\forms\context\PKPThemeForm\__construct
__construct($action, $locales, $context=null)
Definition: PKPThemeForm.inc.php:47
PKP\components\forms\context
Definition: PKPAnnouncementSettingsForm.inc.php:14
PKP\components\forms\context\PKPThemeForm\$themeFields
$themeFields
Definition: PKPThemeForm.inc.php:37
PKP\components\forms\context\PKPThemeForm\$method
$method
Definition: PKPThemeForm.inc.php:31
PKP\components\forms\FormComponent\$fields
$fields
Definition: FormComponent.inc.php:49
PKP\components\forms\FormComponent\getFieldConfig
getFieldConfig($field)
Definition: FormComponent.inc.php:311
PKP\components\forms\FormComponent\$action
$action
Definition: FormComponent.inc.php:37
PKP\components\forms\context\PKPThemeForm\$id
$id
Definition: PKPThemeForm.inc.php:28
PluginRegistry\loadCategory
static loadCategory($category, $enabledOnly=false, $mainContextId=null)
Definition: PluginRegistry.inc.php:103
PKP\components\forms\context\PKPThemeForm
Definition: PKPThemeForm.inc.php:26
PKP\components\forms\FormComponent
Definition: FormComponent.inc.php:20
PKP\components\forms\context\PKPThemeForm\addThemeField
addThemeField($theme, $field, $position=[])
Definition: PKPThemeForm.inc.php:108
PKP\components\forms\context\PKPThemeForm\getConfig
getConfig()
Definition: PKPThemeForm.inc.php:123
PKP\components\forms\FormComponent\addToPosition
addToPosition($id, $list, $item, $position)
Definition: FormComponent.inc.php:227
PKP\components\forms\FormComponent\addField
addField($field, $position=[])
Definition: FormComponent.inc.php:94
PKP\components\forms\FormComponent\$locales
$locales
Definition: FormComponent.inc.php:43
PKP\components\forms\context\FORM_THEME
const FORM_THEME
Definition: PKPThemeForm.inc.php:24
PKPApplication\get
static get()
Definition: PKPApplication.inc.php:235
FieldSelect
A select field in a form.
PKP\components\forms\FieldSelect
Definition: FieldSelect.inc.php:15