From e493fd08c7746e5273beaa73de2caf67ace661dd Mon Sep 17 00:00:00 2001 From: Nicola Galgano Date: Tue, 17 Apr 2018 18:25:27 +0200 Subject: [PATCH] insert privacy consent note only one --- .../user/privacyconsent/privacyconsent.php | 84 ++++++++++--------- 1 file changed, 46 insertions(+), 38 deletions(-) diff --git a/plugins/user/privacyconsent/privacyconsent.php b/plugins/user/privacyconsent/privacyconsent.php index ce5c2a239063c..1d826501d2331 100644 --- a/plugins/user/privacyconsent/privacyconsent.php +++ b/plugins/user/privacyconsent/privacyconsent.php @@ -202,48 +202,56 @@ public function onUserAfterSave($data, $isNew, $result, $error) return; } - // Get the user's ID - $userId = ArrayHelper::getValue($data, 'id', 0, 'int'); - - // Get the user's IP address - $ip = $this->app->input->server->get('REMOTE_ADDR', '', 'string'); - - // Get the user agent string - $userAgent = $this->app->input->server->get('HTTP_USER_AGENT', '', 'string'); - - // Create the user note - $userNote = (object) array( - 'user_id' => $userId, - 'catid' => 0, - 'subject' => Text::_('PLG_USER_PRIVACYCONSENT_SUBJECT'), - 'body' => Text::sprintf('PLG_USER_PRIVACYCONSENT_BODY', $ip, $userAgent), - 'state' => 1, - 'created_time' => Factory::getDate()->toSql(), - ); + $form = $this->app->input->post->get('jform', array(), 'array'); - try - { - $this->db->insertObject('#__user_notes', $userNote); - } - catch (Exception $e) + if (isset($form['privacyconsent']['privacy']) && ($form['privacyconsent']['privacy'])) { - // Do nothing if the save fails - } + // Get the user's ID + $userId = ArrayHelper::getValue($data, 'id', 0, 'int'); - // Create the consent confirmation - $confirm = (object) array( - 'user_id' => $userId, - 'profile_key' => 'consent', - 'profile_value' => 1 - ); + // Get the user's IP address + $ip = $this->app->input->server->get('REMOTE_ADDR'); - try - { - $this->db->insertObject('#__user_profiles', $confirm); - } - catch (Exception $e) - { - // Do nothing if the save fails + // Get the user agent string + $user_agent = $this->app->input->server->get('HTTP_USER_AGENT'); + + // Get the date in DB format + $now = JFactory::getDate()->toSql(); + + // Create the user note + $userNote = (object) array( + 'user_id' => $userId, + 'catid' => 0, + 'subject' => JText::_('PLG_USER_PRIVACY_SUBJECT'), + 'body' => JText::sprintf('PLG_USER_PRIVACY_BODY', $ip, $user_agent), + 'state' => 1, + 'created_time' => $now + ); + + try + { + $result = JFactory::getDbo()->insertObject('#__user_notes', $userNote); + } + catch (Exception $e) + { + // Do nothing if the save fails + } + + // Create the consent confirmation + $confirm = (object) array( + 'user_id' => $userId, + 'profile_key' => 'consent', + 'profile_value' => 1 + ); + + try + { + $result = JFactory::getDbo()->insertObject('#__user_profiles', $confirm); + } + catch (Exception $e) + { + // Do nothing if the save fails + } } return true;