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