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) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
48 | $member->Password = ''; //prevents password field from being populated with encrypted password data |
||
49 | $this->loadDataFrom($member); |
||
50 | } |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Save the changes to the form |
||
55 | * |
||
56 | * @param array $data |
||
57 | * @param Form $form |
||
58 | * @param HTTPRequest $request |
||
59 | * |
||
60 | * @return bool|HTTPResponse |
||
61 | * @throws \SilverStripe\ORM\ValidationException |
||
62 | */ |
||
63 | public function submit($data, $form, $request) |
||
64 | { |
||
65 | $member = Security::getCurrentUser(); |
||
66 | if (!$member) { |
||
0 ignored issues
–
show
|
|||
67 | return false; |
||
68 | } |
||
69 | |||
70 | $form->saveInto($member); |
||
71 | $member->write(); |
||
72 | $form->sessionMessage(_t(__CLASS__ . '.DetailsSaved', 'Your details have been saved'), 'good'); |
||
73 | |||
74 | $this->extend('updateShopAccountFormResponse', $request, $form, $data, $response); |
||
75 | |||
76 | return $response ?: $this->getController()->redirectBack(); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Save the changes to the form, and redirect to the checkout page |
||
81 | * |
||
82 | * @param array $data |
||
83 | * @param Form $form |
||
84 | * @param HTTPRequest $request |
||
85 | * |
||
86 | * @return bool|HTTPResponse |
||
87 | * @throws \SilverStripe\ORM\ValidationException |
||
88 | */ |
||
89 | public function proceed($data, $form, $request) |
||
90 | { |
||
91 | $member = Security::getCurrentUser(); |
||
92 | if (!$member) { |
||
0 ignored issues
–
show
|
|||
93 | return false; |
||
94 | } |
||
95 | |||
96 | $form->saveInto($member); |
||
97 | $member->write(); |
||
98 | $form->sessionMessage(_t(__CLASS__ . '.DetailsSaved', 'Your details have been saved'), 'good'); |
||
99 | |||
100 | $this->extend('updateShopAccountFormResponse', $request, $form, $data, $response); |
||
101 | |||
102 | return $response ?: $this->getController()->redirect(CheckoutPage::find_link()); |
||
103 | } |
||
104 | } |
||
105 |