[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Form/Field/ -> ComponentlayoutField.php (source)

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


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer