diff --git a/core/lib/Thelia/Action/Customer.php b/core/lib/Thelia/Action/Customer.php index 2e18872f6..3bfedc5ca 100755 --- a/core/lib/Thelia/Action/Customer.php +++ b/core/lib/Thelia/Action/Customer.php @@ -60,6 +60,27 @@ class Customer extends BaseAction implements EventSubscriberInterface } + public function updateProfil(CustomerCreateOrUpdateEvent $event) + { + + $customer = $event->getCustomer(); + + $customer->setDispatcher($this->getDispatcher()); + + $customer + ->setTitleId($event->getTitle()) + ->setFirstname($event->getFirstname()) + ->setLastname($event->getLastname()) + ->setEmail($event->getEmail()) + ->setPassword($event->getPassword()) + ->setReseller($event->getReseller()) + ->setSponsor($event->getSponsor()) + ->setDiscount($event->getDiscount()) + ->save(); + + $event->setCustomer($customer); + } + public function delete(CustomerEvent $event) { $customer = $event->getCustomer(); @@ -110,11 +131,6 @@ class Customer extends BaseAction implements EventSubscriberInterface $this->getSecurityContext()->clearCustomerUser(); } - public function changePassword(ActionEvent $event) - { - // TODO - } - /** * Return the security context * @@ -150,6 +166,7 @@ class Customer extends BaseAction implements EventSubscriberInterface return array( TheliaEvents::CUSTOMER_CREATEACCOUNT => array('create', 128), TheliaEvents::CUSTOMER_UPDATEACCOUNT => array('modify', 128), + TheliaEvents::CUSTOMER_UPDATEPROFIL => array('updateProfil', 128), TheliaEvents::CUSTOMER_LOGOUT => array('logout', 128), TheliaEvents::CUSTOMER_LOGIN => array('login', 128), TheliaEvents::CUSTOMER_DELETEACCOUNT => array('delete', 128), diff --git a/core/lib/Thelia/Action/Newsletter.php b/core/lib/Thelia/Action/Newsletter.php index fa2e54f09..905cf48b0 100644 --- a/core/lib/Thelia/Action/Newsletter.php +++ b/core/lib/Thelia/Action/Newsletter.php @@ -49,6 +49,11 @@ class Newsletter extends BaseAction implements EventSubscriberInterface ->save(); } + public function unsubscribe(NewsletterEvent $event) + { + // TODO + } + /** * Returns an array of event names this subscriber wants to listen to. * @@ -72,7 +77,8 @@ class Newsletter extends BaseAction implements EventSubscriberInterface public static function getSubscribedEvents() { return array( - TheliaEvents::NEWSLETTER_SUBSCRIBE => array('subscribe', 128) + TheliaEvents::NEWSLETTER_SUBSCRIBE => array('subscribe', 128), + TheliaEvents::NEWSLETTER_UNSUBSCRIBE => array('unsubscribe', 128) ); } } \ No newline at end of file diff --git a/core/lib/Thelia/Controller/Front/CustomerController.php b/core/lib/Thelia/Controller/Front/CustomerController.php index fa5a9e3a3..c02a36e57 100755 --- a/core/lib/Thelia/Controller/Front/CustomerController.php +++ b/core/lib/Thelia/Controller/Front/CustomerController.php @@ -32,11 +32,11 @@ use Thelia\Form\CustomerCreateForm; use Thelia\Form\CustomerLogin; use Thelia\Form\CustomerLostPasswordForm; use Thelia\Form\CustomerPasswordUpdateForm; -use Thelia\Form\CustomerUpdateForm; +use Thelia\Form\CustomerProfilUpdateForm; use Thelia\Form\Exception\FormValidationException; use Thelia\Model\Customer; use Thelia\Core\Event\TheliaEvents; -use Thelia\Model\CustomerQuery; +use Thelia\Model\NewsletterQuery; use Thelia\Tools\URL; use Thelia\Log\Tlog; use Thelia\Core\Security\Exception\WrongPasswordException; @@ -131,12 +131,6 @@ class CustomerController extends BaseFrontController } } - protected function getExistingCustomer($customer_id) - { - return CustomerQuery::create() - ->findOneById($customer_id); - } - /** * Update customer data. On success, redirect to success_url if exists. * Otherwise, display the same view again. @@ -152,12 +146,13 @@ class CustomerController extends BaseFrontController 'firstname' => $customer->getFirstName(), 'lastname' => $customer->getLastName(), 'email' => $customer->getEmail(), + 'newsletter' => NewsletterQuery::isSubscribed($customer->getEmail()) ); - $customerUpdateForm = new CustomerUpdateForm($this->getRequest(), 'form', $data); + $customerProfilUpdateForm = new CustomerProfilUpdateForm($this->getRequest(), 'form', $data); // Pass it to the parser - $this->getParserContext()->addForm($customerUpdateForm); + $this->getParserContext()->addForm($customerProfilUpdateForm); } @@ -175,7 +170,7 @@ class CustomerController extends BaseFrontController $customerChangeEvent = $this->createEventInstance($form->getData()); $customerChangeEvent->setCustomer($customer); - //$this->dispatch(TheliaEvents::CUSTOMER_UPDATEACCOUNT, $customerChangeEvent); + $this->dispatch(TheliaEvents::CUSTOMER_UPDATEPROFIL, $customerChangeEvent); $this->redirectSuccess($customerPasswordUpdateForm); @@ -204,21 +199,27 @@ class CustomerController extends BaseFrontController $message = false; - $customerUpdateForm = new CustomerUpdateForm($this->getRequest()); + $customerProfilUpdateForm = new CustomerProfilUpdateForm($this->getRequest()); try { - $customer = $this->getSecurityContext()->getCustomerUser(); - $form = $this->validateForm($customerUpdateForm, "post"); + $form = $this->validateForm($customerProfilUpdateForm, "post"); $customerChangeEvent = $this->createEventInstance($form->getData()); $customerChangeEvent->setCustomer($customer); - $this->dispatch(TheliaEvents::CUSTOMER_UPDATEACCOUNT, $customerChangeEvent); + $this->dispatch(TheliaEvents::CUSTOMER_UPDATEPROFIL, $customerChangeEvent); + + // Newsletter + if ($form->get('newsletter')->getData()){ + //$this->dispatch(TheliaEvents::NEWSLETTER_SUBSCRIBE, $customerChangeEvent); + } else { + //$this->dispatch(TheliaEvents::NEWSLETTER_UNSUBSCRIBE, $customerChangeEvent); + } $this->processLogin($customerChangeEvent->getCustomer()); - $this->redirectSuccess($customerUpdateForm); + $this->redirectSuccess($customerProfilUpdateForm); } catch (FormValidationException $e) { $message = sprintf("Please check your input: %s", $e->getMessage()); @@ -229,10 +230,10 @@ class CustomerController extends BaseFrontController if ($message !== false) { Tlog::getInstance()->error(sprintf("Error during customer modification process : %s.", $message)); - $customerUpdateForm->setErrorMessage($message); + $customerProfilUpdateForm->setErrorMessage($message); $this->getParserContext() - ->addForm($customerUpdateForm) + ->addForm($customerProfilUpdateForm) ->setGeneralError($message) ; } @@ -329,9 +330,9 @@ class CustomerController extends BaseFrontController private function createEventInstance($data) { $customerCreateEvent = new CustomerCreateOrUpdateEvent( - $data["title"], - $data["firstname"], - $data["lastname"], + isset($data["title"])?$data["title"]:null, + isset($data["firstname"])?$data["firstname"]:null, + isset($data["lastname"])?$data["lastname"]:null, isset($data["address1"])?$data["address1"]:null, isset($data["address2"])?$data["address2"]:null, isset($data["address3"])?$data["address3"]:null, diff --git a/core/lib/Thelia/Core/Event/TheliaEvents.php b/core/lib/Thelia/Core/Event/TheliaEvents.php index 731c03f23..6c44638d7 100755 --- a/core/lib/Thelia/Core/Event/TheliaEvents.php +++ b/core/lib/Thelia/Core/Event/TheliaEvents.php @@ -71,6 +71,11 @@ final class TheliaEvents */ const CUSTOMER_UPDATEACCOUNT = "action.updateCustomer"; + /** + * sent on customer account update profil + */ + const CUSTOMER_UPDATEPROFIL = "action.updateProfilCustomer"; + /** * sent on customer removal */ @@ -686,4 +691,9 @@ final class TheliaEvents * sent for subscribing to the newsletter */ const NEWSLETTER_SUBSCRIBE = 'thelia.newsletter.subscribe'; + + /** + * sent for subscribing to the newsletter + */ + const NEWSLETTER_UNSUBSCRIBE = 'thelia.newsletter.unsubscribe'; } diff --git a/core/lib/Thelia/Form/CustomerCreateForm.php b/core/lib/Thelia/Form/CustomerCreateForm.php index 2860b3b0c..38da394cc 100755 --- a/core/lib/Thelia/Form/CustomerCreateForm.php +++ b/core/lib/Thelia/Form/CustomerCreateForm.php @@ -111,7 +111,7 @@ class CustomerCreateForm extends AddressCreateForm public function verifyExistingEmail($value, ExecutionContextInterface $context) { - $customer = CustomerQuery::create()->findOneByEmail($value); + $customer = CustomerQuery::getCustomerByEmail($value); if ($customer) { $context->addViolation("This email already exists."); } diff --git a/core/lib/Thelia/Form/CustomerPasswordUpdateForm.php b/core/lib/Thelia/Form/CustomerPasswordUpdateForm.php index 78e1218ff..3b4cfec40 100755 --- a/core/lib/Thelia/Form/CustomerPasswordUpdateForm.php +++ b/core/lib/Thelia/Form/CustomerPasswordUpdateForm.php @@ -26,6 +26,7 @@ use Symfony\Component\Validator\Constraints; use Symfony\Component\Validator\ExecutionContextInterface; use Thelia\Model\ConfigQuery; use Thelia\Core\Translation\Translator; +use Thelia\Model\CustomerQuery; /** * Class CustomerPasswordUpdateForm @@ -79,7 +80,10 @@ class CustomerPasswordUpdateForm extends BaseForm public function verifyCurrentPasswordField($value, ExecutionContextInterface $context) { - // Check current password + // Check if value of the old password match the password of the current user + if (!password_verify($value, $this->getRequest()->getSession()->getCustomerUser()->getPassword())) { + $context->addViolation("Your current password does not match."); + } } public function verifyPasswordField($value, ExecutionContextInterface $context) diff --git a/core/lib/Thelia/Form/CustomerProfilUpdateForm.php b/core/lib/Thelia/Form/CustomerProfilUpdateForm.php index 74e888e5b..210d6edbc 100755 --- a/core/lib/Thelia/Form/CustomerProfilUpdateForm.php +++ b/core/lib/Thelia/Form/CustomerProfilUpdateForm.php @@ -23,6 +23,8 @@ namespace Thelia\Form; use Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\ExecutionContextInterface; +use Thelia\Model\CustomerQuery; use Thelia\Model\ConfigQuery; use Thelia\Core\Translation\Translator; @@ -51,6 +53,9 @@ class CustomerProfilUpdateForm extends CustomerCreateForm ->remove("city") ->remove("zipcode") ->remove("country") + // Remove Login Information + ->remove("password") + ->remove("password_confirm") // Remove Terms & conditions ->remove("agreed") @@ -64,6 +69,20 @@ class CustomerProfilUpdateForm extends CustomerCreateForm )); } + + /** + * @param $value + * @param ExecutionContextInterface $context + */ + public function verifyExistingEmail($value, ExecutionContextInterface $context) + { + $customer = CustomerQuery::getCustomerByEmail($value); + // If there is already a customer for this email address and if the customer is different from the current user, do a violation + if ($customer && $customer->getId() != $this->getRequest()->getSession()->getCustomerUser()->getId()) { + $context->addViolation("This email already exists."); + } + } + public function getName() { return "thelia_customer_profil_update"; diff --git a/core/lib/Thelia/Model/CustomerQuery.php b/core/lib/Thelia/Model/CustomerQuery.php index 8cf373af6..359d072bd 100755 --- a/core/lib/Thelia/Model/CustomerQuery.php +++ b/core/lib/Thelia/Model/CustomerQuery.php @@ -17,4 +17,8 @@ use Thelia\Model\Base\CustomerQuery as BaseCustomerQuery; */ class CustomerQuery extends BaseCustomerQuery { + public static function getCustomerByEmail($email) + { + return self::create()->findOneByEmail($email); + } } // CustomerQuery diff --git a/core/lib/Thelia/Model/NewsletterQuery.php b/core/lib/Thelia/Model/NewsletterQuery.php index 26b4d2614..d1577b73f 100644 --- a/core/lib/Thelia/Model/NewsletterQuery.php +++ b/core/lib/Thelia/Model/NewsletterQuery.php @@ -17,5 +17,8 @@ use Thelia\Model\Base\NewsletterQuery as BaseNewsletterQuery; */ class NewsletterQuery extends BaseNewsletterQuery { - + public static function isSubscribed($email) + { + return (null === self::create()->findOneByEmail($email)) ? false : true; + } } // NewsletterQuery diff --git a/templates/default/account-password.html b/templates/default/account-password.html index a60a2f72a..5b14ac1f6 100644 --- a/templates/default/account-password.html +++ b/templates/default/account-password.html @@ -14,9 +14,6 @@ {block name="main-content"} - {* Used to display the success icon *} - {assign var="isPost" value="{$smarty.post|count}"} -