create.class.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 snippet page
  12. *
  13. * @package modx
  14. * @subpackage manager.controllers
  15. */
  16. class ElementSnippetCreateManagerController extends modManagerController {
  17. public $category;
  18. public $onSnipFormRender = '';
  19. public $onSnipFormPrerender = '';
  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_snippet');
  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.snippet.js');
  36. $this->addJavascript($mgrUrl.'assets/modext/sections/element/snippet/create.js');
  37. $this->addHtml('
  38. <script type="text/javascript">
  39. // <![CDATA[
  40. MODx.onSnipFormRender = "'.$this->onSnipFormRender.'";
  41. MODx.perm.unlock_element_properties = "'.($this->modx->hasPermission('unlock_element_properties') ? 1 : 0).'";
  42. Ext.onReady(function() {
  43. MODx.load({
  44. xtype: "modx-page-snippet-create"
  45. ,record: {
  46. category: "'.($this->category ? $this->category->get('id') : 0).'"
  47. }
  48. });
  49. });
  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. /* grab category if preset */
  61. if (isset($scriptProperties['category'])) {
  62. $this->category = $this->modx->getObject('modCategory',$scriptProperties['category']);
  63. if ($this->category != null) {
  64. $placeholders['category'] = $this->category;
  65. }
  66. }
  67. /* invoke OnSnipFormRender event */
  68. $placeholders['onSnipFormRender'] = $this->fireRenderEvent();
  69. return $placeholders;
  70. }
  71. /**
  72. * Invoke OnSnipFormPrerender event
  73. * @return void
  74. */
  75. public function firePreRenderEvents() {
  76. /* PreRender events inject directly into the HTML, as opposed to the JS-based Render event which injects HTML
  77. into the panel */
  78. $this->onSnipFormPrerender = $this->modx->invokeEvent('OnSnipFormPrerender',array(
  79. 'id' => 0,
  80. 'mode' => modSystemEvent::MODE_NEW,
  81. ));
  82. if (is_array($this->onSnipFormPrerender)) $this->onSnipFormPrerender = implode('',$this->onSnipFormPrerender);
  83. $this->setPlaceholder('onSnipFormPrerender', $this->onSnipFormPrerender);
  84. }
  85. /**
  86. * Invoke OnSnipFormRender event
  87. * @return string
  88. */
  89. public function fireRenderEvent() {
  90. $this->onSnipFormRender = $this->modx->invokeEvent('OnSnipFormRender',array(
  91. 'id' => 0,
  92. 'mode' => modSystemEvent::MODE_NEW,
  93. ));
  94. if (is_array($this->onSnipFormRender)) $this->onSnipFormRender = implode('',$this->onSnipFormRender);
  95. $this->onSnipFormRender = str_replace(array('"',"\n","\r"),array('\"','',''),$this->onSnipFormRender);
  96. return $this->onSnipFormRender;
  97. }
  98. /**
  99. * Return the pagetitle
  100. *
  101. * @return string
  102. */
  103. public function getPageTitle() {
  104. return $this->modx->lexicon('snippet_new');
  105. }
  106. /**
  107. * Return the location of the template file
  108. * @return string
  109. */
  110. public function getTemplateFile() {
  111. return 'element/snippet/create.tpl';
  112. }
  113. /**
  114. * Specify the language topics to load
  115. * @return array
  116. */
  117. public function getLanguageTopics() {
  118. return array('snippet','category','system_events','propertyset','element');
  119. }
  120. /**
  121. * Get the Help URL
  122. * @return string
  123. */
  124. public function getHelpUrl() {
  125. return 'Snippets';
  126. }
  127. }