[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Table/ -> Category.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2005 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\Table;
  11  
  12  use Joomla\CMS\Access\Rules;
  13  use Joomla\CMS\Application\ApplicationHelper;
  14  use Joomla\CMS\Factory;
  15  use Joomla\CMS\Language\Text;
  16  use Joomla\CMS\Tag\TaggableTableInterface;
  17  use Joomla\CMS\Tag\TaggableTableTrait;
  18  use Joomla\CMS\Versioning\VersionableTableInterface;
  19  use Joomla\Database\DatabaseDriver;
  20  use Joomla\Database\ParameterType;
  21  use Joomla\Registry\Registry;
  22  
  23  // phpcs:disable PSR1.Files.SideEffects
  24  \defined('JPATH_PLATFORM') or die;
  25  // phpcs:enable PSR1.Files.SideEffects
  26  
  27  /**
  28   * Category table
  29   *
  30   * @since  1.5
  31   */
  32  class Category extends Nested implements VersionableTableInterface, TaggableTableInterface
  33  {
  34      use TaggableTableTrait;
  35  
  36      /**
  37       * Indicates that columns fully support the NULL value in the database
  38       *
  39       * @var    boolean
  40       * @since  4.0.0
  41       */
  42      protected $_supportNullValue = true;
  43  
  44      /**
  45       * Constructor
  46       *
  47       * @param   DatabaseDriver  $db  Database driver object.
  48       *
  49       * @since   1.5
  50       */
  51      public function __construct(DatabaseDriver $db)
  52      {
  53          // @deprecated 5.0 This format was used by tags and versioning before 4.0 before the introduction of the
  54          //                 getTypeAlias function. This notation with the {} will be removed in Joomla 5
  55          $this->typeAlias = '{extension}.category';
  56          parent::__construct('#__categories', 'id', $db);
  57          $this->access = (int) Factory::getApplication()->get('access');
  58      }
  59  
  60      /**
  61       * Method to compute the default name of the asset.
  62       * The default name is in the form table_name.id
  63       * where id is the value of the primary key of the table.
  64       *
  65       * @return  string
  66       *
  67       * @since   1.6
  68       */
  69      protected function _getAssetName()
  70      {
  71          $k = $this->_tbl_key;
  72  
  73          return $this->extension . '.category.' . (int) $this->$k;
  74      }
  75  
  76      /**
  77       * Method to return the title to use for the asset table.
  78       *
  79       * @return  string
  80       *
  81       * @since   1.6
  82       */
  83      protected function _getAssetTitle()
  84      {
  85          return $this->title;
  86      }
  87  
  88      /**
  89       * Get the parent asset id for the record
  90       *
  91       * @param   Table    $table  A Table object for the asset parent.
  92       * @param   integer  $id     The id for the asset
  93       *
  94       * @return  integer  The id of the asset's parent
  95       *
  96       * @since   1.6
  97       */
  98      protected function _getAssetParentId(Table $table = null, $id = null)
  99      {
 100          $assetId = null;
 101  
 102          // This is a category under a category.
 103          if ($this->parent_id > 1) {
 104              // Build the query to get the asset id for the parent category.
 105              $query = $this->_db->getQuery(true)
 106                  ->select($this->_db->quoteName('asset_id'))
 107                  ->from($this->_db->quoteName('#__categories'))
 108                  ->where($this->_db->quoteName('id') . ' = :parentId')
 109                  ->bind(':parentId', $this->parent_id, ParameterType::INTEGER);
 110  
 111              // Get the asset id from the database.
 112              $this->_db->setQuery($query);
 113  
 114              if ($result = $this->_db->loadResult()) {
 115                  $assetId = (int) $result;
 116              }
 117          } elseif ($assetId === null) {
 118              // This is a category that needs to parent with the extension.
 119              // Build the query to get the asset id for the parent category.
 120              $query = $this->_db->getQuery(true)
 121                  ->select($this->_db->quoteName('id'))
 122                  ->from($this->_db->quoteName('#__assets'))
 123                  ->where($this->_db->quoteName('name') . ' = :extension')
 124                  ->bind(':extension', $this->extension);
 125  
 126              // Get the asset id from the database.
 127              $this->_db->setQuery($query);
 128  
 129              if ($result = $this->_db->loadResult()) {
 130                  $assetId = (int) $result;
 131              }
 132          }
 133  
 134          // Return the asset id.
 135          if ($assetId) {
 136              return $assetId;
 137          } else {
 138              return parent::_getAssetParentId($table, $id);
 139          }
 140      }
 141  
 142      /**
 143       * Override check function
 144       *
 145       * @return  boolean
 146       *
 147       * @see     Table::check()
 148       * @since   1.5
 149       */
 150      public function check()
 151      {
 152          try {
 153              parent::check();
 154          } catch (\Exception $e) {
 155              $this->setError($e->getMessage());
 156  
 157              return false;
 158          }
 159  
 160          // Check for a title.
 161          if (trim($this->title) == '') {
 162              $this->setError(Text::_('JLIB_DATABASE_ERROR_MUSTCONTAIN_A_TITLE_CATEGORY'));
 163  
 164              return false;
 165          }
 166  
 167          $this->alias = trim($this->alias);
 168  
 169          if (empty($this->alias)) {
 170              $this->alias = $this->title;
 171          }
 172  
 173          $this->alias = ApplicationHelper::stringURLSafe($this->alias, $this->language);
 174  
 175          if (trim(str_replace('-', '', $this->alias)) == '') {
 176              $this->alias = Factory::getDate()->format('Y-m-d-H-i-s');
 177          }
 178  
 179          return true;
 180      }
 181  
 182      /**
 183       * Overloaded bind function.
 184       *
 185       * @param   array   $array   named array
 186       * @param   string  $ignore  An optional array or space separated list of properties
 187       *                           to ignore while binding.
 188       *
 189       * @return  mixed   Null if operation was satisfactory, otherwise returns an error
 190       *
 191       * @see     Table::bind()
 192       * @since   1.6
 193       */
 194      public function bind($array, $ignore = '')
 195      {
 196          if (isset($array['params']) && \is_array($array['params'])) {
 197              $registry = new Registry($array['params']);
 198              $array['params'] = (string) $registry;
 199          }
 200  
 201          if (isset($array['metadata']) && \is_array($array['metadata'])) {
 202              $registry = new Registry($array['metadata']);
 203              $array['metadata'] = (string) $registry;
 204          }
 205  
 206          // Bind the rules.
 207          if (isset($array['rules']) && \is_array($array['rules'])) {
 208              $rules = new Rules($array['rules']);
 209              $this->setRules($rules);
 210          }
 211  
 212          return parent::bind($array, $ignore);
 213      }
 214  
 215      /**
 216       * Overridden Table::store to set created/modified and user id.
 217       *
 218       * @param   boolean  $updateNulls  True to update fields even if they are null.
 219       *
 220       * @return  boolean  True on success.
 221       *
 222       * @since   1.6
 223       */
 224      public function store($updateNulls = true)
 225      {
 226          $date = Factory::getDate()->toSql();
 227          $user = Factory::getUser();
 228  
 229          // Set created date if not set.
 230          if (!(int) $this->created_time) {
 231              $this->created_time = $date;
 232          }
 233  
 234          if ($this->id) {
 235              // Existing category
 236              $this->modified_user_id = $user->get('id');
 237              $this->modified_time    = $date;
 238          } else {
 239              if (!(int) ($this->modified_time)) {
 240                  $this->modified_time = $this->created_time;
 241              }
 242  
 243              // Field created_user_id can be set by the user, so we don't touch it if it's set.
 244              if (empty($this->created_user_id)) {
 245                  $this->created_user_id = $user->get('id');
 246              }
 247  
 248              if (empty($this->modified_user_id)) {
 249                  $this->modified_user_id = $this->created_user_id;
 250              }
 251          }
 252  
 253          // Verify that the alias is unique
 254          $table = Table::getInstance('Category', 'JTable', array('dbo' => $this->getDbo()));
 255  
 256          if (
 257              $table->load(array('alias' => $this->alias, 'parent_id' => (int) $this->parent_id, 'extension' => $this->extension))
 258              && ($table->id != $this->id || $this->id == 0)
 259          ) {
 260              $this->setError(Text::_('JLIB_DATABASE_ERROR_CATEGORY_UNIQUE_ALIAS'));
 261  
 262              return false;
 263          }
 264  
 265          return parent::store($updateNulls);
 266      }
 267  
 268      /**
 269       * Get the type alias for the history table
 270       *
 271       * @return  string  The alias as described above
 272       *
 273       * @since   4.0.0
 274       */
 275      public function getTypeAlias()
 276      {
 277          return $this->extension . '.category';
 278      }
 279  }


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