-
Notifications
You must be signed in to change notification settings - Fork 3
/
PlumAnalyticsSettingsForm.php
134 lines (114 loc) · 4.36 KB
/
PlumAnalyticsSettingsForm.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
/**
* @file plugins/generic/plumAnalytics/PlumAnalyticsSettingsForm.inc.php
*
* Copyright (c) 2018 University of Pittsburgh
* Distributed under the GNU GPL v2 or later. For full terms see the file docs/COPYING.
*
* @class PlumAnalyticsSettingsForm
* @ingroup plugins_generic_plumAnalytics
*
* @brief Form for journal managers to modify Plum Analytics plugin settings
*/
namespace APP\plugins\generic\plumAnalytics;
use PKP\form\Form;
use PKP\form\validation\FormValidator;
use PKP\form\validation\FormValidatorPost;
use APP\template\TemplateManager;
class PlumAnalyticsSettingsForm extends Form {
/** @var $contextId int */
var $_contextId;
/** @var $plugin PlumAnalyticsPlugin */
var $_plugin;
/** @var $widgetTypes array() hash of valid widget type options, for use in the form select element */
var $widgetTypes;
/** @var $options array() hash of valid widget settings options */
var $options;
/** @var $options array() convenience variable for each keyname for retrieving settings */
var $settingsKeys;
/**
* Constructor
* @param $plugin PlumAnalyticsPlugin
* @param $contextId int
*/
function __construct($plugin, $contextId) {
$this->_contextId = $contextId;
$this->_plugin = $plugin;
// Set options for widgetTypes, and setup convenience variable for settings iterators
$this->widgetTypes = array();
$this->settingsKeys = array();
$this->widgetTypes[''] = '';
foreach ($plugin->settingsByWidgetType as $k => $v) {
$this->widgetTypes[$k] = 'plugins.generic.plumAnalytics.manager.settings.widgetType.'.$k;
$this->settingsKeys = array_merge($this->settingsKeys, $v);
}
unset($this->widgetTypes['_all']);
$this->settingsKeys = array_unique($this->settingsKeys);
// Set options for popup alignment
$this->options = array();
foreach ($plugin->valuesByWidgetSetting as $k => $v) {
$this->options[$k] = array_merge(array('' => ''), $v);
}
parent::__construct(method_exists($plugin, 'getTemplateResource') ? $plugin->getTemplateResource('settingsForm.tpl') : $plugin->getTemplatePath() . 'settingsForm.tpl');
$this->addCheck(new FormValidator($this, 'plumWidgetType', FORM_VALIDATOR_REQUIRED_VALUE, 'plugins.generic.plumAnalytics.manager.settings.widgetTypeRequired'));
$this->addCheck(new FormValidator($this, 'plumHook', FORM_VALIDATOR_REQUIRED_VALUE, 'plugins.generic.plumAnalytics.manager.settings.hookRequired'));
$this->addCheck(new FormValidatorPost($this));
}
/**
* Initialize form data.
*/
function initData() {
$contextId = $this->_contextId;
$plugin =& $this->_plugin;
parent::initData();
foreach ($this->settingsKeys as $k) {
// database setting is stored without "plum" prefix, e.g. plumSettingName is settingName
$this->setData($k, $plugin->getSetting($contextId, lcfirst(substr($k, 4))));
}
}
/**
* Assign form data to user-submitted data.
*/
function readInputData() {
$this->readUserVars($this->settingsKeys);
}
/**
* Fetch the form.
* @copydoc Form::fetch()
*/
function fetch($request, $template = NULL, $display = false) {
$templateMgr = TemplateManager::getManager($request);
$templateMgr->assign('pluginName', $this->_plugin->getName());
// This assigns select options
foreach ($this->options as $k => $v) {
$templateMgr->assign($k.'Types', $v);
}
$templateMgr->assign('plumWidgetTypes', $this->widgetTypes);
$templateMgr->assign('plumAllWidgetSettings', $this->settingsKeys);
$hideSettings = array('' => array());
foreach ($this->_plugin->settingsByWidgetType as $k => $v) {
$hideSettings[$k] = array_diff($this->settingsKeys, array_merge($this->_plugin->settingsByWidgetType[$k], $this->_plugin->settingsByWidgetType['_all']));
}
$templateMgr->assign('plumWidgetHideSettings', $hideSettings);
return parent::fetch($request, $template = NULL, $display = false);
}
/**
* Save settings.
*/
function execute(...$functionArgs) {
$plugin =& $this->_plugin;
$contextId = $this->_contextId;
foreach ($this->settingsKeys as $k) {
$saveData = $this->getData($k);
$saveType = 'string';
// special handling of checkboxes
if (in_array($k, array('plumHideWhenEmpty', 'plumHidePrint', 'plumBorder'))) {
$saveType = 'bool';
$saveData = boolval($saveData);
}
// database setting is stored without "plum" prefix, e.g. plumSettingName is settingName
$plugin->updateSetting($contextId, lcfirst(substr($k, 4)), $saveData, $saveType);
}
}
}
?>