update.class.php 6.4 KB

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