[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/libraries/joomla/form/fields/ -> componentlayout.php (source)

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


Generated: Tue Apr 3 11:40:28 2012 Cross-referenced by PHPXref 0.7.1