/libraries/cms/form/field/tag.php

https://bitbucket.org/pastor399/newcastleunifc · PHP · 246 lines · 127 code · 33 blank · 86 comment · 22 complexity · a275f3e0f3a522de396baf635d3c8987 MD5 · raw file

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