From 7a2469cdc868fcbb1e966adb2a018322dcda85ea Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Mon, 28 Oct 2013 16:55:07 +0100 Subject: [PATCH] allow to create a customer in bakcoffice --- .../Thelia/Config/Resources/routing/admin.xml | 4 ++ .../Controller/Admin/CustomerController.php | 54 ++++++++++++++++++- core/lib/Thelia/Model/Customer.php | 1 + core/lib/Thelia/Tools/Password.php | 54 +++++++++++++++++++ templates/admin/default/customers.html | 12 +++++ 5 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 core/lib/Thelia/Tools/Password.php diff --git a/core/lib/Thelia/Config/Resources/routing/admin.xml b/core/lib/Thelia/Config/Resources/routing/admin.xml index 259c0f54a..dc5a2c522 100755 --- a/core/lib/Thelia/Config/Resources/routing/admin.xml +++ b/core/lib/Thelia/Config/Resources/routing/admin.xml @@ -114,6 +114,10 @@ Thelia\Controller\Admin\CustomerController::deleteAction + + Thelia\Controller\Admin\CustomerController::createAction + + diff --git a/core/lib/Thelia/Controller/Admin/CustomerController.php b/core/lib/Thelia/Controller/Admin/CustomerController.php index df75333c8..814008b7a 100644 --- a/core/lib/Thelia/Controller/Admin/CustomerController.php +++ b/core/lib/Thelia/Controller/Admin/CustomerController.php @@ -29,10 +29,12 @@ use Thelia\Core\Event\Customer\CustomerCreateOrUpdateEvent; use Thelia\Core\Event\Customer\CustomerEvent; use Thelia\Core\Event\TheliaEvents; use Thelia\Core\Security\AccessManager; +use Thelia\Form\CustomerCreateForm; use Thelia\Form\CustomerUpdateForm; use Thelia\Form\Exception\FormValidationException; use Thelia\Model\CustomerQuery; use Thelia\Core\Translation\Translator; +use Thelia\Tools\Password; /** * Class CustomerController @@ -102,7 +104,7 @@ class CustomerController extends BaseAdminController } if ($message !== false) { - \Thelia\Log\Tlog::getInstance()->error(sprintf("Error during customer login process : %s.", $message)); + \Thelia\Log\Tlog::getInstance()->error(sprintf("Error during customer update process : %s.", $message)); $customerUpdateForm->setErrorMessage($message); @@ -117,6 +119,56 @@ class CustomerController extends BaseAdminController )); } + public function createAction() + { + if (null !== $response = $this->checkAuth(AdminResources::CUSTOMER, AccessManager::CREATE)) return $response; + + $message = null; + + $customerCreateForm = new CustomerCreateForm($this->getRequest()); + + try { + + $form = $this->validateForm($customerCreateForm); + + $data = $form->getData(); + $data["password"] = Password::generateRandom(); + + $event = $this->createEventInstance($form->getData()); + + + + $this->dispatch(TheliaEvents::CUSTOMER_CREATEACCOUNT, $event); + + $successUrl = $customerCreateForm->getSuccessUrl(); + + $successUrl = str_replace('_ID_', $event->getCustomer()->getId(), $successUrl); + + $this->redirect($successUrl); + + + }catch (FormValidationException $e) { + $message = sprintf("Please check your input: %s", $e->getMessage()); + } catch (PropelException $e) { + $message = $e->getMessage(); + } catch (\Exception $e) { + $message = sprintf("Sorry, an error occured: %s", $e->getMessage()." ".$e->getFile()); + } + + if ($message !== false) { + \Thelia\Log\Tlog::getInstance()->error(sprintf("Error during customer creation process : %s.", $message)); + + $customerCreateForm->setErrorMessage($message); + + $this->getParserContext() + ->addForm($customerCreateForm) + ->setGeneralError($message) + ; + } + + return $this->render("customers", array("display_customer" => 20)); + } + public function deleteAction() { if (null !== $response = $this->checkAuth(AdminResources::CUSTOMER, AccessManager::DELETE)) return $response; diff --git a/core/lib/Thelia/Model/Customer.php b/core/lib/Thelia/Model/Customer.php index 230023410..ffdc316de 100755 --- a/core/lib/Thelia/Model/Customer.php +++ b/core/lib/Thelia/Model/Customer.php @@ -79,6 +79,7 @@ class Customer extends BaseCustomer implements UserInterface $address = new Address(); $address + ->setLabel("default") ->setCompany($company) ->setTitleId($titleId) ->setFirstname($firstname) diff --git a/core/lib/Thelia/Tools/Password.php b/core/lib/Thelia/Tools/Password.php new file mode 100644 index 000000000..dbb4cf8cb --- /dev/null +++ b/core/lib/Thelia/Tools/Password.php @@ -0,0 +1,54 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Tools; + + +/** + * Class Password + * @package Thelia\Tools + * @author Manuel Raynaud + */ +class Password +{ + + private static function randgen($letter, $length) { + + return substr(str_shuffle($letter), 0, $length); + } + + /** + * generate a Random password with defined length + * + * @param int $length + * @return mixed + */ + public static function generateRandom($length = 8){ + + $letter = "abcdefghijklmnopqrstuvwxyz"; + $letter .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + $letter .= "0123456789"; + + return self::randgen($letter, $length); + } +} \ No newline at end of file diff --git a/templates/admin/default/customers.html b/templates/admin/default/customers.html index 81a60fdf3..6ae0caa0c 100644 --- a/templates/admin/default/customers.html +++ b/templates/admin/default/customers.html @@ -166,6 +166,18 @@ {/form_field} + {form_field form=$form field="password"} + + {/form_field} + + {form_field form=$form field="password_confirm"} + + {/form_field} + + {form_field form=$form field="agreed"} + + {/form_field} + {form_field form=$form field='company'}