update.class.php 9.6 KB

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