CoreFiles by mySites.guru
CompareModel.php183 lines, 4,938 bytes, from Joomla_4.0.0-beta1-Beta-Full_Package.zip
Raw History Compare with 3.9.0-beta4
md5
f240c935720e94fb11143437374b0461
sha1
ab76a5f152668d929179df3e4a9d2a141d1ddae5
sha256
6f10a0b909f5431182c91fce7e51670f8e77e0b5757d9fe4ec4eef48a6034f04
<?php
/**
 * @package     Joomla.Administrator
 * @subpackage  com_contenthistory
 *
 * @copyright   Copyright (C) 2005 - 2020 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

namespace Joomla\Component\Contenthistory\Administrator\Model;

\defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Model\ListModel;
use Joomla\CMS\Table\ContentHistory;
use Joomla\CMS\Table\ContentType;
use Joomla\CMS\Table\Table;
use Joomla\Component\Contenthistory\Administrator\Helper\ContenthistoryHelper;

/**
 * Methods supporting a list of contenthistory records.
 *
 * @since  3.2
 */
class CompareModel extends ListModel
{
	/**
	 * Method to get a version history row.
	 *
	 * @return  array|boolean    On success, array of populated tables. False on failure.
	 *
	 * @since   3.2
	 */
	public function getItems()
	{
		$input = Factory::getApplication()->input;

		/** @var ContentHistory $table1 */
		$table1 = $this->getTable('ContentHistory');

		/** @var ContentHistory $table2 */
		$table2 = $this->getTable('ContentHistory');

		$id1 = $input->getInt('id1');
		$id2 = $input->getInt('id2');
		$result = array();

		if ($table1->load($id1) && $table2->load($id2))
		{
			// Get the first history record's content type record so we can check ACL
			/** @var ContentType $contentTypeTable */
			$contentTypeTable = $this->getTable('ContentType');
			$typeAlias        = explode('.', $table1->item_id);
			array_pop($typeAlias);
			$typeAlias        = implode('.', $typeAlias);

			if (!$contentTypeTable->load(array('type_alias' => $typeAlias)))
			{
				// Assume a failure to load the content type means broken data, abort mission
				return false;
			}

			$user = Factory::getUser();

			// Access check
			if ($user->authorise('core.edit', $table1->item_id) || $this->canEdit($table1))
			{
				$return = true;
			}
			else
			{
				$this->setError(Text::_('JERROR_ALERTNOAUTHOR'));

				return false;
			}

			// All's well, process the records
			if ($return == true)
			{
				$nullDate = $this->getDbo()->getNullDate();

				foreach (array($table1, $table2) as $table)
				{
					$object = new \stdClass;
					$object->data = ContenthistoryHelper::prepareData($table);
					$object->version_note = $table->version_note;

					// Let's use custom calendars when present
					$object->save_date = HTMLHelper::_('date', $table->save_date, Text::_('DATE_FORMAT_LC6'));

					$dateProperties = array (
						'modified_time',
						'created_time',
						'modified',
						'created',
						'checked_out_time',
						'publish_up',
						'publish_down',
					);

					foreach ($dateProperties as $dateProperty)
					{
						if (property_exists($object->data, $dateProperty)
							&& $object->data->$dateProperty->value !== null
							&& $object->data->$dateProperty->value !== $nullDate)
						{
							$object->data->$dateProperty->value = HTMLHelper::_(
								'date',
								$object->data->$dateProperty->value,
								Text::_('DATE_FORMAT_LC6')
							);
						}
					}

					$result[] = $object;
				}

				return $result;
			}
		}

		return false;
	}

	/**
	 * Method to get a table object, load it if necessary.
	 *
	 * @param   string  $type    The table name. Optional.
	 * @param   string  $prefix  The class prefix. Optional.
	 * @param   array   $config  Configuration array for model. Optional.
	 *
	 * @return  Table   A Table object
	 *
	 * @since   3.2
	 */
	public function getTable($type = 'Contenthistory', $prefix = 'Joomla\\CMS\\Table\\', $config = array())
	{
		return Table::getInstance($type, $prefix, $config);
	}

	/**
	 * Method to test whether a record is editable
	 *
	 * @param   ContentHistory  $record  A \JTable object.
	 *
	 * @return  boolean  True if allowed to edit the record. Defaults to the permission set in the component.
	 *
	 * @since   3.6
	 */
	protected function canEdit($record)
	{
		$result = false;

		if (!empty($record->item_id))
		{
			/**
			 * Make sure user has edit privileges for this content item. Note that we use edit permissions
			 * for the content item, not delete permissions for the content history row.
			 */
			$user   = Factory::getUser();
			$result = $user->authorise('core.edit', $record->item_id);

			// Finally try session (this catches edit.own case too)
			if (!$result)
			{
				/** @var ContentType $contentTypeTable */
				$contentTypeTable = $this->getTable('ContentType');

				$typeAlias        = explode('.', $record->item_id);
				$id = array_pop($typeAlias);
				$typeAlias        = implode('.', $typeAlias);
				$contentTypeTable->load(array('type_alias' => $typeAlias));
				$typeEditables = (array) Factory::getApplication()->getUserState(str_replace('.', '.edit.', $contentTypeTable->type_alias) . '.id');
				$result = in_array((int) $id, $typeEditables);
			}
		}

		return $result;
	}
}

The same content is in 2 releases, Joomla 4.0.0-beta to 4.0.0-beta2. See them all, or every version of this file.