[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Table/ -> Content.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  use Joomla\String\StringHelper;
  23  
  24  // phpcs:disable PSR1.Files.SideEffects
  25  \defined('JPATH_PLATFORM') or die;
  26  // phpcs:enable PSR1.Files.SideEffects
  27  
  28  /**
  29   * Content table
  30   *
  31   * @since  1.5
  32   */
  33  class Content extends Table implements VersionableTableInterface, TaggableTableInterface
  34  {
  35      use TaggableTableTrait;
  36  
  37      /**
  38       * Indicates that columns fully support the NULL value in the database
  39       *
  40       * @var    boolean
  41       * @since  4.0.0
  42       */
  43      protected $_supportNullValue = true;
  44  
  45      /**
  46       * Constructor
  47       *
  48       * @param   DatabaseDriver  $db  A database connector object
  49       *
  50       * @since   1.5
  51       */
  52      public function __construct(DatabaseDriver $db)
  53      {
  54          $this->typeAlias = 'com_content.article';
  55  
  56          parent::__construct('#__content', 'id', $db);
  57  
  58          // Set the alias since the column is called state
  59          $this->setColumnAlias('published', 'state');
  60      }
  61  
  62      /**
  63       * Method to compute the default name of the asset.
  64       * The default name is in the form table_name.id
  65       * where id is the value of the primary key of the table.
  66       *
  67       * @return  string
  68       *
  69       * @since   1.6
  70       */
  71      protected function _getAssetName()
  72      {
  73          $k = $this->_tbl_key;
  74  
  75          return 'com_content.article.' . (int) $this->$k;
  76      }
  77  
  78      /**
  79       * Method to return the title to use for the asset table.
  80       *
  81       * @return  string
  82       *
  83       * @since   1.6
  84       */
  85      protected function _getAssetTitle()
  86      {
  87          return $this->title;
  88      }
  89  
  90      /**
  91       * Method to get the parent asset id for the record
  92       *
  93       * @param   Table    $table  A Table object (optional) for the asset parent
  94       * @param   integer  $id     The id (optional) of the content.
  95       *
  96       * @return  integer
  97       *
  98       * @since   1.6
  99       */
 100      protected function _getAssetParentId(Table $table = null, $id = null)
 101      {
 102          $assetId = null;
 103  
 104          // This is an article under a category.
 105          if ($this->catid) {
 106              $catId = (int) $this->catid;
 107  
 108              // Build the query to get the asset id for the parent category.
 109              $query = $this->_db->getQuery(true)
 110                  ->select($this->_db->quoteName('asset_id'))
 111                  ->from($this->_db->quoteName('#__categories'))
 112                  ->where($this->_db->quoteName('id') . ' = :catid')
 113                  ->bind(':catid', $catId, ParameterType::INTEGER);
 114  
 115              // Get the asset id from the database.
 116              $this->_db->setQuery($query);
 117  
 118              if ($result = $this->_db->loadResult()) {
 119                  $assetId = (int) $result;
 120              }
 121          }
 122  
 123          // Return the asset id.
 124          if ($assetId) {
 125              return $assetId;
 126          } else {
 127              return parent::_getAssetParentId($table, $id);
 128          }
 129      }
 130  
 131      /**
 132       * Overloaded bind function
 133       *
 134       * @param   array  $array   Named array
 135       * @param   mixed  $ignore  An optional array or space separated list of properties
 136       *                          to ignore while binding.
 137       *
 138       * @return  mixed  Null if operation was satisfactory, otherwise returns an error string
 139       *
 140       * @see     Table::bind()
 141       * @since   1.6
 142       */
 143      public function bind($array, $ignore = '')
 144      {
 145          // Search for the {readmore} tag and split the text up accordingly.
 146          if (isset($array['articletext'])) {
 147              $pattern = '#<hr\s+id=("|\')system-readmore("|\')\s*\/*>#i';
 148              $tagPos = preg_match($pattern, $array['articletext']);
 149  
 150              if ($tagPos == 0) {
 151                  $this->introtext = $array['articletext'];
 152                  $this->fulltext = '';
 153              } else {
 154                  list ($this->introtext, $this->fulltext) = preg_split($pattern, $array['articletext'], 2);
 155              }
 156          }
 157  
 158          if (isset($array['attribs']) && \is_array($array['attribs'])) {
 159              $registry = new Registry($array['attribs']);
 160              $array['attribs'] = (string) $registry;
 161          }
 162  
 163          if (isset($array['metadata']) && \is_array($array['metadata'])) {
 164              $registry = new Registry($array['metadata']);
 165              $array['metadata'] = (string) $registry;
 166          }
 167  
 168          // Bind the rules.
 169          if (isset($array['rules']) && \is_array($array['rules'])) {
 170              $rules = new Rules($array['rules']);
 171              $this->setRules($rules);
 172          }
 173  
 174          return parent::bind($array, $ignore);
 175      }
 176  
 177      /**
 178       * Overloaded check function
 179       *
 180       * @return  boolean  True on success, false on failure
 181       *
 182       * @see     Table::check()
 183       * @since   1.5
 184       */
 185      public function check()
 186      {
 187          try {
 188              parent::check();
 189          } catch (\Exception $e) {
 190              $this->setError($e->getMessage());
 191  
 192              return false;
 193          }
 194  
 195          if (trim($this->title) == '') {
 196              $this->setError(Text::_('COM_CONTENT_WARNING_PROVIDE_VALID_NAME'));
 197  
 198              return false;
 199          }
 200  
 201          if (trim($this->alias) == '') {
 202              $this->alias = $this->title;
 203          }
 204  
 205          $this->alias = ApplicationHelper::stringURLSafe($this->alias, $this->language);
 206  
 207          if (trim(str_replace('-', '', $this->alias)) == '') {
 208              $this->alias = Factory::getDate()->format('Y-m-d-H-i-s');
 209          }
 210  
 211          // Check for a valid category.
 212          if (!$this->catid = (int) $this->catid) {
 213              $this->setError(Text::_('JLIB_DATABASE_ERROR_CATEGORY_REQUIRED'));
 214  
 215              return false;
 216          }
 217  
 218          if (trim(str_replace('&nbsp;', '', $this->fulltext)) == '') {
 219              $this->fulltext = '';
 220          }
 221  
 222          /**
 223           * Ensure any new items have compulsory fields set. This is needed for things like
 224           * frontend editing where we don't show all the fields or using some kind of API
 225           */
 226          if (!$this->id) {
 227              // Images can be an empty json string
 228              if (!isset($this->images)) {
 229                  $this->images = '{}';
 230              }
 231  
 232              // URLs can be an empty json string
 233              if (!isset($this->urls)) {
 234                  $this->urls = '{}';
 235              }
 236  
 237              // Attributes (article params) can be an empty json string
 238              if (!isset($this->attribs)) {
 239                  $this->attribs = '{}';
 240              }
 241  
 242              // Metadata can be an empty json string
 243              if (!isset($this->metadata)) {
 244                  $this->metadata = '{}';
 245              }
 246  
 247              // Hits must be zero on a new item
 248              $this->hits = 0;
 249          }
 250  
 251          // Set publish_up to null if not set
 252          if (!$this->publish_up) {
 253              $this->publish_up = null;
 254          }
 255  
 256          // Set publish_down to null if not set
 257          if (!$this->publish_down) {
 258              $this->publish_down = null;
 259          }
 260  
 261          // Check the publish down date is not earlier than publish up.
 262          if (!is_null($this->publish_up) && !is_null($this->publish_down) && $this->publish_down < $this->publish_up) {
 263              // Swap the dates.
 264              $temp = $this->publish_up;
 265              $this->publish_up = $this->publish_down;
 266              $this->publish_down = $temp;
 267          }
 268  
 269          // Clean up keywords -- eliminate extra spaces between phrases
 270          // and cr (\r) and lf (\n) characters from string
 271          if (!empty($this->metakey)) {
 272              // Only process if not empty
 273  
 274              // Array of characters to remove
 275              $badCharacters = ["\n", "\r", "\"", '<', '>'];
 276  
 277              // Remove bad characters
 278              $afterClean = StringHelper::str_ireplace($badCharacters, '', $this->metakey);
 279  
 280              // Create array using commas as delimiter
 281              $keys = explode(',', $afterClean);
 282  
 283              $cleanKeys = [];
 284  
 285              foreach ($keys as $key) {
 286                  if (trim($key)) {
 287                      // Ignore blank keywords
 288                      $cleanKeys[] = trim($key);
 289                  }
 290              }
 291  
 292              // Put array back together delimited by ", "
 293              $this->metakey = implode(', ', $cleanKeys);
 294          } else {
 295              $this->metakey = '';
 296          }
 297  
 298          if ($this->metadesc === null) {
 299              $this->metadesc = '';
 300          }
 301  
 302          return true;
 303      }
 304  
 305      /**
 306       * Overrides Table::store to set modified data and user id.
 307       *
 308       * @param   boolean  $updateNulls  True to update fields even if they are null.
 309       *
 310       * @return  boolean  True on success.
 311       *
 312       * @since   1.6
 313       */
 314      public function store($updateNulls = true)
 315      {
 316          $date = Factory::getDate()->toSql();
 317          $user = Factory::getUser();
 318  
 319          // Set created date if not set.
 320          if (!(int) $this->created) {
 321              $this->created = $date;
 322          }
 323  
 324          if ($this->id) {
 325              // Existing item
 326              $this->modified_by = $user->get('id');
 327              $this->modified    = $date;
 328          } else {
 329              // Field created_by can be set by the user, so we don't touch it if it's set.
 330              if (empty($this->created_by)) {
 331                  $this->created_by = $user->get('id');
 332              }
 333  
 334              // Set modified to created date if not set
 335              if (!(int) $this->modified) {
 336                  $this->modified = $this->created;
 337              }
 338  
 339              // Set modified_by to created_by user if not set
 340              if (empty($this->modified_by)) {
 341                  $this->modified_by = $this->created_by;
 342              }
 343          }
 344  
 345          // Verify that the alias is unique
 346          $table = Table::getInstance('Content', 'JTable', array('dbo' => $this->getDbo()));
 347  
 348          if ($table->load(array('alias' => $this->alias, 'catid' => $this->catid)) && ($table->id != $this->id || $this->id == 0)) {
 349              $this->setError(Text::_('JLIB_DATABASE_ERROR_ARTICLE_UNIQUE_ALIAS'));
 350  
 351              return false;
 352          }
 353  
 354          return parent::store($updateNulls);
 355      }
 356  
 357      /**
 358       * Get the type alias for UCM features
 359       *
 360       * @return  string  The alias as described above
 361       *
 362       * @since   4.0.0
 363       */
 364      public function getTypeAlias()
 365      {
 366          return $this->typeAlias;
 367      }
 368  }


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