create.class.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * Load create snippet page
  4. *
  5. * @package modx
  6. * @subpackage manager.controllers
  7. */
  8. class ElementSnippetCreateManagerController extends modManagerController {
  9. public $category;
  10. public $onSnipFormRender = '';
  11. public $onSnipFormPrerender = '';
  12. /**
  13. * Check for any permissions or requirements to load page
  14. * @return bool
  15. */
  16. public function checkPermissions() {
  17. return $this->modx->hasPermission('new_snippet');
  18. }
  19. /**
  20. * Register custom CSS/JS for the page
  21. * @return void
  22. */
  23. public function loadCustomCssJs() {
  24. $mgrUrl = $this->modx->getOption('manager_url',null,MODX_MANAGER_URL);
  25. $this->addJavascript($mgrUrl.'assets/modext/widgets/core/modx.grid.local.property.js');
  26. $this->addJavascript($mgrUrl.'assets/modext/widgets/element/modx.grid.element.properties.js');
  27. $this->addJavascript($mgrUrl.'assets/modext/widgets/element/modx.panel.snippet.js');
  28. $this->addJavascript($mgrUrl.'assets/modext/sections/element/snippet/create.js');
  29. $this->addHtml('
  30. <script type="text/javascript">
  31. // <![CDATA[
  32. MODx.onSnipFormRender = "'.$this->onSnipFormRender.'";
  33. MODx.perm.unlock_element_properties = "'.($this->modx->hasPermission('unlock_element_properties') ? 1 : 0).'";
  34. Ext.onReady(function() {
  35. MODx.load({
  36. xtype: "modx-page-snippet-create"
  37. ,record: {
  38. category: "'.($this->category ? $this->category->get('id') : 0).'"
  39. }
  40. });
  41. });
  42. // ]]>
  43. </script>');
  44. }
  45. /**
  46. * Custom logic code here for setting placeholders, etc
  47. * @param array $scriptProperties
  48. * @return mixed
  49. */
  50. public function process(array $scriptProperties = array()) {
  51. $placeholders = array();
  52. /* grab category if preset */
  53. if (isset($scriptProperties['category'])) {
  54. $this->category = $this->modx->getObject('modCategory',$scriptProperties['category']);
  55. if ($this->category != null) {
  56. $placeholders['category'] = $this->category;
  57. }
  58. }
  59. /* invoke OnSnipFormRender event */
  60. $placeholders['onSnipFormRender'] = $this->fireRenderEvent();
  61. return $placeholders;
  62. }
  63. /**
  64. * Invoke OnSnipFormPrerender event
  65. * @return void
  66. */
  67. public function firePreRenderEvents() {
  68. /* PreRender events inject directly into the HTML, as opposed to the JS-based Render event which injects HTML
  69. into the panel */
  70. $this->onSnipFormPrerender = $this->modx->invokeEvent('OnSnipFormPrerender',array(
  71. 'id' => 0,
  72. 'mode' => modSystemEvent::MODE_NEW,
  73. ));
  74. if (is_array($this->onSnipFormPrerender)) $this->onSnipFormPrerender = implode('',$this->onSnipFormPrerender);
  75. $this->setPlaceholder('onSnipFormPrerender', $this->onSnipFormPrerender);
  76. }
  77. /**
  78. * Invoke OnSnipFormRender event
  79. * @return string
  80. */
  81. public function fireRenderEvent() {
  82. $this->onSnipFormRender = $this->modx->invokeEvent('OnSnipFormRender',array(
  83. 'id' => 0,
  84. 'mode' => modSystemEvent::MODE_NEW,
  85. ));
  86. if (is_array($this->onSnipFormRender)) $this->onSnipFormRender = implode('',$this->onSnipFormRender);
  87. $this->onSnipFormRender = str_replace(array('"',"\n","\r"),array('\"','',''),$this->onSnipFormRender);
  88. return $this->onSnipFormRender;
  89. }
  90. /**
  91. * Return the pagetitle
  92. *
  93. * @return string
  94. */
  95. public function getPageTitle() {
  96. return $this->modx->lexicon('snippet_new');
  97. }
  98. /**
  99. * Return the location of the template file
  100. * @return string
  101. */
  102. public function getTemplateFile() {
  103. return 'element/snippet/create.tpl';
  104. }
  105. /**
  106. * Specify the language topics to load
  107. * @return array
  108. */
  109. public function getLanguageTopics() {
  110. return array('snippet','category','system_events','propertyset','element');
  111. }
  112. /**
  113. * Get the Help URL
  114. * @return string
  115. */
  116. public function getHelpUrl() {
  117. return 'Snippets';
  118. }
  119. }