[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Site
   5   * @subpackage  com_users
   6   *
   7   * @copyright   (C) 2010 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\Users\Site\Model;
  12  
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Form\Form;
  15  use Joomla\CMS\Language\Text;
  16  use Joomla\CMS\Log\Log;
  17  use Joomla\CMS\Mail\MailTemplate;
  18  use Joomla\CMS\MVC\Model\FormModel;
  19  use Joomla\CMS\Router\Route;
  20  use Joomla\CMS\String\PunycodeHelper;
  21  use Joomla\Utilities\ArrayHelper;
  22  
  23  // phpcs:disable PSR1.Files.SideEffects
  24  \defined('_JEXEC') or die;
  25  // phpcs:enable PSR1.Files.SideEffects
  26  
  27  /**
  28   * Remind model class for Users.
  29   *
  30   * @since  1.5
  31   */
  32  class RemindModel extends FormModel
  33  {
  34      /**
  35       * Method to get the username remind request form.
  36       *
  37       * @param   array    $data      An optional array of data for the form to interrogate.
  38       * @param   boolean  $loadData  True if the form is to load its own data (default case), false if not.
  39       *
  40       * @return  Form|bool A Form object on success, false on failure
  41       *
  42       * @since   1.6
  43       */
  44      public function getForm($data = array(), $loadData = true)
  45      {
  46          // Get the form.
  47          $form = $this->loadForm('com_users.remind', 'remind', array('control' => 'jform', 'load_data' => $loadData));
  48  
  49          if (empty($form)) {
  50              return false;
  51          }
  52  
  53          return $form;
  54      }
  55  
  56      /**
  57       * Override preprocessForm to load the user plugin group instead of content.
  58       *
  59       * @param   Form    $form   A Form object.
  60       * @param   mixed   $data   The data expected for the form.
  61       * @param   string  $group  The name of the plugin group to import (defaults to "content").
  62       *
  63       * @return  void
  64       *
  65       * @throws  \Exception if there is an error in the form event.
  66       *
  67       * @since   1.6
  68       */
  69      protected function preprocessForm(Form $form, $data, $group = 'user')
  70      {
  71          parent::preprocessForm($form, $data, 'user');
  72      }
  73  
  74      /**
  75       * Method to auto-populate the model state.
  76       *
  77       * Note. Calling getState in this method will result in recursion.
  78       *
  79       * @return  void
  80       *
  81       * @since   1.6
  82       *
  83       * @throws  \Exception
  84       */
  85      protected function populateState()
  86      {
  87          // Get the application object.
  88          $app = Factory::getApplication();
  89          $params = $app->getParams('com_users');
  90  
  91          // Load the parameters.
  92          $this->setState('params', $params);
  93      }
  94  
  95      /**
  96       * Send the remind username email
  97       *
  98       * @param   array  $data  Array with the data received from the form
  99       *
 100       * @return  boolean
 101       *
 102       * @since   1.6
 103       */
 104      public function processRemindRequest($data)
 105      {
 106          // Get the form.
 107          $form = $this->getForm();
 108          $data['email'] = PunycodeHelper::emailToPunycode($data['email']);
 109  
 110          // Check for an error.
 111          if (empty($form)) {
 112              return false;
 113          }
 114  
 115          // Validate the data.
 116          $data = $this->validate($form, $data);
 117  
 118          // Check for an error.
 119          if ($data instanceof \Exception) {
 120              return false;
 121          }
 122  
 123          // Check the validation results.
 124          if ($data === false) {
 125              // Get the validation messages from the form.
 126              foreach ($form->getErrors() as $formError) {
 127                  $this->setError($formError->getMessage());
 128              }
 129  
 130              return false;
 131          }
 132  
 133          // Find the user id for the given email address.
 134          $db = $this->getDatabase();
 135          $query = $db->getQuery(true)
 136              ->select('*')
 137              ->from($db->quoteName('#__users'))
 138              ->where('LOWER(' . $db->quoteName('email') . ') = LOWER(:email)')
 139              ->bind(':email', $data['email']);
 140  
 141          // Get the user id.
 142          $db->setQuery($query);
 143  
 144          try {
 145              $user = $db->loadObject();
 146          } catch (\RuntimeException $e) {
 147              $this->setError(Text::sprintf('COM_USERS_DATABASE_ERROR', $e->getMessage()));
 148  
 149              return false;
 150          }
 151  
 152          // Check for a user.
 153          if (empty($user)) {
 154              $this->setError(Text::_('COM_USERS_USER_NOT_FOUND'));
 155  
 156              return false;
 157          }
 158  
 159          // Make sure the user isn't blocked.
 160          if ($user->block) {
 161              $this->setError(Text::_('COM_USERS_USER_BLOCKED'));
 162  
 163              return false;
 164          }
 165  
 166          $app = Factory::getApplication();
 167  
 168          // Assemble the login link.
 169          $link = 'index.php?option=com_users&view=login';
 170          $mode = $app->get('force_ssl', 0) == 2 ? 1 : (-1);
 171  
 172          // Put together the email template data.
 173          $data = ArrayHelper::fromObject($user);
 174          $data['sitename'] = $app->get('sitename');
 175          $data['link_text'] = Route::_($link, false, $mode);
 176          $data['link_html'] = Route::_($link, true, $mode);
 177  
 178          $mailer = new MailTemplate('com_users.reminder', $app->getLanguage()->getTag());
 179          $mailer->addTemplateData($data);
 180          $mailer->addRecipient($user->email, $user->name);
 181  
 182          // Try to send the password reset request email.
 183          try {
 184              $return = $mailer->send();
 185          } catch (\Exception $exception) {
 186              try {
 187                  Log::add(Text::_($exception->getMessage()), Log::WARNING, 'jerror');
 188  
 189                  $return = false;
 190              } catch (\RuntimeException $exception) {
 191                  Factory::getApplication()->enqueueMessage(Text::_($exception->errorMessage()), 'warning');
 192  
 193                  $return = false;
 194              }
 195          }
 196  
 197          // Check for an error.
 198          if ($return !== true) {
 199              $this->setError(Text::_('COM_USERS_MAIL_FAILED'));
 200  
 201              return false;
 202          }
 203  
 204          Factory::getApplication()->triggerEvent('onUserAfterRemind', array($user));
 205  
 206          return true;
 207      }
 208  }


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