Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | * $Id$ |
17 | * | |
18 | */ | |
19 | ||
20 | /** | |
ca87146b | 21 | * This class generates form components for processing a contribution. |
6a488035 TO |
22 | */ |
23 | class CRM_PCP_Form_PCPAccount extends CRM_Core_Form { | |
24 | ||
25 | /** | |
fe482240 | 26 | * Variable defined for Contribution Page Id. |
c86d4e7c | 27 | * @var int |
6a488035 | 28 | */ |
6a488035 TO |
29 | public $_pageId = NULL; |
30 | public $_id = NULL; | |
31 | public $_component = NULL; | |
32 | ||
33 | /** | |
100fef9d | 34 | * Are we in single form mode or wizard mode? |
6a488035 | 35 | * |
b67daa72 | 36 | * @var bool |
6a488035 TO |
37 | */ |
38 | public $_single; | |
39 | ||
40 | public function preProcess() { | |
353ffa53 TO |
41 | $session = CRM_Core_Session::singleton(); |
42 | $config = CRM_Core_Config::singleton(); | |
43 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE); | |
44 | $this->_pageId = CRM_Utils_Request::retrieve('pageId', 'Positive', $this); | |
6a488035 | 45 | $this->_component = CRM_Utils_Request::retrieve('component', 'String', $this); |
353ffa53 | 46 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); |
6a488035 | 47 | |
9dcce349 | 48 | if (!$this->_pageId && $config->userFramework == 'Joomla' && $config->userFrameworkFrontend) { |
6a488035 TO |
49 | $this->_pageId = $this->_id; |
50 | } | |
51 | ||
52 | if ($this->_id) { | |
53 | $contactID = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $this->_id, 'contact_id'); | |
54 | } | |
55 | ||
56 | $this->_contactID = isset($contactID) ? $contactID : $session->get('userID'); | |
57 | if (!$this->_pageId) { | |
58 | if (!$this->_id) { | |
59 | $msg = ts('We can\'t load the requested web page due to an incomplete link. This can be caused by using your browser\'s Back button or by using an incomplete or invalid link.'); | |
60 | CRM_Core_Error::fatal($msg); | |
61 | } | |
62 | else { | |
63 | $this->_pageId = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $this->_id, 'page_id'); | |
64 | } | |
65 | } | |
66 | ||
67 | if (!$this->_pageId) { | |
68 | CRM_Core_Error::fatal(ts('Could not find source page id.')); | |
69 | } | |
70 | ||
71 | $this->_single = $this->get('single'); | |
72 | ||
73 | if (!$this->_single) { | |
74 | $this->_single = $session->get('singleForm'); | |
75 | } | |
76 | ||
77 | $this->set('action', $this->_action); | |
78 | $this->set('page_id', $this->_id); | |
79 | $this->set('component_page_id', $this->_pageId); | |
80 | ||
81 | // we do not want to display recently viewed items, so turn off | |
82 | $this->assign('displayRecent', FALSE); | |
83 | ||
84 | $this->assign('pcpComponent', $this->_component); | |
85 | ||
86 | if ($this->_single) { | |
87 | CRM_Utils_System::setTitle(ts('Update Contact Information')); | |
88 | } | |
89 | } | |
90 | ||
e0ef6999 EM |
91 | /** |
92 | * @return array | |
93 | */ | |
00be9182 | 94 | public function setDefaultValues() { |
be2fb01f | 95 | $this->_defaults = []; |
f49b8be4 RN |
96 | if ($this->_contactID) { |
97 | foreach ($this->_fields as $name => $dontcare) { | |
98 | $fields[$name] = 1; | |
99 | } | |
2efcf0c2 | 100 | |
f49b8be4 | 101 | CRM_Core_BAO_UFGroup::setProfileDefaults($this->_contactID, $fields, $this->_defaults); |
6a488035 | 102 | } |
6a488035 TO |
103 | //set custom field defaults |
104 | foreach ($this->_fields as $name => $field) { | |
105 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) { | |
106 | if (!isset($this->_defaults[$name])) { | |
107 | CRM_Core_BAO_CustomField::setProfileDefaults($customFieldID, $name, $this->_defaults, | |
108 | NULL, CRM_Profile_Form::MODE_REGISTER | |
109 | ); | |
110 | } | |
111 | } | |
6a488035 | 112 | } |
6a488035 TO |
113 | return $this->_defaults; |
114 | } | |
115 | ||
116 | /** | |
fe482240 | 117 | * Build the form object. |
6a488035 | 118 | * |
355ba699 | 119 | * @return void |
6a488035 TO |
120 | */ |
121 | public function buildQuickForm() { | |
122 | $id = CRM_PCP_BAO_PCP::getSupporterProfileId($this->_pageId, $this->_component); | |
123 | if (CRM_PCP_BAO_PCP::checkEmailProfile($id)) { | |
124 | $this->assign('profileDisplay', TRUE); | |
125 | } | |
126 | $fields = NULL; | |
127 | if ($this->_contactID) { | |
128 | if (CRM_Core_BAO_UFGroup::filterUFGroups($id, $this->_contactID)) { | |
129 | $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD); | |
130 | } | |
be2fb01f | 131 | $this->addFormRule(['CRM_PCP_Form_PCPAccount', 'formRule'], $this); |
6a488035 TO |
132 | } |
133 | else { | |
134 | CRM_Core_BAO_CMSUser::buildForm($this, $id, TRUE); | |
135 | ||
136 | $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD); | |
137 | } | |
138 | ||
139 | if ($fields) { | |
140 | $this->assign('fields', $fields); | |
141 | $addCaptcha = FALSE; | |
142 | foreach ($fields as $key => $field) { | |
143 | if (isset($field['data_type']) && $field['data_type'] == 'File') { | |
144 | // ignore file upload fields | |
145 | continue; | |
146 | } | |
147 | CRM_Core_BAO_UFGroup::buildProfile($this, $field, CRM_Profile_Form::MODE_CREATE); | |
148 | $this->_fields[$key] = $field; | |
71fc6ea4 DG |
149 | |
150 | // CRM-11316 Is ReCAPTCHA enabled for this profile AND is this an anonymous visitor | |
151 | if ($field['add_captcha'] && !$this->_contactID) { | |
6a488035 TO |
152 | $addCaptcha = TRUE; |
153 | } | |
154 | } | |
155 | ||
156 | if ($addCaptcha) { | |
157 | $captcha = &CRM_Utils_ReCAPTCHA::singleton(); | |
158 | $captcha->add($this); | |
159 | $this->assign('isCaptcha', TRUE); | |
160 | } | |
161 | } | |
162 | ||
6a488035 TO |
163 | if ($this->_component == 'contribute') { |
164 | $this->assign('campaignName', CRM_Contribute_PseudoConstant::contributionPage($this->_pageId)); | |
165 | } | |
166 | elseif ($this->_component == 'event') { | |
167 | $this->assign('campaignName', CRM_Event_PseudoConstant::event($this->_pageId)); | |
168 | } | |
169 | ||
170 | if ($this->_single) { | |
be2fb01f CW |
171 | $button = [ |
172 | [ | |
353ffa53 | 173 | 'type' => 'next', |
6a488035 TO |
174 | 'name' => ts('Save'), |
175 | 'spacing' => ' ', | |
176 | 'isDefault' => TRUE, | |
be2fb01f CW |
177 | ], |
178 | [ | |
6a488035 TO |
179 | 'type' => 'cancel', |
180 | 'name' => ts('Cancel'), | |
be2fb01f CW |
181 | ], |
182 | ]; | |
6a488035 TO |
183 | } |
184 | else { | |
be2fb01f | 185 | $button[] = [ |
6a488035 | 186 | 'type' => 'next', |
f212d37d | 187 | 'name' => ts('Continue'), |
6a488035 TO |
188 | 'spacing' => ' ', |
189 | 'isDefault' => TRUE, | |
be2fb01f | 190 | ]; |
6a488035 | 191 | } |
be2fb01f | 192 | $this->addFormRule(['CRM_PCP_Form_PCPAccount', 'formRule'], $this); |
6a488035 TO |
193 | $this->addButtons($button); |
194 | } | |
195 | ||
196 | /** | |
fe482240 | 197 | * Global form rule. |
6a488035 | 198 | * |
db95eff6 TO |
199 | * @param array $fields |
200 | * The input form values. | |
201 | * @param array $files | |
202 | * The uploaded files if any. | |
fd31fa4c EM |
203 | * @param $self |
204 | * | |
6a488035 | 205 | * |
72b3a70c CW |
206 | * @return bool|array |
207 | * true if no errors, else array of errors | |
6a488035 | 208 | */ |
00be9182 | 209 | public static function formRule($fields, $files, $self) { |
be2fb01f | 210 | $errors = []; |
6a488035 TO |
211 | foreach ($fields as $key => $value) { |
212 | if (strpos($key, 'email-') !== FALSE && !empty($value)) { | |
213 | $ufContactId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFMatch', $value, 'contact_id', 'uf_name'); | |
214 | if ($ufContactId && $ufContactId != $self->_contactID) { | |
215 | $errors[$key] = ts('There is already an user associated with this email address. Please enter different email address.'); | |
216 | } | |
217 | } | |
218 | } | |
219 | return empty($errors) ? TRUE : $errors; | |
220 | } | |
221 | ||
222 | /** | |
fe482240 | 223 | * Process the form submission. |
6a488035 | 224 | * |
6a488035 | 225 | * |
355ba699 | 226 | * @return void |
6a488035 TO |
227 | */ |
228 | public function postProcess() { | |
229 | $params = $this->controller->exportValues($this->getName()); | |
230 | ||
231 | if (!$this->_contactID && isset($params['cms_create_account'])) { | |
232 | foreach ($params as $key => $value) { | |
233 | if (substr($key, 0, 5) == 'email' && !empty($value)) { | |
234 | list($fieldName, $locTypeId) = CRM_Utils_System::explode('-', $key, 2); | |
235 | $isPrimary = 0; | |
236 | if ($locTypeId == 'Primary') { | |
237 | $locTypeDefault = CRM_Core_BAO_LocationType::getDefault(); | |
238 | $locTypeId = NULL; | |
239 | if ($locTypeDefault) { | |
240 | $locTypeId = $locTypeDefault->id; | |
241 | } | |
242 | $isPrimary = 1; | |
243 | } | |
244 | ||
be2fb01f | 245 | $params['email'] = []; |
6a488035 TO |
246 | $params['email'][1]['email'] = $value; |
247 | $params['email'][1]['location_type_id'] = $locTypeId; | |
248 | $params['email'][1]['is_primary'] = $isPrimary; | |
249 | } | |
250 | } | |
251 | } | |
252 | ||
c86d4e7c | 253 | $this->_contactID = CRM_Contact_BAO_Contact::getFirstDuplicateContact($params, 'Individual', 'Unsupervised', [], FALSE); |
03b40a7b | 254 | |
9d9922e7 | 255 | $contactID = CRM_Contact_BAO_Contact::createProfileContact($params, $this->_fields, $this->_contactID); |
6a488035 TO |
256 | $this->set('contactID', $contactID); |
257 | ||
258 | if (!empty($params['email'])) { | |
259 | $params['email'] = $params['email'][1]['email']; | |
260 | } | |
261 | ||
262 | CRM_Contribute_BAO_Contribution_Utils::createCMSUser($params, $contactID, 'email'); | |
263 | } | |
96025800 | 264 | |
6a488035 | 265 | } |