update.class.php 7.3 KB

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