create.class.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 create chunk page
  12. *
  13. * @package modx
  14. * @subpackage manager.controllers
  15. */
  16. class ElementChunkCreateManagerController extends modManagerController {
  17. public $category;
  18. public $onChunkFormRender;
  19. public $onChunkFormPrerender;
  20. /**
  21. * Check for any permissions or requirements to load page
  22. * @return bool
  23. */
  24. public function checkPermissions() {
  25. return $this->modx->hasPermission('new_chunk');
  26. }
  27. /**
  28. * Register custom CSS/JS for the page
  29. * @return void
  30. */
  31. public function loadCustomCssJs() {
  32. $mgrUrl = $this->modx->getOption('manager_url',null,MODX_MANAGER_URL);
  33. $this->addJavascript($mgrUrl.'assets/modext/widgets/core/modx.grid.local.property.js');
  34. $this->addJavascript($mgrUrl.'assets/modext/widgets/element/modx.grid.element.properties.js');
  35. $this->addJavascript($mgrUrl.'assets/modext/widgets/element/modx.panel.chunk.js');
  36. $this->addJavascript($mgrUrl.'assets/modext/sections/element/chunk/create.js');
  37. $this->addHtml('
  38. <script type="text/javascript">
  39. // <![CDATA[
  40. Ext.onReady(function() {
  41. MODx.load({
  42. xtype: "modx-page-chunk-create"
  43. ,record: {
  44. category: "'.($this->category ? $this->category->get('id') : 0).'"
  45. }
  46. });
  47. });
  48. MODx.onChunkFormRender = "'.$this->onChunkFormRender.'";
  49. MODx.perm.unlock_element_properties = '.($this->modx->hasPermission('unlock_element_properties') ? 1 : 0).';
  50. // ]]>
  51. </script>');
  52. }
  53. /**
  54. * Custom logic code here for setting placeholders, etc
  55. * @param array $scriptProperties
  56. * @return mixed
  57. */
  58. public function process(array $scriptProperties = array()) {
  59. $placeholders = array();
  60. $placeholders['category'] = $this->getCategory($scriptProperties);
  61. /* invoke OnChunkFormRender event */
  62. $placeholders['onChunkFormRender'] = $this->fireRenderEvent();
  63. $placeholders['onRTEInit'] = $this->loadRte();
  64. return $placeholders;
  65. }
  66. /**
  67. * Get the current category
  68. *
  69. * @param array $scriptProperties
  70. * @return void|modCategory
  71. */
  72. public function getCategory(array $scriptProperties = array()) {
  73. /* grab default category if specified */
  74. if (isset($scriptProperties['category'])) {
  75. $this->category = $this->modx->getObject('modCategory',$scriptProperties['category']);
  76. } else { $this->category = null; }
  77. return $this->category;
  78. }
  79. /**
  80. * Invoke OnRichTextEditorInit event, loading the RTE
  81. * @return string
  82. */
  83. public function loadRte() {
  84. $o = '';
  85. if ($this->modx->getOption('use_editor') == 1) {
  86. $onRTEInit = $this->modx->invokeEvent('OnRichTextEditorInit',array(
  87. 'elements' => array('post'),
  88. 'mode' => modSystemEvent::MODE_NEW,
  89. ));
  90. if (is_array($onRTEInit)) {
  91. $onRTEInit = implode('', $onRTEInit);
  92. }
  93. $o = $onRTEInit;
  94. }
  95. return $o;
  96. }
  97. /**
  98. * Fire the OnChunkFormRender event
  99. * @return mixed
  100. */
  101. public function fireRenderEvent() {
  102. $this->onChunkFormRender = $this->modx->invokeEvent('OnChunkFormRender',array(
  103. 'id' => 0,
  104. 'mode' => modSystemEvent::MODE_NEW,
  105. 'chunk' => null,
  106. ));
  107. if (is_array($this->onChunkFormRender)) $this->onChunkFormRender = implode('', $this->onChunkFormRender);
  108. $this->onChunkFormRender = str_replace(array('"',"\n","\r"),array('\"','',''),$this->onChunkFormRender);
  109. return $this->onChunkFormRender;
  110. }
  111. /**
  112. * Fire the OnChunkFormPrerender event
  113. * @return mixed
  114. */
  115. public function firePreRenderEvents() {
  116. /* PreRender events inject directly into the HTML, as opposed to the JS-based Render event which injects HTML
  117. into the panel */
  118. $this->onChunkFormPrerender = $this->modx->invokeEvent('OnChunkFormPrerender',array(
  119. 'id' => 0,
  120. 'mode' => modSystemEvent::MODE_NEW,
  121. 'chunk' => null,
  122. ));
  123. if (is_array($this->onChunkFormPrerender)) { $this->onChunkFormPrerender = implode('',$this->onChunkFormPrerender); }
  124. $this->setPlaceholder('onChunkFormPrerender', $this->onChunkFormPrerender);
  125. }
  126. /**
  127. * Return the pagetitle
  128. *
  129. * @return string
  130. */
  131. public function getPageTitle() {
  132. return $this->modx->lexicon('chunk_new');
  133. }
  134. /**
  135. * Return the location of the template file
  136. * @return string
  137. */
  138. public function getTemplateFile() {
  139. return 'element/chunk/create.tpl';
  140. }
  141. /**
  142. * Specify the language topics to load
  143. * @return array
  144. */
  145. public function getLanguageTopics() {
  146. return array('chunk','category','propertyset','element');
  147. }
  148. /**
  149. * Get the Help URL
  150. * @return string
  151. */
  152. public function getHelpUrl() {
  153. return 'Chunks';
  154. }
  155. }