[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/plugins/finder/categories/ -> categories.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Plugin
   5   * @subpackage  Finder.Categories
   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\Component\ComponentHelper;
  14  use Joomla\CMS\Table\Table;
  15  use Joomla\Component\Finder\Administrator\Indexer\Adapter;
  16  use Joomla\Component\Finder\Administrator\Indexer\Helper;
  17  use Joomla\Component\Finder\Administrator\Indexer\Indexer;
  18  use Joomla\Component\Finder\Administrator\Indexer\Result;
  19  use Joomla\Database\DatabaseQuery;
  20  use Joomla\Database\ParameterType;
  21  use Joomla\Registry\Registry;
  22  
  23  // phpcs:disable PSR1.Files.SideEffects
  24  \defined('_JEXEC') or die;
  25  // phpcs:enable PSR1.Files.SideEffects
  26  
  27  /**
  28   * Smart Search adapter for Joomla Categories.
  29   *
  30   * @since  2.5
  31   */
  32  class PlgFinderCategories extends Adapter
  33  {
  34      /**
  35       * The plugin identifier.
  36       *
  37       * @var    string
  38       * @since  2.5
  39       */
  40      protected $context = 'Categories';
  41  
  42      /**
  43       * The extension name.
  44       *
  45       * @var    string
  46       * @since  2.5
  47       */
  48      protected $extension = 'com_categories';
  49  
  50      /**
  51       * The sublayout to use when rendering the results.
  52       *
  53       * @var    string
  54       * @since  2.5
  55       */
  56      protected $layout = 'category';
  57  
  58      /**
  59       * The type of content that the adapter indexes.
  60       *
  61       * @var    string
  62       * @since  2.5
  63       */
  64      protected $type_title = 'Category';
  65  
  66      /**
  67       * The table name.
  68       *
  69       * @var    string
  70       * @since  2.5
  71       */
  72      protected $table = '#__categories';
  73  
  74      /**
  75       * The field the published state is stored in.
  76       *
  77       * @var    string
  78       * @since  2.5
  79       */
  80      protected $state_field = 'published';
  81  
  82      /**
  83       * Load the language file on instantiation.
  84       *
  85       * @var    boolean
  86       * @since  3.1
  87       */
  88      protected $autoloadLanguage = true;
  89  
  90      /**
  91       * Method to setup the indexer to be run.
  92       *
  93       * @return  boolean  True on success.
  94       *
  95       * @since   2.5
  96       */
  97      protected function setup()
  98      {
  99          return true;
 100      }
 101  
 102      /**
 103       * Method to remove the link information for items that have been deleted.
 104       *
 105       * @param   string  $context  The context of the action being performed.
 106       * @param   Table   $table    A Table object containing the record to be deleted
 107       *
 108       * @return  boolean  True on success.
 109       *
 110       * @since   2.5
 111       * @throws  Exception on database error.
 112       */
 113      public function onFinderDelete($context, $table)
 114      {
 115          if ($context === 'com_categories.category') {
 116              $id = $table->id;
 117          } elseif ($context === 'com_finder.index') {
 118              $id = $table->link_id;
 119          } else {
 120              return true;
 121          }
 122  
 123          // Remove item from the index.
 124          return $this->remove($id);
 125      }
 126  
 127      /**
 128       * Smart Search after save content method.
 129       * Reindexes the link information for a category that has been saved.
 130       * It also makes adjustments if the access level of the category has changed.
 131       *
 132       * @param   string   $context  The context of the category passed to the plugin.
 133       * @param   Table    $row      A Table object.
 134       * @param   boolean  $isNew    True if the category has just been created.
 135       *
 136       * @return  void
 137       *
 138       * @since   2.5
 139       * @throws  Exception on database error.
 140       */
 141      public function onFinderAfterSave($context, $row, $isNew): void
 142      {
 143          // We only want to handle categories here.
 144          if ($context === 'com_categories.category') {
 145              // Check if the access levels are different.
 146              if (!$isNew && $this->old_access != $row->access) {
 147                  // Process the change.
 148                  $this->itemAccessChange($row);
 149              }
 150  
 151              // Reindex the category item.
 152              $this->reindex($row->id);
 153  
 154              // Check if the parent access level is different.
 155              if (!$isNew && $this->old_cataccess != $row->access) {
 156                  $this->categoryAccessChange($row);
 157              }
 158          }
 159      }
 160  
 161      /**
 162       * Smart Search before content save method.
 163       * This event is fired before the data is actually saved.
 164       *
 165       * @param   string   $context  The context of the category passed to the plugin.
 166       * @param   Table    $row      A Table object.
 167       * @param   boolean  $isNew    True if the category is just about to be created.
 168       *
 169       * @return  boolean  True on success.
 170       *
 171       * @since   2.5
 172       * @throws  Exception on database error.
 173       */
 174      public function onFinderBeforeSave($context, $row, $isNew)
 175      {
 176          // We only want to handle categories here.
 177          if ($context === 'com_categories.category') {
 178              // Query the database for the old access level and the parent if the item isn't new.
 179              if (!$isNew) {
 180                  $this->checkItemAccess($row);
 181                  $this->checkCategoryAccess($row);
 182              }
 183          }
 184  
 185          return true;
 186      }
 187  
 188      /**
 189       * Method to update the link information for items that have been changed
 190       * from outside the edit screen. This is fired when the item is published,
 191       * unpublished, archived, or unarchived from the list view.
 192       *
 193       * @param   string   $context  The context for the category passed to the plugin.
 194       * @param   array    $pks      An array of primary key ids of the category that has changed state.
 195       * @param   integer  $value    The value of the state that the category has been changed to.
 196       *
 197       * @return  void
 198       *
 199       * @since   2.5
 200       */
 201      public function onFinderChangeState($context, $pks, $value)
 202      {
 203          // We only want to handle categories here.
 204          if ($context === 'com_categories.category') {
 205              /*
 206               * The category published state is tied to the parent category
 207               * published state so we need to look up all published states
 208               * before we change anything.
 209               */
 210              foreach ($pks as $pk) {
 211                  $pk    = (int) $pk;
 212                  $query = clone $this->getStateQuery();
 213  
 214                  $query->where($this->db->quoteName('a.id') . ' = :plgFinderCategoriesId')
 215                      ->bind(':plgFinderCategoriesId', $pk, ParameterType::INTEGER);
 216  
 217                  $this->db->setQuery($query);
 218                  $item = $this->db->loadObject();
 219  
 220                  // Translate the state.
 221                  $state = null;
 222  
 223                  if ($item->parent_id != 1) {
 224                      $state = $item->cat_state;
 225                  }
 226  
 227                  $temp = $this->translateState($value, $state);
 228  
 229                  // Update the item.
 230                  $this->change($pk, 'state', $temp);
 231  
 232                  // Reindex the item.
 233                  $this->reindex($pk);
 234              }
 235          }
 236  
 237          // Handle when the plugin is disabled.
 238          if ($context === 'com_plugins.plugin' && $value === 0) {
 239              $this->pluginDisable($pks);
 240          }
 241      }
 242  
 243      /**
 244       * Method to index an item. The item must be a Result object.
 245       *
 246       * @param   Result  $item  The item to index as a Result object.
 247       *
 248       * @return  void
 249       *
 250       * @since   2.5
 251       * @throws  Exception on database error.
 252       */
 253      protected function index(Result $item)
 254      {
 255          // Check if the extension is enabled.
 256          if (ComponentHelper::isEnabled($this->extension) === false) {
 257              return;
 258          }
 259  
 260          // Extract the extension element
 261          $parts = explode('.', $item->extension);
 262          $extension_element = $parts[0];
 263  
 264          // Check if the extension that owns the category is also enabled.
 265          if (ComponentHelper::isEnabled($extension_element) === false) {
 266              return;
 267          }
 268  
 269          $item->setLanguage();
 270  
 271          $extension = ucfirst(substr($extension_element, 4));
 272  
 273          // Initialize the item parameters.
 274          $item->params = new Registry($item->params);
 275  
 276          $item->metadata = new Registry($item->metadata);
 277  
 278          /*
 279           * Add the metadata processing instructions based on the category's
 280           * configuration parameters.
 281           */
 282  
 283          // Add the meta author.
 284          $item->metaauthor = $item->metadata->get('author');
 285  
 286          // Handle the link to the metadata.
 287          $item->addInstruction(Indexer::META_CONTEXT, 'link');
 288          $item->addInstruction(Indexer::META_CONTEXT, 'metakey');
 289          $item->addInstruction(Indexer::META_CONTEXT, 'metadesc');
 290          $item->addInstruction(Indexer::META_CONTEXT, 'metaauthor');
 291          $item->addInstruction(Indexer::META_CONTEXT, 'author');
 292  
 293          // Deactivated Methods
 294          // $item->addInstruction(Indexer::META_CONTEXT, 'created_by_alias');
 295  
 296          // Trigger the onContentPrepare event.
 297          $item->summary = Helper::prepareContent($item->summary, $item->params);
 298  
 299          // Create a URL as identifier to recognise items again.
 300          $item->url = $this->getUrl($item->id, $item->extension, $this->layout);
 301  
 302          /*
 303           * Build the necessary route information.
 304           * Need to import component route helpers dynamically, hence the reason it's handled here.
 305           */
 306          $class = $extension . 'HelperRoute';
 307  
 308          // Need to import component route helpers dynamically, hence the reason it's handled here.
 309          JLoader::register($class, JPATH_SITE . '/components/' . $extension_element . '/helpers/route.php');
 310  
 311          if (class_exists($class) && method_exists($class, 'getCategoryRoute')) {
 312              $item->route = $class::getCategoryRoute($item->id, $item->language);
 313          } else {
 314              $class = 'Joomla\\Component\\' . $extension . '\\Site\\Helper\\RouteHelper';
 315  
 316              if (class_exists($class) && method_exists($class, 'getCategoryRoute')) {
 317                  $item->route = $class::getCategoryRoute($item->id, $item->language);
 318              } else {
 319                  // This category has no frontend route.
 320                  return;
 321              }
 322          }
 323  
 324          // Get the menu title if it exists.
 325          $title = $this->getItemMenuTitle($item->url);
 326  
 327          // Adjust the title if necessary.
 328          if (!empty($title) && $this->params->get('use_menu_title', true)) {
 329              $item->title = $title;
 330          }
 331  
 332          // Translate the state. Categories should only be published if the parent category is published.
 333          $item->state = $this->translateState($item->state);
 334  
 335          // Add the type taxonomy data.
 336          $item->addTaxonomy('Type', 'Category');
 337  
 338          // Add the language taxonomy data.
 339          $item->addTaxonomy('Language', $item->language);
 340  
 341          // Get content extras.
 342          Helper::getContentExtras($item);
 343  
 344          // Index the item.
 345          $this->indexer->index($item);
 346      }
 347  
 348      /**
 349       * Method to get the SQL query used to retrieve the list of content items.
 350       *
 351       * @param   mixed  $query  A DatabaseQuery object or null.
 352       *
 353       * @return  DatabaseQuery  A database object.
 354       *
 355       * @since   2.5
 356       */
 357      protected function getListQuery($query = null)
 358      {
 359          $db = $this->db;
 360  
 361          // Check if we can use the supplied SQL query.
 362          $query = $query instanceof DatabaseQuery ? $query : $db->getQuery(true);
 363  
 364          $query->select(
 365              $db->quoteName(
 366                  [
 367                      'a.id',
 368                      'a.title',
 369                      'a.alias',
 370                      'a.extension',
 371                      'a.metakey',
 372                      'a.metadesc',
 373                      'a.metadata',
 374                      'a.language',
 375                      'a.lft',
 376                      'a.parent_id',
 377                      'a.level',
 378                      'a.access',
 379                      'a.params',
 380                  ]
 381              )
 382          )
 383              ->select(
 384                  $db->quoteName(
 385                      [
 386                          'a.description',
 387                          'a.created_user_id',
 388                          'a.modified_time',
 389                          'a.modified_user_id',
 390                          'a.created_time',
 391                          'a.published',
 392                      ],
 393                      [
 394                          'summary',
 395                          'created_by',
 396                          'modified',
 397                          'modified_by',
 398                          'start_date',
 399                          'state',
 400                      ]
 401                  )
 402              );
 403  
 404          // Handle the alias CASE WHEN portion of the query.
 405          $case_when_item_alias = ' CASE WHEN ';
 406          $case_when_item_alias .= $query->charLength($db->quoteName('a.alias'), '!=', '0');
 407          $case_when_item_alias .= ' THEN ';
 408          $a_id = $query->castAsChar($db->quoteName('a.id'));
 409          $case_when_item_alias .= $query->concatenate([$a_id, 'a.alias'], ':');
 410          $case_when_item_alias .= ' ELSE ';
 411          $case_when_item_alias .= $a_id . ' END AS slug';
 412  
 413          $query->select($case_when_item_alias)
 414              ->from($db->quoteName('#__categories', 'a'))
 415              ->where($db->quoteName('a.id') . ' > 1');
 416  
 417          return $query;
 418      }
 419  
 420      /**
 421       * Method to get a SQL query to load the published and access states for
 422       * a category and its parents.
 423       *
 424       * @return  DatabaseQuery  A database object.
 425       *
 426       * @since   2.5
 427       */
 428      protected function getStateQuery()
 429      {
 430          $query = $this->db->getQuery(true);
 431  
 432          $query->select(
 433              $this->db->quoteName(
 434                  [
 435                      'a.id',
 436                      'a.parent_id',
 437                      'a.access',
 438                  ]
 439              )
 440          )
 441              ->select(
 442                  $this->db->quoteName(
 443                      [
 444                          'a.' . $this->state_field,
 445                          'c.published',
 446                          'c.access',
 447                      ],
 448                      [
 449                          'state',
 450                          'cat_state',
 451                          'cat_access',
 452                      ]
 453                  )
 454              )
 455              ->from($this->db->quoteName('#__categories', 'a'))
 456              ->join(
 457                  'INNER',
 458                  $this->db->quoteName('#__categories', 'c'),
 459                  $this->db->quoteName('c.id') . ' = ' . $this->db->quoteName('a.parent_id')
 460              );
 461  
 462          return $query;
 463      }
 464  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer