PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/manager/controllers/default/element/tv/update.class.php

http://github.com/modxcms/revolution
PHP | 226 lines | 136 code | 21 blank | 69 comment | 9 complexity | fceda4399644b14cbfbf4c2af0474176 MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, BSD-3-Clause, LGPL-2.1
  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 template page
  12. *
  13. * @package modx
  14. * @subpackage manager.controllers
  15. */
  16. class ElementTVUpdateManagerController extends modManagerController {
  17. /** @var modCategory $category */
  18. public $category;
  19. /** @var modTemplateVar $tv */
  20. public $tv;
  21. /** @var array $tvArray */
  22. public $tvArray = array();
  23. /** @var string $onTVFormRender */
  24. public $onTVFormRender = '';
  25. /** @var string $onTVFormPrerender */
  26. public $onTVFormPrerender = '';
  27. /**
  28. * Check for any permissions or requirements to load page
  29. * @return bool
  30. */
  31. public function checkPermissions() {
  32. return $this->modx->hasPermission('edit_tv');
  33. }
  34. /**
  35. * Register custom CSS/JS for the page
  36. * @return void
  37. */
  38. public function loadCustomCssJs() {
  39. $mgrUrl = $this->modx->getOption('manager_url',null,MODX_MANAGER_URL);
  40. $this->addJavascript($mgrUrl.'assets/modext/widgets/core/modx.grid.local.property.js');
  41. $this->addJavascript($mgrUrl.'assets/modext/widgets/element/modx.grid.element.properties.js');
  42. $this->addJavascript($mgrUrl.'assets/modext/widgets/element/modx.grid.tv.template.js');
  43. $this->addJavascript($mgrUrl.'assets/modext/widgets/element/modx.grid.tv.security.js');
  44. $this->addJavascript($mgrUrl.'assets/modext/widgets/element/modx.panel.tv.js');
  45. $this->addJavascript($mgrUrl.'assets/modext/sections/element/tv/update.js');
  46. $this->addHtml('
  47. <script type="text/javascript">
  48. // <![CDATA[
  49. MODx.onTVFormRender = "'.$this->onTVFormRender.'";
  50. MODx.perm.tree_show_element_ids = '.($this->modx->hasPermission('tree_show_element_ids') ? 1 : 0).';
  51. MODx.perm.unlock_element_properties = "'.($this->modx->hasPermission('unlock_element_properties') ? 1 : 0).'";
  52. Ext.onReady(function() {
  53. MODx.load({
  54. xtype: "modx-page-tv-update"
  55. ,id: "'.$this->tvArray['id'].'"
  56. ,record: '.$this->modx->toJSON($this->tvArray).'
  57. });
  58. });
  59. // ]]>
  60. </script>');
  61. }
  62. /**
  63. * Custom logic code here for setting placeholders, etc
  64. * @param array $scriptProperties
  65. * @return mixed
  66. */
  67. public function process(array $scriptProperties = array()) {
  68. $placeholders = array();
  69. /* load tv */
  70. if (empty($scriptProperties['id']) || strlen($scriptProperties['id']) !== strlen((integer)$scriptProperties['id'])) {
  71. return $this->failure($this->modx->lexicon('tv_err_ns'));
  72. }
  73. $this->tv = $this->modx->getObject('modTemplateVar', array('id' => $scriptProperties['id']));
  74. if ($this->tv == null) return $this->failure($this->modx->lexicon('tv_err_nf'));
  75. if (!$this->tv->checkPolicy('view')) return $this->failure($this->modx->lexicon('access_denied'));
  76. /* get properties */
  77. $properties = $this->tv->get('properties');
  78. if (!is_array($properties)) $properties = array();
  79. $data = array();
  80. foreach ($properties as $property) {
  81. $data[] = array(
  82. $property['name'],
  83. $property['desc'],
  84. !empty($property['type']) ? $property['type'] : 'textfield',
  85. !empty($property['options']) ? $property['options'] : array(),
  86. $property['value'],
  87. !empty($property['lexicon']) ? $property['lexicon'] : '',
  88. false, /* overridden set to false */
  89. $property['desc_trans'],
  90. !empty($property['area']) ? $property['area'] : '',
  91. !empty($property['area_trans']) ? $property['area_trans'] : '',
  92. );
  93. }
  94. $this->tvArray = $this->tv->toArray();
  95. $this->tvArray['properties'] = $data;
  96. $this->tvArray['default_text'] = $this->tv->getContent();
  97. $this->tvArray['sources'] = $this->getElementSources();
  98. $this->prepareElement();
  99. /* load tv into parser */
  100. $placeholders['tv'] = $this->tv;
  101. /* invoke OnTVFormRender event */
  102. $placeholders['onTVFormRender'] = $this->fireRenderEvent();
  103. return $placeholders;
  104. }
  105. /**
  106. * Prepare the element and get the static openTo path if needed
  107. *
  108. * @return void|string
  109. */
  110. public function prepareElement() {
  111. $this->tvArray['openTo'] = '/';
  112. if (!empty($this->tvArray['static'])) {
  113. $file = $this->tv->get('static_file');
  114. $this->tvArray['openTo'] = dirname($file).'/';
  115. }
  116. return $this->tvArray['openTo'];
  117. }
  118. public function getElementSources() {
  119. $c = $this->modx->newQuery('modContext');
  120. $c->leftJoin('sources.modMediaSourceElement','SourceElements',array(
  121. 'SourceElements.object' => $this->tv->get('id'),
  122. 'SourceElements.object_class' => $this->tv->_class,
  123. 'SourceElements.context_key = modContext.key',
  124. ));
  125. $c->leftJoin('sources.modMediaSource','Source','SourceElements.source = Source.id');
  126. $c->select($this->modx->getSelectColumns('modContext','modContext'));
  127. $c->select($this->modx->getSelectColumns('sources.modMediaSourceElement','SourceElements'));
  128. $c->select($this->modx->getSelectColumns('sources.modMediaSource','Source','',array('name')));
  129. $c->where(array(
  130. 'key:!=' => 'mgr',
  131. ));
  132. $c->sortby($this->modx->escape('rank'),'ASC');
  133. $c->sortby($this->modx->escape('key'),'DESC');
  134. $contexts = $this->modx->getCollection('modContext',$c);
  135. $list = array();
  136. /** @var modContext $context */
  137. foreach ($contexts as $context) {
  138. $source = $context->get('source');
  139. $list[] = array(
  140. $context->get('key'),
  141. !empty($source) ? $source : $this->modx->getOption('default_media_source',null,1),
  142. $context->get('name'),
  143. );
  144. }
  145. return $list;
  146. }
  147. /**
  148. * Invoke OnTVFormPrerender event
  149. * @return void
  150. */
  151. public function firePreRenderEvents() {
  152. /* PreRender events inject directly into the HTML, as opposed to the JS-based Render event which injects HTML
  153. into the panel */
  154. $this->onTVFormPrerender = $this->modx->invokeEvent('OnTVFormPrerender',array(
  155. 'id' => $this->tvArray['id'],
  156. 'tv' => &$this->tv,
  157. 'mode' => modSystemEvent::MODE_UPD,
  158. ));
  159. if (is_array($this->onTVFormPrerender)) $this->onTVFormPrerender = implode('',$this->onTVFormPrerender);
  160. $this->setPlaceholder('onTVFormPrerender', $this->onTVFormPrerender);
  161. }
  162. /**
  163. * Invoke OnTVFormRender event
  164. * @return string
  165. */
  166. public function fireRenderEvent() {
  167. $this->onTVFormRender = $this->modx->invokeEvent('OnTVFormRender',array(
  168. 'id' => $this->tvArray['id'],
  169. 'tv' => &$this->tv,
  170. 'mode' => modSystemEvent::MODE_UPD,
  171. ));
  172. if (is_array($this->onTVFormRender)) $this->onTVFormRender = implode('',$this->onTVFormRender);
  173. $this->onTVFormRender = str_replace(array('"',"\n","\r"),array('\"','',''),$this->onTVFormRender);
  174. return $this->onTVFormRender;
  175. }
  176. /**
  177. * Return the pagetitle
  178. *
  179. * @return string
  180. */
  181. public function getPageTitle() {
  182. return $this->modx->lexicon('tv').': '.$this->tvArray['name'];
  183. }
  184. /**
  185. * Return the location of the template file
  186. * @return string
  187. */
  188. public function getTemplateFile() {
  189. return 'element/tv/update.tpl';
  190. }
  191. /**
  192. * Specify the language topics to load
  193. * @return array
  194. */
  195. public function getLanguageTopics() {
  196. return array('tv','category','tv_widget','propertyset','element');
  197. }
  198. /**
  199. * Get the Help URL
  200. * @return string
  201. */
  202. public function getHelpUrl() {
  203. return 'Template+Variables';
  204. }
  205. }