- md5
- 1bfaac5d9275245f9014bfb5998e83ea
- sha1
- ac4b7e50dc3a3d18712a8bb369be0f7f961f3f57
- sha256
- 27103d6de1cf5d4f50dc0909dbac311fe690b086ac65d17cffc02ccd4d1c273d
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
<?php
/**
* @package Joomla.Plugin
* @subpackage Taggable
*
* @copyright Copyright (C) 2005 - 2020 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\Event as CmsEvent;
use Joomla\CMS\Helper\TagsHelper;
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 JHelperObserverTags.
*
* @since 4.0.0
*/
class PlgBehaviourTaggable 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 TableInterface $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 already has a tags helper we have nothing to do
if (property_exists($table, 'tagsHelper'))
{
return;
}
$table->tagsHelper = new TagsHelper;
$table->tagsHelper->typeAlias = $table->typeAlias;
// This is required because getTagIds overrides the tags property of the Tags Helper.
$cloneHelper = clone $table->tagsHelper;
$tagIds = $cloneHelper->getTagIds($table->getId(), $typeAlias);
if (!empty($tagIds))
{
$table->tagsHelper->tags = explode(',', $tagIds);
}
}
/**
* Pre-processor for $table->store($updateNulls)
*
* @param CmsEvent\Table\BeforeStoreEvent $event The event to handle
*
* @return void
*
* @since 4.0.0
*/
public function onTableBeforeStore(CmsEvent\Table\BeforeStoreEvent $event)
{
// Extract arguments
/** @var TableInterface $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, 'tagsHelper'))
{
return;
}
/** @var TagsHelper $tagsHelper */
$tagsHelper = $table->tagsHelper;
$tagsHelper->typeAlias = $typeAlias;
$newTags = $table->newTags ?? array();
if (empty($newTags))
{
$tagsHelper->preStoreProcess($table);
}
else
{
$tagsHelper->preStoreProcess($table, (array) $newTags);
}
}
/**
* 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 TableInterface $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 Taggable behaviour
if (is_null($typeAlias))
{
return;
}
// If the table doesn't have a tags helper we can't proceed
if (!property_exists($table, 'tagsHelper'))
{
return;
}
// Get the Tags helper and assign the parsed alias
/** @var TagsHelper $tagsHelper */
$tagsHelper = $table->tagsHelper;
$tagsHelper->typeAlias = $typeAlias;
$newTags = $table->newTags ?? array();
if (empty($newTags))
{
$result = $tagsHelper->postStoreProcess($table);
}
else
{
if (is_string($newTags) && (strpos($newTags, ',') !== false))
{
$newTags = explode(',', $newTags);
}
elseif (!is_array($newTags))
{
$newTags = (array) $newTags;
}
$result = $tagsHelper->postStoreProcess($table, $newTags);
}
}
/**
* 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 TableInterface $table */
$table = $event['subject'];
$pk = $event['pk'];
// 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, 'tagsHelper'))
{
return;
}
// Get the Tags helper and assign the parsed alias
$table->tagsHelper->typeAlias = $typeAlias;
$table->tagsHelper->deleteTagData($table, $pk);
}
/**
* Handles the tag setting in $table->batchTag($value, $pks, $contexts)
*
* @param CmsEvent\Table\SetNewTagsEvent $event The event to handle
*
* @return void
*
* @since 4.0.0
*/
public function onTableSetNewTags(CmsEvent\Table\SetNewTagsEvent $event)
{
// Extract arguments
/** @var TableInterface $table */
$table = $event['subject'];
$newTags = $event['newTags'];
$replaceTags = $event['replaceTags'];
// 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, 'tagsHelper'))
{
return;
}
// Get the Tags helper and assign the parsed alias
/** @var TagsHelper $tagsHelper */
$tagsHelper = $table->tagsHelper;
$tagsHelper->typeAlias = $typeAlias;
if (!$tagsHelper->postStoreProcess($table, $newTags, $replaceTags))
{
throw new RuntimeException($table->getError());
}
}
/**
* Runs when an existing table object is reset
*
* @param CmsEvent\Table\AfterResetEvent $event The event to handle
*
* @return void
*
* @since 4.0.0
*/
public function onTableAfterReset(CmsEvent\Table\AfterResetEvent $event)
{
// Extract arguments
/** @var TableInterface $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;
}
$table->tagsHelper = new TagsHelper;
$table->tagsHelper->typeAlias = $table->typeAlias;
}
/**
* Runs when an existing table object is reset
*
* @param CmsEvent\Table\AfterLoadEvent $event The event to handle
*
* @return void
*
* @since 4.0.0
*/
public function onTableAfterLoad(CmsEvent\Table\AfterLoadEvent $event)
{
// Extract arguments
/** @var TableInterface $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, 'tagsHelper'))
{
return;
}
// This is required because getTagIds overrides the tags property of the Tags Helper.
$cloneHelper = clone $table->tagsHelper;
$tagIds = $cloneHelper->getTagIds($table->getId(), $typeAlias);
if (!empty($tagIds))
{
$table->tagsHelper->tags = explode(',', $tagIds);
}
}
/**
* Internal method
* Parses a TypeAlias of the form "{variableName}.type", replacing {variableName} with table-instance variables variableName
*
* @param TableInterface $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 5 releases, Joomla 4.0.0-beta to 4.0.0-beta5. See them all, or every version of this file.