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