[ 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 Finder.Content 6 * 7 * @copyright (C) 2011 Open Source Matters, Inc. <https://www.joomla.org> 8 * @license GNU General Public License version 2 or later; see LICENSE.txt 9 10 * @phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace 11 */ 12 13 use Joomla\CMS\Categories\Categories; 14 use Joomla\CMS\Component\ComponentHelper; 15 use Joomla\CMS\Table\Table; 16 use Joomla\Component\Content\Site\Helper\RouteHelper; 17 use Joomla\Component\Finder\Administrator\Indexer\Adapter; 18 use Joomla\Component\Finder\Administrator\Indexer\Helper; 19 use Joomla\Component\Finder\Administrator\Indexer\Indexer; 20 use Joomla\Component\Finder\Administrator\Indexer\Result; 21 use Joomla\Database\DatabaseQuery; 22 use Joomla\Registry\Registry; 23 24 // phpcs:disable PSR1.Files.SideEffects 25 \defined('_JEXEC') or die; 26 // phpcs:enable PSR1.Files.SideEffects 27 28 /** 29 * Smart Search adapter for com_content. 30 * 31 * @since 2.5 32 */ 33 class PlgFinderContent extends Adapter 34 { 35 /** 36 * The plugin identifier. 37 * 38 * @var string 39 * @since 2.5 40 */ 41 protected $context = 'Content'; 42 43 /** 44 * The extension name. 45 * 46 * @var string 47 * @since 2.5 48 */ 49 protected $extension = 'com_content'; 50 51 /** 52 * The sublayout to use when rendering the results. 53 * 54 * @var string 55 * @since 2.5 56 */ 57 protected $layout = 'article'; 58 59 /** 60 * The type of content that the adapter indexes. 61 * 62 * @var string 63 * @since 2.5 64 */ 65 protected $type_title = 'Article'; 66 67 /** 68 * The table name. 69 * 70 * @var string 71 * @since 2.5 72 */ 73 protected $table = '#__content'; 74 75 /** 76 * Load the language file on instantiation. 77 * 78 * @var boolean 79 * @since 3.1 80 */ 81 protected $autoloadLanguage = true; 82 83 /** 84 * Method to setup the indexer to be run. 85 * 86 * @return boolean True on success. 87 * 88 * @since 2.5 89 */ 90 protected function setup() 91 { 92 return true; 93 } 94 95 /** 96 * Method to update the item link information when the item category is 97 * changed. This is fired when the item category is published or unpublished 98 * from the list view. 99 * 100 * @param string $extension The extension whose category has been updated. 101 * @param array $pks A list of primary key ids of the content that has changed state. 102 * @param integer $value The value of the state that the content has been changed to. 103 * 104 * @return void 105 * 106 * @since 2.5 107 */ 108 public function onFinderCategoryChangeState($extension, $pks, $value) 109 { 110 // Make sure we're handling com_content categories. 111 if ($extension === 'com_content') { 112 $this->categoryStateChange($pks, $value); 113 } 114 } 115 116 /** 117 * Method to remove the link information for items that have been deleted. 118 * 119 * @param string $context The context of the action being performed. 120 * @param Table $table A Table object containing the record to be deleted 121 * 122 * @return void 123 * 124 * @since 2.5 125 * @throws Exception on database error. 126 */ 127 public function onFinderAfterDelete($context, $table): void 128 { 129 if ($context === 'com_content.article') { 130 $id = $table->id; 131 } elseif ($context === 'com_finder.index') { 132 $id = $table->link_id; 133 } else { 134 return; 135 } 136 137 // Remove item from the index. 138 $this->remove($id); 139 } 140 141 /** 142 * Smart Search after save content method. 143 * Reindexes the link information for an article that has been saved. 144 * It also makes adjustments if the access level of an item or the 145 * category to which it belongs has changed. 146 * 147 * @param string $context The context of the content passed to the plugin. 148 * @param Table $row A Table object. 149 * @param boolean $isNew True if the content has just been created. 150 * 151 * @return void 152 * 153 * @since 2.5 154 * @throws Exception on database error. 155 */ 156 public function onFinderAfterSave($context, $row, $isNew): void 157 { 158 // We only want to handle articles here. 159 if ($context === 'com_content.article' || $context === 'com_content.form') { 160 // Check if the access levels are different. 161 if (!$isNew && $this->old_access != $row->access) { 162 // Process the change. 163 $this->itemAccessChange($row); 164 } 165 166 // Reindex the item. 167 $this->reindex($row->id); 168 } 169 170 // Check for access changes in the category. 171 if ($context === 'com_categories.category') { 172 // Check if the access levels are different. 173 if (!$isNew && $this->old_cataccess != $row->access) { 174 $this->categoryAccessChange($row); 175 } 176 } 177 } 178 179 /** 180 * Smart Search before content save method. 181 * This event is fired before the data is actually saved. 182 * 183 * @param string $context The context of the content passed to the plugin. 184 * @param Table $row A Table object. 185 * @param boolean $isNew If the content is just about to be created. 186 * 187 * @return boolean True on success. 188 * 189 * @since 2.5 190 * @throws Exception on database error. 191 */ 192 public function onFinderBeforeSave($context, $row, $isNew) 193 { 194 // We only want to handle articles here. 195 if ($context === 'com_content.article' || $context === 'com_content.form') { 196 // Query the database for the old access level if the item isn't new. 197 if (!$isNew) { 198 $this->checkItemAccess($row); 199 } 200 } 201 202 // Check for access levels from the category. 203 if ($context === 'com_categories.category') { 204 // Query the database for the old access level if the item isn't new. 205 if (!$isNew) { 206 $this->checkCategoryAccess($row); 207 } 208 } 209 210 return true; 211 } 212 213 /** 214 * Method to update the link information for items that have been changed 215 * from outside the edit screen. This is fired when the item is published, 216 * unpublished, archived, or unarchived from the list view. 217 * 218 * @param string $context The context for the content passed to the plugin. 219 * @param array $pks An array of primary key ids of the content that has changed state. 220 * @param integer $value The value of the state that the content has been changed to. 221 * 222 * @return void 223 * 224 * @since 2.5 225 */ 226 public function onFinderChangeState($context, $pks, $value) 227 { 228 // We only want to handle articles here. 229 if ($context === 'com_content.article' || $context === 'com_content.form') { 230 $this->itemStateChange($pks, $value); 231 } 232 233 // Handle when the plugin is disabled. 234 if ($context === 'com_plugins.plugin' && $value === 0) { 235 $this->pluginDisable($pks); 236 } 237 } 238 239 /** 240 * Method to index an item. The item must be a Result object. 241 * 242 * @param Result $item The item to index as a Result object. 243 * 244 * @return void 245 * 246 * @since 2.5 247 * @throws Exception on database error. 248 */ 249 protected function index(Result $item) 250 { 251 $item->setLanguage(); 252 253 // Check if the extension is enabled. 254 if (ComponentHelper::isEnabled($this->extension) === false) { 255 return; 256 } 257 258 $item->context = 'com_content.article'; 259 260 // Initialise the item parameters. 261 $registry = new Registry($item->params); 262 $item->params = clone ComponentHelper::getParams('com_content', true); 263 $item->params->merge($registry); 264 265 $item->metadata = new Registry($item->metadata); 266 267 // Trigger the onContentPrepare event. 268 $item->summary = Helper::prepareContent($item->summary, $item->params, $item); 269 $item->body = Helper::prepareContent($item->body, $item->params, $item); 270 271 // Create a URL as identifier to recognise items again. 272 $item->url = $this->getUrl($item->id, $this->extension, $this->layout); 273 274 // Build the necessary route and path information. 275 $item->route = RouteHelper::getArticleRoute($item->slug, $item->catid, $item->language); 276 277 // Get the menu title if it exists. 278 $title = $this->getItemMenuTitle($item->url); 279 280 // Adjust the title if necessary. 281 if (!empty($title) && $this->params->get('use_menu_title', true)) { 282 $item->title = $title; 283 } 284 285 $images = $item->images ? json_decode($item->images) : false; 286 287 // Add the image. 288 if ($images && !empty($images->image_intro)) { 289 $item->imageUrl = $images->image_intro; 290 $item->imageAlt = $images->image_intro_alt ?? ''; 291 } 292 293 // Add the meta author. 294 $item->metaauthor = $item->metadata->get('author'); 295 296 // Add the metadata processing instructions. 297 $item->addInstruction(Indexer::META_CONTEXT, 'metakey'); 298 $item->addInstruction(Indexer::META_CONTEXT, 'metadesc'); 299 $item->addInstruction(Indexer::META_CONTEXT, 'metaauthor'); 300 $item->addInstruction(Indexer::META_CONTEXT, 'author'); 301 $item->addInstruction(Indexer::META_CONTEXT, 'created_by_alias'); 302 303 // Translate the state. Articles should only be published if the category is published. 304 $item->state = $this->translateState($item->state, $item->cat_state); 305 306 // Add the type taxonomy data. 307 $item->addTaxonomy('Type', 'Article'); 308 309 // Add the author taxonomy data. 310 if (!empty($item->author) || !empty($item->created_by_alias)) { 311 $item->addTaxonomy('Author', !empty($item->created_by_alias) ? $item->created_by_alias : $item->author, $item->state); 312 } 313 314 // Add the category taxonomy data. 315 $categories = Categories::getInstance('com_content', ['published' => false, 'access' => false]); 316 $category = $categories->get($item->catid); 317 318 // Category does not exist, stop here 319 if (!$category) { 320 return; 321 } 322 323 $item->addNestedTaxonomy('Category', $category, $this->translateState($category->published), $category->access, $category->language); 324 325 // Add the language taxonomy data. 326 $item->addTaxonomy('Language', $item->language); 327 328 // Get content extras. 329 Helper::getContentExtras($item); 330 331 // Index the item. 332 $this->indexer->index($item); 333 } 334 335 /** 336 * Method to get the SQL query used to retrieve the list of content items. 337 * 338 * @param mixed $query A DatabaseQuery object or null. 339 * 340 * @return DatabaseQuery A database object. 341 * 342 * @since 2.5 343 */ 344 protected function getListQuery($query = null) 345 { 346 $db = $this->db; 347 348 // Check if we can use the supplied SQL query. 349 $query = $query instanceof DatabaseQuery ? $query : $db->getQuery(true) 350 ->select('a.id, a.title, a.alias, a.introtext AS summary, a.fulltext AS body') 351 ->select('a.images') 352 ->select('a.state, a.catid, a.created AS start_date, a.created_by') 353 ->select('a.created_by_alias, a.modified, a.modified_by, a.attribs AS params') 354 ->select('a.metakey, a.metadesc, a.metadata, a.language, a.access, a.version, a.ordering') 355 ->select('a.publish_up AS publish_start_date, a.publish_down AS publish_end_date') 356 ->select('c.title AS category, c.published AS cat_state, c.access AS cat_access'); 357 358 // Handle the alias CASE WHEN portion of the query 359 $case_when_item_alias = ' CASE WHEN '; 360 $case_when_item_alias .= $query->charLength('a.alias', '!=', '0'); 361 $case_when_item_alias .= ' THEN '; 362 $a_id = $query->castAsChar('a.id'); 363 $case_when_item_alias .= $query->concatenate(array($a_id, 'a.alias'), ':'); 364 $case_when_item_alias .= ' ELSE '; 365 $case_when_item_alias .= $a_id . ' END as slug'; 366 $query->select($case_when_item_alias); 367 368 $case_when_category_alias = ' CASE WHEN '; 369 $case_when_category_alias .= $query->charLength('c.alias', '!=', '0'); 370 $case_when_category_alias .= ' THEN '; 371 $c_id = $query->castAsChar('c.id'); 372 $case_when_category_alias .= $query->concatenate(array($c_id, 'c.alias'), ':'); 373 $case_when_category_alias .= ' ELSE '; 374 $case_when_category_alias .= $c_id . ' END as catslug'; 375 $query->select($case_when_category_alias) 376 377 ->select('u.name AS author') 378 ->from('#__content AS a') 379 ->join('LEFT', '#__categories AS c ON c.id = a.catid') 380 ->join('LEFT', '#__users AS u ON u.id = a.created_by'); 381 382 return $query; 383 } 384 }
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 |