PageRenderTime 446ms CodeModel.GetById 91ms app.highlight 135ms RepoModel.GetById 117ms app.codeStats 1ms

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