[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Table/ -> CoreContent.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\Table;
  11  
  12  use Joomla\CMS\Application\ApplicationHelper;
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Helper\ContentHelper;
  15  use Joomla\CMS\Language\Text;
  16  use Joomla\Database\DatabaseDriver;
  17  use Joomla\Database\ParameterType;
  18  use Joomla\String\StringHelper;
  19  
  20  // phpcs:disable PSR1.Files.SideEffects
  21  \defined('JPATH_PLATFORM') or die;
  22  // phpcs:enable PSR1.Files.SideEffects
  23  
  24  /**
  25   * Core content table
  26   *
  27   * @since  3.1
  28   */
  29  class CoreContent extends Table
  30  {
  31      /**
  32       * Indicates that columns fully support the NULL value in the database
  33       *
  34       * @var    boolean
  35       * @since  4.0.0
  36       */
  37      protected $_supportNullValue = true;
  38  
  39      /**
  40       * Encode necessary fields to JSON in the bind method
  41       *
  42       * @var    array
  43       * @since  4.0.0
  44       */
  45      protected $_jsonEncode = ['core_params', 'core_metadata', 'core_images', 'core_urls', 'core_body'];
  46  
  47      /**
  48       * Constructor
  49       *
  50       * @param   DatabaseDriver  $db  A database connector object
  51       *
  52       * @since   3.1
  53       */
  54      public function __construct(DatabaseDriver $db)
  55      {
  56          parent::__construct('#__ucm_content', 'core_content_id', $db);
  57  
  58          $this->setColumnAlias('published', 'core_state');
  59          $this->setColumnAlias('checked_out', 'core_checked_out_user_id');
  60          $this->setColumnAlias('checked_out_time', 'core_checked_out_time');
  61      }
  62  
  63      /**
  64       * Overloaded check function
  65       *
  66       * @return  boolean  True on success, false on failure
  67       *
  68       * @see     Table::check()
  69       * @since   3.1
  70       */
  71      public function check()
  72      {
  73          try {
  74              parent::check();
  75          } catch (\Exception $e) {
  76              $this->setError($e->getMessage());
  77  
  78              return false;
  79          }
  80  
  81          if (trim($this->core_title) === '') {
  82              $this->setError(Text::_('JLIB_CMS_WARNING_PROVIDE_VALID_NAME'));
  83  
  84              return false;
  85          }
  86  
  87          if (trim($this->core_alias) === '') {
  88              $this->core_alias = $this->core_title;
  89          }
  90  
  91          $this->core_alias = ApplicationHelper::stringURLSafe($this->core_alias);
  92  
  93          if (trim(str_replace('-', '', $this->core_alias)) === '') {
  94              $this->core_alias = Factory::getDate()->format('Y-m-d-H-i-s');
  95          }
  96  
  97          // Not Null sanity check
  98          if (empty($this->core_images)) {
  99              $this->core_images = '{}';
 100          }
 101  
 102          if (empty($this->core_urls)) {
 103              $this->core_urls = '{}';
 104          }
 105  
 106          // Check the publish down date is not earlier than publish up.
 107          if (
 108              $this->core_publish_up !== null
 109              && $this->core_publish_down !== null
 110              && $this->core_publish_down < $this->core_publish_up
 111              && $this->core_publish_down > $this->_db->getNullDate()
 112          ) {
 113              // Swap the dates.
 114              $temp = $this->core_publish_up;
 115              $this->core_publish_up = $this->core_publish_down;
 116              $this->core_publish_down = $temp;
 117          }
 118  
 119          // Clean up keywords -- eliminate extra spaces between phrases
 120          // and cr (\r) and lf (\n) characters from string
 121          if (!empty($this->core_metakey)) {
 122              // Only process if not empty
 123  
 124              // Array of characters to remove
 125              $bad_characters = array("\n", "\r", "\"", '<', '>');
 126  
 127              // Remove bad characters
 128              $after_clean = StringHelper::str_ireplace($bad_characters, '', $this->core_metakey);
 129  
 130              // Create array using commas as delimiter
 131              $keys = explode(',', $after_clean);
 132  
 133              $clean_keys = array();
 134  
 135              foreach ($keys as $key) {
 136                  if (trim($key)) {
 137                      // Ignore blank keywords
 138                      $clean_keys[] = trim($key);
 139                  }
 140              }
 141  
 142              // Put array back together delimited by ", "
 143              $this->core_metakey = implode(', ', $clean_keys);
 144          }
 145  
 146          return true;
 147      }
 148  
 149      /**
 150       * Override JTable delete method to include deleting corresponding row from #__ucm_base.
 151       *
 152       * @param   integer  $pk  primary key value to delete. Must be set or throws an exception.
 153       *
 154       * @return  boolean  True on success.
 155       *
 156       * @since   3.1
 157       * @throws  \UnexpectedValueException
 158       */
 159      public function delete($pk = null)
 160      {
 161          $baseTable = Table::getInstance('Ucm', 'JTable', array('dbo' => $this->getDbo()));
 162  
 163          return parent::delete($pk) && $baseTable->delete($pk);
 164      }
 165  
 166      /**
 167       * Method to delete a row from the #__ucm_content table by content_item_id.
 168       *
 169       * @param   integer  $contentItemId  value of the core_content_item_id to delete. Corresponds to the primary key of the content table.
 170       * @param   string   $typeAlias      Alias for the content type
 171       *
 172       * @return  boolean  True on success.
 173       *
 174       * @since   3.1
 175       * @throws  \UnexpectedValueException
 176       */
 177      public function deleteByContentId($contentItemId = null, $typeAlias = null)
 178      {
 179          $contentItemId = (int) $contentItemId;
 180  
 181          if ($contentItemId === 0) {
 182              throw new \UnexpectedValueException('Null content item key not allowed.');
 183          }
 184  
 185          if ($typeAlias === null) {
 186              throw new \UnexpectedValueException('Null type alias not allowed.');
 187          }
 188  
 189          $db = $this->getDbo();
 190          $query = $db->getQuery(true);
 191          $query->select($db->quoteName('core_content_id'))
 192              ->from($db->quoteName('#__ucm_content'))
 193              ->where(
 194                  [
 195                      $db->quoteName('core_content_item_id') . ' = :contentItemId',
 196                      $db->quoteName('core_type_alias') . ' = :typeAlias',
 197                  ]
 198              )
 199              ->bind(':contentItemId', $contentItemId, ParameterType::INTEGER)
 200              ->bind(':typeAlias', $typeAlias);
 201  
 202          $db->setQuery($query);
 203  
 204          if ($ucmId = $db->loadResult()) {
 205              return $this->delete($ucmId);
 206          } else {
 207              return true;
 208          }
 209      }
 210  
 211      /**
 212       * Overrides Table::store to set modified data and user id.
 213       *
 214       * @param   boolean  $updateNulls  True to update fields even if they are null.
 215       *
 216       * @return  boolean  True on success.
 217       *
 218       * @since   3.1
 219       */
 220      public function store($updateNulls = true)
 221      {
 222          $date = Factory::getDate();
 223          $user = Factory::getUser();
 224  
 225          if ($this->core_content_id) {
 226              // Existing item
 227              $this->core_modified_time = $date->toSql();
 228              $this->core_modified_user_id = $user->get('id');
 229              $isNew = false;
 230          } else {
 231              // New content item. A content item core_created_time and core_created_user_id field can be set by the user,
 232              // so we don't touch either of these if they are set.
 233              if (!(int) $this->core_created_time) {
 234                  $this->core_created_time = $date->toSql();
 235              }
 236  
 237              if (empty($this->core_created_user_id)) {
 238                  $this->core_created_user_id = $user->get('id');
 239              }
 240  
 241              if (!(int) $this->core_modified_time) {
 242                  $this->core_modified_time = $this->core_created_time;
 243              }
 244  
 245              if (empty($this->core_modified_user_id)) {
 246                  $this->core_modified_user_id = $this->core_created_user_id;
 247              }
 248  
 249              $isNew = true;
 250          }
 251  
 252          $oldRules = $this->getRules();
 253  
 254          if (empty($oldRules)) {
 255              $this->setRules('{}');
 256          }
 257  
 258          $result = parent::store($updateNulls);
 259  
 260          return $result && $this->storeUcmBase($updateNulls, $isNew);
 261      }
 262  
 263      /**
 264       * Insert or update row in ucm_base table
 265       *
 266       * @param   boolean  $updateNulls  True to update fields even if they are null.
 267       * @param   boolean  $isNew        if true, need to insert. Otherwise update.
 268       *
 269       * @return  boolean  True on success.
 270       *
 271       * @since   3.1
 272       */
 273      protected function storeUcmBase($updateNulls = true, $isNew = false)
 274      {
 275          // Store the ucm_base row
 276          $db         = $this->getDbo();
 277          $query      = $db->getQuery(true);
 278          $languageId = ContentHelper::getLanguageId($this->core_language);
 279  
 280          // Selecting "all languages" doesn't give a language id - we can't store a blank string in non mysql databases, so save 0 (the default value)
 281          if (!$languageId) {
 282              $languageId = 0;
 283          }
 284  
 285          if ($isNew) {
 286              $query->insert($db->quoteName('#__ucm_base'))
 287                  ->columns(
 288                      [
 289                          $db->quoteName('ucm_id'),
 290                          $db->quoteName('ucm_item_id'),
 291                          $db->quoteName('ucm_type_id'),
 292                          $db->quoteName('ucm_language_id'),
 293                      ]
 294                  )
 295                  ->values(
 296                      implode(
 297                          ',',
 298                          $query->bindArray(
 299                              [
 300                                  $this->core_content_id,
 301                                  $this->core_content_item_id,
 302                                  $this->core_type_id,
 303                                  $languageId,
 304                              ]
 305                          )
 306                      )
 307                  );
 308          } else {
 309              $query->update($db->quoteName('#__ucm_base'))
 310                  ->set(
 311                      [
 312                          $db->quoteName('ucm_item_id') . ' = :coreContentItemId',
 313                          $db->quoteName('ucm_type_id') . ' = :typeId',
 314                          $db->quoteName('ucm_language_id') . ' = :languageId',
 315                      ]
 316                  )
 317                  ->where($db->quoteName('ucm_id') . ' = :coreContentId')
 318                  ->bind(':coreContentItemId', $this->core_content_item_id, ParameterType::INTEGER)
 319                  ->bind(':typeId', $this->core_type_id, ParameterType::INTEGER)
 320                  ->bind(':languageId', $languageId, ParameterType::INTEGER)
 321                  ->bind(':coreContentId', $this->core_content_id, ParameterType::INTEGER);
 322          }
 323  
 324          $db->setQuery($query);
 325  
 326          return $db->execute();
 327      }
 328  }


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