|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* OpauthRegisterForm |
|
5
|
|
|
* Presented to users whose OpauthIdentity object does not provide enough info. |
|
6
|
|
|
* This is triggered by the Member failing validation; you can modify this by |
|
7
|
|
|
* hooking in to the Member::validate() method via a DataExtension. |
|
8
|
|
|
* @author Will Morgan <@willmorgan> |
|
9
|
|
|
* @copyright Copyright (c) 2013, Better Brief LLP |
|
10
|
|
|
*/ |
|
11
|
|
|
class OpauthRegisterForm extends Form { |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
protected |
|
14
|
|
|
$fields, |
|
|
|
|
|
|
15
|
|
|
$requiredFields; |
|
16
|
|
|
|
|
17
|
|
|
protected static |
|
18
|
|
|
$field_source; |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param Controller $controller |
|
22
|
|
|
* @param string $name |
|
23
|
|
|
* @param array $requiredFields |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct($controller, $name, array $requiredFields = null) { |
|
26
|
|
|
if(isset($requiredFields)) { |
|
27
|
|
|
$this->requiredFields = $requiredFields; |
|
28
|
|
|
} |
|
29
|
|
|
parent::__construct($controller, $name, $this->getFields(), $this->getActions(), $this->getValidator()); |
|
30
|
|
|
// Manually call extensions here as Object must first construct extensions |
|
31
|
|
|
$this->extend('updateFields', $this->fields); |
|
32
|
|
|
$this->extend('updateActions', $this->actions); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* setRequiredFields |
|
37
|
|
|
* Resets everything if the fields change |
|
38
|
|
|
*/ |
|
39
|
|
|
public function setRequiredFields($fields) { |
|
40
|
|
|
$this->requiredFields = $fields; |
|
41
|
|
|
$this->setValidator($this->getValidator()); |
|
42
|
|
|
return $this; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* getFields |
|
47
|
|
|
* Picks only the required fields from the field source |
|
48
|
|
|
* and then presents them in a field set. |
|
49
|
|
|
* @return FieldList |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getFields() { |
|
52
|
|
|
$fields = $this->getFieldSource(); |
|
53
|
|
|
$this->extend('updateFields', $fields); |
|
54
|
|
|
return $fields; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Uses the field_source defined, or falls back to the Member's getCMSFields |
|
59
|
|
|
* @return FieldList |
|
60
|
|
|
*/ |
|
61
|
|
|
public function getFieldSource() { |
|
62
|
|
|
if(is_callable(self::$field_source)) { |
|
63
|
|
|
$fields = call_user_func(self::$field_source, $this); |
|
64
|
|
|
if(!$fields instanceof FieldList) { |
|
65
|
|
|
throw new InvalidArgumentException('Field source must be callable and return a FieldList'); |
|
66
|
|
|
} |
|
67
|
|
|
return $fields; |
|
68
|
|
|
} |
|
69
|
|
|
return new FieldList(singleton('Member')->getCMSFields()->dataFields()); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Set a callable as a data provider for the field source. Field names must |
|
74
|
|
|
* match those found on @see Member so they can be filtered accordingly. |
|
75
|
|
|
* |
|
76
|
|
|
* Callable docs: http://php.net/manual/en/language.types.callable.php |
|
77
|
|
|
* @param callable $sourceFn Source closure to use, accepts $this as param |
|
78
|
|
|
*/ |
|
79
|
|
|
public static function set_field_source($sourceFn) { |
|
80
|
|
|
if(!is_callable($sourceFn)) { |
|
81
|
|
|
throw new InvalidArgumentException('$sourceFn must be callable and return a FieldList'); |
|
82
|
|
|
} |
|
83
|
|
|
self::$field_source = $sourceFn; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Get actions |
|
88
|
|
|
* Points to a controller action |
|
89
|
|
|
* @return FieldList |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getActions() { |
|
92
|
|
|
$actions = new FieldList(array( |
|
93
|
|
|
new FormAction('doCompleteRegister', _t('OpauthRegisterForm.COMPLETE', 'Complete')), |
|
94
|
|
|
)); |
|
95
|
|
|
$this->extend('updateActions', $actions); |
|
96
|
|
|
return $actions; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return RequiredFields |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getValidator() { |
|
103
|
|
|
return Injector::inst()->create('OpauthValidator', $this->requiredFields); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Populates the form somewhat intelligently |
|
108
|
|
|
* @param SS_HTTPRequest $request Any request |
|
109
|
|
|
* @param Member $member Any member |
|
110
|
|
|
* @param array $required Any validation messages |
|
111
|
|
|
* @return $this |
|
112
|
|
|
*/ |
|
113
|
|
|
public function populateFromSources(SS_HTTPRequest $request = null, Member $member = null, array $required = null) { |
|
114
|
|
|
$dataPath = "FormInfo.{$this->FormName()}.data"; |
|
115
|
|
|
if(isset($member)) { |
|
116
|
|
|
$this->loadDataFrom($member); |
|
117
|
|
|
} |
|
118
|
|
|
else if(isset($request)) { |
|
119
|
|
|
$this->loadDataFrom($request->postVars()); |
|
120
|
|
|
} |
|
121
|
|
|
// Hacky again :( |
|
122
|
|
|
else if(Session::get($dataPath)) { |
|
123
|
|
|
$this->loadDataFrom(Session::get($dataPath)); |
|
124
|
|
|
} |
|
125
|
|
|
else if($failover = $this->getSessionData()) { |
|
126
|
|
|
$this->loadDataFrom($failover); |
|
127
|
|
|
} |
|
128
|
|
|
if(!empty($required)) { |
|
129
|
|
|
$this->setRequiredFields($required); |
|
130
|
|
|
} |
|
131
|
|
|
return $this; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Set failover data, so a user can refresh without losing his or her data |
|
136
|
|
|
* @param mixed $data Any type useable with $this->loadDataFrom |
|
137
|
|
|
*/ |
|
138
|
|
|
public function setSessionData($data) { |
|
139
|
|
|
Session::set($this->class.'.data', $data); |
|
140
|
|
|
return $this; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function getSessionData() { |
|
144
|
|
|
return Session::get($this->class.'.data'); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public function clearSessionData() { |
|
148
|
|
|
Session::clear($this->class.'.data'); |
|
149
|
|
|
return $this; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* mockErrors |
|
154
|
|
|
* Uses a very nasty trick to dynamically create some required field errors |
|
155
|
|
|
*/ |
|
156
|
|
|
public function mockErrors() { |
|
157
|
|
|
$this->validate(); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
} |
|
161
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.