PageRenderTime 112ms CodeModel.GetById 37ms app.highlight 14ms RepoModel.GetById 56ms app.codeStats 1ms

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

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