- md5
- 2882d588b6cc1c6607bfbf2edc80cf5e
- sha1
- 67a88342f4512757edba15a046d0686d16c098a2
- sha256
- 4e2cf39ba0e0d4efb09f5cfcf8009c1e8f60ab48378a3df3a6350e453ed3816c
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
<?php
/**
* @package Joomla.Plugin
* @subpackage Taggable
*
* @copyright Copyright (C) 2005 - 2018 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\Plugin\CMSPlugin;
use Joomla\CMS\Table\TableInterface;
use Joomla\Event\DispatcherInterface;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Helper\ContentHistoryHelper;
use Joomla\CMS\Event as CmsEvent;
/**
* 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
);
}
}
No other release has this exact content. See every version of this file.