Source for file category.php

Documentation is available at category.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Libraries
  4.  * @subpackage  HTML
  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('JPATH_BASE'or die;
  11.  
  12. /**
  13.  * Utility class for categories
  14.  *
  15.  * @package     Joomla.Libraries
  16.  * @subpackage  HTML
  17.  * @since       1.5
  18.  */
  19. abstract class JHtmlCategory
  20. {
  21.     /**
  22.      * Cached array of the category items.
  23.      *
  24.      * @var    array 
  25.      * @since  1.5
  26.      */
  27.     protected static $items array();
  28.  
  29.     /**
  30.      * Returns an array of categories for the given extension.
  31.      *
  32.      * @param   string  $extension  The extension option e.g. com_something.
  33.      * @param   array   $config     An array of configuration options. By default, only
  34.      *                               published and unpublished categories are returned.
  35.      *
  36.      * @return  array 
  37.      *
  38.      * @since   1.5
  39.      */
  40.     public static function options($extension$config array('filter.published' => array(01)))
  41.     {
  42.         $hash md5($extension '.' serialize($config));
  43.  
  44.         if (!isset(static::$items[$hash]))
  45.         {
  46.             $config = (array) $config;
  47.             $db JFactory::getDbo();
  48.             $query $db->getQuery(true)
  49.                 ->select('a.id, a.title, a.level')
  50.                 ->from('#__categories AS a')
  51.                 ->where('a.parent_id > 0');
  52.  
  53.             // Filter on extension.
  54.             $query->where('extension = ' $db->quote($extension));
  55.  
  56.             // Filter on the published state
  57.             if (isset($config['filter.published']))
  58.             {
  59.                 if (is_numeric($config['filter.published']))
  60.                 {
  61.                     $query->where('a.published = ' . (int) $config['filter.published']);
  62.                 }
  63.                 elseif (is_array($config['filter.published']))
  64.                 {
  65.                     JArrayHelper::toInteger($config['filter.published']);
  66.                     $query->where('a.published IN (' implode(','$config['filter.published']')');
  67.                 }
  68.             }
  69.  
  70.             // Filter on the language
  71.             if (isset($config['filter.language']))
  72.             {
  73.                 if (is_string($config['filter.language']))
  74.                 {
  75.                     $query->where('a.language = ' $db->quote($config['filter.language']));
  76.                 }
  77.                 elseif (is_array($config['filter.language']))
  78.                 {
  79.                     foreach ($config['filter.language'as &$language)
  80.                     {
  81.                         $language $db->quote($language);
  82.                     }
  83.                     $query->where('a.language IN (' implode(','$config['filter.language']')');
  84.                 }
  85.             }
  86.  
  87.             $query->order('a.lft');
  88.  
  89.             $db->setQuery($query);
  90.             $items $db->loadObjectList();
  91.  
  92.             // Assemble the list options.
  93.             static::$items[$hasharray();
  94.  
  95.             foreach ($items as &$item)
  96.             {
  97.                 $repeat ($item->level >= 0$item->level 0;
  98.                 $item->title str_repeat('- '$repeat$item->title;
  99.                 static::$items[$hash][JHtml::_('select.option'$item->id$item->title);
  100.             }
  101.         }
  102.  
  103.         return static::$items[$hash];
  104.     }
  105.  
  106.     /**
  107.      * Returns an array of categories for the given extension.
  108.      *
  109.      * @param   string  $extension  The extension option.
  110.      * @param   array   $config     An array of configuration options. By default, only published and unpublished categories are returned.
  111.      *
  112.      * @return  array   Categories for the extension
  113.      *
  114.      * @since   1.6
  115.      */
  116.     public static function categories($extension$config array('filter.published' => array(01)))
  117.     {
  118.         $hash md5($extension '.' serialize($config));
  119.  
  120.         if (!isset(static::$items[$hash]))
  121.         {
  122.             $config = (array) $config;
  123.             $db JFactory::getDbo();
  124.             $query $db->getQuery(true)
  125.                 ->select('a.id, a.title, a.level, a.parent_id')
  126.                 ->from('#__categories AS a')
  127.                 ->where('a.parent_id > 0');
  128.  
  129.             // Filter on extension.
  130.             $query->where('extension = ' $db->quote($extension));
  131.  
  132.             // Filter on the published state
  133.             if (isset($config['filter.published']))
  134.             {
  135.                 if (is_numeric($config['filter.published']))
  136.                 {
  137.                     $query->where('a.published = ' . (int) $config['filter.published']);
  138.                 }
  139.                 elseif (is_array($config['filter.published']))
  140.                 {
  141.                     JArrayHelper::toInteger($config['filter.published']);
  142.                     $query->where('a.published IN (' implode(','$config['filter.published']')');
  143.                 }
  144.             }
  145.  
  146.             $query->order('a.lft');
  147.  
  148.             $db->setQuery($query);
  149.             $items $db->loadObjectList();
  150.  
  151.             // Assemble the list options.
  152.             static::$items[$hasharray();
  153.  
  154.             foreach ($items as &$item)
  155.             {
  156.                 $repeat ($item->level >= 0$item->level 0;
  157.                 $item->title str_repeat('- '$repeat$item->title;
  158.                 static::$items[$hash][JHtml::_('select.option'$item->id$item->title);
  159.             }
  160.             // Special "Add to root" option:
  161.             static::$items[$hash][JHtml::_('select.option''1'JText::_('JLIB_HTML_ADD_TO_ROOT'));
  162.         }
  163.  
  164.         return static::$items[$hash];
  165.     }
  166. }

Documentation generated on Tue, 19 Nov 2013 14:55:17 +0100 by phpDocumentor 1.4.3