PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 3ms RepoModel.GetById 37ms app.codeStats 0ms

/sally/core/lib/sly/Form/Widget/LinkBase.php

https://bitbucket.org/SallyCMS/trunk
PHP | 85 lines | 39 code | 11 blank | 35 comment | 3 complexity | e7075e2322ef0911326ceb5d5bd40d10 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
11/**
12 * Link widget
13 *
14 * This element will render a special widget that allows the user to select
15 * one article. The article will be returned without any language information,
16 * so only its ID is returned.
17 * Selection will be performed in the so-called 'linkmap', a special popup for
18 * browsing through the article structure.
19 *
20 * @ingroup form
21 * @author  Christoph
22 */
23abstract class sly_Form_Widget_LinkBase extends sly_Form_ElementBase {
24	protected $types      = array();
25	protected $categories = array();
26
27	/**
28	 * @return sly_Form_Widget_LinkBase  the widget itself
29	 */
30	public function filterByCategories(array $cats, $recursive = false) {
31		foreach ($cats as $cat) $this->filterByCategory($cat, $recursive);
32		return $this;
33	}
34
35	/**
36	 * @return sly_Form_Widget_LinkBase  the widget itself
37	 */
38	public function filterByCategory($cat, $recursive = false) {
39		$catID = $cat instanceof sly_Model_Category ? $cat->getId() : (int) $cat;
40
41		if (!$recursive) {
42			if (!in_array($catID, $this->categories)) {
43				$this->categories[] = $catID;
44			}
45		}
46		else {
47			$serv = sly_Core::getContainer()->getCategoryService();
48			$tree = $serv->findTree($catID);
49
50			foreach ($tree as $cat) {
51				$this->categories[] = $cat->getId();
52			}
53
54			$this->categories = array_unique($this->categories);
55		}
56
57		return $this;
58	}
59
60	/**
61	 * @return sly_Form_Widget_LinkBase  the widget itself
62	 */
63	public function filterByArticleTypes(array $types) {
64		foreach ($types as $type) $this->types[] = $type;
65		$this->types = array_unique($this->types);
66
67		return $this;
68	}
69
70	/**
71	 * @return sly_Form_Widget_LinkBase  the widget itself
72	 */
73	public function clearCategoryFilter() {
74		$this->categories = array();
75		return $this;
76	}
77
78	/**
79	 * @return sly_Form_Widget_LinkBase  the widget itself
80	 */
81	public function clearArticleTypeFilter() {
82		$this->types = array();
83		return $this;
84	}
85}