Source for file filter.php

Documentation is available at filter.php

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

Documentation generated on Tue, 19 Nov 2013 15:03:15 +0100 by phpDocumentor 1.4.3