update.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /**
  3. * Update controller for Collections Container
  4. *
  5. * @package collections
  6. * @subpackage controller
  7. */
  8. class CollectionContainerUpdateManagerController extends ResourceUpdateManagerController {
  9. /** @var modResource $resource */
  10. public $resource;
  11. /** @var Collections $collections */
  12. public $collections;
  13. public function initialize()
  14. {
  15. $corePath = $this->modx->getOption('collections.core_path', null, $this->modx->getOption('core_path', null, MODX_CORE_PATH) . 'components/collections/');
  16. $this->collections = $this->modx->getService(
  17. 'collections',
  18. 'Collections',
  19. $corePath . 'model/collections/',
  20. array(
  21. 'core_path' => $corePath
  22. )
  23. );
  24. parent::initialize();
  25. }
  26. public function getLanguageTopics() {
  27. return array('resource','collections:default', 'collections:templates', 'collections:custom', 'collections:selections');
  28. }
  29. /**
  30. * Register custom CSS/JS for the page
  31. * @return void
  32. */
  33. public function loadCustomCssJs() {
  34. $managerUrl = $this->context->getOption('manager_url', MODX_MANAGER_URL, $this->modx->_userConfig);
  35. $collectionsAssetsUrl = $this->modx->getOption('collections.assets_url',null,$this->modx->getOption('assets_url',null,MODX_ASSETS_URL).'components/collections/');
  36. $connectorUrl = $collectionsAssetsUrl.'connector.php';
  37. $collectionsJsUrl = $collectionsAssetsUrl.'js/mgr/';
  38. $this->addCss($collectionsAssetsUrl . 'css/mgr.css');
  39. $userCSS = $this->modx->getOption('collections.user_css', '');
  40. if ($userCSS != '') {
  41. $this->addCss($userCSS);
  42. }
  43. $this->addJavascript($managerUrl.'assets/modext/util/datetime.js');
  44. $this->addJavascript($managerUrl.'assets/modext/widgets/element/modx.panel.tv.renders.js');
  45. $this->addJavascript($managerUrl.'assets/modext/widgets/resource/modx.grid.resource.security.local.js');
  46. $this->addJavascript($managerUrl.'assets/modext/widgets/resource/modx.panel.resource.tv.js');
  47. $this->addJavascript($managerUrl.'assets/modext/widgets/resource/modx.panel.resource.js');
  48. $this->addJavascript($managerUrl.'assets/modext/sections/resource/update.js');
  49. $this->addJavascript($collectionsJsUrl.'collections.js');
  50. $this->addLastJavascript($collectionsJsUrl.'sections/category/update.js');
  51. $this->addLastJavascript($collectionsJsUrl.'widgets/category/collections.panel.category.js');
  52. $this->addLastJavascript($collectionsJsUrl.'widgets/category/collections.grid.resources.js');
  53. $this->addLastJavascript($collectionsJsUrl.'widgets/category/collections.window.js');
  54. $this->addLastJavascript($collectionsJsUrl.'extra/collections.combo.js');
  55. $this->addLastJavascript($collectionsJsUrl.'extra/griddraganddrop.js');
  56. $this->addLastJavascript($collectionsJsUrl.'extra/collections.renderers.js');
  57. $userJS = $this->modx->getOption('collections.user_js', '');
  58. if ($userJS != '') {
  59. $this->addLastJavascript($userJS);
  60. }
  61. $collectionsTemplate = $this->getCollectionsTemplate();
  62. $this->loadConfig();
  63. $this->addHtml('
  64. <script type="text/javascript">
  65. // <![CDATA[
  66. collections.assetsUrl = "'.$collectionsAssetsUrl.'";
  67. collections.connectorUrl = "'.$connectorUrl.'";
  68. collections.config = '.$this->modx->toJSON($this->collections->config).';
  69. collections.config.connector_url = "'.$this->collections->config['connectorUrl'].'";
  70. MODx.config.publish_document = "'.$this->canPublish.'";
  71. MODx.onDocFormRender = "'.$this->onDocFormRender.'";
  72. MODx.ctx = "'.$this->resource->get('context_key').'";
  73. collections.template = ' . $collectionsTemplate . ';
  74. Ext.onReady(function() {
  75. MODx.load({
  76. xtype: "collections-page-category-update"
  77. ,resource: "'.$this->resource->get('id').'"
  78. ,record: '.$this->modx->toJSON($this->resourceArray).'
  79. ,publish_document: "'.$this->canPublish.'"
  80. ,preview_url: "'.$this->previewUrl.'"
  81. ,locked: '.($this->locked ? 1 : 0).'
  82. ,lockedText: "'.$this->lockedText.'"
  83. ,canSave: '.($this->canSave ? 1 : 0).'
  84. ,canEdit: '.($this->canEdit ? 1 : 0).'
  85. ,canCreate: '.($this->canCreate ? 1 : 0).'
  86. ,canDuplicate: '.($this->canDuplicate ? 1 : 0).'
  87. ,canDelete: '.($this->canDelete ? 1 : 0).'
  88. ,show_tvs: '.(!empty($this->tvCounts) ? 1 : 0).'
  89. ,mode: "update"
  90. });
  91. });
  92. // ]]>
  93. </script>');
  94. /* load RTE */
  95. $this->loadRichTextEditor();
  96. }
  97. public function getCollectionsTemplate() {
  98. $template = $this->collections->getCollectionsView($this->resource);
  99. $c = $this->modx->newQuery('CollectionTemplateColumn');
  100. $c->sortby('position', 'ASC');
  101. $c->where(array(
  102. 'template' => $template->id
  103. ));
  104. /** @var CollectionTemplateColumn[] $columns */
  105. $columns = $this->modx->getIterator('CollectionTemplateColumn', $c);
  106. $derivates = array();
  107. if ($template->resource_type_selection) {
  108. $response = $this->modx->runProcessor('mgr/extra/getderivates', array(
  109. 'skip' => 'modXMLRPCResource',
  110. 'class' => 'modResource',
  111. ), array(
  112. 'processors_path' => $this->collections->getOption('processorsPath'),
  113. ));
  114. $response = $this->modx->fromJSON($response->response);
  115. if ($response != '') {
  116. if ($template->allowed_resource_types == '') {
  117. foreach ($response['results'] as $type) {
  118. $derivates[] = $type;
  119. }
  120. } else {
  121. $allowedTypes = $this->collections->explodeAndClean($template->allowed_resource_types);
  122. foreach ($allowedTypes as $type) {
  123. if (isset($response['results'][$type])) {
  124. $derivates[] = $response['results'][$type];
  125. }
  126. }
  127. }
  128. }
  129. }
  130. $parent = !empty($template->parent) ? $template->parent : '';
  131. if (substr($parent,0,8) == '@SNIPPET'){
  132. $snippet = trim(substr($parent,8));
  133. $properties = array(
  134. 'resource' => & $this->resource
  135. );
  136. $parent = $this->modx->runSnippet($snippet,$properties);
  137. }
  138. $parent_context = $this->resource->get('context_key');
  139. if (!empty($parent)){
  140. //we have a custom parent - try to get the context_key
  141. if ($p_resource = $this->modx->getObject('modResource',$parent)){
  142. $parent_context = $p_resource->get('context_key');
  143. }
  144. }else{
  145. $parent = $this->resource->get('id');
  146. }
  147. $templateOptions = array(
  148. 'fields' => array('actions', 'action_edit', 'preview_url', 'menu_actions', 'icons', 'has_children'),
  149. 'columns' => array(),
  150. 'sort' => array(
  151. 'field' => $template->sort_field . ':' . $template->sort_type,
  152. 'dir' => $template->sort_dir,
  153. ),
  154. 'pageSize' => $template->page_size,
  155. 'bulkActions' => $template->bulk_actions,
  156. 'allowDD' => $template->allow_dd,
  157. 'resource_type_selection' => $template->resource_type_selection,
  158. 'show_quick_create' => $template->show_quick_create,
  159. 'quick_create_label' => $template->quick_create_label,
  160. 'children' => array(
  161. 'template' => $template->child_template,
  162. 'resource_type' => $template->child_resource_type,
  163. 'rich_text' => $template->child_richtext,
  164. 'cacheable' => $template->child_cacheable,
  165. 'content_type' => $template->child_content_type,
  166. 'published' => $template->child_published,
  167. 'content_disposition' => $template->child_content_disposition,
  168. 'hide_from_menu' => $template->child_hide_from_menu,
  169. 'searchable' => $template->child_searchable
  170. ),
  171. 'tab_label' => $template->tab_label,
  172. 'button_label' => $template->button_label,
  173. 'link_label' => $template->link_label,
  174. 'content_place' => $template->content_place,
  175. 'context_menu' => $this->collections->explodeAndClean($template->context_menu, ',', 1),
  176. 'resourceDerivatives' => $derivates,
  177. 'selection_create_sort' => $template->selection_create_sort,
  178. 'parent' => $parent,
  179. 'parent_context' => $parent_context,
  180. 'permanent_sort' => array (
  181. 'before' => $template->permanent_sort_before,
  182. 'after' => $template->permanent_sort_after,
  183. ),
  184. 'search_query_exclude_tvs' => $template->search_query_exclude_tvs,
  185. 'search_query_exclude_tagger' => $template->search_query_exclude_tagger,
  186. 'search_query_title_only' => $template->search_query_title_only,
  187. );
  188. foreach ($columns as $column) {
  189. $templateOptions['fields'][] = $column->name;
  190. $header = $this->modx->lexicon($column->label);
  191. if ($header == null) {
  192. $header = $column->label;
  193. }
  194. $columnDef = array(
  195. 'header' => $header,
  196. 'dataIndex' => $column->name,
  197. 'hidden' => $column->hidden,
  198. 'sortable' => $column->sortable,
  199. 'width' => $column->width,
  200. );
  201. if ($column->editor != '') {
  202. $editorObj = $this->modx->fromJSON($column->editor);
  203. if ($editorObj == null) {
  204. $editorObj = array(
  205. 'xtype' => $column->editor,
  206. 'renderer' => false
  207. );
  208. }
  209. $columnDef['editor'] = $editorObj;
  210. }
  211. if ($column->renderer != '') {
  212. $columnDef['renderer'] = $column->renderer;
  213. }
  214. $templateOptions['columns'][] = $columnDef;
  215. }
  216. return $this->modx->toJSON($templateOptions);
  217. }
  218. public function loadConfig() {
  219. $config = $this->modx->getObject('CollectionSetting', array('collection' => $this->resource->id));
  220. if ($config) {
  221. $this->resourceArray['collections_template'] = $config->template;
  222. }
  223. }
  224. }