CoreFiles by mySites.guru
VersionableModelTrait.php146 lines, 3,867 bytes, from Joomla_6.0.0-beta2-Beta-Full_Package.zip
Raw History Compare with 6.0.0-beta1
md5
75df4a5fe476bcf72c052af12608e5b9
sha1
f3103a7c40fa1d600157e4c93d2857801a53ce2a
sha256
1f09231db15ca1da1d703f291078c75736a48e2149905a8ace84bc905a399953
<?php

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

namespace Joomla\CMS\Versioning;

use Joomla\CMS\Date\Date;
use Joomla\Utilities\ArrayHelper;

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

/**
 * Defines the trait for a Versionable Model Class.
 *
 * @since  3.10.0
 */
trait VersionableModelTrait
{
    /**
     * Method to get the item id from the version history table.
     *
     * @param   integer  $historyId  Key to the version history table.
     *
     * @return  integer  False on failure or error, id otherwise.
     *
     * @since   6.0.0
     */
    public function getItemIdFromHistory($historyId)
    {
        $rowArray = $this->getHistoryData($historyId);

        if (false === $rowArray) {
            return false;
        }

        $table = $this->getTable();
        $key   = $table->getKeyName();

        if (isset($rowArray[$key])) {
            return $rowArray[$key];
        }

        return false;
    }

    /**
     * Method to get the version data from the version history table.
     *
     * @param   integer  $historyId  Key to the version history table.
     *
     * @return  mixed    False on failure or error, data otherwise.
     *
     * @since   6.0.0
     */
    protected function getHistoryData($historyId)
    {
        // Get an instance of the row to checkout.
        $historyTable = new ContentHistory($this->getDatabase());

        if (!$historyTable->load($historyId)) {
            return false;
        }

        $rowArray = ArrayHelper::fromObject(json_decode($historyTable->version_data));

        return $rowArray;
    }

    /**
     * Method to get a version history table.
     *
     * @param   integer  $historyId  Key to the version history table.
     *
     * @return  mixed    False on failure or error, table otherwise.
     *
     * @since   6.0.0
     */
    protected function getHistoryTable($historyId)
    {
        if (empty($historyId)) {
            return false;
        }

        // Get an instance of the row to checkout.
        $historyTable = new ContentHistory($this->getDatabase());

        if (!$historyTable->load($historyId)) {
            return false;
        }

        return $historyTable;
    }

    /**
     * Method to load a row for editing from the version history table.
     *
     * @param   integer  $historyId  Key to the version history table.
     *
     * @return  boolean  False on failure or error, true otherwise.
     *
     * @since   6.0.0
     */
    public function loadHistory(int $historyId)
    {
        $rowArray = $this->getHistoryData($historyId);

        if (false === $rowArray) {
            return false;
        }

        $table = $this->getTable();

        // We set checked_out to the current user
        if ($table->hasField('checked_out')) {
            $rowArray[$table->getColumnAlias('checked_out')] = $this->getCurrentUser()->id;
        }

        if ($table->hasField('checked_out_time')) {
            $rowArray[$table->getColumnAlias('checked_out_time')] = (new Date())->toSql();
        }

        // Fix null ordering when restoring history
        if (\array_key_exists('ordering', $rowArray) && $rowArray['ordering'] === null) {
            $rowArray['ordering'] = 0;
        }

        [$extension, $type] = explode('.', $this->typeAlias);

        $app  = Factory::getApplication();
        $app->setUserState($extension . '.edit.' . $type . '.data', $rowArray);

        $historyTable = $this->getHistoryTable($historyId);

        $this->setState('save_date', $historyTable->save_date);
        $this->setState('version_note', $historyTable->version_note);

        return true;
    }
}

No other release has this exact content. See every version of this file.