[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/components/com_newsfeeds/src/Model/ -> NewsfeedModel.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Site
   5   * @subpackage  com_newsfeeds
   6   *
   7   * @copyright   (C) 2006 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\Newsfeeds\Site\Model;
  12  
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Language\Text;
  15  use Joomla\CMS\MVC\Model\ItemModel;
  16  use Joomla\Database\ParameterType;
  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   * Newsfeeds Component Newsfeed Model
  25   *
  26   * @since  1.5
  27   */
  28  class NewsfeedModel extends ItemModel
  29  {
  30      /**
  31       * Model context string.
  32       *
  33       * @var     string
  34       * @since   1.6
  35       */
  36      protected $_context = 'com_newsfeeds.newsfeed';
  37  
  38      /**
  39       * Method to auto-populate the model state.
  40       *
  41       * Note. Calling getState in this method will result in recursion.
  42       *
  43       * @return  void
  44       *
  45       * @since   1.6
  46       */
  47      protected function populateState()
  48      {
  49          $app = Factory::getApplication();
  50  
  51          // Load state from the request.
  52          $pk = $app->input->getInt('id');
  53          $this->setState('newsfeed.id', $pk);
  54  
  55          $offset = $app->input->get('limitstart', 0, 'uint');
  56          $this->setState('list.offset', $offset);
  57  
  58          // Load the parameters.
  59          $params = $app->getParams();
  60          $this->setState('params', $params);
  61  
  62          $user = Factory::getUser();
  63  
  64          if ((!$user->authorise('core.edit.state', 'com_newsfeeds')) && (!$user->authorise('core.edit', 'com_newsfeeds'))) {
  65              $this->setState('filter.published', 1);
  66              $this->setState('filter.archived', 2);
  67          }
  68      }
  69  
  70      /**
  71       * Method to get newsfeed data.
  72       *
  73       * @param   integer  $pk  The id of the newsfeed.
  74       *
  75       * @return  mixed  Menu item data object on success, false on failure.
  76       *
  77       * @since   1.6
  78       */
  79      public function &getItem($pk = null)
  80      {
  81          $pk = (int) $pk ?: (int) $this->getState('newsfeed.id');
  82  
  83          if ($this->_item === null) {
  84              $this->_item = array();
  85          }
  86  
  87          if (!isset($this->_item[$pk])) {
  88              try {
  89                  $db = $this->getDatabase();
  90                  $query = $db->getQuery(true)
  91                      ->select(
  92                          [
  93                              $this->getState('item.select', $db->quoteName('a') . '.*'),
  94                              $db->quoteName('c.title', 'category_title'),
  95                              $db->quoteName('c.alias', 'category_alias'),
  96                              $db->quoteName('c.access', 'category_access'),
  97                              $db->quoteName('u.name', 'author'),
  98                              $db->quoteName('parent.title', 'parent_title'),
  99                              $db->quoteName('parent.id', 'parent_id'),
 100                              $db->quoteName('parent.path', 'parent_route'),
 101                              $db->quoteName('parent.alias', 'parent_alias'),
 102                          ]
 103                      )
 104                      ->from($db->quoteName('#__newsfeeds', 'a'))
 105                      ->join('LEFT', $db->quoteName('#__categories', 'c'), $db->quoteName('c.id') . ' = ' . $db->quoteName('a.catid'))
 106                      ->join('LEFT', $db->quoteName('#__users', 'u'), $db->quoteName('u.id') . ' = ' . $db->quoteName('a.created_by'))
 107                      ->join('LEFT', $db->quoteName('#__categories', 'parent'), $db->quoteName('parent.id') . ' = ' . $db->quoteName('c.parent_id'))
 108                      ->where($db->quoteName('a.id') . ' = :id')
 109                      ->bind(':id', $pk, ParameterType::INTEGER);
 110  
 111                  // Filter by published state.
 112                  $published = $this->getState('filter.published');
 113                  $archived  = $this->getState('filter.archived');
 114  
 115                  if (is_numeric($published)) {
 116                      // Filter by start and end dates.
 117                      $nowDate = Factory::getDate()->toSql();
 118  
 119                      $published = (int) $published;
 120                      $archived  = (int) $archived;
 121  
 122                      $query->extendWhere(
 123                          'AND',
 124                          [
 125                              $db->quoteName('a.published') . ' = :published1',
 126                              $db->quoteName('a.published') . ' = :archived1',
 127                          ],
 128                          'OR'
 129                      )
 130                          ->extendWhere(
 131                              'AND',
 132                              [
 133                                  $db->quoteName('a.publish_up') . ' IS NULL',
 134                                  $db->quoteName('a.publish_up') . ' <= :nowDate1',
 135                              ],
 136                              'OR'
 137                          )
 138                          ->extendWhere(
 139                              'AND',
 140                              [
 141                                  $db->quoteName('a.publish_down') . ' IS NULL',
 142                                  $db->quoteName('a.publish_down') . ' >= :nowDate2',
 143                              ],
 144                              'OR'
 145                          )
 146                          ->extendWhere(
 147                              'AND',
 148                              [
 149                                  $db->quoteName('c.published') . ' = :published2',
 150                                  $db->quoteName('c.published') . ' = :archived2',
 151                              ],
 152                              'OR'
 153                          )
 154                          ->bind([':published1', ':published2'], $published, ParameterType::INTEGER)
 155                          ->bind([':archived1', ':archived2'], $archived, ParameterType::INTEGER)
 156                          ->bind([':nowDate1', ':nowDate2'], $nowDate);
 157                  }
 158  
 159                  $db->setQuery($query);
 160  
 161                  $data = $db->loadObject();
 162  
 163                  if ($data === null) {
 164                      throw new \Exception(Text::_('COM_NEWSFEEDS_ERROR_FEED_NOT_FOUND'), 404);
 165                  }
 166  
 167                  // Check for published state if filter set.
 168  
 169                  if ((is_numeric($published) || is_numeric($archived)) && $data->published != $published && $data->published != $archived) {
 170                      throw new \Exception(Text::_('COM_NEWSFEEDS_ERROR_FEED_NOT_FOUND'), 404);
 171                  }
 172  
 173                  // Convert parameter fields to objects.
 174                  $registry = new Registry($data->params);
 175                  $data->params = clone $this->getState('params');
 176                  $data->params->merge($registry);
 177  
 178                  $data->metadata = new Registry($data->metadata);
 179  
 180                  // Compute access permissions.
 181  
 182                  if ($access = $this->getState('filter.access')) {
 183                      // If the access filter has been set, we already know this user can view.
 184                      $data->params->set('access-view', true);
 185                  } else {
 186                      // If no access filter is set, the layout takes some responsibility for display of limited information.
 187                      $user   = Factory::getUser();
 188                      $groups = $user->getAuthorisedViewLevels();
 189                      $data->params->set('access-view', in_array($data->access, $groups) && in_array($data->category_access, $groups));
 190                  }
 191  
 192                  $this->_item[$pk] = $data;
 193              } catch (\Exception $e) {
 194                  $this->setError($e);
 195                  $this->_item[$pk] = false;
 196              }
 197          }
 198  
 199          return $this->_item[$pk];
 200      }
 201  
 202      /**
 203       * Increment the hit counter for the newsfeed.
 204       *
 205       * @param   int  $pk  Optional primary key of the item to increment.
 206       *
 207       * @return  boolean  True if successful; false otherwise and internal error set.
 208       *
 209       * @since   3.0
 210       */
 211      public function hit($pk = 0)
 212      {
 213          $input = Factory::getApplication()->input;
 214          $hitcount = $input->getInt('hitcount', 1);
 215  
 216          if ($hitcount) {
 217              $pk = (!empty($pk)) ? $pk : (int) $this->getState('newsfeed.id');
 218  
 219              $table = $this->getTable('Newsfeed', 'Administrator');
 220              $table->hit($pk);
 221          }
 222  
 223          return true;
 224      }
 225  }


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