1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverShop\Forms; |
4
|
|
|
|
5
|
|
|
use SilverShop\Extension\ShopConfigExtension; |
6
|
|
|
use SilverShop\Page\AccountPageController; |
7
|
|
|
use SilverShop\Page\CheckoutPage; |
8
|
|
|
use SilverStripe\Core\Config\Config; |
9
|
|
|
use SilverStripe\Control\HTTPRequest; |
10
|
|
|
use SilverStripe\Control\HTTPResponse; |
11
|
|
|
use SilverStripe\Forms\FieldList; |
12
|
|
|
use SilverStripe\Forms\Form; |
13
|
|
|
use SilverStripe\Forms\FormAction; |
14
|
|
|
use SilverStripe\Security\Security; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Allows shop members to update their details with the shop. |
18
|
|
|
*/ |
19
|
|
|
class ShopAccountForm extends Form |
20
|
|
|
{ |
21
|
|
|
public function __construct($controller, $name) |
22
|
|
|
{ |
23
|
|
|
$member = Security::getCurrentUser(); |
24
|
|
|
$requiredFields = null; |
25
|
|
|
if ($member && $member->exists()) { |
26
|
|
|
$fields = $member->getMemberFormFields(); |
27
|
|
|
$fields->removeByName('Password'); |
28
|
|
|
$requiredFields = $member->getValidator(); |
29
|
|
|
$requiredFields->addRequiredField('Surname'); |
30
|
|
|
} else { |
31
|
|
|
$fields = FieldList::create(); |
32
|
|
|
} |
33
|
|
|
if ($controller instanceof AccountPageController) { |
34
|
|
|
$actions = FieldList::create(FormAction::create('submit', _t(__CLASS__ . '.Save', 'Save Changes'))); |
35
|
|
|
} else { |
36
|
|
|
$actions = FieldList::create( |
37
|
|
|
FormAction::create('submit', _t(__CLASS__ . '.Save', 'Save Changes')) |
38
|
|
|
->setUseButtonTag(Config::inst()->get(ShopConfigExtension::class, 'forms_use_button_tag')), |
39
|
|
|
FormAction::create('proceed', _t(__CLASS__ . '.SaveAndProceed', 'Save and proceed to checkout')) |
40
|
|
|
->setUseButtonTag(Config::inst()->get(ShopConfigExtension::class, 'forms_use_button_tag')) |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
parent::__construct($controller, $name, $fields, $actions, $requiredFields); |
44
|
|
|
|
45
|
|
|
$this->extend('updateShopAccountForm'); |
46
|
|
|
|
47
|
|
|
if ($member) { |
|
|
|
|
48
|
|
|
$this->loadDataFrom($member); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Save the changes to the form |
54
|
|
|
* |
55
|
|
|
* @param array $data |
56
|
|
|
* @param Form $form |
57
|
|
|
* @param HTTPRequest $request |
58
|
|
|
* |
59
|
|
|
* @return bool|HTTPResponse |
60
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
61
|
|
|
*/ |
62
|
|
|
public function submit($data, $form, $request) |
63
|
|
|
{ |
64
|
|
|
$member = Security::getCurrentUser(); |
65
|
|
|
if (!$member) { |
|
|
|
|
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$form->saveInto($member); |
70
|
|
|
$member->write(); |
71
|
|
|
$form->sessionMessage(_t(__CLASS__ . '.DetailsSaved', 'Your details have been saved'), 'good'); |
72
|
|
|
|
73
|
|
|
$this->extend('updateShopAccountFormResponse', $request, $form, $data, $response); |
74
|
|
|
|
75
|
|
|
return $response ?: $this->getController()->redirectBack(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Save the changes to the form, and redirect to the checkout page |
80
|
|
|
* |
81
|
|
|
* @param array $data |
82
|
|
|
* @param Form $form |
83
|
|
|
* @param HTTPRequest $request |
84
|
|
|
* |
85
|
|
|
* @return bool|HTTPResponse |
86
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
87
|
|
|
*/ |
88
|
|
|
public function proceed($data, $form, $request) |
89
|
|
|
{ |
90
|
|
|
$member = Security::getCurrentUser(); |
91
|
|
|
if (!$member) { |
|
|
|
|
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$form->saveInto($member); |
96
|
|
|
$member->write(); |
97
|
|
|
$form->sessionMessage(_t(__CLASS__ . '.DetailsSaved', 'Your details have been saved'), 'good'); |
98
|
|
|
|
99
|
|
|
$this->extend('updateShopAccountFormResponse', $request, $form, $data, $response); |
100
|
|
|
|
101
|
|
|
return $response ?: $this->getController()->redirect(CheckoutPage::find_link()); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|