Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
modulelayout.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 module display from the module or template overrides.
16  *
17  * @package Joomla.Legacy
18  * @subpackage Form
19  * @since 11.1
20  */
22 {
23  /**
24  * The form field type.
25  *
26  * @var string
27  * @since 11.1
28  */
29  protected $type = 'ModuleLayout';
30 
31  /**
32  * Method to get the field input for module layouts.
33  *
34  * @return string The field input.
35  *
36  * @since 11.1
37  */
38  protected function getInput()
39  {
40  // Get the client id.
41  $clientId = $this->element['client_id'];
42 
43  if (is_null($clientId) && $this->form instanceof JForm)
44  {
45  $clientId = $this->form->getValue('client_id');
46  }
47  $clientId = (int) $clientId;
48 
49  $client = JApplicationHelper::getClientInfo($clientId);
50 
51  // Get the module.
52  $module = (string) $this->element['module'];
53 
54  if (empty($module) && ($this->form instanceof JForm))
55  {
56  $module = $this->form->getValue('module');
57  }
58 
59  $module = preg_replace('#\W#', '', $module);
60 
61  // Get the template.
62  $template = (string) $this->element['template'];
63  $template = preg_replace('#\W#', '', $template);
64 
65  // Get the style.
66  if ($this->form instanceof JForm)
67  {
68  $template_style_id = $this->form->getValue('template_style_id');
69  }
70 
71  $template_style_id = preg_replace('#\W#', '', $template_style_id);
72 
73  // If an extension and view are present build the options.
74  if ($module && $client)
75  {
76 
77  // Load language file
78  $lang = JFactory::getLanguage();
79  $lang->load($module . '.sys', $client->path, null, false, false)
80  || $lang->load($module . '.sys', $client->path . '/modules/' . $module, null, false, false)
81  || $lang->load($module . '.sys', $client->path, $lang->getDefault(), false, false)
82  || $lang->load($module . '.sys', $client->path . '/modules/' . $module, $lang->getDefault(), false, false);
83 
84  // Get the database object and a new query object.
85  $db = JFactory::getDbo();
86  $query = $db->getQuery(true);
87 
88  // Build the query.
89  $query->select('element, name')
90  ->from('#__extensions as e')
91  ->where('e.client_id = ' . (int) $clientId)
92  ->where('e.type = ' . $db->quote('template'))
93  ->where('e.enabled = 1');
94 
95  if ($template)
96  {
97  $query->where('e.element = ' . $db->quote($template));
98  }
99 
100  if ($template_style_id)
101  {
102  $query->join('LEFT', '#__template_styles as s on s.template=e.element')
103  ->where('s.id=' . (int) $template_style_id);
104  }
105 
106  // Set the query and load the templates.
107  $db->setQuery($query);
108  $templates = $db->loadObjectList('element');
109 
110  // Build the search paths for module layouts.
111  $module_path = JPath::clean($client->path . '/modules/' . $module . '/tmpl');
112 
113  // Prepare array of component layouts
114  $module_layouts = array();
115 
116  // Prepare the grouped list
117  $groups = array();
118 
119  // Add the layout options from the module path.
120  if (is_dir($module_path) && ($module_layouts = JFolder::files($module_path, '^[^_]*\.php$')))
121  {
122  // Create the group for the module
123  $groups['_'] = array();
124  $groups['_']['id'] = $this->id . '__';
125  $groups['_']['text'] = JText::sprintf('JOPTION_FROM_MODULE');
126  $groups['_']['items'] = array();
127 
128  foreach ($module_layouts as $file)
129  {
130  // Add an option to the module group
131  $value = basename($file, '.php');
132  $text = $lang->hasKey($key = strtoupper($module . '_LAYOUT_' . $value)) ? JText::_($key) : $value;
133  $groups['_']['items'][] = JHtml::_('select.option', '_:' . $value, $text);
134  }
135  }
136 
137  // Loop on all templates
138  if ($templates)
139  {
140  foreach ($templates as $template)
141  {
142  // Load language file
143  $lang->load('tpl_' . $template->element . '.sys', $client->path, null, false, false)
144  || $lang->load('tpl_' . $template->element . '.sys', $client->path . '/templates/' . $template->element, null, false, false)
145  || $lang->load('tpl_' . $template->element . '.sys', $client->path, $lang->getDefault(), false, false)
146  || $lang->load(
147  'tpl_' . $template->element . '.sys', $client->path . '/templates/' . $template->element, $lang->getDefault(),
148  false, false
149  );
150 
151  $template_path = JPath::clean($client->path . '/templates/' . $template->element . '/html/' . $module);
152 
153  // Add the layout options from the template path.
154  if (is_dir($template_path) && ($files = JFolder::files($template_path, '^[^_]*\.php$')))
155  {
156  foreach ($files as $i => $file)
157  {
158  // Remove layout that already exist in component ones
159  if (in_array($file, $module_layouts))
160  {
161  unset($files[$i]);
162  }
163  }
164 
165  if (count($files))
166  {
167  // Create the group for the template
168  $groups[$template->element] = array();
169  $groups[$template->element]['id'] = $this->id . '_' . $template->element;
170  $groups[$template->element]['text'] = JText::sprintf('JOPTION_FROM_TEMPLATE', $template->name);
171  $groups[$template->element]['items'] = array();
172 
173  foreach ($files as $file)
174  {
175  // Add an option to the template group
176  $value = basename($file, '.php');
177  $text = $lang->hasKey($key = strtoupper('TPL_' . $template->element . '_' . $module . '_LAYOUT_' . $value))
178  ? JText::_($key) : $value;
179  $groups[$template->element]['items'][] = JHtml::_('select.option', $template->element . ':' . $value, $text);
180  }
181  }
182  }
183  }
184  }
185  // Compute attributes for the grouped list
186  $attr = $this->element['size'] ? ' size="' . (int) $this->element['size'] . '"' : '';
187 
188  // Prepare HTML code
189  $html = array();
190 
191  // Compute the current selected values
192  $selected = array($this->value);
193 
194  // Add a grouped list
195  $html[] = JHtml::_(
196  'select.groupedlist', $groups, $this->name,
197  array('id' => $this->id, 'group.id' => 'id', 'list.attr' => $attr, 'list.select' => $selected)
198  );
199 
200  return implode($html);
201  }
202  else
203  {
204 
205  return '';
206  }
207  }
208 }