update.class.php 6.5 KB

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