1 <?php
  2   3   4   5   6   7   8 
  9 
 10 defined('JPATH_PLATFORM') or die;
 11 
 12 use Joomla\Utilities\ArrayHelper;
 13 
 14 JFormHelper::loadFieldClass('list');
 15 
 16  17  18  19  20 
 21 class JFormFieldTag extends JFormFieldList
 22 {
 23      24  25  26  27  28 
 29     public $type = 'Tag';
 30 
 31      32  33  34  35  36 
 37     public $isNested = null;
 38 
 39      40  41  42  43  44 
 45     protected $comParams = null;
 46 
 47      48  49  50  51 
 52     public function __construct()
 53     {
 54         parent::__construct();
 55 
 56         
 57         $this->comParams = JComponentHelper::getParams('com_tags');
 58     }
 59 
 60      61  62  63  64  65  66 
 67     protected function getInput()
 68     {
 69         
 70         if (!$this->isNested())
 71         {
 72             
 73             $id    = isset($this->element['id']) ? $this->element['id'] : null;
 74             $cssId = '#' . $this->getId($id, $this->element['name']);
 75 
 76             
 77             JHtml::_('tag.ajaxfield', $cssId, $this->allowCustom());
 78         }
 79 
 80         if (!is_array($this->value) && !empty($this->value))
 81         {
 82             if ($this->value instanceof JHelperTags)
 83             {
 84                 if (empty($this->value->tags))
 85                 {
 86                     $this->value = array();
 87                 }
 88                 else
 89                 {
 90                     $this->value = $this->value->tags;
 91                 }
 92             }
 93 
 94             
 95             if (is_string($this->value))
 96             {
 97                 $this->value = explode(',', $this->value);
 98             }
 99         }
100 
101         return parent::getInput();
102     }
103 
104     105 106 107 108 109 110 
111     protected function getOptions()
112     {
113         $published = $this->element['published']?: array(0, 1);
114         $app       = JFactory::getApplication();
115         $tag       = $app->getLanguage()->getTag();
116 
117         $db    = JFactory::getDbo();
118         $query = $db->getQuery(true)
119             ->select('DISTINCT a.id AS value, a.path, a.title AS text, a.level, a.published, a.lft')
120             ->from('#__tags AS a')
121             ->join('LEFT', $db->qn('#__tags') . ' AS b ON a.lft > b.lft AND a.rgt < b.rgt');
122 
123         
124         if ($app->isClient('site') && JLanguageMultilang::isEnabled())
125         {
126             $lang = JComponentHelper::getParams('com_tags')->get('tag_list_language_filter');
127 
128             if ($lang == 'current_language')
129             {
130                 $query->where('a.language in (' . $db->quote($tag) . ',' . $db->quote('*') . ')');
131             }
132         }
133         
134         elseif (!empty($this->element['language']))
135         {
136             if (strpos($this->element['language'], ',') !== false)
137             {
138                 $language = implode(',', $db->quote(explode(',', $this->element['language'])));
139             }
140             else
141             {
142                 $language = $db->quote($this->element['language']);
143             }
144 
145             $query->where($db->quoteName('a.language') . ' IN (' . $language . ')');
146         }
147 
148         $query->where($db->qn('a.lft') . ' > 0');
149 
150         
151         if (is_numeric($published))
152         {
153             $query->where('a.published = ' . (int) $published);
154         }
155         elseif (is_array($published))
156         {
157             $published = ArrayHelper::toInteger($published);
158             $query->where('a.published IN (' . implode(',', $published) . ')');
159         }
160 
161         $query->order('a.lft ASC');
162 
163         
164         $db->setQuery($query);
165 
166         try
167         {
168             $options = $db->loadObjectList();
169         }
170         catch (RuntimeException $e)
171         {
172             return array();
173         }
174 
175         
176         if ($this->form->getName() === 'com_tags.tag')
177         {
178             $id   = (int) $this->form->getValue('id', 0);
179 
180             foreach ($options as $option)
181             {
182                 if ($option->value == $id)
183                 {
184                     $option->disable = true;
185                 }
186             }
187         }
188 
189         
190         $options = array_merge(parent::getOptions(), $options);
191 
192         
193         if ($this->isNested())
194         {
195             $this->prepareOptionsNested($options);
196         }
197         else
198         {
199             $options = JHelperTags::convertPathsToNames($options);
200         }
201 
202         return $options;
203     }
204 
205     206 207 208 209 210 211 212 213 
214     protected function prepareOptionsNested(&$options)
215     {
216         if ($options)
217         {
218             foreach ($options as &$option)
219             {
220                 $repeat = (isset($option->level) && $option->level - 1 >= 0) ? $option->level - 1 : 0;
221                 $option->text = str_repeat('- ', $repeat) . $option->text;
222             }
223         }
224 
225         return $options;
226     }
227 
228     229 230 231 232 233 234 
235     public function isNested()
236     {
237         if ($this->isNested === null)
238         {
239             
240             if (isset($this->element['mode']) && $this->element['mode'] === 'nested'
241                 || !isset($this->element['mode']) && $this->comParams->get('tag_field_ajax_mode', 1) == 0)
242             {
243                 $this->isNested = true;
244             }
245         }
246 
247         return $this->isNested;
248     }
249 
250     251 252 253 254 
255     public function allowCustom()
256     {
257         if (isset($this->element['custom']) && $this->element['custom'] === 'deny')
258         {
259             return false;
260         }
261 
262         return true;
263     }
264 }
265