Source for file tag.php

Documentation is available at tag.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 tags
  14.  *
  15.  * @package     Joomla.Libraries
  16.  * @subpackage  HTML
  17.  * @since       3.1
  18.  */
  19. abstract class JHtmlTag
  20. {
  21.     /**
  22.      * Cached array of the tag items.
  23.      *
  24.      * @var    array 
  25.      * @since  3.1
  26.      */
  27.     protected static $items array();
  28.  
  29.     /**
  30.      * Returns an array of tags.
  31.      *
  32.      * @param   array  $config  An array of configuration options. By default, only
  33.      *                           published and unpublished categories are returned.
  34.      *
  35.      * @return  array 
  36.      *
  37.      * @since   3.1
  38.      */
  39.     public static function options($config array('filter.published' => array(01)))
  40.     {
  41.         $hash md5(serialize($config));
  42.  
  43.         if (!isset(static::$items[$hash]))
  44.         {
  45.             $config = (array) $config;
  46.             $db JFactory::getDbo();
  47.             $query $db->getQuery(true)
  48.                 ->select('a.id, a.title, a.level')
  49.                 ->from('#__tags AS a')
  50.                 ->where('a.parent_id > 0');
  51.  
  52.             // Filter on the published state
  53.             if (isset($config['filter.published']))
  54.             {
  55.                 if (is_numeric($config['filter.published']))
  56.                 {
  57.                     $query->where('a.published = ' . (int) $config['filter.published']);
  58.                 }
  59.                 elseif (is_array($config['filter.published']))
  60.                 {
  61.                     JArrayHelper::toInteger($config['filter.published']);
  62.                     $query->where('a.published IN (' implode(','$config['filter.published']')');
  63.                 }
  64.             }
  65.  
  66.             // Filter on the language
  67.             if (isset($config['filter.language']))
  68.             {
  69.                 if (is_string($config['filter.language']))
  70.                 {
  71.                     $query->where('a.language = ' $db->quote($config['filter.language']));
  72.                 }
  73.                 elseif (is_array($config['filter.language']))
  74.                 {
  75.                     foreach ($config['filter.language'as &$language)
  76.                     {
  77.                         $language $db->quote($language);
  78.                     }
  79.                     $query->where('a.language IN (' implode(','$config['filter.language']')');
  80.                 }
  81.             }
  82.  
  83.             $query->order('a.lft');
  84.  
  85.             $db->setQuery($query);
  86.             $items $db->loadObjectList();
  87.  
  88.             // Assemble the list options.
  89.             static::$items[$hasharray();
  90.  
  91.             foreach ($items as &$item)
  92.             {
  93.                 $repeat ($item->level >= 0$item->level 0;
  94.                 $item->title str_repeat('- '$repeat$item->title;
  95.                 static::$items[$hash][JHtml::_('select.option'$item->id$item->title);
  96.             }
  97.         }
  98.  
  99.         return static::$items[$hash];
  100.     }
  101.  
  102.     /**
  103.      * Returns an array of tags.
  104.      *
  105.      * @param   array  $config  An array of configuration options. By default, only published and unpublished tags are returned.
  106.      *
  107.      * @return  array  Tag data
  108.      *
  109.      * @since   3.1
  110.      */
  111.     public static function tags($config array('filter.published' => array(01)))
  112.     {
  113.         $hash md5(serialize($config));
  114.         $config = (array) $config;
  115.         $db JFactory::getDbo();
  116.         $query $db->getQuery(true)
  117.             ->select('a.id, a.title, a.level, a.parent_id')
  118.             ->from('#__tags AS a')
  119.             ->where('a.parent_id > 0');
  120.  
  121.         // Filter on the published state
  122.         if (isset($config['filter.published']))
  123.         {
  124.             if (is_numeric($config['filter.published']))
  125.             {
  126.                 $query->where('a.published = ' . (int) $config['filter.published']);
  127.             }
  128.             elseif (is_array($config['filter.published']))
  129.             {
  130.                 JArrayHelper::toInteger($config['filter.published']);
  131.                 $query->where('a.published IN (' implode(','$config['filter.published']')');
  132.             }
  133.         }
  134.  
  135.         $query->order('a.lft');
  136.  
  137.         $db->setQuery($query);
  138.         $items $db->loadObjectList();
  139.  
  140.         // Assemble the list options.
  141.         static::$items[$hasharray();
  142.  
  143.         foreach ($items as &$item)
  144.         {
  145.             $repeat ($item->level >= 0$item->level 0;
  146.             $item->title str_repeat('- '$repeat$item->title;
  147.             static::$items[$hash][JHtml::_('select.option'$item->id$item->title);
  148.         }
  149.         return static::$items[$hash];
  150.     }
  151.  
  152.     /**
  153.      * This is just a proxy for the formbehavior.ajaxchosen method
  154.      *
  155.      * @param   string   $selector     DOM id of the tag field
  156.      * @param   boolean  $allowCustom  Flag to allow custom values
  157.      *
  158.      * @return  void 
  159.      *
  160.      * @since   3.1
  161.      */
  162.     public static function ajaxfield($selector='#jform_tags'$allowCustom true)
  163.     {
  164.         // Tags field ajax
  165.         $chosenAjaxSettings new JRegistry(
  166.             array(
  167.                 'selector'    => $selector,
  168.                 'type'        => 'GET',
  169.                 'url'         => JUri::root('index.php?option=com_tags&task=tags.searchAjax',
  170.                 'dataType'    => 'json',
  171.                 'jsonTermKey' => 'like'
  172.             )
  173.         );
  174.         JHtml::_('formbehavior.ajaxchosen'$chosenAjaxSettings);
  175.  
  176.         // Allow custom values ?
  177.         if ($allowCustom)
  178.         {
  179.             JFactory::getDocument()->addScriptDeclaration("
  180.                 (function($){
  181.                     $(document).ready(function () {
  182.  
  183.                         var customTagPrefix = '#new#';
  184.  
  185.                         // Method to add tags pressing enter
  186.                         $('" $selector "_chzn input').keydown(function(event) {
  187.  
  188.                             // Tag is greater than 3 chars and enter pressed
  189.                             if (this.value.length >= 3 && (event.which === 13 || event.which === 188)) {
  190.  
  191.                                 // Search an highlighted result
  192.                                 var highlighted = $('" $selector "_chzn').find('li.active-result.highlighted').first();
  193.  
  194.                                 // Add the highlighted option
  195.                                 if (event.which === 13 && highlighted.text() !== '')
  196.                                 {
  197.                                     // Extra check. If we have added a custom tag with this text remove it
  198.                                     var customOptionValue = customTagPrefix + highlighted.text();
  199.                                     $('" $selector " option').filter(function () { return $(this).val() == customOptionValue; }).remove();
  200.  
  201.                                     // Select the highlighted result
  202.                                     var tagOption = $('" $selector " option').filter(function () { return $(this).html() == highlighted.text(); });
  203.                                     tagOption.attr('selected', 'selected');
  204.                                 }
  205.                                 // Add the custom tag option
  206.                                 else
  207.                                 {
  208.                                     var customTag = this.value;
  209.  
  210.                                     // Extra check. Search if the custom tag already exists (typed faster than AJAX ready)
  211.                                     var tagOption = $('" $selector " option').filter(function () { return $(this).html() == customTag; });
  212.                                     if (tagOption.text() !== '')
  213.                                     {
  214.                                         tagOption.attr('selected', 'selected');
  215.                                     }
  216.                                     else
  217.                                     {
  218.                                         var option = $('<option>');
  219.                                         option.text(this.value).val(customTagPrefix + this.value);
  220.                                         option.attr('selected','selected');
  221.  
  222.                                         // Append the option an repopulate the chosen field
  223.                                         $('" $selector "').append(option);
  224.                                     }
  225.                                 }
  226.  
  227.                                 this.value = '';
  228.                                 $('" $selector "').trigger('liszt:updated');
  229.                                 event.preventDefault();
  230.  
  231.                             }
  232.                         });
  233.                     });
  234.                 })(jQuery);
  235.                 "
  236.             );
  237.         }
  238.     }
  239. }

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