1 <?php
2 3 4 5 6 7 8
9
10 defined('JPATH_PLATFORM') or die;
11
12 jimport('joomla.filesystem.folder');
13
14 15 16 17 18 19
20 class JFormFieldComponentlayout extends JFormField
21 {
22 23 24 25 26 27
28 protected $type = 'ComponentLayout';
29
30 31 32 33 34 35 36
37 protected function getInput()
38 {
39
40 $clientId = $this->element['client_id'];
41
42 if (is_null($clientId) && $this->form instanceof JForm)
43 {
44 $clientId = $this->form->getValue('client_id');
45 }
46
47 $clientId = (int) $clientId;
48
49 $client = JApplicationHelper::getClientInfo($clientId);
50
51
52 $extension = (string) $this->element['extension'];
53
54 if (empty($extension) && ($this->form instanceof JForm))
55 {
56 $extension = $this->form->getValue('extension');
57 }
58
59 $extension = preg_replace('#\W#', '', $extension);
60
61 $template = (string) $this->element['template'];
62 $template = preg_replace('#\W#', '', $template);
63
64 $template_style_id = '';
65 if ($this->form instanceof JForm)
66 {
67 $template_style_id = $this->form->getValue('template_style_id');
68 $template_style_id = preg_replace('#\W#', '', $template_style_id);
69 }
70
71 $view = (string) $this->element['view'];
72 $view = preg_replace('#\W#', '', $view);
73
74
75 if ($extension && $view && $client)
76 {
77
78 $lang = JFactory::getLanguage();
79 $lang->load($extension . '.sys', JPATH_ADMINISTRATOR, null, false, true)
80 || $lang->load($extension . '.sys', JPATH_ADMINISTRATOR . '/components/' . $extension, null, false, true);
81
82
83 $db = JFactory::getDbo();
84 $query = $db->getQuery(true);
85
86
87 $query->select('e.element, e.name')
88 ->from('#__extensions as e')
89 ->where('e.client_id = ' . (int) $clientId)
90 ->where('e.type = ' . $db->quote('template'))
91 ->where('e.enabled = 1');
92
93 if ($template)
94 {
95 $query->where('e.element = ' . $db->quote($template));
96 }
97
98 if ($template_style_id)
99 {
100 $query->join('LEFT', '#__template_styles as s on s.template=e.element')
101 ->where('s.id=' . (int) $template_style_id);
102 }
103
104
105 $db->setQuery($query);
106 $templates = $db->loadObjectList('element');
107
108
109 $component_path = JPath::clean($client->path . '/components/' . $extension . '/views/' . $view . '/tmpl');
110
111
112 $component_layouts = array();
113
114
115 $groups = array();
116
117
118 if ($this->element['useglobal'] == 'true')
119 {
120 $groups[JText::_('JOPTION_FROM_STANDARD')]['items'][] = JHtml::_('select.option', '', JText::_('JGLOBAL_USE_GLOBAL'));
121 }
122
123
124 if (is_dir($component_path) && ($component_layouts = JFolder::files($component_path, '^[^_]*\.xml$', false, true)))
125 {
126
127 $groups['_'] = array();
128 $groups['_']['id'] = $this->id . '__';
129 $groups['_']['text'] = JText::sprintf('JOPTION_FROM_COMPONENT');
130 $groups['_']['items'] = array();
131
132 foreach ($component_layouts as $i => $file)
133 {
134
135 if (!$xml = simplexml_load_file($file))
136 {
137 unset($component_layouts[$i]);
138
139 continue;
140 }
141
142
143 if (!$menu = $xml->xpath('layout[1]'))
144 {
145 unset($component_layouts[$i]);
146
147 continue;
148 }
149
150 $menu = $menu[0];
151
152
153 $value = basename($file, '.xml');
154 $component_layouts[$i] = $value;
155 $text = isset($menu['option']) ? JText::_($menu['option']) : (isset($menu['title']) ? JText::_($menu['title']) : $value);
156 $groups['_']['items'][] = JHtml::_('select.option', '_:' . $value, $text);
157 }
158 }
159
160
161 if ($templates)
162 {
163 foreach ($templates as $template)
164 {
165
166 $lang->load('tpl_' . $template->element . '.sys', $client->path, null, false, true)
167 || $lang->load('tpl_' . $template->element . '.sys', $client->path . '/templates/' . $template->element, null, false, true);
168
169 $template_path = JPath::clean(
170 $client->path
171 . '/templates/'
172 . $template->element
173 . '/html/'
174 . $extension
175 . '/'
176 . $view
177 );
178
179
180 if (is_dir($template_path) && ($files = JFolder::files($template_path, '^[^_]*\.php$', false, true)))
181 {
182
183
184 $xml_files = JFolder::files($template_path, '^[^_]*\.xml$', false, true);
185
186 for ($j = 0, $count = count($xml_files); $j < $count; $j++)
187 {
188 $xml_files[$j] = basename($xml_files[$j], '.xml');
189 }
190
191 foreach ($files as $i => $file)
192 {
193
194 if (in_array(basename($file, '.php'), $component_layouts)
195 || in_array(basename($file, '.php'), $xml_files))
196 {
197 unset($files[$i]);
198 }
199 }
200
201 if (count($files))
202 {
203
204 $groups[$template->name] = array();
205 $groups[$template->name]['id'] = $this->id . '_' . $template->element;
206 $groups[$template->name]['text'] = JText::sprintf('JOPTION_FROM_TEMPLATE', $template->name);
207 $groups[$template->name]['items'] = array();
208
209 foreach ($files as $file)
210 {
211
212 $value = basename($file, '.php');
213 $text = $lang
214 ->hasKey(
215 $key = strtoupper(
216 'TPL_'
217 . $template->name
218 . '_'
219 . $extension
220 . '_'
221 . $view
222 . '_LAYOUT_'
223 . $value
224 )
225 )
226 ? JText::_($key) : $value;
227 $groups[$template->name]['items'][] = JHtml::_('select.option', $template->element . ':' . $value, $text);
228 }
229 }
230 }
231 }
232 }
233
234
235 $attr = $this->element['size'] ? ' size="' . (int) $this->element['size'] . '"' : '';
236 $attr .= $this->element['class'] ? ' class="' . (string) $this->element['class'] . '"' : '';
237
238
239 $html = array();
240
241
242 $selected = array($this->value);
243
244
245 $html[] = JHtml::_(
246 'select.groupedlist', $groups, $this->name,
247 array('id' => $this->id, 'group.id' => 'id', 'list.attr' => $attr, 'list.select' => $selected)
248 );
249
250 return implode($html);
251 }
252 else
253 {
254 return '';
255 }
256 }
257 }
258