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