update.class.php 7.7 KB

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