[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_mails/src/Controller/ -> TemplateController.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_mails
   6   *
   7   * @copyright   (C) 2019 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\Mails\Administrator\Controller;
  12  
  13  use Joomla\CMS\Application\CMSApplication;
  14  use Joomla\CMS\Factory;
  15  use Joomla\CMS\Language\Text;
  16  use Joomla\CMS\MVC\Controller\FormController;
  17  use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
  18  use Joomla\CMS\Router\Route;
  19  use Joomla\CMS\Uri\Uri;
  20  use Joomla\Input\Input;
  21  
  22  // phpcs:disable PSR1.Files.SideEffects
  23  \defined('_JEXEC') or die;
  24  // phpcs:enable PSR1.Files.SideEffects
  25  
  26  /**
  27   * The template controller
  28   *
  29   * @since  4.0.0
  30   */
  31  class TemplateController extends FormController
  32  {
  33      /**
  34       * Constructor.
  35       *
  36       * @param   array                $config   An optional associative array of configuration settings.
  37       *                                         Recognized key values include 'name', 'default_task', 'model_path', and
  38       *                                         'view_path' (this list is not meant to be comprehensive).
  39       * @param   MVCFactoryInterface  $factory  The factory.
  40       * @param   CMSApplication       $app      The Application for the dispatcher
  41       * @param   Input                $input    Input
  42       *
  43       * @since   4.0.0
  44       * @throws  \Exception
  45       */
  46      public function __construct($config = array(), MVCFactoryInterface $factory = null, $app = null, $input = null)
  47      {
  48          parent::__construct($config, $factory, $app, $input);
  49  
  50          $this->view_item = 'template';
  51          $this->view_list = 'templates';
  52      }
  53  
  54      /**
  55       * Method to check if you can add a new record.
  56       *
  57       * @param   array  $data  An array of input data.
  58       *
  59       * @return  boolean
  60       *
  61       * @since   4.0.0
  62       */
  63      protected function allowAdd($data = [])
  64      {
  65          return false;
  66      }
  67  
  68      /**
  69       * Method to edit an existing record.
  70       *
  71       * @param   string  $key     The name of the primary key of the URL variable.
  72       * @param   string  $urlVar  The name of the URL variable if different from the primary key
  73       *                           (sometimes required to avoid router collisions).
  74       *
  75       * @return  boolean  True if access level check and checkout passes, false otherwise.
  76       *
  77       * @since   4.0.0
  78       */
  79      public function edit($key = null, $urlVar = null)
  80      {
  81          // Do not cache the response to this, its a redirect, and mod_expires and google chrome browser bugs cache it forever!
  82          $this->app->allowCache(false);
  83  
  84          $context = "$this->option.edit.$this->context";
  85  
  86          // Get the previous record id (if any) and the current record id.
  87          $template_id = $this->input->getCmd('template_id');
  88          $language = $this->input->getCmd('language');
  89  
  90          // Access check.
  91          if (!$this->allowEdit(array('template_id' => $template_id, 'language' => $language), $template_id)) {
  92              $this->setMessage(Text::_('JLIB_APPLICATION_ERROR_EDIT_NOT_PERMITTED'), 'error');
  93  
  94              $this->setRedirect(
  95                  Route::_(
  96                      'index.php?option=' . $this->option . '&view=' . $this->view_list
  97                      . $this->getRedirectToListAppend(),
  98                      false
  99                  )
 100              );
 101  
 102              return false;
 103          }
 104  
 105          // Check-out succeeded, push the new record id into the session.
 106          $this->holdEditId($context, $template_id . '.' . $language);
 107          $this->app->setUserState($context . '.data', null);
 108  
 109          $this->setRedirect(
 110              Route::_(
 111                  'index.php?option=' . $this->option . '&view=' . $this->view_item
 112                  . $this->getRedirectToItemAppend(array($template_id, $language), 'template_id'),
 113                  false
 114              )
 115          );
 116  
 117          return true;
 118      }
 119  
 120      /**
 121       * Gets the URL arguments to append to an item redirect.
 122       *
 123       * @param   string[]  $recordId  The primary key id for the item in the first element and the language of the
 124       *                               mail template in the second key.
 125       * @param   string    $urlVar    The name of the URL variable for the id.
 126       *
 127       * @return  string  The arguments to append to the redirect URL.
 128       *
 129       * @since   4.0.0
 130       */
 131      protected function getRedirectToItemAppend($recordId = null, $urlVar = 'id')
 132      {
 133          $language = array_pop($recordId);
 134          $return = parent::getRedirectToItemAppend(array_pop($recordId), $urlVar);
 135          $return .= '&language=' . $language;
 136  
 137          return $return;
 138      }
 139  
 140      /**
 141       * Method to save a record.
 142       *
 143       * @param   string  $key     The name of the primary key of the URL variable.
 144       * @param   string  $urlVar  The name of the URL variable if different from the primary key (sometimes required to avoid router collisions).
 145       *
 146       * @return  boolean  True if successful, false otherwise.
 147       *
 148       * @since   4.0.0
 149       */
 150      public function save($key = null, $urlVar = null)
 151      {
 152          // Check for request forgeries.
 153          $this->checkToken();
 154  
 155          /** @var \Joomla\CMS\MVC\Model\AdminModel $model */
 156          $model = $this->getModel();
 157          $data  = $this->input->post->get('jform', array(), 'array');
 158          $context = "$this->option.edit.$this->context";
 159          $task = $this->getTask();
 160  
 161          $recordId = $this->input->getCmd('template_id');
 162          $language = $this->input->getCmd('language');
 163  
 164          // Populate the row id from the session.
 165          $data['template_id'] = $recordId;
 166          $data['language'] = $language;
 167  
 168          // Access check.
 169          if (!$this->allowSave($data, 'template_id')) {
 170              $this->setMessage(Text::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED'), 'error');
 171  
 172              $this->setRedirect(
 173                  Route::_(
 174                      'index.php?option=' . $this->option . '&view=' . $this->view_list
 175                      . $this->getRedirectToListAppend(),
 176                      false
 177                  )
 178              );
 179  
 180              return false;
 181          }
 182  
 183          // Validate the posted data.
 184          // Sometimes the form needs some posted data, such as for plugins and modules.
 185          $form = $model->getForm($data, false);
 186  
 187          if (!$form) {
 188              $this->app->enqueueMessage($model->getError(), 'error');
 189  
 190              return false;
 191          }
 192  
 193          // Send an object which can be modified through the plugin event
 194          $objData = (object) $data;
 195          $this->app->triggerEvent(
 196              'onContentNormaliseRequestData',
 197              array($this->option . '.' . $this->context, $objData, $form)
 198          );
 199          $data = (array) $objData;
 200  
 201          // Test whether the data is valid.
 202          $validData = $model->validate($form, $data);
 203  
 204          // Check for validation errors.
 205          if ($validData === false) {
 206              // Get the validation messages.
 207              $errors = $model->getErrors();
 208  
 209              // Push up to three validation messages out to the user.
 210              for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++) {
 211                  if ($errors[$i] instanceof \Exception) {
 212                      $this->app->enqueueMessage($errors[$i]->getMessage(), 'warning');
 213                  } else {
 214                      $this->app->enqueueMessage($errors[$i], 'warning');
 215                  }
 216              }
 217  
 218              // Save the data in the session.
 219              $this->app->setUserState($context . '.data', $data);
 220  
 221              // Redirect back to the edit screen.
 222              $this->setRedirect(
 223                  Route::_(
 224                      'index.php?option=' . $this->option . '&view=' . $this->view_item
 225                      . $this->getRedirectToItemAppend(array($recordId, $language), 'template_id'),
 226                      false
 227                  )
 228              );
 229  
 230              return false;
 231          }
 232  
 233          // Attempt to save the data.
 234          if (!$model->save($validData)) {
 235              // Save the data in the session.
 236              $this->app->setUserState($context . '.data', $validData);
 237  
 238              // Redirect back to the edit screen.
 239              $this->setMessage(Text::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $model->getError()), 'error');
 240  
 241              $this->setRedirect(
 242                  Route::_(
 243                      'index.php?option=' . $this->option . '&view=' . $this->view_item
 244                      . $this->getRedirectToItemAppend(array($recordId, $language), 'template_id'),
 245                      false
 246                  )
 247              );
 248  
 249              return false;
 250          }
 251  
 252          $langKey = $this->text_prefix . ($recordId === 0 && $this->app->isClient('site') ? '_SUBMIT' : '') . '_SAVE_SUCCESS';
 253          $prefix  = Factory::getLanguage()->hasKey($langKey) ? $this->text_prefix : 'COM_MAILS';
 254  
 255          $this->setMessage(Text::_($prefix . ($recordId === 0 && $this->app->isClient('site') ? '_SUBMIT' : '') . '_SAVE_SUCCESS'));
 256  
 257          // Redirect the user and adjust session state based on the chosen task.
 258          switch ($task) {
 259              case 'apply':
 260                  // Set the record data in the session.
 261                  $this->holdEditId($context, $recordId);
 262                  $this->app->setUserState($context . '.data', null);
 263  
 264                  // Redirect back to the edit screen.
 265                  $this->setRedirect(
 266                      Route::_(
 267                          'index.php?option=' . $this->option . '&view=' . $this->view_item
 268                          . $this->getRedirectToItemAppend(array($recordId, $language), 'template_id'),
 269                          false
 270                      )
 271                  );
 272                  break;
 273  
 274              default:
 275                  // Clear the record id and data from the session.
 276                  $this->releaseEditId($context, $recordId);
 277                  $this->app->setUserState($context . '.data', null);
 278  
 279                  $url = 'index.php?option=' . $this->option . '&view=' . $this->view_list
 280                      . $this->getRedirectToListAppend();
 281  
 282                  // Check if there is a return value
 283                  $return = $this->input->get('return', null, 'base64');
 284  
 285                  if (!is_null($return) && Uri::isInternal(base64_decode($return))) {
 286                      $url = base64_decode($return);
 287                  }
 288  
 289                  // Redirect to the list screen.
 290                  $this->setRedirect(Route::_($url, false));
 291                  break;
 292          }
 293  
 294          // Invoke the postSave method to allow for the child class to access the model.
 295          $this->postSaveHook($model, $validData);
 296  
 297          return true;
 298      }
 299  }


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