From 42c41d4de1f664fdb7b17efd3054b287d3a43e33 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Wed, 3 Jul 2013 09:54:17 +0200 Subject: [PATCH 1/9] add some phpdoc --- core/lib/Thelia/Model/Customer.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/core/lib/Thelia/Model/Customer.php b/core/lib/Thelia/Model/Customer.php index 1aef516b7..e295bb215 100755 --- a/core/lib/Thelia/Model/Customer.php +++ b/core/lib/Thelia/Model/Customer.php @@ -27,6 +27,23 @@ class Customer extends BaseCustomer */ protected $dispatcher; + /** + * @param int $titleId customer title id (from customer_title table) + * @param string $firstname customer first name + * @param string $lastname customer last name + * @param string $address1 customer address + * @param string $address2 customer adress complement 1 + * @param string $address3 customer adress complement 2 + * @param string $phone customer phone number + * @param string $cellphone customer cellphone number + * @param string $zipcode customer zipcode + * @param int $countryId customer country id (from Country table) + * @param string $email customer email, must be unique + * @param string $plainPassword customer plain password, hash is made calling setPassword method + * @param int $reseller + * @param null $sponsor + * @param int $discount + */ public function createOrUpdate($titleId, $firstname, $lastname, $address1, $address2, $address3, $phone, $cellphone, $zipcode, $countryId, $email, $plainPassword, $reseller = 0, $sponsor = null, $discount = 0 ) { $this From ad8fa59e9a8a5b8f91a701d9750aaf74e61a598d Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Wed, 3 Jul 2013 10:11:40 +0200 Subject: [PATCH 2/9] put form in error if saving customer fails --- core/lib/Thelia/Action/Customer.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core/lib/Thelia/Action/Customer.php b/core/lib/Thelia/Action/Customer.php index d0c96eef5..31bfd16ab 100755 --- a/core/lib/Thelia/Action/Customer.php +++ b/core/lib/Thelia/Action/Customer.php @@ -68,9 +68,11 @@ class Customer implements EventSubscriberInterface ); } catch (\PropelException $e) { Tlog::getInstance()->error(sprintf('error during creating customer on action/createCustomer with message "%s"', $e->getMessage())); + $event->setFormError($form); } - echo "ok"; exit; + //Customer is create, he is automatically connected + } else { $event->setFormError($form); From 8b8ea994ef7012f282df201ed986e9064914ab16 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Wed, 3 Jul 2013 10:33:25 +0200 Subject: [PATCH 3/9] allow to reuse creation customer method for modification. Password is not mandatory --- core/lib/Thelia/Model/Customer.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/core/lib/Thelia/Model/Customer.php b/core/lib/Thelia/Model/Customer.php index e295bb215..ca3cc7882 100755 --- a/core/lib/Thelia/Model/Customer.php +++ b/core/lib/Thelia/Model/Customer.php @@ -2,6 +2,7 @@ namespace Thelia\Model; +use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Thelia\Core\Event\CustomRefEvent; use Thelia\Core\Event\TheliaEvents; @@ -39,12 +40,12 @@ class Customer extends BaseCustomer * @param string $zipcode customer zipcode * @param int $countryId customer country id (from Country table) * @param string $email customer email, must be unique - * @param string $plainPassword customer plain password, hash is made calling setPassword method + * @param string $plainPassword customer plain password, hash is made calling setPassword method. Not mandatory parameter but an exception is thrown if customer is new without password * @param int $reseller * @param null $sponsor * @param int $discount */ - public function createOrUpdate($titleId, $firstname, $lastname, $address1, $address2, $address3, $phone, $cellphone, $zipcode, $countryId, $email, $plainPassword, $reseller = 0, $sponsor = null, $discount = 0 ) + public function createOrUpdate($titleId, $firstname, $lastname, $address1, $address2, $address3, $phone, $cellphone, $zipcode, $countryId, $email, $plainPassword = null, $reseller = 0, $sponsor = null, $discount = 0 ) { $this ->setCustomerTitleId($titleId) @@ -86,8 +87,15 @@ class Customer extends BaseCustomer public function setPassword($password) { - $this->setAlgo("PASSWORD_BCRYPT"); - return parent::setPassword(password_hash($password, PASSWORD_BCRYPT)); + if ($this->isNew() && ($password === null || trim($password) == "")) { + throw new InvalidArgumentException("customer password is mandatory on creation"); + } + + if($password !== null && trim($password) != "") { + $this->setAlgo("PASSWORD_BCRYPT"); + return parent::setPassword(password_hash($password, PASSWORD_BCRYPT)); + } + } public function setDispatcher(EventDispatcherInterface $dispatcher) From bc6545e91486efe9493ea8e2df633071eeb6d96b Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Wed, 3 Jul 2013 14:38:06 +0200 Subject: [PATCH 4/9] create class form for customer modification --- core/lib/Thelia/Action/Customer.php | 40 +++++- core/lib/Thelia/Config/Resources/config.xml | 1 + core/lib/Thelia/Form/BaseForm.php | 2 +- core/lib/Thelia/Form/CustomerModification.php | 120 ++++++++++++++++++ 4 files changed, 161 insertions(+), 2 deletions(-) create mode 100644 core/lib/Thelia/Form/CustomerModification.php diff --git a/core/lib/Thelia/Action/Customer.php b/core/lib/Thelia/Action/Customer.php index 31bfd16ab..e3481bdce 100755 --- a/core/lib/Thelia/Action/Customer.php +++ b/core/lib/Thelia/Action/Customer.php @@ -28,7 +28,10 @@ use Thelia\Core\Event\ActionEvent; use Thelia\Core\Event\TheliaEvents; use Thelia\Form\BaseForm; use Thelia\Form\CustomerCreation; +use Thelia\Form\CustomerModification; +use Thelia\Model\Customer as CustomerModel; use Thelia\Log\Tlog; +use Thelia\Model\CustomerQuery; class Customer implements EventSubscriberInterface { @@ -50,7 +53,7 @@ class Customer implements EventSubscriberInterface if ($form->isValid()) { $data = $form->getData(); - $customer = new \Thelia\Model\Customer(); + $customer = new CustomerModel(); try { $customer->createOrUpdate( $data["title"], @@ -84,6 +87,41 @@ class Customer implements EventSubscriberInterface public function modify(ActionEvent $event) { + $request = $event->getRequest(); + + $customerModification = new CustomerModification($request); + + $form = $customerModification->getForm(); + + if ($request->isMethod("post")) { + $form->bind($request); + + if ($form->isValid()) { + $data = $form->getData(); + + $customer = CustomerQuery::create()->findPk(1); + try { + $data = $form->getData(); + $customer->createOrUpdate( + $data["title"], + $data["firstname"], + $data["lastname"], + $data["address1"], + $data["address2"], + $data["address3"], + $data["phone"], + $data["cellphone"], + $data["zipcode"], + $data["country"] + ); + } catch(\PropelException $e) { + Tlog::getInstance()->error(sprintf('error during modifying customer on action/modifyCustomer with message "%s"', $e->getMessage())); + $event->setFormError($form); + } + } else { + $event->setFormError($form); + } + } } diff --git a/core/lib/Thelia/Config/Resources/config.xml b/core/lib/Thelia/Config/Resources/config.xml index 4eb0217ae..6f9ec52f6 100755 --- a/core/lib/Thelia/Config/Resources/config.xml +++ b/core/lib/Thelia/Config/Resources/config.xml @@ -23,6 +23,7 @@
+ diff --git a/core/lib/Thelia/Form/BaseForm.php b/core/lib/Thelia/Form/BaseForm.php index 3a6d69cb3..0b034345b 100644 --- a/core/lib/Thelia/Form/BaseForm.php +++ b/core/lib/Thelia/Form/BaseForm.php @@ -89,7 +89,7 @@ abstract class BaseForm { * ), * "label" => "email", * "constraints" => array( - * new NotBlank() + * new \Symfony\Component\Validator\Constraints\NotBlank() * ) * ) * ) diff --git a/core/lib/Thelia/Form/CustomerModification.php b/core/lib/Thelia/Form/CustomerModification.php new file mode 100644 index 000000000..df699c873 --- /dev/null +++ b/core/lib/Thelia/Form/CustomerModification.php @@ -0,0 +1,120 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Form; + +use Symfony\Component\Validator\Constraints; +use Thelia\Model\Customer; + + +class CustomerModification extends BaseForm { + + /** + * + * in this function you add all the fields you need for your Form. + * Form this you have to call add method on $this->form attribute : + * + * $this->form->add("name", "text") + * ->add("email", "email", array( + * "attr" => array( + * "class" => "field" + * ), + * "label" => "email", + * "constraints" => array( + * new NotBlank() + * ) + * ) + * ) + * ->add('age', 'integer'); + * + * @return null + */ + protected function buildForm() + { + + $this->form + ->add("firstname", "text", array( + "constraints" => array( + new Constraints\NotBlank() + ), + "label" => "firstname" + )) + ->add("lastname", "text", array( + "constraints" => array( + new Constraints\NotBlank() + ), + "label" => "lastname" + )) + ->add("address1", "text", array( + "constraints" => array( + new Constraints\NotBlank() + ), + "label" => "address" + )) + ->add("address2", "text", array( + "label" => "Address Line 2" + )) + ->add("address3", "text", array( + "label" => "Address Line 3" + )) + ->add("phone", "text", array( + "label" => "phone" + )) + ->add("cellphone", "text", array( + "label" => "cellphone" + )) + ->add("zipcode", "text", array( + "constraints" => array( + new Constraints\NotBlank() + ), + "label" => "zipcode" + )) + ->add("city", "text", array( + "constraints" => array( + new Constraints\NotBlank() + ), + "label" => "city" + )) + ->add("country", "text", array( + "constraints" => array( + new Constraints\NotBlank() + ), + "label" => "country" + )) + ->add("title", "text", array( + "constraints" => array( + new Constraints\NotBlank() + ), + "label" => "title" + )) + ; + } + + /** + * @return string the name of you form. This name must be unique + */ + public function getName() + { + return "customerModification"; + } +} \ No newline at end of file From 89ca995f29b0d2f1c5eafa9829a70694c64263c7 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Wed, 3 Jul 2013 14:45:18 +0200 Subject: [PATCH 5/9] fix typo in phpdoc --- core/lib/Thelia/Form/BaseForm.php | 1 - core/lib/Thelia/Form/CustomerModification.php | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/core/lib/Thelia/Form/BaseForm.php b/core/lib/Thelia/Form/BaseForm.php index 0b034345b..b53747a5d 100644 --- a/core/lib/Thelia/Form/BaseForm.php +++ b/core/lib/Thelia/Form/BaseForm.php @@ -30,7 +30,6 @@ use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationExtension; use Symfony\Component\Form\Extension\Csrf\CsrfExtension; use Symfony\Component\Form\Extension\Csrf\CsrfProvider\SessionCsrfProvider; use Symfony\Component\Validator\Validation; -use Thelia\Form\Extension\NameFormExtension; use Thelia\Model\ConfigQuery; abstract class BaseForm { diff --git a/core/lib/Thelia/Form/CustomerModification.php b/core/lib/Thelia/Form/CustomerModification.php index df699c873..cb21be2fa 100644 --- a/core/lib/Thelia/Form/CustomerModification.php +++ b/core/lib/Thelia/Form/CustomerModification.php @@ -4,7 +4,7 @@ /* Thelia */ /* */ /* Copyright (c) OpenStudio */ -/* email : info@thelia.net */ +/* email : info@thelia.net */ /* web : http://www.thelia.net */ /* */ /* This program is free software; you can redistribute it and/or modify */ @@ -17,7 +17,7 @@ /* GNU General Public License for more details. */ /* */ /* You should have received a copy of the GNU General Public License */ -/* along with this program. If not, see . */ +/* along with this program. If not, see . */ /* */ /*************************************************************************************/ From 0d1e0c6a2c945ff152a9404d7e575f7a440ef71b Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Wed, 3 Jul 2013 15:05:08 +0200 Subject: [PATCH 6/9] fiw typo in schema model --- core/lib/Thelia/Action/Customer.php | 1 + core/lib/Thelia/Form/CustomerCreation.php | 2 ++ core/lib/Thelia/Model/Customer.php | 1 + install/thelia.sql | 4 ++-- local/config/schema.xml | 2 +- 5 files changed, 7 insertions(+), 3 deletions(-) diff --git a/core/lib/Thelia/Action/Customer.php b/core/lib/Thelia/Action/Customer.php index e3481bdce..94d27793a 100755 --- a/core/lib/Thelia/Action/Customer.php +++ b/core/lib/Thelia/Action/Customer.php @@ -55,6 +55,7 @@ class Customer implements EventSubscriberInterface $data = $form->getData(); $customer = new CustomerModel(); try { + \Thelia\Log\Tlog::getInstance()->debug($data); $customer->createOrUpdate( $data["title"], $data["firstname"], diff --git a/core/lib/Thelia/Form/CustomerCreation.php b/core/lib/Thelia/Form/CustomerCreation.php index e2816edc0..b151fc98d 100644 --- a/core/lib/Thelia/Form/CustomerCreation.php +++ b/core/lib/Thelia/Form/CustomerCreation.php @@ -117,12 +117,14 @@ class CustomerCreation extends BaseForm )) ->add("password", "password", array( "constraints" => array( + new Constraints\NotBlank(), new Constraints\Length(array("min" => ConfigQuery::read("password.length", 4))) ), "label" => "password" )) ->add("password_confirm", "password", array( "constraints" => array( + new Constraints\NotBlank(), new Constraints\Length(array("min" => ConfigQuery::read("password.length", 4))), new Constraints\Callback(array("methods" => array( array($this, "verifyPasswordField") diff --git a/core/lib/Thelia/Model/Customer.php b/core/lib/Thelia/Model/Customer.php index ca3cc7882..20146cd94 100755 --- a/core/lib/Thelia/Model/Customer.php +++ b/core/lib/Thelia/Model/Customer.php @@ -87,6 +87,7 @@ class Customer extends BaseCustomer public function setPassword($password) { + \Thelia\Log\Tlog::getInstance()->debug($password); if ($this->isNew() && ($password === null || trim($password) == "")) { throw new InvalidArgumentException("customer password is mandatory on creation"); } diff --git a/install/thelia.sql b/install/thelia.sql index 46a934a3b..94d933587 100755 --- a/install/thelia.sql +++ b/install/thelia.sql @@ -477,8 +477,8 @@ CREATE TABLE `customer` `updated_at` DATETIME, PRIMARY KEY (`id`), UNIQUE INDEX `ref_UNIQUE` (`ref`), - INDEX `idx_ customer_customer_title_id` (`customer_title_id`), - CONSTRAINT `fk_ customer_customer_title_id` + INDEX `idx_customer_customer_title_id` (`customer_title_id`), + CONSTRAINT `fk_customer_customer_title_id` FOREIGN KEY (`customer_title_id`) REFERENCES `customer_title` (`id`) ON UPDATE RESTRICT diff --git a/local/config/schema.xml b/local/config/schema.xml index f9063c614..8b97f4e8c 100755 --- a/local/config/schema.xml +++ b/local/config/schema.xml @@ -349,7 +349,7 @@ - + From b7a8648c108fe6ee44ed6a17f4c510af108ddefa Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Thu, 4 Jul 2013 18:10:08 +0200 Subject: [PATCH 7/9] start lang management --- core/lib/Thelia/Core/TheliaHttpKernel.php | 53 +++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/core/lib/Thelia/Core/TheliaHttpKernel.php b/core/lib/Thelia/Core/TheliaHttpKernel.php index c315662e0..e04009301 100755 --- a/core/lib/Thelia/Core/TheliaHttpKernel.php +++ b/core/lib/Thelia/Core/TheliaHttpKernel.php @@ -119,6 +119,59 @@ class TheliaHttpKernel extends HttpKernel */ protected function initParam(Request $request) { + $lang = $this->detectLang($request); + + if ($lang) { + $request->getSession()->set("lang", $lang->getCode()); + $request->getSession()->set("locale", $lang->getLocale()); + } + } + + /** + * @param Request $request + * @return null|\Thelia\Model\Lang + */ + protected function detectLang(Request $request) + { + $lang = null; + //first priority => lang parameter present in request (get or post) + if($request->query->has("lang")) { + $lang = Model\LangQuery::create()->findOneByCode($request->query->get("lang")); + + if(is_null($lang)) { + return; + } + + //if each lang had is own domain, we redirect the user to the good one. + if (Model\ConfigQuery::read("one_domain_foreach_lang", false) == 1) { + //if lang domain is different from actuel domain, redirect to the good one + if (rtrim($lang->getUrl(), "/") != $request->getSchemeAndHttpHost()) { + // TODO : search if http status 302 is the good one. + $redirect = new RedirectResponse($lang->getUrl(), 302); + $redirect->send(); + exit; + } else { + //the user is actually on the good domain, nothing to change + return null; + } + } else { + //one domain for all languages, the lang is set into session + return $lang; + } + } + + //check if lang is not defined. If not we have to search the good one. + if (null === $request->getSession()->get("lang")) { + + if (Model\ConfigQuery::read("one_domain_foreach_lang", false) == 1) { + //find lang with domain + return Model\LangQuery::create()->filterByUrl($request->getSchemeAndHttpHost(), \Criteria::LIKE)->findOne(); + } + + //find default lang + return Model\LangQuery::create()->findOneByByDefault(1); + + } } From ca44544f84549d0e6a65443239b491afdfaf810d Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Thu, 4 Jul 2013 18:21:24 +0200 Subject: [PATCH 8/9] manage lang for customer --- core/lib/Thelia/Action/Customer.php | 3 ++- core/lib/Thelia/Core/TheliaHttpKernel.php | 1 + core/lib/Thelia/Model/Customer.php | 9 +++++++-- templates/smarty-sample/index.html | 21 ++------------------- 4 files changed, 12 insertions(+), 22 deletions(-) diff --git a/core/lib/Thelia/Action/Customer.php b/core/lib/Thelia/Action/Customer.php index 94d27793a..98341ee28 100755 --- a/core/lib/Thelia/Action/Customer.php +++ b/core/lib/Thelia/Action/Customer.php @@ -68,7 +68,8 @@ class Customer implements EventSubscriberInterface $data["zipcode"], $data["country"], $data["email"], - $data["password"] + $data["password"], + $request->getSession()->get("lang") ); } catch (\PropelException $e) { Tlog::getInstance()->error(sprintf('error during creating customer on action/createCustomer with message "%s"', $e->getMessage())); diff --git a/core/lib/Thelia/Core/TheliaHttpKernel.php b/core/lib/Thelia/Core/TheliaHttpKernel.php index e04009301..0af91fa08 100755 --- a/core/lib/Thelia/Core/TheliaHttpKernel.php +++ b/core/lib/Thelia/Core/TheliaHttpKernel.php @@ -76,6 +76,7 @@ class TheliaHttpKernel extends HttpKernel { //$request->headers->set('X-Php-Ob-Level', ob_get_level()); $request = $this->initSession($request); + $this->initParam($request); $this->container->enterScope('request'); $this->container->set('request', $request, 'request'); diff --git a/core/lib/Thelia/Model/Customer.php b/core/lib/Thelia/Model/Customer.php index 20146cd94..197df4c7c 100755 --- a/core/lib/Thelia/Model/Customer.php +++ b/core/lib/Thelia/Model/Customer.php @@ -45,7 +45,7 @@ class Customer extends BaseCustomer * @param null $sponsor * @param int $discount */ - public function createOrUpdate($titleId, $firstname, $lastname, $address1, $address2, $address3, $phone, $cellphone, $zipcode, $countryId, $email, $plainPassword = null, $reseller = 0, $sponsor = null, $discount = 0 ) + public function createOrUpdate($titleId, $firstname, $lastname, $address1, $address2, $address3, $phone, $cellphone, $zipcode, $countryId, $email, $plainPassword = null, $lang = null, $reseller = 0, $sponsor = null, $discount = 0) { $this ->setCustomerTitleId($titleId) @@ -63,9 +63,14 @@ class Customer extends BaseCustomer ->setReseller($reseller) ->setSponsor($sponsor) ->setDiscount($discount) - ->save() ; + if(!is_null($lang)) { + $this->setLang($lang); + } + + $this->save(); + } public function preInsert(\PropelPDO $con = null) diff --git a/templates/smarty-sample/index.html b/templates/smarty-sample/index.html index d7c2b2e62..77ba1e978 100755 --- a/templates/smarty-sample/index.html +++ b/templates/smarty-sample/index.html @@ -13,22 +13,7 @@ An image from asset directory : jQuery data: -
-

Category loop example

-
    - {loop type="category" name="catloop1"} -
  • {$__COUNT__}/{$__TOTAL__} : {$ID} {$TITLE}, children: {$NB_CHILD} - {ifloop rel="inner1"} -
      - {loop type="category" name="inner1" parent="{$ID}"} -
    • Sub cat {$ID} (parent is {$PARENT}): {$TITLE}
    • - {/loop} -
    - {/ifloop} -
  • - {/loop} -
-
+

Conditional example #1

@@ -66,9 +51,7 @@ An image from asset directory : {elseloop rel="catloop2"}

... but catloop2 is still empty :-)

{/elseloop} - {ifloop rel="catloop1"} -

... and catloop1 is still NOT empty :-)

- {/ifloop} +
From abe60649551acbaee0f546b9b3140b3ffeb09b04 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Thu, 4 Jul 2013 18:47:20 +0200 Subject: [PATCH 9/9] allow to display result or not if translation is present --- core/lib/Thelia/Core/Template/Loop/Category.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/core/lib/Thelia/Core/Template/Loop/Category.php b/core/lib/Thelia/Core/Template/Loop/Category.php index 55be4d69a..90de19e1c 100755 --- a/core/lib/Thelia/Core/Template/Loop/Category.php +++ b/core/lib/Thelia/Core/Template/Loop/Category.php @@ -32,6 +32,7 @@ use Thelia\Core\Template\Loop\Argument\Argument; use Thelia\Log\Tlog; use Thelia\Model\CategoryQuery; +use Thelia\Model\ConfigQuery; use Thelia\Type\TypeCollection; use Thelia\Type; @@ -156,7 +157,10 @@ class Category extends BaseLoop * * @todo : verify here if we want results for row without translations. */ - $search->joinWithI18n($this->request->getSession()->get('locale', 'en_US'), \Criteria::INNER_JOIN); + $search->joinWithI18n( + $this->request->getSession()->get('locale', 'en_US'), + (ConfigQuery::read("default_lang_without_translation", 1)) ? \Criteria::LEFT_JOIN : \Criteria::INNER_JOIN + ); $categories = $this->search($search, $pagination);