forked from joomla/joomla-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
componentlayout.php
239 lines (195 loc) · 7.57 KB
/
componentlayout.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
/**
* @version $Id$
* @package Joomla.Framework
* @subpackage Form
* @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('JPATH_BASE') or die;
jimport('joomla.html.html');
jimport('joomla.filesystem.file');
jimport('joomla.filesystem.folder');
jimport('joomla.form.formfield');
jimport('joomla.form.helper');
/**
* Form Field to display a list of the layouts for a component view from the extension or template overrides.
*
* @package Joomla.Framework
* @subpackage Form
* @since 1.6
*/
class JFormFieldComponentLayout extends JFormField
{
/**
* The form field type.
*
* @var string
* @since 1.6
*/
protected $type = 'ComponentLayout';
/**
* Method to get the field input.
*
* @return string The field input.
* @since 1.6
*/
protected function getInput()
{
// Initialize variables.
// Get the client id.
$clientId = $this->element['client_id'];
if (is_null($clientId) && $this->form instanceof JForm) {
$clientId = $this->form->getValue('client_id');
}
$clientId = (int) $clientId;
$client = JApplicationHelper::getClientInfo($clientId);
// Get the extension.
$extn = (string) $this->element['extension'];
if (empty($extn) && ($this->form instanceof JForm)) {
$extn = $this->form->getValue('extension');
}
$extn = preg_replace('#\W#', '', $extn);
// Get the template.
$template = (string) $this->element['template'];
$template = preg_replace('#\W#', '', $template);
// Get the style.
if ($this->form instanceof JForm) {
$template_style_id = $this->form->getValue('template_style_id');
}
$template_style_id = preg_replace('#\W#', '', $template_style_id);
// Get the view.
$view = (string) $this->element['view'];
$view = preg_replace('#\W#', '', $view);
// If a template, extension and view are present build the options.
if ($extn && $view && $client) {
// Load language file
$lang = JFactory::getLanguage();
$lang->load($extn.'.sys', JPATH_ADMINISTRATOR, null, false, false)
|| $lang->load($extn.'.sys', JPATH_ADMINISTRATOR.'/components/'.$extn, null, false, false)
|| $lang->load($extn.'.sys', JPATH_ADMINISTRATOR, $lang->getDefault(), false, false)
|| $lang->load($extn.'.sys', JPATH_ADMINISTRATOR.'/components/'.$extn, $lang->getDefault(), false, false);
// Get the database object and a new query object.
$db = JFactory::getDBO();
$query = $db->getQuery(true);
// Build the query.
$query->select('e.element, e.name');
$query->from('#__extensions as e');
$query->where('e.client_id = '.(int) $clientId);
$query->where('e.type = '.$db->quote('template'));
$query->where('e.enabled = 1');
if ($template) {
$query->where('e.element = '.$db->quote($template));
}
if ($template_style_id) {
$query->join('LEFT', '#__template_styles as s on s.template=e.element');
$query->where('s.id='.(int)$template_style_id);
}
// Set the query and load the templates.
$db->setQuery($query);
$templates = $db->loadObjectList('element');
// Check for a database error.
if ($db->getErrorNum()) {
JError::raiseWarning(500, $db->getErrorMsg());
}
// Build the search paths for component layouts.
$component_path = JPath::clean($client->path.'/components/'.$extn.'/views/'.$view.'/tmpl');
// Prepare array of component layouts
$component_layouts = array();
// Prepare the grouped list
$groups=array();
// Add a Use Global option if useglobal="true" in XML file
if ($this->element['useglobal'] == 'true') {
$groups[JText::_('JOPTION_FROM_STANDARD')]['items'][] = JHTML::_('select.option', '', JText::_('JGLOBAL_USE_GLOBAL'));
}
// Add the layout options from the component path.
if (is_dir($component_path) && ($component_layouts = JFolder::files($component_path, '^[^_]*\.xml$', false, true))) {
// Create the group for the component
$groups['_'] = array();
$groups['_']['id'] = $this->id.'__';
$groups['_']['text'] = JText::sprintf('JOPTION_FROM_COMPONENT');
$groups['_']['items'] = array();
foreach ($component_layouts as $i=>$file)
{
// Attempt to load the xml file.
if (!$xml = simplexml_load_file($file)) {
unset($component_layouts[$i]);
continue;
}
// Get the help data from the XML file if present.
if (!$menu = $xml->xpath('layout[1]')) {
unset($component_layouts[$i]);
continue;
}
$menu = $menu[0];
// Add an option to the component group
$value = JFile::stripext(JFile::getName($file));
$component_layouts[$i] = $value;
$text = isset($menu['option']) ? JText::_($menu['option']) : (isset($menu['title']) ? JText::_($menu['title']) : $value);
$groups['_']['items'][] = JHTML::_('select.option', '_:'.$value, $text);
}
}
// Loop on all templates
if ($templates)
{
foreach ($templates as $template)
{
// Load language file
$lang->load('tpl_'.$template->element.'.sys', $client->path, null, false, false)
|| $lang->load('tpl_'.$template->element.'.sys', $client->path.'/templates/'.$template->element, null, false, false)
|| $lang->load('tpl_'.$template->element.'.sys', $client->path, $lang->getDefault(), false, false)
|| $lang->load('tpl_'.$template->element.'.sys', $client->path.'/templates/'.$template->element, $lang->getDefault(), false, false);
$template_path = JPath::clean($client->path.'/templates/'.$template->element.'/html/'.$extn.'/'.$view);
// Add the layout options from the template path.
if (is_dir($template_path) && ($files = JFolder::files($template_path, '^[^_]*\.php$', false, true)))
{
// Files with corresponding xml files are alternate menu items, not alternate layout files
// So we need to exclude these files
$xml_files = JFolder::files($template_path, '^[^_]*\.xml$', false, true);
for ($j = 0; $j < count($xml_files); $j++)
{
$xml_files[$j] = JFile::stripext(JFile::getName($xml_files[$j]));
}
foreach ($files as $i => $file)
{
// Remove layout files that exist in the component folder or that have xml files
if ((in_array(JFile::stripext(JFile::getName($file)), $component_layouts))
|| (in_array(JFile::stripext(JFile::getName($file)), $xml_files)))
{
unset($files[$i]);
}
}
if (count($files))
{
// Create the group for the template
$groups[$template->name]=array();
$groups[$template->name]['id']=$this->id.'_'.$template->element;
$groups[$template->name]['text']=JText::sprintf('JOPTION_FROM_TEMPLATE', $template->name);
$groups[$template->name]['items']=array();
foreach ($files as $file)
{
// Add an option to the template group
$value = JFile::stripext(JFile::getName($file));
$text = $lang->hasKey($key = strtoupper('TPL_'.$template->name.'_'.$extn.'_'.$view.'_LAYOUT_'.$value)) ? JText::_($key) : $value;
$groups[$template->name]['items'][] = JHTML::_('select.option', $template->element.':'.$value, $text);
}
}
}
}
}
// Compute attributes for the grouped list
$attr = $this->element['size'] ? ' size="'.(int) $this->element['size'].'"' : '';
// Prepare HTML code
$html = array();
// Compute the current selected values
$selected = array($this->value);
// Add a grouped list
$html[] = JHtml::_('select.groupedlist', $groups, $this->name, array('id'=>$this->id, 'group.id'=>'id', 'list.attr'=>$attr, 'list.select'=>$selected));
return implode($html);
}
else
{
return '';
}
}
}