update.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. require_once dirname(__FILE__).'/resource.class.php';
  11. /**
  12. * Loads the update resource page
  13. *
  14. * @package modx
  15. * @subpackage manager.controllers
  16. */
  17. class ResourceUpdateManagerController extends ResourceManagerController {
  18. /** @var boolean Whether or not this Resource is locked for editing */
  19. public $locked = false;
  20. /** @var string If the Resource is locked, the text on the locked button to show */
  21. public $lockedText = '';
  22. /** @var string The URL of the resource on the front-end */
  23. public $previewUrl = '';
  24. /** @var modResource $resource */
  25. public $resource;
  26. /**
  27. * Register custom CSS/JS for the page
  28. * @return void
  29. */
  30. public function loadCustomCssJs() {
  31. $managerUrl = $this->context->getOption('manager_url', MODX_MANAGER_URL, $this->modx->_userConfig);
  32. $this->addJavascript($managerUrl.'assets/modext/widgets/element/modx.panel.tv.renders.js');
  33. $this->addJavascript($managerUrl.'assets/modext/widgets/resource/modx.grid.resource.security.local.js');
  34. $this->addJavascript($managerUrl.'assets/modext/widgets/resource/modx.panel.resource.tv.js');
  35. $this->addJavascript($managerUrl.'assets/modext/widgets/resource/modx.panel.resource.js');
  36. $this->addJavascript($managerUrl.'assets/modext/sections/resource/update.js');
  37. $this->addHtml('
  38. <script type="text/javascript">
  39. // <![CDATA[
  40. MODx.config.publish_document = "'.$this->canPublish.'";
  41. MODx.onDocFormRender = "'.$this->onDocFormRender.'";
  42. MODx.ctx = "'.$this->resource->get('context_key').'";
  43. Ext.onReady(function() {
  44. MODx.load({
  45. xtype: "modx-page-resource-update"
  46. ,resource: "'.$this->resource->get('id').'"
  47. ,record: '.$this->modx->toJSON($this->resourceArray).'
  48. ,publish_document: "'.$this->canPublish.'"
  49. ,preview_url: "'.$this->previewUrl.'"
  50. ,locked: '.($this->locked ? 1 : 0).'
  51. ,lockedText: "'.$this->lockedText.'"
  52. ,canSave: '.($this->canSave ? 1 : 0).'
  53. ,canEdit: '.($this->canEdit ? 1 : 0).'
  54. ,canCreate: '.($this->canCreate ? 1 : 0).'
  55. ,canCreateRoot: '.($this->canCreateRoot ? 1 : 0).'
  56. ,canDuplicate: '.($this->canDuplicate ? 1 : 0).'
  57. ,canDelete: '.($this->canDelete ? 1 : 0).'
  58. ,show_tvs: '.(!empty($this->tvCounts) ? 1 : 0).'
  59. ,mode: "update"
  60. });
  61. });
  62. MODx.perm.tree_show_resource_ids = '.($this->modx->hasPermission('tree_show_resource_ids') ? 1 : 0).';
  63. // ]]>
  64. </script>');
  65. /* load RTE */
  66. $this->loadRichTextEditor();
  67. }
  68. public function getResource() {
  69. if (empty($this->scriptProperties['id']) || strlen($this->scriptProperties['id']) !== strlen((integer)$this->scriptProperties['id'])) {
  70. return $this->failure($this->modx->lexicon('resource_err_nf'));
  71. }
  72. $this->resource = $this->modx->getObject($this->resourceClass, array('id' => $this->scriptProperties['id']));
  73. if (empty($this->resource)) return $this->failure($this->modx->lexicon('resource_err_nfs',array('id' => $this->scriptProperties['id'])));
  74. if (!$this->resource->checkPolicy('save')) {
  75. $this->canSave = false;
  76. }
  77. return true;
  78. }
  79. public function process(array $scriptProperties = array()) {
  80. $placeholders = array();
  81. $reloadData = $this->getReloadData();
  82. $loaded = $this->getResource();
  83. if ($loaded !== true) {
  84. return $this->failure($loaded);
  85. }
  86. if(is_array($reloadData) && !empty($reloadData)) {
  87. $this->resource->fromArray($reloadData);
  88. }
  89. /* get context */
  90. $this->setContext();
  91. if (!$this->context) { return $this->failure($this->modx->lexicon('access_denied')); }
  92. /* check for locked status */
  93. $this->checkForLocks();
  94. /* set template overrides */
  95. if (isset($scriptProperties['template'])) $this->resource->set('template',$scriptProperties['template']);
  96. $this->setParent();
  97. /* invoke OnDocFormRender event */
  98. $this->fireOnRenderEvent();
  99. /* check permissions */
  100. $this->setPermissions();
  101. /* register FC rules */
  102. $this->resourceArray = $this->resource->toArray();
  103. $overridden = $this->checkFormCustomizationRules($this->resource);
  104. $this->resourceArray = array_merge($this->resourceArray,$overridden);
  105. $this->resourceArray['published'] = intval($this->resourceArray['published']) == 1 ? true : false;
  106. $this->resourceArray['hidemenu'] = intval($this->resourceArray['hidemenu']) == 1 ? true : false;
  107. $this->resourceArray['isfolder'] = intval($this->resourceArray['isfolder']) == 1 ? true : false;
  108. $this->resourceArray['richtext'] = intval($this->resourceArray['richtext']) == 1 ? true : false;
  109. $this->resourceArray['searchable'] = intval($this->resourceArray['searchable']) == 1 ? true : false;
  110. $this->resourceArray['cacheable'] = intval($this->resourceArray['cacheable']) == 1 ? true : false;
  111. $this->resourceArray['deleted'] = intval($this->resourceArray['deleted']) == 1 ? true : false;
  112. $this->resourceArray['uri_override'] = intval($this->resourceArray['uri_override']) == 1 ? true : false;
  113. if (isset($this->resourceArray['syncsite'])) {
  114. $this->resourceArray['syncsite'] = intval($this->resourceArray['syncsite']) == 1 ? true : false;
  115. } else {
  116. $syncsiteDefault = $this->context->getOption('syncsite_default', 1, $this->modx->_userConfig);
  117. $this->resourceArray['syncsite'] = intval($syncsiteDefault) == 1 ? true : false;
  118. }
  119. if (!empty($this->resourceArray['parent'])) {
  120. if ($this->parent->get('id') == $this->resourceArray['parent']) {
  121. $this->resourceArray['parent_pagetitle'] = $this->modx->stripTags($this->parent->get('pagetitle'));
  122. } else {
  123. $overriddenParent = $this->modx->getObject('modResource',$this->resourceArray['parent']);
  124. if ($overriddenParent) {
  125. $this->resourceArray['parent_pagetitle'] = $this->modx->stripTags($overriddenParent->get('pagetitle'));
  126. }
  127. }
  128. }
  129. /* get TVs */
  130. $this->resource->set('template',$this->resourceArray['template']);
  131. if (!empty($reloadData)) {
  132. $this->resourceArray['resourceGroups'] = array();
  133. $this->resourceArray['resource_groups'] = $this->modx->getOption('resource_groups',
  134. $this->resourceArray, array());
  135. $this->resourceArray['resource_groups'] = is_array($this->resourceArray['resource_groups']) ?
  136. $this->resourceArray['resource_groups'] :
  137. $this->modx->fromJSON($this->resourceArray['resource_groups']);
  138. foreach ($this->resourceArray['resource_groups'] as $resourceGroup) {
  139. $this->resourceArray['resourceGroups'][] = array(
  140. $resourceGroup['id'],
  141. $resourceGroup['name'],
  142. $resourceGroup['access'],
  143. );
  144. }
  145. unset($this->resourceArray['resource_groups']);
  146. } else {
  147. $this->getResourceGroups();
  148. }
  149. $this->prepareResource();
  150. $this->loadTVs($reloadData);
  151. $this->getPreviewUrl();
  152. /* single-use token for reloading resource */
  153. $this->setResourceToken();
  154. $this->setPlaceholder('resource',$this->resource);
  155. return $placeholders;
  156. }
  157. /**
  158. * Get url for resource for preview window
  159. * @return string
  160. */
  161. public function getPreviewUrl() {
  162. if (!$this->resource->get('deleted')) {
  163. $this->modx->setOption('cache_alias_map', false);
  164. $sessionEnabled = '';
  165. $ctxSetting = $this->modx->getObject('modContextSetting', array('context_key' => $this->resource->get('context_key'), 'key' => 'session_enabled'));
  166. if ($ctxSetting) {
  167. $sessionEnabled = $ctxSetting->get('value') == 0 ? array('preview' => 'true') : '';
  168. }
  169. $this->previewUrl = $this->modx->makeUrl($this->resource->get('id'), $this->resource->get('context_key'), $sessionEnabled, 'full', array('xhtml_urls' => false));
  170. }
  171. return $this->previewUrl;
  172. }
  173. /**
  174. * Check for locks on the Resource
  175. *
  176. * @return bool
  177. */
  178. public function checkForLocks() {
  179. $lockedBy = $this->resource->addLock($this->modx->user->get('id'));
  180. $this->canSave = $this->modx->hasPermission('save_document') ? 1 : 0;
  181. $this->locked = false;
  182. $this->lockedText = '';
  183. if (!empty($lockedBy) && $lockedBy !== true) {
  184. $this->canSave = false;
  185. $this->locked = true;
  186. $locker = $this->modx->getObject('modUser',$lockedBy);
  187. if ($locker) {
  188. $lockedBy = $locker->get('username');
  189. }
  190. $this->lockedText = $this->modx->lexicon('resource_locked_by', array('user' => $lockedBy, 'id' => $this->resource->get('id')));
  191. }
  192. return $this->locked;
  193. }
  194. /**
  195. * Check for any permissions or requirements to load page
  196. * @return bool
  197. */
  198. public function checkPermissions() {
  199. return $this->modx->hasPermission('edit_document');
  200. }
  201. /**
  202. * Return the pagetitle
  203. *
  204. * @return string
  205. */
  206. public function getPageTitle() {
  207. return $this->modx->lexicon('editing',array('name' => $this->resourceArray['pagetitle']));
  208. }
  209. /**
  210. * Return the location of the template file
  211. * @return string
  212. */
  213. public function getTemplateFile() {
  214. return 'resource/update.tpl';
  215. }
  216. }
  217. class DocumentUpdateManagerController extends ResourceUpdateManagerController {}