PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/sly/Controller/Linkmap.php

https://bitbucket.org/phoenixh/sallycms-backend
PHP | 277 lines | 200 code | 62 blank | 15 comment | 45 complexity | ada96b9c3038ea4c151ac89f57bbe36a MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright (c) 2014, webvariants GmbH & Co. KG, http://www.webvariants.de
  4. *
  5. * This file is released under the terms of the MIT license. You can find the
  6. * complete text in the attached LICENSE file or online at:
  7. *
  8. * http://www.opensource.org/licenses/mit-license.php
  9. */
  10. class sly_Controller_Linkmap extends sly_Controller_Backend implements sly_Controller_Interface {
  11. protected $globals = null;
  12. protected $tree = array();
  13. protected $categories = array();
  14. protected $types = array();
  15. protected $roots = array();
  16. protected $forced = array();
  17. protected $popupHelper = array();
  18. protected $category = null;
  19. protected $router = null;
  20. protected $path = null; // @edge navi path
  21. public function setContainer(sly_Container $container) {
  22. parent::setContainer($container);
  23. $this->router = $this->getContainer()->getApplication()->getRouter();
  24. }
  25. protected function init() {
  26. $request = $this->getRequest();
  27. $dispatcher = $this->container->getDispatcher();
  28. $params = array('callback' => 'string', 'args' => 'array');
  29. $this->popupHelper = new sly_Helper_Popup($params, 'SLY_LINKMAP_URL_PARAMS');
  30. $this->popupHelper->init($request, $dispatcher);
  31. // init category filter
  32. $cats = $this->popupHelper->getArgument('categories');
  33. // do NOT use empty(), as '0' is a valid value!
  34. if (strlen($cats) > 0) {
  35. $cats = array_map('intval', explode('|', $cats));
  36. foreach (array_unique($cats) as $catID) {
  37. $cat = sly_Util_Category::findById($catID);
  38. if ($cat) $this->categories[] = $catID;
  39. }
  40. }
  41. // init article type filter
  42. $types = $this->popupHelper->getArgument('types');
  43. if (!empty($types)) {
  44. $types = explode('|', $types);
  45. $this->types = array_unique($types);
  46. }
  47. // generate list of categories that have to be opened (in case we have
  48. // a deeply nested allow category that would otherwise be unreachable)
  49. foreach ($this->categories as $catID) {
  50. if (in_array($catID, $this->forced)) continue;
  51. $category = sly_Util_Category::findById($catID);
  52. if (!$category) continue;
  53. $root = null;
  54. foreach ($category->getParentTree() as $cat) {
  55. if ($root === null) $root = $cat->getId();
  56. $this->forced[] = $cat->getId();
  57. }
  58. $this->roots[] = $root;
  59. $this->forced = array_unique($this->forced);
  60. $this->roots = array_unique($this->roots);
  61. }
  62. $catID = $this->getGlobals('category_id', 0);
  63. $naviPath = '<ul class="sly-navi-path">';
  64. $isRoot = $catID === 0;
  65. $category = $isRoot ? null : sly_Util_Category::findById($catID);
  66. // respect category filter
  67. if ($category === null || (!empty($this->categories) && !in_array($category->getId(), $this->forced))) {
  68. $category = reset($this->categories);
  69. $category = sly_Util_Category::findById($category);
  70. }
  71. $naviPath .= '<li>'.t('path').'</li>';
  72. if (empty($this->categories) || in_array(0, $this->categories)) {
  73. $link = $this->url(array('category_id' => 0));
  74. $naviPath .= '<li><a href="'.$link.'">'.t('home').'</a></li>';
  75. }
  76. else {
  77. $naviPath .= '<li><span>'.t('home').'</span></li>';
  78. }
  79. if ($category) {
  80. $root = null;
  81. foreach ($category->getParentTree() as $cat) {
  82. $id = $cat->getId();
  83. $this->tree[] = $id;
  84. $this->forced[] = $id;
  85. if (empty($this->categories) || in_array($id, $this->categories)) {
  86. $link = $this->url(array('category_id' => $id));
  87. $naviPath .= '<li><a href="'.$link.'">'.sly_html($cat->getName()).'</a></li>';
  88. }
  89. else {
  90. $naviPath .= '<li><span>'.sly_html($cat->getName()).'</span></li>';
  91. }
  92. if ($root === null) $root = $id;
  93. }
  94. $this->roots[] = $root;
  95. $this->forced = array_unique($this->forced);
  96. $this->roots = array_unique($this->roots);
  97. }
  98. if (empty($this->categories)) {
  99. $this->roots = sly_Util_Category::getRootCategories();
  100. }
  101. $this->category = $category;
  102. $naviPath .= '</ul>';
  103. $layout = $this->getContainer()->getLayout();
  104. // @edge navi path
  105. $this->path = $naviPath;
  106. $layout->setBodyAttr('class', 'sly-popup');
  107. $layout->showNavigation(false);
  108. $layout->pageHeader(t('linkmap')/*, $naviPath*/);
  109. }
  110. protected function getGlobals($key = null, $default = null) {
  111. if ($this->globals === null) {
  112. $this->globals = array(
  113. 'page' => 'linkmap',
  114. 'category_id' => $this->getRequest()->request('category_id', 'int', 0),
  115. 'clang' => $this->container->getCurrentLanguageID()
  116. );
  117. }
  118. if ($key !== null) {
  119. return isset($this->globals[$key]) ? $this->globals[$key] : $default;
  120. }
  121. return $this->globals;
  122. }
  123. public function indexAction() {
  124. $this->init();
  125. $this->render('linkmap/javascript.phtml', array(), false);
  126. $this->render('linkmap/index.phtml', array(), false);
  127. }
  128. public function checkPermission($action) {
  129. $user = $this->getCurrentUser();
  130. return $user && ($user->isAdmin() || $user->hasRight('pages', 'structure'));
  131. }
  132. protected function url($local = array()) {
  133. $globals = $this->getGlobals();
  134. $extra = $this->popupHelper->getValues();
  135. $params = array_merge($globals, $extra, $local);
  136. return $this->router->getUrl('linkmap', 'index', $params);
  137. }
  138. protected function formatLabel($object) {
  139. $user = $this->getCurrentUser();
  140. $label = trim($object->getName());
  141. if (empty($label)) $label = '&nbsp;';
  142. if (sly_Util_Article::isValid($object) && !$object->hasType()) {
  143. $label .= ' ['.t('no_articletype').']';
  144. }
  145. return $label;
  146. }
  147. protected function tree($children, $level = 1) {
  148. $ul = '';
  149. if (is_array($children)) {
  150. foreach ($children as $idx => $cat) {
  151. if (!($cat instanceof sly_Model_Category)) {
  152. $cat = sly_Util_Category::findById($cat);
  153. }
  154. if (!empty($this->categories) && !in_array($cat->getId(), $this->forced)) {
  155. unset($children[$idx]);
  156. continue;
  157. }
  158. $children[$idx] = $cat;
  159. }
  160. $children = array_values($children);
  161. $len = count($children);
  162. $li = '';
  163. foreach ($children as $idx => $cat) {
  164. $cat_children = $cat->getChildren();
  165. $cat_id = $cat->getId();
  166. $classes = array('lvl-'.$level);
  167. $sub_li = '';
  168. if ($idx === 0) {
  169. $classes[] = 'first';
  170. }
  171. if ($idx === $len-1) {
  172. $classes[] = 'last';
  173. }
  174. $hasForcedChildren = false;
  175. $isVisitable = empty($this->categories) || in_array($cat_id, $this->categories);
  176. foreach ($cat_children as $child) {
  177. if (in_array($child->getId(), $this->forced)) {
  178. $hasForcedChildren = true;
  179. break;
  180. }
  181. }
  182. if ($hasForcedChildren) {
  183. $classes[] = 'children';
  184. }
  185. else {
  186. $classes[] = 'empty';
  187. }
  188. if (in_array($cat_id, $this->tree) || ($hasForcedChildren && !$isVisitable)) {
  189. $sub_li = $this->tree($cat_children, $level + 1);
  190. $classes[] = 'active';
  191. if ($cat_id == end($this->tree)) {
  192. $classes[] = 'leaf';
  193. }
  194. }
  195. $classes[] = $cat->isOnline() ? 'sly-online' : 'sly-offline';
  196. $label = $this->formatLabel($cat);
  197. if (!empty($classes)) $classes = ' class="'.implode(' ', $classes).'"';
  198. else $classes = '';
  199. $li .= '<li class="lvl-'.$level.'">';
  200. if ($isVisitable) {
  201. $li .= '<a'.$classes.' href="'.$this->url(array('category_id' => $cat_id)).'">'.sly_html($label).'</a>';
  202. }
  203. else {
  204. $li .= '<span'.$classes.'>'.sly_html($label).'</span>';
  205. }
  206. $li .= $sub_li;
  207. $li .= '</li>';
  208. }
  209. if (!empty($li)) {
  210. $ul = "<ul>$li</ul>";
  211. }
  212. }
  213. return $ul;
  214. }
  215. }