update.class.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. * Loads the Media Sources page
  12. *
  13. * @package modx
  14. * @subpackage manager.controllers
  15. */
  16. class SourceUpdateManagerController extends modManagerController {
  17. /** @var modMediaSource $source */
  18. public $source;
  19. /** @var array $sourceArray An array of fields for the source */
  20. public $sourceArray = array();
  21. /** @var array $sourceDefaultProperties The default properties on the source */
  22. public $sourceDefaultProperties = array();
  23. /**
  24. * Check for any permissions or requirements to load page
  25. * @return bool
  26. */
  27. public function checkPermissions() {
  28. return $this->modx->hasPermission('source_edit');
  29. }
  30. /**
  31. * Register custom CSS/JS for the page
  32. * @return void
  33. */
  34. public function loadCustomCssJs() {
  35. $mgrUrl = $this->modx->getOption('manager_url',null,MODX_MANAGER_URL);
  36. $this->addJavascript($mgrUrl.'assets/modext/widgets/core/modx.grid.local.property.js');
  37. $this->addJavascript($mgrUrl.'assets/modext/widgets/source/modx.grid.source.properties.js');
  38. $this->addJavascript($mgrUrl.'assets/modext/widgets/source/modx.grid.source.access.js');
  39. $this->addJavascript($mgrUrl.'assets/modext/widgets/source/modx.panel.source.js');
  40. $this->addJavascript($mgrUrl.'assets/modext/sections/source/update.js');
  41. $this->addHtml('<script type="text/javascript">Ext.onReady(function() {MODx.load({
  42. xtype: "modx-page-source-update"
  43. ,record: '.$this->modx->toJSON($this->sourceArray).'
  44. ,defaultProperties: '.$this->modx->toJSON($this->sourceDefaultProperties).'
  45. });});</script>');
  46. }
  47. /**
  48. * Custom logic code here for setting placeholders, etc
  49. * @param array $scriptProperties
  50. * @return mixed
  51. */
  52. public function process(array $scriptProperties = array()) {
  53. if (empty($this->scriptProperties['id']) || strlen($this->scriptProperties['id']) !== strlen((integer)$this->scriptProperties['id'])) {
  54. return $this->failure($this->modx->lexicon('source_err_ns'));
  55. }
  56. $this->source = $this->modx->getObject('sources.modMediaSource', array('id' => $this->scriptProperties['id']));
  57. if (empty($this->source)) return $this->failure($this->modx->lexicon('source_err_nf'));
  58. $this->sourceArray = $this->source->toArray();
  59. $this->getProperties();
  60. $this->getAccess();
  61. $this->getDefaultProperties();
  62. return array();
  63. }
  64. public function getProperties() {
  65. $properties = $this->source->getProperties();
  66. $data = array();
  67. foreach ($properties as $property) {
  68. $data[] = array(
  69. $property['name'],
  70. !empty($property['desc']) ? $property['desc'] : '',
  71. !empty($property['type']) ? $property['type'] : 'textfield',
  72. !empty($property['options']) ? $property['options'] : array(),
  73. $property['value'],
  74. !empty($property['lexicon']) ? $property['lexicon'] : '',
  75. !empty($property['overridden']) ? $property['overridden'] : 0,
  76. !empty($property['desc_trans']) ? $property['desc_trans'] : '',
  77. !empty($property['name_trans']) ? $property['name_trans'] : '',
  78. );
  79. }
  80. $this->sourceArray['properties'] = $data;
  81. }
  82. public function getDefaultProperties() {
  83. $default = $this->source->getDefaultProperties();
  84. $default = $this->source->prepareProperties($default);
  85. $data = array();
  86. foreach ($default as $property) {
  87. $data[] = array(
  88. $property['name'],
  89. !empty($property['desc']) ? $property['desc'] : '',
  90. !empty($property['type']) ? $property['type'] : 'textfield',
  91. !empty($property['options']) ? $property['options'] : array(),
  92. $property['value'],
  93. !empty($property['lexicon']) ? $property['lexicon'] : '',
  94. 0,
  95. !empty($property['desc_trans']) ? $property['desc_trans'] : '',
  96. !empty($property['name_trans']) ? $property['name_trans'] : '',
  97. );
  98. }
  99. $this->sourceDefaultProperties = $data;
  100. return $data;
  101. }
  102. public function getAccess() {
  103. $c = $this->modx->newQuery('sources.modAccessMediaSource');
  104. $c->innerJoin('sources.modMediaSource','Target');
  105. $c->innerJoin('modAccessPolicy','Policy');
  106. $c->innerJoin('modUserGroup','Principal');
  107. $c->innerJoin('modUserGroupRole','MinimumRole');
  108. $c->where(array(
  109. 'target' => $this->source->get('id'),
  110. ));
  111. $c->select($this->modx->getSelectColumns('sources.modAccessMediaSource','modAccessMediaSource'));
  112. $c->select(array(
  113. 'target_name' => 'Target.name',
  114. 'principal_name' => 'Principal.name',
  115. 'policy_name' => 'Policy.name',
  116. 'authority_name' => 'MinimumRole.name',
  117. ));
  118. $acls = $this->modx->getCollection('sources.modAccessMediaSource',$c);
  119. $access = array();
  120. /** @var modAccessMediaSource $acl */
  121. foreach ($acls as $acl) {
  122. $access[] = array(
  123. $acl->get('id'),
  124. $acl->get('target'),
  125. $acl->get('target_name'),
  126. $acl->get('principal_class'),
  127. $acl->get('principal'),
  128. $acl->get('principal_name'),
  129. $acl->get('authority'),
  130. $acl->get('authority_name'),
  131. $acl->get('policy'),
  132. $acl->get('policy_name'),
  133. $acl->get('context_key'),
  134. );
  135. }
  136. $this->sourceArray['access'] = $this->modx->toJSON($access);
  137. }
  138. /**
  139. * Return the pagetitle
  140. *
  141. * @return string
  142. */
  143. public function getPageTitle() {
  144. return $this->modx->lexicon('source_update');
  145. }
  146. /**
  147. * Return the location of the template file
  148. * @return string
  149. */
  150. public function getTemplateFile() {
  151. return '';
  152. }
  153. /**
  154. * Specify the language topics to load
  155. * @return array
  156. */
  157. public function getLanguageTopics() {
  158. return array('source','namespace','propertyset');
  159. }
  160. /**
  161. * Get the Help URL
  162. * @return string
  163. */
  164. public function getHelpUrl() {
  165. return 'Media+Sources';
  166. }
  167. }