From c5e434bd018b8f58eca01ce9d76e80797edef785 Mon Sep 17 00:00:00 2001 From: gCharmes Date: Thu, 10 May 2012 19:51:09 +0000 Subject: [PATCH] [-] FO : #FSCFV-903 : Add Mail confirmation sending when subscribing to the newsletter via Accont creation or edition git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@15198 b9a71923-0436-4b27-9f14-aed3839534dd --- controllers/front/AuthController.php | 112 +++++++++++--------- controllers/front/IdentityController.php | 28 +++-- modules/blocknewsletter/blocknewsletter.php | 20 ++++ 3 files changed, 101 insertions(+), 59 deletions(-) diff --git a/controllers/front/AuthController.php b/controllers/front/AuthController.php index 54dacfd31..90d638f51 100644 --- a/controllers/front/AuthController.php +++ b/controllers/front/AuthController.php @@ -106,12 +106,12 @@ class AuthControllerCore extends FrontController $countries = Country::getCountries($this->context->language->id, true); $this->context->smarty->assign(array( - 'inOrderProcess' => true, - 'PS_GUEST_CHECKOUT_ENABLED' => Configuration::get('PS_GUEST_CHECKOUT_ENABLED'), - 'PS_REGISTRATION_PROCESS_TYPE' => Configuration::get('PS_REGISTRATION_PROCESS_TYPE'), - 'sl_country' => (int)Tools::getValue('id_country', Configuration::get('PS_COUNTRY_DEFAULT')), - 'countries' => $countries - )); + 'inOrderProcess' => true, + 'PS_GUEST_CHECKOUT_ENABLED' => Configuration::get('PS_GUEST_CHECKOUT_ENABLED'), + 'PS_REGISTRATION_PROCESS_TYPE' => Configuration::get('PS_REGISTRATION_PROCESS_TYPE'), + 'sl_country' => (int)Tools::getValue('id_country', Configuration::get('PS_COUNTRY_DEFAULT')), + 'countries' => $countries + )); } } @@ -127,17 +127,17 @@ class AuthControllerCore extends FrontController // Call a hook to display more information on form $this->context->smarty->assign(array( - 'HOOK_CREATE_ACCOUNT_FORM' => Hook::exec('displayCustomerAccountForm'), - 'HOOK_CREATE_ACCOUNT_TOP' => Hook::exec('displayCustomerAccountFormTop') - )); + 'HOOK_CREATE_ACCOUNT_FORM' => Hook::exec('displayCustomerAccountForm'), + 'HOOK_CREATE_ACCOUNT_TOP' => Hook::exec('displayCustomerAccountFormTop') + )); if ($this->ajax) { // Call a hook to display more information on form $this->context->smarty->assign(array( - 'PS_REGISTRATION_PROCESS_TYPE' => Configuration::get('PS_REGISTRATION_PROCESS_TYPE'), - 'genders' => Gender::getGenders() - )); + 'PS_REGISTRATION_PROCESS_TYPE' => Configuration::get('PS_REGISTRATION_PROCESS_TYPE'), + 'genders' => Gender::getGenders() + )); $return = array( 'hasError' => !empty($this->errors), @@ -168,13 +168,13 @@ class AuthControllerCore extends FrontController $days = Tools::dateDays(); $this->context->smarty->assign(array( - 'years' => $years, - 'sl_year' => (isset($selectedYears) ? $selectedYears : 0), - 'months' => $months, - 'sl_month' => (isset($selectedMonths) ? $selectedMonths : 0), - 'days' => $days, - 'sl_day' => (isset($selectedDays) ? $selectedDays : 0) - )); + 'years' => $years, + 'sl_year' => (isset($selectedYears) ? $selectedYears : 0), + 'months' => $months, + 'sl_month' => (isset($selectedMonths) ? $selectedMonths : 0), + 'days' => $days, + 'sl_day' => (isset($selectedDays) ? $selectedDays : 0) + )); } /** @@ -192,16 +192,16 @@ class AuthControllerCore extends FrontController * But for now it's a bug ! * @see : bug #6968 * @link:http://www.prestashop.com/bug_tracker/view/6968/ - elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) - { - $array = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']); - if (Validate::isLanguageIsoCode($array[0])) - { - $selectedCountry = Country::getByIso($array[0]); - if (!$selectedCountry) - $selectedCountry = (int)(Configuration::get('PS_COUNTRY_DEFAULT')); - } - }*/ + elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) + { + $array = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']); + if (Validate::isLanguageIsoCode($array[0])) + { + $selectedCountry = Country::getByIso($array[0]); + if (!$selectedCountry) + $selectedCountry = (int)(Configuration::get('PS_COUNTRY_DEFAULT')); + } + }*/ if (!isset($selectedCountry)) $selectedCountry = (int)(Configuration::get('PS_COUNTRY_DEFAULT')); @@ -210,11 +210,11 @@ class AuthControllerCore extends FrontController else $countries = Country::getCountries($this->context->language->id, true); $this->context->smarty->assign(array( - 'countries' => $countries, - 'PS_REGISTRATION_PROCESS_TYPE' => Configuration::get('PS_REGISTRATION_PROCESS_TYPE'), - 'sl_country' => (isset($selectedCountry) ? $selectedCountry : 0), - 'vat_management' => Configuration::get('VATNUMBER_MANAGEMENT') - )); + 'countries' => $countries, + 'PS_REGISTRATION_PROCESS_TYPE' => Configuration::get('PS_REGISTRATION_PROCESS_TYPE'), + 'sl_country' => (isset($selectedCountry) ? $selectedCountry : 0), + 'vat_management' => Configuration::get('VATNUMBER_MANAGEMENT') + )); } } @@ -229,7 +229,7 @@ class AuthControllerCore extends FrontController foreach ($addressFormat as $addressline) foreach (explode(' ', $addressline) as $addressItem) - $addressItems[] = trim($addressItem); + $addressItems[] = trim($addressItem); // Add missing require fields for a new user susbscription form foreach ($requireFormFieldsList as $fieldName) @@ -347,6 +347,26 @@ class AuthControllerCore extends FrontController $this->context->smarty->assign('authentification_error', $this->errors); } + /** + * Process the newsletter settings and set the customer infos. + * + * @param Customer $customer Reference on the customer Object. + * + * @note At this point, the email has been validated. + */ + protected function processCustomerNewsletter(&$customer) + { + if (Tools::getValue('newsletter')) + { + $customer->ip_registration_newsletter = pSQL(Tools::getRemoteAddr()); + $customer->newsletter_date_add = pSQL(date('Y-m-d H:i:s')); + + if ($module_newsletter = Module::getInstanceByName('blocknewsletter')) + if ($module_newsletter->active) + $module_newsletter->confirmSubscription(Tools::getValue('email')); + } + } + /** * Process submit on an account */ @@ -379,10 +399,7 @@ class AuthControllerCore extends FrontController if (!count($this->errors)) { if (Tools::isSubmit('newsletter')) - { - $customer->ip_registration_newsletter = pSQL(Tools::getRemoteAddr()); - $customer->newsletter_date_add = pSQL(date('Y-m-d H:i:s')); - } + $this->processCustomerNewsletter($customer); $customer->birthday = (empty($_POST['years']) ? '' : (int)$_POST['years'].'-'.(int)$_POST['months'].'-'.(int)$_POST['days']); $customer->active = 1; // New Guest customer @@ -414,9 +431,9 @@ class AuthControllerCore extends FrontController $this->context->cart->update(); Hook::exec('actionCustomerAccountAdd', array( - '_POST' => $_POST, - 'newCustomer' => $customer - )); + '_POST' => $_POST, + 'newCustomer' => $customer + )); if ($this->ajax) { $return = array( @@ -494,10 +511,7 @@ class AuthControllerCore extends FrontController if (Customer::customerExists(Tools::getValue('email'))) $this->errors[] = Tools::displayError('An account is already registered with this e-mail, please enter your password or request a new one.', false); if (Tools::isSubmit('newsletter')) - { - $customer->ip_registration_newsletter = pSQL(Tools::getRemoteAddr()); - $customer->newsletter_date_add = pSQL(date('Y-m-d H:i:s')); - } + $this->processCustomerNewsletter($customer); $customer->birthday = (empty($_POST['years']) ? '' : (int)$_POST['years'].'-'.(int)$_POST['months'].'-'.(int)$_POST['days']); if (!count($this->errors)) @@ -549,9 +563,9 @@ class AuthControllerCore extends FrontController // If a logged guest logs in as a customer, the cart secure key was already set and needs to be updated $this->context->cart->update(); Hook::exec('actionCustomerAccountAdd', array( - '_POST' => $_POST, - 'newCustomer' => $customer - )); + '_POST' => $_POST, + 'newCustomer' => $customer + )); if ($this->ajax) { $return = array( diff --git a/controllers/front/IdentityController.php b/controllers/front/IdentityController.php index e7bb2f1fb..4ef77d8dc 100644 --- a/controllers/front/IdentityController.php +++ b/controllers/front/IdentityController.php @@ -44,13 +44,15 @@ class IdentityControllerCore extends FrontController */ public function postProcess() { + $origin_newsletter = (bool)$this->customer->newsletter; + if (isset($_POST['years']) && isset($_POST['months']) && isset($_POST['days'])) $this->customer->birthday = (int)($_POST['years']).'-'.(int)($_POST['months']).'-'.(int)($_POST['days']); if (Tools::isSubmit('submitIdentity')) { if (!@checkdate(Tools::getValue('months'), Tools::getValue('days'), Tools::getValue('years')) && - !(Tools::getValue('months') == '' && Tools::getValue('days') == '' && Tools::getValue('years') == '')) + !(Tools::getValue('months') == '' && Tools::getValue('days') == '' && Tools::getValue('years') == '')) $this->errors[] = Tools::displayError('Invalid date of birth'); else { @@ -76,8 +78,14 @@ class IdentityControllerCore extends FrontController { $this->customer->id_default_group = (int)$prev_id_default_group; $this->customer->firstname = Tools::ucfirst(Tools::strtolower($this->customer->firstname)); + if (!isset($_POST['newsletter'])) $this->customer->newsletter = 0; + elseif (!$origin_newsletter && isset($_POST['newsletter'])) + if ($module_newsletter = Module::getInstanceByName('blocknewsletter')) + if ($module_newsletter->active) + $module_newsletter->confirmSubscription($this->customer->email); + if (!isset($_POST['optin'])) $this->customer->optin = 0; if (Tools::getValue('passwd')) @@ -113,15 +121,15 @@ class IdentityControllerCore extends FrontController /* Generate years, months and days */ $this->context->smarty->assign(array( - 'years' => Tools::dateYears(), - 'sl_year' => $birthday[0], - 'months' => Tools::dateMonths(), - 'sl_month' => $birthday[1], - 'days' => Tools::dateDays(), - 'sl_day' => $birthday[2], - 'errors' => $this->errors, - 'genders' => Gender::getGenders(), - )); + 'years' => Tools::dateYears(), + 'sl_year' => $birthday[0], + 'months' => Tools::dateMonths(), + 'sl_month' => $birthday[1], + 'days' => Tools::dateDays(), + 'sl_day' => $birthday[2], + 'errors' => $this->errors, + 'genders' => Gender::getGenders(), + )); $this->context->smarty->assign('newsletter', (int)Module::getInstanceByName('blocknewsletter')->active); diff --git a/modules/blocknewsletter/blocknewsletter.php b/modules/blocknewsletter/blocknewsletter.php index 58794685c..2eb581690 100644 --- a/modules/blocknewsletter/blocknewsletter.php +++ b/modules/blocknewsletter/blocknewsletter.php @@ -422,6 +422,26 @@ class Blocknewsletter extends Module return $this->l('Thank you for subscribing to our newsletter.'); } + /** + * Send the confirmation mails to the given $email address if needed. + * + * @param string $email Email where to send the confirmation + * + * @note the email has been verified and might not yet been registered. Called by AuthController::processCustomerNewsletter + * + */ + public function confirmSubscription($email) + { + if ($email) + { + if ($discount = Configuration::get('NW_VOUCHER_CODE')) + $this->sendVoucher($email, $discount); + + if (Configuration::get('NW_CONFIRMATION_EMAIL')) + $this->sendConfirmationEmail($email); + } + } + /** * Send an email containing a voucher code * @param string $email