PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

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