[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/components/com_privacy/src/Model/ -> RemindModel.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Site
   5   * @subpackage  com_privacy
   6   *
   7   * @copyright   (C) 2018 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\Privacy\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\AdminModel;
  17  use Joomla\CMS\String\PunycodeHelper;
  18  use Joomla\CMS\Table\Table;
  19  use Joomla\CMS\User\UserHelper;
  20  use Joomla\Component\Privacy\Administrator\Table\ConsentTable;
  21  use Joomla\Database\Exception\ExecutionFailureException;
  22  
  23  // phpcs:disable PSR1.Files.SideEffects
  24  \defined('_JEXEC') or die;
  25  // phpcs:enable PSR1.Files.SideEffects
  26  
  27  /**
  28   * Remind confirmation model class.
  29   *
  30   * @since  3.9.0
  31   */
  32  class RemindModel extends AdminModel
  33  {
  34      /**
  35       * Confirms the remind request.
  36       *
  37       * @param   array  $data  The data expected for the form.
  38       *
  39       * @return  mixed  \Exception | JException | boolean
  40       *
  41       * @since   3.9.0
  42       */
  43      public function remindRequest($data)
  44      {
  45          // Get the form.
  46          $form = $this->getForm();
  47          $data['email'] = PunycodeHelper::emailToPunycode($data['email']);
  48  
  49          // Check for an error.
  50          if ($form instanceof \Exception) {
  51              return $form;
  52          }
  53  
  54          // Filter and validate the form data.
  55          $data = $form->filter($data);
  56          $return = $form->validate($data);
  57  
  58          // Check for an error.
  59          if ($return instanceof \Exception) {
  60              return $return;
  61          }
  62  
  63          // Check the validation results.
  64          if ($return === false) {
  65              // Get the validation messages from the form.
  66              foreach ($form->getErrors() as $formError) {
  67                  $this->setError($formError->getMessage());
  68              }
  69  
  70              return false;
  71          }
  72  
  73          /** @var ConsentTable $table */
  74          $table = $this->getTable();
  75  
  76          $db = $this->getDatabase();
  77          $query = $db->getQuery(true)
  78              ->select($db->quoteName(['r.id', 'r.user_id', 'r.token']));
  79          $query->from($db->quoteName('#__privacy_consents', 'r'));
  80          $query->join(
  81              'LEFT',
  82              $db->quoteName('#__users', 'u'),
  83              $db->quoteName('u.id') . ' = ' . $db->quoteName('r.user_id')
  84          );
  85          $query->where($db->quoteName('u.email') . ' = :email')
  86              ->bind(':email', $data['email']);
  87          $query->where($db->quoteName('r.remind') . ' = 1');
  88          $db->setQuery($query);
  89  
  90          try {
  91              $remind = $db->loadObject();
  92          } catch (ExecutionFailureException $e) {
  93              $this->setError(Text::_('COM_PRIVACY_ERROR_NO_PENDING_REMIND'));
  94  
  95              return false;
  96          }
  97  
  98          if (!$remind) {
  99              $this->setError(Text::_('COM_PRIVACY_ERROR_NO_PENDING_REMIND'));
 100  
 101              return false;
 102          }
 103  
 104          // Verify the token
 105          if (!UserHelper::verifyPassword($data['remind_token'], $remind->token)) {
 106              $this->setError(Text::_('COM_PRIVACY_ERROR_NO_REMIND_REQUESTS'));
 107  
 108              return false;
 109          }
 110  
 111          // Everything is good to go, transition the request to extended
 112          $saved = $this->save(
 113              [
 114                  'id'      => $remind->id,
 115                  'remind'  => 0,
 116                  'token'   => '',
 117                  'created' => Factory::getDate()->toSql(),
 118              ]
 119          );
 120  
 121          if (!$saved) {
 122              // Error was set by the save method
 123              return false;
 124          }
 125  
 126          return true;
 127      }
 128  
 129      /**
 130       * Method for getting the form from the model.
 131       *
 132       * @param   array    $data      Data for the form.
 133       * @param   boolean  $loadData  True if the form is to load its own data (default case), false if not.
 134       *
 135       * @return  Form|boolean  A Form object on success, false on failure
 136       *
 137       * @since   3.9.0
 138       */
 139      public function getForm($data = [], $loadData = true)
 140      {
 141          // Get the form.
 142          $form = $this->loadForm('com_privacy.remind', 'remind', ['control' => 'jform']);
 143  
 144          if (empty($form)) {
 145              return false;
 146          }
 147  
 148          $input = Factory::getApplication()->input;
 149  
 150          if ($input->getMethod() === 'GET') {
 151              $form->setValue('remind_token', '', $input->get->getAlnum('remind_token'));
 152          }
 153  
 154          return $form;
 155      }
 156  
 157      /**
 158       * Method to get a table object, load it if necessary.
 159       *
 160       * @param   string  $name     The table name. Optional.
 161       * @param   string  $prefix   The class prefix. Optional.
 162       * @param   array   $options  Configuration array for model. Optional.
 163       *
 164       * @return  Table  A Table object
 165       *
 166       * @throws  \Exception
 167       * @since   3.9.0
 168       */
 169      public function getTable($name = 'Consent', $prefix = 'Administrator', $options = [])
 170      {
 171          return parent::getTable($name, $prefix, $options);
 172      }
 173  
 174      /**
 175       * Method to auto-populate the model state.
 176       *
 177       * Note. Calling getState in this method will result in recursion.
 178       *
 179       * @return  void
 180       *
 181       * @since   3.9.0
 182       */
 183      protected function populateState()
 184      {
 185          // Get the application object.
 186          $params = Factory::getApplication()->getParams('com_privacy');
 187  
 188          // Load the parameters.
 189          $this->setState('params', $params);
 190      }
 191  }


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