[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Site
   5   * @subpackage  com_contact
   6   *
   7   * @copyright   (C) 2008 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\Contact\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 contact 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_contact.categories';
  36  
  37      /**
  38       * The category context (allows other extensions to derived from this model).
  39       *
  40       * @var     string
  41       */
  42      protected $_extension = 'com_contact';
  43  
  44      /**
  45       * Parent category of the current one
  46       *
  47       * @var    CategoryNode|null
  48       */
  49      private $_parent = null;
  50  
  51      /**
  52       * Array of child-categories
  53       *
  54       * @var    CategoryNode[]|null
  55       */
  56      private $_items = null;
  57  
  58      /**
  59       * Method to auto-populate the model state.
  60       *
  61       * Note. Calling getState in this method will result in recursion.
  62       *
  63       * @param   string  $ordering   An optional ordering field.
  64       * @param   string  $direction  An optional direction (asc|desc).
  65       *
  66       * @return  void
  67       *
  68       * @since   1.6
  69       */
  70      protected function populateState($ordering = null, $direction = null)
  71      {
  72          $app = Factory::getApplication();
  73          $this->setState('filter.extension', $this->_extension);
  74  
  75          // Get the parent id if defined.
  76          $parentId = $app->input->getInt('id');
  77          $this->setState('filter.parentId', $parentId);
  78  
  79          $params = $app->getParams();
  80          $this->setState('params', $params);
  81  
  82          $this->setState('filter.published', 1);
  83          $this->setState('filter.access', true);
  84      }
  85  
  86      /**
  87       * Method to get a store id based on model configuration state.
  88       *
  89       * This is necessary because the model is used by the component and
  90       * different modules that might need different sets of data or different
  91       * ordering requirements.
  92       *
  93       * @param   string  $id  A prefix for the store id.
  94       *
  95       * @return  string  A store id.
  96       */
  97      protected function getStoreId($id = '')
  98      {
  99          // Compile the store id.
 100          $id .= ':' . $this->getState('filter.extension');
 101          $id .= ':' . $this->getState('filter.published');
 102          $id .= ':' . $this->getState('filter.access');
 103          $id .= ':' . $this->getState('filter.parentId');
 104  
 105          return parent::getStoreId($id);
 106      }
 107  
 108      /**
 109       * Redefine the function and add some properties to make the styling easier
 110       *
 111       * @return  mixed  An array of data items on success, false on failure.
 112       */
 113      public function getItems()
 114      {
 115          if ($this->_items === null) {
 116              $app = Factory::getApplication();
 117              $menu = $app->getMenu();
 118              $active = $menu->getActive();
 119  
 120              if ($active) {
 121                  $params = $active->getParams();
 122              } else {
 123                  $params = new Registry();
 124              }
 125  
 126              $options = array();
 127              $options['countItems'] = $params->get('show_cat_items_cat', 1) || !$params->get('show_empty_categories_cat', 0);
 128              $categories = Categories::getInstance('Contact', $options);
 129              $this->_parent = $categories->get($this->getState('filter.parentId', 'root'));
 130  
 131              if (is_object($this->_parent)) {
 132                  $this->_items = $this->_parent->getChildren();
 133              } else {
 134                  $this->_items = false;
 135              }
 136          }
 137  
 138          return $this->_items;
 139      }
 140  
 141      /**
 142       * Gets the id of the parent category for the selected list of categories
 143       *
 144       * @return   integer  The id of the parent category
 145       *
 146       * @since    1.6.0
 147       */
 148      public function getParent()
 149      {
 150          if (!is_object($this->_parent)) {
 151              $this->getItems();
 152          }
 153  
 154          return $this->_parent;
 155      }
 156  }


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