/sally/core/lib/sly/Form/Widget/MediaBase.php
PHP | 84 lines | 41 code | 11 blank | 32 comment | 3 complexity | e7a0b03a3549b4b54d7dd11e083ad88b MD5 | raw file
- <?php
- /*
- * Copyright (c) 2013, webvariants GbR, http://www.webvariants.de
- *
- * This file is released under the terms of the MIT license. You can find the
- * complete text in the attached LICENSE file or online at:
- *
- * http://www.opensource.org/licenses/mit-license.php
- */
- /**
- * Media widget
- *
- * This element will render a special widget that allows the user to select
- * a file from the mediapool. The handled value is the file's name, not its ID.
- *
- * @ingroup form
- * @author Christoph
- */
- abstract class sly_Form_Widget_MediaBase extends sly_Form_ElementBase {
- protected $filetypes = array();
- protected $categories = array();
- /**
- * @return sly_Form_Widget_MediaBase the widget itself
- */
- public function filterByCategories(array $cats, $recursive = false) {
- foreach ($cats as $cat) $this->filterByCategory($cat, $recursive);
- return $this;
- }
- /**
- * @return sly_Form_Widget_MediaBase the widget itself
- */
- public function filterByCategory($cat, $recursive = false) {
- $catID = $cat instanceof sly_Model_MediaCategory ? $cat->getId() : (int) $cat;
- if (!$recursive) {
- if (!in_array($catID, $this->categories)) {
- $this->categories[] = $catID;
- }
- }
- else {
- $serv = sly_Service_Factory::getMediaCategoryService();
- $tree = $serv->findTree($catID, false);
- foreach ($tree as $id) {
- $this->categories[] = $id;
- }
- $this->categories = array_unique($this->categories);
- }
- return $this;
- }
- /**
- * @return sly_Form_Widget_MediaBase the widget itself
- */
- public function filterByFiletypes(array $types) {
- foreach ($types as $type) {
- $this->filetypes[] = sly_Util_Mime::getType('tmp.'.ltrim($type, '.'));
- }
- $this->filetypes = array_unique($this->filetypes);
- return $this;
- }
- /**
- * @return sly_Form_Widget_MediaBase the widget itself
- */
- public function clearCategoryFilter() {
- $this->categories = array();
- return $this;
- }
- /**
- * @return sly_Form_Widget_MediaBase the widget itself
- */
- public function clearFiletypeFilter() {
- $this->filetypes = array();
- return $this;
- }
- }