update.class.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 plugin page
  12. *
  13. * @package modx
  14. * @subpackage manager.controllers
  15. */
  16. class ElementPluginUpdateManagerController extends modManagerController {
  17. /** @var modCategory $category */
  18. public $category;
  19. /** @var modPlugin $plugin */
  20. public $plugin;
  21. /** @var array $pluginArray */
  22. public $pluginArray;
  23. /** @var string $onPluginFormRender */
  24. public $onPluginFormRender = '';
  25. /** @var string $onPluginFormPrerender */
  26. public $onPluginFormPrerender = '';
  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_plugin');
  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.plugin.event.js');
  43. $this->addJavascript($mgrUrl.'assets/modext/widgets/element/modx.panel.plugin.js');
  44. $this->addJavascript($mgrUrl.'assets/modext/sections/element/plugin/update.js');
  45. $this->addHtml('
  46. <script type="text/javascript">
  47. // <![CDATA[
  48. Ext.onReady(function() {
  49. MODx.load({
  50. xtype: "modx-page-plugin-update"
  51. ,id: "'.$this->pluginArray['id'].'"
  52. ,record: '.$this->modx->toJSON($this->pluginArray).'
  53. });
  54. });
  55. MODx.onPluginFormRender = "'.$this->onPluginFormRender.'";
  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 plugin */
  69. if (empty($scriptProperties['id']) || strlen($scriptProperties['id']) !== strlen((integer)$scriptProperties['id'])) {
  70. return $this->failure($this->modx->lexicon('plugin_err_ns'));
  71. }
  72. $this->plugin = $this->modx->getObject('modPlugin', array('id' => $scriptProperties['id']));
  73. if ($this->plugin == null) return $this->failure($this->modx->lexicon('plugin_err_nf'));
  74. if (!$this->plugin->checkPolicy('view')) return $this->failure($this->modx->lexicon('access_denied'));
  75. /* get properties */
  76. $properties = $this->plugin->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->pluginArray = $this->plugin->toArray();
  94. $this->pluginArray['properties'] = $data;
  95. $this->pluginArray['plugincode'] = $this->plugin->getContent();
  96. if (strpos($this->pluginArray['plugincode'],'<?php') === false) {
  97. $this->pluginArray['plugincode'] = "<?php\n".$this->pluginArray['plugincode'];
  98. }
  99. $this->prepareElement();
  100. /* load plugin into parser */
  101. $placeholders['plugin'] = $this->plugin;
  102. /* invoke OnPluginFormRender event */
  103. $placeholders['onPluginFormRender'] = $this->fireRenderEvent();
  104. return $placeholders;
  105. }
  106. /**
  107. * Prepare the element and get the static openTo path if needed
  108. *
  109. * @return void|string
  110. */
  111. public function prepareElement() {
  112. $this->pluginArray['openTo'] = '/';
  113. if (!empty($this->pluginArray['static'])) {
  114. $file = $this->plugin->get('static_file');
  115. $this->pluginArray['openTo'] = dirname($file).'/';
  116. }
  117. return $this->pluginArray['openTo'];
  118. }
  119. /**
  120. * Invoke OnPluginFormPrerender event
  121. * @return string
  122. */
  123. public function firePreRenderEvents() {
  124. /* PreRender events inject directly into the HTML, as opposed to the JS-based Render event which injects HTML
  125. into the panel */
  126. $this->onPluginFormPrerender = $this->modx->invokeEvent('OnPluginFormPrerender',array(
  127. 'id' => $this->pluginArray['id'],
  128. 'plugin' => &$this->plugin,
  129. 'mode' => modSystemEvent::MODE_UPD,
  130. ));
  131. if (is_array($this->onPluginFormPrerender)) $this->onPluginFormPrerender = implode('',$this->onPluginFormPrerender);
  132. $this->setPlaceholder('onPluginFormPrerender', $this->onPluginFormPrerender);
  133. }
  134. /**
  135. * Invoke OnPluginFormRender event
  136. * @return string
  137. */
  138. public function fireRenderEvent() {
  139. $this->onPluginFormRender = $this->modx->invokeEvent('OnPluginFormRender',array(
  140. 'id' => $this->pluginArray['id'],
  141. 'plugin' => &$this->plugin,
  142. 'mode' => modSystemEvent::MODE_UPD,
  143. ));
  144. if (is_array($this->onPluginFormRender)) $this->onPluginFormRender = implode('',$this->onPluginFormRender);
  145. $this->onPluginFormRender = str_replace(array('"',"\n","\r"),array('\"','',''),$this->onPluginFormRender);
  146. return $this->onPluginFormRender;
  147. }
  148. /**
  149. * Return the pagetitle
  150. *
  151. * @return string
  152. */
  153. public function getPageTitle() {
  154. return $this->modx->lexicon('plugin').': '.$this->pluginArray['name'];
  155. }
  156. /**
  157. * Return the location of the template file
  158. * @return string
  159. */
  160. public function getTemplateFile() {
  161. return 'element/plugin/update.tpl';
  162. }
  163. /**
  164. * Specify the language topics to load
  165. * @return array
  166. */
  167. public function getLanguageTopics() {
  168. return array('plugin','category','system_events','propertyset','element');
  169. }
  170. /**
  171. * Get the Help URL
  172. * @return string
  173. */
  174. public function getHelpUrl() {
  175. return 'Plugins';
  176. }
  177. }