CoreFiles by mySites.guru
versionable.php202 lines, 4,954 bytes, from Joomla_4.0.0-alpha10-Alpha-Full_Package.zip
Raw History Compare with 4.0.0-alpha9
md5
def3d94c379fd9dcdb402563e063f774
sha1
f41f3c520e9ea8d0eceac4a1fcf2ac41da4d7f97
sha256
28af93dc232713737f6d7da8d45fee1909dead4cfd09f3f7583eb70e5353b760
<?php
/**
 * @package     Joomla.Plugin
 * @subpackage  Taggable
 *
 * @copyright   Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Event as CmsEvent;
use Joomla\CMS\Helper\ContentHistoryHelper;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Table\TableInterface;
use Joomla\Event\DispatcherInterface;

/**
 * Implements the Taggable behaviour which allows extensions to automatically support tags for their content items.
 *
 * This plugin supersedes JTableObserverContenthistory.
 *
 * @since  4.0.0
 */
class PlgBehaviourVersionable extends CMSPlugin
{
	/**
	 * Constructor
	 *
	 * @param   DispatcherInterface  &$subject  The object to observe
	 * @param   array                $config    An optional associative array of configuration settings.
	 *                                          Recognized key values include 'name', 'group', 'params', 'language'
	 *                                          (this list is not meant to be comprehensive).
	 *
	 * @since  4.0.0
	 */
	public function __construct(&$subject, $config = array())
	{
		$this->allowLegacyListeners = false;

		parent::__construct($subject, $config);
	}

	/**
	 * Runs when a new table object is being created
	 *
	 * @param   CmsEvent\Table\ObjectCreateEvent  $event  The event to handle
	 *
	 * @return  void
	 *
	 * @since   4.0.0
	 */
	public function onTableObjectCreate(CmsEvent\Table\ObjectCreateEvent $event)
	{
		// Extract arguments
		/** @var JTableInterface $table */
		$table			= $event['subject'];

		// When we create the object the table is empty, so we can't parse the typeAlias field
		$typeAlias = $table->typeAlias;

		// If the table doesn't support UCM we can't use the Taggable behaviour
		if (is_null($typeAlias))
		{
			return;
		}

		// If the table already has a tags helper we have nothing to do
		if (property_exists($table, 'contenthistoryHelper'))
		{
			return;
		}

		$table->contenthistoryHelper = new ContentHistoryHelper($typeAlias);
		$table->contenthistoryHelper->typeAlias = $table->typeAlias;
	}

	/**
	 * Post-processor for $table->store($updateNulls)
	 *
	 * @param   CmsEvent\Table\AfterStoreEvent  $event  The event to handle
	 *
	 * @return  void
	 *
	 * @since   4.0.0
	 */
	public function onTableAfterStore(CmsEvent\Table\AfterStoreEvent $event)
	{
		// Extract arguments
		/** @var JTableInterface $table */
		$table	= $event['subject'];
		$result = $event['result'];

		if (!$result)
		{
			return;
		}

		if (!is_object($table) || !($table instanceof TableInterface))
		{
			return;
		}

		// Parse the type alias
		$typeAlias = $this->parseTypeAlias($table);

		// If the table doesn't support UCM we can't use the Versionable behaviour
		if (is_null($typeAlias))
		{
			return;
		}

		// If the table doesn't have a tags helper we can't proceed
		if (!property_exists($table, 'contenthistoryHelper'))
		{
			return;
		}

		// Get the Tags helper and assign the parsed alias
		$table->contenthistoryHelper->typeAlias = $typeAlias;

		$aliasParts = explode('.', $table->contenthistoryHelper->typeAlias);

		if ($aliasParts[0] && ComponentHelper::getParams($aliasParts[0])->get('save_history', 0))
		{
			$table->contenthistoryHelper->store($table);
		}
	}

	/**
	 * Pre-processor for $table->delete($pk)
	 *
	 * @param   CmsEvent\Table\BeforeDeleteEvent  $event  The event to handle
	 *
	 * @return  void
	 *
	 * @since   4.0.0
	 */
	public function onTableBeforeDelete(CmsEvent\Table\BeforeDeleteEvent $event)
	{
		// Extract arguments
		/** @var JTableInterface $table */
		$table			= $event['subject'];

		// Parse the type alias
		$typeAlias = $this->parseTypeAlias($table);

		// If the table doesn't support UCM we can't use the Taggable behaviour
		if (is_null($typeAlias))
		{
			return;
		}

		// If the table doesn't have a tags helper we can't proceed
		if (!property_exists($table, 'contenthistoryHelper'))
		{
			return;
		}

		$table->contenthistoryHelper->typeAlias = $typeAlias;
		$aliasParts = explode('.', $table->contenthistoryHelper->typeAlias);

		if ($aliasParts[0] && ComponentHelper::getParams($aliasParts[0])->get('save_history', 0))
		{
			$table->contenthistoryHelper->deleteHistory($table);
		}
	}

	/**
	 * Internal method
	 * Parses a TypeAlias of the form "{variableName}.type", replacing {variableName} with table-instance variables variableName
	 *
	 * @param   JTableInterface  &$table  The table
	 *
	 * @return  string
	 *
	 * @since   4.0.0
	 *
	 * @internal
	 */
	protected function parseTypeAlias(TableInterface &$table)
	{
		if (!isset($table->typeAlias))
		{
			return null;
		}

		if (empty($table->typeAlias))
		{
			return null;
		}

		return preg_replace_callback('/{([^}]+)}/',
			function($matches) use ($table)
			{
				return $table->{$matches[1]};
			},
			$table->typeAlias
		);
	}
}

The same content is in 4 releases, Joomla 4.0.0-alpha7 to 4.0.0-alpha10. See them all, or every version of this file.