[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_finder/src/Controller/ -> FilterController.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_finder
   6   *
   7   * @copyright   (C) 2011 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\Finder\Administrator\Controller;
  12  
  13  use Joomla\CMS\Language\Text;
  14  use Joomla\CMS\MVC\Controller\FormController;
  15  use Joomla\CMS\Router\Route;
  16  use Joomla\Utilities\ArrayHelper;
  17  
  18  // phpcs:disable PSR1.Files.SideEffects
  19  \defined('_JEXEC') or die;
  20  // phpcs:enable PSR1.Files.SideEffects
  21  
  22  /**
  23   * Indexer controller class for Finder.
  24   *
  25   * @since  2.5
  26   */
  27  class FilterController extends FormController
  28  {
  29      /**
  30       * Method to save a record.
  31       *
  32       * @param   string  $key     The name of the primary key of the URL variable.
  33       * @param   string  $urlVar  The name of the URL variable if different from the primary key (sometimes required to avoid router collisions).
  34       *
  35       * @return  boolean  True if successful, false otherwise.
  36       *
  37       * @since   2.5
  38       */
  39      public function save($key = null, $urlVar = null)
  40      {
  41          // Check for request forgeries.
  42          $this->checkToken();
  43  
  44          /** @var \Joomla\Component\Finder\Administrator\Model\FilterModel $model */
  45          $model = $this->getModel();
  46          $table = $model->getTable();
  47          $data = $this->input->post->get('jform', array(), 'array');
  48          $checkin = $table->hasField('checked_out');
  49          $context = "$this->option.edit.$this->context";
  50          $task = $this->getTask();
  51  
  52          // Determine the name of the primary key for the data.
  53          if (empty($key)) {
  54              $key = $table->getKeyName();
  55          }
  56  
  57          // To avoid data collisions the urlVar may be different from the primary key.
  58          if (empty($urlVar)) {
  59              $urlVar = $key;
  60          }
  61  
  62          $recordId = $this->input->get($urlVar, '', 'int');
  63  
  64          if (!$this->checkEditId($context, $recordId)) {
  65              // Somehow the person just went to the form and tried to save it. We don't allow that.
  66              $this->setMessage(Text::sprintf('JLIB_APPLICATION_ERROR_UNHELD_ID', $recordId), 'error');
  67              $this->setRedirect(Route::_('index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(), false));
  68  
  69              return false;
  70          }
  71  
  72          // Populate the row id from the session.
  73          $data[$key] = $recordId;
  74  
  75          // The save2copy task needs to be handled slightly differently.
  76          if ($task === 'save2copy') {
  77              // Check-in the original row.
  78              if ($checkin && $model->checkin($data[$key]) === false) {
  79                  // Check-in failed. Go back to the item and display a notice.
  80                  if (!\count($this->app->getMessageQueue())) {
  81                      $this->setMessage(Text::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError()), 'error');
  82                  }
  83  
  84                  $this->setRedirect('index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId, $urlVar));
  85  
  86                  return false;
  87              }
  88  
  89              // Reset the ID and then treat the request as for Apply.
  90              $data[$key] = 0;
  91              $task = 'apply';
  92          }
  93  
  94          // Access check.
  95          if (!$this->allowSave($data, $key)) {
  96              $this->setMessage(Text::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED'), 'error');
  97              $this->setRedirect(Route::_('index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(), false));
  98  
  99              return false;
 100          }
 101  
 102          // Validate the posted data.
 103          // Sometimes the form needs some posted data, such as for plugins and modules.
 104          $form = $model->getForm($data, false);
 105  
 106          if (!$form) {
 107              $this->app->enqueueMessage($model->getError(), 'error');
 108  
 109              return false;
 110          }
 111  
 112          // Test whether the data is valid.
 113          $validData = $model->validate($form, $data);
 114  
 115          // Check for validation errors.
 116          if ($validData === false) {
 117              // Get the validation messages.
 118              $errors = $model->getErrors();
 119  
 120              // Push up to three validation messages out to the user.
 121              for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++) {
 122                  if ($errors[$i] instanceof \Exception) {
 123                      $this->app->enqueueMessage($errors[$i]->getMessage(), 'warning');
 124                  } else {
 125                      $this->app->enqueueMessage($errors[$i], 'warning');
 126                  }
 127              }
 128  
 129              // Save the data in the session.
 130              $this->app->setUserState($context . '.data', $data);
 131  
 132              // Redirect back to the edit screen.
 133              $this->setRedirect(
 134                  Route::_('index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId, $key), false)
 135              );
 136  
 137              return false;
 138          }
 139  
 140          // Get and sanitize the filter data.
 141          $validData['data'] = $this->input->post->get('t', array(), 'array');
 142          $validData['data'] = array_unique($validData['data']);
 143          $validData['data'] = ArrayHelper::toInteger($validData['data']);
 144  
 145          // Remove any values of zero.
 146          if (array_search(0, $validData['data'], true)) {
 147              unset($validData['data'][array_search(0, $validData['data'], true)]);
 148          }
 149  
 150          // Attempt to save the data.
 151          if (!$model->save($validData)) {
 152              // Save the data in the session.
 153              $this->app->setUserState($context . '.data', $validData);
 154  
 155              // Redirect back to the edit screen.
 156              $this->setMessage(Text::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $model->getError()), 'error');
 157              $this->setRedirect(
 158                  Route::_('index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId, $key), false)
 159              );
 160  
 161              return false;
 162          }
 163  
 164          // Save succeeded, so check-in the record.
 165          if ($checkin && $model->checkin($validData[$key]) === false) {
 166              // Save the data in the session.
 167              $this->app->setUserState($context . '.data', $validData);
 168  
 169              // Check-in failed, so go back to the record and display a notice.
 170              $this->setMessage(Text::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError()), 'error');
 171              $this->setRedirect('index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId, $key));
 172  
 173              return false;
 174          }
 175  
 176          $this->setMessage(
 177              Text::_(
 178                  ($this->app->getLanguage()->hasKey($this->text_prefix . ($recordId === 0 && $this->app->isClient('site') ? '_SUBMIT' : '') . '_SAVE_SUCCESS')
 179                  ? $this->text_prefix : 'JLIB_APPLICATION') . ($recordId === 0 && $this->app->isClient('site') ? '_SUBMIT' : '') . '_SAVE_SUCCESS'
 180              )
 181          );
 182  
 183          // Redirect the user and adjust session state based on the chosen task.
 184          switch ($task) {
 185              case 'apply':
 186                  // Set the record data in the session.
 187                  $recordId = $model->getState($this->context . '.id');
 188                  $this->holdEditId($context, $recordId);
 189                  $this->app->setUserState($context . '.data', null);
 190                  $model->checkout($recordId);
 191  
 192                  // Redirect back to the edit screen.
 193                  $this->setRedirect(
 194                      Route::_('index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId, $key), false)
 195                  );
 196  
 197                  break;
 198  
 199              case 'save2new':
 200                  // Clear the record id and data from the session.
 201                  $this->releaseEditId($context, $recordId);
 202                  $this->app->setUserState($context . '.data', null);
 203  
 204                  // Redirect back to the edit screen.
 205                  $this->setRedirect(
 206                      Route::_('index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend(null, $key), false)
 207                  );
 208  
 209                  break;
 210  
 211              default:
 212                  // Clear the record id and data from the session.
 213                  $this->releaseEditId($context, $recordId);
 214                  $this->app->setUserState($context . '.data', null);
 215  
 216                  // Redirect to the list screen.
 217                  $this->setRedirect(
 218                      Route::_('index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(), false)
 219                  );
 220  
 221                  break;
 222          }
 223  
 224          // Invoke the postSave method to allow for the child class to access the model.
 225          $this->postSaveHook($model, $validData);
 226  
 227          return true;
 228      }
 229  }


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