update.class.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /*
  3. * This file is part of MODX Revolution.
  4. *
  5. * Copyright (c) MODX, LLC. All Rights Reserved.
  6. *
  7. * For complete copyright and license information, see the COPYRIGHT and LICENSE
  8. * files found in the top-level directory of this distribution.
  9. */
  10. /**
  11. * Load update chunk page
  12. *
  13. * @package modx
  14. * @subpackage manager.controllers
  15. */
  16. class ElementChunkUpdateManagerController extends modManagerController {
  17. /** @var string $onChunkFormRender */
  18. public $onChunkFormRender = '';
  19. /** @var string $onChunkForPrerender */
  20. public $onChunkFormPrerender = '';
  21. /** @var modChunk $chunk */
  22. public $chunk;
  23. /** @var array $chunkArray */
  24. public $chunkArray = array();
  25. /**
  26. * Check for any permissions or requirements to load page
  27. * @return bool
  28. */
  29. public function checkPermissions() {
  30. return $this->modx->hasPermission('edit_chunk');
  31. }
  32. /**
  33. * Register custom CSS/JS for the page
  34. * @return void
  35. */
  36. public function loadCustomCssJs() {
  37. $mgrUrl = $this->modx->getOption('manager_url',null,MODX_MANAGER_URL);
  38. $this->addJavascript($mgrUrl.'assets/modext/widgets/core/modx.grid.local.property.js');
  39. $this->addJavascript($mgrUrl.'assets/modext/widgets/element/modx.grid.element.properties.js');
  40. $this->addJavascript($mgrUrl.'assets/modext/widgets/element/modx.panel.chunk.js');
  41. $this->addJavascript($mgrUrl.'assets/modext/sections/element/chunk/update.js');
  42. $this->addHtml('<script type="text/javascript">
  43. // <![CDATA[
  44. Ext.onReady(function() {
  45. MODx.load({
  46. xtype: "modx-page-chunk-update"
  47. ,chunk: "'.$this->chunkArray['id'].'"
  48. ,record: '.$this->modx->toJSON($this->chunkArray).'
  49. });
  50. });
  51. MODx.onChunkFormRender = "'.$this->onChunkFormRender.'";
  52. MODx.perm.tree_show_element_ids = '.($this->modx->hasPermission('tree_show_element_ids') ? 1 : 0).';
  53. MODx.perm.unlock_element_properties = '.($this->modx->hasPermission('unlock_element_properties') ? 1 : 0).';
  54. // ]]>
  55. </script>');
  56. }
  57. /**
  58. * Custom logic code here for setting placeholders, etc
  59. * @param array $scriptProperties
  60. * @return mixed
  61. */
  62. public function process(array $scriptProperties = array()) {
  63. $placeholders = array();
  64. /* grab chunk */
  65. if (empty($scriptProperties['id']) || strlen($scriptProperties['id']) !== strlen((integer)$scriptProperties['id'])) {
  66. return $this->failure($this->modx->lexicon('chunk_err_ns'));
  67. }
  68. $this->chunk = $this->modx->getObject('modChunk', array('id' => $scriptProperties['id']));
  69. if (empty($this->chunk)) return $this->failure($this->modx->lexicon('chunk_err_nfs',array('id' => $scriptProperties['id'])));
  70. if (!$this->chunk->checkPolicy('view')) return $this->failure($this->modx->lexicon('access_denied'));
  71. /* grab category for chunk, assign to parser */
  72. $placeholders['chunk'] = $this->chunk;
  73. /* invoke OnChunkFormRender event */
  74. $placeholders['onChunkFormRender'] = $this->fireRenderEvent();
  75. /* get properties */
  76. $properties = $this->chunk->get('properties');
  77. if (!is_array($properties)) $properties = array();
  78. $data = array();
  79. foreach ($properties as $property) {
  80. $data[] = array(
  81. $property['name'],
  82. $property['desc'],
  83. !empty($property['type']) ? $property['type'] : 'textfield',
  84. !empty($property['options']) ? $property['options'] : array(),
  85. $property['value'],
  86. !empty($property['lexicon']) ? $property['lexicon'] : '',
  87. false, /* overridden set to false */
  88. $property['desc_trans'],
  89. !empty($property['area']) ? $property['area'] : '',
  90. !empty($property['area_trans']) ? $property['area_trans'] : '',
  91. );
  92. }
  93. $this->chunkArray = $this->chunk->toArray();
  94. $this->chunkArray['properties'] = $data;
  95. $this->chunkArray['snippet'] = $this->chunk->getContent();
  96. $this->prepareElement();
  97. /* invoke OnRichTextEditorInit event */
  98. $placeholders['onRTEInit'] = $this->loadRte();
  99. /* check unlock default element properties permission */
  100. $placeholders['unlock_element_properties'] = $this->modx->hasPermission('unlock_element_properties') ? 1 : 0;
  101. return $placeholders;
  102. }
  103. /**
  104. * Prepare the element and get the static openTo path if needed
  105. *
  106. * @return void|string
  107. */
  108. public function prepareElement() {
  109. $this->chunkArray['openTo'] = '/';
  110. if (!empty($this->chunkArray['static'])) {
  111. $file = $this->chunk->get('static_file');
  112. $this->chunkArray['openTo'] = dirname($file).'/';
  113. }
  114. return $this->chunkArray['openTo'];
  115. }
  116. /**
  117. * Fire the OnChunkFormPrerender event
  118. * @return mixed
  119. */
  120. public function firePreRenderEvents() {
  121. /* PreRender events inject directly into the HTML, as opposed to the JS-based Render event which injects HTML
  122. into the panel */
  123. $this->onChunkFormPrerender = $this->modx->invokeEvent('OnChunkFormPrerender',array(
  124. 'id' => $this->chunkArray['id'],
  125. 'mode' => modSystemEvent::MODE_UPD,
  126. 'chunk' => $this->chunk,
  127. ));
  128. if (is_array($this->onChunkFormPrerender)) { $this->onChunkFormPrerender = implode('',$this->onChunkFormPrerender); }
  129. $this->setPlaceholder('onChunkFormPrerender', $this->onChunkFormPrerender);
  130. }
  131. /**
  132. * Invoke OnRichTextEditorInit event, loading the RTE
  133. * @return string
  134. */
  135. public function loadRte() {
  136. $o = '';
  137. if ($this->modx->getOption('use_editor') == 1) {
  138. $onRTEInit = $this->modx->invokeEvent('OnRichTextEditorInit',array(
  139. 'elements' => array('post'),
  140. 'chunk' => &$this->chunk,
  141. 'mode' => modSystemEvent::MODE_UPD,
  142. ));
  143. if (is_array($onRTEInit)) {
  144. $onRTEInit = implode('', $onRTEInit);
  145. }
  146. $o = $onRTEInit;
  147. }
  148. return $o;
  149. }
  150. /**
  151. * Fire the OnChunkFormRender event
  152. * @return mixed
  153. */
  154. public function fireRenderEvent() {
  155. $this->onChunkFormRender = $this->modx->invokeEvent('OnChunkFormRender',array(
  156. 'id' => $this->chunk->get('id'),
  157. 'mode' => modSystemEvent::MODE_UPD,
  158. 'chunk' => $this->chunk,
  159. ));
  160. if (is_array($this->onChunkFormRender)) $this->onChunkFormRender = implode('', $this->onChunkFormRender);
  161. $this->onChunkFormRender = str_replace(array('"',"\n","\r"),array('\"','',''),$this->onChunkFormRender);
  162. return $this->onChunkFormRender;
  163. }
  164. /**
  165. * Return the pagetitle
  166. *
  167. * @return string
  168. */
  169. public function getPageTitle() {
  170. return $this->modx->lexicon('chunk').': '.$this->chunkArray['name'];
  171. }
  172. /**
  173. * Return the location of the template file
  174. * @return string
  175. */
  176. public function getTemplateFile() {
  177. return 'element/chunk/update.tpl';
  178. }
  179. /**
  180. * Specify the language topics to load
  181. * @return array
  182. */
  183. public function getLanguageTopics() {
  184. return array('chunk','category','propertyset','element');
  185. }
  186. /**
  187. * Get the Help URL
  188. * @return string
  189. */
  190. public function getHelpUrl() {
  191. return 'Chunks';
  192. }
  193. }