update.class.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. * Loads the view context preview page.
  12. *
  13. * @package modx
  14. * @subpackage manager.controllers
  15. */
  16. class ContextUpdateManagerController extends modManagerController {
  17. /**
  18. * The key of the current context
  19. * @var string $contextKey
  20. */
  21. public $contextKey;
  22. /**
  23. * The return value from the OnContextFormRender event
  24. * @var string $onContextFormRender
  25. */
  26. public $onContextFormRender;
  27. /**
  28. * The context to update.
  29. * @var modContext $context
  30. */
  31. public $context;
  32. /**
  33. * Check for any permissions or requirements to load page
  34. * @return bool
  35. */
  36. public function checkPermissions() {
  37. return $this->modx->hasPermission('edit_context');
  38. }
  39. /**
  40. * Get the context to update
  41. * @return void
  42. */
  43. public function initialize() {
  44. $this->context= $this->modx->getObjectGraph('modContext', '{"ContextSettings":{}}', array('key' => $this->scriptProperties['key']));
  45. if ($this->context) {
  46. $this->contextKey = $this->context->get('key');
  47. }
  48. }
  49. /**
  50. * Register custom CSS/JS for the page
  51. * @return void
  52. */
  53. public function loadCustomCssJs() {
  54. $mgrUrl = $this->modx->getOption('manager_url',null,MODX_MANAGER_URL);
  55. $perm = (bool)$this->modx->hasPermission('new_context');
  56. $this->addHtml("<script>
  57. // <![CDATA[
  58. MODx.onContextFormRender = '".$this->onContextFormRender."';
  59. MODx.ctx = '".$this->contextKey."';
  60. MODx.perm.new_context = {$perm};
  61. Ext.onReady(function() {
  62. MODx.add('modx-page-context-update');
  63. });
  64. // ]]>
  65. </script>");
  66. $this->addJavascript($mgrUrl.'assets/modext/widgets/security/modx.grid.access.context.js');
  67. $this->addJavascript($mgrUrl.'assets/modext/widgets/core/modx.grid.settings.js');
  68. $this->addJavascript($mgrUrl.'assets/modext/widgets/system/modx.grid.context.settings.js');
  69. $this->addJavascript($mgrUrl.'assets/modext/widgets/system/modx.panel.context.js');
  70. $this->addJavascript($mgrUrl.'assets/modext/sections/context/update.js');
  71. }
  72. /**
  73. * Custom logic code here for setting placeholders, etc
  74. * @param array $scriptProperties
  75. * @return mixed
  76. */
  77. public function process(array $scriptProperties = array()) {
  78. if (empty($this->context)) {
  79. return $this->failure(sprintf($this->modx->lexicon('context_with_key_not_found'), $this->scriptProperties['key']));
  80. }
  81. if (!$this->context->checkPolicy(array('view' => true, 'save' => true))) {
  82. return $this->failure($this->modx->lexicon('permission_denied'));
  83. }
  84. /* prepare context data for display */
  85. if (!$this->context->prepare()) {
  86. return $this->failure($this->modx->lexicon('context_err_load_data'), $this->context->toArray());
  87. }
  88. /* invoke OnContextFormPrerender event */
  89. $this->setPlaceholder('OnContextFormPrerender',$this->onPreRender());
  90. /* invoke OnContextFormRender event */
  91. $this->setPlaceholder('OnContextFormRender',$this->onRender());
  92. /* assign context to smarty and display */
  93. $this->setPlaceholder('context',$this->context);
  94. $this->setPlaceholder('_ctx',$this->context->get('key'));
  95. return null;
  96. }
  97. /**
  98. * @return mixed
  99. */
  100. public function onPreRender() {
  101. $onContextFormPrerender = $this->modx->invokeEvent('OnContextFormPrerender',array(
  102. 'key' => $this->context->get('key'),
  103. 'context' => &$this->context,
  104. 'mode' => modSystemEvent::MODE_UPD,
  105. ));
  106. if (is_array($onContextFormPrerender)) $onContextFormPrerender = implode('',$onContextFormPrerender);
  107. return $onContextFormPrerender;
  108. }
  109. /**
  110. * @return mixed
  111. */
  112. public function onRender() {
  113. $this->onContextFormRender = $this->modx->invokeEvent('OnContextFormRender',array(
  114. 'key' => $this->context->get('key'),
  115. 'context' => &$this->context,
  116. 'mode' => modSystemEvent::MODE_UPD,
  117. ));
  118. if (is_array($this->onContextFormRender)) $this->onContextFormRender = implode('',$this->onContextFormRender);
  119. $this->onContextFormRender = str_replace(array('"',"\n","\r"),array('\"','',''),$this->onContextFormRender);
  120. return $this->onContextFormRender;
  121. }
  122. /**
  123. * Return the pagetitle
  124. *
  125. * @return string
  126. */
  127. public function getPageTitle() {
  128. return $this->modx->lexicon('context').': '.$this->contextKey;
  129. }
  130. /**
  131. * Return the location of the template file
  132. * @return string
  133. */
  134. public function getTemplateFile() {
  135. return '';
  136. }
  137. /**
  138. * Specify the language topics to load
  139. * @return array
  140. */
  141. public function getLanguageTopics() {
  142. return array('context','setting','access','policy','user');
  143. }
  144. /**
  145. * Get the Help URL
  146. * @return string
  147. */
  148. public function getHelpUrl() {
  149. return 'Contexts';
  150. }
  151. }