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