CoreFiles by mySites.guru
UCMBase.php139 lines, 3,252 bytes, from Joomla_5.2.2-Stable-Full_Package.zip
Raw History Compare with 5.2.1
md5
0e0649abcc6d885fd1a7357764b6a4d2
sha1
dd9c6ce9767ccb3ce96f5b5863972a312ddbcbea
sha256
04fd78ab4373a15cdde7aec55a612afb85e6450b93cd279575adf93749840400
<?php

/**
 * Joomla! Content Management System
 *
 * @copyright  (C) 2013 Open Source Matters, Inc. <https://www.joomla.org>
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 */

namespace Joomla\CMS\UCM;

use Joomla\CMS\Factory;
use Joomla\CMS\Helper\ContentHelper;
use Joomla\CMS\Table\Table;
use Joomla\CMS\Table\TableInterface;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects

/**
 * Base class for implementing UCM
 *
 * @since  3.1
 */
class UCMBase implements UCM
{
    /**
     * The UCM type object
     *
     * @var    UCMType
     * @since  3.1
     */
    protected $type;

    /**
     * The alias for the content table
     *
     * @var    string
     * @since  3.1
     */
    protected $alias;

    /**
     * Instantiate the UCMBase.
     *
     * @param   string    $alias  The alias string
     * @param   ?UCMType  $type   The type object
     *
     * @since   3.1
     */
    public function __construct($alias = null, ?UCMType $type = null)
    {
        // Setup dependencies.
        $input       = Factory::getApplication()->getInput();
        $this->alias = $alias ?: $input->get('option') . '.' . $input->get('view');

        $this->type = $type ?: $this->getType();
    }

    /**
     * Store data to the appropriate table
     *
     * @param   array            $data        Data to be stored
     * @param   ?TableInterface  $table       Table Object
     * @param   string           $primaryKey  The primary key name
     *
     * @return  boolean  True on success
     *
     * @since   3.1
     * @throws  \Exception
     */
    protected function store($data, ?TableInterface $table = null, $primaryKey = null)
    {
        if (!$table) {
            $table = Table::getInstance('Ucm');
        }

        $ucmId      = $data['ucm_id'] ?? null;
        $primaryKey = $primaryKey ?: $ucmId;

        if (isset($primaryKey)) {
            $table->load($primaryKey);
        }

        try {
            $table->bind($data);
        } catch (\RuntimeException $e) {
            throw new \Exception($e->getMessage(), 500, $e);
        }

        try {
            $table->store();
        } catch (\RuntimeException $e) {
            throw new \Exception($e->getMessage(), 500, $e);
        }

        return true;
    }

    /**
     * Get the UCM Content type.
     *
     * @return  UCMType  The UCM content type
     *
     * @since   3.1
     */
    public function getType()
    {
        if (!$this->type) {
            $this->type = new UCMType($this->alias);
        }

        return $this->type;
    }

    /**
     * Method to map the base ucm fields
     *
     * @param   array     $original  Data array
     * @param   ?UCMType  $type      UCM Content Type
     *
     * @return  array  Data array of UCM mappings
     *
     * @since   3.1
     */
    public function mapBase($original, ?UCMType $type = null)
    {
        $type = $type ?: $this->type;

        $data = [
            'ucm_type_id'     => $type->id,
            'ucm_item_id'     => $original[$type->primary_key],
            'ucm_language_id' => ContentHelper::getLanguageId($original['language']),
        ];

        return $data;
    }
}

The same content is in 37 releases, Joomla 5.2.0-beta1 to 5.4.0-alpha2. See them all, or every version of this file.