PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

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