3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class generates form components for processing a contribution.
21 class CRM_PCP_Form_PCPAccount
extends CRM_Core_Form
{
24 * Variable defined for Contribution Page Id.
27 public $_pageId = NULL;
29 public $_component = NULL;
32 * Are we in single form mode or wizard mode?
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);
46 if (!$this->_pageId
&& $config->userFramework
== 'Joomla' && $config->userFrameworkFrontend
) {
47 $this->_pageId
= $this->_id
;
51 $contactID = CRM_Core_DAO
::getFieldValue('CRM_PCP_DAO_PCP', $this->_id
, 'contact_id');
54 $this->_contactID
= $contactID ?? CRM_Core_Session
::getLoggedInContactID();
55 if (!$this->_pageId
) {
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);
61 $this->_pageId
= CRM_Core_DAO
::getFieldValue('CRM_PCP_DAO_PCP', $this->_id
, 'page_id');
65 if (!$this->_pageId
) {
66 CRM_Core_Error
::statusBounce(ts('Could not find source page id.'));
69 $this->_single
= $this->get('single');
71 if (!$this->_single
) {
72 $this->_single
= $session->get('singleForm');
75 $this->set('action', $this->_action
);
76 $this->set('page_id', $this->_id
);
77 $this->set('component_page_id', $this->_pageId
);
79 // we do not want to display recently viewed items, so turn off
80 $this->assign('displayRecent', FALSE);
82 $this->assign('pcpComponent', $this->_component
);
85 CRM_Utils_System
::setTitle(ts('Update Contact Information'));
92 public function setDefaultValues() {
93 $this->_defaults
= [];
94 if ($this->_contactID
) {
95 foreach ($this->_fields
as $name => $dontcare) {
99 CRM_Core_BAO_UFGroup
::setProfileDefaults($this->_contactID
, $fields, $this->_defaults
);
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
111 return $this->_defaults
;
115 * Build the form object.
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);
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
);
129 $this->addFormRule(['CRM_PCP_Form_PCPAccount', 'formRule'], $this);
132 CRM_Core_BAO_CMSUser
::buildForm($this, $id, TRUE);
134 $fields = CRM_Core_BAO_UFGroup
::getFields($id, FALSE, CRM_Core_Action
::ADD
);
138 $this->assign('fields', $fields);
140 foreach ($fields as $key => $field) {
141 if (isset($field['data_type']) && $field['data_type'] == 'File') {
142 // ignore file upload fields
145 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, CRM_Profile_Form
::MODE_CREATE
);
146 $this->_fields
[$key] = $field;
148 // CRM-11316 Is ReCAPTCHA enabled for this profile AND is this an anonymous visitor
149 if ($field['add_captcha'] && !$this->_contactID
) {
155 $captcha = &CRM_Utils_ReCAPTCHA
::singleton();
156 $captcha->add($this);
157 $this->assign('isCaptcha', TRUE);
161 if ($this->_component
== 'contribute') {
162 $this->assign('campaignName', CRM_Contribute_PseudoConstant
::contributionPage($this->_pageId
));
164 elseif ($this->_component
== 'event') {
165 $this->assign('campaignName', CRM_Event_PseudoConstant
::event($this->_pageId
));
168 if ($this->_single
) {
172 'name' => ts('Save'),
173 'spacing' => ' ',
178 'name' => ts('Cancel'),
185 'name' => ts('Continue'),
186 'spacing' => ' ',
190 $this->addFormRule(['CRM_PCP_Form_PCPAccount', 'formRule'], $this);
191 $this->addButtons($button);
197 * @param array $fields
198 * The input form values.
199 * @param array $files
200 * The uploaded files if any.
205 * true if no errors, else array of errors
207 public static function formRule($fields, $files, $self) {
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.');
217 return empty($errors) ?
TRUE : $errors;
221 * Process the form submission.
226 public function postProcess() {
227 $params = $this->controller
->exportValues($this->getName());
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);
234 if ($locTypeId == 'Primary') {
235 $locTypeDefault = CRM_Core_BAO_LocationType
::getDefault();
237 if ($locTypeDefault) {
238 $locTypeId = $locTypeDefault->id
;
243 $params['email'] = [];
244 $params['email'][1]['email'] = $value;
245 $params['email'][1]['location_type_id'] = $locTypeId;
246 $params['email'][1]['is_primary'] = $isPrimary;
251 $this->_contactID
= CRM_Contact_BAO_Contact
::getFirstDuplicateContact($params, 'Individual', 'Unsupervised', [], FALSE);
253 $contactID = CRM_Contact_BAO_Contact
::createProfileContact($params, $this->_fields
, $this->_contactID
);
254 $this->set('contactID', $contactID);
256 if (!empty($params['email'])) {
257 $params['email'] = $params['email'][1]['email'];
260 CRM_Contribute_BAO_Contribution_Utils
::createCMSUser($params, $contactID, 'email');