[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_templates/src/Controller/ -> StyleController.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_templates
   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\Templates\Administrator\Controller;
  12  
  13  use Joomla\CMS\Form\Form;
  14  use Joomla\CMS\Language\Text;
  15  use Joomla\CMS\MVC\Controller\FormController;
  16  
  17  // phpcs:disable PSR1.Files.SideEffects
  18  \defined('_JEXEC') or die;
  19  // phpcs:enable PSR1.Files.SideEffects
  20  
  21  /**
  22   * Template style controller class.
  23   *
  24   * @since  1.6
  25   */
  26  class StyleController extends FormController
  27  {
  28      /**
  29       * The prefix to use with controller messages.
  30       *
  31       * @var     string
  32       * @since   1.6
  33       */
  34      protected $text_prefix = 'COM_TEMPLATES_STYLE';
  35  
  36      /**
  37       * Method to save a template style.
  38       *
  39       * @param   string  $key     The name of the primary key of the URL variable.
  40       * @param   string  $urlVar  The name of the URL variable if different from the primary key (sometimes required to avoid router collisions).
  41       *
  42       * @return  boolean  True if successful, false otherwise.
  43       *
  44       * @since   1.6
  45       */
  46      public function save($key = null, $urlVar = null)
  47      {
  48          $this->checkToken();
  49  
  50          if ($this->app->getDocument()->getType() === 'json') {
  51              $model = $this->getModel('Style', 'Administrator');
  52              $table = $model->getTable();
  53              $data  = $this->input->post->get('params', array(), 'array');
  54              $checkin = $table->hasField('checked_out');
  55              $context = $this->option . '.edit.' . $this->context;
  56  
  57              $item = $model->getItem($this->app->getTemplate(true)->id);
  58  
  59              // Setting received params
  60              $item->set('params', $data);
  61  
  62              $data = $item->getProperties();
  63              unset($data['xml']);
  64  
  65              $key = $table->getKeyName();
  66  
  67              // Access check.
  68              if (!$this->allowSave($data, $key)) {
  69                  $this->app->enqueueMessage(Text::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED'), 'error');
  70  
  71                  return false;
  72              }
  73  
  74              Form::addFormPath(JPATH_ADMINISTRATOR . '/components/com_templates/forms');
  75  
  76              // Validate the posted data.
  77              // Sometimes the form needs some posted data, such as for plugins and modules.
  78              $form = $model->getForm($data, false);
  79  
  80              if (!$form) {
  81                  $this->app->enqueueMessage($model->getError(), 'error');
  82  
  83                  return false;
  84              }
  85  
  86              // Test whether the data is valid.
  87              $validData = $model->validate($form, $data);
  88  
  89              if ($validData === false) {
  90                  // Get the validation messages.
  91                  $errors = $model->getErrors();
  92  
  93                  // Push up to three validation messages out to the user.
  94                  for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++) {
  95                      if ($errors[$i] instanceof \Exception) {
  96                          $this->app->enqueueMessage($errors[$i]->getMessage(), 'warning');
  97                      } else {
  98                          $this->app->enqueueMessage($errors[$i], 'warning');
  99                      }
 100                  }
 101  
 102                  // Save the data in the session.
 103                  $this->app->setUserState($context . '.data', $data);
 104  
 105                  return false;
 106              }
 107  
 108              if (!isset($validData['tags'])) {
 109                  $validData['tags'] = null;
 110              }
 111  
 112              // Attempt to save the data.
 113              if (!$model->save($validData)) {
 114                  // Save the data in the session.
 115                  $this->app->setUserState($context . '.data', $validData);
 116  
 117                  $this->app->enqueueMessage(Text::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $model->getError()), 'error');
 118  
 119                  return false;
 120              }
 121  
 122              // Save succeeded, so check-in the record.
 123              if ($checkin && $model->checkin($validData[$key]) === false) {
 124                  // Save the data in the session.
 125                  $this->app->setUserState($context . '.data', $validData);
 126  
 127                  // Check-in failed, so go back to the record and display a notice.
 128                  $this->app->enqueueMessage(Text::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError()), 'error');
 129  
 130                  return false;
 131              }
 132  
 133              // Redirect the user and adjust session state
 134              // Set the record data in the session.
 135              $recordId = $model->getState($this->context . '.id');
 136              $this->holdEditId($context, $recordId);
 137              $this->app->setUserState($context . '.data', null);
 138              $model->checkout($recordId);
 139  
 140              // Invoke the postSave method to allow for the child class to access the model.
 141              $this->postSaveHook($model, $validData);
 142  
 143              return true;
 144          }
 145  
 146          return parent::save($key, $urlVar);
 147      }
 148  }


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