PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/components/com_finder/helpers/html/filter.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 497 lines | 325 code | 71 blank | 101 comment | 30 complexity | 822061ad8a66d029d8d8b8d6e4e81ba9 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Site
  4. * @subpackage com_finder
  5. *
  6. * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('_JEXEC') or die;
  10. JLoader::register('FinderHelperLanguage', JPATH_ADMINISTRATOR . '/components/com_finder/helpers/language.php');
  11. /**
  12. * Filter HTML Behaviors for Finder.
  13. *
  14. * @package Joomla.Site
  15. * @subpackage com_finder
  16. * @since 2.5
  17. */
  18. abstract class JHtmlFilter
  19. {
  20. /**
  21. * Method to generate filters using the slider widget and decorated
  22. * with the FinderFilter JavaScript behaviors.
  23. *
  24. * @param array $options An array of configuration options. [optional]
  25. *
  26. * @return mixed A rendered HTML widget on success, null otherwise.
  27. *
  28. * @since 2.5
  29. */
  30. public static function slider($options = array())
  31. {
  32. $db = JFactory::getDbo();
  33. $query = $db->getQuery(true);
  34. $user = JFactory::getUser();
  35. $groups = implode(',', $user->getAuthorisedViewLevels());
  36. $html = '';
  37. $filter = null;
  38. // Get the configuration options.
  39. $filterId = array_key_exists('filter_id', $options) ? $options['filter_id'] : null;
  40. $activeNodes = array_key_exists('selected_nodes', $options) ? $options['selected_nodes'] : array();
  41. $classSuffix = array_key_exists('class_suffix', $options) ? $options['class_suffix'] : '';
  42. $loadMedia = array_key_exists('load_media', $options) ? $options['load_media'] : true;
  43. // Load the predefined filter if specified.
  44. if (!empty($filterId))
  45. {
  46. $query->select('f.data, f.params')
  47. ->from($db->quoteName('#__finder_filters') . ' AS f')
  48. ->where('f.filter_id = ' . (int) $filterId);
  49. // Load the filter data.
  50. $db->setQuery($query);
  51. try
  52. {
  53. $filter = $db->loadObject();
  54. }
  55. catch (RuntimeException $e)
  56. {
  57. return null;
  58. }
  59. // Initialize the filter parameters.
  60. if ($filter)
  61. {
  62. $registry = new JRegistry;
  63. $registry->loadString($filter->params);
  64. $filter->params = $registry;
  65. }
  66. }
  67. // Build the query to get the branch data and the number of child nodes.
  68. $query->clear()
  69. ->select('t.*, count(c.id) AS children')
  70. ->from($db->quoteName('#__finder_taxonomy') . ' AS t')
  71. ->join('INNER', $db->quoteName('#__finder_taxonomy') . ' AS c ON c.parent_id = t.id')
  72. ->where('t.parent_id = 1')
  73. ->where('t.state = 1')
  74. ->where('t.access IN (' . $groups . ')')
  75. ->where('c.state = 1')
  76. ->where('c.access IN (' . $groups . ')')
  77. ->group('t.id, t.parent_id, t.state, t.access, t.ordering, t.title, c.parent_id')
  78. ->order('t.ordering, t.title');
  79. // Limit the branch children to a predefined filter.
  80. if ($filter)
  81. {
  82. $query->where('c.id IN(' . $filter->data . ')');
  83. }
  84. // Load the branches.
  85. $db->setQuery($query);
  86. try
  87. {
  88. $branches = $db->loadObjectList('id');
  89. }
  90. catch (RuntimeException $e)
  91. {
  92. return null;
  93. }
  94. // Check that we have at least one branch.
  95. if (count($branches) === 0)
  96. {
  97. return null;
  98. }
  99. // Load the CSS/JS resources.
  100. if ($loadMedia)
  101. {
  102. JHtml::_('stylesheet', 'com_finder/sliderfilter.css', false, true, false);
  103. JHtml::_('script', 'com_finder/sliderfilter.js', false, true);
  104. }
  105. // Load plug-in language files.
  106. FinderHelperLanguage::loadPluginLanguage();
  107. // Start the widget.
  108. $html .= '<div id="finder-filter-container">';
  109. $html .= '<dl id="branch-selectors">';
  110. $html .= '<dt>';
  111. $html .= '<label for="tax-select-all" class="checkbox">';
  112. $html .= '<input type="checkbox" id="tax-select-all" />';
  113. $html .= JText::_('COM_FINDER_FILTER_SELECT_ALL_LABEL');
  114. $html .= '</label>';
  115. $html .= '</dt>';
  116. $html .= '<div class="control-group">';
  117. // Iterate through the branches to build the branch selector.
  118. foreach ($branches as $bk => $bv)
  119. {
  120. // If the multi-lang plug-in is enabled then drop the language branch.
  121. if ($bv->title == 'Language' && JLanguageMultilang::isEnabled())
  122. {
  123. continue;
  124. }
  125. $html .= '<label for="tax-' . $bk . '" class="checkbox">';
  126. $html .= '<input type="checkbox" class="toggler" id="tax-' . $bk . '"/>';
  127. $html .= JText::sprintf('COM_FINDER_FILTER_BRANCH_LABEL', JText::_(FinderHelperLanguage::branchSingular($bv->title)));
  128. $html .= '</label>';
  129. }
  130. $html .= '</div>';
  131. $html .= '</dl>';
  132. $html .= '<div id="finder-filter-container">';
  133. // Iterate through the branches and build the branch groups.
  134. foreach ($branches as $bk => $bv)
  135. {
  136. // If the multi-lang plug-in is enabled then drop the language branch.
  137. if ($bv->title == 'Language' && JLanguageMultilang::isEnabled())
  138. {
  139. continue;
  140. }
  141. // Build the query to get the child nodes for this branch.
  142. $query->clear()
  143. ->select('t.*')
  144. ->from($db->quoteName('#__finder_taxonomy') . ' AS t')
  145. ->where('t.parent_id = ' . (int) $bk)
  146. ->where('t.state = 1')
  147. ->where('t.access IN (' . $groups . ')')
  148. ->order('t.ordering, t.title');
  149. // Load the branches.
  150. $db->setQuery($query);
  151. try
  152. {
  153. $nodes = $db->loadObjectList('id');
  154. }
  155. catch (RuntimeException $e)
  156. {
  157. return null;
  158. }
  159. // Translate node titles if possible.
  160. $lang = JFactory::getLanguage();
  161. foreach ($nodes as $nk => $nv)
  162. {
  163. $key = FinderHelperLanguage::branchPlural($nv->title);
  164. if ($lang->hasKey($key))
  165. {
  166. $nodes[$nk]->title = JText::_($key);
  167. }
  168. }
  169. // Start the group.
  170. $html .= '<dl class="checklist" rel="tax-' . $bk . '">';
  171. $html .= '<dt>';
  172. $html .= '<label for="tax-' . JFilterOutput::stringUrlSafe($bv->title) . '" class="checkbox">';
  173. $html .= '<input type="checkbox" class="branch-selector filter-branch' . $classSuffix . '" id="tax-' . JFilterOutput::stringUrlSafe($bv->title) . '" />';
  174. $html .= JText::sprintf('COM_FINDER_FILTER_BRANCH_LABEL', JText::_(FinderHelperLanguage::branchSingular($bv->title)));
  175. $html .= '</label>';
  176. $html .= '</dt>';
  177. $html .= '<div class="control-group">';
  178. // Populate the group with nodes.
  179. foreach ($nodes as $nk => $nv)
  180. {
  181. // Determine if the node should be checked.
  182. $checked = in_array($nk, $activeNodes) ? ' checked="checked"' : '';
  183. // Build a node.
  184. $html .= '<label for="tax-' . $nk . '" class="checkbox">';
  185. $html .= '<input class="selector filter-node' . $classSuffix . '" type="checkbox" value="' . $nk . '" name="t[]" id="tax-' . $nk . '"' . $checked . ' />';
  186. $html .= $nv->title;
  187. $html .= '</label>';
  188. }
  189. // Close the group.
  190. $html .= '</div>';
  191. $html .= '</dl>';
  192. }
  193. // Close the widget.
  194. $html .= '<div class="clr"></div>';
  195. $html .= '</div>';
  196. $html .= '</div>';
  197. return $html;
  198. }
  199. /**
  200. * Method to generate filters using select box drop down controls.
  201. *
  202. * @param FinderIndexerQuery $idxQuery A FinderIndexerQuery object.
  203. * @param array $options An array of options.
  204. *
  205. * @return mixed A rendered HTML widget on success, null otherwise.
  206. *
  207. * @since 2.5
  208. */
  209. public static function select($idxQuery, $options)
  210. {
  211. $user = JFactory::getUser();
  212. $groups = implode(',', $user->getAuthorisedViewLevels());
  213. $filter = null;
  214. // Get the configuration options.
  215. $classSuffix = $options->get('class_suffix', null);
  216. $loadMedia = $options->get('load_media', true);
  217. $showDates = $options->get('show_date_filters', false);
  218. // Try to load the results from cache.
  219. $cache = JFactory::getCache('com_finder', '');
  220. $cacheId = 'filter_select_' . serialize(array($idxQuery->filter, $options, $groups, JFactory::getLanguage()->getTag()));
  221. // Check the cached results.
  222. if (!($branches = $cache->get($cacheId)))
  223. {
  224. $db = JFactory::getDbo();
  225. $query = $db->getQuery(true);
  226. // Load the predefined filter if specified.
  227. if (!empty($idxQuery->filter))
  228. {
  229. $query->select('f.data, '. $db->quoteName('f.params'))
  230. ->from($db->quoteName('#__finder_filters') . ' AS f')
  231. ->where('f.filter_id = ' . (int) $idxQuery->filter);
  232. // Load the filter data.
  233. $db->setQuery($query);
  234. try
  235. {
  236. $filter = $db->loadObject();
  237. }
  238. catch (RuntimeException $e)
  239. {
  240. return null;
  241. }
  242. // Initialize the filter parameters.
  243. if ($filter)
  244. {
  245. $registry = new JRegistry;
  246. $registry->loadString($filter->params);
  247. $filter->params = $registry;
  248. }
  249. }
  250. // Build the query to get the branch data and the number of child nodes.
  251. $query->clear()
  252. ->select('t.*, count(c.id) AS children')
  253. ->from($db->quoteName('#__finder_taxonomy') . ' AS t')
  254. ->join('INNER', $db->quoteName('#__finder_taxonomy') . ' AS c ON c.parent_id = t.id')
  255. ->where('t.parent_id = 1')
  256. ->where('t.state = 1')
  257. ->where('t.access IN (' . $groups . ')')
  258. ->where('c.state = 1')
  259. ->where('c.access IN (' . $groups . ')')
  260. ->group($db->quoteName('t.id'))
  261. ->order('t.ordering, t.title');
  262. // Limit the branch children to a predefined filter.
  263. if (!empty($filter->data))
  264. {
  265. $query->where('c.id IN(' . $filter->data . ')');
  266. }
  267. // Load the branches.
  268. $db->setQuery($query);
  269. try
  270. {
  271. $branches = $db->loadObjectList('id');
  272. }
  273. catch (RuntimeException $e)
  274. {
  275. return null;
  276. }
  277. // Check that we have at least one branch.
  278. if (count($branches) === 0)
  279. {
  280. return null;
  281. }
  282. // Iterate through the branches and build the branch groups.
  283. foreach ($branches as $bk => $bv)
  284. {
  285. // If the multi-lang plug-in is enabled then drop the language branch.
  286. if ($bv->title == 'Language' && JLanguageMultilang::isEnabled())
  287. {
  288. continue;
  289. }
  290. // Build the query to get the child nodes for this branch.
  291. $query->clear()
  292. ->select('t.*')
  293. ->from($db->quoteName('#__finder_taxonomy') . ' AS t')
  294. ->where('t.parent_id = ' . (int) $bk)
  295. ->where('t.state = 1')
  296. ->where('t.access IN (' . $groups . ')')
  297. ->order('t.ordering, t.title');
  298. // Limit the nodes to a predefined filter.
  299. if (!empty($filter->data))
  300. {
  301. $query->where('t.id IN(' . $filter->data . ')');
  302. }
  303. // Load the branches.
  304. $db->setQuery($query);
  305. try
  306. {
  307. $branches[$bk]->nodes = $db->loadObjectList('id');
  308. }
  309. catch (RuntimeException $e)
  310. {
  311. return null;
  312. }
  313. // Translate branch nodes if possible.
  314. $language = JFactory::getLanguage();
  315. foreach ($branches[$bk]->nodes as $node_id => $node)
  316. {
  317. $key = FinderHelperLanguage::branchPlural($node->title);
  318. if ($language->hasKey($key))
  319. {
  320. $branches[$bk]->nodes[$node_id]->title = JText::_($key);
  321. }
  322. }
  323. // Add the Search All option to the branch.
  324. array_unshift($branches[$bk]->nodes, array('id' => null, 'title' => JText::_('COM_FINDER_FILTER_SELECT_ALL_LABEL')));
  325. }
  326. // Store the data in cache.
  327. $cache->store($branches, $cacheId);
  328. }
  329. $html = '';
  330. // Add the dates if enabled.
  331. if ($showDates)
  332. {
  333. $html .= JHtml::_('filter.dates', $idxQuery, $options);
  334. }
  335. $html .= '<div id="finder-filter-select-list" class="form-horizontal">';
  336. // Iterate through all branches and build code.
  337. foreach ($branches as $bk => $bv)
  338. {
  339. // If the multi-lang plug-in is enabled then drop the language branch.
  340. if ($bv->title == 'Language' && JLanguageMultilang::isEnabled())
  341. {
  342. continue;
  343. }
  344. $active = null;
  345. // Check if the branch is in the filter.
  346. if (array_key_exists($bv->title, $idxQuery->filters))
  347. {
  348. // Get the request filters.
  349. $temp = JFactory::getApplication()->input->request->get('t', array(), 'array');
  350. // Search for active nodes in the branch and get the active node.
  351. $active = array_intersect($temp, $idxQuery->filters[$bv->title]);
  352. $active = count($active) === 1 ? array_shift($active) : null;
  353. }
  354. $html .= '<div class="filter-branch' . $classSuffix . ' control-group">';
  355. $html .= '<label for="tax-' . JFilterOutput::stringUrlSafe($bv->title) . '" class="control-label">';
  356. $html .= JText::sprintf('COM_FINDER_FILTER_BRANCH_LABEL', JText::_(FinderHelperLanguage::branchSingular($bv->title)));
  357. $html .= '</label>';
  358. $html .= '<div class="controls">';
  359. $html .= JHtml::_('select.genericlist', $branches[$bk]->nodes, 't[]', 'class="inputbox"', 'id', 'title', $active, 'tax-' . JFilterOutput::stringUrlSafe($bv->title));
  360. $html .= '</div>';
  361. $html .= '</div>';
  362. }
  363. // Close the widget.
  364. $html .= '</div>';
  365. // Load the CSS/JS resources.
  366. if ($loadMedia)
  367. {
  368. JHtml::stylesheet('com_finder/sliderfilter.css', false, true, false);
  369. }
  370. return $html;
  371. }
  372. /**
  373. * Method to generate fields for filtering dates
  374. *
  375. * @param FinderIndexerQuery $idxQuery A FinderIndexerQuery object.
  376. * @param array $options An array of options.
  377. *
  378. * @return mixed A rendered HTML widget on success, null otherwise.
  379. *
  380. * @since 2.5
  381. */
  382. public static function dates($idxQuery, $options)
  383. {
  384. $html = '';
  385. // Get the configuration options.
  386. $classSuffix = $options->get('class_suffix', null);
  387. $loadMedia = $options->get('load_media', true);
  388. $showDates = $options->get('show_date_filters', false);
  389. if (!empty($showDates))
  390. {
  391. // Build the date operators options.
  392. $operators = array();
  393. $operators[] = JHtml::_('select.option', 'before', JText::_('COM_FINDER_FILTER_DATE_BEFORE'));
  394. $operators[] = JHtml::_('select.option', 'exact', JText::_('COM_FINDER_FILTER_DATE_EXACTLY'));
  395. $operators[] = JHtml::_('select.option', 'after', JText::_('COM_FINDER_FILTER_DATE_AFTER'));
  396. // Load the CSS/JS resources.
  397. if ($loadMedia)
  398. {
  399. JHtml::stylesheet('com_finder/dates.css', false, true, false);
  400. }
  401. // Open the widget.
  402. $html .= '<ul id="finder-filter-select-dates">';
  403. // Start date filter.
  404. $html .= '<li class="filter-date' . $classSuffix . '">';
  405. $html .= '<label for="filter_date1">';
  406. $html .= JText::_('COM_FINDER_FILTER_DATE1');
  407. $html .= '</label>';
  408. $html .= '<br />';
  409. $html .= JHtml::_('select.genericlist', $operators, 'w1', 'class="inputbox filter-date-operator"', 'value', 'text', $idxQuery->when1, 'finder-filter-w1');
  410. $html .= JHtml::calendar($idxQuery->date1, 'd1', 'filter_date1', '%Y-%m-%d', 'title="' . JText::_('COM_FINDER_FILTER_DATE1_DESC') . '"');
  411. $html .= '</li>';
  412. // End date filter.
  413. $html .= '<li class="filter-date' . $classSuffix . '">';
  414. $html .= '<label for="filter_date2">';
  415. $html .= JText::_('COM_FINDER_FILTER_DATE2');
  416. $html .= '</label>';
  417. $html .= '<br />';
  418. $html .= JHtml::_('select.genericlist', $operators, 'w2', 'class="inputbox filter-date-operator"', 'value', 'text', $idxQuery->when2, 'finder-filter-w2');
  419. $html .= JHtml::calendar($idxQuery->date2, 'd2', 'filter_date2', '%Y-%m-%d', 'title="' . JText::_('COM_FINDER_FILTER_DATE2_DESC') . '"');
  420. $html .= '</li>';
  421. // Close the widget.
  422. $html .= '</ul>';
  423. }
  424. return $html;
  425. }
  426. }