This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.driver.php
executable file
·282 lines (255 loc) · 8.36 KB
/
extension.driver.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
/**
* Include Symphony classes.
*/
@require_once(TOOLKIT . '/class.entrymanager.php');
/**
* Include external library for form validation.
*/
@require_once(EXTENSIONS . '/formvalidation/lib/validation.php');
/**
* Form validation extension class
*
* @package formvalidation
* @author Thomas Off - retiolum.de <info@retiolum.de>
**/
class extension_formvalidation extends Extension {
/**
* Set array containing all the data for the 'About' page in the extension administration
*
* @return void
**/
public function about() {
return array(
'name' => 'Form Validation',
'version' => '0.1',
'release-date' => '2009-02-19',
'author' => array(
'name' => 'Thomas Off',
'website' => 'http://www.retiolum.de',
'email' => 'info@retiolum.de',
),
'description' => 'Allows you to add extended validation to your forms.'
);
}
/**
* Return an array containing the delegates that this extension subscribes itself to.
*
* @return array
**/
public function getSubscribedDelegates() {
return array(
array(
'page' => '/blueprints/events/new/',
'delegate' => 'AppendEventFilter',
'callback' => 'addFilterToEventEditor',
),
array(
'page' => '/blueprints/events/edit/',
'delegate' => 'AppendEventFilter',
'callback' => 'addFilterToEventEditor',
),
array(
'page' => '/blueprints/events/new/',
'delegate' => 'AppendEventFilterDocumentation',
'callback' => 'addFilterDocumentationToEvent',
),
array(
'page' => '/blueprints/events/edit/',
'delegate' => 'AppendEventFilterDocumentation',
'callback' => 'addFilterDocumentationToEvent',
),
array(
'page' => '/frontend/',
'delegate' => 'EventPreSaveFilter',
'callback' => 'processEventData',
),
array(
'page' => '/system/preferences/',
'delegate' => 'AddCustomPreferenceFieldsets',
'callback' => 'appendPreferences',
),
);
}
/**
* Add the event filter to the list for creating events.
*
* @param array $context
* @return void
**/
public function addFilterToEventEditor($context) {
$context['options'][] = array(
'formvalidation',
@in_array('formvalidation', $context['selected']) ,
'Form Validation',
);
}
/**
* Add documentation for the filter to the event page.
*
* @param array $context
* @return array
**/
public function addFilterDocumentationToEvent($context) {
// Check whether something should be done at all.
if (!in_array('formvalidation', $context['selected'])) {
return;
}
// Add documentation text to the context.
$context['documentation'][] = new XMLElement('h3', 'Form Validation');
$context['documentation'][] = new XMLElement('p', 'This extension adds the possibility for real form validation to Symphony.');
$context['documentation'][] = new XMLElement('p', 'The form validation is done by Benjamin Keen\'s \'PHP Validation\' script (http://www.benjaminkeen.com/software/php_validation/).');
$context['documentation'][] = new XMLElement('p', 'See the README.txt file shipped with the extension for installation details.');
$context['documentation'][] = new XMLElement('p', 'Upon error the filter returns an XML structure like the following:');
$code = <<<EOT
<filter type="formvalidation" status="failed">
<errors>
<error>The field 'Name' is required.</error>
<error>The field 'Address' is required.</error>
<error>Please enter a valid email address.</error>
</errors>
</filter>
EOT;
$context['documentation'][] = contentBlueprintsEvents::processDocumentationCode($code);
$context['documentation'][] = new XMLElement('p', 'To enable the form validation in your form and choose the proper record containing the validation rules, add the following to your form (assuming that the record has the id 1):');
$code = <<<EOT
<input type="hidden" name="formvalidation[formname]" value="1" />
EOT;
$context['documentation'][] = contentBlueprintsEvents::processDocumentationCode($code);
}
/**
* Process the data from a form when the event is called.
*
* @param array $context
* @return array
**/
public function processEventData($context) {
// Check whether something should be done at all.
if (!in_array('formvalidation', $context['event']->eParamFILTERS)) {
return;
}
// Fetch data for this filter from the form ...
$mapping = $_POST['formvalidation'];
// ... and check it for completeness.
if (!isset($mapping['formname'])) {
$context['messages'][] = array(
'formvalidation',
false,
'The name of the form validation ruleset must be given.',
);
return;
}
// Load the specified ruleset.
$ruleset = $this->fetchRuleset($mapping['formname']);
// Continue only if a ruleset was found.
$errors = array();
if (is_array($ruleset) && !empty($ruleset)) {
// Do the validation using the loaded rules.
$errors = validateFields($context['fields'], $ruleset);
$result = empty($errors);
}
else {
// Validation impossible and thus failed.
$result = false;
}
// Convert the errors into a XML object.
$message = NULL;
if (!$result) {
$message = new XMLElement('errors');
foreach ($errors as $error) {
$message->appendChild(new XMLElement('error', General::sanitize($error)));
}
}
// Return the result.
$context['messages'][] = array(
'formvalidation',
$result,
$message,
);
}
/**
* Fetch the ruleset stored in the specified record in the section and field as set in the preferences.
*
* @param string $entryId
* @return array
*/
protected function fetchRuleset($entryId) {
// Initialize a SectionManager and fetch the section ID.
$sectionManager = new SectionManager($this->_Parent);
$sectionId = $sectionManager->fetchIDFromHandle($this->getSection());
// Initialize an EntryManager and fetch the entry.
$entryManager = new EntryManager($this->_Parent);
$entries = $entryManager->fetch($entryId, $sectionId);
// Check the result.
if (!is_array($entries) || empty($entries)) {
return false;
}
// Initialize a FieldManager and fetch the ruleset.
$fieldManager = new FieldManager($this->_Parent);
$fieldId = $fieldManager->fetchFieldIDFromElementName($this->getField());
$fieldData = $entries[0]->getData($fieldId);
$field = $fieldData['value'];
// Split the ruleset by newlines into an array.
$field = preg_replace('/\r\n/', '\n', $field);
$field = preg_replace('/\r/', '\n', $field);
return explode('\n', $field);
}
/**
* Add a new preferences field for the name of the section where form valiation rulesets are stored.
*
* @param array $context
* @return void
*/
public function appendPreferences($context) {
// Initialize the group.
$group = new XMLElement('fieldset');
$group->setAttribute('class', 'settings');
$group->appendChild(new XMLElement('legend', 'Form Validation'));
// Add the field for the section name.
$label = Widget::Label('Section Name');
$label->appendChild(Widget::Input('settings[formvalidation][section]', General::Sanitize($this->getSection())));
$group->appendChild($label);
// Add the field for the field name.
$label = Widget::Label('Field Name');
$label->appendChild(Widget::Input('settings[formvalidation][field]', General::Sanitize($this->getField())));
$group->appendChild($label);
// Append everything to the wrapper.
$context['wrapper']->appendChild($group);
}
/**
* Get the preference setting for the section name where form validation rulesets are stored in.
*
* @return string
*/
public function getSection() {
if (class_exists('ConfigurationAccessor')) {
return ConfigurationAccessor::get('section', 'formvalidation');
}
return $this->_Parent->Configuration->get('section', 'formvalidation');
}
/**
* Get the preference setting for the name of the field inside the section where form validation rulesets are stored in.
*
* @return string
*/
public function getField() {
if (class_exists('ConfigurationAccessor')) {
return ConfigurationAccessor::get('field', 'formvalidation');
}
return $this->_Parent->Configuration->get('field', 'formvalidation');
}
/**
* Do some uninstall work by removing the set preferences.
*
* @return void
*/
public function uninstall() {
if (class_exists('ConfigurationAccessor')) {
ConfigurationAccessor::remove('formvalidation');
}
else {
$this->_Parent->Configuration->remove('formvalidation');
}
$this->_Parent->saveConfig();
}
}