[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/components/com_config/src/Model/ -> FormModel.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Site
   5   * @subpackage  com_config
   6   *
   7   * @copyright   (C) 2013 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\Config\Site\Model;
  12  
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Form\Form;
  15  use Joomla\CMS\Language\Text;
  16  use Joomla\CMS\MVC\Model\FormModel as BaseForm;
  17  use Joomla\CMS\Plugin\PluginHelper;
  18  use Joomla\Utilities\ArrayHelper;
  19  
  20  // phpcs:disable PSR1.Files.SideEffects
  21  \defined('_JEXEC') or die;
  22  // phpcs:enable PSR1.Files.SideEffects
  23  
  24  /**
  25   * Prototype form model.
  26   *
  27   * @see    JForm
  28   * @see    \Joomla\CMS\Form\FormField
  29   * @see    \Joomla\CMS\Form\FormRule
  30   * @since  3.2
  31   */
  32  abstract class FormModel extends BaseForm
  33  {
  34      /**
  35       * Array of form objects.
  36       *
  37       * @var    array
  38       * @since  3.2
  39       */
  40      protected $forms = array();
  41  
  42      /**
  43       * Method to checkin a row.
  44       *
  45       * @param   integer  $pk  The numeric id of the primary key.
  46       *
  47       * @return  boolean  False on failure or error, true otherwise.
  48       *
  49       * @since   3.2
  50       * @throws  \RuntimeException
  51       */
  52      public function checkin($pk = null)
  53      {
  54          // Only attempt to check the row in if it exists.
  55          if ($pk) {
  56              $user = Factory::getUser();
  57  
  58              // Get an instance of the row to checkin.
  59              $table = $this->getTable();
  60  
  61              if (!$table->load($pk)) {
  62                  throw new \RuntimeException($table->getError());
  63              }
  64  
  65              // Check if this is the user has previously checked out the row.
  66              if (!is_null($table->checked_out) && $table->checked_out != $user->get('id') && !$user->authorise('core.admin', 'com_checkin')) {
  67                  throw new \RuntimeException($table->getError());
  68              }
  69  
  70              // Attempt to check the row in.
  71              if (!$table->checkIn($pk)) {
  72                  throw new \RuntimeException($table->getError());
  73              }
  74          }
  75  
  76          return true;
  77      }
  78  
  79      /**
  80       * Method to check-out a row for editing.
  81       *
  82       * @param   integer  $pk  The numeric id of the primary key.
  83       *
  84       * @return  boolean  False on failure or error, true otherwise.
  85       *
  86       * @since   3.2
  87       */
  88      public function checkout($pk = null)
  89      {
  90          // Only attempt to check the row in if it exists.
  91          if ($pk) {
  92              $user = Factory::getUser();
  93  
  94              // Get an instance of the row to checkout.
  95              $table = $this->getTable();
  96  
  97              if (!$table->load($pk)) {
  98                  throw new \RuntimeException($table->getError());
  99              }
 100  
 101              // Check if this is the user having previously checked out the row.
 102              if (!is_null($table->checked_out) && $table->checked_out != $user->get('id')) {
 103                  throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_CHECKOUT_USER_MISMATCH'));
 104              }
 105  
 106              // Attempt to check the row out.
 107              if (!$table->checkOut($user->get('id'), $pk)) {
 108                  throw new \RuntimeException($table->getError());
 109              }
 110          }
 111  
 112          return true;
 113      }
 114  
 115      /**
 116       * Method to get a form object.
 117       *
 118       * @param   string   $name     The name of the form.
 119       * @param   string   $source   The form source. Can be XML string if file flag is set to false.
 120       * @param   array    $options  Optional array of options for the form creation.
 121       * @param   boolean  $clear    Optional argument to force load a new form.
 122       * @param   string   $xpath    An optional xpath to search for the fields.
 123       *
 124       * @return  mixed  JForm object on success, False on error.
 125       *
 126       * @see     JForm
 127       * @since   3.2
 128       */
 129      protected function loadForm($name, $source = null, $options = array(), $clear = false, $xpath = false)
 130      {
 131          // Handle the optional arguments.
 132          $options['control'] = ArrayHelper::getValue($options, 'control', false);
 133  
 134          // Create a signature hash.
 135          $hash = sha1($source . serialize($options));
 136  
 137          // Check if we can use a previously loaded form.
 138          if (isset($this->_forms[$hash]) && !$clear) {
 139              return $this->_forms[$hash];
 140          }
 141  
 142          //  Register the paths for the form.
 143          Form::addFormPath(JPATH_SITE . '/components/com_config/forms');
 144          Form::addFormPath(JPATH_ADMINISTRATOR . '/components/com_config/forms');
 145  
 146          try {
 147              // Get the form.
 148              $form = Form::getInstance($name, $source, $options, false, $xpath);
 149  
 150              if (isset($options['load_data']) && $options['load_data']) {
 151                  // Get the data for the form.
 152                  $data = $this->loadFormData();
 153              } else {
 154                  $data = array();
 155              }
 156  
 157              // Allow for additional modification of the form, and events to be triggered.
 158              // We pass the data because plugins may require it.
 159              $this->preprocessForm($form, $data);
 160  
 161              // Load the data into the form after the plugins have operated.
 162              $form->bind($data);
 163          } catch (\Exception $e) {
 164              Factory::getApplication()->enqueueMessage($e->getMessage());
 165  
 166              return false;
 167          }
 168  
 169          // Store the form for later.
 170          $this->_forms[$hash] = $form;
 171  
 172          return $form;
 173      }
 174  
 175      /**
 176       * Method to get the data that should be injected in the form.
 177       *
 178       * @return  array    The default data is an empty array.
 179       *
 180       * @since   3.2
 181       */
 182      protected function loadFormData()
 183      {
 184          return array();
 185      }
 186  
 187      /**
 188       * Method to allow derived classes to preprocess the data.
 189       *
 190       * @param   string  $context  The context identifier.
 191       * @param   mixed   &$data    The data to be processed. It gets altered directly.
 192       * @param   string  $group    The name of the plugin group to import (defaults to "content").
 193       *
 194       * @return  void
 195       *
 196       * @since   3.2
 197       */
 198      protected function preprocessData($context, &$data, $group = 'content')
 199      {
 200          // Get the dispatcher and load the users plugins.
 201          PluginHelper::importPlugin('content');
 202  
 203          // Trigger the data preparation event.
 204          Factory::getApplication()->triggerEvent('onContentPrepareData', array($context, $data));
 205      }
 206  
 207      /**
 208       * Method to allow derived classes to preprocess the form.
 209       *
 210       * @param   Form    $form   A Form object.
 211       * @param   mixed   $data   The data expected for the form.
 212       * @param   string  $group  The name of the plugin group to import (defaults to "content").
 213       *
 214       * @return  void
 215       *
 216       * @see     \Joomla\CMS\Form\FormField
 217       * @since   3.2
 218       * @throws  \Exception if there is an error in the form event.
 219       */
 220      protected function preprocessForm(Form $form, $data, $group = 'content')
 221      {
 222          // Import the appropriate plugin group.
 223          PluginHelper::importPlugin($group);
 224  
 225          // Trigger the form preparation event.
 226          Factory::getApplication()->triggerEvent('onContentPrepareForm', array($form, $data));
 227      }
 228  
 229      /**
 230       * Method to validate the form data.
 231       *
 232       * @param   Form    $form   The form to validate against.
 233       * @param   array   $data   The data to validate.
 234       * @param   string  $group  The name of the field group to validate.
 235       *
 236       * @return  mixed  Array of filtered data if valid, false otherwise.
 237       *
 238       * @see     \Joomla\CMS\Form\FormRule
 239       * @see     JFilterInput
 240       * @since   3.2
 241       */
 242      public function validate($form, $data, $group = null)
 243      {
 244          // Filter and validate the form data.
 245          $data   = $form->filter($data);
 246          $return = $form->validate($data, $group);
 247  
 248          // Check for an error.
 249          if ($return instanceof \Exception) {
 250              Factory::getApplication()->enqueueMessage($return->getMessage(), 'error');
 251  
 252              return false;
 253          }
 254  
 255          // Check the validation results.
 256          if ($return === false) {
 257              // Get the validation messages from the form.
 258              foreach ($form->getErrors() as $message) {
 259                  if ($message instanceof \Exception) {
 260                      $message = $message->getMessage();
 261                  }
 262  
 263                  Factory::getApplication()->enqueueMessage($message, 'error');
 264              }
 265  
 266              return false;
 267          }
 268  
 269          return $data;
 270      }
 271  }


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