[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/MVC/Model/ -> FormModel.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2009 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\MVC\Model;
  11  
  12  use Joomla\CMS\Factory;
  13  use Joomla\CMS\Filter\InputFilter;
  14  use Joomla\CMS\Form\Form;
  15  use Joomla\CMS\Form\FormFactoryAwareInterface;
  16  use Joomla\CMS\Form\FormFactoryAwareTrait;
  17  use Joomla\CMS\Form\FormFactoryInterface;
  18  use Joomla\CMS\Form\FormRule;
  19  use Joomla\CMS\Language\Text;
  20  use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
  21  use Joomla\CMS\Plugin\PluginHelper;
  22  
  23  // phpcs:disable PSR1.Files.SideEffects
  24  \defined('JPATH_PLATFORM') or die;
  25  // phpcs:enable PSR1.Files.SideEffects
  26  
  27  /**
  28   * Prototype form model.
  29   *
  30   * @see    Form
  31   * @see    FormField
  32   * @see    FormRule
  33   * @since  1.6
  34   */
  35  abstract class FormModel extends BaseDatabaseModel implements FormFactoryAwareInterface, FormModelInterface
  36  {
  37      use FormBehaviorTrait;
  38      use FormFactoryAwareTrait;
  39  
  40      /**
  41       * Maps events to plugin groups.
  42       *
  43       * @var    array
  44       * @since  3.6
  45       */
  46      protected $events_map = null;
  47  
  48      /**
  49       * Constructor
  50       *
  51       * @param   array                 $config       An array of configuration options (name, state, dbo, table_path, ignore_request).
  52       * @param   MVCFactoryInterface   $factory      The factory.
  53       * @param   FormFactoryInterface  $formFactory  The form factory.
  54       *
  55       * @since   3.6
  56       * @throws  \Exception
  57       */
  58      public function __construct($config = array(), MVCFactoryInterface $factory = null, FormFactoryInterface $formFactory = null)
  59      {
  60          $config['events_map'] = $config['events_map'] ?? array();
  61  
  62          $this->events_map = array_merge(
  63              array('validate' => 'content'),
  64              $config['events_map']
  65          );
  66  
  67          parent::__construct($config, $factory);
  68  
  69          $this->setFormFactory($formFactory);
  70      }
  71  
  72      /**
  73       * Method to checkin a row.
  74       *
  75       * @param   integer  $pk  The numeric id of the primary key.
  76       *
  77       * @return  boolean  False on failure or error, true otherwise.
  78       *
  79       * @since   1.6
  80       */
  81      public function checkin($pk = null)
  82      {
  83          // Only attempt to check the row in if it exists.
  84          if ($pk) {
  85              $user = $this->getCurrentUser();
  86  
  87              // Get an instance of the row to checkin.
  88              $table = $this->getTable();
  89  
  90              if (!$table->load($pk)) {
  91                  $this->setError($table->getError());
  92  
  93                  return false;
  94              }
  95  
  96              // If there is no checked_out or checked_out_time field, just return true.
  97              if (!$table->hasField('checked_out') || !$table->hasField('checked_out_time')) {
  98                  return true;
  99              }
 100  
 101              $checkedOutField = $table->getColumnAlias('checked_out');
 102  
 103              // Check if this is the user having previously checked out the row.
 104              if ($table->$checkedOutField > 0 && $table->$checkedOutField != $user->get('id') && !$user->authorise('core.manage', 'com_checkin')) {
 105                  $this->setError(Text::_('JLIB_APPLICATION_ERROR_CHECKIN_USER_MISMATCH'));
 106  
 107                  return false;
 108              }
 109  
 110              // Attempt to check the row in.
 111              if (!$table->checkIn($pk)) {
 112                  $this->setError($table->getError());
 113  
 114                  return false;
 115              }
 116          }
 117  
 118          return true;
 119      }
 120  
 121      /**
 122       * Method to check-out a row for editing.
 123       *
 124       * @param   integer  $pk  The numeric id of the primary key.
 125       *
 126       * @return  boolean  False on failure or error, true otherwise.
 127       *
 128       * @since   1.6
 129       */
 130      public function checkout($pk = null)
 131      {
 132          // Only attempt to check the row in if it exists.
 133          if ($pk) {
 134              // Get an instance of the row to checkout.
 135              $table = $this->getTable();
 136  
 137              if (!$table->load($pk)) {
 138                  if ($table->getError() === false) {
 139                      // There was no error returned, but false indicates that the row did not exist in the db, so probably previously deleted.
 140                      $this->setError(Text::_('JLIB_APPLICATION_ERROR_NOT_EXIST'));
 141                  } else {
 142                      $this->setError($table->getError());
 143                  }
 144  
 145                  return false;
 146              }
 147  
 148              // If there is no checked_out or checked_out_time field, just return true.
 149              if (!$table->hasField('checked_out') || !$table->hasField('checked_out_time')) {
 150                  return true;
 151              }
 152  
 153              $user            = $this->getCurrentUser();
 154              $checkedOutField = $table->getColumnAlias('checked_out');
 155  
 156              // Check if this is the user having previously checked out the row.
 157              if ($table->$checkedOutField > 0 && $table->$checkedOutField != $user->get('id')) {
 158                  $this->setError(Text::_('JLIB_APPLICATION_ERROR_CHECKOUT_USER_MISMATCH'));
 159  
 160                  return false;
 161              }
 162  
 163              // Attempt to check the row out.
 164              if (!$table->checkOut($user->get('id'), $pk)) {
 165                  $this->setError($table->getError());
 166  
 167                  return false;
 168              }
 169          }
 170  
 171          return true;
 172      }
 173  
 174      /**
 175       * Method to validate the form data.
 176       *
 177       * @param   Form    $form   The form to validate against.
 178       * @param   array   $data   The data to validate.
 179       * @param   string  $group  The name of the field group to validate.
 180       *
 181       * @return  array|boolean  Array of filtered data if valid, false otherwise.
 182       *
 183       * @see     FormRule
 184       * @see     InputFilter
 185       * @since   1.6
 186       */
 187      public function validate($form, $data, $group = null)
 188      {
 189          // Include the plugins for the delete events.
 190          PluginHelper::importPlugin($this->events_map['validate']);
 191  
 192          $dispatcher = Factory::getContainer()->get('dispatcher');
 193  
 194          if (!empty($dispatcher->getListeners('onUserBeforeDataValidation'))) {
 195              @trigger_error(
 196                  'The `onUserBeforeDataValidation` event is deprecated and will be removed in 5.0.'
 197                  . 'Use the `onContentValidateData` event instead.',
 198                  E_USER_DEPRECATED
 199              );
 200  
 201              Factory::getApplication()->triggerEvent('onUserBeforeDataValidation', array($form, &$data));
 202          }
 203  
 204          Factory::getApplication()->triggerEvent('onContentBeforeValidateData', array($form, &$data));
 205  
 206          // Filter and validate the form data.
 207          $return = $form->process($data, $group);
 208  
 209          // Check for an error.
 210          if ($return instanceof \Exception) {
 211              $this->setError($return->getMessage());
 212  
 213              return false;
 214          }
 215  
 216          // Check the validation results.
 217          if ($return === false) {
 218              // Get the validation messages from the form.
 219              foreach ($form->getErrors() as $message) {
 220                  $this->setError($message);
 221              }
 222  
 223              return false;
 224          }
 225  
 226          $data = $return;
 227  
 228          // Tags B/C break at 3.1.2
 229          if (!isset($data['tags']) && isset($data['metadata']['tags'])) {
 230              $data['tags'] = $data['metadata']['tags'];
 231          }
 232  
 233          return $data;
 234      }
 235  }


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