/libraries/legacy/form/field/modulelayout.php

https://github.com/Hackwar/joomla-platform · PHP · 209 lines · 122 code · 34 blank · 53 comment · 22 complexity · 825640e9551a98fe07bbbf3b65c72249 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Legacy
  4. * @subpackage Form
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. jimport('joomla.filesystem.folder');
  11. /**
  12. * Form Field to display a list of the layouts for module display from the module or template overrides.
  13. *
  14. * @package Joomla.Legacy
  15. * @subpackage Form
  16. * @since 11.1
  17. * @deprecated 13.3
  18. */
  19. class JFormFieldModulelayout extends JFormField
  20. {
  21. /**
  22. * The form field type.
  23. *
  24. * @var string
  25. * @since 11.1
  26. */
  27. protected $type = 'ModuleLayout';
  28. /**
  29. * Method to get the field input for module layouts.
  30. *
  31. * @return string The field input.
  32. *
  33. * @since 11.1
  34. */
  35. protected function getInput()
  36. {
  37. // Get the client id.
  38. $clientId = $this->element['client_id'];
  39. if (is_null($clientId) && $this->form instanceof JForm)
  40. {
  41. $clientId = $this->form->getValue('client_id');
  42. }
  43. $clientId = (int) $clientId;
  44. $client = JApplicationHelper::getClientInfo($clientId);
  45. // Get the module.
  46. $module = (string) $this->element['module'];
  47. if (empty($module) && ($this->form instanceof JForm))
  48. {
  49. $module = $this->form->getValue('module');
  50. }
  51. $module = preg_replace('#\W#', '', $module);
  52. // Get the template.
  53. $template = (string) $this->element['template'];
  54. $template = preg_replace('#\W#', '', $template);
  55. // Get the style.
  56. if ($this->form instanceof JForm)
  57. {
  58. $template_style_id = $this->form->getValue('template_style_id');
  59. }
  60. $template_style_id = preg_replace('#\W#', '', $template_style_id);
  61. // If an extension and view are present build the options.
  62. if ($module && $client)
  63. {
  64. // Load language file
  65. $lang = JFactory::getLanguage();
  66. $lang->load($module . '.sys', $client->path, null, false, false)
  67. || $lang->load($module . '.sys', $client->path . '/modules/' . $module, null, false, false)
  68. || $lang->load($module . '.sys', $client->path, $lang->getDefault(), false, false)
  69. || $lang->load($module . '.sys', $client->path . '/modules/' . $module, $lang->getDefault(), false, false);
  70. // Get the database object and a new query object.
  71. $db = JFactory::getDBO();
  72. $query = $db->getQuery(true);
  73. // Build the query.
  74. $query->select('element, name');
  75. $query->from('#__extensions as e');
  76. $query->where('e.client_id = ' . (int) $clientId);
  77. $query->where('e.type = ' . $db->quote('template'));
  78. $query->where('e.enabled = 1');
  79. if ($template)
  80. {
  81. $query->where('e.element = ' . $db->quote($template));
  82. }
  83. if ($template_style_id)
  84. {
  85. $query->join('LEFT', '#__template_styles as s on s.template=e.element');
  86. $query->where('s.id=' . (int) $template_style_id);
  87. }
  88. // Set the query and load the templates.
  89. $db->setQuery($query);
  90. $templates = $db->loadObjectList('element');
  91. // Build the search paths for module layouts.
  92. $module_path = JPath::clean($client->path . '/modules/' . $module . '/tmpl');
  93. // Prepare array of component layouts
  94. $module_layouts = array();
  95. // Prepare the grouped list
  96. $groups = array();
  97. // Add the layout options from the module path.
  98. if (is_dir($module_path) && ($module_layouts = JFolder::files($module_path, '^[^_]*\.php$')))
  99. {
  100. // Create the group for the module
  101. $groups['_'] = array();
  102. $groups['_']['id'] = $this->id . '__';
  103. $groups['_']['text'] = JText::sprintf('JOPTION_FROM_MODULE');
  104. $groups['_']['items'] = array();
  105. foreach ($module_layouts as $file)
  106. {
  107. // Add an option to the module group
  108. $value = basename($file, '.php');
  109. $text = $lang->hasKey($key = strtoupper($module . '_LAYOUT_' . $value)) ? JText::_($key) : $value;
  110. $groups['_']['items'][] = JHtml::_('select.option', '_:' . $value, $text);
  111. }
  112. }
  113. // Loop on all templates
  114. if ($templates)
  115. {
  116. foreach ($templates as $template)
  117. {
  118. // Load language file
  119. $lang->load('tpl_' . $template->element . '.sys', $client->path, null, false, false)
  120. || $lang->load('tpl_' . $template->element . '.sys', $client->path . '/templates/' . $template->element, null, false, false)
  121. || $lang->load('tpl_' . $template->element . '.sys', $client->path, $lang->getDefault(), false, false)
  122. || $lang->load(
  123. 'tpl_' . $template->element . '.sys', $client->path . '/templates/' . $template->element, $lang->getDefault(),
  124. false, false
  125. );
  126. $template_path = JPath::clean($client->path . '/templates/' . $template->element . '/html/' . $module);
  127. // Add the layout options from the template path.
  128. if (is_dir($template_path) && ($files = JFolder::files($template_path, '^[^_]*\.php$')))
  129. {
  130. foreach ($files as $i => $file)
  131. {
  132. // Remove layout that already exist in component ones
  133. if (in_array($file, $module_layouts))
  134. {
  135. unset($files[$i]);
  136. }
  137. }
  138. if (count($files))
  139. {
  140. // Create the group for the template
  141. $groups[$template->element] = array();
  142. $groups[$template->element]['id'] = $this->id . '_' . $template->element;
  143. $groups[$template->element]['text'] = JText::sprintf('JOPTION_FROM_TEMPLATE', $template->name);
  144. $groups[$template->element]['items'] = array();
  145. foreach ($files as $file)
  146. {
  147. // Add an option to the template group
  148. $value = basename($file, '.php');
  149. $text = $lang->hasKey($key = strtoupper('TPL_' . $template->element . '_' . $module . '_LAYOUT_' . $value))
  150. ? JText::_($key) : $value;
  151. $groups[$template->element]['items'][] = JHtml::_('select.option', $template->element . ':' . $value, $text);
  152. }
  153. }
  154. }
  155. }
  156. }
  157. // Compute attributes for the grouped list
  158. $attr = $this->element['size'] ? ' size="' . (int) $this->element['size'] . '"' : '';
  159. // Prepare HTML code
  160. $html = array();
  161. // Compute the current selected values
  162. $selected = array($this->value);
  163. // Add a grouped list
  164. $html[] = JHtml::_(
  165. 'select.groupedlist', $groups, $this->name,
  166. array('id' => $this->id, 'group.id' => 'id', 'list.attr' => $attr, 'list.select' => $selected)
  167. );
  168. return implode($html);
  169. }
  170. else
  171. {
  172. return '';
  173. }
  174. }
  175. }