/blog/core/components/handyman/controllers/resource/list.class.php

https://bitbucket.org/orchdork10159/dnsman.ly · PHP · 229 lines · 210 code · 14 blank · 5 comment · 12 complexity · 9537e25655cd4514ce7c6c22c1f1f837 MD5 · raw file

  1. <?php
  2. class hmcResourceList extends hmController {
  3. protected $cache = false;
  4. protected $templateFile = 'resource/list';
  5. public $parent;
  6. public $context;
  7. public $contexts = array();
  8. public $start;
  9. public $limit;
  10. public $list;
  11. public function getPageTitle() {
  12. return 'Listing Resources';
  13. }
  14. public function setup() {
  15. if (empty($this->config['gpc']['ctx'])) {
  16. $this->context = null;
  17. } else {
  18. $this->context = $this->config['gpc']['ctx'];
  19. }
  20. $this->modx->lexicon->load('default','resource');
  21. return true;
  22. }
  23. public function process() {
  24. $parent = (isset($this->config['get']['parent'])) ? (int)$this->config['get']['parent'] : 0;
  25. $start = (isset($this->config['get']['start'])) ? $this->config['get']['start'] : null;
  26. $limit = (isset($this->config['get']['limit'])) ? $this->config['get']['limit'] : null;
  27. $list = (isset($this->config['get']['list'])) ? $this->config['get']['list'] : null;
  28. /* First make sure we got a context to load from. */
  29. if ($this->context === null) {
  30. $this->contexts = $this->listContexts();
  31. // If we have multiple contexts show the contexts listing and halt the processing here.
  32. if (count($this->contexts) > 1) {
  33. $headerRow = array(
  34. array(hmController::LIST_DIVIDER => 'Choose a Context')
  35. );
  36. $contextMap = array_merge($headerRow,$this->contexts);
  37. $this->setPlaceholder('contexts',$this->processActions($contextMap));
  38. $this->templateFile = 'resource/contexts';
  39. return;
  40. // If we have exactly one context let's just go with that one and skip a click.
  41. } elseif (count($this->contexts) == 1) {
  42. $this->context = $this->contexts[0]['text'];
  43. }
  44. }
  45. if ($parent > 0) {
  46. /** @var modResource $current */
  47. $current = $this->modx->getObject('modResource',$parent);
  48. $this->setPlaceholders($current->toArray());
  49. /* Set up a breadcrumbs trail */
  50. $parents = $this->modx->getParentIds($parent, 10, array('context' => $this->context));
  51. $trail = array();
  52. foreach ($parents as $p) {
  53. if ($p > 0) {
  54. $obj = $this->modx->getObject('modResource',$p);
  55. if ($obj instanceof modResource) {
  56. $phs = array_merge($this->placeholders,array('resid' => $p, 'ctx' => $this->context, 'title' => $obj->get('pagetitle')));
  57. $trail[] = $this->hm->getTpl('widgets/crumbsli',$phs);
  58. }
  59. }
  60. }
  61. $trail[] = $this->hm->getTpl('widgets/crumbsli',array_merge($this->placeholders,array('ctx' => $this->context, 'title' => $this->context)));
  62. $trail = implode("\n",array_reverse($trail));
  63. $trail = $this->hm->getTpl('widgets/crumbsouter',array('wrapper' => $trail));
  64. $this->setPlaceholder('crumbs',$trail);
  65. $pubstate = (boolean)$current->get('published');
  66. $deleted = (boolean)$current->get('deleted');
  67. $resEditMap = array(
  68. array(
  69. hmController::LIST_DIVIDER => $this->modx->lexicon('options'),
  70. ),
  71. array (
  72. 'action' => 'resource/view',
  73. 'text' => $this->modx->lexicon->exists('resource_overview') ? $this->modx->lexicon('resource_overview') : $this->modx->lexicon('resource_view'),
  74. 'linkparams' => array(
  75. 'ctx' => $this->context,
  76. 'rid' => $parent,
  77. ),
  78. 'icon' => 'grid',
  79. 'permission' => $this->modx->hasPermission('view_document'),
  80. ),
  81. array(
  82. 'action' => 'resource/update',
  83. 'text' => $this->modx->lexicon('resource_edit'),
  84. 'linkparams' => array(
  85. 'ctx' => $this->context,
  86. 'rid' => $parent,
  87. ),
  88. 'icon' => 'gear',
  89. 'permission' => $this->modx->hasPermission('save_document'),
  90. ),
  91. array(
  92. 'action' => 'resource/preview',
  93. 'text' => $this->modx->lexicon('resource_view'),
  94. 'linkparams' => array(
  95. 'rid' => $parent,
  96. ),
  97. 'icon' => 'arrow-r',
  98. 'target' => '_blank',
  99. 'permission' => $this->modx->hasPermission('view_document'),
  100. ),
  101. array(
  102. 'action' => 'resource/create',
  103. 'text' => $this->modx->lexicon('document_create_here'),
  104. 'linkparams' => array(
  105. 'ctx' => $this->context,
  106. 'parent' => $parent,
  107. ),
  108. 'icon' => 'plus',
  109. 'permission' => $this->modx->hasPermission('new_document'),
  110. ),
  111. array(
  112. hmController::LIST_DIVIDER => 'Quick Options',
  113. ),
  114. array(
  115. 'action' => 'resource/publish',
  116. 'text' => ($pubstate) ? $this->modx->lexicon('resource_unpublish') : $this->modx->lexicon('resource_publish'),
  117. 'linkparams' => array(
  118. 'ctx' => $this->context,
  119. 'rid' => $parent,
  120. ),
  121. 'icon' => 'star',
  122. 'dialog' => true,
  123. 'permission' => $this->modx->hasPermission('publish_document'),
  124. ),
  125. array(
  126. 'action' => 'resource/delete',
  127. 'text' => ($deleted) ? $this->modx->lexicon('resource_undelete') : $this->modx->lexicon('resource_delete'),
  128. 'linkparams' => array(
  129. 'ctx' => $this->context,
  130. 'rid' => $parent
  131. ),
  132. 'icon' => 'delete',
  133. 'dialog' => true,
  134. 'permission' => $this->modx->hasPermission('delete_document'),
  135. ),
  136. );
  137. $this->setPlaceholder('actions',$this->processActions($resEditMap));
  138. $this->setPlaceholder('view',$this->hm->getTpl('resource/list.view.resource',$this->getPlaceholders()));
  139. } else {
  140. $parent = 0;
  141. $this->setPlaceholder('context_key',$this->context);
  142. $contextActionMap = array(
  143. array(
  144. hmController::LIST_DIVIDER => $this->modx->lexicon('options'),
  145. ),
  146. array(
  147. 'action' => 'resource/create',
  148. 'text' => $this->modx->lexicon('document_create_here'),
  149. 'linkparams' => array(
  150. 'ctx' => $this->context,
  151. 'parent' => 0,
  152. ),
  153. 'icon' => 'plus',
  154. 'permission' => $this->modx->hasPermission('new_document'),
  155. )
  156. );
  157. $this->setPlaceholder('actions',$this->processActions($contextActionMap));
  158. $this->setPlaceholder('view',$this->hm->getTpl('resource/list.view.context',$this->getPlaceholders()));
  159. }
  160. $subResources = $this->listResources($parent);
  161. $resources = '';
  162. if (count($subResources) > 0) {
  163. $parentText = ($parent > 0) ? $this->getPlaceholder('pagetitle') : $this->context;
  164. $header = array(array(hmController::LIST_DIVIDER => 'Listing Resources in '.$parentText));
  165. $resources = $this->processActions(array_merge($header,$subResources));
  166. }
  167. $this->setPlaceholder('resources',$resources);
  168. }
  169. public function listResources($parent = 0) {
  170. $c = $this->modx->newQuery('modResource');
  171. $c->where(array(
  172. 'context_key' => $this->context,
  173. 'parent' => $parent,
  174. ));
  175. $c->sortby('menuindex','asc');
  176. $resources = array();
  177. $ress = $this->modx->getCollection('modResource',$c);
  178. foreach ($ress as $res) {
  179. $aside = array();
  180. $aside[] = ($res->get('published')) ? 'Published' : 'Unpublished';
  181. if ($res->get('deleted')) { $aside[] = 'Deleted'; }
  182. if ($res->get('hidemenu')) { $aside[] = 'Hidden from menu'; }
  183. $aside = implode(", ",$aside);
  184. $count = $res->hasChildren();
  185. $resources[] = array(
  186. 'action' => 'resource/list',
  187. 'text' => $res->get('pagetitle').' ('.$res->get('id').')',
  188. 'aside' => $aside,
  189. 'linkparams' => array('ctx' => $this->context, 'parent' => $res->get('id')),
  190. 'object' => $this->context,
  191. 'count' => $count
  192. );
  193. }
  194. return $resources;
  195. }
  196. public function listContexts() {
  197. $c = $this->modx->newQuery('modContext');
  198. $c->where(array(
  199. 'key:!=' => 'mgr',
  200. ));
  201. $contexts = array();
  202. $contextobjects = $this->modx->getCollection('modContext',$c);
  203. foreach ($contextobjects as $ctx) {
  204. $count = $this->modx->getCount('modResource',array('context_key' => $ctx->get('key')));
  205. $contexts[] = array(
  206. 'action' => 'resource/list',
  207. 'text' => $ctx->get('key'),
  208. 'linkparams' => array('ctx' => $ctx->get('key')),
  209. 'object' => $ctx,
  210. 'count' => $count
  211. );
  212. }
  213. return $contexts;
  214. }
  215. }