PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/symphony/content/content.ajaxeventdocumentation.php

http://github.com/symphonycms/symphony-2
PHP | 239 lines | 175 code | 36 blank | 28 comment | 16 complexity | 6c1d11612e9ad47ee08e7a680d5f60af MD5 | raw file
  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. public function view()
  18. {
  19. $name = General::sanitize($_REQUEST['name']);
  20. $section = General::sanitize($_REQUEST['section']);
  21. $filters = self::processFilters($_REQUEST['filters']);
  22. $rootelement = Lang::createHandle($name);
  23. $doc_parts = array();
  24. // Add Documentation (Success/Failure)
  25. $this->addEntrySuccessDoc($doc_parts, $rootelement, $filters);
  26. $this->addEntryFailureDoc($doc_parts, $rootelement, $filters);
  27. // Filters
  28. $this->addDefaultFiltersDoc($doc_parts, $rootelement, $filters);
  29. // Frontend Markup
  30. $this->addFrontendMarkupDoc($doc_parts, $rootelement, $section, $filters);
  31. $this->addSendMailFilterDoc($doc_parts, $filters);
  32. /**
  33. * Allows adding documentation for new filters. A reference to the $documentation
  34. * array is provided, along with selected filters
  35. *
  36. * @delegate AppendEventFilterDocumentation
  37. * @param string $context
  38. * '/blueprints/events/(edit|new|info)/'
  39. * @param array $selected
  40. * An array of all the selected filters for this Event
  41. * @param array $documentation
  42. * An array of all the documentation XMLElements, passed by reference
  43. * @param string $rootelment
  44. * The name of this event, as a handle.
  45. */
  46. Symphony::ExtensionManager()->notifyMembers('AppendEventFilterDocumentation', '/blueprints/events/', array(
  47. 'selected' => $filters,
  48. 'documentation' => &$doc_parts,
  49. 'rootelement' => $rootelement
  50. ));
  51. $documentation = join(PHP_EOL, array_map(function($part) {
  52. return rtrim($part->generate(true, 4));
  53. }, $doc_parts));
  54. $documentation = str_replace('\'', '\\\'', $documentation);
  55. $documentation = '<fieldset id="event-documentation" class="settings"><legend>' . __('Documentation') . '</legend>' . $documentation . '</fieldset>';
  56. $this->_Result = $documentation;
  57. }
  58. /**
  59. * Utilities
  60. */
  61. public static function hasMultipleFilter($filters)
  62. {
  63. if (!is_array($filters)) {
  64. return false;
  65. }
  66. return in_array('expect-multiple', $filters);
  67. }
  68. public static function hasSendEmailFilter($filters)
  69. {
  70. if (!is_array($filters)) {
  71. return false;
  72. }
  73. return in_array('send-email', $filters);
  74. }
  75. public static function processFilters($filters)
  76. {
  77. $filter_names = array();
  78. if (is_array($filters) && !empty($filters)) {
  79. foreach ($filters as $filter) {
  80. $filter_names[] = $filter['value'];
  81. }
  82. }
  83. return $filter_names;
  84. }
  85. public static function processDocumentationCode($code)
  86. {
  87. return new XMLElement('pre', '<code>' . str_replace('<', '&lt;', str_replace('&', '&amp;', trim((is_object($code) ? $code->generate(true) : $code)))) . '</code>', array('class' => 'XML'));
  88. }
  89. public function addEntrySuccessDoc(array &$doc_parts, $rootelement, $filters)
  90. {
  91. $doc_parts[] = new XMLElement('h3', __('Success and Failure XML Examples'));
  92. $doc_parts[] = new XMLElement('p', __('When saved successfully, the following XML will be returned:'));
  93. if ($this->hasMultipleFilter($filters)) {
  94. $code = new XMLElement($rootelement);
  95. $entry = new XMLElement('entry', null, array('index' => '0', 'result' => 'success', 'type' => 'create | edit'));
  96. $entry->appendChild(new XMLElement('message', __('Entry [created | edited] successfully.')));
  97. $code->appendChild($entry);
  98. } else {
  99. $code = new XMLElement($rootelement, null, array('result' => 'success', 'type' => 'create | edit'));
  100. $code->appendChild(new XMLElement('message', __('Entry [created | edited] successfully.')));
  101. }
  102. $doc_parts[] = self::processDocumentationCode($code);
  103. }
  104. public function addEntryFailureDoc(array &$doc_parts, $rootelement, $filters)
  105. {
  106. $doc_parts[] = new XMLElement('p', __('When an error occurs during saving, due to either missing or invalid fields, the following XML will be returned.'));
  107. if ($this->hasMultipleFilter($filters)) {
  108. $doc_parts[] = new XMLElement('p', __('Notice that it is possible to get mixtures of success and failure messages when using the ‘Allow Multiple’ option.'));
  109. $code = new XMLElement($rootelement);
  110. $entry = new XMLElement('entry', null, array('index' => '0', 'result' => 'error'));
  111. $entry->appendChild(new XMLElement('message', __('Entry encountered errors when saving.')));
  112. $entry->appendChild(new XMLElement('field-name', null, array('type' => 'invalid | missing')));
  113. $code->appendChild($entry);
  114. $entry = new XMLElement('entry', null, array('index' => '1', 'result' => 'success', 'type' => 'create | edit'));
  115. $entry->appendChild(new XMLElement('message', __('Entry [created | edited] successfully.')));
  116. $code->appendChild($entry);
  117. } else {
  118. $code = new XMLElement($rootelement, null, array('result' => 'error'));
  119. $code->appendChild(new XMLElement('message', __('Entry encountered errors when saving.')));
  120. $code->appendChild(new XMLElement('field-name', null, array('type' => 'invalid | missing')));
  121. }
  122. $code->setValue("\t...\n");
  123. $doc_parts[] = self::processDocumentationCode($code);
  124. }
  125. public function addDefaultFiltersDoc(array &$doc_parts, $rootelement, $filters)
  126. {
  127. if (is_array($filters) && !empty($filters)) {
  128. $doc_parts[] = new XMLElement('p', __('The following is an example of what is returned if any options return an error:'));
  129. $code = new XMLElement($rootelement, null, array('result' => 'error'));
  130. $code->appendChild(new XMLElement('message', __('Entry encountered errors when saving.')));
  131. $code->appendChild(new XMLElement('filter', null, array('name' => 'admin-only', 'status' => 'failed')));
  132. $code->appendChild(new XMLElement('filter', __('Recipient not found'), array('name' => 'send-email', 'status' => 'failed')));
  133. $code->setValue("\t...\n");
  134. $doc_parts[] = self::processDocumentationCode($code);
  135. }
  136. }
  137. public function addFrontendMarkupDoc(array &$doc_parts, $rootelement, $section, $filters)
  138. {
  139. $multiple = $this->hasMultipleFilter($filters);
  140. $doc_parts[] = new XMLElement('h3', __('Example Front-end Form Markup'));
  141. $doc_parts[] = new XMLElement('p', __('This is an example of the form markup you can use on your frontend:'));
  142. $container = new XMLElement('form', null, array('method' => 'post', 'action' => '{$current-url}/', 'enctype' => 'multipart/form-data'));
  143. $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'));
  144. if (is_numeric($section)) {
  145. $section = (new SectionManager)->select()->section($section)->execute()->next();
  146. if ($section) {
  147. $section_fields = $section->fetchFields();
  148. if (is_array($section_fields) && !empty($section_fields)) {
  149. foreach ($section_fields as $f) {
  150. if ($f->getExampleFormMarkup() instanceof XMLElement) {
  151. $container->appendChild($f->getExampleFormMarkup());
  152. }
  153. }
  154. }
  155. }
  156. }
  157. $container->appendChild(Widget::Input('action['.$rootelement.']', __('Submit'), 'submit'));
  158. $code = $container->generate(true);
  159. $doc_parts[] = self::processDocumentationCode(($multiple ? str_replace('fields[', 'fields[0][', $code) : $code));
  160. $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:'));
  161. $doc_parts[] = self::processDocumentationCode(Widget::Input('id' . ($multiple ? '[0]' : null), '23', 'hidden'));
  162. $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:'));
  163. $doc_parts[] = self::processDocumentationCode(Widget::Input('redirect', URL.'/success/', 'hidden'));
  164. }
  165. public function addSendMailFilterDoc(array &$doc_parts, $filters)
  166. {
  167. if ($this->hasSendEmailFilter($filters)) {
  168. $doc_parts[] = new XMLElement('h3', __('Send Notification Email'));
  169. $doc_parts[] = new XMLElement('p',
  170. __('Upon the event successfully saving the entry, this option takes input from the form and send an email to the desired recipient.')
  171. . ' <strong>'
  172. . __('It currently does not work with ‘Allow Multiple’')
  173. . '</strong>. '
  174. . __('The following are the recognised fields:')
  175. );
  176. $doc_parts[] = self::processDocumentationCode(
  177. 'send-email[sender-email] // '.__('Optional').PHP_EOL.
  178. 'send-email[sender-name] // '.__('Optional').PHP_EOL.
  179. 'send-email[reply-to-email] // '.__('Optional').PHP_EOL.
  180. 'send-email[reply-to-name] // '.__('Optional').PHP_EOL.
  181. 'send-email[subject]'.PHP_EOL.
  182. 'send-email[body]'.PHP_EOL.
  183. 'send-email[recipient] // '.__('list of comma-separated author usernames.'));
  184. $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:'));
  185. $doc_parts[] = self::processDocumentationCode('<form action="" method="post">
  186. <fieldset>
  187. <label>'.__('Name').' <input type="text" name="fields[author]" value="" /></label>
  188. <label>'.__('Email').' <input type="text" name="fields[email]" value="" /></label>
  189. <label>'.__('Message').' <textarea name="fields[message]" rows="5" cols="21"></textarea></label>
  190. <input name="send-email[sender-email]" value="fields[email]" type="hidden" />
  191. <input name="send-email[sender-name]" value="fields[author]" type="hidden" />
  192. <input name="send-email[reply-to-email]" value="fields[email]" type="hidden" />
  193. <input name="send-email[reply-to-name]" value="fields[author]" type="hidden" />
  194. <input name="send-email[subject]" value="You are being contacted" type="hidden" />
  195. <input name="send-email[body]" value="fields[message]" type="hidden" />
  196. <input name="send-email[recipient]" value="fred" type="hidden" />
  197. <input id="submit" type="submit" name="action[save-contact-form]" value="Send" />
  198. </fieldset>
  199. </form>'
  200. );
  201. }
  202. }
  203. }