[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/plugins/user/terms/ -> terms.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Plugin
   5   * @subpackage  User.terms
   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   * @phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
  11   */
  12  
  13  use Joomla\CMS\Form\Form;
  14  use Joomla\CMS\Form\FormHelper;
  15  use Joomla\CMS\Language\Text;
  16  use Joomla\CMS\Plugin\CMSPlugin;
  17  use Joomla\Component\Actionlogs\Administrator\Model\ActionlogModel;
  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   * An example custom terms and conditions plugin.
  26   *
  27   * @since  3.9.0
  28   */
  29  class PlgUserTerms extends CMSPlugin
  30  {
  31      /**
  32       * Load the language file on instantiation.
  33       *
  34       * @var    boolean
  35       *
  36       * @since  3.9.0
  37       */
  38      protected $autoloadLanguage = true;
  39  
  40      /**
  41       * @var    \Joomla\CMS\Application\CMSApplication
  42       *
  43       * @since  3.9.0
  44       */
  45      protected $app;
  46  
  47      /**
  48       * @var    \Joomla\Database\DatabaseDriver
  49       *
  50       * @since  3.9.0
  51       */
  52      protected $db;
  53  
  54      /**
  55       * Adds additional fields to the user registration form
  56       *
  57       * @param   Form   $form  The form to be altered.
  58       * @param   mixed  $data  The associated data for the form.
  59       *
  60       * @return  boolean
  61       *
  62       * @since   3.9.0
  63       */
  64      public function onContentPrepareForm(Form $form, $data)
  65      {
  66          // Check we are manipulating a valid form - we only display this on user registration form.
  67          $name = $form->getName();
  68  
  69          if (!in_array($name, array('com_users.registration'))) {
  70              return true;
  71          }
  72  
  73          // Add the terms and conditions fields to the form.
  74          FormHelper::addFieldPrefix('Joomla\\Plugin\\User\\Terms\\Field');
  75          FormHelper::addFormPath(__DIR__ . '/forms');
  76          $form->loadFile('terms');
  77  
  78          $termsarticle = $this->params->get('terms_article');
  79          $termsnote    = $this->params->get('terms_note');
  80  
  81          // Push the terms and conditions article ID into the terms field.
  82          $form->setFieldAttribute('terms', 'article', $termsarticle, 'terms');
  83          $form->setFieldAttribute('terms', 'note', $termsnote, 'terms');
  84      }
  85  
  86      /**
  87       * Method is called before user data is stored in the database
  88       *
  89       * @param   array    $user   Holds the old user data.
  90       * @param   boolean  $isNew  True if a new user is stored.
  91       * @param   array    $data   Holds the new user data.
  92       *
  93       * @return  boolean
  94       *
  95       * @since   3.9.0
  96       * @throws  InvalidArgumentException on missing required data.
  97       */
  98      public function onUserBeforeSave($user, $isNew, $data)
  99      {
 100          // // Only check for front-end user registration
 101          if ($this->app->isClient('administrator')) {
 102              return true;
 103          }
 104  
 105          $userId = ArrayHelper::getValue($user, 'id', 0, 'int');
 106  
 107          // User already registered, no need to check it further
 108          if ($userId > 0) {
 109              return true;
 110          }
 111  
 112          // Check that the terms is checked if required ie only in registration from frontend.
 113          $option = $this->app->input->get('option');
 114          $task   = $this->app->input->post->get('task');
 115          $form   = $this->app->input->post->get('jform', [], 'array');
 116  
 117          if ($option == 'com_users' && in_array($task, ['registration.register']) && empty($form['terms']['terms'])) {
 118              throw new InvalidArgumentException(Text::_('PLG_USER_TERMS_FIELD_ERROR'));
 119          }
 120  
 121          return true;
 122      }
 123  
 124      /**
 125       * Saves user profile data
 126       *
 127       * @param   array    $data    entered user data
 128       * @param   boolean  $isNew   true if this is a new user
 129       * @param   boolean  $result  true if saving the user worked
 130       * @param   string   $error   error message
 131       *
 132       * @return  void
 133       *
 134       * @since   3.9.0
 135       */
 136      public function onUserAfterSave($data, $isNew, $result, $error): void
 137      {
 138          if (!$isNew || !$result) {
 139              return;
 140          }
 141  
 142          $userId = ArrayHelper::getValue($data, 'id', 0, 'int');
 143  
 144          $message = [
 145              'action'      => 'consent',
 146              'id'          => $userId,
 147              'title'       => $data['name'],
 148              'itemlink'    => 'index.php?option=com_users&task=user.edit&id=' . $userId,
 149              'userid'      => $userId,
 150              'username'    => $data['username'],
 151              'accountlink' => 'index.php?option=com_users&task=user.edit&id=' . $userId,
 152          ];
 153  
 154          /** @var ActionlogModel $model */
 155          $model = $this->app
 156              ->bootComponent('com_actionlogs')
 157              ->getMVCFactory()
 158              ->createModel('Actionlog', 'Administrator');
 159  
 160          $model->addLog([$message], 'PLG_USER_TERMS_LOGGING_CONSENT_TO_TERMS', 'plg_user_terms', $userId);
 161      }
 162  }


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