[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @package Joomla.Plugin 5 * @subpackage Behaviour.taggable 6 * 7 * @copyright (C) 2016 Open Source Matters, Inc. <https://www.joomla.org> 8 * @license GNU General Public License version 2 or later; see LICENSE.txt 9 */ 10 11 namespace Joomla\Plugin\Behaviour\Taggable\Extension; 12 13 use Joomla\CMS\Event\Model\BeforeBatchEvent; 14 use Joomla\CMS\Event\Table\AfterLoadEvent; 15 use Joomla\CMS\Event\Table\AfterResetEvent; 16 use Joomla\CMS\Event\Table\AfterStoreEvent; 17 use Joomla\CMS\Event\Table\BeforeDeleteEvent; 18 use Joomla\CMS\Event\Table\BeforeStoreEvent; 19 use Joomla\CMS\Event\Table\ObjectCreateEvent; 20 use Joomla\CMS\Event\Table\SetNewTagsEvent; 21 use Joomla\CMS\Helper\TagsHelper; 22 use Joomla\CMS\Plugin\CMSPlugin; 23 use Joomla\CMS\Table\TableInterface; 24 use Joomla\CMS\Tag\TaggableTableInterface; 25 use Joomla\Event\SubscriberInterface; 26 use RuntimeException; 27 28 // phpcs:disable PSR1.Files.SideEffects 29 \defined('_JEXEC') or die; 30 // phpcs:enable PSR1.Files.SideEffects 31 32 /** 33 * Implements the Taggable behaviour which allows extensions to automatically support tags for their content items. 34 * 35 * This plugin supersedes JHelperObserverTags. 36 * 37 * @since 4.0.0 38 */ 39 final class Taggable extends CMSPlugin implements SubscriberInterface 40 { 41 /** 42 * Returns an array of events this subscriber will listen to. 43 * 44 * @return array 45 * 46 * @since 4.2.0 47 */ 48 public static function getSubscribedEvents(): array 49 { 50 return [ 51 'onTableObjectCreate' => 'onTableObjectCreate', 52 'onTableBeforeStore' => 'onTableBeforeStore', 53 'onTableAfterStore' => 'onTableAfterStore', 54 'onTableBeforeDelete' => 'onTableBeforeDelete', 55 'onTableSetNewTags' => 'onTableSetNewTags', 56 'onTableAfterReset' => 'onTableAfterReset', 57 'onTableAfterLoad' => 'onTableAfterLoad', 58 'onBeforeBatch' => 'onBeforeBatch', 59 ]; 60 } 61 62 /** 63 * Runs when a new table object is being created 64 * 65 * @param ObjectCreateEvent $event The event to handle 66 * 67 * @return void 68 * 69 * @since 4.0.0 70 */ 71 public function onTableObjectCreate(ObjectCreateEvent $event) 72 { 73 // Extract arguments 74 /** @var TableInterface $table */ 75 $table = $event['subject']; 76 77 // If the tags table doesn't implement the interface bail 78 if (!($table instanceof TaggableTableInterface)) { 79 return; 80 } 81 82 // If the table already has a tags helper we have nothing to do 83 if (!is_null($table->getTagsHelper())) { 84 return; 85 } 86 87 $tagsHelper = new TagsHelper(); 88 $tagsHelper->typeAlias = $table->typeAlias; 89 $table->setTagsHelper($tagsHelper); 90 91 // This is required because getTagIds overrides the tags property of the Tags Helper. 92 $cloneHelper = clone $table->getTagsHelper(); 93 $tagIds = $cloneHelper->getTagIds($table->getId(), $table->getTypeAlias()); 94 95 if (!empty($tagIds)) { 96 $table->getTagsHelper()->tags = explode(',', $tagIds); 97 } 98 } 99 100 /** 101 * Pre-processor for $table->store($updateNulls) 102 * 103 * @param BeforeStoreEvent $event The event to handle 104 * 105 * @return void 106 * 107 * @since 4.0.0 108 */ 109 public function onTableBeforeStore(BeforeStoreEvent $event) 110 { 111 // Extract arguments 112 /** @var TableInterface $table */ 113 $table = $event['subject']; 114 115 // If the tags table doesn't implement the interface bail 116 if (!($table instanceof TaggableTableInterface)) { 117 return; 118 } 119 120 // If the table doesn't have a tags helper we can't proceed 121 if (is_null($table->getTagsHelper())) { 122 return; 123 } 124 125 /** @var TagsHelper $tagsHelper */ 126 $tagsHelper = $table->getTagsHelper(); 127 $tagsHelper->typeAlias = $table->getTypeAlias(); 128 129 $newTags = $table->newTags ?? array(); 130 131 if (empty($newTags)) { 132 $tagsHelper->preStoreProcess($table); 133 } else { 134 $tagsHelper->preStoreProcess($table, (array) $newTags); 135 } 136 } 137 138 /** 139 * Post-processor for $table->store($updateNulls) 140 * 141 * @param AfterStoreEvent $event The event to handle 142 * 143 * @return void 144 * 145 * @since 4.0.0 146 */ 147 public function onTableAfterStore(AfterStoreEvent $event) 148 { 149 // Extract arguments 150 /** @var TableInterface $table */ 151 $table = $event['subject']; 152 $result = $event['result']; 153 154 if (!$result) { 155 return; 156 } 157 158 if (!is_object($table) || !($table instanceof TaggableTableInterface)) { 159 return; 160 } 161 162 // If the table doesn't have a tags helper we can't proceed 163 if (is_null($table->getTagsHelper())) { 164 return; 165 } 166 167 // Get the Tags helper and assign the parsed alias 168 /** @var TagsHelper $tagsHelper */ 169 $tagsHelper = $table->getTagsHelper(); 170 $tagsHelper->typeAlias = $table->getTypeAlias(); 171 172 $newTags = $table->newTags ?? array(); 173 174 if (empty($newTags)) { 175 $result = $tagsHelper->postStoreProcess($table); 176 } else { 177 if (is_string($newTags) && (strpos($newTags, ',') !== false)) { 178 $newTags = explode(',', $newTags); 179 } elseif (!is_array($newTags)) { 180 $newTags = (array) $newTags; 181 } 182 183 $result = $tagsHelper->postStoreProcess($table, $newTags); 184 } 185 } 186 187 /** 188 * Pre-processor for $table->delete($pk) 189 * 190 * @param BeforeDeleteEvent $event The event to handle 191 * 192 * @return void 193 * 194 * @since 4.0.0 195 */ 196 public function onTableBeforeDelete(BeforeDeleteEvent $event) 197 { 198 // Extract arguments 199 /** @var TableInterface $table */ 200 $table = $event['subject']; 201 $pk = $event['pk']; 202 203 // If the tags table doesn't implement the interface bail 204 if (!($table instanceof TaggableTableInterface)) { 205 return; 206 } 207 208 // If the table doesn't have a tags helper we can't proceed 209 if (is_null($table->getTagsHelper())) { 210 return; 211 } 212 213 // Get the Tags helper and assign the parsed alias 214 $table->getTagsHelper()->typeAlias = $table->getTypeAlias(); 215 216 $table->getTagsHelper()->deleteTagData($table, $pk); 217 } 218 219 /** 220 * Handles the tag setting in $table->batchTag($value, $pks, $contexts) 221 * 222 * @param SetNewTagsEvent $event The event to handle 223 * 224 * @return void 225 * 226 * @since 4.0.0 227 */ 228 public function onTableSetNewTags(SetNewTagsEvent $event) 229 { 230 // Extract arguments 231 /** @var TableInterface $table */ 232 $table = $event['subject']; 233 $newTags = $event['newTags']; 234 $replaceTags = $event['replaceTags']; 235 236 // If the tags table doesn't implement the interface bail 237 if (!($table instanceof TaggableTableInterface)) { 238 return; 239 } 240 241 // If the table doesn't have a tags helper we can't proceed 242 if (is_null($table->getTagsHelper())) { 243 return; 244 } 245 246 // Get the Tags helper and assign the parsed alias 247 /** @var TagsHelper $tagsHelper */ 248 $tagsHelper = $table->getTagsHelper(); 249 $tagsHelper->typeAlias = $table->getTypeAlias(); 250 251 if (!$tagsHelper->postStoreProcess($table, $newTags, $replaceTags)) { 252 throw new RuntimeException($table->getError()); 253 } 254 } 255 256 /** 257 * Runs when an existing table object is reset 258 * 259 * @param AfterResetEvent $event The event to handle 260 * 261 * @return void 262 * 263 * @since 4.0.0 264 */ 265 public function onTableAfterReset(AfterResetEvent $event) 266 { 267 // Extract arguments 268 /** @var TableInterface $table */ 269 $table = $event['subject']; 270 271 // If the tags table doesn't implement the interface bail 272 if (!($table instanceof TaggableTableInterface)) { 273 return; 274 } 275 276 // Parse the type alias 277 $tagsHelper = new TagsHelper(); 278 $tagsHelper->typeAlias = $table->getTypeAlias(); 279 $table->setTagsHelper($tagsHelper); 280 } 281 282 /** 283 * Runs when an existing table object has been loaded 284 * 285 * @param AfterLoadEvent $event The event to handle 286 * 287 * @return void 288 * 289 * @since 4.0.0 290 */ 291 public function onTableAfterLoad(AfterLoadEvent $event) 292 { 293 // Extract arguments 294 /** @var TableInterface $table */ 295 $table = $event['subject']; 296 297 // If the tags table doesn't implement the interface bail 298 if (!($table instanceof TaggableTableInterface)) { 299 return; 300 } 301 302 // If the table doesn't have a tags helper we can't proceed 303 if (is_null($table->getTagsHelper())) { 304 return; 305 } 306 307 // This is required because getTagIds overrides the tags property of the Tags Helper. 308 $cloneHelper = clone $table->getTagsHelper(); 309 $tagIds = $cloneHelper->getTagIds($table->getId(), $table->getTypeAlias()); 310 311 if (!empty($tagIds)) { 312 $table->getTagsHelper()->tags = explode(',', $tagIds); 313 } 314 } 315 316 /** 317 * Runs when an existing table object has been loaded 318 * 319 * @param BeforeBatchEvent $event The event to handle 320 * 321 * @return void 322 * 323 * @since 4.0.0 324 */ 325 public function onBeforeBatch(BeforeBatchEvent $event) 326 { 327 /** @var TableInterface $sourceTable */ 328 $sourceTable = $event['src']; 329 330 if (!($sourceTable instanceof TaggableTableInterface)) { 331 return; 332 } 333 334 if ($event['type'] === 'copy') { 335 $sourceTable->newTags = $sourceTable->getTagsHelper()->tags; 336 } else { 337 /** 338 * All other batch actions we don't want the tags to be modified so clear the helper - that way no actions 339 * will be performed on store 340 */ 341 $sourceTable->clearTagsHelper(); 342 } 343 } 344 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |