create.class.php 5.0 KB

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