PageRenderTime 51ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/sly/Controller/Linkmap.php

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