update.class.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 template page
  12. *
  13. * @package modx
  14. * @subpackage manager.controllers
  15. */
  16. class ElementTemplateUpdateManagerController extends modManagerController {
  17. /** @var modCategory $category */
  18. public $category;
  19. /** @var modTemplate $template */
  20. public $template;
  21. /** @var array $templateArray */
  22. public $templateArray;
  23. /** @var string $onTempFormRender */
  24. public $onTempFormRender = '';
  25. /** @var string $onTempFormPrerender */
  26. public $onTempFormPrerender = '';
  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_template');
  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.grid.template.tv.js');
  43. $this->addJavascript($mgrUrl.'assets/modext/widgets/element/modx.panel.template.js');
  44. $this->addJavascript($mgrUrl.'assets/modext/sections/element/template/update.js');
  45. $this->addHtml('
  46. <script type="text/javascript">
  47. // <![CDATA[
  48. Ext.onReady(function() {
  49. MODx.load({
  50. xtype: "modx-page-template-update"
  51. ,id: "'.$this->templateArray['id'].'"
  52. ,record: '.$this->modx->toJSON($this->templateArray).'
  53. });
  54. });
  55. MODx.onTempFormRender = "'.$this->onTempFormRender.'";
  56. MODx.perm.tree_show_element_ids = '.($this->modx->hasPermission('tree_show_element_ids') ? 1 : 0).';
  57. MODx.perm.unlock_element_properties = "'.($this->modx->hasPermission('unlock_element_properties') ? 1 : 0).'";
  58. // ]]>
  59. </script>');
  60. }
  61. /**
  62. * Custom logic code here for setting placeholders, etc
  63. * @param array $scriptProperties
  64. * @return mixed
  65. */
  66. public function process(array $scriptProperties = array()) {
  67. $placeholders = array();
  68. /* load template */
  69. if (empty($scriptProperties['id']) || strlen($scriptProperties['id']) !== strlen((integer)$scriptProperties['id'])) {
  70. return $this->failure($this->modx->lexicon('template_err_ns'));
  71. }
  72. $this->template = $this->modx->getObject('modTemplate', array('id' => $scriptProperties['id']));
  73. if ($this->template == null) return $this->failure($this->modx->lexicon('template_err_nf'));
  74. if (!$this->template->checkPolicy('view')) return $this->failure($this->modx->lexicon('access_denied'));
  75. /* get properties */
  76. $properties = $this->template->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->templateArray = $this->template->toArray();
  94. $this->templateArray['properties'] = $data;
  95. $this->templateArray['content'] = $this->template->getContent();
  96. $this->prepareElement();
  97. /* load template into parser */
  98. $placeholders['template'] = $this->template;
  99. /* invoke OnTempFormRender event */
  100. $placeholders['onTempFormRender'] = $this->fireRenderEvent();
  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->templateArray['openTo'] = '/';
  110. if (!empty($this->templateArray['static'])) {
  111. $file = $this->template->get('static_file');
  112. $this->templateArray['openTo'] = dirname($file).'/';
  113. }
  114. return $this->templateArray['openTo'];
  115. }
  116. /**
  117. * Invoke OnTempFormPrerender event
  118. * @return void
  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->onTempFormPrerender = $this->modx->invokeEvent('OnTempFormPrerender',array(
  124. 'id' => $this->templateArray['id'],
  125. 'template' => &$this->template,
  126. 'mode' => modSystemEvent::MODE_UPD,
  127. ));
  128. if (is_array($this->onTempFormPrerender)) $this->onTempFormPrerender = implode('',$this->onTempFormPrerender);
  129. $this->setPlaceholder('onTempFormPrerender', $this->onTempFormPrerender);
  130. }
  131. /**
  132. * Invoke OnTempFormRender event
  133. * @return string
  134. */
  135. public function fireRenderEvent() {
  136. $this->onTempFormRender = $this->modx->invokeEvent('OnTempFormRender',array(
  137. 'id' => $this->templateArray['id'],
  138. 'template' => &$this->template,
  139. 'mode' => modSystemEvent::MODE_UPD,
  140. ));
  141. if (is_array($this->onTempFormRender)) $this->onTempFormRender = implode('',$this->onTempFormRender);
  142. $this->onTempFormRender = str_replace(array('"',"\n","\r"),array('\"','',''),$this->onTempFormRender);
  143. return $this->onTempFormRender;
  144. }
  145. /**
  146. * Return the pagetitle
  147. *
  148. * @return string
  149. */
  150. public function getPageTitle() {
  151. return $this->modx->lexicon('template').': '.$this->templateArray['templatename'];
  152. }
  153. /**
  154. * Return the location of the template file
  155. * @return string
  156. */
  157. public function getTemplateFile() {
  158. return 'element/template/update.tpl';
  159. }
  160. /**
  161. * Specify the language topics to load
  162. * @return array
  163. */
  164. public function getLanguageTopics() {
  165. return array('template','category','system_events','propertyset','element','tv');
  166. }
  167. /**
  168. * Get the Help URL
  169. * @return string
  170. */
  171. public function getHelpUrl() {
  172. return 'Templates';
  173. }
  174. }