Source for file componentlayout.php

Documentation is available at componentlayout.php

  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.  */
  22. {
  23.     /**
  24.      * The form field type.
  25.      *
  26.      * @var    string 
  27.      * @since  11.1
  28.      */
  29.     protected $type = 'ComponentLayout';
  30.  
  31.     /**
  32.      * Method to get the field input for a component layout field.
  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 extension.
  52.         $extn = (string) $this->element['extension'];
  53.  
  54.         if (empty($extn) && ($this->form instanceof JForm))
  55.         {
  56.             $extn = $this->form->getValue('extension');
  57.         }
  58.  
  59.         $extn = preg_replace('#\W#', '', $extn);
  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.         // Get the view.
  74.         $view = (string) $this->element['view'];
  75.         $view = preg_replace('#\W#', '', $view);
  76.  
  77.         // If a template, extension and view are present build the options.
  78.         if ($extn && $view && $client)
  79.         {
  80.  
  81.             // Load language file
  82.             $lang = JFactory::getLanguage();
  83.             $lang->load($extn . '.sys', JPATH_ADMINISTRATOR, null, false, true)
  84.                 || $lang->load($extn . '.sys', JPATH_ADMINISTRATOR . '/components/' . $extn, null, false, true);
  85.  
  86.             // Get the database object and a new query object.
  87.             $db = JFactory::getDbo();
  88.             $query = $db->getQuery(true);
  89.  
  90.             // Build the query.
  91.             $query->select('e.element, e.name')
  92.                 ->from('#__extensions as e')
  93.                 ->where('e.client_id = ' . (int) $clientId)
  94.                 ->where('e.type = ' . $db->quote('template'))
  95.                 ->where('e.enabled = 1');
  96.  
  97.             if ($template)
  98.             {
  99.                 $query->where('e.element = ' . $db->quote($template));
  100.             }
  101.  
  102.             if ($template_style_id)
  103.             {
  104.                 $query->join('LEFT', '#__template_styles as s on s.template=e.element')
  105.                     ->where('s.id=' . (int) $template_style_id);
  106.             }
  107.  
  108.             // Set the query and load the templates.
  109.             $db->setQuery($query);
  110.             $templates = $db->loadObjectList('element');
  111.  
  112.             // Build the search paths for component layouts.
  113.             $component_path = JPath::clean($client->path . '/components/' . $extn . '/views/' . $view . '/tmpl');
  114.  
  115.             // Prepare array of component layouts
  116.             $component_layouts = array();
  117.  
  118.             // Prepare the grouped list
  119.             $groups = array();
  120.  
  121.             // Add a Use Global option if useglobal="true" in XML file
  122.             if ($this->element['useglobal'] == 'true')
  123.             {
  124.                 $groups[JText::_('JOPTION_FROM_STANDARD')]['items'][] = JHtml::_('select.option', '', JText::_('JGLOBAL_USE_GLOBAL'));
  125.             }
  126.  
  127.             // Add the layout options from the component path.
  128.             if (is_dir($component_path) && ($component_layouts = JFolder::files($component_path, '^[^_]*\.xml$', false, true)))
  129.             {
  130.                 // Create the group for the component
  131.                 $groups['_'] = array();
  132.                 $groups['_']['id'] = $this->id . '__';
  133.                 $groups['_']['text'] = JText::sprintf('JOPTION_FROM_COMPONENT');
  134.                 $groups['_']['items'] = array();
  135.  
  136.                 foreach ($component_layouts as $i => $file)
  137.                 {
  138.                     // Attempt to load the XML file.
  139.                     if (!$xml = simplexml_load_file($file))
  140.                     {
  141.                         unset($component_layouts[$i]);
  142.  
  143.                         continue;
  144.                     }
  145.  
  146.                     // Get the help data from the XML file if present.
  147.                     if (!$menu = $xml->xpath('layout[1]'))
  148.                     {
  149.                         unset($component_layouts[$i]);
  150.  
  151.                         continue;
  152.                     }
  153.  
  154.                     $menu = $menu[0];
  155.  
  156.                     // Add an option to the component group
  157.                     $value = basename($file, '.xml');
  158.                     $component_layouts[$i] = $value;
  159.                     $text = isset($menu['option']) ? JText::_($menu['option']) : (isset($menu['title']) ? JText::_($menu['title']) : $value);
  160.                     $groups['_']['items'][] = JHtml::_('select.option', '_:' . $value, $text);
  161.                 }
  162.             }
  163.  
  164.             // Loop on all templates
  165.             if ($templates)
  166.             {
  167.                 foreach ($templates as $template)
  168.                 {
  169.                     // Load language file
  170.                     $lang->load('tpl_' . $template->element . '.sys', $client->path, null, false, true)
  171.                         || $lang->load('tpl_' . $template->element . '.sys', $client->path . '/templates/' . $template->element, null, false, true);
  172.  
  173.                     $template_path = JPath::clean($client->path . '/templates/' . $template->element . '/html/' . $extn . '/' . $view);
  174.  
  175.                     // Add the layout options from the template path.
  176.                     if (is_dir($template_path) && ($files = JFolder::files($template_path, '^[^_]*\.php$', false, true)))
  177.                     {
  178.                         // Files with corresponding XML files are alternate menu items, not alternate layout files
  179.                         // so we need to exclude these files from the list.
  180.                         $xml_files = JFolder::files($template_path, '^[^_]*\.xml$', false, true);
  181.                         for ($j = 0, $count = count($xml_files); $j < $count; $j++)
  182.                         {
  183.                             $xml_files[$j] = basename($xml_files[$j], '.xml');
  184.                         }
  185.                         foreach ($files as $i => $file)
  186.                         {
  187.                             // Remove layout files that exist in the component folder or that have XML files
  188.                             if ((in_array(basename($file, '.php'), $component_layouts))
  189.                                 || (in_array(basename($file, '.php'), $xml_files)))
  190.                             {
  191.                                 unset($files[$i]);
  192.                             }
  193.                         }
  194.                         if (count($files))
  195.                         {
  196.                             // Create the group for the template
  197.                             $groups[$template->name] = array();
  198.                             $groups[$template->name]['id'] = $this->id . '_' . $template->element;
  199.                             $groups[$template->name]['text'] = JText::sprintf('JOPTION_FROM_TEMPLATE', $template->name);
  200.                             $groups[$template->name]['items'] = array();
  201.  
  202.                             foreach ($files as $file)
  203.                             {
  204.                                 // Add an option to the template group
  205.                                 $value = basename($file, '.php');
  206.                                 $text = $lang
  207.                                     ->hasKey($key = strtoupper('TPL_' . $template->name . '_' . $extn . '_' . $view . '_LAYOUT_' . $value))
  208.                                     ? JText::_($key) : $value;
  209.                                 $groups[$template->name]['items'][] = JHtml::_('select.option', $template->element . ':' . $value, $text);
  210.                             }
  211.                         }
  212.                     }
  213.                 }
  214.             }
  215.  
  216.             // Compute attributes for the grouped list
  217.             $attr = $this->element['size'] ? ' size="' . (int) $this->element['size'] . '"' : '';
  218.  
  219.             // Prepare HTML code
  220.             $html = array();
  221.  
  222.             // Compute the current selected values
  223.             $selected = array($this->value);
  224.  
  225.             // Add a grouped list
  226.             $html[] = JHtml::_(
  227.                 'select.groupedlist', $groups, $this->name,
  228.                 array('id' => $this->id, 'group.id' => 'id', 'list.attr' => $attr, 'list.select' => $selected)
  229.             );
  230.  
  231.             return implode($html);
  232.         }
  233.         else
  234.         {
  235.             return '';
  236.         }
  237.     }
  238. }

Documentation generated on Tue, 19 Nov 2013 14:56:25 +0100 by phpDocumentor 1.4.3