Merge pull request #18299 from tunbola/email-separate-activity
[civicrm-core.git] / CRM / PCP / Form / PCPAccount.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
ca87146b 19 * This class generates form components for processing a contribution.
6a488035
TO
20 */
21class CRM_PCP_Form_PCPAccount extends CRM_Core_Form {
22
23 /**
fe482240 24 * Variable defined for Contribution Page Id.
c86d4e7c 25 * @var int
6a488035 26 */
6a488035
TO
27 public $_pageId = NULL;
28 public $_id = NULL;
29 public $_component = NULL;
30
31 /**
100fef9d 32 * Are we in single form mode or wizard mode?
6a488035 33 *
b67daa72 34 * @var bool
6a488035
TO
35 */
36 public $_single;
37
38 public function preProcess() {
353ffa53
TO
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);
6a488035 43 $this->_component = CRM_Utils_Request::retrieve('component', 'String', $this);
353ffa53 44 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
6a488035 45
9dcce349 46 if (!$this->_pageId && $config->userFramework == 'Joomla' && $config->userFrameworkFrontend) {
6a488035
TO
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
f0c3f9bc 54 $this->_contactID = $contactID ?? CRM_Core_Session::getLoggedInContactID();
6a488035
TO
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.');
79e11805 58 CRM_Core_Error::statusBounce($msg);
6a488035
TO
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) {
79e11805 66 CRM_Core_Error::statusBounce(ts('Could not find source page id.'));
6a488035
TO
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
e0ef6999
EM
89 /**
90 * @return array
91 */
00be9182 92 public function setDefaultValues() {
be2fb01f 93 $this->_defaults = [];
f49b8be4
RN
94 if ($this->_contactID) {
95 foreach ($this->_fields as $name => $dontcare) {
96 $fields[$name] = 1;
97 }
2efcf0c2 98
f49b8be4 99 CRM_Core_BAO_UFGroup::setProfileDefaults($this->_contactID, $fields, $this->_defaults);
6a488035 100 }
6a488035
TO
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 }
6a488035 110 }
6a488035
TO
111 return $this->_defaults;
112 }
113
114 /**
fe482240 115 * Build the form object.
6a488035 116 *
355ba699 117 * @return void
6a488035
TO
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 }
be2fb01f 129 $this->addFormRule(['CRM_PCP_Form_PCPAccount', 'formRule'], $this);
6a488035
TO
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;
71fc6ea4
DG
147
148 // CRM-11316 Is ReCAPTCHA enabled for this profile AND is this an anonymous visitor
149 if ($field['add_captcha'] && !$this->_contactID) {
6a488035
TO
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
6a488035
TO
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) {
be2fb01f
CW
169 $button = [
170 [
353ffa53 171 'type' => 'next',
6a488035
TO
172 'name' => ts('Save'),
173 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
174 'isDefault' => TRUE,
be2fb01f
CW
175 ],
176 [
6a488035
TO
177 'type' => 'cancel',
178 'name' => ts('Cancel'),
be2fb01f
CW
179 ],
180 ];
6a488035
TO
181 }
182 else {
be2fb01f 183 $button[] = [
6a488035 184 'type' => 'next',
f212d37d 185 'name' => ts('Continue'),
6a488035
TO
186 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
187 'isDefault' => TRUE,
be2fb01f 188 ];
6a488035 189 }
be2fb01f 190 $this->addFormRule(['CRM_PCP_Form_PCPAccount', 'formRule'], $this);
6a488035
TO
191 $this->addButtons($button);
192 }
193
194 /**
fe482240 195 * Global form rule.
6a488035 196 *
db95eff6
TO
197 * @param array $fields
198 * The input form values.
199 * @param array $files
200 * The uploaded files if any.
fd31fa4c
EM
201 * @param $self
202 *
6a488035 203 *
72b3a70c
CW
204 * @return bool|array
205 * true if no errors, else array of errors
6a488035 206 */
00be9182 207 public static function formRule($fields, $files, $self) {
be2fb01f 208 $errors = [];
6a488035
TO
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 /**
fe482240 221 * Process the form submission.
6a488035 222 *
6a488035 223 *
355ba699 224 * @return void
6a488035
TO
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
be2fb01f 243 $params['email'] = [];
6a488035
TO
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
c86d4e7c 251 $this->_contactID = CRM_Contact_BAO_Contact::getFirstDuplicateContact($params, 'Individual', 'Unsupervised', [], FALSE);
03b40a7b 252
9d9922e7 253 $contactID = CRM_Contact_BAO_Contact::createProfileContact($params, $this->_fields, $this->_contactID);
6a488035
TO
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 }
96025800 262
6a488035 263}