PageRenderTime 73ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/sally/backend/lib/Controller/Linkmap.php

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