[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/UCM/ -> UCMContent.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\UCM;
  11  
  12  use Joomla\CMS\Factory;
  13  use Joomla\CMS\Helper\ContentHelper;
  14  use Joomla\CMS\Table\Table;
  15  use Joomla\CMS\Table\TableInterface;
  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   * Base class for implementing UCM
  24   *
  25   * @since  3.1
  26   */
  27  class UCMContent extends UCMBase
  28  {
  29      /**
  30       * The related table object
  31       *
  32       * @var    Table
  33       * @since  3.1
  34       */
  35      protected $table;
  36  
  37      /**
  38       * The UCM data array
  39       *
  40       * @var    array
  41       * @since  3.1
  42       */
  43      public $ucmData;
  44  
  45      /**
  46       * Instantiate UCMContent.
  47       *
  48       * @param   TableInterface  $table  The table object
  49       * @param   string          $alias  The type alias
  50       * @param   UCMType         $type   The type object
  51       *
  52       * @since   3.1
  53       */
  54      public function __construct(TableInterface $table = null, $alias = null, UCMType $type = null)
  55      {
  56          parent::__construct($alias, $type);
  57  
  58          if ($table) {
  59              $this->table = $table;
  60          } else {
  61              $tableObject = json_decode($this->type->type->table);
  62              $this->table = Table::getInstance($tableObject->special->type, $tableObject->special->prefix, $tableObject->special->config);
  63          }
  64      }
  65  
  66      /**
  67       * Method to save the data
  68       *
  69       * @param   array    $original  The original data to be saved
  70       * @param   UCMType  $type      The UCM Type object
  71       *
  72       * @return  boolean  true
  73       *
  74       * @since   3.1
  75       */
  76      public function save($original = null, UCMType $type = null)
  77      {
  78          $type    = $type ?: $this->type;
  79          $ucmData = $original ? $this->mapData($original, $type) : $this->ucmData;
  80  
  81          // Store the Common fields
  82          $this->store($ucmData['common']);
  83  
  84          // Store the special fields
  85          if (isset($ucmData['special'])) {
  86              $table = $this->table;
  87              $this->store($ucmData['special'], $table, '');
  88          }
  89  
  90          return true;
  91      }
  92  
  93      /**
  94       * Delete content from the Core Content table
  95       *
  96       * @param   mixed    $pk    Array or comma-separated string of ids to delete
  97       * @param   UCMType  $type  The content type object
  98       *
  99       * @return  boolean  True if success
 100       *
 101       * @since   3.1
 102       */
 103      public function delete($pk, UCMType $type = null)
 104      {
 105          $db   = Factory::getDbo();
 106          $type = $type ?: $this->type;
 107  
 108          if (!\is_array($pk)) {
 109              $pk = explode(',', $pk);
 110          }
 111  
 112          $query = $db->getQuery(true)
 113              ->delete($db->quoteName('#__ucm_content'))
 114              ->where($db->quoteName('core_type_id') . ' = :typeId')
 115              ->whereIn($db->quoteName('core_content_item_id'), $pk)
 116              ->bind(':typeId', $type->type_id, ParameterType::INTEGER);
 117  
 118          $db->setQuery($query);
 119          $db->execute();
 120  
 121          return true;
 122      }
 123  
 124      /**
 125       * Map the original content to the Core Content fields
 126       *
 127       * @param   array    $original  The original data array
 128       * @param   UCMType  $type      Type object for this data
 129       *
 130       * @return  array  $ucmData  The mapped UCM data
 131       *
 132       * @since   3.1
 133       */
 134      public function mapData($original, UCMType $type = null)
 135      {
 136          $contentType = $type ?: $this->type;
 137  
 138          $fields = json_decode($contentType->type->field_mappings);
 139  
 140          $ucmData = array();
 141  
 142          $common = \is_object($fields->common) ? $fields->common : $fields->common[0];
 143  
 144          foreach ($common as $i => $field) {
 145              if ($field && $field !== 'null' && \array_key_exists($field, $original)) {
 146                  $ucmData['common'][$i] = $original[$field];
 147              }
 148          }
 149  
 150          if (\array_key_exists('special', $ucmData)) {
 151              $special = \is_object($fields->special) ? $fields->special : $fields->special[0];
 152  
 153              foreach ($special as $i => $field) {
 154                  if ($field && $field !== 'null' && \array_key_exists($field, $original)) {
 155                      $ucmData['special'][$i] = $original[$field];
 156                  }
 157              }
 158          }
 159  
 160          $ucmData['common']['core_type_alias'] = $contentType->type->type_alias;
 161          $ucmData['common']['core_type_id']    = $contentType->type->type_id;
 162  
 163          if (isset($ucmData['special'])) {
 164              $ucmData['special']['ucm_id'] = $ucmData['common']['ucm_id'];
 165          }
 166  
 167          $this->ucmData = $ucmData;
 168  
 169          return $this->ucmData;
 170      }
 171  
 172      /**
 173       * Store data to the appropriate table
 174       *
 175       * @param   array           $data        Data to be stored
 176       * @param   TableInterface  $table       Table Object
 177       * @param   boolean         $primaryKey  Flag that is true for data that are using #__ucm_content as their primary table
 178       *
 179       * @return  boolean  true on success
 180       *
 181       * @since   3.1
 182       */
 183      protected function store($data, TableInterface $table = null, $primaryKey = null)
 184      {
 185          $table = $table ?: Table::getInstance('Corecontent');
 186  
 187          $typeId     = $this->getType()->type->type_id;
 188          $primaryKey = $primaryKey ?: $this->getPrimaryKey($typeId, $data['core_content_item_id']);
 189  
 190          if (!$primaryKey) {
 191              // Store the core UCM mappings
 192              $baseData = array();
 193              $baseData['ucm_type_id']     = $typeId;
 194              $baseData['ucm_item_id']     = $data['core_content_item_id'];
 195              $baseData['ucm_language_id'] = ContentHelper::getLanguageId($data['core_language']);
 196  
 197              if (parent::store($baseData)) {
 198                  $primaryKey = $this->getPrimaryKey($typeId, $data['core_content_item_id']);
 199              }
 200          }
 201  
 202          return parent::store($data, $table, $primaryKey);
 203      }
 204  
 205      /**
 206       * Get the value of the primary key from #__ucm_base
 207       *
 208       * @param   integer  $typeId         The ID for the type
 209       * @param   integer  $contentItemId  Value of the primary key in the legacy or secondary table
 210       *
 211       * @return  integer  The integer of the primary key
 212       *
 213       * @since   3.1
 214       */
 215      public function getPrimaryKey($typeId, $contentItemId)
 216      {
 217          $db = Factory::getDbo();
 218          $query = $db->getQuery(true)
 219              ->select($db->quoteName('ucm_id'))
 220              ->from($db->quoteName('#__ucm_base'))
 221              ->where(
 222                  [
 223                      $db->quoteName('ucm_item_id') . ' = :itemId',
 224                      $db->quoteName('ucm_type_id') . ' = :typeId',
 225                  ]
 226              )
 227              ->bind(':itemId', $contentItemId, ParameterType::INTEGER)
 228              ->bind(':typeId', $typeId, ParameterType::INTEGER);
 229          $db->setQuery($query);
 230  
 231          return $db->loadResult();
 232      }
 233  }


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