PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

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