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