/
CustomerLogin.php
executable file
·130 lines (122 loc) · 5.5 KB
/
CustomerLogin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* 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 <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Form;
use Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ExecutionContextInterface;
use Thelia\Core\Translation\Translator;
use Thelia\Model\Base\CustomerQuery;
/**
* Class CustomerLogin
* @package Thelia\Form
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class CustomerLogin extends BaseForm
{
protected function buildForm()
{
$this->formBuilder
->add("email", "email", array(
"constraints" => array(
new Constraints\NotBlank(),
new Constraints\Email(),
new Constraints\Callback(array(
"methods" => array(
array($this, "verifyExistingEmail")
)
))
),
"label" => Translator::getInstance()->trans("Please enter your email address"),
"label_attr" => array(
"for" => "email"
)
))
->add("account", "choice", array(
"constraints" => array(
new Constraints\Callback(array(
"methods" => array(
array($this, "verifyAccount")
)
))
),
"choices" => array(
0 => Translator::getInstance()->trans("No, I am a new customer."),
1 => Translator::getInstance()->trans("Yes, I have a password :")
),
"label_attr" => array(
"for" => "account"
),
"data" => 0
))
->add("password", "password", array(
"constraints" => array(
new Constraints\NotBlank(array(
'groups' => array('existing_customer'),
))
),
"label" => Translator::getInstance()->trans("Please enter your password"),
"label_attr" => array(
"for" => "password"
),
"required" => false
));
}
/**
* If the user select "Yes, I have a password", we check the password.
*/
public function verifyAccount($value, ExecutionContextInterface $context)
{
if ($value == 1) {
$data = $context->getRoot()->getData();
if (false === $data['password'] || (empty($data['password']) && '0' != $data['password'])) {
$context->getViolations()->add(new ConstraintViolation(
'This value should not be blank.',
'account_password',
array(),
$context->getRoot(),
'children[password].data',
'propertyPath'
));
}
}
}
/**
* If the user select "I'am a new customer", we make sure is email address does not exit in the database.
*/
public function verifyExistingEmail($value, ExecutionContextInterface $context)
{
$data = $context->getRoot()->getData();
if ($data["account"] == 0) {
$customer = CustomerQuery::create()->findOneByEmail($value);
if ($customer) {
$context->addViolation("A user already exists with this email address. Please login or if you've forgotten your password, go to Reset Your Password.");
}
}
}
public function getName()
{
return "thelia_customer_login";
}
}