Commit | Line | Data |
---|---|---|
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 | ||
19 | /** | |
20 | * This class generates form components for custom data | |
21 | * | |
22 | * It delegates the work to lower level subclasses and integrates the changes | |
23 | * back in. It also uses a lot of functionality with the CRM API's, so any change | |
24 | * made here could potentially affect the API etc. Be careful, be aware, use unit tests. | |
25 | * | |
26 | */ | |
27 | class CRM_Profile_Form extends CRM_Core_Form { | |
7da04cde | 28 | const |
6a488035 | 29 | MODE_REGISTER = 1, |
353ffa53 TO |
30 | MODE_SEARCH = 2, |
31 | MODE_CREATE = 4, | |
32 | MODE_EDIT = 8; | |
6a488035 TO |
33 | |
34 | protected $_mode; | |
35 | ||
36 | protected $_skipPermission = FALSE; | |
37 | ||
38 | /** | |
fe482240 | 39 | * The contact id that we are editing. |
6a488035 TO |
40 | * |
41 | * @var int | |
42 | */ | |
43 | protected $_id; | |
44 | ||
45 | /** | |
fe482240 | 46 | * The group id that we are editing. |
6a488035 TO |
47 | * |
48 | * @var int | |
49 | */ | |
50 | protected $_gid; | |
51 | ||
a8387f19 | 52 | /** |
a3dbd1bc | 53 | * @var array details of the UFGroup used on this page |
a8387f19 | 54 | */ |
be2fb01f | 55 | protected $_ufGroup = ['name' => 'unknown']; |
a8387f19 | 56 | |
6a488035 | 57 | /** |
fe482240 | 58 | * The group id that we are passing in url. |
6a488035 TO |
59 | * |
60 | * @var int | |
61 | */ | |
62 | public $_grid; | |
63 | ||
426ac5e0 | 64 | /** |
fe482240 | 65 | * Name of button for saving matching contacts. |
c86d4e7c | 66 | * @var string |
426ac5e0 EM |
67 | */ |
68 | protected $_duplicateButtonName; | |
6a488035 | 69 | /** |
fe482240 | 70 | * The title of the category we are editing. |
6a488035 TO |
71 | * |
72 | * @var string | |
73 | */ | |
74 | protected $_title; | |
75 | ||
76 | /** | |
fe482240 | 77 | * The fields needed to build this form. |
6a488035 TO |
78 | * |
79 | * @var array | |
80 | */ | |
81 | public $_fields; | |
82 | ||
83 | /** | |
fe482240 | 84 | * store contact details. |
6a488035 TO |
85 | * |
86 | * @var array | |
87 | */ | |
88 | protected $_contact; | |
89 | ||
6a488035 | 90 | /** |
fe482240 | 91 | * Do we allow updates of the contact. |
6a488035 TO |
92 | * |
93 | * @var int | |
94 | */ | |
95 | public $_isUpdateDupe = 0; | |
96 | ||
90d804ed ML |
97 | /** |
98 | * Dedupe using a specific rule (CRM-6131). | |
99 | * Not currently exposed in profile settings, but can be set in a buildForm hook. | |
c86d4e7c | 100 | * @var int |
90d804ed ML |
101 | */ |
102 | public $_ruleGroupID = NULL; | |
103 | ||
6a488035 TO |
104 | public $_isAddCaptcha = FALSE; |
105 | ||
106 | protected $_isPermissionedChecksum = FALSE; | |
107 | ||
108 | /** | |
abdd5e3e | 109 | * THe context from which we came from, allows us to go there if redirect not set. |
6a488035 TO |
110 | * |
111 | * @var string | |
112 | */ | |
113 | protected $_context; | |
114 | ||
115 | /** | |
fe482240 | 116 | * THe contact type for registration case. |
6a488035 TO |
117 | * |
118 | * @var string | |
119 | */ | |
120 | protected $_ctype = NULL; | |
121 | ||
6a488035 TO |
122 | /** |
123 | * Store profile ids if multiple profile ids are passed using comma separated. | |
abdd5e3e | 124 | * Currently lets implement this functionality only for dialog mode. |
c86d4e7c | 125 | * @var array |
6a488035 | 126 | */ |
be2fb01f | 127 | protected $_profileIds = []; |
6a488035 TO |
128 | |
129 | /** | |
130 | * Contact profile having activity fields? | |
131 | * | |
132 | * @var string | |
133 | */ | |
134 | protected $_isContactActivityProfile = FALSE; | |
135 | ||
136 | /** | |
fe482240 | 137 | * Activity Id connected to the profile. |
6a488035 TO |
138 | * |
139 | * @var string | |
140 | */ | |
141 | protected $_activityId = NULL; | |
142 | ||
143 | ||
144 | protected $_multiRecordFields = NULL; | |
145 | ||
146 | protected $_recordId = NULL; | |
147 | ||
148 | /** | |
abdd5e3e | 149 | * Action for multi record profile (create/edit/delete). |
6a488035 TO |
150 | * |
151 | * @var string | |
152 | */ | |
153 | protected $_multiRecord = NULL; | |
154 | ||
155 | protected $_multiRecordProfile = FALSE; | |
156 | ||
157 | protected $_recordExists = TRUE; | |
158 | ||
159 | protected $_customGroupTitle = NULL; | |
160 | ||
161 | protected $_deleteButtonName = NULL; | |
162 | ||
163 | protected $_customGroupId = NULL; | |
164 | ||
165 | protected $_currentUserID = NULL; | |
353ffa53 | 166 | protected $_session = NULL; |
d5965a37 | 167 | |
58ba7cf7 | 168 | /** |
169 | * Check for any duplicates. | |
170 | * | |
171 | * Depending on form settings & usage scenario we potentially use the found id, | |
172 | * create links to found ids or add an error. | |
173 | * | |
174 | * @param array $errors | |
175 | * @param array $fields | |
176 | * @param CRM_Profile_Form $form | |
177 | * | |
178 | * @return array | |
179 | */ | |
180 | protected static function handleDuplicateChecking(&$errors, $fields, $form) { | |
181 | if ($form->_mode == CRM_Profile_Form::MODE_CREATE) { | |
182 | // fix for CRM-2888 | |
183 | $exceptions = []; | |
184 | } | |
185 | else { | |
186 | // for edit mode we need to allow our own record to be a dupe match! | |
d35e06da | 187 | $exceptions = [CRM_Core_Session::singleton()->get('userID')]; |
58ba7cf7 | 188 | } |
189 | $contactType = CRM_Core_BAO_UFGroup::getContactType($form->_gid); | |
190 | // If all profile fields is of Contact Type then consider | |
191 | // profile is of Individual type(default). | |
192 | if (!$contactType) { | |
193 | $contactType = 'Individual'; | |
194 | } | |
195 | ||
196 | $ids = CRM_Contact_BAO_Contact::getDuplicateContacts( | |
197 | $fields, $contactType, | |
198 | ($form->_context === 'dialog' ? 'Supervised' : 'Unsupervised'), | |
199 | $exceptions, | |
200 | FALSE, | |
201 | $form->_ruleGroupID | |
202 | ); | |
203 | if ($ids) { | |
204 | if ($form->_isUpdateDupe == 2) { | |
205 | CRM_Core_Session::setStatus(ts('Note: this contact may be a duplicate of an existing record.'), ts('Possible Duplicate Detected'), 'alert'); | |
206 | } | |
207 | elseif ($form->_isUpdateDupe == 1) { | |
7c06861a | 208 | $form->_id = $ids[0]; |
58ba7cf7 | 209 | } |
210 | else { | |
211 | if ($form->_context == 'dialog') { | |
212 | $contactLinks = CRM_Contact_BAO_Contact_Utils::formatContactIDSToLinks($ids, TRUE, TRUE); | |
213 | ||
214 | $duplicateContactsLinks = '<div class="matching-contacts-found">'; | |
215 | $duplicateContactsLinks .= ts('One matching contact was found. ', [ | |
216 | 'count' => count($contactLinks['rows']), | |
217 | 'plural' => '%count matching contacts were found.<br />', | |
218 | ]); | |
219 | if ($contactLinks['msg'] == 'view') { | |
220 | $duplicateContactsLinks .= ts('You can View the existing contact.', [ | |
221 | 'count' => count($contactLinks['rows']), | |
222 | 'plural' => 'You can View the existing contacts.', | |
223 | ]); | |
224 | } | |
225 | else { | |
226 | $duplicateContactsLinks .= ts('You can View or Edit the existing contact.', [ | |
227 | 'count' => count($contactLinks['rows']), | |
228 | 'plural' => 'You can View or Edit the existing contacts.', | |
229 | ]); | |
230 | } | |
231 | $duplicateContactsLinks .= '</div>'; | |
232 | $duplicateContactsLinks .= '<table class="matching-contacts-actions">'; | |
233 | $row = ''; | |
234 | for ($i = 0; $i < count($contactLinks['rows']); $i++) { | |
235 | $row .= ' <tr> '; | |
236 | $row .= ' <td class="matching-contacts-name"> '; | |
237 | $row .= $contactLinks['rows'][$i]['display_name']; | |
238 | $row .= ' </td>'; | |
239 | $row .= ' <td class="matching-contacts-email"> '; | |
240 | $row .= $contactLinks['rows'][$i]['primary_email']; | |
241 | $row .= ' </td>'; | |
242 | $row .= ' <td class="action-items"> '; | |
243 | $row .= $contactLinks['rows'][$i]['view'] . ' '; | |
244 | $row .= $contactLinks['rows'][$i]['edit']; | |
245 | $row .= ' </td>'; | |
246 | $row .= ' </tr> '; | |
247 | } | |
248 | ||
249 | $duplicateContactsLinks .= $row . '</table>'; | |
250 | $duplicateContactsLinks .= "If you're sure this record is not a duplicate, click the 'Save Matching Contact' button below."; | |
251 | ||
252 | $errors['_qf_default'] = $duplicateContactsLinks; | |
253 | ||
254 | // let smarty know that there are duplicates | |
255 | $template = CRM_Core_Smarty::singleton(); | |
256 | $template->assign('isDuplicate', 1); | |
257 | } | |
258 | else { | |
259 | $errors['_qf_default'] = ts('A record already exists with the same information.'); | |
260 | } | |
261 | } | |
262 | } | |
263 | return $errors; | |
264 | } | |
265 | ||
6e62b28c TM |
266 | /** |
267 | * Explicitly declare the entity api name. | |
268 | */ | |
269 | public function getDefaultEntity() { | |
270 | return 'Profile'; | |
271 | } | |
6a488035 TO |
272 | |
273 | /** | |
100fef9d | 274 | * Pre processing work done here. |
6a488035 TO |
275 | * |
276 | * gets session variables for table name, id of entity in table, type of entity and stores them. | |
6a488035 | 277 | */ |
00be9182 | 278 | public function preProcess() { |
353ffa53 | 279 | $this->_id = $this->get('id'); |
6a488035 | 280 | $this->_profileIds = $this->get('profileIds'); |
353ffa53 | 281 | $this->_grid = CRM_Utils_Request::retrieve('grid', 'Integer', $this); |
edc80cda | 282 | $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this); |
6a488035 TO |
283 | |
284 | //unset from session when $_GET doesn't have it | |
285 | //except when the form is submitted | |
286 | if (empty($_POST)) { | |
287 | if (!array_key_exists('multiRecord', $_GET)) { | |
288 | $this->set('multiRecord', NULL); | |
289 | } | |
290 | if (!array_key_exists('recordId', $_GET)) { | |
291 | $this->set('recordId', NULL); | |
292 | } | |
293 | } | |
294 | ||
d35e06da | 295 | $this->_currentUserID = CRM_Core_Session::singleton()->get('userID'); |
6a488035 TO |
296 | |
297 | if ($this->_mode == self::MODE_EDIT) { | |
298 | //specifies the action being done on a multi record field | |
299 | $multiRecordAction = CRM_Utils_Request::retrieve('multiRecord', 'String', $this); | |
dc98079b | 300 | $this->_multiRecord = (!is_numeric($multiRecordAction)) ? CRM_Core_Action::resolve($multiRecordAction) : $multiRecordAction; |
6a488035 TO |
301 | if ($this->_multiRecord) { |
302 | $this->set('multiRecord', $this->_multiRecord); | |
303 | } | |
304 | ||
305 | if ($this->_multiRecord && | |
be2fb01f | 306 | !in_array($this->_multiRecord, [CRM_Core_Action::UPDATE, CRM_Core_Action::ADD, CRM_Core_Action::DELETE]) |
353ffa53 | 307 | ) { |
6a488035 TO |
308 | CRM_Core_Error::fatal(ts('Proper action not specified for this custom value record profile')); |
309 | } | |
310 | } | |
311 | $this->_duplicateButtonName = $this->getButtonName('upload', 'duplicate'); | |
312 | ||
a3dbd1bc | 313 | $gids = explode(',', CRM_Utils_Request::retrieve('gid', 'String', CRM_Core_DAO::$_nullObject, FALSE, 0)); |
6a488035 TO |
314 | |
315 | if ((count($gids) > 1) && !$this->_profileIds && empty($this->_profileIds)) { | |
316 | if (!empty($gids)) { | |
317 | foreach ($gids as $pfId) { | |
318 | $this->_profileIds[] = CRM_Utils_Type::escape($pfId, 'Positive'); | |
319 | } | |
320 | } | |
321 | ||
322 | // check if we are rendering mixed profiles | |
323 | if (CRM_Core_BAO_UFGroup::checkForMixProfiles($this->_profileIds)) { | |
324 | CRM_Core_Error::fatal(ts('You cannot combine profiles of multiple types.')); | |
325 | } | |
326 | ||
327 | // for now consider 1'st profile as primary profile and validate it | |
328 | // i.e check for profile type etc. | |
329 | // FIX ME: validations for other than primary | |
330 | $this->_gid = $this->_profileIds[0]; | |
331 | $this->set('gid', $this->_gid); | |
332 | $this->set('profileIds', $this->_profileIds); | |
333 | } | |
334 | ||
335 | if (!$this->_gid) { | |
a3dbd1bc TO |
336 | $this->_gid = CRM_Utils_Request::retrieve('gid', 'Positive', $this, FALSE, 0); |
337 | $this->set('gid', $this->_gid); | |
338 | } | |
339 | ||
6a488035 TO |
340 | $this->_activityId = CRM_Utils_Request::retrieve('aid', 'Positive', $this, FALSE, 0, 'GET'); |
341 | if (is_numeric($this->_activityId)) { | |
342 | $latestRevisionId = CRM_Activity_BAO_Activity::getLatestActivityId($this->_activityId); | |
343 | if ($latestRevisionId) { | |
344 | $this->_activityId = $latestRevisionId; | |
345 | } | |
346 | } | |
347 | $this->_isContactActivityProfile = CRM_Core_BAO_UFField::checkContactActivityProfileType($this->_gid); | |
348 | ||
abdd5e3e | 349 | //get values for ufGroupName, captcha and dupe update. |
6a488035 TO |
350 | if ($this->_gid) { |
351 | $dao = new CRM_Core_DAO_UFGroup(); | |
352 | $dao->id = $this->_gid; | |
353 | if ($dao->find(TRUE)) { | |
354 | $this->_isUpdateDupe = $dao->is_update_dupe; | |
355 | $this->_isAddCaptcha = $dao->add_captcha; | |
a3dbd1bc | 356 | $this->_ufGroup = (array) $dao; |
6a488035 | 357 | } |
a3dbd1bc | 358 | |
cacc57d7 | 359 | if (!CRM_Utils_Array::value('is_active', $this->_ufGroup)) { |
be2fb01f | 360 | CRM_Core_Error::fatal(ts('The requested profile (gid=%1) is inactive or does not exist.', [ |
cacc57d7 | 361 | 1 => $this->_gid, |
be2fb01f | 362 | ])); |
cacc57d7 | 363 | } |
a3dbd1bc | 364 | } |
cacc57d7 | 365 | $this->assign('ufGroupName', $this->_ufGroup['name']); |
6a488035 TO |
366 | |
367 | $gids = empty($this->_profileIds) ? $this->_gid : $this->_profileIds; | |
368 | ||
abdd5e3e | 369 | // if we don't have a gid use the default, else just use that specific gid |
6a488035 TO |
370 | if (($this->_mode == self::MODE_REGISTER || $this->_mode == self::MODE_CREATE) && !$this->_gid) { |
371 | $this->_ctype = CRM_Utils_Request::retrieve('ctype', 'String', $this, FALSE, 'Individual', 'REQUEST'); | |
372 | $this->_fields = CRM_Core_BAO_UFGroup::getRegistrationFields($this->_action, $this->_mode, $this->_ctype); | |
373 | } | |
374 | elseif ($this->_mode == self::MODE_SEARCH) { | |
375 | $this->_fields = CRM_Core_BAO_UFGroup::getListingFields($this->_action, | |
376 | CRM_Core_BAO_UFGroup::PUBLIC_VISIBILITY | CRM_Core_BAO_UFGroup::LISTINGS_VISIBILITY, | |
377 | FALSE, | |
378 | $gids, | |
379 | TRUE, NULL, | |
380 | $this->_skipPermission, | |
381 | CRM_Core_Permission::SEARCH | |
382 | ); | |
383 | } | |
384 | else { | |
385 | $this->_fields = CRM_Core_BAO_UFGroup::getFields($gids, FALSE, NULL, | |
386 | NULL, NULL, | |
387 | FALSE, NULL, | |
388 | $this->_skipPermission, | |
389 | NULL, | |
390 | ($this->_action == CRM_Core_Action::ADD) ? CRM_Core_Permission::CREATE : CRM_Core_Permission::EDIT | |
391 | ); | |
392 | $multiRecordFieldListing = FALSE; | |
abdd5e3e | 393 | //using selector for listing of multi-record fields |
6a488035 TO |
394 | if ($this->_mode == self::MODE_EDIT && $this->_gid) { |
395 | CRM_Core_BAO_UFGroup::shiftMultiRecordFields($this->_fields, $this->_multiRecordFields); | |
396 | ||
397 | if ($this->_multiRecord) { | |
398 | if ($this->_multiRecord != CRM_Core_Action::ADD) { | |
399 | $this->_recordId = CRM_Utils_Request::retrieve('recordId', 'Positive', $this); | |
0db6c3e1 TO |
400 | } |
401 | else { | |
6a488035 TO |
402 | $this->_recordId = NULL; |
403 | $this->set('recordId', NULL); | |
404 | } | |
b44e3f84 | 405 | //record id is necessary for _multiRecord view and update/edit action |
6a488035 | 406 | if (!$this->_recordId |
353ffa53 TO |
407 | && ($this->_multiRecord == CRM_Core_Action::UPDATE || $this->_multiRecord == CRM_Core_Action::DELETE) |
408 | ) { | |
6a488035 | 409 | CRM_Core_Error::fatal(ts('The requested Profile (gid=%1) requires record id while performing this action', |
be2fb01f | 410 | [1 => $this->_gid] |
6a488035 | 411 | )); |
0db6c3e1 TO |
412 | } |
413 | elseif (empty($this->_multiRecordFields)) { | |
dc98079b | 414 | CRM_Core_Error::fatal(ts('No Multi-Record Fields configured for this profile (gid=%1)', |
be2fb01f | 415 | [1 => $this->_gid] |
6a488035 TO |
416 | )); |
417 | } | |
418 | ||
419 | $fieldId = CRM_Core_BAO_CustomField::getKeyID(key($this->_multiRecordFields)); | |
be2fb01f | 420 | $customGroupDetails = CRM_Core_BAO_CustomGroup::getGroupTitles([$fieldId]); |
6a488035 TO |
421 | $this->_customGroupTitle = $customGroupDetails[$fieldId]['groupTitle']; |
422 | $this->_customGroupId = $customGroupDetails[$fieldId]['groupID']; | |
423 | ||
424 | if ($this->_multiRecord == CRM_Core_Action::UPDATE || $this->_multiRecord == CRM_Core_Action::DELETE) { | |
425 | //record exists check | |
426 | foreach ($this->_multiRecordFields as $key => $field) { | |
427 | $fieldIds[] = CRM_Core_BAO_CustomField::getKeyID($key); | |
428 | } | |
429 | $getValues = CRM_Core_BAO_CustomValueTable::getEntityValues($this->_id, NULL, $fieldIds, TRUE); | |
430 | ||
431 | if (array_key_exists($this->_recordId, $getValues)) { | |
432 | $this->_recordExists = TRUE; | |
0db6c3e1 TO |
433 | } |
434 | else { | |
6a488035 TO |
435 | $this->_recordExists = FALSE; |
436 | if ($this->_multiRecord & CRM_Core_Action::UPDATE) { | |
be2fb01f | 437 | CRM_Core_Session::setStatus(ts('Note: The record %1 doesnot exists. Upon save a new record will be create', [1 => $this->_recordId]), ts('Record doesnot exist'), 'alert'); |
6a488035 TO |
438 | } |
439 | } | |
440 | } | |
441 | if ($this->_multiRecord & CRM_Core_Action::ADD) { | |
442 | $this->_maxRecordLimit = CRM_Core_BAO_CustomGroup::hasReachedMaxLimit($customGroupDetails[$fieldId]['groupID'], $this->_id); | |
443 | if ($this->_maxRecordLimit) { | |
444 | CRM_Core_Session::setStatus(ts('You cannot add a new record as maximum allowed limit is reached'), ts('Sorry'), 'error'); | |
445 | } | |
446 | } | |
447 | ||
0db6c3e1 TO |
448 | } |
449 | elseif (!empty($this->_multiRecordFields) | |
be2fb01f | 450 | && (!$this->_multiRecord || !in_array($this->_multiRecord, [ |
c5c263ca AH |
451 | CRM_Core_Action::DELETE, |
452 | CRM_Core_Action::UPDATE, | |
be2fb01f | 453 | ])) |
353ffa53 | 454 | ) { |
96ed17aa | 455 | CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js', 1, 'html-header'); |
abdd5e3e | 456 | //multi-record listing page |
6a488035 TO |
457 | $multiRecordFieldListing = TRUE; |
458 | $page = new CRM_Profile_Page_MultipleRecordFieldsListing(); | |
459 | $cs = $this->get('cs'); | |
460 | $page->set('pageCheckSum', $cs); | |
461 | $page->set('contactId', $this->_id); | |
462 | $page->set('profileId', $this->_gid); | |
463 | $page->set('action', CRM_Core_Action::BROWSE); | |
464 | $page->set('multiRecordFieldListing', $multiRecordFieldListing); | |
465 | $page->run(); | |
466 | } | |
467 | } | |
468 | $this->assign('multiRecordFieldListing', $multiRecordFieldListing); | |
469 | ||
470 | // is profile double-opt in? | |
a7488080 | 471 | if (!empty($this->_fields['group']) && |
6a488035 TO |
472 | CRM_Core_BAO_UFGroup::isProfileDoubleOptin() |
473 | ) { | |
474 | $emailField = FALSE; | |
475 | foreach ($this->_fields as $name => $values) { | |
476 | if (substr($name, 0, 6) == 'email-') { | |
477 | $emailField = TRUE; | |
478 | } | |
479 | } | |
480 | ||
481 | if (!$emailField) { | |
482 | $status = ts("Email field should be included in profile if you want to use Group(s) when Profile double-opt in process is enabled."); | |
d35e06da | 483 | CRM_Core_Session::singleton()->setStatus($status); |
6a488035 TO |
484 | } |
485 | } | |
486 | ||
426ac5e0 | 487 | //transferring all the multi-record custom fields in _fields |
6a488035 TO |
488 | if ($this->_multiRecord && !empty($this->_multiRecordFields)) { |
489 | $this->_fields = $this->_multiRecordFields; | |
490 | $this->_multiRecordProfile = TRUE; | |
0db6c3e1 TO |
491 | } |
492 | elseif ($this->_multiRecord && empty($this->_multiRecordFields)) { | |
6a488035 | 493 | CRM_Core_Session::setStatus(ts('This feature is not currently available.'), ts('Sorry'), 'error'); |
426ac5e0 | 494 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm', 'reset=1')); |
6a488035 TO |
495 | } |
496 | } | |
497 | ||
498 | if (!is_array($this->_fields)) { | |
499 | CRM_Core_Session::setStatus(ts('This feature is not currently available.'), ts('Sorry'), 'error'); | |
426ac5e0 | 500 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm', 'reset=1')); |
6a488035 TO |
501 | } |
502 | } | |
503 | ||
504 | /** | |
c490a46a | 505 | * Set default values for the form. Note that in edit/view mode |
6a488035 TO |
506 | * the default values are retrieved from the database |
507 | * | |
6a488035 | 508 | */ |
00be9182 | 509 | public function setDefaultsValues() { |
be2fb01f | 510 | $this->_defaults = []; |
6a488035 TO |
511 | if ($this->_multiRecordProfile && ($this->_multiRecord == CRM_Core_Action::DELETE)) { |
512 | return; | |
513 | } | |
514 | ||
515 | if ($this->_mode != self::MODE_SEARCH) { | |
516 | // set default values for country / state to start with | |
517 | CRM_Core_BAO_UFGroup::setRegisterDefaults($this->_fields, $this->_defaults); | |
518 | } | |
519 | ||
520 | if ($this->_id && !$this->_multiRecordProfile) { | |
521 | if ($this->_isContactActivityProfile) { | |
be2fb01f | 522 | $contactFields = $activityFields = []; |
6a488035 TO |
523 | foreach ($this->_fields as $fieldName => $field) { |
524 | if (CRM_Utils_Array::value('field_type', $field) == 'Activity') { | |
525 | $activityFields[$fieldName] = $field; | |
526 | } | |
527 | else { | |
528 | $contactFields[$fieldName] = $field; | |
529 | } | |
530 | } | |
531 | ||
532 | CRM_Core_BAO_UFGroup::setProfileDefaults($this->_id, $contactFields, $this->_defaults, TRUE); | |
533 | if ($this->_activityId) { | |
534 | CRM_Core_BAO_UFGroup::setComponentDefaults($activityFields, $this->_activityId, 'Activity', $this->_defaults, TRUE); | |
535 | } | |
536 | } | |
537 | else { | |
538 | CRM_Core_BAO_UFGroup::setProfileDefaults($this->_id, $this->_fields, $this->_defaults, TRUE); | |
539 | } | |
540 | } | |
541 | ||
542 | //set custom field defaults | |
543 | if ($this->_multiRecordProfile) { | |
544 | foreach ($this->_multiRecordFields as $key => $field) { | |
545 | $fieldIds[] = CRM_Core_BAO_CustomField::getKeyID($key); | |
546 | } | |
547 | ||
be2fb01f | 548 | $defaultValues = []; |
6a488035 TO |
549 | if ($this->_multiRecord && $this->_multiRecord == CRM_Core_Action::UPDATE) { |
550 | $defaultValues = CRM_Core_BAO_CustomValueTable::getEntityValues($this->_id, NULL, $fieldIds, TRUE); | |
551 | if ($this->_recordExists == TRUE) { | |
552 | $defaultValues = $defaultValues[$this->_recordId]; | |
0db6c3e1 TO |
553 | } |
554 | else { | |
6a488035 TO |
555 | $defaultValues = NULL; |
556 | } | |
557 | } | |
558 | ||
559 | if (!empty($defaultValues)) { | |
560 | foreach ($defaultValues as $key => $value) { | |
561 | $name = "custom_{$key}"; | |
562 | $htmlType = $this->_multiRecordFields[$name]['html_type']; | |
563 | if ($htmlType != 'File') { | |
564 | if (isset($value)) { | |
565 | CRM_Core_BAO_CustomField::setProfileDefaults($key, | |
566 | $name, | |
567 | $this->_defaults, | |
568 | $this->_id, | |
569 | $this->_mode, | |
570 | $value | |
571 | ); | |
0db6c3e1 TO |
572 | } |
573 | else { | |
6a488035 TO |
574 | $this->_defaults[$name] = ""; |
575 | } | |
576 | } | |
577 | ||
578 | if ($htmlType == 'File') { | |
579 | $entityId = $this->_id; | |
580 | if (CRM_Utils_Array::value('field_type', $field) == 'Activity' && | |
353ffa53 TO |
581 | $this->_activityId |
582 | ) { | |
6a488035 TO |
583 | $entityId = $this->_activityId; |
584 | } | |
f3e5959f BS |
585 | |
586 | $url = ''; | |
587 | if (isset($value)) { | |
588 | $url = CRM_Core_BAO_CustomField::getFileURL($entityId, $key, $value); | |
589 | } | |
6a488035 TO |
590 | |
591 | if ($url) { | |
592 | $customFiles[$name]['displayURL'] = ts("Attached File") . ": {$url['file_url']}"; | |
593 | ||
594 | $deleteExtra = ts("Are you sure you want to delete attached file?"); | |
353ffa53 | 595 | $fileId = $url['file_id']; |
ff9aeadb | 596 | $fileHash = CRM_Core_BAO_File::generateFileHash($entityId, $fileId); |
353ffa53 | 597 | $deleteURL = CRM_Utils_System::url('civicrm/file', |
ff9aeadb | 598 | "reset=1&id={$fileId}&eid=$entityId&fid={$key}&action=delete&fcs={$fileHash}" |
353ffa53 | 599 | ); |
6a488035 TO |
600 | $text = ts("Delete Attached File"); |
601 | $customFiles[$field['name']]['deleteURL'] = "<a href=\"{$deleteURL}\" onclick = \"if (confirm( ' $deleteExtra ' )) this.href+='&confirmed=1'; else return false;\">$text</a>"; | |
602 | ||
603 | // also delete the required rule that we've set on the form element | |
604 | $this->removeFileRequiredRules($name); | |
605 | } | |
606 | } | |
607 | } | |
608 | } | |
455d8c42 | 609 | } |
610 | else { | |
6a488035 TO |
611 | foreach ($this->_fields as $name => $field) { |
612 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) { | |
613 | $htmlType = $field['html_type']; | |
614 | if ((!isset($this->_defaults[$name]) || $htmlType == 'File') && | |
353ffa53 TO |
615 | (CRM_Utils_Array::value('field_type', $field) != 'Activity') |
616 | ) { | |
6a488035 TO |
617 | CRM_Core_BAO_CustomField::setProfileDefaults($customFieldID, |
618 | $name, | |
619 | $this->_defaults, | |
620 | $this->_id, | |
621 | $this->_mode | |
622 | ); | |
623 | } | |
624 | ||
625 | if ($htmlType == 'File') { | |
626 | $entityId = $this->_id; | |
455d8c42 | 627 | if (CRM_Utils_Array::value('field_type', $field) == 'Activity' && $this->_activityId) { |
6a488035 TO |
628 | $entityId = $this->_activityId; |
629 | } | |
630 | $url = CRM_Core_BAO_CustomField::getFileURL($entityId, $customFieldID); | |
631 | ||
632 | if ($url) { | |
633 | $customFiles[$field['name']]['displayURL'] = ts("Attached File") . ": {$url['file_url']}"; | |
634 | ||
635 | $deleteExtra = ts("Are you sure you want to delete attached file?"); | |
353ffa53 | 636 | $fileId = $url['file_id']; |
e382506a | 637 | $fileHash = CRM_Core_BAO_File::generateFileHash($entityId, $fileId); /* fieldId=$customFieldID */ |
353ffa53 | 638 | $deleteURL = CRM_Utils_System::url('civicrm/file', |
e382506a | 639 | "reset=1&id={$fileId}&eid=$entityId&fid={$customFieldID}&action=delete&fcs={$fileHash}" |
6a488035 TO |
640 | ); |
641 | $text = ts("Delete Attached File"); | |
642 | $customFiles[$field['name']]['deleteURL'] = "<a href=\"{$deleteURL}\" onclick = \"if (confirm( ' $deleteExtra ' )) this.href+='&confirmed=1'; else return false;\">$text</a>"; | |
643 | ||
644 | // also delete the required rule that we've set on the form element | |
645 | $this->removeFileRequiredRules($field['name']); | |
646 | } | |
647 | } | |
648 | } | |
649 | } | |
650 | } | |
651 | if (isset($customFiles)) { | |
652 | $this->assign('customFiles', $customFiles); | |
653 | } | |
654 | ||
655 | if ($this->_multiRecordProfile) { | |
656 | $this->setDefaults($this->_defaults); | |
657 | return; | |
658 | } | |
659 | ||
a7488080 | 660 | if (!empty($this->_defaults['image_URL'])) { |
c3821398 | 661 | $this->assign("imageURL", CRM_Utils_File::getImageURL($this->_defaults['image_URL'])); |
455d8c42 | 662 | $this->removeFileRequiredRules('image_URL'); |
6a488035 TO |
663 | } |
664 | ||
6a488035 TO |
665 | $this->setDefaults($this->_defaults); |
666 | } | |
667 | ||
668 | /** | |
fe482240 | 669 | * Build the form object. |
6a488035 | 670 | * |
6a488035 TO |
671 | */ |
672 | public function buildQuickForm() { | |
a3dbd1bc TO |
673 | $this->add('hidden', 'gid', $this->_gid); |
674 | ||
a8387f19 TO |
675 | switch ($this->_mode) { |
676 | case self::MODE_CREATE: | |
677 | case self::MODE_EDIT: | |
678 | case self::MODE_REGISTER: | |
a3dbd1bc | 679 | CRM_Utils_Hook::buildProfile($this->_ufGroup['name']); |
a8387f19 | 680 | break; |
dc98079b | 681 | |
a8387f19 | 682 | case self::MODE_SEARCH: |
a3dbd1bc | 683 | CRM_Utils_Hook::searchProfile($this->_ufGroup['name']); |
a8387f19 | 684 | break; |
dc98079b | 685 | |
a8387f19 TO |
686 | default: |
687 | } | |
688 | ||
6a488035 TO |
689 | //lets have single status message, CRM-4363 |
690 | $return = FALSE; | |
691 | $statusMessage = NULL; | |
692 | if (($this->_multiRecord & CRM_Core_Action::ADD) && $this->_maxRecordLimit) { | |
693 | return; | |
694 | } | |
695 | ||
696 | if (($this->_multiRecord & CRM_Core_Action::DELETE)) { | |
697 | if (!$this->_recordExists) { | |
be2fb01f | 698 | CRM_Core_Session::setStatus(ts('The record %1 doesnot exists', [1 => $this->_recordId]), ts('Record doesnot exists'), 'alert'); |
6a488035 TO |
699 | } |
700 | else { | |
701 | $this->assign('deleteRecord', TRUE); | |
702 | } | |
703 | return; | |
704 | } | |
705 | ||
706 | CRM_Core_BAO_Address::checkContactSharedAddressFields($this->_fields, $this->_id); | |
707 | ||
708 | // we should not allow component and mix profiles in search mode | |
709 | if ($this->_mode != self::MODE_REGISTER) { | |
710 | //check for mix profile fields (eg: individual + other contact type) | |
711 | if (CRM_Core_BAO_UFField::checkProfileType($this->_gid)) { | |
712 | if (($this->_mode & self::MODE_EDIT) && $this->_isContactActivityProfile) { | |
713 | $errors = self::validateContactActivityProfile($this->_activityId, $this->_id, $this->_gid); | |
714 | if (!empty($errors)) { | |
715 | $statusMessage = array_pop($errors); | |
716 | $return = TRUE; | |
717 | } | |
718 | } | |
719 | else { | |
720 | $statusMessage = ts('Profile search, view and edit are not supported for Profiles which include fields for more than one record type.'); | |
721 | $return = TRUE; | |
722 | } | |
723 | } | |
724 | ||
725 | $profileType = CRM_Core_BAO_UFField::getProfileType($this->_gid); | |
726 | ||
727 | if ($this->_id) { | |
728 | $contactTypes = CRM_Contact_BAO_Contact::getContactTypes($this->_id); | |
353ffa53 | 729 | $contactType = $contactTypes[0]; |
6a488035 TO |
730 | |
731 | array_shift($contactTypes); | |
732 | $contactSubtypes = $contactTypes; | |
733 | ||
734 | $profileSubType = FALSE; | |
735 | if (CRM_Contact_BAO_ContactType::isaSubType($profileType)) { | |
736 | $profileSubType = $profileType; | |
737 | $profileType = CRM_Contact_BAO_ContactType::getBasicType($profileType); | |
738 | } | |
739 | ||
740 | if ( | |
741 | ($profileType != 'Contact' && !$this->_isContactActivityProfile) && | |
dc98079b | 742 | (($profileSubType && !empty($contactSubtypes) && (!in_array($profileSubType, $contactSubtypes))) || |
6a488035 TO |
743 | ($profileType != $contactType)) |
744 | ) { | |
745 | $return = TRUE; | |
746 | if (!$statusMessage) { | |
d5cc0fc2 | 747 | $statusMessage = ts("This profile is configured for contact type '%1'. It cannot be used to edit contacts of other types.", |
be2fb01f | 748 | [1 => $profileSubType ? $profileSubType : $profileType]); |
6a488035 TO |
749 | } |
750 | } | |
751 | } | |
752 | ||
753 | if ( | |
353ffa53 TO |
754 | in_array( |
755 | $profileType, | |
be2fb01f | 756 | ["Membership", "Participant", "Contribution"] |
353ffa53 | 757 | ) |
6a488035 TO |
758 | ) { |
759 | $return = TRUE; | |
760 | if (!$statusMessage) { | |
761 | $statusMessage = ts('Profile is not configured for the selected action.'); | |
762 | } | |
763 | } | |
764 | } | |
765 | ||
426ac5e0 | 766 | //lets have single status message, |
6a488035 TO |
767 | $this->assign('statusMessage', $statusMessage); |
768 | if ($return) { | |
769 | return FALSE; | |
770 | } | |
771 | ||
772 | $this->assign('id', $this->_id); | |
773 | $this->assign('mode', $this->_mode); | |
774 | $this->assign('action', $this->_action); | |
775 | $this->assign('fields', $this->_fields); | |
776 | $this->assign('fieldset', (isset($this->_fieldset)) ? $this->_fieldset : ""); | |
777 | ||
778 | // should we restrict what we display | |
779 | $admin = TRUE; | |
780 | if ($this->_mode == self::MODE_EDIT) { | |
781 | $admin = FALSE; | |
abdd5e3e | 782 | // show all fields that are visible: |
6a488035 TO |
783 | // if we are a admin OR the same user OR acl-user with access to the profile |
784 | // or we have checksum access to this contact (i.e. the user without a login) - CRM-5909 | |
785 | if ( | |
786 | CRM_Core_Permission::check('administer users') || | |
787 | $this->_id == $this->_currentUserID || | |
788 | $this->_isPermissionedChecksum || | |
789 | in_array( | |
790 | $this->_gid, | |
791 | CRM_ACL_API::group( | |
792 | CRM_Core_Permission::EDIT, | |
793 | NULL, | |
794 | 'civicrm_uf_group', | |
ff4f7744 | 795 | CRM_Core_PseudoConstant::get('CRM_Core_DAO_UFField', 'uf_group_id') |
6a488035 TO |
796 | ) |
797 | ) | |
798 | ) { | |
799 | $admin = TRUE; | |
800 | } | |
801 | } | |
802 | ||
803 | // if false, user is not logged-in. | |
804 | $anonUser = FALSE; | |
805 | if (!$this->_currentUserID) { | |
806 | $defaultLocationType = CRM_Core_BAO_LocationType::getDefault(); | |
807 | $primaryLocationType = $defaultLocationType->id; | |
353ffa53 | 808 | $anonUser = TRUE; |
6a488035 TO |
809 | } |
810 | $this->assign('anonUser', $anonUser); | |
811 | ||
be2fb01f | 812 | $addCaptcha = []; |
6a488035 TO |
813 | $emailPresent = FALSE; |
814 | ||
6a488035 TO |
815 | // add the form elements |
816 | foreach ($this->_fields as $name => $field) { | |
817 | // make sure that there is enough permission to expose this field | |
818 | if (!$admin && $field['visibility'] == 'User and User Admin Only') { | |
819 | unset($this->_fields[$name]); | |
820 | continue; | |
821 | } | |
822 | ||
823 | // since the CMS manages the email field, suppress the email display if in | |
824 | // register mode which occur within the CMS form | |
825 | if ($this->_mode == self::MODE_REGISTER && substr($name, 0, 5) == 'email') { | |
826 | unset($this->_fields[$name]); | |
827 | continue; | |
828 | } | |
829 | ||
830 | list($prefixName, $index) = CRM_Utils_System::explode('-', $name, 2); | |
6a488035 TO |
831 | |
832 | CRM_Core_BAO_UFGroup::buildProfile($this, $field, $this->_mode); | |
833 | ||
dd78acd9 TO |
834 | if ($field['add_to_group_id']) { |
835 | $addToGroupId = $field['add_to_group_id']; | |
836 | } | |
837 | ||
6a488035 TO |
838 | //build array for captcha |
839 | if ($field['add_captcha']) { | |
840 | $addCaptcha[$field['group_id']] = $field['add_captcha']; | |
841 | } | |
842 | ||
843 | if (($name == 'email-Primary') || ($name == 'email-' . isset($primaryLocationType) ? $primaryLocationType : "")) { | |
844 | $emailPresent = TRUE; | |
845 | $this->_mail = $name; | |
846 | } | |
847 | } | |
848 | ||
849 | // add captcha only for create mode. | |
850 | if ($this->_mode == self::MODE_CREATE) { | |
851 | // suppress captcha for logged in users only | |
852 | if ($this->_currentUserID) { | |
853 | $this->_isAddCaptcha = FALSE; | |
854 | } | |
4c9b6178 | 855 | elseif (!$this->_isAddCaptcha && !empty($addCaptcha)) { |
6a488035 TO |
856 | $this->_isAddCaptcha = TRUE; |
857 | } | |
858 | ||
859 | if ($this->_gid) { | |
860 | $dao = new CRM_Core_DAO_UFGroup(); | |
861 | $dao->id = $this->_gid; | |
862 | $dao->addSelect(); | |
863 | $dao->addSelect('is_update_dupe'); | |
864 | if ($dao->find(TRUE)) { | |
865 | if ($dao->is_update_dupe) { | |
866 | $this->_isUpdateDupe = $dao->is_update_dupe; | |
867 | } | |
868 | } | |
869 | } | |
870 | } | |
871 | else { | |
872 | $this->_isAddCaptcha = FALSE; | |
873 | } | |
874 | ||
875 | //finally add captcha to form. | |
876 | if ($this->_isAddCaptcha) { | |
268ff0e8 | 877 | CRM_Utils_ReCAPTCHA::enableCaptchaOnForm($this); |
6a488035 | 878 | } |
6a488035 | 879 | |
dd78acd9 TO |
880 | if ($this->_mode != self::MODE_SEARCH) { |
881 | if (isset($addToGroupId)) { | |
882 | $this->_ufGroup['add_to_group_id'] = $addToGroupId; | |
883 | } | |
884 | } | |
885 | ||
7f1a247f | 886 | //let's do set defaults for the profile |
887 | $this->setDefaultsValues(); | |
888 | ||
6a488035 | 889 | $action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, NULL); |
2be4a151 BS |
890 | |
891 | if ($this->_mode == self::MODE_CREATE || $this->_mode == self::MODE_EDIT) { | |
6a488035 TO |
892 | CRM_Core_BAO_CMSUser::buildForm($this, $this->_gid, $emailPresent, $action); |
893 | } | |
894 | else { | |
895 | $this->assign('showCMS', FALSE); | |
896 | } | |
897 | ||
898 | $this->assign('groupId', $this->_gid); | |
899 | ||
6a488035 TO |
900 | // if view mode pls freeze it with the done button. |
901 | if ($this->_action & CRM_Core_Action::VIEW) { | |
902 | $this->freeze(); | |
903 | } | |
904 | ||
905 | if ($this->_context == 'dialog') { | |
906 | $this->addElement( | |
907 | 'submit', | |
908 | $this->_duplicateButtonName, | |
909 | ts('Save Matching Contact') | |
910 | ); | |
911 | } | |
912 | } | |
913 | ||
ffd93213 | 914 | /** |
fe482240 | 915 | * Validate profile and provided activity Id. |
c490a46a CW |
916 | * |
917 | * @param int $activityId | |
918 | * @param int $contactId | |
919 | * @param int $gid | |
ffd93213 EM |
920 | * |
921 | * @return array | |
922 | */ | |
00be9182 | 923 | public static function validateContactActivityProfile($activityId, $contactId, $gid) { |
be2fb01f | 924 | $errors = []; |
6a488035 | 925 | if (!$activityId) { |
e60e0c01 | 926 | $errors[] = ts('Profile is using one or more activity fields, and is missing the activity Id (aid) in the URL.'); |
6a488035 TO |
927 | return $errors; |
928 | } | |
929 | ||
be2fb01f CW |
930 | $activityDetails = []; |
931 | $activityParams = ['id' => $activityId]; | |
6a488035 TO |
932 | CRM_Activity_BAO_Activity::retrieve($activityParams, $activityDetails); |
933 | ||
934 | if (empty($activityDetails)) { | |
e60e0c01 | 935 | $errors[] = ts('Invalid Activity Id (aid).'); |
6a488035 TO |
936 | return $errors; |
937 | } | |
938 | ||
939 | $profileActivityTypes = CRM_Core_BAO_UFGroup::groupTypeValues($gid, 'Activity'); | |
940 | ||
8cc574cf | 941 | if ((!empty($profileActivityTypes['Activity']) && |
6a488035 TO |
942 | !in_array($activityDetails['activity_type_id'], $profileActivityTypes['Activity']) |
943 | ) || | |
944 | (!in_array($contactId, $activityDetails['assignee_contact']) && | |
945 | !in_array($contactId, $activityDetails['target_contact']) | |
946 | ) | |
947 | ) { | |
e60e0c01 | 948 | $errors[] = ts('This activity cannot be edited or viewed via this profile.'); |
6a488035 TO |
949 | } |
950 | ||
951 | return $errors; | |
952 | } | |
953 | ||
954 | /** | |
fe482240 | 955 | * Global form rule. |
6a488035 | 956 | * |
68c9fb83 TO |
957 | * @param array $fields |
958 | * The input form values. | |
959 | * @param array $files | |
960 | * The uploaded files if any. | |
961 | * @param CRM_Core_Form $form | |
962 | * The form object. | |
6a488035 | 963 | * |
72b3a70c CW |
964 | * @return bool|array |
965 | * true if no errors, else array of errors | |
6a488035 | 966 | */ |
00be9182 | 967 | public static function formRule($fields, $files, $form) { |
a3dbd1bc | 968 | CRM_Utils_Hook::validateProfile($form->_ufGroup['name']); |
a8387f19 | 969 | |
6a488035 TO |
970 | // if no values, return |
971 | if (empty($fields)) { | |
972 | return TRUE; | |
973 | } | |
974 | ||
58ba7cf7 | 975 | $errors = []; |
6a488035 TO |
976 | $register = NULL; |
977 | ||
978 | // hack we use a -1 in options to indicate that its registration | |
7c06861a | 979 | // ... and I can't remove that comment because even though it's clear as mud |
980 | // perhaps someone will find it helpful in the absence of ANY OTHER EXPLANATION | |
981 | // as to what it means.... | |
6a488035 | 982 | if ($form->_id) { |
7c06861a | 983 | // @todo - wonder if it ever occurred to someone that if they didn't document this param |
984 | // it might not be crystal clear why we have it.... | |
6a488035 TO |
985 | $form->_isUpdateDupe = 1; |
986 | } | |
987 | ||
988 | if ($form->_mode == CRM_Profile_Form::MODE_REGISTER) { | |
989 | $register = TRUE; | |
990 | } | |
991 | ||
abdd5e3e | 992 | // don't check for duplicates during registration validation: CRM-375 |
8cc574cf | 993 | if (!$register && empty($fields['_qf_Edit_upload_duplicate'])) { |
6a488035 | 994 | // fix for CRM-3240 |
a7488080 | 995 | if (!empty($fields['email-Primary'])) { |
6a488035 TO |
996 | $fields['email'] = CRM_Utils_Array::value('email-Primary', $fields); |
997 | } | |
998 | ||
999 | // fix for CRM-6141 | |
8cc574cf | 1000 | if (!empty($fields['phone-Primary-1']) && empty($fields['phone-Primary'])) { |
6a488035 TO |
1001 | $fields['phone-Primary'] = $fields['phone-Primary-1']; |
1002 | } | |
1003 | ||
7c06861a | 1004 | if (!$form->_id) { |
1005 | self::handleDuplicateChecking($errors, $fields, $form); | |
1006 | } | |
6a488035 TO |
1007 | } |
1008 | ||
1009 | foreach ($fields as $key => $value) { | |
1010 | list($fieldName, $locTypeId, $phoneTypeId) = CRM_Utils_System::explode('-', $key, 3); | |
1011 | if ($fieldName == 'state_province' && !empty($fields["country-{$locTypeId}"])) { | |
1012 | // Validate Country - State list | |
1013 | $countryId = $fields["country-{$locTypeId}"]; | |
1014 | $stateProvinceId = $value; | |
1015 | ||
1016 | if ($stateProvinceId && $countryId) { | |
1017 | $stateProvinceDAO = new CRM_Core_DAO_StateProvince(); | |
1018 | $stateProvinceDAO->id = $stateProvinceId; | |
1019 | $stateProvinceDAO->find(TRUE); | |
1020 | ||
1021 | if ($stateProvinceDAO->country_id != $countryId) { | |
1022 | // country mismatch hence display error | |
1023 | $stateProvinces = CRM_Core_PseudoConstant::stateProvince(); | |
353ffa53 TO |
1024 | $countries = CRM_Core_PseudoConstant::country(); |
1025 | $errors[$key] = "State/Province " . $stateProvinces[$stateProvinceId] . " is not part of " . $countries[$countryId] . ". It belongs to " . $countries[$stateProvinceDAO->country_id] . "."; | |
6a488035 TO |
1026 | } |
1027 | } | |
1028 | } | |
1029 | ||
1030 | if ($fieldName == 'county' && $fields["state_province-{$locTypeId}"]) { | |
1031 | // Validate County - State list | |
1032 | $stateProvinceId = $fields["state_province-{$locTypeId}"]; | |
1033 | $countyId = $value; | |
1034 | ||
1035 | if ($countyId && $stateProvinceId) { | |
1036 | $countyDAO = new CRM_Core_DAO_County(); | |
1037 | $countyDAO->id = $countyId; | |
1038 | $countyDAO->find(TRUE); | |
1039 | ||
1040 | if ($countyDAO->state_province_id != $stateProvinceId) { | |
1041 | // state province mismatch hence display error | |
1042 | $stateProvinces = CRM_Core_PseudoConstant::stateProvince(); | |
353ffa53 TO |
1043 | $counties = CRM_Core_PseudoConstant::county(); |
1044 | $errors[$key] = "County " . $counties[$countyId] . " is not part of " . $stateProvinces[$stateProvinceId] . ". It belongs to " . $stateProvinces[$countyDAO->state_province_id] . "."; | |
6a488035 TO |
1045 | } |
1046 | } | |
1047 | } | |
1048 | } | |
1049 | foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) { | |
1050 | if ($greetingType = CRM_Utils_Array::value($greeting, $fields)) { | |
676159c9 | 1051 | $customizedValue = CRM_Core_PseudoConstant::getKey('CRM_Contact_BAO_Contact', $greeting . '_id', 'Customized'); |
6a488035 TO |
1052 | if ($customizedValue == $greetingType && empty($fields[$greeting . '_custom'])) { |
1053 | $errors[$greeting . '_custom'] = ts('Custom %1 is a required field if %1 is of type Customized.', | |
be2fb01f | 1054 | [1 => ucwords(str_replace('_', ' ', $greeting))] |
6a488035 TO |
1055 | ); |
1056 | } | |
1057 | } | |
1058 | } | |
1059 | ||
1060 | return empty($errors) ? TRUE : $errors; | |
1061 | } | |
1062 | ||
1063 | /** | |
1064 | * Process the user submitted custom data values. | |
1065 | * | |
6a488035 TO |
1066 | */ |
1067 | public function postProcess() { | |
1068 | $params = $this->controller->exportValues($this->_name); | |
1069 | ||
1070 | //if the delete record button is clicked | |
1071 | if ($this->_deleteButtonName) { | |
a7488080 | 1072 | if (!empty($_POST[$this->_deleteButtonName]) && $this->_recordId) { |
6a488035 | 1073 | $filterParams['id'] = $this->_customGroupId; |
be2fb01f | 1074 | $returnProperties = ['is_multiple', 'table_name']; |
426ac5e0 | 1075 | CRM_Core_DAO::commonRetrieve("CRM_Core_DAO_CustomGroup", $filterParams, $returnValues, $returnProperties); |
a7488080 | 1076 | if (!empty($returnValues['is_multiple'])) { |
6a488035 TO |
1077 | if ($tableName = CRM_Utils_Array::value('table_name', $returnValues)) { |
1078 | $sql = "DELETE FROM {$tableName} WHERE id = %1 AND entity_id = %2"; | |
be2fb01f CW |
1079 | $sqlParams = [ |
1080 | 1 => [$this->_recordId, 'Integer'], | |
1081 | 2 => [$this->_id, 'Integer'], | |
1082 | ]; | |
dc98079b TO |
1083 | CRM_Core_DAO::executeQuery($sql, $sqlParams); |
1084 | CRM_Core_Session::setStatus(ts('Your record has been deleted.'), ts('Deleted'), 'success'); | |
6a488035 TO |
1085 | } |
1086 | } | |
1087 | return; | |
1088 | } | |
1089 | } | |
a3dbd1bc | 1090 | CRM_Utils_Hook::processProfile($this->_ufGroup['name']); |
a7488080 | 1091 | if (!empty($params['image_URL'])) { |
6a488035 TO |
1092 | CRM_Contact_BAO_Contact::processImageParams($params); |
1093 | } | |
1094 | ||
be2fb01f | 1095 | $greetingTypes = [ |
6a488035 TO |
1096 | 'addressee' => 'addressee_id', |
1097 | 'email_greeting' => 'email_greeting_id', | |
1098 | 'postal_greeting' => 'postal_greeting_id', | |
be2fb01f | 1099 | ]; |
6a488035 | 1100 | |
be2fb01f | 1101 | $details = []; |
6a488035 TO |
1102 | if ($this->_id) { |
1103 | $contactDetails = CRM_Contact_BAO_Contact::getHierContactDetails($this->_id, | |
1104 | $greetingTypes | |
1105 | ); | |
00bb741c | 1106 | $details = $contactDetails[$this->_id]; |
6a488035 | 1107 | } |
8cc574cf | 1108 | if (!(!empty($details['addressee_id']) || !empty($details['email_greeting_id']) || |
353ffa53 TO |
1109 | CRM_Utils_Array::value('postal_greeting_id', $details) |
1110 | ) | |
1111 | ) { | |
6a488035 TO |
1112 | |
1113 | $profileType = CRM_Core_BAO_UFField::getProfileType($this->_gid); | |
1114 | //Though Profile type is contact we need | |
1115 | //Individual/Household/Organization for setting Greetings. | |
1116 | if ($profileType == 'Contact') { | |
1117 | $profileType = 'Individual'; | |
1118 | //if we editing Household/Organization. | |
1119 | if ($this->_id) { | |
1120 | $profileType = CRM_Contact_BAO_Contact::getContactType($this->_id); | |
1121 | } | |
1122 | } | |
1123 | if (CRM_Contact_BAO_ContactType::isaSubType($profileType)) { | |
1124 | $profileType = CRM_Contact_BAO_ContactType::getBasicType($profileType); | |
1125 | } | |
1126 | ||
1127 | foreach ($greetingTypes as $key => $value) { | |
1128 | if (!array_key_exists($key, $params)) { | |
1129 | $params[$key] = CRM_Contact_BAO_Contact_Utils::defaultGreeting($profileType, $key); | |
1130 | } | |
1131 | } | |
1132 | } | |
1133 | ||
1134 | $transaction = new CRM_Core_Transaction(); | |
1135 | ||
426ac5e0 | 1136 | //used to send subscribe mail to the group which user want. |
6a488035 | 1137 | //if the profile double option in is enabled |
be2fb01f | 1138 | $mailingType = []; |
6a488035 TO |
1139 | |
1140 | $result = NULL; | |
1141 | foreach ($params as $name => $values) { | |
1142 | if (substr($name, 0, 6) == 'email-') { | |
1143 | $result['email'] = $values; | |
1144 | } | |
1145 | } | |
1146 | ||
1147 | //array of group id, subscribed by contact | |
be2fb01f | 1148 | $contactGroup = []; |
a7488080 | 1149 | if (!empty($params['group']) && |
6a488035 TO |
1150 | CRM_Core_BAO_UFGroup::isProfileDoubleOptin() |
1151 | ) { | |
be2fb01f | 1152 | $groupSubscribed = []; |
a7488080 | 1153 | if (!empty($result['email'])) { |
6a488035 TO |
1154 | if ($this->_id) { |
1155 | $contactGroups = new CRM_Contact_DAO_GroupContact(); | |
1156 | $contactGroups->contact_id = $this->_id; | |
1157 | $contactGroups->status = 'Added'; | |
1158 | $contactGroups->find(); | |
be2fb01f | 1159 | $contactGroup = []; |
6a488035 TO |
1160 | while ($contactGroups->fetch()) { |
1161 | $contactGroup[] = $contactGroups->group_id; | |
1162 | $groupSubscribed[$contactGroups->group_id] = 1; | |
1163 | } | |
1164 | } | |
1165 | foreach ($params['group'] as $key => $val) { | |
1166 | if (!$val) { | |
1167 | unset($params['group'][$key]); | |
1168 | continue; | |
1169 | } | |
1170 | $groupTypes = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', | |
1171 | $key, 'group_type', 'id' | |
1172 | ); | |
1173 | $groupType = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
1174 | substr($groupTypes, 1, -1) | |
1175 | ); | |
1176 | //filter group of mailing type and unset it from params | |
1177 | if (in_array(2, $groupType)) { | |
1178 | //if group is already subscribed , ignore it | |
1179 | $groupExist = CRM_Utils_Array::key($key, $contactGroup); | |
1180 | if (!isset($groupExist)) { | |
1181 | $mailingType[] = $key; | |
1182 | unset($params['group'][$key]); | |
1183 | } | |
1184 | } | |
1185 | } | |
1186 | } | |
1187 | } | |
1188 | ||
a3dbd1bc | 1189 | $addToGroupId = CRM_Utils_Array::value('add_to_group_id', $this->_ufGroup); |
bada0f66 | 1190 | if (!empty($addToGroupId)) { |
6a488035 TO |
1191 | //run same check whether group is a mailing list |
1192 | $groupTypes = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', | |
1193 | $addToGroupId, 'group_type', 'id' | |
1194 | ); | |
1195 | $groupType = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
1196 | substr($groupTypes, 1, -1) | |
1197 | ); | |
1198 | //filter group of mailing type and unset it from params | |
8cc574cf | 1199 | if (in_array(2, $groupType) && !empty($result['email']) && |
6a488035 TO |
1200 | CRM_Core_BAO_UFGroup::isProfileAddToGroupDoubleOptin() |
1201 | ) { | |
1202 | if (!count($contactGroup)) { | |
1203 | //array of group id, subscribed by contact | |
be2fb01f | 1204 | $contactGroup = []; |
6a488035 TO |
1205 | if ($this->_id) { |
1206 | $contactGroups = new CRM_Contact_DAO_GroupContact(); | |
1207 | $contactGroups->contact_id = $this->_id; | |
1208 | $contactGroups->status = 'Added'; | |
1209 | $contactGroups->find(); | |
be2fb01f | 1210 | $contactGroup = []; |
6a488035 TO |
1211 | while ($contactGroups->fetch()) { |
1212 | $contactGroup[] = $contactGroups->group_id; | |
1213 | $groupSubscribed[$contactGroups->group_id] = 1; | |
1214 | } | |
1215 | } | |
1216 | } | |
1217 | //if group is already subscribed , ignore it | |
1218 | $groupExist = CRM_Utils_Array::key($addToGroupId, $contactGroup); | |
1219 | if (!isset($groupExist)) { | |
1220 | $mailingType[] = $addToGroupId; | |
1221 | $addToGroupId = NULL; | |
1222 | } | |
1223 | } | |
1224 | else { | |
1225 | // since we are directly adding contact to group lets unset it from mailing | |
1226 | if ($key = array_search($addToGroupId, $mailingType)) { | |
1227 | unset($mailingType[$key]); | |
1228 | } | |
1229 | } | |
1230 | } | |
1231 | ||
1232 | if ($this->_grid) { | |
1233 | $params['group'] = $groupSubscribed; | |
1234 | } | |
1235 | ||
1236 | // commenting below code, since we potentially | |
1237 | // triggered maximum name field formatting cases during CRM-4430. | |
1238 | // CRM-4343 | |
1239 | // $params['preserveDBName'] = true; | |
1240 | ||
1241 | $profileFields = $this->_fields; | |
1242 | if (($this->_mode & self::MODE_EDIT) && $this->_activityId && $this->_isContactActivityProfile) { | |
be2fb01f | 1243 | $profileFields = $activityParams = []; |
6a488035 TO |
1244 | foreach ($this->_fields as $fieldName => $field) { |
1245 | if (CRM_Utils_Array::value('field_type', $field) == 'Activity') { | |
1246 | if (isset($params[$fieldName])) { | |
1247 | $activityParams[$fieldName] = $params[$fieldName]; | |
1248 | } | |
1249 | if (isset($params['activity_date_time'])) { | |
1250 | $activityParams['activity_date_time'] = CRM_Utils_Date::processDate($params['activity_date_time'], $params['activity_date_time_time']); | |
1251 | } | |
a7488080 | 1252 | if (!empty($params[$fieldName]) && isset($params["{$fieldName}_id"])) { |
6a488035 TO |
1253 | $activityParams[$fieldName] = $params["{$fieldName}_id"]; |
1254 | } | |
1255 | } | |
1256 | else { | |
1257 | $profileFields[$fieldName] = $field; | |
1258 | } | |
1259 | } | |
1260 | ||
1261 | if (!empty($activityParams)) { | |
1262 | $activityParams['version'] = 3; | |
1263 | $activityParams['id'] = $this->_activityId; | |
1264 | $activityParams['skipRecentView'] = TRUE; | |
1265 | civicrm_api('Activity', 'create', $activityParams); | |
1266 | } | |
1267 | } | |
1268 | ||
1269 | if ($this->_multiRecord && $this->_recordId && $this->_multiRecordFields && $this->_recordExists) { | |
1270 | $params['customRecordValues'][$this->_recordId] = array_keys($this->_multiRecordFields); | |
1271 | } | |
1272 | ||
1273 | $this->_id = CRM_Contact_BAO_Contact::createProfileContact( | |
1274 | $params, | |
1275 | $profileFields, | |
1276 | $this->_id, | |
1277 | $addToGroupId, | |
1278 | $this->_gid, | |
1279 | $this->_ctype, | |
1280 | TRUE | |
1281 | ); | |
1282 | ||
1283 | //mailing type group | |
1284 | if (!empty($mailingType)) { | |
1285 | // we send in the contactID so we match the same groups and are exact, rather than relying on email | |
1286 | // CRM-8710 | |
1287 | CRM_Mailing_Event_BAO_Subscribe::commonSubscribe($mailingType, $result, $this->_id, 'profile'); | |
1288 | } | |
1289 | ||
be2fb01f | 1290 | $ufGroups = []; |
6a488035 TO |
1291 | if ($this->_gid) { |
1292 | $ufGroups[$this->_gid] = 1; | |
1293 | } | |
1294 | elseif ($this->_mode == self::MODE_REGISTER) { | |
1295 | $ufGroups = CRM_Core_BAO_UFGroup::getModuleUFGroup('User Registration'); | |
1296 | } | |
1297 | ||
1298 | foreach ($ufGroups as $gId => $val) { | |
1299 | if ($notify = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $gId, 'notify')) { | |
1300 | $values = CRM_Core_BAO_UFGroup::checkFieldsEmptyValues($gId, $this->_id, NULL); | |
1301 | CRM_Core_BAO_UFGroup::commonSendMail($this->_id, $values); | |
1302 | } | |
1303 | } | |
1304 | ||
1305 | //create CMS user (if CMS user option is selected in profile) | |
a7488080 | 1306 | if (!empty($params['cms_create_account']) && |
8ccd852d | 1307 | ($this->_mode == self::MODE_CREATE || $this->_mode == self::MODE_EDIT) |
6a488035 TO |
1308 | ) { |
1309 | $params['contactID'] = $this->_id; | |
1310 | if (!CRM_Core_BAO_CMSUser::create($params, $this->_mail)) { | |
1311 | CRM_Core_Session::setStatus(ts('Your profile is not saved and Account is not created.'), ts('Profile Error'), 'error'); | |
b24f69db | 1312 | CRM_Core_Error::debug_log_message("Rolling back transaction as CMSUser Create failed in Profile_Form for contact " . $params['contactID']); |
6a488035 TO |
1313 | $transaction->rollback(); |
1314 | return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/profile/create', | |
353ffa53 TO |
1315 | 'reset=1&gid=' . $this->_gid |
1316 | )); | |
6a488035 TO |
1317 | } |
1318 | } | |
1319 | ||
1320 | $transaction->commit(); | |
1321 | } | |
1322 | ||
ffd93213 EM |
1323 | /** |
1324 | * @param null $suffix | |
1325 | * | |
1326 | * @return null|string | |
1327 | */ | |
00be9182 | 1328 | public function checkTemplateFileExists($suffix = NULL) { |
6a488035 TO |
1329 | if ($this->_gid) { |
1330 | $templateFile = "CRM/Profile/Form/{$this->_gid}/{$this->_name}.{$suffix}tpl"; | |
1331 | $template = CRM_Core_Form::getTemplate(); | |
1332 | if ($template->template_exists($templateFile)) { | |
1333 | return $templateFile; | |
1334 | } | |
1335 | ||
1336 | // lets see if we have customized by name | |
1337 | $ufGroupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid, 'name'); | |
1338 | if ($ufGroupName) { | |
1339 | $templateFile = "CRM/Profile/Form/{$ufGroupName}/{$this->_name}.{$suffix}tpl"; | |
1340 | if ($template->template_exists($templateFile)) { | |
1341 | return $templateFile; | |
1342 | } | |
1343 | } | |
1344 | } | |
1345 | return NULL; | |
1346 | } | |
1347 | ||
ffd93213 | 1348 | /** |
fe482240 | 1349 | * Use the form name to create the tpl file name. |
ffd93213 EM |
1350 | * |
1351 | * @return string | |
ffd93213 | 1352 | */ |
00be9182 | 1353 | public function getTemplateFileName() { |
6a488035 TO |
1354 | $fileName = $this->checkTemplateFileExists(); |
1355 | return $fileName ? $fileName : parent::getTemplateFileName(); | |
1356 | } | |
1357 | ||
ffd93213 EM |
1358 | /** |
1359 | * Default extra tpl file basically just replaces .tpl with .extra.tpl | |
1360 | * i.e. we dont override | |
1361 | * | |
1362 | * @return string | |
ffd93213 | 1363 | */ |
00be9182 | 1364 | public function overrideExtraTemplateFileName() { |
6a488035 TO |
1365 | $fileName = $this->checkTemplateFileExists('extra.'); |
1366 | return $fileName ? $fileName : parent::overrideExtraTemplateFileName(); | |
1367 | } | |
96025800 | 1368 | |
6a488035 | 1369 | } |