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 class JFormFieldModulelayout extends JFormField
20 {
21 22 23 24 25 26
27 protected $type = 'ModuleLayout';
28
29 30 31 32 33 34 35
36 protected function getInput()
37 {
38
39 $clientId = $this->element['client_id'];
40
41 if (is_null($clientId) && $this->form instanceof JForm)
42 {
43 $clientId = $this->form->getValue('client_id');
44 }
45
46 $clientId = (int) $clientId;
47
48 $client = JApplicationHelper::getClientInfo($clientId);
49
50
51 $module = (string) $this->element['module'];
52
53 if (empty($module) && ($this->form instanceof JForm))
54 {
55 $module = $this->form->getValue('module');
56 }
57
58 $module = preg_replace('#\W#', '', $module);
59
60
61 $template = (string) $this->element['template'];
62 $template = preg_replace('#\W#', '', $template);
63
64
65 $template_style_id = '';
66 if ($this->form instanceof JForm)
67 {
68 $template_style_id = $this->form->getValue('template_style_id');
69 $template_style_id = preg_replace('#\W#', '', $template_style_id);
70 }
71
72
73 if ($module && $client)
74 {
75
76 $lang = JFactory::getLanguage();
77 $lang->load($module . '.sys', $client->path, null, false, true)
78 || $lang->load($module . '.sys', $client->path . '/modules/' . $module, null, false, true);
79
80
81 $db = JFactory::getDbo();
82 $query = $db->getQuery(true);
83
84
85 $query->select('element, name')
86 ->from('#__extensions as e')
87 ->where('e.client_id = ' . (int) $clientId)
88 ->where('e.type = ' . $db->quote('template'))
89 ->where('e.enabled = 1');
90
91 if ($template)
92 {
93 $query->where('e.element = ' . $db->quote($template));
94 }
95
96 if ($template_style_id)
97 {
98 $query->join('LEFT', '#__template_styles as s on s.template=e.element')
99 ->where('s.id=' . (int) $template_style_id);
100 }
101
102
103 $db->setQuery($query);
104 $templates = $db->loadObjectList('element');
105
106
107 $module_path = JPath::clean($client->path . '/modules/' . $module . '/tmpl');
108
109
110 $module_layouts = array();
111
112
113 $groups = array();
114
115
116 if (is_dir($module_path) && ($module_layouts = JFolder::files($module_path, '^[^_]*\.php$')))
117 {
118
119 $groups['_'] = array();
120 $groups['_']['id'] = $this->id . '__';
121 $groups['_']['text'] = JText::sprintf('JOPTION_FROM_MODULE');
122 $groups['_']['items'] = array();
123
124 foreach ($module_layouts as $file)
125 {
126
127 $value = basename($file, '.php');
128 $text = $lang->hasKey($key = strtoupper($module . '_LAYOUT_' . $value)) ? JText::_($key) : $value;
129 $groups['_']['items'][] = JHtml::_('select.option', '_:' . $value, $text);
130 }
131 }
132
133
134 if ($templates)
135 {
136 foreach ($templates as $template)
137 {
138
139 $lang->load('tpl_' . $template->element . '.sys', $client->path, null, false, true)
140 || $lang->load('tpl_' . $template->element . '.sys', $client->path . '/templates/' . $template->element, null, false, true);
141
142 $template_path = JPath::clean($client->path . '/templates/' . $template->element . '/html/' . $module);
143
144
145 if (is_dir($template_path) && ($files = JFolder::files($template_path, '^[^_]*\.php$')))
146 {
147 foreach ($files as $i => $file)
148 {
149
150 if (in_array($file, $module_layouts))
151 {
152 unset($files[$i]);
153 }
154 }
155
156 if (count($files))
157 {
158
159 $groups[$template->element] = array();
160 $groups[$template->element]['id'] = $this->id . '_' . $template->element;
161 $groups[$template->element]['text'] = JText::sprintf('JOPTION_FROM_TEMPLATE', $template->name);
162 $groups[$template->element]['items'] = array();
163
164 foreach ($files as $file)
165 {
166
167 $value = basename($file, '.php');
168 $text = $lang->hasKey($key = strtoupper('TPL_' . $template->element . '_' . $module . '_LAYOUT_' . $value))
169 ? JText::_($key) : $value;
170 $groups[$template->element]['items'][] = JHtml::_('select.option', $template->element . ':' . $value, $text);
171 }
172 }
173 }
174 }
175 }
176
177 $attr = $this->element['size'] ? ' size="' . (int) $this->element['size'] . '"' : '';
178 $attr .= $this->element['class'] ? ' class="' . (string) $this->element['class'] . '"' : '';
179
180
181 $html = array();
182
183
184 $selected = array($this->value);
185
186
187 $html[] = JHtml::_(
188 'select.groupedlist', $groups, $this->name,
189 array('id' => $this->id, 'group.id' => 'id', 'list.attr' => $attr, 'list.select' => $selected)
190 );
191
192 return implode($html);
193 }
194 else
195 {
196 return '';
197 }
198 }
199 }
200