[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2013 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\Form\FormField;
  13  use Joomla\CMS\HTML\HTMLHelper;
  14  use Joomla\CMS\UCM\UCMType;
  15  use Joomla\Database\DatabaseQuery;
  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   * Ordering field.
  24   *
  25   * @since  3.2
  26   */
  27  class OrderingField extends FormField
  28  {
  29      /**
  30       * The form field type.
  31       *
  32       * @var     string
  33       * @since   3.2
  34       */
  35      protected $type = 'Ordering';
  36  
  37      /**
  38       * The form field content type.
  39       *
  40       * @var     string
  41       * @since   3.2
  42       */
  43      protected $contentType;
  44  
  45      /**
  46       * Method to get certain otherwise inaccessible properties from the form field object.
  47       *
  48       * @param   string  $name  The property name for which to get the value.
  49       *
  50       * @return  mixed  The property value or null.
  51       *
  52       * @since   3.2
  53       */
  54      public function __get($name)
  55      {
  56          if ($name === 'contentType') {
  57              return $this->contentType;
  58          }
  59  
  60          return parent::__get($name);
  61      }
  62  
  63      /**
  64       * Method to set certain otherwise inaccessible properties of the form field object.
  65       *
  66       * @param   string  $name   The property name for which to set the value.
  67       * @param   mixed   $value  The value of the property.
  68       *
  69       * @return  void
  70       *
  71       * @since   3.2
  72       */
  73      public function __set($name, $value)
  74      {
  75          switch ($name) {
  76              case 'contentType':
  77                  $this->contentType = (string) $value;
  78                  break;
  79  
  80              default:
  81                  parent::__set($name, $value);
  82          }
  83      }
  84  
  85      /**
  86       * Method to attach a Form object to the field.
  87       *
  88       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
  89       * @param   mixed              $value    The form field value to validate.
  90       * @param   string             $group    The field name group control value. This acts as an array container for the field.
  91       *                                       For example if the field has name="foo" and the group value is set to "bar" then the
  92       *                                       full field name would end up being "bar[foo]".
  93       *
  94       * @return  boolean  True on success.
  95       *
  96       * @see     FormField::setup()
  97       * @since   3.2
  98       */
  99      public function setup(\SimpleXMLElement $element, $value, $group = null)
 100      {
 101          $result = parent::setup($element, $value, $group);
 102  
 103          if ($result === true) {
 104              $this->contentType = (string) $this->element['content_type'];
 105          }
 106  
 107          return $result;
 108      }
 109  
 110      /**
 111       * Method to get the field input markup.
 112       *
 113       * @return  string  The field input markup.
 114       *
 115       * @since   3.2
 116       */
 117      protected function getInput()
 118      {
 119          $html = array();
 120          $attr = '';
 121  
 122          // Initialize some field attributes.
 123          $attr .= !empty($this->class) ? ' class="form-select' . $this->class . '"' : ' class="form-select"';
 124          $attr .= $this->disabled ? ' disabled' : '';
 125          $attr .= !empty($this->size) ? ' size="' . $this->size . '"' : '';
 126  
 127          // Initialize JavaScript field attributes.
 128          $attr .= !empty($this->onchange) ? ' onchange="' . $this->onchange . '"' : '';
 129  
 130          $itemId = (int) $this->getItemId();
 131  
 132          $query = $this->getQuery();
 133  
 134          // Create a read-only list (no name) with a hidden input to store the value.
 135          if ($this->readonly) {
 136              $html[] = HTMLHelper::_('list.ordering', '', $query, trim($attr), $this->value, $itemId ? 0 : 1, $this->id);
 137              $html[] = '<input type="hidden" name="' . $this->name . '" value="' . $this->value . '">';
 138          } else {
 139              // Create a regular list.
 140              $html[] = HTMLHelper::_('list.ordering', $this->name, $query, trim($attr), $this->value, $itemId ? 0 : 1, $this->id);
 141          }
 142  
 143          return implode($html);
 144      }
 145  
 146      /**
 147       * Builds the query for the ordering list.
 148       *
 149       * @return  DatabaseQuery  The query for the ordering form field
 150       *
 151       * @since   3.2
 152       */
 153      protected function getQuery()
 154      {
 155          $categoryId   = (int) $this->form->getValue('catid');
 156          $ucmType      = new UCMType();
 157          $ucmRow       = $ucmType->getType($ucmType->getTypeId($this->contentType));
 158          $ucmMapCommon = json_decode($ucmRow->field_mappings)->common;
 159  
 160          if (\is_object($ucmMapCommon)) {
 161              $ordering = $ucmMapCommon->core_ordering;
 162              $title    = $ucmMapCommon->core_title;
 163          } elseif (\is_array($ucmMapCommon)) {
 164              $ordering = $ucmMapCommon[0]->core_ordering;
 165              $title    = $ucmMapCommon[0]->core_title;
 166          }
 167  
 168          $db    = $this->getDatabase();
 169          $query = $db->getQuery(true);
 170          $query->select([$db->quoteName($ordering, 'value'), $db->quoteName($title, 'text')])
 171              ->from($db->quoteName(json_decode($ucmRow->table)->special->dbtable))
 172              ->where($db->quoteName('catid') . ' = :categoryId')
 173              ->order($db->quoteName('ordering'))
 174              ->bind(':categoryId', $categoryId, ParameterType::INTEGER);
 175  
 176          return $query;
 177      }
 178  
 179      /**
 180       * Retrieves the current Item's Id.
 181       *
 182       * @return  integer  The current item ID
 183       *
 184       * @since   3.2
 185       */
 186      protected function getItemId()
 187      {
 188          return (int) $this->form->getValue('id');
 189      }
 190  }


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