PageRenderTime 74ms CodeModel.GetById 40ms app.highlight 16ms RepoModel.GetById 14ms app.codeStats 1ms

/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
 11class sly_Controller_Linkmap extends sly_Controller_Backend implements sly_Controller_Interface {
 12	protected $globals     = null;
 13	protected $tree        = array();
 14	protected $categories  = array();
 15	protected $types       = array();
 16	protected $roots       = array();
 17	protected $forced      = array();
 18	protected $popupHelper = array();
 19	protected $category    = null;
 20	protected $router      = null;
 21	protected $path        = null; // @edge navi path
 22
 23	public function setContainer(sly_Container $container) {
 24		parent::setContainer($container);
 25		$this->router = $this->getContainer()->getApplication()->getRouter();
 26	}
 27
 28	protected function init() {
 29		$request    = $this->getRequest();
 30		$dispatcher = $this->container->getDispatcher();
 31		$params     = array('callback' => 'string', 'args' => 'array');
 32
 33		$this->popupHelper = new sly_Helper_Popup($params, 'SLY_LINKMAP_URL_PARAMS');
 34		$this->popupHelper->init($request, $dispatcher);
 35
 36		// init category filter
 37		$cats = $this->popupHelper->getArgument('categories');
 38
 39		// do NOT use empty(), as '0' is a valid value!
 40		if (strlen($cats) > 0) {
 41			$cats = array_map('intval', explode('|', $cats));
 42
 43			foreach (array_unique($cats) as $catID) {
 44				$cat = sly_Util_Category::findById($catID);
 45				if ($cat) $this->categories[] = $catID;
 46			}
 47		}
 48
 49		// init article type filter
 50		$types = $this->popupHelper->getArgument('types');
 51
 52		if (!empty($types)) {
 53			$types       = explode('|', $types);
 54			$this->types = array_unique($types);
 55		}
 56
 57		// generate list of categories that have to be opened (in case we have
 58		// a deeply nested allow category that would otherwise be unreachable)
 59
 60		foreach ($this->categories as $catID) {
 61			if (in_array($catID, $this->forced)) continue;
 62
 63			$category = sly_Util_Category::findById($catID);
 64			if (!$category) continue;
 65
 66			$root = null;
 67
 68			foreach ($category->getParentTree() as $cat) {
 69				if ($root === null) $root = $cat->getId();
 70				$this->forced[] = $cat->getId();
 71			}
 72
 73			$this->roots[] = $root;
 74			$this->forced  = array_unique($this->forced);
 75			$this->roots   = array_unique($this->roots);
 76		}
 77
 78		$catID     = $this->getGlobals('category_id', 0);
 79		$naviPath  = '<ul class="sly-navi-path">';
 80		$isRoot    = $catID === 0;
 81		$category  = $isRoot ? null : sly_Util_Category::findById($catID);
 82
 83		// respect category filter
 84		if ($category === null || (!empty($this->categories) && !in_array($category->getId(), $this->forced))) {
 85			$category = reset($this->categories);
 86			$category = sly_Util_Category::findById($category);
 87		}
 88
 89		$naviPath .= '<li>'.t('path').'</li>';
 90
 91		if (empty($this->categories) || in_array(0, $this->categories)) {
 92			$link      = $this->url(array('category_id' => 0));
 93			$naviPath .= '<li><a href="'.$link.'">'.t('home').'</a></li>';
 94		}
 95		else {
 96			$naviPath .= '<li><span>'.t('home').'</span></li>';
 97		}
 98
 99		if ($category) {
100			$root = null;
101
102			foreach ($category->getParentTree() as $cat) {
103				$id = $cat->getId();
104
105				$this->tree[]   = $id;
106				$this->forced[] = $id;
107
108				if (empty($this->categories) || in_array($id, $this->categories)) {
109					$link      = $this->url(array('category_id' => $id));
110					$naviPath .= '<li><a href="'.$link.'">'.sly_html($cat->getName()).'</a></li>';
111				}
112				else {
113					$naviPath .= '<li><span>'.sly_html($cat->getName()).'</span></li>';
114				}
115
116				if ($root === null) $root = $id;
117			}
118
119			$this->roots[] = $root;
120			$this->forced  = array_unique($this->forced);
121			$this->roots   = array_unique($this->roots);
122		}
123
124		if (empty($this->categories)) {
125			$this->roots = sly_Util_Category::getRootCategories();
126		}
127
128		$this->category = $category;
129
130		$naviPath .= '</ul>';
131		$layout    = $this->getContainer()->getLayout();
132
133		// @edge navi path
134		$this->path = $naviPath;
135
136		$layout->setBodyAttr('class', 'sly-popup');
137		$layout->showNavigation(false);
138		$layout->pageHeader(t('linkmap')/*, $naviPath*/);
139	}
140
141	protected function getGlobals($key = null, $default = null) {
142		if ($this->globals === null) {
143			$this->globals = array(
144				'page'        => 'linkmap',
145				'category_id' => $this->getRequest()->request('category_id', 'int', 0),
146				'clang'       => $this->container->getCurrentLanguageID()
147			);
148		}
149
150		if ($key !== null) {
151			return isset($this->globals[$key]) ? $this->globals[$key] : $default;
152		}
153
154		return $this->globals;
155	}
156
157	public function indexAction() {
158		$this->init();
159
160		$this->render('linkmap/javascript.phtml', array(), false);
161		$this->render('linkmap/index.phtml', array(), false);
162	}
163
164	public function checkPermission($action) {
165		$user = $this->getCurrentUser();
166		return $user && ($user->isAdmin() || $user->hasRight('pages', 'structure'));
167	}
168
169	protected function url($local = array()) {
170		$globals = $this->getGlobals();
171		$extra   = $this->popupHelper->getValues();
172		$params  = array_merge($globals, $extra, $local);
173
174		return $this->router->getUrl('linkmap', 'index', $params);
175	}
176
177	protected function formatLabel($object) {
178		$user  = $this->getCurrentUser();
179		$label = trim($object->getName());
180
181		if (empty($label)) $label = '&nbsp;';
182
183		if (sly_Util_Article::isValid($object) && !$object->hasType()) {
184			$label .= ' ['.t('no_articletype').']';
185		}
186
187		return $label;
188	}
189
190	protected function tree($children, $level = 1) {
191		$ul = '';
192
193		if (is_array($children)) {
194			foreach ($children as $idx => $cat) {
195				if (!($cat instanceof sly_Model_Category)) {
196					$cat = sly_Util_Category::findById($cat);
197				}
198
199				if (!empty($this->categories) && !in_array($cat->getId(), $this->forced)) {
200					unset($children[$idx]);
201					continue;
202				}
203
204				$children[$idx] = $cat;
205			}
206
207			$children = array_values($children);
208			$len      = count($children);
209			$li       = '';
210
211			foreach ($children as $idx => $cat) {
212				$cat_children = $cat->getChildren();
213				$cat_id       = $cat->getId();
214				$classes      = array('lvl-'.$level);
215				$sub_li       = '';
216
217				if ($idx === 0) {
218					$classes[] = 'first';
219				}
220
221				if ($idx === $len-1) {
222					$classes[] = 'last';
223				}
224
225				$hasForcedChildren = false;
226				$isVisitable       = empty($this->categories) || in_array($cat_id, $this->categories);
227
228				foreach ($cat_children as $child) {
229					if (in_array($child->getId(), $this->forced)) {
230						$hasForcedChildren = true;
231						break;
232					}
233				}
234
235				if ($hasForcedChildren) {
236					$classes[] = 'children';
237				}
238				else {
239					$classes[] = 'empty';
240				}
241
242				if (in_array($cat_id, $this->tree) || ($hasForcedChildren && !$isVisitable)) {
243					$sub_li    = $this->tree($cat_children, $level + 1);
244					$classes[] = 'active';
245
246					if ($cat_id == end($this->tree)) {
247						$classes[] = 'leaf';
248					}
249				}
250
251				$classes[] = $cat->isOnline() ? 'sly-online' : 'sly-offline';
252				$label     = $this->formatLabel($cat);
253
254				if (!empty($classes)) $classes = ' class="'.implode(' ', $classes).'"';
255				else $classes = '';
256
257				$li .= '<li class="lvl-'.$level.'">';
258
259				if ($isVisitable) {
260					$li .= '<a'.$classes.' href="'.$this->url(array('category_id' => $cat_id)).'">'.sly_html($label).'</a>';
261				}
262				else {
263					$li .= '<span'.$classes.'>'.sly_html($label).'</span>';
264				}
265
266				$li .= $sub_li;
267				$li .= '</li>';
268			}
269
270			if (!empty($li)) {
271				$ul = "<ul>$li</ul>";
272			}
273		}
274
275		return $ul;
276	}
277}