[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_languages/src/Controller/ -> OverrideController.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_languages
   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\Languages\Administrator\Controller;
  12  
  13  use Joomla\CMS\Language\Text;
  14  use Joomla\CMS\MVC\Controller\FormController;
  15  use Joomla\CMS\Router\Route;
  16  
  17  // phpcs:disable PSR1.Files.SideEffects
  18  \defined('_JEXEC') or die;
  19  // phpcs:enable PSR1.Files.SideEffects
  20  
  21  /**
  22   * Languages Override Controller
  23   *
  24   * @since  2.5
  25   */
  26  class OverrideController extends FormController
  27  {
  28      /**
  29       * Method to edit an existing override.
  30       *
  31       * @param   string  $key     The name of the primary key of the URL variable (not used here).
  32       * @param   string  $urlVar  The name of the URL variable if different from the primary key (not used here).
  33       *
  34       * @return  void
  35       *
  36       * @since   2.5
  37       */
  38      public function edit($key = null, $urlVar = null)
  39      {
  40          // Do not cache the response to this, its a redirect
  41          $this->app->allowCache(false);
  42  
  43          $cid     = (array) $this->input->post->get('cid', array(), 'string');
  44          $context = "$this->option.edit.$this->context";
  45  
  46          // Get the constant name.
  47          $recordId = (count($cid) ? $cid[0] : $this->input->get('id'));
  48  
  49          // Access check.
  50          if (!$this->allowEdit()) {
  51              $this->setMessage(Text::_('JLIB_APPLICATION_ERROR_EDIT_NOT_PERMITTED'), 'error');
  52              $this->setRedirect(Route::_('index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(), false));
  53  
  54              return;
  55          }
  56  
  57          $this->app->setUserState($context . '.data', null);
  58          $this->setRedirect('index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId, 'id'));
  59      }
  60  
  61      /**
  62       * Method to save an override.
  63       *
  64       * @param   string  $key     The name of the primary key of the URL variable (not used here).
  65       * @param   string  $urlVar  The name of the URL variable if different from the primary key (not used here).
  66       *
  67       * @return  void
  68       *
  69       * @since   2.5
  70       */
  71      public function save($key = null, $urlVar = null)
  72      {
  73          // Check for request forgeries.
  74          $this->checkToken();
  75  
  76          $app     = $this->app;
  77          $model   = $this->getModel();
  78          $data    = $this->input->post->get('jform', array(), 'array');
  79          $context = "$this->option.edit.$this->context";
  80          $task    = $this->getTask();
  81  
  82          $recordId = $this->input->get('id');
  83          $data['id'] = $recordId;
  84  
  85          // Access check.
  86          if (!$this->allowSave($data, 'id')) {
  87              $this->setMessage(Text::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED'), 'error');
  88              $this->setRedirect(Route::_('index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(), false));
  89  
  90              return;
  91          }
  92  
  93          // Validate the posted data.
  94          $form = $model->getForm($data, false);
  95  
  96          if (!$form) {
  97              $app->enqueueMessage($model->getError(), 'error');
  98  
  99              return;
 100          }
 101  
 102          // Test whether the data is valid.
 103          $validData = $model->validate($form, $data);
 104  
 105          // Check for validation errors.
 106          if ($validData === false) {
 107              // Get the validation messages.
 108              $errors = $model->getErrors();
 109  
 110              // Push up to three validation messages out to the user.
 111              for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++) {
 112                  if ($errors[$i] instanceof \Exception) {
 113                      $app->enqueueMessage($errors[$i]->getMessage(), 'warning');
 114                  } else {
 115                      $app->enqueueMessage($errors[$i], 'warning');
 116                  }
 117              }
 118  
 119              // Save the data in the session.
 120              $app->setUserState($context . '.data', $data);
 121  
 122              // Redirect back to the edit screen.
 123              $this->setRedirect(
 124                  Route::_('index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId, 'id'), false)
 125              );
 126  
 127              return;
 128          }
 129  
 130          // Attempt to save the data.
 131          if (!$model->save($validData)) {
 132              // Save the data in the session.
 133              $app->setUserState($context . '.data', $validData);
 134  
 135              // Redirect back to the edit screen.
 136              $this->setMessage(Text::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $model->getError()), 'error');
 137              $this->setRedirect(
 138                  Route::_('index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId, 'id'), false)
 139              );
 140  
 141              return;
 142          }
 143  
 144          // Add message of success.
 145          $this->setMessage(Text::_('COM_LANGUAGES_VIEW_OVERRIDE_SAVE_SUCCESS'));
 146  
 147          // Redirect the user and adjust session state based on the chosen task.
 148          switch ($task) {
 149              case 'apply':
 150                  // Set the record data in the session.
 151                  $app->setUserState($context . '.data', null);
 152  
 153                  // Redirect back to the edit screen
 154                  $this->setRedirect(
 155                      Route::_('index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($validData['key'], 'id'), false)
 156                  );
 157                  break;
 158  
 159              case 'save2new':
 160                  // Clear the record id and data from the session.
 161                  $app->setUserState($context . '.data', null);
 162  
 163                  // Redirect back to the edit screen
 164                  $this->setRedirect(
 165                      Route::_('index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend(null, 'id'), false)
 166                  );
 167                  break;
 168  
 169              default:
 170                  // Clear the record id and data from the session.
 171                  $app->setUserState($context . '.data', null);
 172  
 173                  // Redirect to the list screen.
 174                  $this->setRedirect(Route::_('index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(), false));
 175                  break;
 176          }
 177      }
 178  
 179      /**
 180       * Method to cancel an edit.
 181       *
 182       * @param   string  $key  The name of the primary key of the URL variable (not used here).
 183       *
 184       * @return  void
 185       *
 186       * @since   2.5
 187       */
 188      public function cancel($key = null)
 189      {
 190          $this->checkToken();
 191  
 192          $context = "$this->option.edit.$this->context";
 193  
 194          $this->app->setUserState($context . '.data', null);
 195          $this->setRedirect(Route::_('index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(), false));
 196      }
 197  }


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