Source for file tag.php

Documentation is available at tag.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Libraries
  4.  * @subpackage  Form
  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.txt
  8.  */
  9.  
  10. defined('JPATH_BASE'or die;
  11.  
  12.  
  13. /**
  14.  * Form Field class for the Joomla Framework.
  15.  *
  16.  * @package     Joomla.Libraries
  17.  * @subpackage  Form
  18.  * @since       3.1
  19.  */
  20. class JFormFieldTag extends JFormFieldList
  21. {
  22.     /**
  23.      * A flexible tag list that respects access controls
  24.      *
  25.      * @var    string 
  26.      * @since  3.1
  27.      */
  28.     public $type = 'Tag';
  29.  
  30.     /**
  31.      * Flag to work with nested tag field
  32.      *
  33.      * @var    boolean 
  34.      * @since  3.1
  35.      */
  36.     public $isNested = null;
  37.  
  38.     /**
  39.      * com_tags parameters
  40.      *
  41.      * @var    JRegistry 
  42.      * @since  3.1
  43.      */
  44.     protected $comParams = null;
  45.  
  46.     /**
  47.      * Constructor
  48.      *
  49.      * @since  3.1
  50.      */
  51.     public function __construct()
  52.     {
  53.         parent::__construct();
  54.  
  55.         // Load com_tags config
  56.         $this->comParams = JComponentHelper::getParams('com_tags');
  57.     }
  58.  
  59.     /**
  60.      * Method to get the field input for a tag field.
  61.      *
  62.      * @return  string  The field input.
  63.      *
  64.      * @since   3.1
  65.      */
  66.     protected function getInput()
  67.     {
  68.         // AJAX mode requires ajax-chosen
  69.         if (!$this->isNested())
  70.         {
  71.             // Get the field id
  72.             $id    = isset($this->element['id']$this->element['id'null;
  73.             $cssId '#' $this->getId($id$this->element['name']);
  74.  
  75.             // Load the ajax-chosen customised field
  76.             JHtml::_('tag.ajaxfield'$cssId$this->allowCustom());
  77.         }
  78.  
  79.         if (!is_array($this->value&& !empty($this->value))
  80.         {
  81.             if ($this->value instanceof JHelperTags)
  82.             {
  83.                 if (empty($this->value->tags))
  84.                 {
  85.                     $this->value = array();
  86.                 }
  87.                 else
  88.                 {
  89.                     $this->value = $this->value->tags;
  90.                 }
  91.             }
  92.  
  93.             // String in format 2,5,4
  94.             if (is_string($this->value))
  95.             {
  96.                 $this->value = explode(','$this->value);
  97.             }
  98.         }
  99.  
  100.         $input parent::getInput();
  101.  
  102.         return $input;
  103.     }
  104.  
  105.     /**
  106.      * Method to get a list of tags
  107.      *
  108.      * @return  array  The field option objects.
  109.      *
  110.      * @since   3.1
  111.      */
  112.     protected function getOptions()
  113.     {
  114.         $published $this->element['published']$this->element['published'array(0,1);
  115.  
  116.         $db        JFactory::getDbo();
  117.         $query    $db->getQuery(true)
  118.             ->select('a.id AS value, a.path, a.title AS text, a.level, a.published')
  119.             ->from('#__tags AS a')
  120.             ->join('LEFT'$db->quoteName('#__tags'' AS b ON a.lft > b.lft AND a.rgt < b.rgt');
  121.  
  122.         // Ajax tag only loads assigned values
  123.         if (!$this->isNested(&& !empty($this->value))
  124.         {
  125.             // Only item assigned values
  126.             $values = (array) $this->value;
  127.             JArrayHelper::toInteger($values);
  128.             $query->where('a.id IN (' implode(','$values')');
  129.         }
  130.  
  131.         // Block the possibility to set a tag as it own parent
  132.         $id   = (int) $this->form->getValue('id'0);
  133.         $name = (int) $this->form->getValue('name''');
  134.  
  135.         if ($name == 'com_tags.tag')
  136.         {
  137.             $query->where('a.id != ' $db->quote($id));
  138.         }
  139.  
  140.         // Filter language
  141.         if (!empty($this->element['language']))
  142.         {
  143.             $query->where('a.language = ' $db->quote($this->element['language']));
  144.         }
  145.  
  146.         $query->where($db->quoteName('a.alias'' <> ' $db->quote('root'));
  147.  
  148.         // Filter on the published state
  149.         if (is_numeric($published))
  150.         {
  151.             $query->where('a.published = ' . (int) $published);
  152.         }
  153.         elseif (is_array($published))
  154.         {
  155.             JArrayHelper::toInteger($published);
  156.             $query->where('a.published IN (' implode(','$published')');
  157.         }
  158.  
  159.         $query->group('a.id, a.title, a.level, a.lft, a.rgt, a.parent_id, a.published, a.path')
  160.             ->order('a.lft ASC');
  161.  
  162.         // Get the options.
  163.         $db->setQuery($query);
  164.  
  165.         try
  166.         {
  167.             $options $db->loadObjectList();
  168.         }
  169.         catch (RuntimeException $e)
  170.         {
  171.             return false;
  172.         }
  173.  
  174.         // Merge any additional options in the XML definition.
  175.         $options array_merge(parent::getOptions()$options);
  176.  
  177.         // Prepare nested data
  178.         if ($this->isNested())
  179.         {
  180.             $this->prepareOptionsNested($options);
  181.         }
  182.         else
  183.         {
  184.             $options JHelperTags::convertPathsToNames($options);
  185.         }
  186.  
  187.         return $options;
  188.     }
  189.  
  190.     /**
  191.      * Add "-" before nested tags, depending on level
  192.      *
  193.      * @param   array  &$options  Array of tags
  194.      *
  195.      * @return  array  The field option objects.
  196.      *
  197.      * @since   3.1
  198.      */
  199.     protected function prepareOptionsNested(&$options)
  200.     {
  201.         if ($options)
  202.         {
  203.             foreach ($options as &$option)
  204.             {
  205.                 $repeat (isset($option->level&& $option->level >= 0$option->level 0;
  206.                 $option->text str_repeat('- '$repeat$option->text;
  207.             }
  208.         }
  209.  
  210.         return $options;
  211.     }
  212.  
  213.     /**
  214.      * Determine if the field has to be tagnested
  215.      *
  216.      * @return  boolean 
  217.      *
  218.      * @since   3.1
  219.      */
  220.     public function isNested()
  221.     {
  222.         if (is_null($this->isNested))
  223.         {
  224.             // If mode="nested" || ( mode not set & config = nested )
  225.             if ((isset($this->element['mode']&& $this->element['mode'== 'nested')
  226.                 || (!isset($this->element['mode']&& $this->comParams->get('tag_field_ajax_mode'1== 0))
  227.             {
  228.                 $this->isNested = true;
  229.             }
  230.         }
  231.  
  232.         return $this->isNested;
  233.     }
  234.  
  235.     /**
  236.      * Determines if the field allows or denies custom values
  237.      *
  238.      * @return  boolean 
  239.      */
  240.     public function allowCustom()
  241.     {
  242.         if (isset($this->element['custom']&& $this->element['custom'== 'deny')
  243.         {
  244.             return false;
  245.         }
  246.  
  247.         return true;
  248.     }
  249. }

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