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
21 * This class generates form components for processing a contribution.
23 class CRM_PCP_Form_PCPAccount
extends CRM_Core_Form
{
26 * Variable defined for Contribution Page Id.
29 public $_pageId = NULL;
31 public $_component = NULL;
34 * Are we in single form mode or wizard mode?
40 public function preProcess() {
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);
45 $this->_component
= CRM_Utils_Request
::retrieve('component', 'String', $this);
46 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
48 if (!$this->_pageId
&& $config->userFramework
== 'Joomla' && $config->userFrameworkFrontend
) {
49 $this->_pageId
= $this->_id
;
53 $contactID = CRM_Core_DAO
::getFieldValue('CRM_PCP_DAO_PCP', $this->_id
, 'contact_id');
56 $this->_contactID
= $contactID ?? CRM_Core_Session
::getLoggedInContactID();
57 if (!$this->_pageId
) {
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
::statusBounce($msg);
63 $this->_pageId
= CRM_Core_DAO
::getFieldValue('CRM_PCP_DAO_PCP', $this->_id
, 'page_id');
67 if (!$this->_pageId
) {
68 CRM_Core_Error
::statusBounce(ts('Could not find source page id.'));
71 $this->_single
= $this->get('single');
73 if (!$this->_single
) {
74 $this->_single
= $session->get('singleForm');
77 $this->set('action', $this->_action
);
78 $this->set('page_id', $this->_id
);
79 $this->set('component_page_id', $this->_pageId
);
81 // we do not want to display recently viewed items, so turn off
82 $this->assign('displayRecent', FALSE);
84 $this->assign('pcpComponent', $this->_component
);
87 CRM_Utils_System
::setTitle(ts('Update Contact Information'));
94 public function setDefaultValues() {
95 $this->_defaults
= [];
96 if ($this->_contactID
) {
97 foreach ($this->_fields
as $name => $dontcare) {
101 CRM_Core_BAO_UFGroup
::setProfileDefaults($this->_contactID
, $fields, $this->_defaults
);
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
113 return $this->_defaults
;
117 * Build the form object.
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);
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
);
131 $this->addFormRule(['CRM_PCP_Form_PCPAccount', 'formRule'], $this);
134 CRM_Core_BAO_CMSUser
::buildForm($this, $id, TRUE);
136 $fields = CRM_Core_BAO_UFGroup
::getFields($id, FALSE, CRM_Core_Action
::ADD
);
140 $this->assign('fields', $fields);
142 foreach ($fields as $key => $field) {
143 if (isset($field['data_type']) && $field['data_type'] == 'File') {
144 // ignore file upload fields
147 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, CRM_Profile_Form
::MODE_CREATE
);
148 $this->_fields
[$key] = $field;
150 // CRM-11316 Is ReCAPTCHA enabled for this profile AND is this an anonymous visitor
151 if ($field['add_captcha'] && !$this->_contactID
) {
157 $captcha = &CRM_Utils_ReCAPTCHA
::singleton();
158 $captcha->add($this);
159 $this->assign('isCaptcha', TRUE);
163 if ($this->_component
== 'contribute') {
164 $this->assign('campaignName', CRM_Contribute_PseudoConstant
::contributionPage($this->_pageId
));
166 elseif ($this->_component
== 'event') {
167 $this->assign('campaignName', CRM_Event_PseudoConstant
::event($this->_pageId
));
170 if ($this->_single
) {
174 'name' => ts('Save'),
175 'spacing' => ' ',
180 'name' => ts('Cancel'),
187 'name' => ts('Continue'),
188 'spacing' => ' ',
192 $this->addFormRule(['CRM_PCP_Form_PCPAccount', 'formRule'], $this);
193 $this->addButtons($button);
199 * @param array $fields
200 * The input form values.
201 * @param array $files
202 * The uploaded files if any.
207 * true if no errors, else array of errors
209 public static function formRule($fields, $files, $self) {
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.');
219 return empty($errors) ?
TRUE : $errors;
223 * Process the form submission.
228 public function postProcess() {
229 $params = $this->controller
->exportValues($this->getName());
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);
236 if ($locTypeId == 'Primary') {
237 $locTypeDefault = CRM_Core_BAO_LocationType
::getDefault();
239 if ($locTypeDefault) {
240 $locTypeId = $locTypeDefault->id
;
245 $params['email'] = [];
246 $params['email'][1]['email'] = $value;
247 $params['email'][1]['location_type_id'] = $locTypeId;
248 $params['email'][1]['is_primary'] = $isPrimary;
253 $this->_contactID
= CRM_Contact_BAO_Contact
::getFirstDuplicateContact($params, 'Individual', 'Unsupervised', [], FALSE);
255 $contactID = CRM_Contact_BAO_Contact
::createProfileContact($params, $this->_fields
, $this->_contactID
);
256 $this->set('contactID', $contactID);
258 if (!empty($params['email'])) {
259 $params['email'] = $params['email'][1]['email'];
262 CRM_Contribute_BAO_Contribution_Utils
::createCMSUser($params, $contactID, 'email');