create.class.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 tv page
  12. *
  13. * @package modx
  14. * @subpackage manager.controllers
  15. */
  16. class ElementTVCreateManagerController extends modManagerController {
  17. public $category;
  18. public $onTVFormRender = '';
  19. public $onTVFormPrerender = '';
  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_tv');
  26. }
  27. /**
  28. * Register custom CSS/JS for the page
  29. * @return void
  30. */
  31. public function loadCustomCssJs() {
  32. $sources = $this->modx->toJSON($this->getElementSources());
  33. $mgrUrl = $this->modx->getOption('manager_url',null,MODX_MANAGER_URL);
  34. $this->addJavascript($mgrUrl.'assets/modext/widgets/core/modx.grid.local.property.js');
  35. $this->addJavascript($mgrUrl.'assets/modext/widgets/element/modx.grid.element.properties.js');
  36. $this->addJavascript($mgrUrl.'assets/modext/widgets/element/modx.grid.tv.template.js');
  37. $this->addJavascript($mgrUrl.'assets/modext/widgets/element/modx.grid.tv.security.js');
  38. $this->addJavascript($mgrUrl.'assets/modext/widgets/element/modx.panel.tv.js');
  39. $this->addJavascript($mgrUrl.'assets/modext/sections/element/tv/create.js');
  40. $this->addHtml('
  41. <script type="text/javascript">
  42. // <![CDATA[
  43. MODx.onTVFormRender = "'.$this->onTVFormRender.'";
  44. MODx.perm.unlock_element_properties = "'.($this->modx->hasPermission('unlock_element_properties') ? 1 : 0).'";
  45. Ext.onReady(function() {
  46. MODx.load({
  47. xtype: "modx-page-tv-create"
  48. ,record: {
  49. category: "'.($this->category ? $this->category->get('id') : 0).'"
  50. ,sources: '.$sources.'
  51. }
  52. });
  53. });
  54. // ]]>
  55. </script>');
  56. }
  57. /**
  58. * Custom logic code here for setting placeholders, etc
  59. * @param array $scriptProperties
  60. * @return mixed
  61. */
  62. public function process(array $scriptProperties = array()) {
  63. $placeholders = array();
  64. /* grab category if preset */
  65. if (isset($scriptProperties['category'])) {
  66. $this->category = $this->modx->getObject('modCategory',$scriptProperties['category']);
  67. if ($this->category != null) {
  68. $placeholders['category'] = $this->category;
  69. }
  70. }
  71. /* invoke OnTVFormRender event */
  72. $placeholders['onTVFormRender'] = $this->fireRenderEvent();
  73. return $placeholders;
  74. }
  75. public function getElementSources() {
  76. $c = $this->modx->newQuery('modContext');
  77. $c->where(array(
  78. 'key:!=' => 'mgr',
  79. ));
  80. $c->sortby($this->modx->escape('rank'),'ASC');
  81. $c->sortby($this->modx->escape('key'),'DESC');
  82. $contexts = $this->modx->getCollection('modContext',$c);
  83. $list = array();
  84. $this->modx->loadClass('sources.modMediaSource');
  85. /** @var $source modMediaSource */
  86. $source = modMediaSource::getDefaultSource($this->modx);
  87. /** @var modContext $context */
  88. foreach ($contexts as $context) {
  89. $list[] = array(
  90. $context->get('key'),
  91. $source->get('id'),
  92. $source->get('name'),
  93. );
  94. }
  95. return $list;
  96. }
  97. /**
  98. * Invoke OnTVFormPrerender event
  99. * @return void
  100. */
  101. public function firePreRenderEvents() {
  102. /* PreRender events inject directly into the HTML, as opposed to the JS-based Render event which injects HTML
  103. into the panel */
  104. $this->onTVFormPrerender = $this->modx->invokeEvent('OnTVFormPrerender',array(
  105. 'id' => 0,
  106. 'mode' => modSystemEvent::MODE_NEW,
  107. ));
  108. if (is_array($this->onTVFormPrerender)) $this->onTVFormPrerender = implode('',$this->onTVFormPrerender);
  109. $this->setPlaceholder('onTVFormPrerender', $this->onTVFormPrerender);
  110. }
  111. /**
  112. * Invoke OnTVFormRender event
  113. * @return string
  114. */
  115. public function fireRenderEvent() {
  116. $this->onTVFormRender = $this->modx->invokeEvent('OnTVFormRender',array(
  117. 'id' => 0,
  118. 'mode' => modSystemEvent::MODE_NEW,
  119. ));
  120. if (is_array($this->onTVFormRender)) $this->onTVFormRender = implode('',$this->onTVFormRender);
  121. $this->onTVFormRender = str_replace(array('"',"\n","\r"),array('\"','',''),$this->onTVFormRender);
  122. return $this->onTVFormRender;
  123. }
  124. /**
  125. * Return the pagetitle
  126. *
  127. * @return string
  128. */
  129. public function getPageTitle() {
  130. return $this->modx->lexicon('tv_new');
  131. }
  132. /**
  133. * Return the location of the template file
  134. * @return string
  135. */
  136. public function getTemplateFile() {
  137. return 'element/tv/create.tpl';
  138. }
  139. /**
  140. * Specify the language topics to load
  141. * @return array
  142. */
  143. public function getLanguageTopics() {
  144. return array('tv','category','tv_widget','propertyset','element');
  145. }
  146. /**
  147. * Get the Help URL
  148. * @return string
  149. */
  150. public function getHelpUrl() {
  151. return 'Template+Variables';
  152. }
  153. }