PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Filter/Plugin/List.php

https://github.com/zikula-modules/Clip
PHP | 202 lines | 117 code | 7 blank | 78 comment | 21 complexity | 20fc7571ef2b786121b9f33fe8b12ad5 MD5 | raw file
  1. <?php
  2. /**
  3. * Clip
  4. *
  5. * @copyright (c) Clip Team
  6. * @link http://github.com/zikula-modules/clip/
  7. * @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
  8. * @package Clip
  9. * @subpackage Filter_Plugin
  10. */
  11. namespace Matheo\Clip\Filter\Plugin;
  12. use Matheo\Clip\Filter\FormFilter;
  13. use ZLanguage;
  14. use CategoryUtil;
  15. use StringUtil;
  16. use ModUtil;
  17. use Matheo\Clip\Util;
  18. use Matheo\Clip\Util\PluginsUtil;
  19. /**
  20. * List category selector plugin.
  21. *
  22. * This plugin creates a category selector using a drop down list.
  23. * The selected value of the base drop down list will be set to ID of the selected category.
  24. */
  25. class List extends ListDropdown
  26. {
  27. /**
  28. * Base category.
  29. *
  30. * May be the id, the category array or the path.
  31. *
  32. * @var mixed
  33. */
  34. public $category;
  35. /**
  36. * Enable inclusion of an empty null value element.
  37. *
  38. * @var boolean (default true)
  39. */
  40. public $includeEmptyElement;
  41. /**
  42. * Enable inclusion of categories with no publications.
  43. *
  44. * @var boolean (default false)
  45. */
  46. public $includeEmpty;
  47. /**
  48. * Show the quantity of publications inside the category.
  49. *
  50. * @var boolean (default false)
  51. */
  52. public $showQuantity;
  53. /**
  54. * Get filename of this file.
  55. *
  56. * @return string
  57. */
  58. public function getFilename()
  59. {
  60. return __FILE__;
  61. }
  62. /**
  63. * Load the parameters.
  64. *
  65. * static method called by other category based plugins.
  66. *
  67. * @param object &$list The list object (here: $this).
  68. * @param boolean $includeEmptyElement Whether or not to include an empty null item.
  69. * @param array $params The parameters passed from the Smarty plugin.
  70. * @param FormFilter $filter Clip filter form manager instance.
  71. *
  72. * @return void
  73. */
  74. public static function loadParameters(
  75. &$list,
  76. $includeEmptyElement,
  77. $params,
  78. $filter
  79. ) {
  80. $all = isset($params['all']) ? $params['all'] : false;
  81. $lang = isset($params['lang']) ? $params['lang'] : ZLanguage::getLanguageCode();
  82. $list->category = isset($params['category']) ? $params['category'] : 0;
  83. $includeLeaf = isset($params['includeLeaf']) ? $params['includeLeaf'] : true;
  84. $includeRoot = isset($params['includeRoot']) ? $params['includeRoot'] : false;
  85. $includeEmpty = isset($params['includeEmpty']) ? $params['includeEmpty'] : false;
  86. $showQuantity = isset($params['showQuantity']) ? $params['showQuantity'] : false;
  87. $path = isset($params['path']) ? $params['path'] : '';
  88. $pathfield = isset($params['pathfield']) ? $params['pathfield'] : 'path';
  89. $recurse = isset($params['recurse']) ? $params['recurse'] : true;
  90. $relative = isset($params['relative']) ? $params['relative'] : true;
  91. $sortField = isset($params['sortField']) ? $params['sortField'] : 'sort_value';
  92. $catField = isset($params['catField']) ? $params['catField'] : 'id';
  93. $allCats = array();
  94. // if we don't have a category-id we see if we can get a category by path
  95. if (!$list->category && $path) {
  96. $list->category = CategoryUtil::getCategoryByPath($path, $pathfield);
  97. $allCats = CategoryUtil::getSubCategoriesForCategory($list->category, $recurse, $relative, $includeRoot, $includeLeaf, $all, null, '', null, $sortField);
  98. } elseif (is_array($list->category) && isset($list->category['id']) && is_integer($list->category['id'])) {
  99. // check if we have an actual category object with a numeric ID set
  100. $allCats = CategoryUtil::getSubCategoriesForCategory($list->category, $recurse, $relative, $includeRoot, $includeLeaf, $all, null, '', null, $sortField);
  101. } elseif (is_numeric($list->category)) {
  102. // check if we have a numeric category
  103. $list->category = CategoryUtil::getCategoryByID($list->category);
  104. $allCats = CategoryUtil::getSubCategoriesForCategory($list->category, $recurse, $relative, $includeRoot, $includeLeaf, $all, null, '', null, $sortField);
  105. } elseif (is_string($list->category) && strpos($list->category, '/') === 0) {
  106. // check if we have a string/path category
  107. $list->category = CategoryUtil::getCategoryByPath($list->category, $pathfield);
  108. $allCats = CategoryUtil::getSubCategoriesForCategory($list->category, $recurse, $relative, $includeRoot, $includeLeaf, $all, null, '', null, $sortField);
  109. }
  110. if (!$allCats) {
  111. $allCats = array();
  112. }
  113. $line = '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -';
  114. if ($includeEmptyElement) {
  115. $list->addItem('', null);
  116. }
  117. // check the indent level
  118. $maxlevel = 0;
  119. foreach ($allCats as &$c) {
  120. $c['ilevel'] = StringUtil::countInstances(isset($c['ipath_relative']) ? $c['ipath_relative'] : $c['ipath'], '/');
  121. // grab the max one
  122. if ($maxlevel < $c['ilevel']) {
  123. $maxlevel = $c['ilevel'];
  124. }
  125. }
  126. $emptylvl = 99;
  127. foreach ($allCats as $cat) {
  128. // check if we need to omit the category
  129. if (!$includeEmpty) {
  130. if ($emptylvl < $cat['ilevel']) {
  131. continue;
  132. } else {
  133. $emptylvl = 99;
  134. }
  135. }
  136. // count the publications inside the category
  137. $op = $cat['ilevel'] < $maxlevel ? 'sub' : 'eq';
  138. $res = ModUtil::apiFunc('Clip', 'user', 'getall', array('tid' => $filter->getTid(), 'countmode' => 'just', 'filter' => "{$list->field}:{$op}:{$cat['id']},core_online:eq:1,core_visible:eq:1,core_intrash:eq:0", 'checkperm' => false, 'handleplugins' => false));
  139. if (!$includeEmpty && $res['pubcount'] == 0) {
  140. $emptylvl = $cat['ilevel'];
  141. continue;
  142. }
  143. $indent = '';
  144. if ($cat['ilevel'] > 0) {
  145. $indent = '| ' . substr($line, 0, $cat['ilevel'] * 2);
  146. }
  147. $catName = html_entity_decode(isset($cat['display_name'][$lang]) ? $cat['display_name'][$lang] : $cat['name']);
  148. if ($showQuantity) {
  149. $catName .= " ({$res['pubcount']})";
  150. }
  151. $list->addItem($indent . ' ' . $catName, isset($cat[$catField]) ? $cat[$catField] : $cat['id']);
  152. }
  153. }
  154. /**
  155. * Create event handler.
  156. *
  157. * @param array $params Parameters passed from the Smarty plugin function.
  158. * @param Clip_Filter_Form $filter Clip filter form manager instance.
  159. *
  160. * @return void
  161. */
  162. public function create($params, $filter)
  163. {
  164. $this->includeEmptyElement = isset($params['includeEmptyElement']) ? $params['includeEmptyElement'] : true;
  165. $this->op = array_key_exists('op', $params) ? $params['op'] : 'eq';
  166. if (!isset($params['category']) || !$params['category']) {
  167. $field = Util::getPubFields($filter->getTid(), $this->field);
  168. $plugin = PluginsUtil::get($field['fieldplugin']);
  169. $params['category'] = $plugin->getRootCategoryID($field['typedata']);
  170. }
  171. self::loadParameters($this, $this->includeEmptyElement, $params, $filter);
  172. parent::create($params, $filter);
  173. }
  174. /**
  175. * Render event handler.
  176. *
  177. * @param \Zikula_View $view Reference to Zikula_View object.
  178. *
  179. * @return string The rendered output
  180. */
  181. public function render(\Zikula_View $view)
  182. {
  183. // adds the form observer
  184. $filter = $view->get_registered_object('clip_filter');
  185. $filterid = $filter->getFilterID($this->field);
  186. if ($filter->hasPlugin($this->field, $this->id . '_op')) {
  187. $code = "\$('{$filterid}').value = '{$this->field}:'+\$F('{$this->id}_op')+':'+\$F('{$this->id}');";
  188. } else {
  189. $code = "\$('{$filterid}').value = '{$this->field}:{$this->op}:'+\$F('{$this->id}');";
  190. }
  191. $code = "if (\$F('{$this->id}') != '#null#') { {$code} }";
  192. $filter->addFormObserver($code);
  193. return parent::render($view);
  194. }
  195. }