[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/UCM/ -> UCMType.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\Application\AbstractApplication;
  13  use Joomla\CMS\Factory;
  14  use Joomla\Database\DatabaseDriver;
  15  use Joomla\Database\ParameterType;
  16  
  17  // phpcs:disable PSR1.Files.SideEffects
  18  \defined('JPATH_PLATFORM') or die;
  19  // phpcs:enable PSR1.Files.SideEffects
  20  
  21  /**
  22   * UCM Class for handling content types
  23   *
  24   * @property-read  string  $core_content_id
  25   * @property-read  string  $core_type_alias
  26   * @property-read  string  $core_title
  27   * @property-read  string  $core_alias
  28   * @property-read  string  $core_body
  29   * @property-read  string  $core_state
  30   *
  31   * @property-read  string  $core_checked_out_time
  32   * @property-read  string  $core_checked_out_user_id
  33   * @property-read  string  $core_access
  34   * @property-read  string  $core_params
  35   * @property-read  string  $core_featured
  36   * @property-read  string  $core_metadata
  37   * @property-read  string  $core_created_user_id
  38   * @property-read  string  $core_created_by_alias
  39   * @property-read  string  $core_created_time
  40   * @property-read  string  $core_modified_user_id
  41   * @property-read  string  $core_modified_time
  42   * @property-read  string  $core_language
  43   * @property-read  string  $core_publish_up
  44   * @property-read  string  $core_publish_down
  45   * @property-read  string  $core_content_item_id
  46   * @property-read  string  $asset_id
  47   * @property-read  string  $core_images
  48   * @property-read  string  $core_urls
  49   * @property-read  string  $core_hits
  50   * @property-read  string  $core_version
  51   * @property-read  string  $core_ordering
  52   * @property-read  string  $core_metakey
  53   * @property-read  string  $core_metadesc
  54   * @property-read  string  $core_catid
  55   * @property-read  string  $core_typeid
  56   *
  57   * @since  3.1
  58   */
  59  class UCMType implements UCM
  60  {
  61      /**
  62       * The UCM Type
  63       *
  64       * @var    UCMType
  65       * @since  3.1
  66       */
  67      public $type;
  68  
  69      /**
  70       * The Database object
  71       *
  72       * @var    DatabaseDriver
  73       * @since  3.1
  74       */
  75      protected $db;
  76  
  77      /**
  78       * The alias for the content type
  79       *
  80       * @var    string
  81       * @since  3.1
  82       */
  83      protected $alias;
  84  
  85      /**
  86       * Class constructor
  87       *
  88       * @param   string               $alias        The alias for the item
  89       * @param   DatabaseDriver       $database     The database object
  90       * @param   AbstractApplication  $application  The application object
  91       *
  92       * @since   3.1
  93       */
  94      public function __construct($alias = null, DatabaseDriver $database = null, AbstractApplication $application = null)
  95      {
  96          $this->db = $database ?: Factory::getDbo();
  97          $app      = $application ?: Factory::getApplication();
  98  
  99          // Make the best guess we can in the absence of information.
 100          $this->alias = $alias ?: $app->input->get('option') . '.' . $app->input->get('view');
 101          $this->type  = $this->getTypeByAlias($this->alias);
 102      }
 103  
 104      /**
 105       * Get the Content Type
 106       *
 107       * @param   integer  $pk  The primary key of the alias type
 108       *
 109       * @return  object  The UCM Type data
 110       *
 111       * @since   3.1
 112       */
 113      public function getType($pk = null)
 114      {
 115          if (!$pk) {
 116              return $this->getTypeByAlias($this->alias);
 117          }
 118  
 119          $query = $this->db->getQuery(true)
 120              ->select($this->db->quoteName('ct') . '.*')
 121              ->from($this->db->quoteName('#__content_types', 'ct'))
 122              ->where($this->db->quoteName('ct.type_id') . ' = :pk')
 123              ->bind(':pk', $pk, ParameterType::INTEGER);
 124  
 125          $this->db->setQuery($query);
 126  
 127          return $this->db->loadObject();
 128      }
 129  
 130      /**
 131       * Get the Content Type from the alias
 132       *
 133       * @param   string  $typeAlias  The alias for the type
 134       *
 135       * @return  object  The UCM Type data
 136       *
 137       * @since   3.2
 138       */
 139      public function getTypeByAlias($typeAlias = null)
 140      {
 141          $query = $this->db->getQuery(true)
 142              ->select($this->db->quoteName('ct') . '.*')
 143              ->from($this->db->quoteName('#__content_types', 'ct'))
 144              ->where($this->db->quoteName('ct.type_alias') . ' = :alias')
 145              ->bind(':alias', $typeAlias);
 146  
 147          $this->db->setQuery($query);
 148  
 149          return $this->db->loadObject();
 150      }
 151  
 152      /**
 153       * Get the Content Type from the table class name
 154       *
 155       * @param   string  $tableName  The table for the type
 156       *
 157       * @return  mixed  The UCM Type data if found, false if no match is found
 158       *
 159       * @since   3.2
 160       */
 161      public function getTypeByTable($tableName)
 162      {
 163          $query = $this->db->getQuery(true)
 164              ->select($this->db->quoteName('ct') . '.*')
 165              ->from($this->db->quoteName('#__content_types', 'ct'));
 166  
 167          $this->db->setQuery($query);
 168          $types = $this->db->loadObjectList();
 169  
 170          foreach ($types as $type) {
 171              $tableFromType = json_decode($type->table);
 172              $tableNameFromType = $tableFromType->special->prefix . $tableFromType->special->type;
 173  
 174              if ($tableNameFromType === $tableName) {
 175                  return $type;
 176              }
 177          }
 178  
 179          return false;
 180      }
 181  
 182      /**
 183       * Retrieves the UCM type ID
 184       *
 185       * @param   string  $alias  The string of the type alias
 186       *
 187       * @return  mixed  The ID of the requested type or false if type is not found
 188       *
 189       * @since   3.1
 190       */
 191      public function getTypeId($alias = null)
 192      {
 193          if (!$alias) {
 194              $alias = $this->alias;
 195          }
 196  
 197          $query = $this->db->getQuery(true)
 198              ->select($this->db->quoteName('ct.type_id'))
 199              ->from($this->db->quoteName('#__content_types', 'ct'))
 200              ->where($this->db->quoteName('ct.type_alias') . ' = :alias')
 201              ->bind(':alias', $alias);
 202  
 203          $this->db->setQuery($query);
 204  
 205          $id = $this->db->loadResult();
 206  
 207          if (!$id) {
 208              return false;
 209          }
 210  
 211          return $id;
 212      }
 213  
 214      /**
 215       * Method to expand the field mapping
 216       *
 217       * @param   boolean  $assoc  True to return an associative array.
 218       *
 219       * @return  mixed  Array or object with field mappings. Defaults to object.
 220       *
 221       * @since   3.2
 222       */
 223      public function fieldmapExpand($assoc = false)
 224      {
 225          if (!empty($this->type->field_mappings)) {
 226              return $this->fieldmap = json_decode($this->type->field_mappings, $assoc);
 227          } else {
 228              return false;
 229          }
 230      }
 231  
 232      /**
 233       * Magic method to get the name of the field mapped to a ucm field (core_something).
 234       *
 235       * @param   string  $ucmField  The name of the field in JTableCorecontent
 236       *
 237       * @return  string  The name mapped to the $ucmField for a given content type
 238       *
 239       * @since   3.2
 240       */
 241      public function __get($ucmField)
 242      {
 243          if (!isset($this->fieldmap)) {
 244              $this->fieldmapExpand(false);
 245          }
 246  
 247          return $this->fieldmap->common->$ucmField ?? null;
 248      }
 249  }


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