[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_modules/src/Controller/ -> ModuleController.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_modules
   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\Modules\Administrator\Controller;
  12  
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Form\Form;
  15  use Joomla\CMS\Language\Text;
  16  use Joomla\CMS\MVC\Controller\FormController;
  17  use Joomla\CMS\MVC\Model\BaseDatabaseModel;
  18  use Joomla\CMS\Response\JsonResponse;
  19  use Joomla\CMS\Router\Route;
  20  use Joomla\CMS\Session\Session;
  21  use Joomla\CMS\Uri\Uri;
  22  use Joomla\Database\ParameterType;
  23  
  24  // phpcs:disable PSR1.Files.SideEffects
  25  \defined('_JEXEC') or die;
  26  // phpcs:enable PSR1.Files.SideEffects
  27  
  28  /**
  29   * Module controller class.
  30   *
  31   * @since  1.6
  32   */
  33  class ModuleController extends FormController
  34  {
  35      /**
  36       * Override parent add method.
  37       *
  38       * @return  \Exception|void  True if the record can be added, a \Exception object if not.
  39       *
  40       * @since   1.6
  41       */
  42      public function add()
  43      {
  44          $app = $this->app;
  45  
  46          // Get the result of the parent method. If an error, just return it.
  47          $result = parent::add();
  48  
  49          if ($result instanceof \Exception) {
  50              return $result;
  51          }
  52  
  53          // Look for the Extension ID.
  54          $extensionId = $this->input->get('eid', 0, 'int');
  55  
  56          if (empty($extensionId)) {
  57              $redirectUrl = 'index.php?option=' . $this->option . '&view=' . $this->view_item . '&layout=edit';
  58  
  59              $this->setRedirect(Route::_($redirectUrl, false));
  60  
  61              $app->enqueueMessage(Text::_('COM_MODULES_ERROR_INVALID_EXTENSION'), 'warning');
  62          }
  63  
  64          $app->setUserState('com_modules.add.module.extension_id', $extensionId);
  65          $app->setUserState('com_modules.add.module.params', null);
  66  
  67          // Parameters could be coming in for a new item, so let's set them.
  68          $params = $this->input->get('params', array(), 'array');
  69          $app->setUserState('com_modules.add.module.params', $params);
  70      }
  71  
  72      /**
  73       * Override parent cancel method to reset the add module state.
  74       *
  75       * @param   string  $key  The name of the primary key of the URL variable.
  76       *
  77       * @return  boolean  True if access level checks pass, false otherwise.
  78       *
  79       * @since   1.6
  80       */
  81      public function cancel($key = null)
  82      {
  83          $result = parent::cancel();
  84  
  85          $this->app->setUserState('com_modules.add.module.extension_id', null);
  86          $this->app->setUserState('com_modules.add.module.params', null);
  87  
  88          if ($return = $this->input->get('return', '', 'BASE64')) {
  89              $return = base64_decode($return);
  90  
  91              // Don't redirect to an external URL.
  92              if (!Uri::isInternal($return)) {
  93                  $return = Uri::base();
  94              }
  95  
  96              $this->app->redirect($return);
  97          }
  98  
  99          return $result;
 100      }
 101  
 102      /**
 103       * Override parent allowSave method.
 104       *
 105       * @param   array   $data  An array of input data.
 106       * @param   string  $key   The name of the key for the primary key.
 107       *
 108       * @return  boolean
 109       *
 110       * @since   1.6
 111       */
 112      protected function allowSave($data, $key = 'id')
 113      {
 114          // Use custom position if selected
 115          if (isset($data['custom_position'])) {
 116              if (empty($data['position'])) {
 117                  $data['position'] = $data['custom_position'];
 118              }
 119  
 120              unset($data['custom_position']);
 121          }
 122  
 123          return parent::allowSave($data, $key);
 124      }
 125  
 126      /**
 127       * Method override to check if you can edit an existing record.
 128       *
 129       * @param   array   $data  An array of input data.
 130       * @param   string  $key   The name of the key for the primary key.
 131       *
 132       * @return  boolean
 133       *
 134       * @since   3.2
 135       */
 136      protected function allowEdit($data = array(), $key = 'id')
 137      {
 138          // Initialise variables.
 139          $recordId = (int) isset($data[$key]) ? $data[$key] : 0;
 140  
 141          // Zero record (id:0), return component edit permission by calling parent controller method
 142          if (!$recordId) {
 143              return parent::allowEdit($data, $key);
 144          }
 145  
 146          // Check edit on the record asset (explicit or inherited)
 147          if ($this->app->getIdentity()->authorise('core.edit', 'com_modules.module.' . $recordId)) {
 148              return true;
 149          }
 150  
 151          return false;
 152      }
 153  
 154      /**
 155       * Method to run batch operations.
 156       *
 157       * @param   string  $model  The model
 158       *
 159       * @return  boolean  True on success.
 160       *
 161       * @since   1.7
 162       */
 163      public function batch($model = null)
 164      {
 165          $this->checkToken();
 166  
 167          // Set the model
 168          $model = $this->getModel('Module', 'Administrator', array());
 169  
 170          // Preset the redirect
 171          $redirectUrl = 'index.php?option=com_modules&view=modules' . $this->getRedirectToListAppend();
 172  
 173          $this->setRedirect(Route::_($redirectUrl, false));
 174  
 175          return parent::batch($model);
 176      }
 177  
 178      /**
 179       * Function that allows child controller access to model data after the data has been saved.
 180       *
 181       * @param   BaseDatabaseModel  $model      The data model object.
 182       * @param   array              $validData  The validated data.
 183       *
 184       * @return  void
 185       *
 186       * @since   1.6
 187       */
 188      protected function postSaveHook(BaseDatabaseModel $model, $validData = array())
 189      {
 190          $task = $this->getTask();
 191  
 192          switch ($task) {
 193              case 'save2new':
 194                  $this->app->setUserState('com_modules.add.module.extension_id', $model->getState('module.extension_id'));
 195                  break;
 196  
 197              default:
 198                  $this->app->setUserState('com_modules.add.module.extension_id', null);
 199                  break;
 200          }
 201  
 202          $this->app->setUserState('com_modules.add.module.params', null);
 203      }
 204  
 205      /**
 206       * Method to save a record.
 207       *
 208       * @param   string  $key     The name of the primary key of the URL variable.
 209       * @param   string  $urlVar  The name of the URL variable if different from the primary key
 210       *
 211       * @return  boolean  True if successful, false otherwise.
 212       */
 213      public function save($key = null, $urlVar = null)
 214      {
 215          $this->checkToken();
 216  
 217          if ($this->app->getDocument()->getType() == 'json') {
 218              $model = $this->getModel();
 219              $data  = $this->input->post->get('jform', array(), 'array');
 220              $item = $model->getItem($this->input->get('id'));
 221              $properties = $item->getProperties();
 222  
 223              if (isset($data['params'])) {
 224                  unset($properties['params']);
 225              }
 226  
 227              // Replace changed properties
 228              $data = array_replace_recursive($properties, $data);
 229  
 230              if (!empty($data['assigned'])) {
 231                  $data['assigned'] = array_map('abs', $data['assigned']);
 232              }
 233  
 234              // Add new data to input before process by parent save()
 235              $this->input->post->set('jform', $data);
 236  
 237              // Add path of forms directory
 238              Form::addFormPath(JPATH_ADMINISTRATOR . '/components/com_modules/models/forms');
 239          }
 240  
 241          return parent::save($key, $urlVar);
 242      }
 243  
 244      /**
 245       * Method to get the other modules in the same position
 246       *
 247       * @return  string  The data for the Ajax request.
 248       *
 249       * @since   3.6.3
 250       */
 251      public function orderPosition()
 252      {
 253          $app = $this->app;
 254  
 255          // Send json mime type.
 256          $app->mimeType = 'application/json';
 257          $app->setHeader('Content-Type', $app->mimeType . '; charset=' . $app->charSet);
 258          $app->sendHeaders();
 259  
 260          // Check if user token is valid.
 261          if (!Session::checkToken('get')) {
 262              $app->enqueueMessage(Text::_('JINVALID_TOKEN_NOTICE'), 'error');
 263              echo new JsonResponse();
 264              $app->close();
 265          }
 266  
 267          $clientId = $this->input->getValue('client_id');
 268          $position = $this->input->getValue('position');
 269          $moduleId = $this->input->getValue('module_id');
 270  
 271          // Access check.
 272          if (
 273              !$this->app->getIdentity()->authorise('core.create', 'com_modules')
 274              && !$this->app->getIdentity()->authorise('core.edit.state', 'com_modules')
 275              && ($moduleId && !$this->app->getIdentity()->authorise('core.edit.state', 'com_modules.module.' . $moduleId))
 276          ) {
 277              $app->enqueueMessage(Text::_('JLIB_APPLICATION_ERROR_ACCESS_FORBIDDEN'), 'error');
 278              echo new JsonResponse();
 279              $app->close();
 280          }
 281  
 282          $db    = Factory::getDbo();
 283          $clientId = (int) $clientId;
 284          $query = $db->getQuery(true)
 285              ->select($db->quoteName(['position', 'ordering', 'title']))
 286              ->from($db->quoteName('#__modules'))
 287              ->where($db->quoteName('client_id') . ' = :clientid')
 288              ->where($db->quoteName('position') . ' = :position')
 289              ->order($db->quoteName('ordering'))
 290              ->bind(':clientid', $clientId, ParameterType::INTEGER)
 291              ->bind(':position', $position);
 292  
 293          $db->setQuery($query);
 294  
 295          try {
 296              $orders = $db->loadObjectList();
 297          } catch (\RuntimeException $e) {
 298              $app->enqueueMessage($e->getMessage(), 'error');
 299  
 300              return '';
 301          }
 302  
 303          $orders2 = array();
 304          $n = count($orders);
 305  
 306          if ($n > 0) {
 307              for ($i = 0; $i < $n; $i++) {
 308                  if (!isset($orders2[$orders[$i]->position])) {
 309                      $orders2[$orders[$i]->position] = 0;
 310                  }
 311  
 312                  $orders2[$orders[$i]->position]++;
 313                  $ord = $orders2[$orders[$i]->position];
 314                  $title = Text::sprintf('COM_MODULES_OPTION_ORDER_POSITION', $ord, htmlspecialchars($orders[$i]->title, ENT_QUOTES, 'UTF-8'));
 315  
 316                  $html[] = $orders[$i]->position . ',' . $ord . ',' . $title;
 317              }
 318          } else {
 319              $html[] = $position . ',' . 1 . ',' . Text::_('JNONE');
 320          }
 321  
 322          echo new JsonResponse($html);
 323          $app->close();
 324      }
 325  
 326      /**
 327       * Gets the URL arguments to append to an item redirect.
 328       *
 329       * @param   integer  $recordId  The primary key id for the item.
 330       * @param   string   $urlVar    The name of the URL variable for the id.
 331       *
 332       * @return  string  The arguments to append to the redirect URL.
 333       *
 334       * @since  4.0.0
 335       */
 336      protected function getRedirectToItemAppend($recordId = null, $urlVar = 'id')
 337      {
 338          $append = parent::getRedirectToItemAppend($recordId);
 339          $append .= '&client_id=' . $this->input->getInt('client_id');
 340  
 341          return $append;
 342      }
 343  
 344      /**
 345       * Gets the URL arguments to append to a list redirect.
 346       *
 347       * @return  string  The arguments to append to the redirect URL.
 348       *
 349       * @since  4.0.0
 350       */
 351      protected function getRedirectToListAppend()
 352      {
 353          $append = parent::getRedirectToListAppend();
 354          $append .= '&client_id=' . $this->input->getInt('client_id');
 355  
 356          return $append;
 357      }
 358  }


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