update.class.php 6.8 KB

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