[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/components/com_content/src/Controller/ -> ArticleController.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\Controller;
  12  
  13  use Joomla\CMS\Application\SiteApplication;
  14  use Joomla\CMS\Language\Multilanguage;
  15  use Joomla\CMS\Language\Text;
  16  use Joomla\CMS\MVC\Controller\FormController;
  17  use Joomla\CMS\Router\Route;
  18  use Joomla\CMS\Uri\Uri;
  19  use Joomla\CMS\Versioning\VersionableControllerTrait;
  20  use Joomla\Utilities\ArrayHelper;
  21  
  22  // phpcs:disable PSR1.Files.SideEffects
  23  \defined('_JEXEC') or die;
  24  // phpcs:enable PSR1.Files.SideEffects
  25  
  26  /**
  27   * Content article class.
  28   *
  29   * @since  1.6.0
  30   */
  31  class ArticleController extends FormController
  32  {
  33      use VersionableControllerTrait;
  34  
  35      /**
  36       * The URL view item variable.
  37       *
  38       * @var    string
  39       * @since  1.6
  40       */
  41      protected $view_item = 'form';
  42  
  43      /**
  44       * The URL view list variable.
  45       *
  46       * @var    string
  47       * @since  1.6
  48       */
  49      protected $view_list = 'categories';
  50  
  51      /**
  52       * The URL edit variable.
  53       *
  54       * @var    string
  55       * @since  3.2
  56       */
  57      protected $urlVar = 'a.id';
  58  
  59      /**
  60       * Method to add a new record.
  61       *
  62       * @return  mixed  True if the record can be added, an error object if not.
  63       *
  64       * @since   1.6
  65       */
  66      public function add()
  67      {
  68          if (!parent::add()) {
  69              // Redirect to the return page.
  70              $this->setRedirect($this->getReturnPage());
  71  
  72              return;
  73          }
  74  
  75          // Redirect to the edit screen.
  76          $this->setRedirect(
  77              Route::_(
  78                  'index.php?option=' . $this->option . '&view=' . $this->view_item . '&a_id=0'
  79                  . $this->getRedirectToItemAppend(),
  80                  false
  81              )
  82          );
  83  
  84          return true;
  85      }
  86  
  87      /**
  88       * Method override to check if you can add a new record.
  89       *
  90       * @param   array  $data  An array of input data.
  91       *
  92       * @return  boolean
  93       *
  94       * @since   1.6
  95       */
  96      protected function allowAdd($data = array())
  97      {
  98          $user       = $this->app->getIdentity();
  99          $categoryId = ArrayHelper::getValue($data, 'catid', $this->input->getInt('catid'), 'int');
 100          $allow      = null;
 101  
 102          if ($categoryId) {
 103              // If the category has been passed in the data or URL check it.
 104              $allow = $user->authorise('core.create', 'com_content.category.' . $categoryId);
 105          }
 106  
 107          if ($allow === null) {
 108              // In the absence of better information, revert to the component permissions.
 109              return parent::allowAdd();
 110          } else {
 111              return $allow;
 112          }
 113      }
 114  
 115      /**
 116       * Method override to check if you can edit an existing record.
 117       *
 118       * @param   array   $data  An array of input data.
 119       * @param   string  $key   The name of the key for the primary key; default is id.
 120       *
 121       * @return  boolean
 122       *
 123       * @since   1.6
 124       */
 125      protected function allowEdit($data = array(), $key = 'id')
 126      {
 127          $recordId = (int) isset($data[$key]) ? $data[$key] : 0;
 128          $user = $this->app->getIdentity();
 129  
 130          // Zero record (id:0), return component edit permission by calling parent controller method
 131          if (!$recordId) {
 132              return parent::allowEdit($data, $key);
 133          }
 134  
 135          // Check edit on the record asset (explicit or inherited)
 136          if ($user->authorise('core.edit', 'com_content.article.' . $recordId)) {
 137              return true;
 138          }
 139  
 140          // Check edit own on the record asset (explicit or inherited)
 141          if ($user->authorise('core.edit.own', 'com_content.article.' . $recordId)) {
 142              // Existing record already has an owner, get it
 143              $record = $this->getModel()->getItem($recordId);
 144  
 145              if (empty($record)) {
 146                  return false;
 147              }
 148  
 149              // Grant if current user is owner of the record
 150              return $user->get('id') == $record->created_by;
 151          }
 152  
 153          return false;
 154      }
 155  
 156      /**
 157       * Method to cancel an edit.
 158       *
 159       * @param   string  $key  The name of the primary key of the URL variable.
 160       *
 161       * @return  boolean  True if access level checks pass, false otherwise.
 162       *
 163       * @since   1.6
 164       */
 165      public function cancel($key = 'a_id')
 166      {
 167          $result = parent::cancel($key);
 168  
 169          /** @var SiteApplication $app */
 170          $app = $this->app;
 171  
 172          // Load the parameters.
 173          $params = $app->getParams();
 174  
 175          $customCancelRedir = (bool) $params->get('custom_cancel_redirect');
 176  
 177          if ($customCancelRedir) {
 178              $cancelMenuitemId = (int) $params->get('cancel_redirect_menuitem');
 179  
 180              if ($cancelMenuitemId > 0) {
 181                  $item = $app->getMenu()->getItem($cancelMenuitemId);
 182                  $lang = '';
 183  
 184                  if (Multilanguage::isEnabled()) {
 185                      $lang = !is_null($item) && $item->language != '*' ? '&lang=' . $item->language : '';
 186                  }
 187  
 188                  // Redirect to the user specified return page.
 189                  $redirlink = $item->link . $lang . '&Itemid=' . $cancelMenuitemId;
 190              } else {
 191                  // Redirect to the same article submission form (clean form).
 192                  $redirlink = $app->getMenu()->getActive()->link . '&Itemid=' . $app->getMenu()->getActive()->id;
 193              }
 194          } else {
 195              $menuitemId = (int) $params->get('redirect_menuitem');
 196  
 197              if ($menuitemId > 0) {
 198                  $lang = '';
 199                  $item = $app->getMenu()->getItem($menuitemId);
 200  
 201                  if (Multilanguage::isEnabled()) {
 202                      $lang = !is_null($item) && $item->language != '*' ? '&lang=' . $item->language : '';
 203                  }
 204  
 205                  // Redirect to the general (redirect_menuitem) user specified return page.
 206                  $redirlink = $item->link . $lang . '&Itemid=' . $menuitemId;
 207              } else {
 208                  // Redirect to the return page.
 209                  $redirlink = $this->getReturnPage();
 210              }
 211          }
 212  
 213          $this->setRedirect(Route::_($redirlink, false));
 214  
 215          return $result;
 216      }
 217  
 218      /**
 219       * Method to edit an existing record.
 220       *
 221       * @param   string  $key     The name of the primary key of the URL variable.
 222       * @param   string  $urlVar  The name of the URL variable if different from the primary key
 223       * (sometimes required to avoid router collisions).
 224       *
 225       * @return  boolean  True if access level check and checkout passes, false otherwise.
 226       *
 227       * @since   1.6
 228       */
 229      public function edit($key = null, $urlVar = 'a_id')
 230      {
 231          $result = parent::edit($key, $urlVar);
 232  
 233          if (!$result) {
 234              $this->setRedirect(Route::_($this->getReturnPage(), false));
 235          }
 236  
 237          return $result;
 238      }
 239  
 240      /**
 241       * Method to get a model object, loading it if required.
 242       *
 243       * @param   string  $name    The model name. Optional.
 244       * @param   string  $prefix  The class prefix. Optional.
 245       * @param   array   $config  Configuration array for model. Optional.
 246       *
 247       * @return  object  The model.
 248       *
 249       * @since   1.5
 250       */
 251      public function getModel($name = 'Form', $prefix = 'Site', $config = array('ignore_request' => true))
 252      {
 253          return parent::getModel($name, $prefix, $config);
 254      }
 255  
 256      /**
 257       * Gets the URL arguments to append to an item redirect.
 258       *
 259       * @param   integer  $recordId  The primary key id for the item.
 260       * @param   string   $urlVar    The name of the URL variable for the id.
 261       *
 262       * @return  string  The arguments to append to the redirect URL.
 263       *
 264       * @since   1.6
 265       */
 266      protected function getRedirectToItemAppend($recordId = null, $urlVar = 'a_id')
 267      {
 268          // Need to override the parent method completely.
 269          $tmpl   = $this->input->get('tmpl');
 270  
 271          $append = '';
 272  
 273          // Setup redirect info.
 274          if ($tmpl) {
 275              $append .= '&tmpl=' . $tmpl;
 276          }
 277  
 278          // @todo This is a bandaid, not a long term solution.
 279          /**
 280           * if ($layout)
 281           * {
 282           *  $append .= '&layout=' . $layout;
 283           * }
 284           */
 285  
 286          $append .= '&layout=edit';
 287  
 288          if ($recordId) {
 289              $append .= '&' . $urlVar . '=' . $recordId;
 290          }
 291  
 292          $itemId = $this->input->getInt('Itemid');
 293          $return = $this->getReturnPage();
 294          $catId  = $this->input->getInt('catid');
 295  
 296          if ($itemId) {
 297              $append .= '&Itemid=' . $itemId;
 298          }
 299  
 300          if ($catId) {
 301              $append .= '&catid=' . $catId;
 302          }
 303  
 304          if ($return) {
 305              $append .= '&return=' . base64_encode($return);
 306          }
 307  
 308          return $append;
 309      }
 310  
 311      /**
 312       * Get the return URL.
 313       *
 314       * If a "return" variable has been passed in the request
 315       *
 316       * @return  string  The return URL.
 317       *
 318       * @since   1.6
 319       */
 320      protected function getReturnPage()
 321      {
 322          $return = $this->input->get('return', null, 'base64');
 323  
 324          if (empty($return) || !Uri::isInternal(base64_decode($return))) {
 325              return Uri::base();
 326          } else {
 327              return base64_decode($return);
 328          }
 329      }
 330  
 331      /**
 332       * Method to save a record.
 333       *
 334       * @param   string  $key     The name of the primary key of the URL variable.
 335       * @param   string  $urlVar  The name of the URL variable if different from the primary key (sometimes required to avoid router collisions).
 336       *
 337       * @return  boolean  True if successful, false otherwise.
 338       *
 339       * @since   1.6
 340       */
 341      public function save($key = null, $urlVar = 'a_id')
 342      {
 343          $result    = parent::save($key, $urlVar);
 344  
 345          if (\in_array($this->getTask(), ['save2copy', 'apply'], true)) {
 346              return $result;
 347          }
 348  
 349          $app       = $this->app;
 350          $articleId = $app->input->getInt('a_id');
 351  
 352          // Load the parameters.
 353          $params   = $app->getParams();
 354          $menuitem = (int) $params->get('redirect_menuitem');
 355  
 356          // Check for redirection after submission when creating a new article only
 357          if ($menuitem > 0 && $articleId == 0) {
 358              $lang = '';
 359  
 360              if (Multilanguage::isEnabled()) {
 361                  $item = $app->getMenu()->getItem($menuitem);
 362                  $lang = !is_null($item) && $item->language != '*' ? '&lang=' . $item->language : '';
 363              }
 364  
 365              // If ok, redirect to the return page.
 366              if ($result) {
 367                  $this->setRedirect(Route::_('index.php?Itemid=' . $menuitem . $lang, false));
 368              }
 369          } elseif ($this->getTask() === 'save2copy') {
 370              // Redirect to the article page, use the redirect url set from parent controller
 371          } else {
 372              // If ok, redirect to the return page.
 373              if ($result) {
 374                  $this->setRedirect(Route::_($this->getReturnPage(), false));
 375              }
 376          }
 377  
 378          return $result;
 379      }
 380  
 381      /**
 382       * Method to reload a record.
 383       *
 384       * @param   string  $key     The name of the primary key of the URL variable.
 385       * @param   string  $urlVar  The name of the URL variable if different from the primary key (sometimes required to avoid router collisions).
 386       *
 387       * @return  void
 388       *
 389       * @since   3.8.0
 390       */
 391      public function reload($key = null, $urlVar = 'a_id')
 392      {
 393          parent::reload($key, $urlVar);
 394      }
 395  
 396      /**
 397       * Method to save a vote.
 398       *
 399       * @return  void
 400       *
 401       * @since   1.6
 402       */
 403      public function vote()
 404      {
 405          // Check for request forgeries.
 406          $this->checkToken();
 407  
 408          $user_rating = $this->input->getInt('user_rating', -1);
 409  
 410          if ($user_rating > -1) {
 411              $url = $this->input->getString('url', '');
 412              $id = $this->input->getInt('id', 0);
 413              $viewName = $this->input->getString('view', $this->default_view);
 414              $model = $this->getModel($viewName);
 415  
 416              // Don't redirect to an external URL.
 417              if (!Uri::isInternal($url)) {
 418                  $url = Route::_('index.php');
 419              }
 420  
 421              if ($model->storeVote($id, $user_rating)) {
 422                  $this->setRedirect($url, Text::_('COM_CONTENT_ARTICLE_VOTE_SUCCESS'));
 423              } else {
 424                  $this->setRedirect($url, Text::_('COM_CONTENT_ARTICLE_VOTE_FAILURE'));
 425              }
 426          }
 427      }
 428  }


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