[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Table/ -> ContentType.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\Language\Text;
  13  use Joomla\Database\DatabaseDriver;
  14  
  15  // phpcs:disable PSR1.Files.SideEffects
  16  \defined('JPATH_PLATFORM') or die;
  17  // phpcs:enable PSR1.Files.SideEffects
  18  
  19  /**
  20   * Tags table
  21   *
  22   * @since  3.1
  23   */
  24  class ContentType extends Table
  25  {
  26      /**
  27       * Constructor
  28       *
  29       * @param   DatabaseDriver  $db  A database connector object
  30       *
  31       * @since   3.1
  32       */
  33      public function __construct(DatabaseDriver $db)
  34      {
  35          parent::__construct('#__content_types', 'type_id', $db);
  36      }
  37  
  38      /**
  39       * Overloaded check method to ensure data integrity.
  40       *
  41       * @return  boolean  True on success.
  42       *
  43       * @since   3.1
  44       * @throws  \UnexpectedValueException
  45       */
  46      public function check()
  47      {
  48          try {
  49              parent::check();
  50          } catch (\Exception $e) {
  51              $this->setError($e->getMessage());
  52  
  53              return false;
  54          }
  55  
  56          // Check for valid name.
  57          if (trim($this->type_title) === '') {
  58              throw new \UnexpectedValueException(sprintf('The title is empty'));
  59          }
  60  
  61          $this->type_title = ucfirst($this->type_title);
  62  
  63          if (empty($this->type_alias)) {
  64              throw new \UnexpectedValueException(sprintf('The type_alias is empty'));
  65          }
  66  
  67          return true;
  68      }
  69  
  70      /**
  71       * Overridden Table::store.
  72       *
  73       * @param   boolean  $updateNulls  True to update fields even if they are null.
  74       *
  75       * @return  boolean  True on success.
  76       *
  77       * @since   3.1
  78       */
  79      public function store($updateNulls = false)
  80      {
  81          // Verify that the alias is unique
  82          $table = Table::getInstance('Contenttype', 'JTable', array('dbo' => $this->getDbo()));
  83  
  84          if ($table->load(array('type_alias' => $this->type_alias)) && ($table->type_id != $this->type_id || $this->type_id == 0)) {
  85              $this->setError(Text::_('COM_TAGS_ERROR_UNIQUE_ALIAS'));
  86  
  87              return false;
  88          }
  89  
  90          return parent::store($updateNulls);
  91      }
  92  
  93      /**
  94       * Method to expand the field mapping
  95       *
  96       * @param   boolean  $assoc  True to return an associative array.
  97       *
  98       * @return  mixed  Array or object with field mappings. Defaults to object.
  99       *
 100       * @since   3.1
 101       */
 102      public function fieldmapExpand($assoc = true)
 103      {
 104          return $this->fieldmap = json_decode($this->fieldmappings, $assoc);
 105      }
 106  
 107      /**
 108       * Method to get the id given the type alias
 109       *
 110       * @param   string  $typeAlias  Content type alias (for example, 'com_content.article').
 111       *
 112       * @return  mixed  type_id for this alias if successful, otherwise null.
 113       *
 114       * @since   3.2
 115       */
 116      public function getTypeId($typeAlias)
 117      {
 118          $db = $this->_db;
 119          $query = $db->getQuery(true);
 120          $query->select($db->quoteName('type_id'))
 121              ->from($db->quoteName($this->_tbl))
 122              ->where($db->quoteName('type_alias') . ' = :type_alias')
 123              ->bind(':type_alias', $typeAlias);
 124          $db->setQuery($query);
 125  
 126          return $db->loadResult();
 127      }
 128  
 129      /**
 130       * Method to get the Table object for the content type from the table object.
 131       *
 132       * @return  mixed  Table object on success, otherwise false.
 133       *
 134       * @since   3.2
 135       *
 136       * @throws  \RuntimeException
 137       */
 138      public function getContentTable()
 139      {
 140          $result = false;
 141          $tableInfo = json_decode($this->table);
 142  
 143          if (\is_object($tableInfo) && isset($tableInfo->special)) {
 144              if (\is_object($tableInfo->special) && isset($tableInfo->special->type) && isset($tableInfo->special->prefix)) {
 145                  $class = $tableInfo->special->class ?? 'Joomla\\CMS\\Table\\Table';
 146  
 147                  if (!class_implements($class, 'Joomla\\CMS\\Table\\TableInterface')) {
 148                      // This isn't an instance of TableInterface. Abort.
 149                      throw new \RuntimeException('Class must be an instance of Joomla\\CMS\\Table\\TableInterface');
 150                  }
 151  
 152                  $result = $class::getInstance($tableInfo->special->type, $tableInfo->special->prefix);
 153              }
 154          }
 155  
 156          return $result;
 157      }
 158  }


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