[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/components/com_content/src/Model/ -> CategoriesModel.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Site
   5   * @subpackage  com_content
   6   *
   7   * @copyright   (C) 2009 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\Component\Content\Site\Model;
  12  
  13  use Joomla\CMS\Categories\Categories;
  14  use Joomla\CMS\Categories\CategoryNode;
  15  use Joomla\CMS\Factory;
  16  use Joomla\CMS\MVC\Model\ListModel;
  17  use Joomla\Registry\Registry;
  18  
  19  // phpcs:disable PSR1.Files.SideEffects
  20  \defined('_JEXEC') or die;
  21  // phpcs:enable PSR1.Files.SideEffects
  22  
  23  /**
  24   * This models supports retrieving lists of article categories.
  25   *
  26   * @since  1.6
  27   */
  28  class CategoriesModel extends ListModel
  29  {
  30      /**
  31       * Model context string.
  32       *
  33       * @var     string
  34       */
  35      public $_context = 'com_content.categories';
  36  
  37      /**
  38       * The category context (allows other extensions to derived from this model).
  39       *
  40       * @var     string
  41       */
  42      protected $_extension = 'com_content';
  43  
  44      /**
  45       * Parent category of the current one
  46       *
  47       * @var    CategoryNode|null
  48       */
  49      private $_parent = null;
  50  
  51      /**
  52       * Method to auto-populate the model state.
  53       *
  54       * Note. Calling getState in this method will result in recursion.
  55       *
  56       * @param   string  $ordering   The field to order on.
  57       * @param   string  $direction  The direction to order on.
  58       *
  59       * @return  void
  60       *
  61       * @since   1.6
  62       */
  63      protected function populateState($ordering = null, $direction = null)
  64      {
  65          $app = Factory::getApplication();
  66          $this->setState('filter.extension', $this->_extension);
  67  
  68          // Get the parent id if defined.
  69          $parentId = $app->input->getInt('id');
  70          $this->setState('filter.parentId', $parentId);
  71  
  72          $params = $app->getParams();
  73          $this->setState('params', $params);
  74  
  75          $this->setState('filter.published', 1);
  76          $this->setState('filter.access', true);
  77      }
  78  
  79      /**
  80       * Method to get a store id based on model configuration state.
  81       *
  82       * This is necessary because the model is used by the component and
  83       * different modules that might need different sets of data or different
  84       * ordering requirements.
  85       *
  86       * @param   string  $id  A prefix for the store id.
  87       *
  88       * @return  string  A store id.
  89       */
  90      protected function getStoreId($id = '')
  91      {
  92          // Compile the store id.
  93          $id .= ':' . $this->getState('filter.extension');
  94          $id .= ':' . $this->getState('filter.published');
  95          $id .= ':' . $this->getState('filter.access');
  96          $id .= ':' . $this->getState('filter.parentId');
  97  
  98          return parent::getStoreId($id);
  99      }
 100  
 101      /**
 102       * Redefine the function and add some properties to make the styling easier
 103       *
 104       * @param   bool  $recursive  True if you want to return children recursively.
 105       *
 106       * @return  mixed  An array of data items on success, false on failure.
 107       *
 108       * @since   1.6
 109       */
 110      public function getItems($recursive = false)
 111      {
 112          $store = $this->getStoreId();
 113  
 114          if (!isset($this->cache[$store])) {
 115              $app = Factory::getApplication();
 116              $menu = $app->getMenu();
 117              $active = $menu->getActive();
 118  
 119              if ($active) {
 120                  $params = $active->getParams();
 121              } else {
 122                  $params = new Registry();
 123              }
 124  
 125              $options = array();
 126              $options['countItems'] = $params->get('show_cat_num_articles_cat', 1) || !$params->get('show_empty_categories_cat', 0);
 127              $categories = Categories::getInstance('Content', $options);
 128              $this->_parent = $categories->get($this->getState('filter.parentId', 'root'));
 129  
 130              if (is_object($this->_parent)) {
 131                  $this->cache[$store] = $this->_parent->getChildren($recursive);
 132              } else {
 133                  $this->cache[$store] = false;
 134              }
 135          }
 136  
 137          return $this->cache[$store];
 138      }
 139  
 140      /**
 141       * Get the parent.
 142       *
 143       * @return  object  An array of data items on success, false on failure.
 144       *
 145       * @since   1.6
 146       */
 147      public function getParent()
 148      {
 149          if (!is_object($this->_parent)) {
 150              $this->getItems();
 151          }
 152  
 153          return $this->_parent;
 154      }
 155  }


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