1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package content |
4
|
|
|
*/ |
5
|
|
|
/** |
6
|
|
|
* The AjaxEventDocumentation returns the documentation for a particular |
7
|
|
|
* event by invoking all fields to return their documentation. |
8
|
|
|
* Accepts three parameters, `section`, `filters` and `name`. |
9
|
|
|
*/ |
10
|
|
|
class contentAjaxEventDocumentation extends TextPage |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
public function __construct() |
13
|
|
|
{ |
14
|
|
|
parent::__construct(); |
15
|
|
|
$this->addHeaderToPage('Content-Type', 'text/html'); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function view() |
19
|
|
|
{ |
20
|
|
|
$name = General::sanitize($_REQUEST['name']); |
21
|
|
|
$section = General::sanitize($_REQUEST['section']); |
22
|
|
|
$filters = self::processFilters($_REQUEST['filters']); |
23
|
|
|
$rootelement = Lang::createHandle($name); |
24
|
|
|
$doc_parts = array(); |
25
|
|
|
|
26
|
|
|
// Add Documentation (Success/Failure) |
27
|
|
|
$this->addEntrySuccessDoc($doc_parts, $rootelement, $filters); |
28
|
|
|
$this->addEntryFailureDoc($doc_parts, $rootelement, $filters); |
29
|
|
|
|
30
|
|
|
// Filters |
31
|
|
|
$this->addDefaultFiltersDoc($doc_parts, $rootelement, $filters); |
32
|
|
|
|
33
|
|
|
// Frontend Markup |
34
|
|
|
$this->addFrontendMarkupDoc($doc_parts, $rootelement, $section, $filters); |
35
|
|
|
$this->addSendMailFilterDoc($doc_parts, $filters); |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Allows adding documentation for new filters. A reference to the $documentation |
39
|
|
|
* array is provided, along with selected filters |
40
|
|
|
* |
41
|
|
|
* @delegate AppendEventFilterDocumentation |
42
|
|
|
* @param string $context |
43
|
|
|
* '/blueprints/events/(edit|new|info)/' |
44
|
|
|
* @param array $selected |
45
|
|
|
* An array of all the selected filters for this Event |
46
|
|
|
* @param array $documentation |
47
|
|
|
* An array of all the documentation XMLElements, passed by reference |
48
|
|
|
* @param string $rootelment |
49
|
|
|
* The name of this event, as a handle. |
50
|
|
|
*/ |
51
|
|
|
Symphony::ExtensionManager()->notifyMembers('AppendEventFilterDocumentation', '/blueprints/events/', array( |
52
|
|
|
'selected' => $filters, |
53
|
|
|
'documentation' => &$doc_parts, |
54
|
|
|
'rootelement' => $rootelement |
55
|
|
|
)); |
56
|
|
|
|
57
|
|
|
$documentation = join(PHP_EOL, array_map(function($part) { |
|
|
|
|
58
|
|
|
return rtrim($part->generate(true, 4)); |
59
|
|
|
}, $doc_parts)); |
60
|
|
|
$documentation = str_replace('\'', '\\\'', $documentation); |
61
|
|
|
|
62
|
|
|
$documentation = '<fieldset id="event-documentation" class="settings"><legend>' . __('Documentation') . '</legend>' . $documentation . '</fieldset>'; |
63
|
|
|
$this->_Result = $documentation; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Utilities |
68
|
|
|
*/ |
69
|
|
|
public static function hasMultipleFilter($filters) |
70
|
|
|
{ |
71
|
|
|
if (!is_array($filters)) { |
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return in_array('expect-multiple', $filters); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public static function hasSendEmailFilter($filters) |
79
|
|
|
{ |
80
|
|
|
if (!is_array($filters)) { |
81
|
|
|
return false; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return in_array('send-email', $filters); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public static function processFilters($filters) |
88
|
|
|
{ |
89
|
|
|
$filter_names = array(); |
90
|
|
|
|
91
|
|
|
if (is_array($filters) && !empty($filters)) { |
92
|
|
|
foreach ($filters as $filter) { |
93
|
|
|
$filter_names[] = $filter['value']; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $filter_names; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public static function processDocumentationCode($code) |
101
|
|
|
{ |
102
|
|
|
return new XMLElement('pre', '<code>' . str_replace('<', '<', str_replace('&', '&', trim((is_object($code) ? $code->generate(true) : $code)))) . '</code>', array('class' => 'XML')); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function addEntrySuccessDoc(array &$doc_parts, $rootelement, $filters) |
106
|
|
|
{ |
107
|
|
|
$doc_parts[] = new XMLElement('h3', __('Success and Failure XML Examples')); |
108
|
|
|
$doc_parts[] = new XMLElement('p', __('When saved successfully, the following XML will be returned:')); |
109
|
|
|
|
110
|
|
|
if ($this->hasMultipleFilter($filters)) { |
111
|
|
|
$code = new XMLElement($rootelement); |
112
|
|
|
$entry = new XMLElement('entry', null, array('index' => '0', 'result' => 'success', 'type' => 'create | edit')); |
113
|
|
|
$entry->appendChild(new XMLElement('message', __('Entry [created | edited] successfully.'))); |
114
|
|
|
|
115
|
|
|
$code->appendChild($entry); |
116
|
|
|
} else { |
117
|
|
|
$code = new XMLElement($rootelement, null, array('result' => 'success', 'type' => 'create | edit')); |
118
|
|
|
$code->appendChild(new XMLElement('message', __('Entry [created | edited] successfully.'))); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$doc_parts[] = self::processDocumentationCode($code); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function addEntryFailureDoc(array &$doc_parts, $rootelement, $filters) |
125
|
|
|
{ |
126
|
|
|
$doc_parts[] = new XMLElement('p', __('When an error occurs during saving, due to either missing or invalid fields, the following XML will be returned.')); |
127
|
|
|
|
128
|
|
|
if ($this->hasMultipleFilter($filters)) { |
129
|
|
|
$doc_parts[] = new XMLElement('p', __('Notice that it is possible to get mixtures of success and failure messages when using the ‘Allow Multiple’ option.')); |
130
|
|
|
$code = new XMLElement($rootelement); |
131
|
|
|
|
132
|
|
|
$entry = new XMLElement('entry', null, array('index' => '0', 'result' => 'error')); |
133
|
|
|
$entry->appendChild(new XMLElement('message', __('Entry encountered errors when saving.'))); |
134
|
|
|
$entry->appendChild(new XMLElement('field-name', null, array('type' => 'invalid | missing'))); |
135
|
|
|
$code->appendChild($entry); |
136
|
|
|
|
137
|
|
|
$entry = new XMLElement('entry', null, array('index' => '1', 'result' => 'success', 'type' => 'create | edit')); |
138
|
|
|
$entry->appendChild(new XMLElement('message', __('Entry [created | edited] successfully.'))); |
139
|
|
|
$code->appendChild($entry); |
140
|
|
|
} else { |
141
|
|
|
$code = new XMLElement($rootelement, null, array('result' => 'error')); |
142
|
|
|
$code->appendChild(new XMLElement('message', __('Entry encountered errors when saving.'))); |
143
|
|
|
$code->appendChild(new XMLElement('field-name', null, array('type' => 'invalid | missing'))); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$code->setValue('...'); |
147
|
|
|
$doc_parts[] = self::processDocumentationCode($code); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function addDefaultFiltersDoc(array &$doc_parts, $rootelement, $filters) |
151
|
|
|
{ |
152
|
|
|
if (is_array($filters) && !empty($filters)) { |
153
|
|
|
$doc_parts[] = new XMLElement('p', __('The following is an example of what is returned if any options return an error:')); |
154
|
|
|
|
155
|
|
|
$code = new XMLElement($rootelement, null, array('result' => 'error')); |
156
|
|
|
$code->appendChild(new XMLElement('message', __('Entry encountered errors when saving.'))); |
157
|
|
|
$code->appendChild(new XMLElement('filter', null, array('name' => 'admin-only', 'status' => 'failed'))); |
158
|
|
|
$code->appendChild(new XMLElement('filter', __('Recipient not found'), array('name' => 'send-email', 'status' => 'failed'))); |
159
|
|
|
$code->setValue('...'); |
160
|
|
|
|
161
|
|
|
$doc_parts[] = self::processDocumentationCode($code); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function addFrontendMarkupDoc(array &$doc_parts, $rootelement, $section, $filters) |
166
|
|
|
{ |
167
|
|
|
$multiple = $this->hasMultipleFilter($filters); |
168
|
|
|
$doc_parts[] = new XMLElement('h3', __('Example Front-end Form Markup')); |
169
|
|
|
$doc_parts[] = new XMLElement('p', __('This is an example of the form markup you can use on your frontend:')); |
170
|
|
|
$container = new XMLElement('form', null, array('method' => 'post', 'action' => '{$current-url}/', 'enctype' => 'multipart/form-data')); |
171
|
|
|
$container->appendChild(Widget::Input('MAX_FILE_SIZE', (string)min(ini_size_to_bytes(ini_get('upload_max_filesize')), Symphony::Configuration()->get('max_upload_size', 'admin')), 'hidden')); |
172
|
|
|
|
173
|
|
|
if (is_numeric($section)) { |
174
|
|
|
$section = SectionManager::fetch($section); |
175
|
|
|
if ($section instanceof Section) { |
176
|
|
|
$section_fields = $section->fetchFields(); |
177
|
|
|
if (is_array($section_fields) && !empty($section_fields)) { |
178
|
|
|
foreach ($section_fields as $f) { |
179
|
|
|
if ($f->getExampleFormMarkup() instanceof XMLElement) { |
180
|
|
|
$container->appendChild($f->getExampleFormMarkup()); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
$container->appendChild(Widget::Input('action['.$rootelement.']', __('Submit'), 'submit')); |
188
|
|
|
$code = $container->generate(true); |
189
|
|
|
|
190
|
|
|
$doc_parts[] = self::processDocumentationCode(($multiple ? str_replace('fields[', 'fields[0][', $code) : $code)); |
|
|
|
|
191
|
|
|
|
192
|
|
|
$doc_parts[] = new XMLElement('p', __('To edit an existing entry, include the entry ID value of the entry in the form. This is best as a hidden field like so:')); |
193
|
|
|
$doc_parts[] = self::processDocumentationCode(Widget::Input('id' . ($multiple ? '[0]' : null), '23', 'hidden')); |
|
|
|
|
194
|
|
|
|
195
|
|
|
$doc_parts[] = new XMLElement('p', __('To redirect to a different location upon a successful save, include the redirect location in the form. This is best as a hidden field like so, where the value is the URL to redirect to:')); |
196
|
|
|
$doc_parts[] = self::processDocumentationCode(Widget::Input('redirect', URL.'/success/', 'hidden')); |
|
|
|
|
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function addSendMailFilterDoc(array &$doc_parts, $filters) |
200
|
|
|
{ |
201
|
|
|
if ($this->hasSendEmailFilter($filters)) { |
202
|
|
|
$doc_parts[] = new XMLElement('h3', __('Send Notification Email')); |
203
|
|
|
$doc_parts[] = new XMLElement('p', |
204
|
|
|
__('Upon the event successfully saving the entry, this option takes input from the form and send an email to the desired recipient.') |
205
|
|
|
. ' <strong>' |
206
|
|
|
. __('It currently does not work with ‘Allow Multiple’') |
207
|
|
|
. '</strong>. ' |
208
|
|
|
. __('The following are the recognised fields:') |
209
|
|
|
); |
210
|
|
|
|
211
|
|
|
$doc_parts[] = self::processDocumentationCode( |
212
|
|
|
'send-email[sender-email] // '.__('Optional').PHP_EOL. |
213
|
|
|
'send-email[sender-name] // '.__('Optional').PHP_EOL. |
214
|
|
|
'send-email[reply-to-email] // '.__('Optional').PHP_EOL. |
215
|
|
|
'send-email[reply-to-name] // '.__('Optional').PHP_EOL. |
216
|
|
|
'send-email[subject]'.PHP_EOL. |
217
|
|
|
'send-email[body]'.PHP_EOL. |
218
|
|
|
'send-email[recipient] // '.__('list of comma-separated author usernames.')); |
219
|
|
|
|
220
|
|
|
$doc_parts[] = new XMLElement('p', __('All of these fields can be set dynamically using the exact field name of another field in the form as shown below in the example form:')); |
221
|
|
|
$doc_parts[] = self::processDocumentationCode('<form action="" method="post"> |
222
|
|
|
<fieldset> |
223
|
|
|
<label>'.__('Name').' <input type="text" name="fields[author]" value="" /></label> |
224
|
|
|
<label>'.__('Email').' <input type="text" name="fields[email]" value="" /></label> |
225
|
|
|
<label>'.__('Message').' <textarea name="fields[message]" rows="5" cols="21"></textarea></label> |
226
|
|
|
<input name="send-email[sender-email]" value="fields[email]" type="hidden" /> |
227
|
|
|
<input name="send-email[sender-name]" value="fields[author]" type="hidden" /> |
228
|
|
|
<input name="send-email[reply-to-email]" value="fields[email]" type="hidden" /> |
229
|
|
|
<input name="send-email[reply-to-name]" value="fields[author]" type="hidden" /> |
230
|
|
|
<input name="send-email[subject]" value="You are being contacted" type="hidden" /> |
231
|
|
|
<input name="send-email[body]" value="fields[message]" type="hidden" /> |
232
|
|
|
<input name="send-email[recipient]" value="fred" type="hidden" /> |
233
|
|
|
<input id="submit" type="submit" name="action[save-contact-form]" value="Send" /> |
234
|
|
|
</fieldset> |
235
|
|
|
</form>' |
236
|
|
|
); |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
Classes in PHP are usually named in CamelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.
Thus the name database provider becomes
DatabaseProvider
.