From 4236a16e492a61330b3f5d151149ced240f8455b Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Mon, 21 Oct 2013 18:04:48 +0200 Subject: [PATCH 1/3] create example for mail usage --- .../Thelia/Config/Resources/routing/front.xml | 4 ++ core/lib/Thelia/Controller/BaseController.php | 13 +++++ core/lib/Thelia/Controller/Front/Mail.php | 49 +++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 core/lib/Thelia/Controller/Front/Mail.php diff --git a/core/lib/Thelia/Config/Resources/routing/front.xml b/core/lib/Thelia/Config/Resources/routing/front.xml index 351ba2ab4..542a1a319 100755 --- a/core/lib/Thelia/Config/Resources/routing/front.xml +++ b/core/lib/Thelia/Config/Resources/routing/front.xml @@ -164,4 +164,8 @@ + + Thelia\Controller\Front\Mail::test + + diff --git a/core/lib/Thelia/Controller/BaseController.php b/core/lib/Thelia/Controller/BaseController.php index e9f359411..548ef9681 100755 --- a/core/lib/Thelia/Controller/BaseController.php +++ b/core/lib/Thelia/Controller/BaseController.php @@ -303,4 +303,17 @@ class BaseController extends ContainerAware return $mailer->getSwiftMailer(); } + + /** + * @return ParserInterface instance parser + */ + protected function getParser() + { + return $this->container->get("thelia.parser"); + } + + protected function render($inline) + { + return $this->getParser()->fetch(sprintf("string:%s", $inline)); + } } diff --git a/core/lib/Thelia/Controller/Front/Mail.php b/core/lib/Thelia/Controller/Front/Mail.php new file mode 100644 index 000000000..dfaf2d4fa --- /dev/null +++ b/core/lib/Thelia/Controller/Front/Mail.php @@ -0,0 +1,49 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Controller\Front; + + +/** + * Class Mail + * @package Thelia\Controller\Front + * @author Manuel Raynaud + */ +class Mail extends BaseFrontController +{ + /** + * This is a demo how to send a mail using swiftmailer + smarty + */ + public function test() + { + $message = \Swift_Message::newInstance('Wonderful Subject') + ->setFrom(array('john@doe.com' => 'John Doe')) + ->setTo(array('mraynaud@openstudio.fr' => 'name')) + ->setBody($this->render('Here is the message itself')) + ; + + $this->getMailer()->send($message); + + exit; + } +} \ No newline at end of file From c0781455cbdb41c868d26fd1090f53a755bd1eaa Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Mon, 21 Oct 2013 18:33:03 +0200 Subject: [PATCH 2/3] create contact form --- core/lib/Thelia/Config/Resources/config.xml | 2 + .../Thelia/Config/Resources/routing/front.xml | 5 + .../Controller/Front/ContactController.php | 35 ++++++ core/lib/Thelia/Form/ContactForm.php | 119 ++++++++++++++++++ templates/default/contact.html | 5 + 5 files changed, 166 insertions(+) create mode 100644 core/lib/Thelia/Controller/Front/ContactController.php create mode 100644 core/lib/Thelia/Form/ContactForm.php create mode 100644 templates/default/contact.html diff --git a/core/lib/Thelia/Config/Resources/config.xml b/core/lib/Thelia/Config/Resources/config.xml index baee3b2f4..eec3d71c8 100755 --- a/core/lib/Thelia/Config/Resources/config.xml +++ b/core/lib/Thelia/Config/Resources/config.xml @@ -142,6 +142,8 @@
+ + diff --git a/core/lib/Thelia/Config/Resources/routing/front.xml b/core/lib/Thelia/Config/Resources/routing/front.xml index 542a1a319..2235bdb0c 100755 --- a/core/lib/Thelia/Config/Resources/routing/front.xml +++ b/core/lib/Thelia/Config/Resources/routing/front.xml @@ -168,4 +168,9 @@ Thelia\Controller\Front\Mail::test + + Thelia\Controller\Front\DefaultController::noAction + contact + + diff --git a/core/lib/Thelia/Controller/Front/ContactController.php b/core/lib/Thelia/Controller/Front/ContactController.php new file mode 100644 index 000000000..a807f6351 --- /dev/null +++ b/core/lib/Thelia/Controller/Front/ContactController.php @@ -0,0 +1,35 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Controller\Front; + + +/** + * Class ContactController + * @package Thelia\Controller\Front + * @author Manuel Raynaud + */ +class ContactController extends BaseFrontController +{ + +} \ No newline at end of file diff --git a/core/lib/Thelia/Form/ContactForm.php b/core/lib/Thelia/Form/ContactForm.php new file mode 100644 index 000000000..4ac94a2cd --- /dev/null +++ b/core/lib/Thelia/Form/ContactForm.php @@ -0,0 +1,119 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Form; + +use Symfony\Component\Validator\Constraints\Email; +use Symfony\Component\Validator\Constraints\NotBlank; +use Thelia\Core\Translation\Translator; + + +/** + * Class ContactForm + * @package Thelia\Form + * @author Manuel Raynaud + */ +class ContactForm 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->formBuilder attribute : + * + * $this->formBuilder->add("name", "text") + * ->add("email", "email", array( + * "attr" => array( + * "class" => "field" + * ), + * "label" => "email", + * "constraints" => array( + * new \Symfony\Component\Validator\Constraints\NotBlank() + * ) + * ) + * ) + * ->add('age', 'integer'); + * + * @return null + */ + protected function buildForm() + { + $this->formBuilder + ->add('firstname', 'text', array( + 'constraint' => array( + new NotBlank() + ), + 'label' => Translator::getInstance()->trans('firstname'), + 'label_attr' => array( + 'for' => 'firstname_contact' + ) + )) + ->add('lastname', 'text', array( + 'constraint' => array( + new NotBlank() + ), + 'label' => Translator::getInstance()->trans('lastname'), + 'label_attr' => array( + 'for' => 'lastname_contact' + ) + )) + ->add('email', 'email', array( + 'constraint' => array( + new NotBlank(), + new Email() + ), + 'label' => Translator::getInstance()->trans('email'), + 'label_attr' => array( + 'for' => 'email_contact' + ) + )) + ->add('subject', 'text', array( + 'constraint' => array( + new NotBlank() + ), + 'label' => Translator::getInstance()->trans('subject'), + 'label_attr' => array( + 'for' => 'subject_contact' + ) + )) + ->add('message', 'text', array( + 'constraint' => array( + new NotBlank() + ), + 'label' => Translator::getInstance()->trans('message'), + 'label_attr' => array( + 'for' => 'message_contact' + ) + + )) + ; + } + + /** + * @return string the name of you form. This name must be unique + */ + public function getName() + { + return 'thelia.contact'; + } +} \ No newline at end of file diff --git a/templates/default/contact.html b/templates/default/contact.html new file mode 100644 index 000000000..161ef58ab --- /dev/null +++ b/templates/default/contact.html @@ -0,0 +1,5 @@ +{extends file="layout.tpl"} + +{block name="main-content"} + +{/block} \ No newline at end of file From 9ce90abdf5e050c84a7dad6e6e406fe6e236d78f Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Mon, 21 Oct 2013 19:07:59 +0200 Subject: [PATCH 3/3] finish contact process --- .../Thelia/Config/Resources/routing/front.xml | 10 ++ .../Controller/Front/ContactController.php | 38 +++++++ core/lib/Thelia/Form/ContactForm.php | 12 +-- install/insert.sql | 3 +- .../default/assets/less/thelia/forms.less | 1 + templates/default/contact-success.html | 15 +++ templates/default/contact.html | 102 +++++++++++++++++- 7 files changed, 173 insertions(+), 8 deletions(-) create mode 100644 templates/default/contact-success.html diff --git a/core/lib/Thelia/Config/Resources/routing/front.xml b/core/lib/Thelia/Config/Resources/routing/front.xml index 2235bdb0c..74cf23db8 100755 --- a/core/lib/Thelia/Config/Resources/routing/front.xml +++ b/core/lib/Thelia/Config/Resources/routing/front.xml @@ -173,4 +173,14 @@ contact + + Thelia\Controller\Front\ContactController::sendAction + contact + + + + Thelia\Controller\Front\DefaultController::noAction + contact-success + + diff --git a/core/lib/Thelia/Controller/Front/ContactController.php b/core/lib/Thelia/Controller/Front/ContactController.php index a807f6351..83bd2588e 100644 --- a/core/lib/Thelia/Controller/Front/ContactController.php +++ b/core/lib/Thelia/Controller/Front/ContactController.php @@ -22,6 +22,9 @@ /*************************************************************************************/ namespace Thelia\Controller\Front; +use Thelia\Form\ContactForm; +use Thelia\Form\Exception\FormValidationException; +use Thelia\Model\ConfigQuery; /** @@ -31,5 +34,40 @@ namespace Thelia\Controller\Front; */ class ContactController extends BaseFrontController { + /** + * send contact message + */ + public function sendAction() + { + $error_message = false; + $contactForm = new ContactForm($this->getRequest()); + try { + $form = $this->validateForm($contactForm); + + $message = \Swift_Message::newInstance($form->get('subject')->getData()) + ->addFrom($form->get('email')->getData(), $form->get('firstname')->getData().' '.$form->get('lastname')->getData()) + ->addTo(ConfigQuery::read('contact_email'), ConfigQuery::read('company_name')) + ->setBody($form->get('message')->getData()) + ; + + $this->getMailer()->send($message); + + } catch(FormValidationException $e) { + $error_message = $e->getMessage(); + } + + if ($error_message !== false) { + \Thelia\Log\Tlog::getInstance()->error(sprintf("Error during customer creation process : %s", $error_message)); + + $contactForm->setErrorMessage($error_message); + + $this->getParserContext() + ->addForm($contactForm) + ->setGeneralError($error_message) + ; + } else { + $this->redirectToRoute('contact.success'); + } + } } \ No newline at end of file diff --git a/core/lib/Thelia/Form/ContactForm.php b/core/lib/Thelia/Form/ContactForm.php index 4ac94a2cd..097e651da 100644 --- a/core/lib/Thelia/Form/ContactForm.php +++ b/core/lib/Thelia/Form/ContactForm.php @@ -60,7 +60,7 @@ class ContactForm extends BaseForm { $this->formBuilder ->add('firstname', 'text', array( - 'constraint' => array( + 'constraints' => array( new NotBlank() ), 'label' => Translator::getInstance()->trans('firstname'), @@ -69,7 +69,7 @@ class ContactForm extends BaseForm ) )) ->add('lastname', 'text', array( - 'constraint' => array( + 'constraints' => array( new NotBlank() ), 'label' => Translator::getInstance()->trans('lastname'), @@ -78,7 +78,7 @@ class ContactForm extends BaseForm ) )) ->add('email', 'email', array( - 'constraint' => array( + 'constraints' => array( new NotBlank(), new Email() ), @@ -88,7 +88,7 @@ class ContactForm extends BaseForm ) )) ->add('subject', 'text', array( - 'constraint' => array( + 'constraints' => array( new NotBlank() ), 'label' => Translator::getInstance()->trans('subject'), @@ -97,7 +97,7 @@ class ContactForm extends BaseForm ) )) ->add('message', 'text', array( - 'constraint' => array( + 'constraints' => array( new NotBlank() ), 'label' => Translator::getInstance()->trans('message'), @@ -114,6 +114,6 @@ class ContactForm extends BaseForm */ public function getName() { - return 'thelia.contact'; + return 'thelia_contact'; } } \ No newline at end of file diff --git a/install/insert.sql b/install/insert.sql index 991eb7892..b7cbebac7 100755 --- a/install/insert.sql +++ b/install/insert.sql @@ -28,7 +28,8 @@ INSERT INTO `config` (`name`, `value`, `secured`, `hidden`, `created_at`, `updat ('thelia_customer_remember_me_cookie_name', 'tcrmcn', 0, 0, NOW(), NOW()), ('thelia_customer_remember_me_cookie_expiration', 31536000, 0, 0, NOW(), NOW()), ('session_config.handlers', 'Symfony\\Component\\HttpFoundation\\Session\\Storage\\Handler\\NativeFileSessionHandler', 0, 0, NOW(), NOW()), -('company_name','', 0, 0, NOW(), NOW()) +('company_name','', 0, 0, NOW(), NOW()), +('contact_email','', 0, 0, NOW(), NOW()) ; diff --git a/templates/default/assets/less/thelia/forms.less b/templates/default/assets/less/thelia/forms.less index c8e8a3dad..d39d64ea1 100755 --- a/templates/default/assets/less/thelia/forms.less +++ b/templates/default/assets/less/thelia/forms.less @@ -50,6 +50,7 @@ // Form Register #form-address, +#form-contact, #form-register { .panel-body { .control-label { .make-sm-column(3); } diff --git a/templates/default/contact-success.html b/templates/default/contact-success.html new file mode 100644 index 000000000..59a79974e --- /dev/null +++ b/templates/default/contact-success.html @@ -0,0 +1,15 @@ +{extends file="layout.tpl"} + +{* Breadcrumb *} +{block name='no-return-functions' append} + {$breadcrumbs = [['title' => {intl l="Thanks !"}, 'url'=>{url path="/contact/success"}]]} +{/block} + +{block name="main-content"} +
+
+

{intl l="Thanks !"}

+

{intl l="Thanks for your message, we will contact as soon as possible"}

+
+
+{/block} \ No newline at end of file diff --git a/templates/default/contact.html b/templates/default/contact.html index 161ef58ab..2a9458368 100644 --- a/templates/default/contact.html +++ b/templates/default/contact.html @@ -1,5 +1,105 @@ {extends file="layout.tpl"} -{block name="main-content"} +{* Breadcrumb *} +{block name='no-return-functions' append} + {$breadcrumbs = [['title' => {intl l="Contact us"}, 'url'=>{url path="/contact"}]]} +{/block} +{block name="main-content"} +
+
+

{intl l="Contact us"}

+ + {form name="thelia.contact"} + + {form_hidden_fields form=$form} +
+
+ 1. {intl l="Personal Informations"} +
+
+ {form_field form=$form field="firstname"} +
+ +
+ + {if $error } + {$message} + {assign var="error_focus" value="true"} + {elseif $value != "" && !$error} + + {/if} +
+
+ {/form_field} + {form_field form=$form field="lastname"} +
+ +
+ + {if $error } + {$message} + {assign var="error_focus" value="true"} + {elseif $value != "" && !$error} + + {/if} +
+
+ {/form_field} + {form_field form=$form field="email"} +
+ +
+ + {if $error } + {$message} + {assign var="error_focus" value="true"} + {elseif $value != "" && !$error} + + {/if} +
+
+ {/form_field} + {form_field form=$form field="subject"} +
+ +
+ + {if $error } + {$message} + {assign var="error_focus" value="true"} + {elseif $value != "" && !$error} + + {/if} +
+
+ {/form_field} + {form_field form=$form field="message"} +
+ +
+ + {if $error } + {$message} + {assign var="error_focus" value="true"} + {elseif $value != "" && !$error} + + {/if} +
+
+ {/form_field} + +
+
+ +
+
+
+
+ + {/form} +
+
{/block} \ No newline at end of file