-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathSettings.php
230 lines (199 loc) · 7.47 KB
/
Settings.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
/*-------------------------------------------------------+
| SYSTOPIA - MORE GREETINGS EXTENSION |
| Copyright (C) 2017 SYSTOPIA |
| Author: B. Endres ([email protected]) |
| P. Batroff ([email protected]) |
| http://www.systopia.de/ |
+--------------------------------------------------------+
| This program is released as free software under the |
| Affero GPL license. You can redistribute it and/or |
| modify it under the terms of this license which you |
| can read by viewing the included agpl.txt or online |
| at www.gnu.org/licenses/agpl.html. Removal of this |
| copyright header is strictly prohibited without |
| written permission from the original author(s). |
+--------------------------------------------------------*/
/**
* Form controller class
*
* @see https://wiki.civicrm.org/confluence/display/CRMDOC/QuickForm+Reference
*/
class CRM_Moregreetings_Form_Settings extends CRM_Core_Form {
/**
* @var string | array | NULL
* Used to store the original error handler, which will be temporarily
* replaced for identifying smarty errors when saving the MoreGreetings
* configuration form.
*/
protected static $_original_error_handler = NULL;
public function buildQuickForm() {
$this->registerRule('is_valid_smarty', 'callback', 'validateSmarty', 'CRM_Moregreetings_Form_Settings');
$this->registerRule('is_valid_field_count', 'callback', 'validateFieldCount', 'CRM_Moregreetings_Form_Settings');
$this->add(
'text',
"greeting_count",
ts("Number of fields", array('domain' => 'de.systopia.moregreetings'))
);
$this->addRule("greeting_count",
ts('Please enter a number between 1 and %1', array(
1 => CRM_Moregreetings_Config::getMaxActiveFieldCount(),
'domain' => 'de.systopia.moregreetings')),
'is_valid_field_count');
$this->assign('greetings_count', range(1,self::getNumberOfGreetings()));
for ($i = 1; $i <= self::getNumberOfGreetings(); ++$i) {
$fields = CRM_Moregreetings_Config::getFields();
$field_id = array_search('greeting_field_'.$i, array_column($fields, 'name', 'id'));
$this->add(
'textarea', // field type
"greeting_smarty_{$i}", // field name
$fields[$field_id]['label'] . ' (' . ts("Greeting %1", array(1 => $i, 'domain' => 'de.systopia.moregreetings')) . ')', // field label
array('rows' => 4,
'cols' => 50,
), // list of options
FALSE // is required
);
$this->addRule("greeting_smarty_{$i}",
ts('Please enter valid smarty code.', array('domain' => 'de.systopia.moregreetings')),
'is_valid_smarty'
);
}
// add form elements
$this->addButtons(array(
array(
'type' => 'submit',
'name' => ts('Save', array('domain' => 'de.systopia.moregreetings')),
'isDefault' => TRUE,
),
array(
'type' => 'upload',
'name' => ts('Save & Apply to all Contacts', array('domain' => 'de.systopia.moregreetings')),
'isDefault' => FALSE,
),
));
// add link
$group_id = CRM_Moregreetings_Config::getGroupID();
$group_url = CRM_Utils_System::url('civicrm/admin/custom/group/field', "reset=1&action=browse&gid={$group_id}");
$this->assign('group_url', $group_url);
// export form elements
parent::buildQuickForm();
}
/**
* set the default (=current) values in the form
*/
public function setDefaultValues() {
$values = Civi::settings()->get('moregreetings_templates');
if (!is_array($values)) {
$values = array();
}
$values['greeting_count'] = self::getNumberOfGreetings();
return $values;
}
/**
* POST PROCESS: Store the new values
*/
public function postProcess() {
$values = $this->exportValues();
// first: update the greetings
$old_greetings = Civi::settings()->get('moregreetings_templates');
$greetings_changed = FALSE;
for ($i = 1; $i <= self::getNumberOfGreetings(); ++$i) {
if (isset($values["greeting_smarty_{$i}"])) {
$values_array["greeting_smarty_{$i}"] = $values["greeting_smarty_{$i}"];
} else {
$values_array["greeting_smarty_{$i}"] = "";
}
// check if it changed
if (CRM_Utils_Array::value("greeting_smarty_{$i}", $old_greetings) != $values_array["greeting_smarty_{$i}"]) {
$greetings_changed = TRUE;
}
}
Civi::settings()->set('moregreetings_templates', $values_array);
// then: adjust the greeting count
if ($values['greeting_count'] != self::getNumberOfGreetings()) {
CRM_Moregreetings_Config::setActiveFieldCount($values['greeting_count']);
// reload b/c the form has already been generated
$url = CRM_Utils_System::url('civicrm/admin/setting/moregreetings', "reset=1");
CRM_Utils_System::redirect($url);
}
if (isset($values['_qf_Settings_upload'])) {
// somebody pressed the SAVE & APPLY button:
CRM_Moregreetings_Job::launchApplicationRunner(); // doesn't return
}
parent::postProcess();
}
/**
* Form validation rule
*/
public static function validateSmarty($smartyValue) {
if ($smartyValue === "") {
return TRUE;
}
try {
$smarty = CRM_Core_Smarty::singleton();
CRM_Utils_Smarty::registerCustomFunctions($smarty);
// Smarty uses trigger_error() to indicate Smarty errors. In order to
// fetch those, replace the current error handler with a custom one, which
// will throw an exception, that will be caught here. Store as a static
// class member in order to access it within the custom error handler.
static::$_original_error_handler = set_error_handler(array(get_class(), 'smartyErrorHandler'));
// Try the rendering.
try {
$renderOut = $smarty->fetch('string:' . $smartyValue);
} catch (ErrorException $exception) {
// Coming from the custom error handler.
$renderOut = FALSE;
}
if (!is_string($renderOut)) {
return FALSE;
}
} catch (Exception $e) {
return FALSE;
}
return TRUE;
}
/**
* Error handler that throws an exception, that can be caught and Smarty
* errors be identified.
*
* @param $errNo
* @param $errStr
* @param $errFile
* @param $errLine
*
* @throws \ErrorException
*/
public static function smartyErrorHandler($errNo, $errStr, $errFile, $errLine, $errContext = []) {
// Call the original error handler with the original error parameters. This
// makes sure the error still gets printed or logged or whatever the
// original error handler is supposed to do with it.
call_user_func(
static::$_original_error_handler,
$errNo,
$errStr,
$errFile,
$errLine,
$errContext
);
// Restore the original error handler for subsequent error handling.
restore_error_handler();
if (strpos($errStr, 'Smarty error:') === 0) {
throw new ErrorException(
$errStr,
$errNo,
1,
$errFile,
$errLine
);
}
}
/**
* Form validation rule
*/
public static function validateFieldCount($field_count) {
return ($field_count > 0 && $field_count <= CRM_Moregreetings_Config::getMaxActiveFieldCount());
}
public static function getNumberOfGreetings() {
return CRM_Moregreetings_Config::getActiveFieldCount();
}
}