1 <?php
2 3 4 5 6 7
8
9 defined('FOF_INCLUDED') or die;
10
11 JFormHelper::loadFieldClass('tag');
12
13 14 15 16 17 18 19
20 class FOFFormFieldTag extends JFormFieldTag implements FOFFormField
21 {
22 protected $static;
23
24 protected $repeatable;
25
26
27 public $item;
28
29
30 public $rowid;
31
32 33 34 35 36 37 38 39 40
41 public function __get($name)
42 {
43 switch ($name)
44 {
45 case 'static':
46 if (empty($this->static))
47 {
48 $this->static = $this->getStatic();
49 }
50
51 return $this->static;
52 break;
53
54 case 'repeatable':
55 if (empty($this->repeatable))
56 {
57 $this->repeatable = $this->getRepeatable();
58 }
59
60 return $this->repeatable;
61 break;
62
63 default:
64 return parent::__get($name);
65 }
66 }
67
68 69 70 71 72 73 74
75 protected function getOptions()
76 {
77 $options = array();
78
79 $published = $this->element['published']? $this->element['published'] : array(0,1);
80
81 $db = FOFPlatform::getInstance()->getDbo();
82 $query = $db->getQuery(true)
83 ->select('DISTINCT a.id AS value, a.path, a.title AS text, a.level, a.published, a.lft')
84 ->from('#__tags AS a')
85 ->join('LEFT', $db->quoteName('#__tags') . ' AS b ON a.lft > b.lft AND a.rgt < b.rgt');
86
87 if ($this->item instanceof FOFTable)
88 {
89 $item = $this->item;
90 }
91 else
92 {
93 $item = $this->form->getModel()->getItem();
94 }
95
96 if ($item instanceof FOFTable)
97 {
98
99 $keyfield = $item->getKeyName();
100 $content_id = $item->$keyfield;
101 $type = $item->getContentType();
102
103 $selected_query = $db->getQuery(true);
104 $selected_query
105 ->select('tag_id')
106 ->from('#__contentitem_tag_map')
107 ->where('content_item_id = ' . (int) $content_id)
108 ->where('type_alias = ' . $db->quote($type));
109
110 $db->setQuery($selected_query);
111
112 $this->value = $db->loadColumn();
113 }
114
115
116 if (!empty($this->element['language']))
117 {
118 $query->where('a.language = ' . $db->quote($this->element['language']));
119 }
120
121 $query->where($db->qn('a.lft') . ' > 0');
122
123
124
125
126 if (is_numeric($published))
127 {
128 $query->where('a.published = ' . (int) $published);
129 }
130 elseif (is_array($published))
131 {
132 FOFUtilsArray::toInteger($published);
133 $query->where('a.published IN (' . implode(',', $published) . ')');
134 }
135
136 $query->order('a.lft ASC');
137
138
139 $db->setQuery($query);
140
141 try
142 {
143 $options = $db->loadObjectList();
144 }
145 catch (RuntimeException $e)
146 {
147 return false;
148 }
149
150
151 if ($this->isNested())
152 {
153 $this->prepareOptionsNested($options);
154 }
155 else
156 {
157 $options = JHelperTags::convertPathsToNames($options);
158 }
159
160 return $options;
161 }
162
163 164 165 166 167 168 169 170
171 public function getStatic()
172 {
173 $class = $this->element['class'] ? (string) $this->element['class'] : '';
174 $translate = $this->element['translate'] ? (string) $this->element['translate'] : false;
175
176 $options = $this->getOptions();
177
178 $html = '';
179
180 foreach ($options as $option) {
181
182 $html .= '<span>';
183
184 if ($translate == true)
185 {
186 $html .= JText::_($option->text);
187 }
188 else
189 {
190 $html .= $option->text;
191 }
192
193 $html .= '</span>';
194 }
195
196 return '<span id="' . $this->id . '" class="' . $class . '">' .
197 $html .
198 '</span>';
199 }
200
201 202 203 204 205 206 207 208
209 public function getRepeatable()
210 {
211 $class = $this->element['class'] ? (string) $this->element['class'] : '';
212 $translate = $this->element['translate'] ? (string) $this->element['translate'] : false;
213
214 $options = $this->getOptions();
215
216 $html = '';
217
218 foreach ($options as $option) {
219
220 $html .= '<span>';
221
222 if ($translate == true)
223 {
224 $html .= JText::_($option->text);
225 }
226 else
227 {
228 $html .= $option->text;
229 }
230
231 $html .= '</span>';
232 }
233
234 return '<span class="' . $this->id . ' ' . $class . '">' .
235 $html .
236 '</span>';
237 }
238 }
239