PageRenderTime 183ms CodeModel.GetById 100ms app.highlight 3ms RepoModel.GetById 77ms app.codeStats 0ms

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

https://bitbucket.org/SallyCMS/trunk
PHP | 84 lines | 41 code | 11 blank | 32 comment | 3 complexity | 18c8a39fe9d206e50fd5c12cf8956130 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 * Media widget
13 *
14 * This element will render a special widget that allows the user to select
15 * a file from the mediapool. The handled value is the file's name, not its ID.
16 *
17 * @ingroup form
18 * @author  Christoph
19 */
20abstract class sly_Form_Widget_MediaBase extends sly_Form_ElementBase {
21	protected $filetypes  = array();
22	protected $categories = array();
23
24	/**
25	 * @return sly_Form_Widget_MediaBase  the widget itself
26	 */
27	public function filterByCategories(array $cats, $recursive = false) {
28		foreach ($cats as $cat) $this->filterByCategory($cat, $recursive);
29		return $this;
30	}
31
32	/**
33	 * @return sly_Form_Widget_MediaBase  the widget itself
34	 */
35	public function filterByCategory($cat, $recursive = false) {
36		$catID = $cat instanceof sly_Model_MediaCategory ? $cat->getId() : (int) $cat;
37
38		if (!$recursive) {
39			if (!in_array($catID, $this->categories)) {
40				$this->categories[] = $catID;
41			}
42		}
43		else {
44			$serv = sly_Core::getContainer()->getMediaCategoryService();
45			$tree = $serv->findTree($catID, false);
46
47			foreach ($tree as $id) {
48				$this->categories[] = $id;
49			}
50
51			$this->categories = array_unique($this->categories);
52		}
53
54		return $this;
55	}
56
57	/**
58	 * @return sly_Form_Widget_MediaBase  the widget itself
59	 */
60	public function filterByFiletypes(array $types) {
61		foreach ($types as $type) {
62			$this->filetypes[] = sly_Util_Mime::getType('tmp.'.ltrim($type, '.'));
63		}
64
65		$this->filetypes = array_unique($this->filetypes);
66		return $this;
67	}
68
69	/**
70	 * @return sly_Form_Widget_MediaBase  the widget itself
71	 */
72	public function clearCategoryFilter() {
73		$this->categories = array();
74		return $this;
75	}
76
77	/**
78	 * @return sly_Form_Widget_MediaBase  the widget itself
79	 */
80	public function clearFiletypeFilter() {
81		$this->filetypes = array();
82		return $this;
83	}
84}