/
GuestTrackingController.php
194 lines (174 loc) · 6.92 KB
/
GuestTrackingController.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
use PrestaShop\PrestaShop\Adapter\Presenter\Order\OrderPresenter;
use PrestaShop\PrestaShop\Core\Security\PasswordPolicyConfiguration;
class GuestTrackingControllerCore extends FrontController
{
/** @var bool */
public $ssl = true;
/** @var bool */
public $auth = false;
/** @var string */
public $php_self = 'guest-tracking';
protected $order;
/**
* Initialize guest tracking controller.
*
* @see FrontController::init()
*/
public function init()
{
if ($this->context->customer->isLogged()) {
Tools::redirect('history.php');
}
parent::init();
}
/**
* Start forms process.
*
* @see FrontController::postProcess()
*/
public function postProcess()
{
$order_reference = current(explode('#', Tools::getValue('order_reference')));
$email = Tools::getValue('email');
if (!$email && !$order_reference) {
return;
} elseif (!$email || !$order_reference) {
$this->errors[] = $this->getTranslator()->trans(
'Please provide the required information',
[],
'Shop.Notifications.Error'
);
return;
}
$this->order = Order::getByReferenceAndEmail($order_reference, $email);
if (!Validate::isLoadedObject($this->order)) {
$this->errors[] = $this->getTranslator()->trans(
'We couldn\'t find your order with the information provided, please try again',
[],
'Shop.Notifications.Error'
);
}
if (Tools::isSubmit('submitTransformGuestToCustomer') && Tools::getValue('password')) {
$customer = new Customer((int) $this->order->id_customer);
/** @var string $password */
$password = Tools::getValue('password');
if (empty($password)) {
$this->errors[] = $this->trans(
'Enter a password to transform your guest account into a customer account.',
[],
'Shop.Forms.Help'
);
} elseif (!Validate::isAcceptablePasswordLength($password)) {
$this->errors[] = $this->trans(
'Your password length must be between %d and %d',
[Configuration::get(PasswordPolicyConfiguration::CONFIGURATION_MINIMUM_LENGTH), Configuration::get(PasswordPolicyConfiguration::CONFIGURATION_MAXIMUM_LENGTH)],
'Shop.Forms.Help'
);
} elseif (!Validate::isAcceptablePasswordScore($password)) {
$this->errors[] = $this->trans(
'Customer password is too weak',
[],
'Shop.Forms.Help'
);
// Prevent error
// A) either on page refresh
// B) if we already transformed him in other window or through backoffice
} elseif ($customer->is_guest == 0) {
$this->errors[] = $this->trans(
'A customer account has already been created from this guest account. Please sign in.',
[],
'Shop.Notifications.Error'
);
// Attempt to convert the customer
} elseif ($customer->transformToCustomer($this->context->language->id, $password)) {
$this->success[] = $this->trans(
'Your guest account has been successfully transformed into a customer account. You can now log in as a registered shopper.',
[],
'Shop.Notifications.Success'
);
} else {
$this->errors[] = $this->trans(
'An unexpected error occurred while creating your account.',
[],
'Shop.Notifications.Error'
);
}
}
}
/**
* Assign template vars related to page content.
*
* @see FrontController::initContent()
*/
public function initContent()
{
parent::initContent();
if (!Validate::isLoadedObject($this->order)) {
return $this->setTemplate('customer/guest-login');
}
if ((int) $this->order->isReturnable()) {
$this->info[] = $this->trans(
'You cannot return merchandise with a guest account.',
[],
'Shop.Notifications.Warning'
);
}
// Kept for backwards compatibility (is_customer), inline it in later versions
$registered_customer_exists = Customer::customerExists(Tools::getValue('email'));
$this->context->smarty->assign([
'order' => (new OrderPresenter())->present($this->order),
'guest_email' => Tools::getValue('email'),
'registered_customer_exists' => $registered_customer_exists,
'is_customer' => $registered_customer_exists, // Kept for backwards compatibility
'HOOK_DISPLAYORDERDETAIL' => Hook::exec('displayOrderDetail', ['order' => $this->order]),
]);
return $this->setTemplate('customer/guest-tracking');
}
public function getBreadcrumbLinks()
{
$breadcrumbLinks = parent::getBreadcrumbLinks();
$breadcrumbLinks['links'][] = [
'title' => $this->getTranslator()->trans('Guest order tracking', [], 'Shop.Theme.Checkout'),
'url' => $this->context->link->getPageLink('guest-tracking'),
];
if (Validate::isLoadedObject($this->order)) {
$breadcrumbLinks['links'][] = [
'title' => $this->order->reference,
'url' => '#',
];
}
return $breadcrumbLinks;
}
/**
* {@inheritdoc}
*/
public function getCanonicalURL()
{
return $this->context->link->getPageLink('guest-tracking');
}
}