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?
39 * Get the active UFGroups (profiles) on this form
40 * Many forms load one or more UFGroups (profiles).
41 * This provides a standard function to retrieve the IDs of those profiles from the form
42 * so that you can implement things such as "is is_captcha field set on any of the active profiles on this form?"
44 * NOT SUPPORTED FOR USE OUTSIDE CORE EXTENSIONS - Added for reCAPTCHA core extension.
48 public function getUFGroupIDs() {
50 if (!empty($this->_pageId
)) {
51 $ufGroupIDs[] = CRM_PCP_BAO_PCP
::getSupporterProfileId($this->_pageId
, $this->_component
);
56 public function preProcess() {
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);
61 $this->_component
= CRM_Utils_Request
::retrieve('component', 'String', $this);
62 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
64 if (!$this->_pageId
&& $config->userFramework
== 'Joomla' && $config->userFrameworkFrontend
) {
65 $this->_pageId
= $this->_id
;
69 $contactID = CRM_Core_DAO
::getFieldValue('CRM_PCP_DAO_PCP', $this->_id
, 'contact_id');
72 $this->_contactID
= $contactID ?? CRM_Core_Session
::getLoggedInContactID();
73 if (!$this->_pageId
) {
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
::statusBounce($msg);
79 $this->_pageId
= CRM_Core_DAO
::getFieldValue('CRM_PCP_DAO_PCP', $this->_id
, 'page_id');
83 if (!$this->_pageId
) {
84 CRM_Core_Error
::statusBounce(ts('Could not find source page id.'));
87 $this->_single
= $this->get('single');
89 if (!$this->_single
) {
90 $this->_single
= $session->get('singleForm');
93 $this->set('action', $this->_action
);
94 $this->set('page_id', $this->_id
);
95 $this->set('component_page_id', $this->_pageId
);
97 // we do not want to display recently viewed items, so turn off
98 $this->assign('displayRecent', FALSE);
100 $this->assign('pcpComponent', $this->_component
);
102 if ($this->_single
) {
103 $this->setTitle(ts('Update Contact Information'));
110 public function setDefaultValues() {
111 $this->_defaults
= [];
112 if ($this->_contactID
) {
113 foreach ($this->_fields
as $name => $dontcare) {
117 CRM_Core_BAO_UFGroup
::setProfileDefaults($this->_contactID
, $fields, $this->_defaults
);
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
129 return $this->_defaults
;
133 * Build the form object.
137 public function buildQuickForm() {
138 $ufGroupID = CRM_PCP_BAO_PCP
::getSupporterProfileId($this->_pageId
, $this->_component
);
139 if (CRM_PCP_BAO_PCP
::checkEmailProfile($ufGroupID)) {
140 $this->assign('profileDisplay', TRUE);
143 if ($this->_contactID
) {
144 if (CRM_Core_BAO_UFGroup
::filterUFGroups($ufGroupID, $this->_contactID
)) {
145 $fields = CRM_Core_BAO_UFGroup
::getFields($ufGroupID, FALSE, CRM_Core_Action
::ADD
);
147 $this->addFormRule(['CRM_PCP_Form_PCPAccount', 'formRule'], $this);
150 CRM_Core_BAO_CMSUser
::buildForm($this, $ufGroupID, TRUE);
152 $fields = CRM_Core_BAO_UFGroup
::getFields($ufGroupID, FALSE, CRM_Core_Action
::ADD
);
156 $this->assign('fields', $fields);
158 foreach ($fields as $key => $field) {
159 if (isset($field['data_type']) && $field['data_type'] == 'File') {
160 // ignore file upload fields
163 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, CRM_Profile_Form
::MODE_CREATE
);
164 $this->_fields
[$key] = $field;
166 // CRM-11316 Is ReCAPTCHA enabled for this profile AND is this an anonymous visitor
167 if ($field['add_captcha'] && !$this->_contactID
) {
173 CRM_Utils_ReCAPTCHA
::enableCaptchaOnForm($this);
177 if ($this->_component
== 'contribute') {
178 $this->assign('campaignName', CRM_Contribute_PseudoConstant
::contributionPage($this->_pageId
));
180 elseif ($this->_component
== 'event') {
181 $this->assign('campaignName', CRM_Event_PseudoConstant
::event($this->_pageId
));
184 if ($this->_single
) {
188 'name' => ts('Save'),
189 'spacing' => ' ',
194 'name' => ts('Cancel'),
201 'name' => ts('Continue'),
202 'spacing' => ' ',
206 $this->addFormRule(['CRM_PCP_Form_PCPAccount', 'formRule'], $this);
207 $this->addButtons($button);
213 * @param array $fields
214 * The input form values.
215 * @param array $files
216 * The uploaded files if any.
221 * true if no errors, else array of errors
223 public static function formRule($fields, $files, $self) {
225 foreach ($fields as $key => $value) {
226 if (strpos($key, 'email-') !== FALSE && !empty($value)) {
227 $ufContactId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFMatch', $value, 'contact_id', 'uf_name');
228 if ($ufContactId && $ufContactId != $self->_contactID
) {
229 $errors[$key] = ts('There is already an user associated with this email address. Please enter different email address.');
233 return empty($errors) ?
TRUE : $errors;
237 * Process the form submission.
242 public function postProcess() {
243 $params = $this->controller
->exportValues($this->getName());
245 if (!$this->_contactID
&& isset($params['cms_create_account'])) {
246 foreach ($params as $key => $value) {
247 if (substr($key, 0, 5) == 'email' && !empty($value)) {
248 list($fieldName, $locTypeId) = CRM_Utils_System
::explode('-', $key, 2);
250 if ($locTypeId == 'Primary') {
251 $locTypeDefault = CRM_Core_BAO_LocationType
::getDefault();
253 if ($locTypeDefault) {
254 $locTypeId = $locTypeDefault->id
;
259 $params['email'] = [];
260 $params['email'][1]['email'] = $value;
261 $params['email'][1]['location_type_id'] = $locTypeId;
262 $params['email'][1]['is_primary'] = $isPrimary;
267 $this->_contactID
= CRM_Contact_BAO_Contact
::getFirstDuplicateContact($params, 'Individual', 'Unsupervised', [], FALSE);
269 $contactID = CRM_Contact_BAO_Contact
::createProfileContact($params, $this->_fields
, $this->_contactID
);
270 $this->set('contactID', $contactID);
272 if (!empty($params['email'])) {
273 $params['email'] = $params['email'][1]['email'];
276 CRM_Contribute_BAO_Contribution_Utils
::createCMSUser($params, $contactID, 'email');