[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Form/Field/ -> TemplatestyleField.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\HTML\HTMLHelper;
  15  use Joomla\CMS\Language\Text;
  16  use Joomla\Database\ParameterType;
  17  
  18  // phpcs:disable PSR1.Files.SideEffects
  19  \defined('JPATH_PLATFORM') or die;
  20  // phpcs:enable PSR1.Files.SideEffects
  21  
  22  /**
  23   * Supports a select grouped list of template styles
  24   *
  25   * @since  1.6
  26   */
  27  class TemplatestyleField extends GroupedlistField
  28  {
  29      /**
  30       * The form field type.
  31       *
  32       * @var    string
  33       * @since  1.6
  34       */
  35      public $type = 'TemplateStyle';
  36  
  37      /**
  38       * The client name.
  39       *
  40       * @var    mixed
  41       * @since  3.2
  42       */
  43      protected $clientName;
  44  
  45      /**
  46       * The template.
  47       *
  48       * @var    mixed
  49       * @since  3.2
  50       */
  51      protected $template;
  52  
  53      /**
  54       * Method to get certain otherwise inaccessible properties from the form field object.
  55       *
  56       * @param   string  $name  The property name for which to get the value.
  57       *
  58       * @return  mixed  The property value or null.
  59       *
  60       * @since   3.2
  61       */
  62      public function __get($name)
  63      {
  64          switch ($name) {
  65              case 'clientName':
  66              case 'template':
  67                  return $this->$name;
  68          }
  69  
  70          return parent::__get($name);
  71      }
  72  
  73      /**
  74       * Method to set certain otherwise inaccessible properties of the form field object.
  75       *
  76       * @param   string  $name   The property name for which to set the value.
  77       * @param   mixed   $value  The value of the property.
  78       *
  79       * @return  void
  80       *
  81       * @since   3.2
  82       */
  83      public function __set($name, $value)
  84      {
  85          switch ($name) {
  86              case 'clientName':
  87              case 'template':
  88                  $this->$name = (string) $value;
  89                  break;
  90  
  91              default:
  92                  parent::__set($name, $value);
  93          }
  94      }
  95  
  96      /**
  97       * Method to attach a Form object to the field.
  98       *
  99       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
 100       * @param   mixed              $value    The form field value to validate.
 101       * @param   string             $group    The field name group control value. This acts as an array container for the field.
 102       *                                       For example if the field has name="foo" and the group value is set to "bar" then the
 103       *                                       full field name would end up being "bar[foo]".
 104       *
 105       * @return  boolean  True on success.
 106       *
 107       * @see     FormField::setup()
 108       * @since   3.2
 109       */
 110      public function setup(\SimpleXMLElement $element, $value, $group = null)
 111      {
 112          $result = parent::setup($element, $value, $group);
 113  
 114          if ($result === true) {
 115              // Get the clientName template.
 116              $this->clientName = $this->element['client'] ? (string) $this->element['client'] : 'site';
 117              $this->template = (string) $this->element['template'];
 118          }
 119  
 120          return $result;
 121      }
 122  
 123      /**
 124       * Method to get the list of template style options grouped by template.
 125       * Use the client attribute to specify a specific client.
 126       * Use the template attribute to specify a specific template
 127       *
 128       * @return  array  The field option objects as a nested array in groups.
 129       *
 130       * @since   1.6
 131       */
 132      protected function getGroups()
 133      {
 134          $groups = array();
 135          $lang = Factory::getLanguage();
 136  
 137          // Get the client and client_id.
 138          $client = ApplicationHelper::getClientInfo($this->clientName, true);
 139  
 140          // Get the template.
 141          $template = $this->template;
 142  
 143          // Get the database object and a new query object.
 144          $db = $this->getDatabase();
 145          $query = $db->getQuery(true);
 146  
 147          // Build the query.
 148          $query->select(
 149              [
 150                  $db->quoteName('s.id'),
 151                  $db->quoteName('s.title'),
 152                  $db->quoteName('e.name'),
 153                  $db->quoteName('s.template'),
 154              ]
 155          )
 156              ->from($db->quoteName('#__template_styles', 's'))
 157              ->where($db->quoteName('s.client_id') . ' = :clientId')
 158              ->bind(':clientId', $client->id, ParameterType::INTEGER)
 159              ->order(
 160                  [
 161                      $db->quoteName('template'),
 162                      $db->quoteName('title'),
 163                  ]
 164              );
 165  
 166          if ($template) {
 167              $query->where('s.template = ' . $db->quote($template));
 168          }
 169  
 170          $query->join('LEFT', '#__extensions as e on e.element=s.template')
 171              ->where('e.enabled = 1')
 172              ->where($db->quoteName('e.type') . ' = ' . $db->quote('template'));
 173  
 174          // Set the query and load the styles.
 175          $db->setQuery($query);
 176          $styles = $db->loadObjectList();
 177  
 178          // Build the grouped list array.
 179          if ($styles) {
 180              foreach ($styles as $style) {
 181                  $template = $style->template;
 182                  $lang->load('tpl_' . $template . '.sys', $client->path)
 183                      || $lang->load('tpl_' . $template . '.sys', $client->path . '/templates/' . $template);
 184                  $name = Text::_($style->name);
 185  
 186                  // Initialize the group if necessary.
 187                  if (!isset($groups[$name])) {
 188                      $groups[$name] = array();
 189                  }
 190  
 191                  $groups[$name][] = HTMLHelper::_('select.option', $style->id, $style->title);
 192              }
 193          }
 194  
 195          // Merge any additional groups in the XML definition.
 196          $groups = array_merge(parent::getGroups(), $groups);
 197  
 198          return $groups;
 199      }
 200  }


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