[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2012 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\Filesystem\Folder;
  14  use Joomla\CMS\Form\Form;
  15  use Joomla\CMS\HTML\HTMLHelper;
  16  use Joomla\CMS\Language\Text;
  17  use Joomla\Database\ParameterType;
  18  
  19  // phpcs:disable PSR1.Files.SideEffects
  20  \defined('JPATH_PLATFORM') or die;
  21  // phpcs:enable PSR1.Files.SideEffects
  22  
  23  /**
  24   * Chrome Styles field.
  25   *
  26   * @since  3.0
  27   */
  28  class ChromestyleField extends GroupedlistField
  29  {
  30      /**
  31       * The form field type.
  32       *
  33       * @var    string
  34       * @since  3.0
  35       */
  36      public $type = 'ChromeStyle';
  37  
  38      /**
  39       * The client ID.
  40       *
  41       * @var    integer
  42       * @since  3.2
  43       */
  44      protected $clientId;
  45  
  46      /**
  47       * Method to get certain otherwise inaccessible properties from the form field object.
  48       *
  49       * @param   string  $name  The property name for which to get the value.
  50       *
  51       * @return  mixed  The property value or null.
  52       *
  53       * @since   3.2
  54       */
  55      public function __get($name)
  56      {
  57          if ($name === 'clientId') {
  58              return $this->clientId;
  59          }
  60  
  61          return parent::__get($name);
  62      }
  63  
  64      /**
  65       * Method to set certain otherwise inaccessible properties of the form field object.
  66       *
  67       * @param   string  $name   The property name for which to get the value.
  68       * @param   mixed   $value  The value of the property.
  69       *
  70       * @return  void
  71       *
  72       * @since   3.2
  73       */
  74      public function __set($name, $value)
  75      {
  76          switch ($name) {
  77              case 'clientId':
  78                  $this->clientId = (int) $value;
  79                  break;
  80  
  81              default:
  82                  parent::__set($name, $value);
  83          }
  84      }
  85  
  86      /**
  87       * Method to attach a Form object to the field.
  88       *
  89       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
  90       * @param   mixed              $value    The form field value to validate.
  91       * @param   string             $group    The field name group control value. This acts as an array container for the field.
  92       *                                       For example if the field has name="foo" and the group value is set to "bar" then the
  93       *                                       full field name would end up being "bar[foo]".
  94       *
  95       * @return  boolean  True on success.
  96       *
  97       * @see     FormField::setup()
  98       * @since   3.2
  99       */
 100      public function setup(\SimpleXMLElement $element, $value, $group = null)
 101      {
 102          $result = parent::setup($element, $value, $group);
 103  
 104          if ($result === true) {
 105              // Get the client id.
 106              $clientId = $this->element['client_id'];
 107  
 108              if (!isset($clientId)) {
 109                  $clientName = $this->element['client'];
 110  
 111                  if (isset($clientName)) {
 112                      $client = ApplicationHelper::getClientInfo($clientName, true);
 113                      $clientId = $client->id;
 114                  }
 115              }
 116  
 117              if (!isset($clientId) && $this->form instanceof Form) {
 118                  $clientId = $this->form->getValue('client_id');
 119              }
 120  
 121              $this->clientId = (int) $clientId;
 122          }
 123  
 124          return $result;
 125      }
 126  
 127  
 128      /**
 129       * Method to get the list of template chrome style options
 130       * grouped by template.
 131       *
 132       * @return  array  The field option objects as a nested array in groups.
 133       *
 134       * @since   3.0
 135       */
 136      protected function getGroups()
 137      {
 138          $groups = array();
 139  
 140          // Add Module Style Field
 141          $tmp = '---' . Text::_('JLIB_FORM_VALUE_FROM_TEMPLATE') . '---';
 142          $groups[$tmp][] = HTMLHelper::_('select.option', '0', Text::_('JLIB_FORM_VALUE_INHERITED'));
 143  
 144          $templateStyles = $this->getTemplateModuleStyles();
 145  
 146          // Create one new option object for each available style, grouped by templates
 147          foreach ($templateStyles as $template => $styles) {
 148              $template = ucfirst($template);
 149              $groups[$template] = array();
 150  
 151              foreach ($styles as $style) {
 152                  $tmp = HTMLHelper::_('select.option', $template . '-' . $style, $style);
 153                  $groups[$template][] = $tmp;
 154              }
 155          }
 156  
 157          reset($groups);
 158  
 159          return $groups;
 160      }
 161  
 162      /**
 163       * Method to get the templates module styles.
 164       *
 165       * @return  array  The array of styles, grouped by templates.
 166       *
 167       * @since   3.0
 168       */
 169      protected function getTemplateModuleStyles()
 170      {
 171          $moduleStyles = array();
 172  
 173          // Global Layouts
 174          $layouts = Folder::files(JPATH_SITE . '/layouts/chromes', '.*\.php');
 175  
 176          foreach ($layouts as &$layout) {
 177              $layout = basename($layout, '.php');
 178          }
 179  
 180          $moduleStyles['system'] = $layouts;
 181  
 182          $templates = $this->getTemplates();
 183          $path      = JPATH_ADMINISTRATOR;
 184  
 185          if ($this->clientId === 0) {
 186              $path = JPATH_SITE;
 187          }
 188  
 189          foreach ($templates as $template) {
 190              $chromeLayoutPath = $path . '/templates/' . $template->element . '/html/layouts/chromes';
 191  
 192              if (!Folder::exists($chromeLayoutPath)) {
 193                  continue;
 194              }
 195  
 196              $layouts = Folder::files($chromeLayoutPath, '.*\.php');
 197  
 198              if ($layouts) {
 199                  foreach ($layouts as &$layout) {
 200                      $layout = basename($layout, '.php');
 201                  }
 202  
 203                  $moduleStyles[$template->element] = $layouts;
 204              }
 205          }
 206  
 207          return $moduleStyles;
 208      }
 209  
 210      /**
 211       * Return a list of templates
 212       *
 213       * @return  array  List of templates
 214       *
 215       * @since   3.2.1
 216       */
 217      protected function getTemplates()
 218      {
 219          $db = $this->getDatabase();
 220  
 221          // Get the database object and a new query object.
 222          $query = $db->getQuery(true);
 223  
 224          // Build the query.
 225          $query->select(
 226              [
 227                  $db->quoteName('element'),
 228                  $db->quoteName('name'),
 229              ]
 230          )
 231              ->from($db->quoteName('#__extensions'))
 232              ->where(
 233                  [
 234                      $db->quoteName('client_id') . ' = :clientId',
 235                      $db->quoteName('type') . ' = ' . $db->quote('template'),
 236                      $db->quoteName('enabled') . ' = 1',
 237                  ]
 238              )
 239              ->bind(':clientId', $this->clientId, ParameterType::INTEGER);
 240  
 241          // Set the query and load the templates.
 242          $db->setQuery($query);
 243  
 244          return $db->loadObjectList('element');
 245      }
 246  }


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