[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_fields/src/Table/ -> FieldTable.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_fields
   6   *
   7   * @copyright   (C) 2016 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9   */
  10  
  11  namespace Joomla\Component\Fields\Administrator\Table;
  12  
  13  use Joomla\CMS\Access\Rules;
  14  use Joomla\CMS\Application\ApplicationHelper;
  15  use Joomla\CMS\Factory;
  16  use Joomla\CMS\Language\Text;
  17  use Joomla\CMS\Table\Table;
  18  use Joomla\Database\DatabaseDriver;
  19  use Joomla\Registry\Registry;
  20  use Joomla\String\StringHelper;
  21  
  22  // phpcs:disable PSR1.Files.SideEffects
  23  \defined('_JEXEC') or die;
  24  // phpcs:enable PSR1.Files.SideEffects
  25  
  26  /**
  27   * Fields Table
  28   *
  29   * @since  3.7.0
  30   */
  31  class FieldTable extends Table
  32  {
  33      /**
  34       * Indicates that columns fully support the NULL value in the database
  35       *
  36       * @var    boolean
  37       * @since  4.0.0
  38       */
  39      protected $_supportNullValue = true;
  40  
  41      /**
  42       * Class constructor.
  43       *
  44       * @param   DatabaseDriver  $db  DatabaseDriver object.
  45       *
  46       * @since   3.7.0
  47       */
  48      public function __construct($db = null)
  49      {
  50          parent::__construct('#__fields', 'id', $db);
  51  
  52          $this->setColumnAlias('published', 'state');
  53      }
  54  
  55      /**
  56       * Method to bind an associative array or object to the JTable instance.This
  57       * method only binds properties that are publicly accessible and optionally
  58       * takes an array of properties to ignore when binding.
  59       *
  60       * @param   mixed  $src     An associative array or object to bind to the JTable instance.
  61       * @param   mixed  $ignore  An optional array or space separated list of properties to ignore while binding.
  62       *
  63       * @return  boolean  True on success.
  64       *
  65       * @since   3.7.0
  66       * @throws  \InvalidArgumentException
  67       */
  68      public function bind($src, $ignore = '')
  69      {
  70          if (isset($src['params']) && is_array($src['params'])) {
  71              $registry = new Registry();
  72              $registry->loadArray($src['params']);
  73              $src['params'] = (string) $registry;
  74          }
  75  
  76          if (isset($src['fieldparams']) && is_array($src['fieldparams'])) {
  77              // Make sure $registry->options contains no duplicates when the field type is subform
  78              if (isset($src['type']) && $src['type'] == 'subform' && isset($src['fieldparams']['options'])) {
  79                  // Fast lookup map to check which custom field ids we have already seen
  80                  $seen_customfields = array();
  81  
  82                  // Container for the new $src['fieldparams']['options']
  83                  $options = array();
  84  
  85                  // Iterate through the old options
  86                  $i = 0;
  87  
  88                  foreach ($src['fieldparams']['options'] as $option) {
  89                      // Check whether we have not yet seen this custom field id
  90                      if (!isset($seen_customfields[$option['customfield']])) {
  91                          // We haven't, so add it to the final options
  92                          $seen_customfields[$option['customfield']] = true;
  93                          $options['option' . $i] = $option;
  94                          $i++;
  95                      }
  96                  }
  97  
  98                  // And replace the options with the deduplicated ones.
  99                  $src['fieldparams']['options'] = $options;
 100              }
 101  
 102              $registry = new Registry();
 103              $registry->loadArray($src['fieldparams']);
 104              $src['fieldparams'] = (string) $registry;
 105          }
 106  
 107          // Bind the rules.
 108          if (isset($src['rules']) && is_array($src['rules'])) {
 109              $rules = new Rules($src['rules']);
 110              $this->setRules($rules);
 111          }
 112  
 113          return parent::bind($src, $ignore);
 114      }
 115  
 116      /**
 117       * Method to perform sanity checks on the JTable instance properties to ensure
 118       * they are safe to store in the database.  Child classes should override this
 119       * method to make sure the data they are storing in the database is safe and
 120       * as expected before storage.
 121       *
 122       * @return  boolean  True if the instance is sane and able to be stored in the database.
 123       *
 124       * @link    https://docs.joomla.org/Special:MyLanguage/JTable/check
 125       * @since   3.7.0
 126       */
 127      public function check()
 128      {
 129          // Check for valid name
 130          if (trim($this->title) == '') {
 131              $this->setError(Text::_('COM_FIELDS_MUSTCONTAIN_A_TITLE_FIELD'));
 132  
 133              return false;
 134          }
 135  
 136          if (empty($this->name)) {
 137              $this->name = $this->title;
 138          }
 139  
 140          $this->name = ApplicationHelper::stringURLSafe($this->name, $this->language);
 141  
 142          if (trim(str_replace('-', '', $this->name)) == '') {
 143              $this->name = StringHelper::increment($this->name, 'dash');
 144          }
 145  
 146          $this->name = str_replace(',', '-', $this->name);
 147  
 148          // Verify that the name is unique
 149          $table = new static($this->_db);
 150  
 151          if ($table->load(array('name' => $this->name)) && ($table->id != $this->id || $this->id == 0)) {
 152              $this->setError(Text::_('COM_FIELDS_ERROR_UNIQUE_NAME'));
 153  
 154              return false;
 155          }
 156  
 157          $this->name = str_replace(',', '-', $this->name);
 158  
 159          if (empty($this->type)) {
 160              $this->type = 'text';
 161          }
 162  
 163          if (empty($this->fieldparams)) {
 164              $this->fieldparams = '{}';
 165          }
 166  
 167          $date = Factory::getDate()->toSql();
 168          $user = Factory::getUser();
 169  
 170          // Set created date if not set.
 171          if (!(int) $this->created_time) {
 172              $this->created_time = $date;
 173          }
 174  
 175          if ($this->id) {
 176              // Existing item
 177              $this->modified_time = $date;
 178              $this->modified_by = $user->get('id');
 179          } else {
 180              if (!(int) $this->modified_time) {
 181                  $this->modified_time = $this->created_time;
 182              }
 183  
 184              if (empty($this->created_user_id)) {
 185                  $this->created_user_id = $user->get('id');
 186              }
 187  
 188              if (empty($this->modified_by)) {
 189                  $this->modified_by = $this->created_user_id;
 190              }
 191          }
 192  
 193          if (empty($this->group_id)) {
 194              $this->group_id = 0;
 195          }
 196  
 197          return true;
 198      }
 199  
 200      /**
 201       * Overloaded store function
 202       *
 203       * @param   boolean  $updateNulls  True to update fields even if they are null.
 204       *
 205       * @return  mixed  False on failure, positive integer on success.
 206       *
 207       * @see     Table::store()
 208       * @since   4.0.0
 209       */
 210      public function store($updateNulls = true)
 211      {
 212          return parent::store($updateNulls);
 213      }
 214  
 215      /**
 216       * Method to compute the default name of the asset.
 217       * The default name is in the form table_name.id
 218       * where id is the value of the primary key of the table.
 219       *
 220       * @return  string
 221       *
 222       * @since   3.7.0
 223       */
 224      protected function _getAssetName()
 225      {
 226          $contextArray = explode('.', $this->context);
 227  
 228          return $contextArray[0] . '.field.' . (int) $this->id;
 229      }
 230  
 231      /**
 232       * Method to return the title to use for the asset table.  In
 233       * tracking the assets a title is kept for each asset so that there is some
 234       * context available in a unified access manager.  Usually this would just
 235       * return $this->title or $this->name or whatever is being used for the
 236       * primary name of the row. If this method is not overridden, the asset name is used.
 237       *
 238       * @return  string  The string to use as the title in the asset table.
 239       *
 240       * @link    https://docs.joomla.org/Special:MyLanguage/JTable/getAssetTitle
 241       * @since   3.7.0
 242       */
 243      protected function _getAssetTitle()
 244      {
 245          return $this->title;
 246      }
 247  
 248      /**
 249       * Method to get the parent asset under which to register this one.
 250       * By default, all assets are registered to the ROOT node with ID,
 251       * which will default to 1 if none exists.
 252       * The extended class can define a table and id to lookup.  If the
 253       * asset does not exist it will be created.
 254       *
 255       * @param   Table    $table  A Table object for the asset parent.
 256       * @param   integer  $id     Id to look up
 257       *
 258       * @return  integer
 259       *
 260       * @since   3.7.0
 261       */
 262      protected function _getAssetParentId(Table $table = null, $id = null)
 263      {
 264          $contextArray = explode('.', $this->context);
 265          $component = $contextArray[0];
 266  
 267          if ($this->group_id) {
 268              $assetId = $this->getAssetId($component . '.fieldgroup.' . (int) $this->group_id);
 269  
 270              if ($assetId) {
 271                  return $assetId;
 272              }
 273          } else {
 274              $assetId = $this->getAssetId($component);
 275  
 276              if ($assetId) {
 277                  return $assetId;
 278              }
 279          }
 280  
 281          return parent::_getAssetParentId($table, $id);
 282      }
 283  
 284      /**
 285       * Returns an asset id for the given name or false.
 286       *
 287       * @param   string  $name  The asset name
 288       *
 289       * @return  number|boolean
 290       *
 291       * @since    3.7.0
 292       */
 293      private function getAssetId($name)
 294      {
 295          $db = $this->getDbo();
 296          $query = $db->getQuery(true)
 297              ->select($db->quoteName('id'))
 298              ->from($db->quoteName('#__assets'))
 299              ->where($db->quoteName('name') . ' = :name')
 300              ->bind(':name', $name);
 301  
 302          // Get the asset id from the database.
 303          $db->setQuery($query);
 304  
 305          $assetId = null;
 306  
 307          if ($result = $db->loadResult()) {
 308              $assetId = (int) $result;
 309  
 310              if ($assetId) {
 311                  return $assetId;
 312              }
 313          }
 314  
 315          return false;
 316      }
 317  }


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