[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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


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