1
|
|
|
<?php |
2
|
|
|
namespace UserManagement\Forms; |
3
|
|
|
|
4
|
|
|
use SilverStripe\Forms\FieldList; |
5
|
|
|
use SilverStripe\Forms\Form; |
6
|
|
|
use SilverStripe\Forms\ReadOnlyField; |
7
|
|
|
use SilverStripe\Forms\FormAction; |
8
|
|
|
use SilverStripe\SiteConfig\SiteConfig; |
9
|
|
|
use SilverStripe\Security\Security; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class ProfileForm |
13
|
|
|
* |
14
|
|
|
* @package user-management |
15
|
|
|
*/ |
16
|
|
|
class ProfileForm extends SignUpForm |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var \SilverStripe\SiteConfig\SiteConfig |
20
|
|
|
*/ |
21
|
|
|
protected $siteConfig; |
22
|
|
|
|
23
|
2 |
|
public function __construct($controller, $name) |
24
|
|
|
{ |
25
|
2 |
|
$this->setAttribute('id', 'ProfileForm'); |
26
|
2 |
|
$this->setsiteConfig(); |
27
|
2 |
|
parent::__construct($controller, $name, $this->getFormFields($controller), $this->getFormActions()); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Required FieldList creation on a ProfileForm |
32
|
|
|
* |
33
|
|
|
* @return \SilverStripe\Forms\FieldList |
34
|
|
|
*/ |
35
|
2 |
|
protected function getFormFields($controller = null) |
36
|
|
|
{ |
37
|
2 |
|
$member = Security::getCurrentUser(); |
38
|
2 |
|
$fieldList = parent::getFormFields(); |
39
|
2 |
|
$fieldList->removeByName('Password'); |
40
|
2 |
|
$fieldList->removeByName('Email'); |
41
|
2 |
|
$this->setAttribute('id', 'ProfileForm'); |
42
|
2 |
|
$fieldList->insertBefore(ReadOnlyField::create("Email", "Email", $member->Email), "Mobile"); |
|
|
|
|
43
|
2 |
|
return $fieldList; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Returns the Form action |
48
|
|
|
* @return FieldList Actions for this form. |
49
|
|
|
*/ |
50
|
2 |
|
protected function getFormActions() |
51
|
|
|
{ |
52
|
2 |
|
return FieldList::create( |
53
|
2 |
|
FormAction::create("doSubmitProfile", "Submit") |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Form action, updates the user profile. |
59
|
|
|
* @return \SilverStripe\Control\HTTPResponse |
60
|
|
|
*/ |
61
|
1 |
|
public function doSubmitProfile($data, Form $form) |
|
|
|
|
62
|
|
|
{ |
63
|
1 |
|
$member = Security::getCurrentUser(); |
64
|
1 |
|
if ($member) { |
|
|
|
|
65
|
1 |
|
$form->saveInto($member); |
66
|
1 |
|
$member->write(); |
67
|
1 |
|
$msg = $this->getCustomMessage('ProfileUpdateSuccess') != "" |
68
|
1 |
|
? $this->getCustomMessage('ProfileUpdateSuccess') : "Profile updated!"; |
69
|
1 |
|
$form->sessionMessage($msg, 'good'); |
70
|
|
|
} else { |
71
|
|
|
return $this->controller->redirect($this->siteConfig->LoginUrl()->URLSegment); |
72
|
|
|
} |
73
|
1 |
|
return $this->controller->redirectBack(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Return the Message from siteconfig |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
1 |
|
public function getCustomMessage($field) |
81
|
|
|
{ |
82
|
1 |
|
return $this->siteConfig->$field; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Assign siteconfig object |
87
|
|
|
*/ |
88
|
2 |
|
protected function setsiteConfig() |
89
|
|
|
{ |
90
|
2 |
|
$this->siteConfig = SiteConfig::current_site_config(); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.