Merge pull request #14662 from eileenmcnaughton/activity_pdf_71
[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 * $Id$
17 *
18 */
19
20 /**
21 * This class generates form components for processing a contribution.
22 */
23 class CRM_PCP_Form_PCPAccount extends CRM_Core_Form {
24
25 /**
26 * Variable defined for Contribution Page Id.
27 * @var int
28 */
29 public $_pageId = NULL;
30 public $_id = NULL;
31 public $_component = NULL;
32
33 /**
34 * Are we in single form mode or wizard mode?
35 *
36 * @var bool
37 */
38 public $_single;
39
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);
47
48 if (!$this->_pageId && $config->userFramework == 'Joomla' && $config->userFrameworkFrontend) {
49 $this->_pageId = $this->_id;
50 }
51
52 if ($this->_id) {
53 $contactID = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $this->_id, 'contact_id');
54 }
55
56 $this->_contactID = isset($contactID) ? $contactID : $session->get('userID');
57 if (!$this->_pageId) {
58 if (!$this->_id) {
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::fatal($msg);
61 }
62 else {
63 $this->_pageId = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $this->_id, 'page_id');
64 }
65 }
66
67 if (!$this->_pageId) {
68 CRM_Core_Error::fatal(ts('Could not find source page id.'));
69 }
70
71 $this->_single = $this->get('single');
72
73 if (!$this->_single) {
74 $this->_single = $session->get('singleForm');
75 }
76
77 $this->set('action', $this->_action);
78 $this->set('page_id', $this->_id);
79 $this->set('component_page_id', $this->_pageId);
80
81 // we do not want to display recently viewed items, so turn off
82 $this->assign('displayRecent', FALSE);
83
84 $this->assign('pcpComponent', $this->_component);
85
86 if ($this->_single) {
87 CRM_Utils_System::setTitle(ts('Update Contact Information'));
88 }
89 }
90
91 /**
92 * @return array
93 */
94 public function setDefaultValues() {
95 $this->_defaults = [];
96 if ($this->_contactID) {
97 foreach ($this->_fields as $name => $dontcare) {
98 $fields[$name] = 1;
99 }
100
101 CRM_Core_BAO_UFGroup::setProfileDefaults($this->_contactID, $fields, $this->_defaults);
102 }
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
109 );
110 }
111 }
112 }
113 return $this->_defaults;
114 }
115
116 /**
117 * Build the form object.
118 *
119 * @return void
120 */
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);
125 }
126 $fields = NULL;
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);
130 }
131 $this->addFormRule(['CRM_PCP_Form_PCPAccount', 'formRule'], $this);
132 }
133 else {
134 CRM_Core_BAO_CMSUser::buildForm($this, $id, TRUE);
135
136 $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD);
137 }
138
139 if ($fields) {
140 $this->assign('fields', $fields);
141 $addCaptcha = FALSE;
142 foreach ($fields as $key => $field) {
143 if (isset($field['data_type']) && $field['data_type'] == 'File') {
144 // ignore file upload fields
145 continue;
146 }
147 CRM_Core_BAO_UFGroup::buildProfile($this, $field, CRM_Profile_Form::MODE_CREATE);
148 $this->_fields[$key] = $field;
149
150 // CRM-11316 Is ReCAPTCHA enabled for this profile AND is this an anonymous visitor
151 if ($field['add_captcha'] && !$this->_contactID) {
152 $addCaptcha = TRUE;
153 }
154 }
155
156 if ($addCaptcha) {
157 $captcha = &CRM_Utils_ReCAPTCHA::singleton();
158 $captcha->add($this);
159 $this->assign('isCaptcha', TRUE);
160 }
161 }
162
163 if ($this->_component == 'contribute') {
164 $this->assign('campaignName', CRM_Contribute_PseudoConstant::contributionPage($this->_pageId));
165 }
166 elseif ($this->_component == 'event') {
167 $this->assign('campaignName', CRM_Event_PseudoConstant::event($this->_pageId));
168 }
169
170 if ($this->_single) {
171 $button = [
172 [
173 'type' => 'next',
174 'name' => ts('Save'),
175 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
176 'isDefault' => TRUE,
177 ],
178 [
179 'type' => 'cancel',
180 'name' => ts('Cancel'),
181 ],
182 ];
183 }
184 else {
185 $button[] = [
186 'type' => 'next',
187 'name' => ts('Continue'),
188 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
189 'isDefault' => TRUE,
190 ];
191 }
192 $this->addFormRule(['CRM_PCP_Form_PCPAccount', 'formRule'], $this);
193 $this->addButtons($button);
194 }
195
196 /**
197 * Global form rule.
198 *
199 * @param array $fields
200 * The input form values.
201 * @param array $files
202 * The uploaded files if any.
203 * @param $self
204 *
205 *
206 * @return bool|array
207 * true if no errors, else array of errors
208 */
209 public static function formRule($fields, $files, $self) {
210 $errors = [];
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.');
216 }
217 }
218 }
219 return empty($errors) ? TRUE : $errors;
220 }
221
222 /**
223 * Process the form submission.
224 *
225 *
226 * @return void
227 */
228 public function postProcess() {
229 $params = $this->controller->exportValues($this->getName());
230
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);
235 $isPrimary = 0;
236 if ($locTypeId == 'Primary') {
237 $locTypeDefault = CRM_Core_BAO_LocationType::getDefault();
238 $locTypeId = NULL;
239 if ($locTypeDefault) {
240 $locTypeId = $locTypeDefault->id;
241 }
242 $isPrimary = 1;
243 }
244
245 $params['email'] = [];
246 $params['email'][1]['email'] = $value;
247 $params['email'][1]['location_type_id'] = $locTypeId;
248 $params['email'][1]['is_primary'] = $isPrimary;
249 }
250 }
251 }
252
253 $this->_contactID = CRM_Contact_BAO_Contact::getFirstDuplicateContact($params, 'Individual', 'Unsupervised', [], FALSE);
254
255 $contactID = CRM_Contact_BAO_Contact::createProfileContact($params, $this->_fields, $this->_contactID);
256 $this->set('contactID', $contactID);
257
258 if (!empty($params['email'])) {
259 $params['email'] = $params['email'][1]['email'];
260 }
261
262 CRM_Contribute_BAO_Contribution_Utils::createCMSUser($params, $contactID, 'email');
263 }
264
265 }