CRM-19950 : Wrong param passed to checkPermission function
[civicrm-core.git] / CRM / Core / BAO / UFGroup.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
0f03f337 31 * @copyright CiviCRM LLC (c) 2004-2017
6a488035
TO
32 */
33
34/**
8eedd10a 35 * UF group BAO class.
6a488035
TO
36 */
37class CRM_Core_BAO_UFGroup extends CRM_Core_DAO_UFGroup {
7da04cde 38 const PUBLIC_VISIBILITY = 1,
6a488035
TO
39 ADMIN_VISIBILITY = 2,
40 LISTINGS_VISIBILITY = 4;
41
42 /**
fe482240 43 * Cache the match clause used in this transaction.
6a488035
TO
44 *
45 * @var string
46 */
47 static $_matchFields = NULL;
48
49 /**
fe482240 50 * Fetch object based on array of properties.
6a488035 51 *
6a0b768e
TO
52 * @param array $params
53 * (reference) an assoc array of name/value pairs.
54 * @param array $defaults
55 * (reference) an assoc array to hold the flattened values.
6a488035 56 *
a6c01b45
CW
57 * @return object
58 * CRM_Core_DAO_UFGroup object
6a488035 59 */
00be9182 60 public static function retrieve(&$params, &$defaults) {
6a488035
TO
61 return CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_UFGroup', $params, $defaults);
62 }
63
64 /**
65 * Retrieve the first non-generic contact type
66 *
6a0b768e
TO
67 * @param int $id
68 * Id of uf_group.
6a488035 69 *
a6c01b45
CW
70 * @return string
71 * contact type
6a488035 72 */
00be9182 73 public static function getContactType($id) {
6a488035
TO
74
75 $validTypes = array_filter(array_keys(CRM_Core_SelectValues::contactType()));
76 $validSubTypes = CRM_Contact_BAO_ContactType::subTypeInfo();
77
78 $typesParts = explode(CRM_Core_DAO::VALUE_SEPARATOR, CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $id, 'group_type'));
79 $types = explode(',', $typesParts[0]);
80
81 $cType = NULL;
82 foreach ($types as $type) {
83 if (in_array($type, $validTypes)) {
84 $cType = $type;
85 }
86 elseif (array_key_exists($type, $validSubTypes)) {
87 $cType = CRM_Utils_Array::value('parent', $validSubTypes[$type]);
88 }
2aa397bc 89 if ($cType) {
5d6ac993 90 break;
2aa397bc 91 }
6a488035
TO
92 }
93
94 return $cType;
95 }
96
97 /**
98 * Get the form title.
99 *
6a0b768e
TO
100 * @param int $id
101 * Id of uf_form.
6a488035 102 *
a6c01b45
CW
103 * @return string
104 * title
6a488035 105 *
6a488035
TO
106 */
107 public static function getTitle($id) {
108 return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $id, 'title');
109 }
110
111 /**
fe482240 112 * Update the is_active flag in the db.
6a488035 113 *
6a0b768e
TO
114 * @param int $id
115 * Id of the database record.
116 * @param bool $is_active
117 * Value we want to set the is_active field.
6a488035 118 *
a6c01b45
CW
119 * @return Object
120 * CRM_Core_DAO_UFGroup object on success, null otherwise
6a488035 121 */
00be9182 122 public static function setIsActive($id, $is_active) {
6a488035
TO
123 return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_UFGroup', $id, 'is_active', $is_active);
124 }
125
126 /**
fe482240 127 * Get all the registration fields.
6a488035 128 *
6a0b768e
TO
129 * @param int $action
130 * What action are we doing.
131 * @param int $mode
132 * Mode.
da6b46f4
EM
133 *
134 * @param null $ctype
6a488035 135 *
a6c01b45
CW
136 * @return array
137 * the fields that are needed for registration
6a488035 138 */
00be9182 139 public static function getRegistrationFields($action, $mode, $ctype = NULL) {
6a488035
TO
140 if ($mode & CRM_Profile_Form::MODE_REGISTER) {
141 $ufGroups = CRM_Core_BAO_UFGroup::getModuleUFGroup('User Registration');
142 }
143 else {
144 $ufGroups = CRM_Core_BAO_UFGroup::getModuleUFGroup('Profile');
145 }
146
147 if (!is_array($ufGroups)) {
148 return FALSE;
149 }
150
151 $fields = array();
152
153 foreach ($ufGroups as $id => $title) {
154 if ($ctype) {
155 $fieldType = CRM_Core_BAO_UFField::getProfileType($id);
156 if (($fieldType != 'Contact') &&
157 ($fieldType != $ctype) &&
158 !CRM_Contact_BAO_ContactType::isExtendsContactType($fieldType, $ctype)
159 ) {
160 continue;
161 }
162 if (CRM_Contact_BAO_ContactType::isaSubType($fieldType)) {
163 $profileSubType = $fieldType;
164 }
165 }
166
167 $subset = self::getFields($id, TRUE, $action,
168 NULL, NULL, FALSE, NULL, TRUE, $ctype
169 );
170
171 // we do not allow duplicates. the first field is the winner
172 foreach ($subset as $name => $field) {
a7488080 173 if (empty($fields[$name])) {
6a488035
TO
174 $fields[$name] = $field;
175 }
176 }
177 }
178
179 return $fields;
180 }
181
182 /**
fe482240 183 * Get all the listing fields.
6a488035 184 *
6a0b768e
TO
185 * @param int $action
186 * What action are we doing.
187 * @param int $visibility
188 * Visibility of fields we are interested in.
189 * @param bool $considerSelector
190 * Whether to consider the in_selector parameter.
da6b46f4 191 * @param array $ufGroupIds
6a0b768e 192 * @param bool $searchable
6a488035 193 *
da6b46f4
EM
194 * @param null $restrict
195 * @param bool $skipPermission
196 * @param int $permissionType
a6c01b45
CW
197 * @return array
198 * the fields that are listings related
6a488035 199 */
e7483cbe 200 public static function getListingFields(
6a488035
TO
201 $action,
202 $visibility,
203 $considerSelector = FALSE,
5d6ac993
TO
204 $ufGroupIds = NULL,
205 $searchable = NULL,
206 $restrict = NULL,
207 $skipPermission = FALSE,
208 $permissionType = CRM_Core_Permission::SEARCH
6a488035
TO
209 ) {
210 if ($ufGroupIds) {
211 $subset = self::getFields($ufGroupIds, FALSE, $action,
212 $visibility, $searchable,
213 FALSE, $restrict,
214 $skipPermission,
215 NULL,
216 $permissionType
217 );
218 if ($considerSelector) {
219 // drop the fields not meant for the selector
220 foreach ($subset as $name => $field) {
221 if (!$field['in_selector']) {
222 unset($subset[$name]);
223 }
224 }
225 }
226 $fields = $subset;
227 }
228 else {
ff4f7744 229 $ufGroups = CRM_Core_PseudoConstant::get('CRM_Core_DAO_UFField', 'uf_group_id');
6a488035
TO
230
231 $fields = array();
232 foreach ($ufGroups as $id => $title) {
233 $subset = self::getFields($id, FALSE, $action,
234 $visibility, $searchable,
235 FALSE, $restrict,
236 $skipPermission,
237 NULL,
238 $permissionType
239 );
240 if ($considerSelector) {
241 // drop the fields not meant for the selector
242 foreach ($subset as $name => $field) {
4f99ca55
TO
243 if (!$field['in_selector']) {
244 unset($subset[$name]);
2aa397bc 245 }
6a488035
TO
246 }
247 }
248 $fields = array_merge($fields, $subset);
249 }
250 }
251 return $fields;
252 }
253
254 /**
255 * Get all the fields that belong to the group with the name title,
256 * and format for use with buildProfile. This is the SQL analog of
257 * formatUFFields().
258 *
6a0b768e
TO
259 * @param mix $id
260 * The id of the UF group or ids of ufgroup.
fd31fa4c 261 * @param bool|int $register are we interested in registration fields
6a0b768e
TO
262 * @param int $action
263 * What action are we doing.
264 * @param int $visibility
265 * Visibility of fields we are interested in.
266 * @param $searchable
fd31fa4c 267 * @param bool $showAll
6a0b768e
TO
268 * @param string $restrict
269 * Should we restrict based on a specified profile type.
fd31fa4c
EM
270 * @param bool $skipPermission
271 * @param null $ctype
272 * @param int $permissionType
273 * @param string $orderBy
274 * @param null $orderProfiles
275 *
54957108 276 * @param bool $eventProfile
277 *
a6c01b45 278 * @return array
54957108 279 * The fields that belong to this ufgroup(s)
280 * @throws \Exception
6a488035 281 */
e7483cbe 282 public static function getFields(
6a488035
TO
283 $id,
284 $register = FALSE,
285 $action = NULL,
5d6ac993 286 $visibility = NULL,
6a488035 287 $searchable = NULL,
5d6ac993 288 $showAll = FALSE,
6a488035
TO
289 $restrict = NULL,
290 $skipPermission = FALSE,
5d6ac993 291 $ctype = NULL,
6a488035 292 $permissionType = CRM_Core_Permission::CREATE,
5d6ac993 293 $orderBy = 'field_name',
44792363 294 $orderProfiles = NULL,
295 $eventProfile = FALSE
6a488035
TO
296 ) {
297 if (!is_array($id)) {
298 $id = CRM_Utils_Type::escape($id, 'Positive');
299 $profileIds = array($id);
300 }
301 else {
302 $profileIds = $id;
303 }
304
305 $gids = implode(',', $profileIds);
306 $params = array();
307 if ($restrict) {
f9d8a5d1 308 $query = "SELECT g.* from civicrm_uf_group g
309 LEFT JOIN civicrm_uf_join j ON (j.uf_group_id = g.id)
6a488035 310 WHERE g.id IN ( {$gids} )
f9d8a5d1 311 AND ((j.uf_group_id IN ( {$gids} ) AND j.module = %1) OR g.is_reserved = 1 )
6a488035
TO
312 ";
313 $params = array(1 => array($restrict, 'String'));
314 }
315 else {
316 $query = "SELECT g.* from civicrm_uf_group g WHERE g.id IN ( {$gids} ) ";
317 }
318
319 if (!$showAll) {
320 $query .= " AND g.is_active = 1";
321 }
322
44792363 323 $checkPermission = array(
324 array(
325 'administer CiviCRM',
326 'manage event profiles',
327 ),
328 );
329 if ($eventProfile && CRM_Core_Permission::check($checkPermission)) {
330 $skipPermission = TRUE;
331 }
332
6a488035
TO
333 // add permissioning for profiles only if not registration
334 if (!$skipPermission) {
335 $permissionClause = CRM_Core_Permission::ufGroupClause($permissionType, 'g.');
336 $query .= " AND $permissionClause ";
337 }
338
339 if ($orderProfiles AND count($profileIds) > 1) {
340 $query .= " ORDER BY FIELD( g.id, {$gids} )";
341 }
5d6ac993
TO
342 $group = CRM_Core_DAO::executeQuery($query, $params);
343 $fields = array();
6a488035
TO
344 $validGroup = FALSE;
345
346 while ($group->fetch()) {
347 $validGroup = TRUE;
348 $query = self::createUFFieldQuery($group->id, $searchable, $showAll, $visibility, $orderBy);
349 $field = CRM_Core_DAO::executeQuery($query);
350
351 $profileType = CRM_Core_BAO_UFField::getProfileType($group->id);
352 $contactActivityProfile = CRM_Core_BAO_UFField::checkContactActivityProfileType($group->id);
353 $importableFields = self::getImportableFields($showAll, $profileType, $contactActivityProfile);
354 list($customFields, $addressCustomFields) = self::getCustomFields($ctype);
355
356 while ($field->fetch()) {
357 list($name, $formattedField) = self::formatUFField($group, $field, $customFields, $addressCustomFields, $importableFields, $permissionType);
358 if ($formattedField !== NULL) {
359 $fields[$name] = $formattedField;
360 }
361 }
362 $field->free();
363 }
364
365 if (empty($fields) && !$validGroup) {
366 CRM_Core_Error::fatal(ts('The requested Profile (gid=%1) is disabled OR it is not configured to be used for \'Profile\' listings in its Settings OR there is no Profile with that ID OR you do not have permission to access this profile. Please contact the site administrator if you need assistance.',
5d6ac993
TO
367 array(1 => implode(',', $profileIds))
368 ));
6a488035
TO
369 }
370 else {
371 self::reformatProfileFields($fields);
372 }
373
374 return $fields;
375 }
376
377 /**
378 * Format a list of UFFields for use with buildProfile. This is the in-memory analog
379 * of getFields().
380 *
6a0b768e
TO
381 * @param array $groupArr
382 * (mimic CRM_UF_DAO_UFGroup).
383 * @param array $fieldArrs
384 * List of fields (each mimics CRM_UF_DAO_UFField).
385 * @param bool $visibility
386 * Visibility of fields we are interested in.
6a488035 387 * @param bool $searchable
77b97be7
EM
388 * @param bool $showAll
389 * @param null $ctype
390 * @param int $permissionType
391 *
6a488035 392 * @return array
c490a46a 393 * @see self::getFields
6a488035
TO
394 */
395 public static function formatUFFields(
396 $groupArr,
397 $fieldArrs,
398 $visibility = NULL,
399 $searchable = NULL,
400 $showAll = FALSE,
401 $ctype = NULL,
402 $permissionType = CRM_Core_Permission::CREATE
403 ) {
404 // $group = new CRM_Core_DAO_UFGroup();
405 // $group->copyValues($groupArr); // no... converts string('') to string('null')
406 $group = (object) $groupArr;
407
408 // Refactoring note: The $fieldArrs here may be slightly different than the $ufFields
409 // used by calculateGroupType, but I don't think the missing fields matter, and -- if
410 // they did -- the obvious fix would produce mutual recursion.
411 $ufGroupType = self::_calculateGroupType($fieldArrs);
5d6ac993
TO
412 $profileType = CRM_Core_BAO_UFField::calculateProfileType(implode(',', $ufGroupType));
413 $contactActivityProfile = CRM_Core_BAO_UFField::checkContactActivityProfileTypeByGroupType(implode(',', $ufGroupType));
6a488035
TO
414 $importableFields = self::getImportableFields($showAll, $profileType, $contactActivityProfile);
415 list($customFields, $addressCustomFields) = self::getCustomFields($ctype);
416
417 $formattedFields = array();
418 foreach ($fieldArrs as $fieldArr) {
6a488035
TO
419 $field = (object) $fieldArr;
420 if (!self::filterUFField($field, $searchable, $showAll, $visibility)) {
421 continue;
422 }
423
424 list($name, $formattedField) = self::formatUFField($group, $field, $customFields, $addressCustomFields, $importableFields, $permissionType);
425 if ($formattedField !== NULL) {
426 $formattedFields[$name] = $formattedField;
427 }
428 }
429 return $formattedFields;
430 }
431
432 /**
433 * Prepare a field for rendering with CRM_Core_BAO_UFGroup::buildProfile.
434 *
435 * @param CRM_Core_DAO_UFGroup|CRM_Core_DAO $group
436 * @param CRM_Core_DAO_UFField|CRM_Core_DAO $field
e7483cbe 437 * @param array $customFields
6a488035
TO
438 * @param array $addressCustomFields
439 * @param array $importableFields
6a0b768e
TO
440 * @param int $permissionType
441 * Eg CRM_Core_Permission::CREATE.
6a488035
TO
442 * @return array
443 */
444 protected static function formatUFField(
445 $group,
446 $field,
447 $customFields,
448 $addressCustomFields,
449 $importableFields,
450 $permissionType = CRM_Core_Permission::CREATE
451 ) {
452 $name = $field->field_name;
453 $title = $field->label;
454
455 $addressCustom = FALSE;
456 if (in_array($permissionType, array(
5d6ac993
TO
457 CRM_Core_Permission::CREATE,
458 CRM_Core_Permission::EDIT,
459 )) &&
6a488035
TO
460 in_array($field->field_name, array_keys($addressCustomFields))
461 ) {
462 $addressCustom = TRUE;
463 $name = "address_{$name}";
464 }
887e764d
PN
465 if ($field->field_name == 'url') {
466 $name .= "-{$field->website_type_id}";
467 }
468 elseif (!empty($field->location_type_id)) {
6a488035
TO
469 $name .= "-{$field->location_type_id}";
470 }
471 else {
472 $locationFields = self::getLocationFields();
473 if (in_array($field->field_name, $locationFields) || $addressCustom) {
474 $name .= '-Primary';
475 }
476 }
477
478 if (isset($field->phone_type_id)) {
479 $name .= "-{$field->phone_type_id}";
480 }
481
482 // No lie: this is bizarre; why do we need to mix so many UFGroup properties into UFFields?
483 // I guess to make field self sufficient with all the required data and avoid additional calls
484 $formattedField = array(
485 'name' => $name,
486 'groupTitle' => $group->title,
5d6ac993 487 'groupName' => $group->name,
6a488035
TO
488 'groupHelpPre' => empty($group->help_pre) ? '' : $group->help_pre,
489 'groupHelpPost' => empty($group->help_post) ? '' : $group->help_post,
490 'title' => $title,
491 'where' => CRM_Utils_Array::value('where', CRM_Utils_Array::value($field->field_name, $importableFields)),
492 'attributes' => CRM_Core_DAO::makeAttribute(CRM_Utils_Array::value($field->field_name, $importableFields)),
493 'is_required' => $field->is_required,
494 'is_view' => $field->is_view,
495 'help_pre' => $field->help_pre,
496 'help_post' => $field->help_post,
497 'visibility' => $field->visibility,
498 'in_selector' => $field->in_selector,
499 'rule' => CRM_Utils_Array::value('rule', CRM_Utils_Array::value($field->field_name, $importableFields)),
500 'location_type_id' => isset($field->location_type_id) ? $field->location_type_id : NULL,
887e764d 501 'website_type_id' => isset($field->website_type_id) ? $field->website_type_id : NULL,
6a488035
TO
502 'phone_type_id' => isset($field->phone_type_id) ? $field->phone_type_id : NULL,
503 'group_id' => $group->id,
cd0dd278
DG
504 'add_to_group_id' => isset($group->add_to_group_id) ? $group->add_to_group_id : NULL,
505 'add_captcha' => isset($group->add_captcha) ? $group->add_captcha : NULL,
6a488035
TO
506 'field_type' => $field->field_type,
507 'field_id' => $field->id,
0c145cc0
DL
508 'pseudoconstant' => CRM_Utils_Array::value(
509 'pseudoconstant',
510 CRM_Utils_Array::value($field->field_name, $importableFields)
511 ),
512 // obsolete this when we remove the name / dbName discrepancy with gender/suffix/prefix
513 'dbName' => CRM_Utils_Array::value(
514 'dbName',
515 CRM_Utils_Array::value($field->field_name, $importableFields)
516 ),
6a488035
TO
517 'skipDisplay' => 0,
518 );
519
520 //adding custom field property
521 if (substr($field->field_name, 0, 6) == 'custom' ||
522 substr($field->field_name, 0, 14) === 'address_custom'
523 ) {
524 // if field is not present in customFields, that means the user
525 // DOES NOT HAVE permission to access that field
526 if (array_key_exists($field->field_name, $customFields)) {
527 $formattedField['is_search_range'] = $customFields[$field->field_name]['is_search_range'];
528 // fix for CRM-1994
529 $formattedField['options_per_line'] = $customFields[$field->field_name]['options_per_line'];
530 $formattedField['data_type'] = $customFields[$field->field_name]['data_type'];
531 $formattedField['html_type'] = $customFields[$field->field_name]['html_type'];
532
533 if (CRM_Utils_Array::value('html_type', $formattedField) == 'Select Date') {
534 $formattedField['date_format'] = $customFields[$field->field_name]['date_format'];
535 $formattedField['time_format'] = $customFields[$field->field_name]['time_format'];
536 }
537
538 $formattedField['is_multi_summary'] = $field->is_multi_summary;
539 return array($name, $formattedField);
540 }
541 else {
542 $formattedField = NULL;
543 return array($name, $formattedField);
544 }
545 }
546 return array($name, $formattedField);
547 }
548
549 /**
fe482240 550 * Create a query to find all visible UFFields in a UFGroup.
6a488035
TO
551 *
552 * This is the SQL-variant of checkUFFieldDisplayable().
553 *
554 * @param int $groupId
555 * @param bool $searchable
556 * @param bool $showAll
557 * @param int $visibility
6a0b768e
TO
558 * @param string $orderBy
559 * Comma-delimited list of SQL columns.
6a488035
TO
560 * @return string
561 */
562 protected static function createUFFieldQuery($groupId, $searchable, $showAll, $visibility, $orderBy) {
563 $where = " WHERE uf_group_id = {$groupId}";
564
565 if ($searchable) {
566 $where .= " AND is_searchable = 1";
567 }
568
569 if (!$showAll) {
570 $where .= " AND is_active = 1";
571 }
572
573 if ($visibility) {
574 $clause = array();
575 if ($visibility & self::PUBLIC_VISIBILITY) {
576 $clause[] = 'visibility = "Public Pages"';
577 }
578 if ($visibility & self::ADMIN_VISIBILITY) {
579 $clause[] = 'visibility = "User and User Admin Only"';
580 }
581 if ($visibility & self::LISTINGS_VISIBILITY) {
582 $clause[] = 'visibility = "Public Pages and Listings"';
583 }
584 if (!empty($clause)) {
585 $where .= ' AND ( ' . implode(' OR ', $clause) . ' ) ';
586 }
587 }
588
589 $query = "SELECT * FROM civicrm_uf_field $where ORDER BY weight";
590 if ($orderBy) {
591 $query .= ", " . $orderBy;
592 return $query;
593 }
594 return $query;
595 }
596
597 /**
fe482240 598 * Create a query to find all visible UFFields in a UFGroup.
6a488035
TO
599 *
600 * This is the PHP in-memory variant of createUFFieldQuery().
601 *
602 * @param CRM_Core_DAO_UFField|CRM_Core_DAO $field
603 * @param bool $searchable
604 * @param bool $showAll
605 * @param int $visibility
a6c01b45
CW
606 * @return bool
607 * TRUE if field is displayable
6a488035
TO
608 */
609 protected static function filterUFField($field, $searchable, $showAll, $visibility) {
610 if ($searchable && $field->is_searchable != 1) {
611 return FALSE;
612 }
613
614 if (!$showAll && $field->is_active != 1) {
615 return FALSE;
616 }
617
618 if ($visibility) {
619 $allowedVisibilities = array();
620 if ($visibility & self::PUBLIC_VISIBILITY) {
621 $allowedVisibilities[] = 'Public Pages';
622 }
623 if ($visibility & self::ADMIN_VISIBILITY) {
624 $allowedVisibilities[] = 'User and User Admin Only';
625 }
626 if ($visibility & self::LISTINGS_VISIBILITY) {
627 $allowedVisibilities[] = 'Public Pages and Listings';
628 }
629 // !empty($allowedVisibilities) seems silly to me, but it is equivalent to the pre-existing SQL
630 if (!empty($allowedVisibilities) && !in_array($field->visibility, $allowedVisibilities)) {
631 return FALSE;
632 }
633 }
634
635 return TRUE;
636 }
637
b5c2afd0
EM
638 /**
639 * @param $showAll
640 * @param $profileType
641 * @param $contactActivityProfile
642 *
643 * @return array
644 */
6a488035
TO
645 protected static function getImportableFields($showAll, $profileType, $contactActivityProfile) {
646 if (!$showAll) {
647 $importableFields = CRM_Contact_BAO_Contact::importableFields('All', FALSE, FALSE, FALSE, TRUE, TRUE);
648 }
649 else {
650 $importableFields = CRM_Contact_BAO_Contact::importableFields('All', FALSE, TRUE, FALSE, TRUE, TRUE);
651 }
652
653 if ($profileType == 'Activity' || $contactActivityProfile) {
654 $componentFields = CRM_Activity_BAO_Activity::getProfileFields();
655 }
656 else {
657 $componentFields = CRM_Core_Component::getQueryFields();
658 }
659
660 $importableFields = array_merge($importableFields, $componentFields);
661
662 $importableFields['group']['title'] = ts('Group(s)');
663 $importableFields['group']['where'] = NULL;
664 $importableFields['tag']['title'] = ts('Tag(s)');
665 $importableFields['tag']['where'] = NULL;
666 return $importableFields;
667 }
668
bc854509 669 /**
670 * Get the fields relating to locations.
671 *
672 * @return array
673 */
6a488035
TO
674 public static function getLocationFields() {
675 static $locationFields = array(
676 'street_address',
677 'supplemental_address_1',
678 'supplemental_address_2',
679 'city',
680 'postal_code',
681 'postal_code_suffix',
682 'geo_code_1',
683 'geo_code_2',
684 'state_province',
685 'country',
686 'county',
687 'phone',
688 'phone_and_ext',
689 'email',
690 'im',
691 'address_name',
692 'phone_ext',
693 );
694 return $locationFields;
695 }
696
b5c2afd0
EM
697 /**
698 * @param $ctype
699 *
700 * @return mixed
701 */
6a488035
TO
702 protected static function getCustomFields($ctype) {
703 static $customFieldCache = array();
704 if (!isset($customFieldCache[$ctype])) {
705 $customFields = CRM_Core_BAO_CustomField::getFieldsForImport($ctype, FALSE, FALSE, FALSE, TRUE, TRUE);
706
707 // hack to add custom data for components
1cb28d5d 708 $components = array('Contribution', 'Participant', 'Membership', 'Activity', 'Case');
6a488035
TO
709 foreach ($components as $value) {
710 $customFields = array_merge($customFields, CRM_Core_BAO_CustomField::getFieldsForImport($value));
711 }
712 $addressCustomFields = CRM_Core_BAO_CustomField::getFieldsForImport('Address');
713 $customFields = array_merge($customFields, $addressCustomFields);
714 $customFieldCache[$ctype] = array($customFields, $addressCustomFields);
715 }
716 return $customFieldCache[$ctype];
717 }
718
719 /**
fe482240 720 * Check the data validity.
6a488035 721 *
6a0b768e
TO
722 * @param int $userID
723 * The user id that we are actually editing.
b247a732 724 * @param string $name
725 * The machine-name of the group we are interested in.
2a6da8d7 726 * @param bool $register
6a0b768e
TO
727 * @param int $action
728 * The action of the form.
6a488035 729 *
2a6da8d7 730 * @pram boolean $register is this the registrtion form
e7483cbe 731 * @return bool
a6c01b45 732 * true if form is valid
6a488035 733 */
b247a732 734 public static function isValid($userID, $name, $register = FALSE, $action = NULL) {
6a488035
TO
735 if ($register) {
736 $controller = new CRM_Core_Controller_Simple('CRM_Profile_Form_Dynamic',
737 ts('Dynamic Form Creator'),
738 $action
739 );
740 $controller->set('id', $userID);
741 $controller->set('register', 1);
742 $controller->process();
743 return $controller->validate();
744 }
745 else {
746 // make sure we have a valid group
747 $group = new CRM_Core_DAO_UFGroup();
748
b247a732 749 $group->name = $name;
6a488035
TO
750
751 if ($group->find(TRUE) && $userID) {
752 $controller = new CRM_Core_Controller_Simple('CRM_Profile_Form_Dynamic', ts('Dynamic Form Creator'), $action);
753 $controller->set('gid', $group->id);
754 $controller->set('id', $userID);
755 $controller->set('register', 0);
756 $controller->process();
757 return $controller->validate();
758 }
759 return TRUE;
760 }
761 }
762
763 /**
fe482240 764 * Get the html for the form that represents this particular group.
6a488035 765 *
6a0b768e
TO
766 * @param int $userID
767 * The user id that we are actually editing.
768 * @param string $title
769 * The title of the group we are interested in.
770 * @param int $action
771 * The action of the form.
772 * @param bool $register
773 * Is this the registration form.
774 * @param bool $reset
775 * Should we reset the form?.
776 * @param int $profileID
777 * Do we have the profile ID?.
2a6da8d7
EM
778 *
779 * @param bool $doNotProcess
780 * @param null $ctype
6a488035 781 *
a6c01b45
CW
782 * @return string
783 * the html for the form on success, otherwise empty string
6a488035 784 */
e7483cbe 785 public static function getEditHTML(
5d6ac993 786 $userID,
6a488035 787 $title,
5d6ac993
TO
788 $action = NULL,
789 $register = FALSE,
790 $reset = FALSE,
791 $profileID = NULL,
6a488035 792 $doNotProcess = FALSE,
5d6ac993 793 $ctype = NULL
6a488035
TO
794 ) {
795
796 if ($register) {
797 $controller = new CRM_Core_Controller_Simple('CRM_Profile_Form_Dynamic',
798 ts('Dynamic Form Creator'),
799 $action
800 );
801 if ($reset || $doNotProcess) {
802 // hack to make sure we do not process this form
803 $oldQFDefault = CRM_Utils_Array::value('_qf_default',
804 $_POST
805 );
806 unset($_POST['_qf_default']);
807 unset($_REQUEST['_qf_default']);
808 if ($reset) {
809 $controller->reset();
810 }
811 }
812
813 $controller->set('id', $userID);
814 $controller->set('register', 1);
815 $controller->set('skipPermission', 1);
816 $controller->set('ctype', $ctype);
817 $controller->process();
818 if ($doNotProcess || !empty($_POST)) {
819 $controller->validate();
820 }
821 $controller->setEmbedded(TRUE);
822
823 //CRM-5839 - though we want to process form, get the control back.
824 $controller->setSkipRedirection(($doNotProcess) ? FALSE : TRUE);
825
826 $controller->run();
827
828 // we are done processing so restore the POST/REQUEST vars
829 if (($reset || $doNotProcess) && $oldQFDefault) {
830 $_POST['_qf_default'] = $_REQUEST['_qf_default'] = $oldQFDefault;
831 }
832
833 $template = CRM_Core_Smarty::singleton();
834
835 // Hide CRM error messages if they are displayed using drupal form_set_error.
836 if (!empty($_POST)) {
837 $template->assign('suppressForm', TRUE);
838 }
839
840 return trim($template->fetch('CRM/Profile/Form/Dynamic.tpl'));
841 }
842 else {
843 if (!$profileID) {
844 // make sure we have a valid group
845 $group = new CRM_Core_DAO_UFGroup();
846
847 $group->title = $title;
848
849 if ($group->find(TRUE)) {
850 $profileID = $group->id;
851 }
852 }
853
854 if ($profileID) {
855 // make sure profileID and ctype match if ctype exists
856 if ($ctype) {
857 $profileType = CRM_Core_BAO_UFField::getProfileType($profileID);
858 if (CRM_Contact_BAO_ContactType::isaSubType($profileType)) {
859 $profileType = CRM_Contact_BAO_ContactType::getBasicType($profileType);
860 }
861
862 if (($profileType != 'Contact') && ($profileType != $ctype)) {
863 return NULL;
864 }
865 }
866
867 $controller = new CRM_Core_Controller_Simple('CRM_Profile_Form_Dynamic',
868 ts('Dynamic Form Creator'),
869 $action
870 );
871 if ($reset) {
872 $controller->reset();
873 }
874 $controller->set('gid', $profileID);
875 $controller->set('id', $userID);
876 $controller->set('register', 0);
877 $controller->set('skipPermission', 1);
878 if ($ctype) {
879 $controller->set('ctype', $ctype);
880 }
881 $controller->process();
882 $controller->setEmbedded(TRUE);
883
884 //CRM-5846 - give the control back to drupal.
885 $controller->setSkipRedirection(($doNotProcess) ? FALSE : TRUE);
886 $controller->run();
887
888 $template = CRM_Core_Smarty::singleton();
889
890 // Hide CRM error messages if they are displayed using drupal form_set_error.
891 if (!empty($_POST) && CRM_Core_Config::singleton()->userFramework == 'Drupal') {
892 if (arg(0) == 'user' || (arg(0) == 'admin' && arg(1) == 'people')) {
893 $template->assign('suppressForm', TRUE);
894 }
895 }
896
897 $templateFile = "CRM/Profile/Form/{$profileID}/Dynamic.tpl";
898 if (!$template->template_exists($templateFile)) {
899 $templateFile = 'CRM/Profile/Form/Dynamic.tpl';
900 }
901 return trim($template->fetch($templateFile));
902 }
903 else {
904 $userEmail = CRM_Contact_BAO_Contact_Location::getEmailDetails($userID);
905
906 // if post not empty then only proceed
907 if (!empty($_POST)) {
908 // get the new email
909 $config = CRM_Core_Config::singleton();
910 $email = CRM_Utils_Array::value('mail', $_POST);
911
912 if (CRM_Utils_Rule::email($email) && ($email != $userEmail[1])) {
913 CRM_Core_BAO_UFMatch::updateContactEmail($userID, $email);
914 }
915 }
916 }
917 }
918 return '';
919 }
920
921 /**
fe482240 922 * Searches for a contact in the db with similar attributes.
6a488035 923 *
6a0b768e
TO
924 * @param array $params
925 * The list of values to be used in the where clause.
926 * @param int $id
927 * The current contact id (hence excluded from matching).
2a6da8d7
EM
928 * @param string $contactType
929 *
72b3a70c
CW
930 * @return int|null
931 * contact_id if found, null otherwise
6a488035
TO
932 */
933 public static function findContact(&$params, $id = NULL, $contactType = 'Individual') {
934 $dedupeParams = CRM_Dedupe_Finder::formatParams($params, $contactType);
935 $dedupeParams['check_permission'] = CRM_Utils_Array::value('check_permission', $params, TRUE);
936 $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, $contactType, 'Supervised', array($id));
937 if (!empty($ids)) {
938 return implode(',', $ids);
939 }
940 else {
941 return NULL;
942 }
943 }
944
945 /**
f3f00653 946 * Given a contact id and a field set, return the values from the db.
6a488035 947 *
100fef9d 948 * @param int $cid
6a0b768e
TO
949 * @param array $fields
950 * The profile fields of interest.
951 * @param array $values
952 * The values for the above fields.
953 * @param bool $searchable
954 * Searchable or not.
955 * @param array $componentWhere
956 * Component condition.
957 * @param bool $absolute
958 * Return urls in absolute form (useful when sending an email).
2a6da8d7 959 * @param null $additionalWhereClause
f3f00653 960 *
961 * @return null|array
6a488035 962 */
5d6ac993
TO
963 public static function getValues(
964 $cid, &$fields, &$values,
6a488035
TO
965 $searchable = TRUE, $componentWhere = NULL,
966 $absolute = FALSE, $additionalWhereClause = NULL
967 ) {
968 if (empty($cid) && empty($componentWhere)) {
969 return NULL;
970 }
971
972 // get the contact details (hier)
973 $returnProperties = CRM_Contact_BAO_Contact::makeHierReturnProperties($fields);
974 $params = $cid ? array(array('contact_id', '=', $cid, 0, 0)) : array();
975
976 // add conditions specified by components. eg partcipant_id etc
977 if (!empty($componentWhere)) {
978 $params = array_merge($params, $componentWhere);
979 }
980
981 $query = new CRM_Contact_BAO_Query($params, $returnProperties, $fields);
6a488035 982
5d6ac993 983 $details = $query->searchQuery(0, 0, NULL, FALSE, FALSE,
6a488035
TO
984 FALSE, FALSE, FALSE, $additionalWhereClause);
985 if (!$details->fetch()) {
986 return;
987 }
d9ab802d 988 $query->convertToPseudoNames($details);
6a488035
TO
989 $config = CRM_Core_Config::singleton();
990
b2b0530a 991 $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
5d6ac993
TO
992 $imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id');
993 $websiteTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Website', 'website_type_id');
6a488035
TO
994
995 $multipleFields = array('url');
6a488035
TO
996
997 //start of code to set the default values
998 foreach ($fields as $name => $field) {
999 // fix for CRM-3962
1000 if ($name == 'id') {
1001 $name = 'contact_id';
1002 }
1003
1004 // skip fields that should not be displayed separately
a7488080 1005 if (!empty($field['skipDisplay'])) {
6a488035
TO
1006 continue;
1007 }
1008
50bf705c 1009 // Create a unique, non-empty index for each field.
6a488035 1010 $index = $field['title'];
f9f40af3
TO
1011 if ($index === '') {
1012 $index = ' ';
2aa397bc 1013 }
5d6ac993 1014 while (array_key_exists($index, $values)) {
50bf705c 1015 $index .= ' ';
5d6ac993 1016 }
6a488035 1017
6a488035
TO
1018 $params[$index] = $values[$index] = '';
1019 $customFieldName = NULL;
1020 // hack for CRM-665
1021 if (isset($details->$name) || $name == 'group' || $name == 'tag') {
1022 // to handle gender / suffix / prefix
04ffef8d 1023 if (in_array(substr($name, 0, -3), array('gender', 'prefix', 'suffix'))) {
04ffef8d 1024 $params[$index] = $details->$name;
46796b19 1025 $values[$index] = $details->$name;
6a488035
TO
1026 }
1027 elseif (in_array($name, CRM_Contact_BAO_Contact::$_greetingTypes)) {
5d6ac993 1028 $dname = $name . '_display';
6a488035 1029 $values[$index] = $details->$dname;
5d6ac993 1030 $name = $name . '_id';
6a488035
TO
1031 $params[$index] = $details->$name;
1032 }
1033 elseif (in_array($name, array(
5d6ac993
TO
1034 'state_province',
1035 'country',
21dfd5f5 1036 'county',
5d6ac993 1037 ))) {
6a488035 1038 $values[$index] = $details->$name;
5d6ac993 1039 $idx = $name . '_id';
6a488035
TO
1040 $params[$index] = $details->$idx;
1041 }
1042 elseif ($name === 'preferred_communication_method') {
e7e657f0 1043 $communicationFields = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method');
6a488035 1044 $compref = array();
5d6ac993 1045 $pref = explode(CRM_Core_DAO::VALUE_SEPARATOR, $details->$name);
6a488035
TO
1046
1047 foreach ($pref as $k) {
1048 if ($k) {
1049 $compref[] = $communicationFields[$k];
1050 }
1051 }
1052 $params[$index] = $details->$name;
1053 $values[$index] = implode(',', $compref);
1054 }
1055 elseif ($name === 'preferred_language') {
6a488035 1056 $params[$index] = $details->$name;
a8c23526 1057 $values[$index] = CRM_Core_PseudoConstant::getLabel('CRM_Contact_DAO_Contact', 'preferred_language', $details->$name);
6a488035
TO
1058 }
1059 elseif ($name == 'group') {
1060 $groups = CRM_Contact_BAO_GroupContact::getContactGroup($cid, 'Added', NULL, FALSE, TRUE);
1061 $title = $ids = array();
1062
1063 foreach ($groups as $g) {
1064 // CRM-8362: User and User Admin visibility groups should be included in display if user has
1065 // VIEW permission on that group
69953588 1066 $groupPerm = CRM_Contact_BAO_Group::checkPermission($g['group_id'], TRUE);
6a488035
TO
1067
1068 if ($g['visibility'] != 'User and User Admin Only' ||
1069 CRM_Utils_Array::key(CRM_Core_Permission::VIEW, $groupPerm)
1070 ) {
1071 $title[] = $g['title'];
1072 if ($g['visibility'] == 'Public Pages') {
1073 $ids[] = $g['group_id'];
1074 }
1075 }
1076 }
1077 $values[$index] = implode(', ', $title);
1078 $params[$index] = implode(',', $ids);
1079 }
1080 elseif ($name == 'tag') {
1081 $entityTags = CRM_Core_BAO_EntityTag::getTag($cid);
5d6ac993
TO
1082 $allTags = CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
1083 $title = array();
6a488035
TO
1084 foreach ($entityTags as $tagId) {
1085 $title[] = $allTags[$tagId];
1086 }
1087 $values[$index] = implode(', ', $title);
1088 $params[$index] = implode(',', $entityTags);
1089 }
1090 elseif ($name == 'activity_status_id') {
1091 $activityStatus = CRM_Core_PseudoConstant::activityStatus();
1092 $values[$index] = $activityStatus[$details->$name];
1093 $params[$index] = $details->$name;
1094 }
1095 elseif ($name == 'activity_date_time') {
1096 $values[$index] = CRM_Utils_Date::customFormat($details->$name);
1097 $params[$index] = $details->$name;
1098 }
1099 elseif ($name == 'contact_sub_type') {
1100 $contactSubTypeNames = explode(CRM_Core_DAO::VALUE_SEPARATOR, $details->$name);
1101 if (!empty($contactSubTypeNames)) {
1102 $contactSubTypeLabels = array();
1103 // get all contact subtypes
1104 $allContactSubTypes = CRM_Contact_BAO_ContactType::subTypeInfo();
1105 // build contact subtype labels array
5d6ac993 1106 foreach ($contactSubTypeNames as $cstName) {
6a488035
TO
1107 if ($cstName) {
1108 $contactSubTypeLabels[] = $allContactSubTypes[$cstName]['label'];
1109 }
1110 }
1111 $values[$index] = implode(',', $contactSubTypeLabels);
1112 }
1113
1114 $params[$index] = $details->$name;
1115 }
1116 else {
1117 if (substr($name, 0, 7) === 'do_not_' || substr($name, 0, 3) === 'is_') {
1118 if ($details->$name) {
1119 $values[$index] = '[ x ]';
1120 }
1121 }
1122 else {
1123 if ($cfID = CRM_Core_BAO_CustomField::getKeyID($name)) {
1124 $htmlType = $field['html_type'];
1125
1126 // field_type is only set when we are retrieving profile values
1127 // when sending email, we call the same function to get custom field
1128 // values etc, i.e. emulating a profile
1129 $fieldType = CRM_Utils_Array::value('field_type', $field);
1130
1131 if ($htmlType == 'File') {
1132 $entityId = $cid;
1133 if (!$cid &&
5d6ac993
TO
1134 $fieldType == 'Activity' && !empty($componentWhere[0][2])
1135 ) {
6a488035
TO
1136 $entityId = $componentWhere[0][2];
1137 }
1138
1139 $fileURL = CRM_Core_BAO_CustomField::getFileURL($entityId,
1140 $cfID,
1141 NULL,
b42d84aa 1142 $absolute,
1143 $additionalWhereClause
6a488035
TO
1144 );
1145 $params[$index] = $values[$index] = $fileURL['file_url'];
1146 }
1147 else {
1148 $customVal = NULL;
1149 if (isset($dao) && property_exists($dao, 'data_type') &&
1150 ($dao->data_type == 'Int' ||
1151 $dao->data_type == 'Boolean'
1152 )
1153 ) {
5d6ac993 1154 $customVal = (int ) ($details->{$name});
6a488035
TO
1155 }
1156 elseif (isset($dao) && property_exists($dao, 'data_type')
1157 && $dao->data_type == 'Float'
1158 ) {
5d6ac993 1159 $customVal = (float ) ($details->{$name});
6a488035
TO
1160 }
1161 elseif (!CRM_Utils_System::isNull(explode(CRM_Core_DAO::VALUE_SEPARATOR,
5d6ac993
TO
1162 $details->{$name}
1163 ))
1164 ) {
6a488035
TO
1165 $customVal = $details->{$name};
1166 }
1167
1168 //CRM-4582
1169 if (CRM_Utils_System::isNull($customVal)) {
1170 continue;
1171 }
1172
1173 $params[$index] = $customVal;
8cee0c70 1174 $values[$index] = CRM_Core_BAO_CustomField::displayValue($customVal, $cfID);
1b4d9e39 1175 if ($field['data_type'] == 'ContactReference') {
6a488035
TO
1176 $params[$index] = $values[$index];
1177 }
1178 if (CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField',
5d6ac993
TO
1179 $cfID, 'is_search_range'
1180 )
1181 ) {
6a488035
TO
1182 $customFieldName = "{$name}_from";
1183 }
1184 }
1185 }
1186 elseif ($name == 'image_URL') {
77d45291 1187 list($width, $height) = getimagesize(CRM_Utils_String::unstupifyUrl($details->$name));
6a488035
TO
1188 list($thumbWidth, $thumbHeight) = CRM_Contact_BAO_Contact::getThumbSize($width, $height);
1189
1190 $image_URL = '<img src="' . $details->$name . '" height= ' . $thumbHeight . ' width= ' . $thumbWidth . ' />';
1191 $values[$index] = "<a href='#' onclick='contactImagePopUp(\"{$details->$name}\", {$width}, {$height});'>{$image_URL}</a>";
1192 }
1193 elseif (in_array($name, array(
5d6ac993
TO
1194 'birth_date',
1195 'deceased_date',
1196 'membership_start_date',
1197 'membership_end_date',
21dfd5f5 1198 'join_date',
5d6ac993 1199 ))) {
6a488035
TO
1200 $values[$index] = CRM_Utils_Date::customFormat($details->$name);
1201 $params[$index] = CRM_Utils_Date::isoToMysql($details->$name);
1202 }
1203 else {
1204 $dao = '';
1205 if ($index == 'Campaign') {
1206 $dao = 'CRM_Campaign_DAO_Campaign';
1207 }
1208 elseif ($index == 'Contribution Page') {
1209 $dao = 'CRM_Contribute_DAO_ContributionPage';
1210 }
1211 if ($dao) {
1212 $value = CRM_Core_DAO::getFieldValue($dao, $details->$name, 'title');
1213 }
1214 else {
1215 $value = $details->$name;
1216 }
1217 $values[$index] = $value;
1218 }
1219 }
1220 }
1221 }
1222 elseif (strpos($name, '-') !== FALSE) {
1223 list($fieldName, $id, $type) = CRM_Utils_System::explode('-', $name, 3);
1224
1225 if (!in_array($fieldName, $multipleFields)) {
1226 if ($id == 'Primary') {
1227 // fix for CRM-1543
1228 // not sure why we'd every use Primary location type id
1229 // we need to fix the source if we are using it
1230 // $locationTypeName = CRM_Contact_BAO_Contact::getPrimaryLocationType( $cid );
1231 $locationTypeName = 1;
1232 }
1233 else {
1234 $locationTypeName = CRM_Utils_Array::value($id, $locationTypes);
1235 }
1236
1237 if (!$locationTypeName) {
1238 continue;
1239 }
1240
1241 $detailName = "{$locationTypeName}-{$fieldName}";
1242 $detailName = str_replace(' ', '_', $detailName);
1243
1244 if (in_array($fieldName, array(
5d6ac993
TO
1245 'phone',
1246 'im',
1247 'email',
21dfd5f5 1248 'openid',
5d6ac993 1249 ))) {
6a488035
TO
1250 if ($type) {
1251 $detailName .= "-{$type}";
1252 }
1253 }
1254
1255 if (in_array($fieldName, array(
5d6ac993
TO
1256 'state_province',
1257 'country',
21dfd5f5 1258 'county',
5d6ac993 1259 ))) {
6a488035 1260 $values[$index] = $details->$detailName;
5d6ac993 1261 $idx = $detailName . '_id';
6a488035
TO
1262 $params[$index] = $details->$idx;
1263 }
1264 elseif ($fieldName == 'im') {
1265 $providerId = $detailName . '-provider_id';
38913d36
TO
1266 if (isset($imProviders[$details->$providerId])) {
1267 $values[$index] = $details->$detailName . " (" . $imProviders[$details->$providerId] . ")";
6a488035
TO
1268 }
1269 else {
1270 $values[$index] = $details->$detailName;
1271 }
1272 $params[$index] = $details->$detailName;
1273 }
1274 elseif ($fieldName == 'phone') {
1275 $phoneExtField = str_replace('phone', 'phone_ext', $detailName);
1276 if (isset($details->$phoneExtField)) {
1277 $values[$index] = $details->$detailName . " (" . $details->$phoneExtField . ")";
1278 }
1279 else {
1280 $values[$index] = $details->$detailName;
1281 }
1282 $params[$index] = $details->$detailName;
1283 }
1284 else {
1285 $values[$index] = $params[$index] = $details->$detailName;
1286 }
1287 }
1288 else {
1289 $detailName = "website-{$id}-{$fieldName}";
1290 $url = CRM_Utils_System::fixURL($details->$detailName);
1291 if ($details->$detailName) {
5d6ac993
TO
1292 $websiteTypeId = "website-{$id}-website_type_id";
1293 $websiteType = $websiteTypes[$details->$websiteTypeId];
6a488035
TO
1294 $values[$index] = "<a href=\"$url\">{$details->$detailName} ( {$websiteType} )</a>";
1295 }
1296 else {
1297 $values[$index] = '';
1298 }
1299 }
1300 }
1301
1302 if ((CRM_Utils_Array::value('visibility', $field) == 'Public Pages and Listings') &&
1303 CRM_Core_Permission::check('profile listings and forms')
1304 ) {
1305
1306 if (CRM_Utils_System::isNull($params[$index])) {
1307 $params[$index] = $values[$index];
1308 }
1309 if (!isset($params[$index])) {
1310 continue;
1311 }
1312 if (!$customFieldName) {
1313 $fieldName = $field['name'];
1314 }
1315 else {
1316 $fieldName = $customFieldName;
1317 }
1318
1319 $url = NULL;
1320 if (CRM_Core_BAO_CustomField::getKeyID($field['name'])) {
1321 $htmlType = $field['html_type'];
1322 if ($htmlType == 'Link') {
1323 $url = $params[$index];
1324 }
1325 elseif (in_array($htmlType, array(
5d6ac993
TO
1326 'CheckBox',
1327 'Multi-Select',
1328 'AdvMulti-Select',
1329 'Multi-Select State/Province',
1330 'Multi-Select Country',
1331 ))) {
6a488035
TO
1332 $valSeperator = CRM_Core_DAO::VALUE_SEPARATOR;
1333 $selectedOptions = explode($valSeperator, $params[$index]);
1334
1335 foreach ($selectedOptions as $key => $multiOption) {
1336 if ($multiOption) {
1337 $url[] = CRM_Utils_System::url('civicrm/profile',
1338 'reset=1&force=1&gid=' . $field['group_id'] . '&' .
1339 urlencode($fieldName) .
1340 '=' .
1341 urlencode($multiOption)
1342 );
1343 }
1344 }
1345 }
1346 else {
1347 $url = CRM_Utils_System::url('civicrm/profile',
1348 'reset=1&force=1&gid=' . $field['group_id'] . '&' .
1349 urlencode($fieldName) .
1350 '=' .
1351 urlencode($params[$index])
1352 );
1353 }
1354 }
1355 else {
1356 $url = CRM_Utils_System::url('civicrm/profile',
1357 'reset=1&force=1&gid=' . $field['group_id'] . '&' .
1358 urlencode($fieldName) .
1359 '=' .
1360 urlencode($params[$index])
1361 );
1362 }
1363
1364 if ($url &&
1365 !empty($values[$index]) &&
1366 $searchable
1367 ) {
1368
1369 if (is_array($url) && !empty($url)) {
1370 $links = array();
1371 $eachMultiValue = explode(', ', $values[$index]);
1372 foreach ($eachMultiValue as $key => $valueLabel) {
1373 $links[] = '<a href="' . $url[$key] . '">' . $valueLabel . '</a>';
1374 }
1375 $values[$index] = implode(', ', $links);
1376 }
1377 else {
1378 $values[$index] = '<a href="' . $url . '">' . $values[$index] . '</a>';
1379 }
1380 }
1381 }
1382 }
1383 }
1384
1385 /**
1386 * Check if profile Group used by any module.
1387 *
6a0b768e
TO
1388 * @param int $id
1389 * Profile Id.
6a488035 1390 *
e7483cbe 1391 * @return bool
6a488035 1392 *
6a488035
TO
1393 */
1394 public static function usedByModule($id) {
1395 //check whether this group is used by any module(check uf join records)
1396 $sql = "SELECT id
1397 FROM civicrm_uf_join
1398 WHERE civicrm_uf_join.uf_group_id=$id";
1399
1400 $dao = new CRM_Core_DAO();
1401 $dao->query($sql);
1402 if ($dao->fetch()) {
1403 return TRUE;
1404 }
1405 else {
1406 return FALSE;
1407 }
1408 }
1409
1410 /**
1411 * Delete the profile Group.
1412 *
6a0b768e
TO
1413 * @param int $id
1414 * Profile Id.
6a488035 1415 *
e7483cbe 1416 * @return bool
6a488035 1417 *
6a488035
TO
1418 */
1419 public static function del($id) {
1420 //check whether this group contains any profile fields
1421 $profileField = new CRM_Core_DAO_UFField();
1422 $profileField->uf_group_id = $id;
1423 $profileField->find();
1424 while ($profileField->fetch()) {
1425 CRM_Core_BAO_UFField::del($profileField->id);
1426 }
1427
1428 //delete records from uf join table
1429 $ufJoin = new CRM_Core_DAO_UFJoin();
1430 $ufJoin->uf_group_id = $id;
1431 $ufJoin->delete();
1432
1433 //delete profile group
1434 $group = new CRM_Core_DAO_UFGroup();
1435 $group->id = $id;
1436 $group->delete();
1437 return 1;
1438 }
1439
1440 /**
fe482240 1441 * Add the UF Group.
6a488035 1442 *
6a0b768e
TO
1443 * @param array $params
1444 * Reference array contains the values submitted by the form.
1445 * @param array $ids
1446 * Reference array contains the id.
6a488035 1447 *
6a488035
TO
1448 *
1449 * @return object
1450 */
00be9182 1451 public static function add(&$params, $ids = array()) {
5d6ac993
TO
1452 $fields = array(
1453 'is_active',
1454 'add_captcha',
1455 'is_map',
1456 'is_update_dupe',
1457 'is_edit_link',
1458 'is_uf_link',
21dfd5f5 1459 'is_cms_user',
5d6ac993 1460 );
6a488035
TO
1461 foreach ($fields as $field) {
1462 $params[$field] = CRM_Utils_Array::value($field, $params, FALSE);
1463 }
1464
1465 $params['limit_listings_group_id'] = CRM_Utils_Array::value('group', $params);
1466 $params['add_to_group_id'] = CRM_Utils_Array::value('add_contact_to_group', $params);
1467
f80ef0e2 1468 //CRM-15427
1469 if (!empty($params['group_type']) && is_array($params['group_type'])) {
1470 $params['group_type'] = implode(',', $params['group_type']);
1471 }
6a488035
TO
1472 $ufGroup = new CRM_Core_DAO_UFGroup();
1473 $ufGroup->copyValues($params);
1474
6a73ef3f 1475 $ufGroupID = CRM_Utils_Array::value('ufgroup', $ids, CRM_Utils_Array::value('id', $params));
25973039 1476 if (!$ufGroupID && empty($params['name'])) {
6a488035
TO
1477 $ufGroup->name = CRM_Utils_String::munge($ufGroup->title, '_', 56);
1478 }
1479 $ufGroup->id = $ufGroupID;
1480
1481 $ufGroup->save();
1482
25973039 1483 if (!$ufGroupID && empty($params['name'])) {
6a488035
TO
1484 $ufGroup->name = $ufGroup->name . "_{$ufGroup->id}";
1485 $ufGroup->save();
1486 }
1487
1488 return $ufGroup;
1489 }
1490
1491 /**
fe482240 1492 * Make uf join entries for an uf group.
6a488035 1493 *
6a0b768e
TO
1494 * @param array $params
1495 * (reference) an assoc array of name/value pairs.
1496 * @param int $ufGroupId
1497 * Ufgroup id.
6a488035 1498 */
00be9182 1499 public static function createUFJoin(&$params, $ufGroupId) {
6a488035
TO
1500 $groupTypes = CRM_Utils_Array::value('uf_group_type', $params);
1501
1502 // get ufjoin records for uf group
1503 $ufGroupRecord = CRM_Core_BAO_UFGroup::getUFJoinRecord($ufGroupId);
1504
1505 // get the list of all ufgroup types
1506 $allUFGroupType = CRM_Core_SelectValues::ufGroupTypes();
1507
1508 // this fix is done to prevent warning generated by array_key_exits incase of empty array is given as input
1509 if (!is_array($groupTypes)) {
1510 $groupTypes = array();
1511 }
1512
1513 // this fix is done to prevent warning generated by array_key_exits incase of empty array is given as input
1514 if (!is_array($ufGroupRecord)) {
1515 $ufGroupRecord = array();
1516 }
1517
1518 // check which values has to be inserted/deleted for contact
1519 $menuRebuild = FALSE;
1520 foreach ($allUFGroupType as $key => $value) {
1521 $joinParams = array();
1522 $joinParams['uf_group_id'] = $ufGroupId;
1523 $joinParams['module'] = $key;
1524 if ($key == 'User Account') {
1525 $menuRebuild = TRUE;
1526 }
1527 if (array_key_exists($key, $groupTypes) && !in_array($key, $ufGroupRecord)) {
1528 // insert a new record
1529 CRM_Core_BAO_UFGroup::addUFJoin($joinParams);
1530 }
1531 elseif (!array_key_exists($key, $groupTypes) && in_array($key, $ufGroupRecord)) {
1532 // delete a record for existing ufgroup
1533 CRM_Core_BAO_UFGroup::delUFJoin($joinParams);
1534 }
1535 }
1536
1537 //update the weight
1538 $query = "
1539UPDATE civicrm_uf_join
1540SET weight = %1
1541WHERE uf_group_id = %2
1542AND ( entity_id IS NULL OR entity_id <= 0 )
1543";
5d6ac993
TO
1544 $p = array(
1545 1 => array($params['weight'], 'Integer'),
6a488035
TO
1546 2 => array($ufGroupId, 'Integer'),
1547 );
1548 CRM_Core_DAO::executeQuery($query, $p);
1549
1550 // do a menu rebuild if we are on drupal, so it gets all the new menu entries
1551 // for user account
1552 $config = CRM_Core_Config::singleton();
1553 if ($menuRebuild &&
1554 $config->userSystem->is_drupal
1555 ) {
1556 menu_rebuild();
1557 }
1558 }
1559
1560 /**
fe482240 1561 * Get the UF Join records for an ufgroup id.
6a488035 1562 *
6a0b768e
TO
1563 * @param int $ufGroupId
1564 * Uf group id.
1565 * @param int $displayName
1566 * If set return display name in array.
1567 * @param int $status
1568 * If set return module other than default modules (User Account/User registration/Profile).
77b97be7 1569 *
a6c01b45 1570 * @return array
6a488035 1571 *
6a488035
TO
1572 */
1573 public static function getUFJoinRecord($ufGroupId = NULL, $displayName = NULL, $status = NULL) {
1574 if ($displayName) {
1575 $UFGroupType = array();
1576 $UFGroupType = CRM_Core_SelectValues::ufGroupTypes();
1577 }
1578
1579 $ufJoin = array();
1580 $dao = new CRM_Core_DAO_UFJoin();
1581
1582 if ($ufGroupId) {
1583 $dao->uf_group_id = $ufGroupId;
1584 }
1585
1586 $dao->find();
1587 $ufJoin = array();
1588
1589 while ($dao->fetch()) {
1590 if (!$displayName) {
1591 $ufJoin[$dao->id] = $dao->module;
1592 }
1593 else {
1594 if (isset($UFGroupType[$dao->module])) {
1595 // skip the default modules
1596 if (!$status) {
1597 $ufJoin[$dao->id] = $UFGroupType[$dao->module];
1598 }
1599 // added for CRM-1475
1600 }
1601 elseif (!CRM_Utils_Array::key($dao->module, $ufJoin)) {
1602 $ufJoin[$dao->id] = $dao->module;
1603 }
1604 }
1605 }
1606 return $ufJoin;
1607 }
1608
1609 /**
fe482240 1610 * Function takes an associative array and creates a ufjoin record for ufgroup.
6a488035 1611 *
6a0b768e
TO
1612 * @param array $params
1613 * (reference) an assoc array of name/value pairs.
6a488035 1614 *
16b10e64 1615 * @return CRM_Core_BAO_UFJoin
6a488035 1616 */
00be9182 1617 public static function addUFJoin(&$params) {
6a488035
TO
1618 $ufJoin = new CRM_Core_DAO_UFJoin();
1619 $ufJoin->copyValues($params);
1620 $ufJoin->save();
1621 return $ufJoin;
1622 }
1623
1624 /**
fe482240 1625 * Delete the uf join record for an uf group.
6a488035 1626 *
6a0b768e
TO
1627 * @param array $params
1628 * (reference) an assoc array of name/value pairs.
6a488035 1629 */
00be9182 1630 public static function delUFJoin(&$params) {
6a488035
TO
1631 $ufJoin = new CRM_Core_DAO_UFJoin();
1632 $ufJoin->copyValues($params);
1633 $ufJoin->delete();
1634 }
1635
1636 /**
fe482240 1637 * Get the weight for ufjoin record.
6a488035 1638 *
6a0b768e
TO
1639 * @param int $ufGroupId
1640 * If $ufGroupId get update weight or add weight.
6a488035 1641 *
a6c01b45
CW
1642 * @return int
1643 * weight of the UFGroup
6a488035 1644 */
00be9182 1645 public static function getWeight($ufGroupId = NULL) {
6a488035
TO
1646 //calculate the weight
1647 $p = array();
1648 if (!$ufGroupId) {
1649 $queryString = "SELECT ( MAX(civicrm_uf_join.weight)+1) as new_weight
1650 FROM civicrm_uf_join
1651 WHERE module = 'User Registration' OR module = 'User Account' OR module = 'Profile'";
1652 }
1653 else {
1654 $queryString = "SELECT MAX(civicrm_uf_join.weight) as new_weight
1655 FROM civicrm_uf_join
1656 WHERE civicrm_uf_join.uf_group_id = %1
1657 AND ( entity_id IS NULL OR entity_id <= 0 )";
1658 $p[1] = array($ufGroupId, 'Integer');
1659 }
1660
1661 $dao = CRM_Core_DAO::executeQuery($queryString, $p);
1662 $dao->fetch();
1663 return ($dao->new_weight) ? $dao->new_weight : 1;
1664 }
1665
1666 /**
fe482240 1667 * Get the uf group for a module.
6a488035 1668 *
6a0b768e
TO
1669 * @param string $moduleName
1670 * Module name.
1671 * @param int $count
1672 * No to increment the weight.
77b97be7 1673 * @param bool $skipPermission
6a0b768e
TO
1674 * @param int $op
1675 * Which operation (view, edit, create, etc) to check permission for.
2141efbf 1676 * @param array|NULL $returnFields list of UFGroup fields to return; NULL for default
6a488035 1677 *
a6c01b45
CW
1678 * @return array
1679 * array of ufgroups for a module
6a488035 1680 */
2141efbf 1681 public static function getModuleUFGroup($moduleName = NULL, $count = 0, $skipPermission = TRUE, $op = CRM_Core_Permission::VIEW, $returnFields = NULL) {
290d4ccc
DS
1682 $selectFields = array('id', 'title', 'created_id', 'is_active', 'is_reserved', 'group_type');
1683
1684 if (!CRM_Core_Config::isUpgradeMode()) {
1685 // CRM-13555, since description field was added later (4.4), and to avoid any problems with upgrade
1686 $selectFields[] = 'description';
1687 }
7ba38389 1688
1689 if (!empty($returnFields)) {
1690 $selectFields = array_merge($returnFields, array_diff($selectFields, $returnFields));
2141efbf 1691 }
7ba38389 1692
1693 $queryString = 'SELECT civicrm_uf_group.' . implode(', civicrm_uf_group.', $selectFields) . '
6a488035
TO
1694 FROM civicrm_uf_group
1695 LEFT JOIN civicrm_uf_join ON (civicrm_uf_group.id = uf_group_id)';
1696 $p = array();
1697 if ($moduleName) {
1698 $queryString .= ' AND civicrm_uf_group.is_active = 1
1699 WHERE civicrm_uf_join.module = %2';
1700 $p[2] = array($moduleName, 'String');
1701 }
1702
6a488035
TO
1703 // add permissioning for profiles only if not registration
1704 if (!$skipPermission) {
1705 $permissionClause = CRM_Core_Permission::ufGroupClause($op, 'civicrm_uf_group.');
1706 if (strpos($queryString, 'WHERE') !== FALSE) {
1707 $queryString .= " AND $permissionClause ";
1708 }
1709 else {
1710 $queryString .= " $permissionClause ";
1711 }
1712 }
1713
1714 $queryString .= ' ORDER BY civicrm_uf_join.weight, civicrm_uf_group.title';
1715 $dao = CRM_Core_DAO::executeQuery($queryString, $p);
1716
1717 $ufGroups = array();
1718 while ($dao->fetch()) {
1719 //skip mix profiles in user Registration / User Account
1720 if (($moduleName == 'User Registration' || $moduleName == 'User Account') &&
1721 CRM_Core_BAO_UFField::checkProfileType($dao->id)
1722 ) {
1723 continue;
1724 }
7ba38389 1725 foreach ($selectFields as $key => $field) {
5d6ac993 1726 if ($field == 'id') {
7ba38389 1727 continue;
1728 }
7ba38389 1729 $ufGroups[$dao->id][$field] = $dao->$field;
1730 }
6a488035
TO
1731 }
1732
1733 // Allow other modules to alter/override the UFGroups.
1734 CRM_Utils_Hook::buildUFGroupsForModule($moduleName, $ufGroups);
1735
1736 return $ufGroups;
1737 }
1738
1739 /**
fe482240 1740 * Filter ufgroups based on logged in user contact type.
6a488035 1741 *
6a0b768e
TO
1742 * @param int $ufGroupId
1743 * Uf group id (profile id).
c490a46a 1744 * @param int $contactID
77b97be7 1745 *
e7483cbe 1746 * @return bool
a6c01b45 1747 * true or false
6a488035 1748 */
00be9182 1749 public static function filterUFGroups($ufGroupId, $contactID = NULL) {
6a488035
TO
1750 if (!$contactID) {
1751 $session = CRM_Core_Session::singleton();
1752 $contactID = $session->get('userID');
1753 }
1754
1755 if ($contactID) {
1756 //get the contact type
1757 $contactType = CRM_Contact_BAO_Contact::getContactType($contactID);
1758
1759 //match if exixting contact type is same as profile contact type
1760 $profileType = CRM_Core_BAO_UFField::getProfileType($ufGroupId);
1761
1762 if (CRM_Contact_BAO_ContactType::isaSubType($profileType)) {
1763 $profileType = CRM_Contact_BAO_ContactType::getBasicType($profileType);
1764 }
1765
1766 //allow special mix profiles for Contribution and Participant
1767 $specialProfiles = array('Contribution', 'Participant', 'Membership');
1768
1769 if (in_array($profileType, $specialProfiles)) {
1770 return TRUE;
1771 }
1772
1773 if (($contactType == $profileType) || $profileType == 'Contact') {
1774 return TRUE;
1775 }
1776 }
1777
1778 return FALSE;
1779 }
1780
1781 /**
fe482240 1782 * Add profile field to a form.
6a488035 1783 *
c927c151 1784 * @param CRM_Core_Form $form
6a0b768e
TO
1785 * @param array $field
1786 * Properties.
1787 * @param int $mode
1788 * Profile mode.
1f177c6b 1789 * @param int $contactId
77b97be7 1790 * @param bool $online
6a0b768e
TO
1791 * @param string $usedFor
1792 * For building up prefixed fieldname for special cases (e.g. onBehalf, Honor).
1f177c6b 1793 * @param int $rowNumber
77b97be7
EM
1794 * @param string $prefix
1795 *
6a488035 1796 * @return null
6a488035 1797 */
e7483cbe 1798 public static function buildProfile(
6a488035
TO
1799 &$form,
1800 &$field,
1801 $mode,
1802 $contactId = NULL,
1803 $online = FALSE,
133e2c99 1804 $usedFor = NULL,
5d6ac993 1805 $rowNumber = NULL,
6a488035
TO
1806 $prefix = ''
1807 ) {
1808 $defaultValues = array();
1f177c6b
CW
1809 $fieldName = $field['name'];
1810 $title = $field['title'];
1811 $attributes = $field['attributes'];
1812 $rule = $field['rule'];
1813 $view = $field['is_view'];
1814 $required = ($mode == CRM_Profile_Form::MODE_SEARCH) ? FALSE : $field['is_required'];
1815 $search = ($mode == CRM_Profile_Form::MODE_SEARCH) ? TRUE : FALSE;
1816 $isShared = CRM_Utils_Array::value('is_shared', $field, 0);
6a488035
TO
1817
1818 // do not display view fields in drupal registration form
1819 // CRM-4632
1820 if ($view && $mode == CRM_Profile_Form::MODE_REGISTER) {
e7483cbe 1821 return NULL;
6a488035
TO
1822 }
1823
133e2c99 1824 if ($usedFor == 'onbehalf') {
6a488035
TO
1825 $name = "onbehalf[$fieldName]";
1826 }
133e2c99 1827 elseif ($usedFor == 'honor') {
1828 $name = "honor[$fieldName]";
1829 }
6a488035
TO
1830 elseif ($contactId && !$online) {
1831 $name = "field[$contactId][$fieldName]";
1832 }
1833 elseif ($rowNumber) {
1834 $name = "field[$rowNumber][$fieldName]";
1835 }
1836 elseif (!empty($prefix)) {
5d6ac993 1837 $name = $prefix . "[$fieldName]";
6a488035
TO
1838 }
1839 else {
1840 $name = $fieldName;
1841 }
9e1854a1 1842
1f177c6b 1843 $selectAttributes = array('class' => 'crm-select2', 'placeholder' => TRUE);
6a488035
TO
1844
1845 if ($fieldName == 'image_URL' && $mode == CRM_Profile_Form::MODE_EDIT) {
e8fb9449 1846 $deleteExtra = json_encode(ts('Are you sure you want to delete contact image.'));
6a488035 1847 $deleteURL = array(
e7483cbe
J
1848 CRM_Core_Action::DELETE => array(
1849 'name' => ts('Delete Contact Image'),
1850 'url' => 'civicrm/contact/image',
1851 'qs' => 'reset=1&id=%%id%%&gid=%%gid%%&action=delete',
e8fb9449 1852 'extra' => 'onclick = "' . htmlspecialchars("if (confirm($deleteExtra)) this.href+='&confirmed=1'; else return false;") . '"',
e7483cbe 1853 ),
6a488035
TO
1854 );
1855 $deleteURL = CRM_Core_Action::formLink($deleteURL,
1856 CRM_Core_Action::DELETE,
5d6ac993
TO
1857 array(
1858 'id' => $form->get('id'),
6a488035 1859 'gid' => $form->get('gid'),
87dab4a4
AH
1860 ),
1861 ts('more'),
1862 FALSE,
1863 'contact.profileimage.delete',
1864 'Contact',
1865 $form->get('id')
6a488035
TO
1866 );
1867 $form->assign('deleteURL', $deleteURL);
1868 }
1869 $addressOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
1870 'address_options', TRUE, NULL, TRUE
1871 );
1872
1873 if (substr($fieldName, 0, 14) === 'state_province') {
8f9c3cbe 1874 $form->addChainSelect($name, array('label' => $title, 'required' => $required));
6a488035
TO
1875 $config = CRM_Core_Config::singleton();
1876 if (!in_array($mode, array(
5d6ac993 1877 CRM_Profile_Form::MODE_EDIT,
21dfd5f5 1878 CRM_Profile_Form::MODE_SEARCH,
5d6ac993 1879 )) &&
6a488035
TO
1880 $config->defaultContactStateProvince
1881 ) {
1882 $defaultValues[$name] = $config->defaultContactStateProvince;
1883 $form->setDefaults($defaultValues);
1884 }
1885 }
1886 elseif (substr($fieldName, 0, 7) === 'country') {
1f177c6b 1887 $form->add('select', $name, $title, array('' => ts('- select -')) + CRM_Core_PseudoConstant::country(), $required, $selectAttributes);
6a488035
TO
1888 $config = CRM_Core_Config::singleton();
1889 if (!in_array($mode, array(
5d6ac993 1890 CRM_Profile_Form::MODE_EDIT,
21dfd5f5 1891 CRM_Profile_Form::MODE_SEARCH,
5d6ac993 1892 )) &&
6a488035
TO
1893 $config->defaultContactCountry
1894 ) {
1895 $defaultValues[$name] = $config->defaultContactCountry;
1896 $form->setDefaults($defaultValues);
1897 }
1898 }
1899 elseif (substr($fieldName, 0, 6) === 'county') {
1900 if ($addressOptions['county']) {
8f9c3cbe 1901 $form->addChainSelect($name, array('label' => $title, 'required' => $required));
6a488035
TO
1902 }
1903 }
1904 elseif (substr($fieldName, 0, 9) === 'image_URL') {
1905 $form->add('file', $name, $title, $attributes, $required);
1906 $form->addUploadElement($name);
1907 }
1908 elseif (substr($fieldName, 0, 2) === 'im') {
1909 $form->add('text', $name, $title, $attributes, $required);
1910 if (!$contactId) {
133e2c99 1911 if ($usedFor) {
6a488035
TO
1912 if (substr($name, -1) == ']') {
1913 $providerName = substr($name, 0, -1) . '-provider_id]';
1914 }
1915 $form->add('select', $providerName, NULL,
1916 array(
21dfd5f5 1917 '' => ts('- select -'),
5d6ac993 1918 ) + CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id'), $required
6a488035
TO
1919 );
1920 }
1921 else {
1922 $form->add('select', $name . '-provider_id', $title,
1923 array(
21dfd5f5 1924 '' => ts('- select -'),
5d6ac993 1925 ) + CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id'), $required
6a488035
TO
1926 );
1927 }
1928
1929 if ($view && $mode != CRM_Profile_Form::MODE_SEARCH) {
1930 $form->freeze($name . '-provider_id');
1931 }
1932 }
1933 }
1934 elseif (($fieldName === 'birth_date') || ($fieldName === 'deceased_date')) {
1935 $form->addDate($name, $title, $required, array('formatType' => 'birth'));
1936 }
1937 elseif (in_array($fieldName, array(
5d6ac993
TO
1938 'membership_start_date',
1939 'membership_end_date',
21dfd5f5 1940 'join_date',
5d6ac993 1941 ))) {
5f80762f 1942 $form->addDate($name, $title, $required, array('formatType' => 'activityDate'));
6a488035 1943 }
5d6ac993 1944 elseif (CRM_Utils_Array::value('name', $field) == 'membership_type') {
6a488035
TO
1945 list($orgInfo, $types) = CRM_Member_BAO_MembershipType::getMembershipTypeInfo();
1946 $sel = &$form->addElement('hierselect', $name, $title);
5d6ac993
TO
1947 $select = array('' => ts('- select -'));
1948 if (count($orgInfo) == 1 && $field['is_required']) {
53cfc93c 1949 // we only have one org - so we should default to it. Not sure about defaulting to first type
1950 // as it could be missed - so adding a select
1951 // however, possibly that is more similar to the membership form
5d6ac993 1952 if (count($types[1]) > 1) {
53cfc93c 1953 $types[1] = $select + $types[1];
1954 }
1955 }
1956 else {
1957 $orgInfo = $select + $orgInfo;
1958 }
1959 $sel->setOptions(array($orgInfo, $types));
6a488035 1960 }
5d6ac993 1961 elseif (CRM_Utils_Array::value('name', $field) == 'membership_status') {
6a488035
TO
1962 $form->add('select', $name, $title,
1963 array(
21dfd5f5 1964 '' => ts('- select -'),
5d6ac993 1965 ) + CRM_Member_PseudoConstant::membershipStatus(NULL, NULL, 'label'), $required
6a488035
TO
1966 );
1967 }
ee015de4 1968 elseif (in_array($fieldName, array('gender_id', 'communication_style_id'))) {
1969 $options = array();
1970 $pseudoValues = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', $fieldName);
1971 foreach ($pseudoValues as $key => $var) {
1972 $options[$key] = $form->createElement('radio', NULL, ts($title), $var, $key);
6a488035 1973 }
ee015de4 1974 $group = $form->addGroup($options, $name, $title);
d4dcd180 1975 if ($required) {
1976 $form->addRule($name, ts('%1 is a required field.', array(1 => $title)), 'required');
6a488035 1977 }
8a4f27dc 1978 else {
b847e6e7 1979 $group->setAttribute('allowClear', TRUE);
8a4f27dc 1980 }
6a488035 1981 }
f45334bd 1982 elseif ($fieldName === 'prefix_id' || $fieldName === 'suffix_id') {
1f177c6b
CW
1983 $form->addSelect($name, array(
1984 'label' => $title,
1985 'entity' => 'contact',
1986 'field' => $fieldName,
1987 'class' => 'six',
1988 'placeholder' => '',
1989 ), $required);
6a488035
TO
1990 }
1991 elseif ($fieldName === 'contact_sub_type') {
1992 $gId = $form->get('gid') ? $form->get('gid') : CRM_Utils_Array::value('group_id', $field);
133e2c99 1993 if ($usedFor == 'onbehalf') {
6a488035
TO
1994 $profileType = 'Organization';
1995 }
133e2c99 1996 elseif ($usedFor == 'honor') {
1997 $profileType = CRM_Core_BAO_UFField::getProfileType($form->_params['honoree_profile_id']);
1998 }
6a488035
TO
1999 else {
2000 $profileType = $gId ? CRM_Core_BAO_UFField::getProfileType($gId) : NULL;
2001 if ($profileType == 'Contact') {
2002 $profileType = 'Individual';
2003 }
2004 }
2005
2006 $setSubtype = FALSE;
2007 if (CRM_Contact_BAO_ContactType::isaSubType($profileType)) {
2008 $setSubtype = $profileType;
2009 $profileType = CRM_Contact_BAO_ContactType::getBasicType($profileType);
2010 }
2011
2012 $subtypes = $profileType ? CRM_Contact_BAO_ContactType::subTypePairs($profileType) : array();
2013
2014 if ($setSubtype) {
2015 $subtypeList = array();
2016 $subtypeList[$setSubtype] = $subtypes[$setSubtype];
2017 }
2018 else {
2019 $subtypeList = $subtypes;
2020 }
2021
7ca3d518 2022 $form->add('select', $name, $title, $subtypeList, $required, array('class' => 'crm-select2', 'multiple' => TRUE));
6a488035
TO
2023 }
2024 elseif (in_array($fieldName, CRM_Contact_BAO_Contact::$_greetingTypes)) {
2025 //add email greeting, postal greeting, addressee, CRM-4575
2026 $gId = $form->get('gid') ? $form->get('gid') : CRM_Utils_Array::value('group_id', $field);
2027 $profileType = CRM_Core_BAO_UFField::getProfileType($gId, TRUE, FALSE, TRUE);
2028
2029 if (empty($profileType) || in_array($profileType, array(
5d6ac993
TO
2030 'Contact',
2031 'Contribution',
2032 'Participant',
21dfd5f5 2033 'Membership',
5d6ac993
TO
2034 ))
2035 ) {
6a488035
TO
2036 $profileType = 'Individual';
2037 }
2038 if (CRM_Contact_BAO_ContactType::isaSubType($profileType)) {
2039 $profileType = CRM_Contact_BAO_ContactType::getBasicType($profileType);
2040 }
2041 $greeting = array(
2042 'contact_type' => $profileType,
2043 'greeting_type' => $fieldName,
2044 );
2045 $form->add('select', $name, $title,
2046 array(
21dfd5f5 2047 '' => ts('- select -'),
5d6ac993 2048 ) + CRM_Core_PseudoConstant::greeting($greeting), $required
6a488035
TO
2049 );
2050 // add custom greeting element
2051 $form->add('text', $fieldName . '_custom', ts('Custom %1', array(1 => ucwords(str_replace('_', ' ', $fieldName)))),
2052 NULL, FALSE
2053 );
2054 }
2055 elseif ($fieldName === 'preferred_communication_method') {
e7e657f0 2056 $communicationFields = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method');
6a488035
TO
2057 foreach ($communicationFields as $key => $var) {
2058 if ($key == '') {
2059 continue;
2060 }
2061 $communicationOptions[] = $form->createElement('checkbox', $key, NULL, $var);
2062 }
2063 $form->addGroup($communicationOptions, $name, $title, '<br/>');
2064 }
2065 elseif ($fieldName === 'preferred_mail_format') {
2066 $form->add('select', $name, $title, CRM_Core_SelectValues::pmf());
2067 }
2068 elseif ($fieldName === 'preferred_language') {
c0c9cd82 2069 $form->add('select', $name, $title, array('' => ts('- select -')) + CRM_Contact_BAO_Contact::buildOptions('preferred_language'));
6a488035
TO
2070 }
2071 elseif ($fieldName == 'external_identifier') {
2072 $form->add('text', $name, $title, $attributes, $required);
2073 $contID = $contactId;
2074 if (!$contID) {
2075 $contID = $form->get('id');
2076 }
2077 $form->addRule($name,
2078 ts('External ID already exists in Database.'),
2079 'objectExists',
2080 array('CRM_Contact_DAO_Contact', $contID, 'external_identifier')
2081 );
2082 }
2083 elseif ($fieldName === 'group') {
2084 CRM_Contact_Form_Edit_TagsAndGroups::buildQuickForm($form, $contactId,
2085 CRM_Contact_Form_Edit_TagsAndGroups::GROUP,
2086 TRUE, $required,
2087 $title, NULL, $name
2088 );
2089 }
2090 elseif ($fieldName === 'tag') {
2091 CRM_Contact_Form_Edit_TagsAndGroups::buildQuickForm($form, $contactId,
2092 CRM_Contact_Form_Edit_TagsAndGroups::TAG,
2093 FALSE, $required,
2094 NULL, $title, $name
2095 );
2096 }
2097 elseif (substr($fieldName, 0, 4) === 'url-') {
eef9a6da
J
2098 $form->add('text', $name, $title, CRM_Core_DAO::getAttribute('CRM_Core_DAO_Website', 'url'), $required);
2099 $form->addRule($name, ts('Enter a valid web address beginning with \'http://\' or \'https://\'.'), 'url');
6a488035
TO
2100 }
2101 // Note should be rendered as textarea
2102 elseif (substr($fieldName, -4) == 'note') {
2103 $form->add('textarea', $name, $title, $attributes, $required);
2104 }
2105 elseif (substr($fieldName, 0, 6) === 'custom') {
2106 $customFieldID = CRM_Core_BAO_CustomField::getKeyID($fieldName);
2107 if ($customFieldID) {
3a7773be 2108 CRM_Core_BAO_CustomField::addQuickFormElement($form, $name, $customFieldID, $required, $search, $title);
6a488035
TO
2109 }
2110 }
2111 elseif (substr($fieldName, 0, 14) === 'address_custom') {
2112 list($fName, $locTypeId) = CRM_Utils_System::explode('-', $fieldName, 2);
2113 $customFieldID = CRM_Core_BAO_CustomField::getKeyID(substr($fName, 8));
2114 if ($customFieldID) {
3a7773be 2115 CRM_Core_BAO_CustomField::addQuickFormElement($form, $name, $customFieldID, $required, $search, $title);
6a488035
TO
2116 }
2117 }
2118 elseif (in_array($fieldName, array(
5d6ac993
TO
2119 'receive_date',
2120 'receipt_date',
2121 'thankyou_date',
21dfd5f5 2122 'cancel_date',
5d6ac993 2123 ))) {
6a488035
TO
2124 $form->addDateTime($name, $title, $required, array('formatType' => 'activityDateTime'));
2125 }
2126 elseif ($fieldName == 'send_receipt') {
2127 $form->addElement('checkbox', $name, $title);
2128 }
2129 elseif ($fieldName == 'soft_credit') {
ccec9d6b 2130 $form->addEntityRef("soft_credit_contact_id[$rowNumber]", ts('Soft Credit To'), array('create' => TRUE));
5ee60152 2131 $form->addMoney("soft_credit_amount[{$rowNumber}]", ts('Amount'), FALSE, NULL, FALSE);
6a488035
TO
2132 }
2133 elseif ($fieldName == 'product_name') {
2134 list($products, $options) = CRM_Contribute_BAO_Premium::getPremiumProductInfo();
2135 $sel = &$form->addElement('hierselect', $name, $title);
2136 $products = array(
e7483cbe
J
2137 '0' => ts('- select -'),
2138 ) + $products;
6a488035
TO
2139 $sel->setOptions(array($products, $options));
2140 }
2141 elseif ($fieldName == 'payment_instrument') {
2142 $form->add('select', $name, $title,
5d6ac993 2143 array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::paymentInstrument(), $required);
6a488035 2144 }
4c9b6178 2145 elseif ($fieldName == 'financial_type') {
6a488035
TO
2146 $form->add('select', $name, $title,
2147 array(
21dfd5f5 2148 '' => ts('- select -'),
5d6ac993 2149 ) + CRM_Contribute_PseudoConstant::financialType(), $required
6a488035
TO
2150 );
2151 }
2152 elseif ($fieldName == 'contribution_status_id') {
aaf5ca44
DG
2153 $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus();
2154 $statusName = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
2155 foreach (array(
2156 'In Progress',
2157 'Overdue',
21dfd5f5 2158 'Refunded',
aaf5ca44
DG
2159 ) as $suppress) {
2160 unset($contributionStatuses[CRM_Utils_Array::key($suppress, $statusName)]);
2161 }
6a73ef3f 2162
6a488035
TO
2163 $form->add('select', $name, $title,
2164 array(
21dfd5f5 2165 '' => ts('- select -'),
5d6ac993 2166 ) + $contributionStatuses, $required
6a488035
TO
2167 );
2168 }
51fa20cb 2169 elseif ($fieldName == 'soft_credit_type') {
9e1854a1 2170 $name = "soft_credit_type[$rowNumber]";
51fa20cb 2171 $form->add('select', $name, $title,
2172 array(
21dfd5f5 2173 '' => ts('- select -'),
5d6ac993 2174 ) + CRM_Core_OptionGroup::values("soft_credit_type")
51fa20cb 2175 );
9e1854a1 2176 //CRM-15350: choose SCT field default value as 'Gift' for membership use
2177 //else (for contribution), use configured SCT default value
2178 $SCTDefaultValue = CRM_Core_OptionGroup::getDefaultValue("soft_credit_type");
2179 if ($field['field_type'] == 'Membership') {
2180 $SCTDefaultValue = CRM_Core_OptionGroup::getValue('soft_credit_type', 'Gift', 'name');
2181 }
2182 $form->addElement('hidden', 'sct_default_id', $SCTDefaultValue, array('id' => 'sct_default_id'));
51fa20cb 2183 }
a4a361c9 2184 elseif ($fieldName == 'contribution_soft_credit_pcp_id') {
03ad81ae 2185 CRM_Contribute_Form_SoftCredit::addPCPFields($form, "[$rowNumber]");
51fa20cb 2186 }
6a488035
TO
2187 elseif ($fieldName == 'currency') {
2188 $form->addCurrency($name, $title, $required);
2189 }
2190 elseif ($fieldName == 'contribution_page_id') {
2191 $form->add('select', $name, $title,
2192 array(
21dfd5f5 2193 '' => ts('- select -'),
5d6ac993 2194 ) + CRM_Contribute_PseudoConstant::contributionPage(), $required, 'class="big"'
6a488035
TO
2195 );
2196 }
2197 elseif ($fieldName == 'participant_register_date') {
2198 $form->addDateTime($name, $title, $required, array('formatType' => 'activityDateTime'));
2199 }
2200 elseif ($fieldName == 'activity_status_id') {
2201 $form->add('select', $name, $title,
2202 array(
21dfd5f5 2203 '' => ts('- select -'),
5d6ac993 2204 ) + CRM_Core_PseudoConstant::activityStatus(), $required
6a488035
TO
2205 );
2206 }
2207 elseif ($fieldName == 'activity_engagement_level') {
2208 $form->add('select', $name, $title,
2209 array(
21dfd5f5 2210 '' => ts('- select -'),
5d6ac993 2211 ) + CRM_Campaign_PseudoConstant::engagementLevel(), $required
6a488035
TO
2212 );
2213 }
2214 elseif ($fieldName == 'activity_date_time') {
2215 $form->addDateTime($name, $title, $required, array('formatType' => 'activityDateTime'));
2216 }
2217 elseif ($fieldName == 'participant_status') {
2218 $cond = NULL;
2219 if ($online == TRUE) {
2220 $cond = 'visibility_id = 1';
2221 }
2222 $form->add('select', $name, $title,
2223 array(
21dfd5f5 2224 '' => ts('- select -'),
5d6ac993 2225 ) + CRM_Event_PseudoConstant::participantStatus(NULL, $cond, 'label'), $required
6a488035
TO
2226 );
2227 }
2228 elseif ($fieldName == 'participant_role') {
a7488080 2229 if (!empty($field['is_multiple'])) {
6a488035
TO
2230 $form->addCheckBox($name, $title, CRM_Event_PseudoConstant::participantRole(), NULL, NULL, NULL, NULL, '&nbsp', TRUE);
2231 }
2232 else {
2233 $form->add('select', $name, $title,
2234 array(
21dfd5f5 2235 '' => ts('- select -'),
5d6ac993 2236 ) + CRM_Event_PseudoConstant::participantRole(), $required
6a488035
TO
2237 );
2238 }
2239 }
2240 elseif ($fieldName == 'world_region') {
1f177c6b 2241 $form->add('select', $name, $title, CRM_Core_PseudoConstant::worldRegion(), $required, $selectAttributes);
6a488035
TO
2242 }
2243 elseif ($fieldName == 'signature_html') {
5d51a2f9 2244 $form->add('wysiwyg', $name, $title, CRM_Core_DAO::getAttribute('CRM_Core_DAO_Email', $fieldName));
6a488035
TO
2245 }
2246 elseif ($fieldName == 'signature_text') {
2247 $form->add('textarea', $name, $title, CRM_Core_DAO::getAttribute('CRM_Core_DAO_Email', $fieldName));
2248 }
2249 elseif (substr($fieldName, -11) == 'campaign_id') {
2250 if (CRM_Campaign_BAO_Campaign::isCampaignEnable()) {
2251 $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns(CRM_Utils_Array::value($contactId,
2252 $form->_componentCampaigns
2253 ));
2254 $form->add('select', $name, $title,
2255 array(
21dfd5f5 2256 '' => ts('- select -'),
5d6ac993 2257 ) + $campaigns, $required, 'class="crm-select2 big"'
6a488035
TO
2258 );
2259 }
2260 }
2261 elseif ($fieldName == 'activity_details') {
5d51a2f9 2262 $form->add('wysiwyg', $fieldName, $title, array('rows' => 4, 'cols' => 60), $required);
6a488035
TO
2263 }
2264 elseif ($fieldName == 'activity_duration') {
2265 $form->add('text', $name, $title, $attributes, $required);
2266 $form->addRule($name, ts('Please enter the duration as number of minutes (integers only).'), 'positiveInteger');
2267 }
2268 else {
2269 if (substr($fieldName, 0, 3) === 'is_' or substr($fieldName, 0, 7) === 'do_not_') {
2270 $form->add('advcheckbox', $name, $title, $attributes, $required);
2271 }
2272 else {
2273 $form->add('text', $name, $title, $attributes, $required);
2274 }
2275 }
2276
2277 static $hiddenSubtype = FALSE;
2278 if (!$hiddenSubtype && CRM_Contact_BAO_ContactType::isaSubType($field['field_type'])) {
2279 // In registration mode params are submitted via POST and we don't have any clue
2280 // about profile-id or the profile-type (which could be a subtype)
2281 // To generalize the behavior and simplify the process,
2282 // lets always add the hidden
2283 //subtype value if there is any, and we won't have to
2284 // compute it while processing.
133e2c99 2285 if ($usedFor) {
2286 $form->addElement('hidden', $usedFor . '[contact_sub_type]', $field['field_type']);
6a488035
TO
2287 }
2288 else {
2289 $form->addElement('hidden', 'contact_sub_type_hidden', $field['field_type']);
2290 }
2291 $hiddenSubtype = TRUE;
2292 }
2293
2294 if (($view && $mode != CRM_Profile_Form::MODE_SEARCH) || $isShared) {
2295 $form->freeze($name);
2296 }
2297
2298 //add the rules
2299 if (in_array($fieldName, array(
5d6ac993
TO
2300 'non_deductible_amount',
2301 'total_amount',
2302 'fee_amount',
21dfd5f5 2303 'net_amount',
5d6ac993 2304 ))) {
6a488035
TO
2305 $form->addRule($name, ts('Please enter a valid amount.'), 'money');
2306 }
6a488035
TO
2307 if ($rule) {
2308 if (!($rule == 'email' && $mode == CRM_Profile_Form::MODE_SEARCH)) {
2309 $form->addRule($name, ts('Please enter a valid %1', array(1 => $title)), $rule);
2310 }
2311 }
2312 }
2313
2314 /**
fe482240 2315 * Set profile defaults.
6a488035 2316 *
6a0b768e
TO
2317 * @param int $contactId
2318 * Contact id.
2319 * @param array $fields
2320 * Associative array of fields.
2321 * @param array $defaults
2322 * Defaults array.
2323 * @param bool $singleProfile
b581842f 2324 * True for single profile else false(Update multiple items).
6a0b768e
TO
2325 * @param int $componentId
2326 * Id for specific components like contribute, event etc.
2327 * @param null $component
6a488035 2328 */
e7483cbe 2329 public static function setProfileDefaults(
f9f40af3
TO
2330 $contactId, &$fields, &$defaults,
2331 $singleProfile = TRUE, $componentId = NULL, $component = NULL
6a488035
TO
2332 ) {
2333 if (!$componentId) {
2334 //get the contact details
2335 list($contactDetails, $options) = CRM_Contact_BAO_Contact::getHierContactDetails($contactId, $fields);
2336 $details = CRM_Utils_Array::value($contactId, $contactDetails);
2337 $multipleFields = array('website' => 'url');
2338
2339 //start of code to set the default values
2340 foreach ($fields as $name => $field) {
2341 // skip pseudo fields
2342 if (substr($name, 0, 9) == 'phone_ext') {
2343 continue;
2344 }
2345
b581842f 2346 //set the field name depending upon the profile mode(single/multiple)
6a488035
TO
2347 if ($singleProfile) {
2348 $fldName = $name;
2349 }
2350 else {
2351 $fldName = "field[$contactId][$name]";
2352 }
2353
2354 if ($name == 'group') {
2355 CRM_Contact_Form_Edit_TagsAndGroups::setDefaults($contactId, $defaults, CRM_Contact_Form_Edit_TagsAndGroups::GROUP, $fldName);
2356 }
2357 if ($name == 'tag') {
2358 CRM_Contact_Form_Edit_TagsAndGroups::setDefaults($contactId, $defaults, CRM_Contact_Form_Edit_TagsAndGroups::TAG, $fldName);
2359 }
2360
a7488080 2361 if (!empty($details[$name]) || isset($details[$name])) {
6a488035 2362 //to handle custom data (checkbox) to be written
58f00377 2363 // to handle birth/deceased date, greeting_type and few other fields
2364 if (($name == 'birth_date') || ($name == 'deceased_date')) {
6a488035
TO
2365 list($defaults[$fldName]) = CRM_Utils_Date::setDateDefaults($details[$name], 'birth');
2366 }
2367 elseif (in_array($name, CRM_Contact_BAO_Contact::$_greetingTypes)) {
2368 $defaults[$fldName] = $details[$name . '_id'];
2369 $defaults[$name . '_custom'] = $details[$name . '_custom'];
2370 }
2371 elseif ($name == 'preferred_communication_method') {
2372 $v = explode(CRM_Core_DAO::VALUE_SEPARATOR, $details[$name]);
2373 foreach ($v as $item) {
2374 if ($item) {
2375 $defaults[$fldName . "[$item]"] = 1;
2376 }
2377 }
2378 }
8aefc657
RK
2379 elseif ($name == 'contact_sub_type') {
2380 $defaults[$fldName] = explode(CRM_Core_DAO::VALUE_SEPARATOR, trim($details[$name], CRM_Core_DAO::VALUE_SEPARATOR));
2381 }
6a488035
TO
2382 elseif ($name == 'world_region') {
2383 $defaults[$fldName] = $details['worldregion_id'];
2384 }
2385 elseif ($customFieldId = CRM_Core_BAO_CustomField::getKeyID($name)) {
2386 //fix for custom fields
2387 $customFields = CRM_Core_BAO_CustomField::getFields(CRM_Utils_Array::value('contact_type', $details));
2388
2389 // hack to add custom data for components
2390 $components = array('Contribution', 'Participant', 'Membership', 'Activity');
2391 foreach ($components as $value) {
2392 $customFields = CRM_Utils_Array::crmArrayMerge($customFields,
2393 CRM_Core_BAO_CustomField::getFieldsForImport($value)
2394 );
2395 }
2396
2397 switch ($customFields[$customFieldId]['html_type']) {
2398 case 'Multi-Select State/Province':
2399 case 'Multi-Select Country':
2400 case 'AdvMulti-Select':
2401 case 'Multi-Select':
2402 $v = explode(CRM_Core_DAO::VALUE_SEPARATOR, $details[$name]);
2403 foreach ($v as $item) {
2404 if ($item) {
2405 $defaults[$fldName][$item] = $item;
2406 }
2407 }
2408 break;
2409
2410 case 'CheckBox':
2411 $v = explode(CRM_Core_DAO::VALUE_SEPARATOR, $details[$name]);
2412 foreach ($v as $item) {
2413 if ($item) {
2414 $defaults[$fldName][$item] = 1;
2415 // seems like we need this for QF style checkboxes in profile where its multiindexed
2416 // CRM-2969
2417 $defaults["{$fldName}[{$item}]"] = 1;
2418 }
2419 }
2420 break;
2421
6a488035
TO
2422 case 'Select Date':
2423 // CRM-6681, set defult values according to date and time format (if any).
2424 $dateFormat = NULL;
a7488080 2425 if (!empty($customFields[$customFieldId]['date_format'])) {
fd933dc5 2426 $dateFormat = $customFields[$customFieldId]['date_format'];
6a488035
TO
2427 }
2428
a7488080 2429 if (empty($customFields[$customFieldId]['time_format'])) {
6a488035
TO
2430 list($defaults[$fldName]) = CRM_Utils_Date::setDateDefaults($details[$name], NULL,
2431 $dateFormat
2432 );
2433 }
2434 else {
2435 $timeElement = $fldName . '_time';
2436 if (substr($fldName, -1) == ']') {
2437 $timeElement = substr($fldName, 0, -1) . '_time]';
2438 }
fd933dc5
KJ
2439 list($defaults[$fldName], $defaults[$timeElement]) = CRM_Utils_Date::setDateDefaults($details[$name],
2440 NULL, $dateFormat, $customFields[$customFieldId]['time_format']);
6a488035
TO
2441 }
2442 break;
2443
2444 default:
2445 $defaults[$fldName] = $details[$name];
2446 break;
2447 }
2448 }
2449 else {
2450 $defaults[$fldName] = $details[$name];
2451 }
2452 }
2453 else {
2454 $blocks = array('email', 'phone', 'im', 'openid');
2455 list($fieldName, $locTypeId, $phoneTypeId) = CRM_Utils_System::explode('-', $name, 3);
2456 if (!in_array($fieldName, $multipleFields)) {
2457 if (is_array($details)) {
2458 foreach ($details as $key => $value) {
2459 // when we fixed CRM-5319 - get primary loc
2460 // type as per loc field and removed below code.
2461 $primaryLocationType = FALSE;
2462 if ($locTypeId == 'Primary') {
5d6ac993 2463 if (is_array($value) && array_key_exists($fieldName, $value)) {
6a488035 2464 $primaryLocationType = TRUE;
5d6ac993 2465 if (in_array($fieldName, $blocks)) {
6a488035
TO
2466 $locTypeId = CRM_Contact_BAO_Contact::getPrimaryLocationType($contactId, FALSE, $fieldName);
2467 }
5d6ac993 2468 else {
6a488035
TO
2469 $locTypeId = CRM_Contact_BAO_Contact::getPrimaryLocationType($contactId, FALSE, 'address');
2470 }
2471 }
2472 }
2473
2474 // fixed for CRM-665
2475 if (is_numeric($locTypeId)) {
2476 if ($primaryLocationType || $locTypeId == CRM_Utils_Array::value('location_type_id', $value)) {
a7488080 2477 if (!empty($value[$fieldName])) {
6a488035
TO
2478 //to handle stateprovince and country
2479 if ($fieldName == 'state_province') {
2480 $defaults[$fldName] = $value['state_province_id'];
2481 }
2482 elseif ($fieldName == 'county') {
2483 $defaults[$fldName] = $value['county_id'];
2484 }
2485 elseif ($fieldName == 'country') {
6a488035
TO
2486 if (!isset($value['country_id']) || !$value['country_id']) {
2487 $config = CRM_Core_Config::singleton();
2488 if ($config->defaultContactCountry) {
2489 $defaults[$fldName] = $config->defaultContactCountry;
2490 }
2491 }
b3071092
DL
2492 else {
2493 $defaults[$fldName] = $value['country_id'];
2494 }
6a488035
TO
2495 }
2496 elseif ($fieldName == 'phone') {
2497 if ($phoneTypeId) {
2498 if (isset($value['phone'][$phoneTypeId])) {
2499 $defaults[$fldName] = $value['phone'][$phoneTypeId];
2500 }
2501 if (isset($value['phone_ext'][$phoneTypeId])) {
2502 $defaults[str_replace('phone', 'phone_ext', $fldName)] = $value['phone_ext'][$phoneTypeId];
2503 }
2504 }
2505 else {
2506 $phoneDefault = CRM_Utils_Array::value('phone', $value);
2507 // CRM-9216
2508 if (!is_array($phoneDefault)) {
2509 $defaults[$fldName] = $phoneDefault;
2510 }
2511 }
2512 }
2513 elseif ($fieldName == 'email') {
2514 //adding the first email (currently we don't support multiple emails of same location type)
2515 $defaults[$fldName] = $value['email'];
2516 }
2517 elseif ($fieldName == 'im') {
2518 //adding the first im (currently we don't support multiple ims of same location type)
2519 $defaults[$fldName] = $value['im'];
2520 $defaults[$fldName . '-provider_id'] = $value['im_provider_id'];
2521 }
2522 else {
2523 $defaults[$fldName] = $value[$fieldName];
2524 }
2525 }
2526 elseif (substr($fieldName, 0, 14) === 'address_custom' &&
2527 CRM_Utils_Array::value(substr($fieldName, 8), $value)
2528 ) {
2529 $defaults[$fldName] = $value[substr($fieldName, 8)];
2530 }
2531 }
2532 }
2533 }
2534 }
2535 }
2536 else {
2537 if (is_array($details)) {
1fd63589
EM
2538 if ($fieldName === 'url'
2539 && !empty($details['website'])
5d6ac993
TO
2540 && !empty($details['website'][$locTypeId])
2541 ) {
887e764d 2542 $defaults[$fldName] = CRM_Utils_Array::value('url', $details['website'][$locTypeId]);
6a488035
TO
2543 }
2544 }
2545 }
2546 }
2547 }
2548 }
2549
2550 //Handling Contribution Part of the batch profile
2551 if (CRM_Core_Permission::access('CiviContribute') && $component == 'Contribute') {
2552 self::setComponentDefaults($fields, $componentId, $component, $defaults);
2553 }
2554
2555 //Handling Event Participation Part of the batch profile
2556 if (CRM_Core_Permission::access('CiviEvent') && $component == 'Event') {
2557 self::setComponentDefaults($fields, $componentId, $component, $defaults);
2558 }
2559
2560 //Handling membership Part of the batch profile
2561 if (CRM_Core_Permission::access('CiviMember') && $component == 'Membership') {
2562 self::setComponentDefaults($fields, $componentId, $component, $defaults);
2563 }
2564
2565 //Handling Activity Part of the batch profile
2566 if ($component == 'Activity') {
2567 self::setComponentDefaults($fields, $componentId, $component, $defaults);
2568 }
2569 }
2570
2571 /**
100fef9d 2572 * Get profiles by type eg: pure Individual etc
6a488035 2573 *
6a0b768e
TO
2574 * @param array $types
2575 * Associative array of types eg: types('Individual').
2576 * @param bool $onlyPure
2577 * True if only pure profiles are required.
6a488035 2578 *
a6c01b45
CW
2579 * @return array
2580 * associative array of profiles
6a488035 2581 */
00be9182 2582 public static function getProfiles($types, $onlyPure = FALSE) {
6a488035 2583 $profiles = array();
ff4f7744 2584 $ufGroups = CRM_Core_PseudoConstant::get('CRM_Core_DAO_UFField', 'uf_group_id');
6a488035
TO
2585
2586 CRM_Utils_Hook::aclGroup(CRM_Core_Permission::ADMIN, NULL, 'civicrm_uf_group', $ufGroups, $ufGroups);
2587
2588 // Exclude Batch Data Entry profiles - CRM-10901
2589 $batchProfiles = CRM_Core_BAO_UFGroup::getBatchProfiles();
2590
2591 foreach ($ufGroups as $id => $title) {
2592 $ptype = CRM_Core_BAO_UFField::getProfileType($id, FALSE, $onlyPure);
2593 if (in_array($ptype, $types) && !array_key_exists($id, $batchProfiles)) {
2594 $profiles[$id] = $title;
2595 }
2596 }
2597 return $profiles;
2598 }
2599
2600 /**
100fef9d 2601 * Check whether a profile is valid combination of
6a488035
TO
2602 * required and/or optional profile types
2603 *
6a0b768e
TO
2604 * @param array $required
2605 * Array of types those are required.
2606 * @param array $optional
2607 * Array of types those are optional.
6a488035 2608 *
a6c01b45
CW
2609 * @return array
2610 * associative array of profiles
6a488035 2611 */
00be9182 2612 public static function getValidProfiles($required, $optional = NULL) {
6a488035 2613 if (!is_array($required) || empty($required)) {
e7483cbe 2614 return NULL;
6a488035
TO
2615 }
2616
2617 $profiles = array();
ff4f7744 2618 $ufGroups = CRM_Core_PseudoConstant::get('CRM_Core_DAO_UFField', 'uf_group_id');
6a488035
TO
2619
2620 CRM_Utils_Hook::aclGroup(CRM_Core_Permission::ADMIN, NULL, 'civicrm_uf_group', $ufGroups, $ufGroups);
2621
2622 foreach ($ufGroups as $id => $title) {
2623 $type = CRM_Core_BAO_UFField::checkValidProfileType($id, $required, $optional);
2624 if ($type) {
2625 $profiles[$id] = $title;
2626 }
2627 }
2628
2629 return $profiles;
2630 }
2631
2632 /**
100fef9d 2633 * Check whether a profile is valid combination of
6a488035
TO
2634 * required profile fields
2635 *
6a0b768e
TO
2636 * @param array $ufId
2637 * Integer id of the profile.
2638 * @param array $required
2639 * Array of fields those are required in the profile.
6a488035 2640 *
a6c01b45
CW
2641 * @return array
2642 * associative array of profiles
6a488035 2643 */
00be9182 2644 public static function checkValidProfile($ufId, $required = NULL) {
6a488035
TO
2645 $validProfile = FALSE;
2646 if (!$ufId) {
2647 return $validProfile;
2648 }
2649
2650 if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $ufId, 'is_active')) {
2651 return $validProfile;
2652 }
2653
2654 $profileFields = self::getFields($ufId, FALSE, CRM_Core_Action::VIEW, NULL,
2655 NULL, FALSE, NULL, FALSE, NULL,
2656 CRM_Core_Permission::CREATE, NULL
2657 );
2658
2659 $validProfile = array();
2660 if (!empty($profileFields)) {
2661 $fields = array_keys($profileFields);
2662 foreach ($fields as $val) {
2663 foreach ($required as $key => $field) {
2664 if (strpos($val, $field) === 0) {
2665 unset($required[$key]);
2666 }
2667 }
2668 }
2669
2670 $validProfile = (empty($required)) ? TRUE : FALSE;
2671 }
2672
2673 return $validProfile;
2674 }
2675
2676 /**
100fef9d 2677 * Get default value for Register.
6a488035 2678 *
72b3a70c
CW
2679 * @param array $fields
2680 * @param array $defaults
77b97be7 2681 *
72b3a70c 2682 * @return array
6a488035 2683 */
00be9182 2684 public static function setRegisterDefaults(&$fields, &$defaults) {
b525f1cc 2685 $config = CRM_Core_Config::singleton();
6a488035
TO
2686 foreach ($fields as $name => $field) {
2687 if (substr($name, 0, 8) == 'country-') {
b525f1cc 2688 if (!empty($config->defaultContactCountry)) {
6a488035
TO
2689 $defaults[$name] = $config->defaultContactCountry;
2690 }
2691 }
2692 elseif (substr($name, 0, 15) == 'state_province-') {
b525f1cc 2693 if (!empty($config->defaultContactStateProvince)) {
6a488035
TO
2694 $defaults[$name] = $config->defaultContactStateProvince;
2695 }
2696 }
2697 }
2698 return $defaults;
2699 }
2700
2701 /**
dc195289 2702 * make a copy of a profile, including
6a488035
TO
2703 * all the fields in the profile
2704 *
6a0b768e
TO
2705 * @param int $id
2706 * The profile id to copy.
6a488035 2707 *
8eedd10a 2708 * @return \CRM_Core_DAO
6a488035 2709 */
00be9182 2710 public static function copy($id) {
9739890f 2711 $maxId = CRM_Core_DAO::singleValueQuery("SELECT max(id) FROM civicrm_uf_group");
2712
2713 $title = ts('[Copy id %1]', array(1 => $maxId + 1));
2714 $fieldsFix = array(
2715 'suffix' => array(
2716 'title' => ' ' . $title,
2717 'name' => '__Copy_id_' . ($maxId + 1) . '_',
2718 ),
2719 );
2720
6a488035
TO
2721 $copy = &CRM_Core_DAO::copyGeneric('CRM_Core_DAO_UFGroup',
2722 array('id' => $id),
2723 NULL,
2724 $fieldsFix
2725 );
2726
2727 if ($pos = strrpos($copy->name, "_{$id}")) {
2728 $copy->name = substr_replace($copy->name, '', $pos);
2729 }
2730 $copy->name = CRM_Utils_String::munge($copy->name, '_', 56) . "_{$copy->id}";
2731 $copy->save();
2732
2733 $copyUFJoin = &CRM_Core_DAO::copyGeneric('CRM_Core_DAO_UFJoin',
2734 array('uf_group_id' => $id),
2735 array('uf_group_id' => $copy->id),
2736 NULL,
2737 'entity_table'
2738 );
2739
2740 $copyUFField = &CRM_Core_DAO::copyGeneric('CRM_Core_BAO_UFField',
2741 array('uf_group_id' => $id),
2742 array('uf_group_id' => $copy->id)
2743 );
2744
2745 $maxWeight = CRM_Utils_Weight::getMax('CRM_Core_DAO_UFJoin', NULL, 'weight');
2746
2747 //update the weight
2748 $query = "
2749UPDATE civicrm_uf_join
2750SET weight = %1
2751WHERE uf_group_id = %2
2752AND ( entity_id IS NULL OR entity_id <= 0 )
2753";
5d6ac993
TO
2754 $p = array(
2755 1 => array($maxWeight + 1, 'Integer'),
6a488035
TO
2756 2 => array($copy->id, 'Integer'),
2757 );
2758 CRM_Core_DAO::executeQuery($query, $p);
2759 if ($copy->is_reserved) {
2760 $query = "UPDATE civicrm_uf_group SET is_reserved = 0 WHERE id = %1";
2761 $params = array(1 => array($copy->id, 'Integer'));
2762 CRM_Core_DAO::executeQuery($query, $params);
2763 }
2764 CRM_Utils_Hook::copy('UFGroup', $copy);
2765
2766 return $copy;
2767 }
2768
2769 /**
2770 * Process that send notification e-mails
2771 *
6a0b768e
TO
2772 * @param int $contactID
2773 * Contact id.
2774 * @param array $values
2775 * Associative array of name/value pair.
6a488035 2776 */
00be9182 2777 public static function commonSendMail($contactID, &$values) {
6a488035
TO
2778 if (!$contactID || !$values) {
2779 return;
2780
2781 }
2782 $template = CRM_Core_Smarty::singleton();
2783
2784 $displayName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
2785 $contactID,
2786 'display_name'
2787 );
2788
2789 self::profileDisplay($values['id'], $values['values'], $template);
2790 $emailList = explode(',', $values['email']);
2791
2792 $contactLink = CRM_Utils_System::url('civicrm/contact/view',
2793 "reset=1&cid=$contactID",
5d6ac993 2794 TRUE, NULL, FALSE, FALSE, TRUE
6a488035
TO
2795 );
2796
2797 //get the default domain email address.
2798 list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain::getNameAndEmail();
2799
2800 if (!$domainEmailAddress || $domainEmailAddress == 'info@EXAMPLE.ORG') {
2801 $fixUrl = CRM_Utils_System::url('civicrm/admin/domain', 'action=update&reset=1');
2802 CRM_Core_Error::fatal(ts('The site administrator needs to enter a valid \'FROM Email Address\' in <a href="%1">Administer CiviCRM &raquo; Communications &raquo; FROM Email Addresses</a>. The email address used may need to be a valid mail account with your email service provider.', array(1 => $fixUrl)));
2803 }
2804
2805 foreach ($emailList as $emailTo) {
2806 // FIXME: take the below out of the foreach loop
c6327d7d 2807 CRM_Core_BAO_MessageTemplate::sendTemplate(
6a488035
TO
2808 array(
2809 'groupName' => 'msg_tpl_workflow_uf',
2810 'valueName' => 'uf_notify',
2811 'contactId' => $contactID,
2812 'tplParams' => array(
2813 'displayName' => $displayName,
2814 'currentDate' => date('r'),
2815 'contactLink' => $contactLink,
2816 ),
2817 'from' => "$domainEmailName <$domainEmailAddress>",
2818 'toEmail' => $emailTo,
2819 )
2820 );
2821 }
2822 }
2823
2824 /**
2825 * Given a contact id and a group id, returns the field values from the db
2826 * for this group and notify email only if group's notify field is
2827 * set and field values are not empty
2828 *
6a0b768e
TO
2829 * @param int $gid
2830 * Group id.
2831 * @param int $cid
2832 * Contact id.
c490a46a 2833 * @param array $params
1fd63589
EM
2834 * @param bool $skipCheck
2835 *
6a488035 2836 * @return array
6a488035 2837 */
00be9182 2838 public function checkFieldsEmptyValues($gid, $cid, $params, $skipCheck = FALSE) {
6a488035
TO
2839 if ($gid) {
2840 if (CRM_Core_BAO_UFGroup::filterUFGroups($gid, $cid) || $skipCheck) {
2841 $values = array();
2842 $fields = CRM_Core_BAO_UFGroup::getFields($gid, FALSE, CRM_Core_Action::VIEW);
2843 CRM_Core_BAO_UFGroup::getValues($cid, $fields, $values, FALSE, $params, TRUE);
2844
2845 $email = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $gid, 'notify');
2846
2847 if (!empty($values) &&
2848 !empty($email)
2849 ) {
2850 $val = array(
2851 'id' => $gid,
2852 'values' => $values,
2853 'email' => $email,
2854 );
2855 return $val;
2856 }
2857 }
2858 }
2859 return NULL;
2860 }
2861
2862 /**
fe482240 2863 * Assign uf fields to template.
6a488035 2864 *
6a0b768e
TO
2865 * @param int $gid
2866 * Group id.
c490a46a
CW
2867 * @param array $values
2868 * @param CRM_Core_Smarty $template
6a488035 2869 */
59b36d14 2870 static public function profileDisplay($gid, $values, $template) {
6a488035
TO
2871 $groupTitle = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $gid, 'title');
2872 $template->assign('grouptitle', $groupTitle);
2873 if (count($values)) {
2874 $template->assign('values', $values);
2875 }
2876 }
2877
2878 /**
fe482240 2879 * Format fields for dupe Contact Matching.
6a488035 2880 *
6a0b768e 2881 * @param array $params
6a488035 2882 *
100fef9d 2883 * @param int $contactId
1fd63589 2884 *
a6c01b45 2885 * @return array
b44e3f84 2886 * associated formatted array
6a488035 2887 */
00be9182 2888 public static function formatFields($params, $contactId = NULL) {
6a488035
TO
2889 if ($contactId) {
2890 // get the primary location type id and email
2891 list($name, $primaryEmail, $primaryLocationType) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactId);
2892 }
2893 else {
2894 $defaultLocationType = CRM_Core_BAO_LocationType::getDefault();
2895 $primaryLocationType = $defaultLocationType->id;
2896 }
2897
5d6ac993
TO
2898 $data = array();
2899 $locationType = array();
2900 $count = 1;
6a488035
TO
2901 $primaryLocation = 0;
2902 foreach ($params as $key => $value) {
2903 list($fieldName, $locTypeId, $phoneTypeId) = explode('-', $key);
2904
2905 if ($locTypeId == 'Primary') {
2906 $locTypeId = $primaryLocationType;
2907 }
2908
2909 if (is_numeric($locTypeId)) {
2910 if (!in_array($locTypeId, $locationType)) {
2911 $locationType[$count] = $locTypeId;
2912 $count++;
2913 }
2914 $loc = CRM_Utils_Array::key($locTypeId, $locationType);
2915
2916 $data['location'][$loc]['location_type_id'] = $locTypeId;
2917
2918 // if we are getting in a new primary email, dont overwrite the new one
2919 if ($locTypeId == $primaryLocationType) {
a7488080 2920 if (!empty($params['email-' . $primaryLocationType])) {
6a488035
TO
2921 $data['location'][$loc]['email'][$loc]['email'] = $fields['email-' . $primaryLocationType];
2922 }
2923 elseif (isset($primaryEmail)) {
2924 $data['location'][$loc]['email'][$loc]['email'] = $primaryEmail;
2925 }
2926 $primaryLocation++;
2927 }
2928
2929 if ($loc == 1) {
2930 $data['location'][$loc]['is_primary'] = 1;
2931 }
2932 if ($fieldName == 'phone') {
2933 if ($phoneTypeId) {
2934 $data['location'][$loc]['phone'][$loc]['phone_type_id'] = $phoneTypeId;
2935 }
2936 else {
2937 $data['location'][$loc]['phone'][$loc]['phone_type_id'] = '';
2938 }
2939 $data['location'][$loc]['phone'][$loc]['phone'] = $value;
2940 }
2941 elseif ($fieldName == 'email') {
2942 $data['location'][$loc]['email'][$loc]['email'] = $value;
2943 }
2944 elseif ($fieldName == 'im') {
2945 $data['location'][$loc]['im'][$loc]['name'] = $value;
2946 }
2947 else {
2948 if ($fieldName === 'state_province') {
2949 $data['location'][$loc]['address']['state_province_id'] = $value;
2950 }
2951 elseif ($fieldName === 'country') {
2952 $data['location'][$loc]['address']['country_id'] = $value;
2953 }
2954 else {
2955 $data['location'][$loc]['address'][$fieldName] = $value;
2956 }
2957 }
2958 }
2959 else {
67744c4e 2960 // TODO: prefix, suffix and gender translation may no longer be necessary - check inputs
6a488035
TO
2961 if ($key === 'individual_suffix') {
2962 $data['suffix_id'] = $value;
2963 }
2964 elseif ($key === 'individual_prefix') {
2965 $data['prefix_id'] = $value;
2966 }
2967 elseif ($key === 'gender') {
2968 $data['gender_id'] = $value;
2969 }
2970 elseif (substr($key, 0, 6) === 'custom') {
2971 if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
2972 //fix checkbox
2973 if ($customFields[$customFieldID]['html_type'] == 'CheckBox') {
2974 $value = implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($value));
2975 }
2976 // fix the date field
2977 if ($customFields[$customFieldID]['data_type'] == 'Date') {
2978 $date = CRM_Utils_Date::format($value);
2979 if (!$date) {
2980 $date = '';
2981 }
2982 $value = $date;
2983 }
2984
2985 $data['custom'][$customFieldID] = array(
2986 'id' => $id,
2987 'value' => $value,
2988 'extends' => $customFields[$customFieldID]['extends'],
2989 'type' => $customFields[$customFieldID]['data_type'],
2990 'custom_field_id' => $customFieldID,
2991 );
2992 }
2993 }
2994 elseif ($key == 'edit') {
2995 continue;
2996 }
2997 else {
2998 $data[$key] = $value;
2999 }
3000 }
3001 }
3002
3003 if (!$primaryLocation) {
3004 $loc++;
3005 $data['location'][$loc]['email'][$loc]['email'] = $primaryEmail;
3006 }
3007
6a488035
TO
3008 return $data;
3009 }
3010
3011 /**
100fef9d 3012 * Calculate the profile type 'group_type' as per profile fields.
6a488035 3013 *
6a0b768e
TO
3014 * @param int $gId
3015 * Profile id.
1fd63589 3016 * @param bool $includeTypeValues
6a0b768e
TO
3017 * @param int $ignoreFieldId
3018 * Ignore particular profile field.
6a488035 3019 *
a6c01b45
CW
3020 * @return array
3021 * list of calculated group type
6a488035 3022 */
00be9182 3023 public static function calculateGroupType($gId, $includeTypeValues = FALSE, $ignoreFieldId = NULL) {
6a488035
TO
3024 //get the profile fields.
3025 $ufFields = self::getFields($gId, FALSE, NULL, NULL, NULL, TRUE, NULL, TRUE);
3026 return self::_calculateGroupType($ufFields, $includeTypeValues, $ignoreFieldId);
3027 }
3028
3029 /**
100fef9d 3030 * Calculate the profile type 'group_type' as per profile fields.
6a488035 3031 *
1fd63589
EM
3032 * @param $ufFields
3033 * @param bool $includeTypeValues
6a0b768e
TO
3034 * @param int $ignoreFieldId
3035 * Ignore perticular profile field.
6a488035 3036 *
a6c01b45
CW
3037 * @return array
3038 * list of calculated group type
6a488035 3039 */
00be9182 3040 public static function _calculateGroupType($ufFields, $includeTypeValues = FALSE, $ignoreFieldId = NULL) {
6a488035
TO
3041 $groupType = $groupTypeValues = $customFieldIds = array();
3042 if (!empty($ufFields)) {
3043 foreach ($ufFields as $fieldName => $fieldValue) {
3044 //ignore field from group type when provided.
3045 //in case of update profile field.
3046 if ($ignoreFieldId && ($ignoreFieldId == $fieldValue['field_id'])) {
3047 continue;
3048 }
3049 if (!in_array($fieldValue['field_type'], $groupType)) {
3050 $groupType[$fieldValue['field_type']] = $fieldValue['field_type'];
3051 }
3052
3053 if ($includeTypeValues && ($fldId = CRM_Core_BAO_CustomField::getKeyID($fieldName))) {
3054 $customFieldIds[$fldId] = $fldId;
3055 }
3056 }
3057 }
3058
3059 if (!empty($customFieldIds)) {
3060 $query = 'SELECT DISTINCT(cg.id), cg.extends, cg.extends_entity_column_id, cg.extends_entity_column_value FROM civicrm_custom_group cg LEFT JOIN civicrm_custom_field cf ON cf.custom_group_id = cg.id WHERE cg.extends_entity_column_value IS NOT NULL AND cf.id IN (' . implode(',', $customFieldIds) . ')';
3061
3062 $customGroups = CRM_Core_DAO::executeQuery($query);
3063 while ($customGroups->fetch()) {
3064 if (!$customGroups->extends_entity_column_value) {
3065 continue;
3066 }
3067
3068 $groupTypeName = "{$customGroups->extends}Type";
3069 if ($customGroups->extends == 'Participant' && $customGroups->extends_entity_column_id) {
3070 $groupTypeName = CRM_Core_OptionGroup::getValue('custom_data_type', $customGroups->extends_entity_column_id, 'value', 'String', 'name');
3071 }
3072
3073 foreach (explode(CRM_Core_DAO::VALUE_SEPARATOR, $customGroups->extends_entity_column_value) as $val) {
3074 if ($val) {
3075 $groupTypeValues[$groupTypeName][$val] = $val;
3076 }
3077 }
3078 }
3079
3080 if (!empty($groupTypeValues)) {
3081 $groupType = array_merge($groupType, $groupTypeValues);
3082 }
3083 }
3084
3085 return $groupType;
3086 }
3087
3088 /**
3089 * Update the profile type 'group_type' as per profile fields including group types and group subtype values.
3090 * Build and store string like: group_type1,group_type2[VALUE_SEPERATOR]group_type1Type:1:2:3,group_type2Type:1:2
3091 *
3092 * FIELDS GROUP_TYPE
3093 * BirthDate + Email Individual,Contact
3094 * BirthDate + Subject Individual,Activity
3095 * BirthDate + Subject + SurveyOnlyField Individual,Activity\0ActivityType:28
3096 * BirthDate + Subject + SurveyOnlyField + PhoneOnlyField (Not allowed)
3097 * BirthDate + SurveyOnlyField Individual,Activity\0ActivityType:28
3098 * BirthDate + Subject + SurveyOrPhoneField Individual,Activity\0ActivityType:2:28
3099 * BirthDate + SurveyOrPhoneField Individual,Activity\0ActivityType:2:28
3100 * BirthDate + SurveyOrPhoneField + SurveyOnlyField Individual,Activity\0ActivityType:2:28
3101 * BirthDate + StudentField + Subject + SurveyOnlyField Individual,Activity,Student\0ActivityType:28
3102 *
c490a46a 3103 * @param int $gId
6a0b768e
TO
3104 * @param array $groupTypes
3105 * With key having group type names.
6a488035 3106 *
e7483cbe 3107 * @return bool
6a488035 3108 */
00be9182 3109 public static function updateGroupTypes($gId, $groupTypes = array()) {
6a488035
TO
3110 if (!is_array($groupTypes) || !$gId) {
3111 return FALSE;
3112 }
3113
3114 // If empty group types set group_type as 'null'
3115 if (empty($groupTypes)) {
3116 return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_UFGroup', $gId, 'group_type', 'null');
3117 }
3118
1cb28d5d 3119 $componentGroupTypes = array('Contribution', 'Participant', 'Membership', 'Activity', 'Case');
5d6ac993
TO
3120 $validGroupTypes = array_merge(array(
3121 'Contact',
3122 'Individual',
3123 'Organization',
21dfd5f5 3124 'Household',
5d6ac993 3125 ), $componentGroupTypes, CRM_Contact_BAO_ContactType::subTypes());
6a488035
TO
3126
3127 $gTypes = $gTypeValues = array();
3128
3129 $participantExtends = array('ParticipantRole', 'ParticipantEventName', 'ParticipantEventType');
3130 // Get valid group type and group subtypes
3131 foreach ($groupTypes as $groupType => $value) {
3132 if (in_array($groupType, $validGroupTypes) && !in_array($groupType, $gTypes)) {
3133 $gTypes[] = $groupType;
3134 }
3135
3136 $subTypesOf = NULL;
3137
3138 if (in_array($groupType, $participantExtends)) {
3139 $subTypesOf = $groupType;
3140 }
3141 elseif (strpos($groupType, 'Type') > 0) {
3142 $subTypesOf = substr($groupType, 0, strpos($groupType, 'Type'));
3143 }
3144 else {
3145 continue;
3146 }
3147
3148 if (!empty($value) &&
3149 (in_array($subTypesOf, $componentGroupTypes) ||
3150 in_array($subTypesOf, $participantExtends)
3151 )
3152 ) {
3153 $gTypeValues[$subTypesOf] = $groupType . ":" . implode(':', $value);
3154 }
3155 }
3156
3157 if (empty($gTypes)) {
3158 return FALSE;
3159 }
3160
3161 // Build String to store group types and group subtypes
3162 $groupTypeString = implode(',', $gTypes);
3163 if (!empty($gTypeValues)) {
3164 $groupTypeString .= CRM_Core_DAO::VALUE_SEPARATOR . implode(',', $gTypeValues);
3165 }
3166
3167 return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_UFGroup', $gId, 'group_type', $groupTypeString);
3168 }
3169
3170 /**
fe482240 3171 * Create a "group_type" string.
6a488035 3172 *
6a0b768e
TO
3173 * @param array $coreTypes
3174 * E.g. array('Individual','Contact','Student').
3175 * @param array $subTypes
3176 * E.g. array('ActivityType' => array(7, 11)).
6a488035 3177 * @param string $delim
1fd63589
EM
3178 *
3179 * @return string
6a488035
TO
3180 * @throws CRM_Core_Exception
3181 */
00be9182 3182 public static function encodeGroupType($coreTypes, $subTypes, $delim = CRM_Core_DAO::VALUE_SEPARATOR) {
6a488035
TO
3183 $groupTypeExpr = '';
3184 if ($coreTypes) {
3185 $groupTypeExpr .= implode(',', $coreTypes);
3186 }
3187 if ($subTypes) {
99e239bc 3188 //CRM-15427 Allow Multiple subtype filtering
3189 //if (count($subTypes) > 1) {
5d6ac993 3190 //throw new CRM_Core_Exception("Multiple subtype filtering is not currently supported by widget.");
99e239bc 3191 //}
6a488035
TO
3192 foreach ($subTypes as $subType => $subTypeIds) {
3193 $groupTypeExpr .= $delim . $subType . ':' . implode(':', $subTypeIds);
3194 }
3195 }
3196 return $groupTypeExpr;
3197 }
3198
3199 /**
dc195289 3200 * setDefault componet specific profile fields.
6a488035 3201 *
6a0b768e
TO
3202 * @param array $fields
3203 * Profile fields.
3204 * @param int $componentId
3205 * ComponetID.
3206 * @param string $component
3207 * Component name.
3208 * @param array $defaults
3209 * An array of default values.
1fd63589
EM
3210 *
3211 * @param bool $isStandalone
6a488035 3212 */
f0385140 3213 public static function setComponentDefaults(&$fields, $componentId, $component, &$defaults, $isStandalone = FALSE) {
6a488035
TO
3214 if (!$componentId ||
3215 !in_array($component, array('Contribute', 'Membership', 'Event', 'Activity'))
3216 ) {
3217 return;
3218 }
3219
3220 $componentBAO = $componentSubType = NULL;
3221 switch ($component) {
3222 case 'Membership':
5d6ac993 3223 $componentBAO = 'CRM_Member_BAO_Membership';
6a488035
TO
3224 $componentBAOName = 'Membership';
3225 $componentSubType = array('membership_type_id');
3226 break;
3227
3228 case 'Contribute':
5d6ac993 3229 $componentBAO = 'CRM_Contribute_BAO_Contribution';
6a488035 3230 $componentBAOName = 'Contribution';
5d6ac993 3231 $componentSubType = array('financial_type_id');
6a488035
TO
3232 break;
3233
3234 case 'Event':
5d6ac993 3235 $componentBAO = 'CRM_Event_BAO_Participant';
6a488035 3236 $componentBAOName = 'Participant';
d623de42 3237 $componentSubType = array('role_id', 'event_id', 'event_type_id');
6a488035
TO
3238 break;
3239
3240 case 'Activity':
5d6ac993 3241 $componentBAO = 'CRM_Activity_BAO_Activity';
6a488035
TO
3242 $componentBAOName = 'Activity';
3243 $componentSubType = array('activity_type_id');
3244 break;
3245 }
3246
3247 $values = array();
3248 $params = array('id' => $componentId);
3249
3250 //get the component values.
3251 CRM_Core_DAO::commonRetrieve($componentBAO, $params, $values);
d623de42
M
3252 if ($componentBAOName == 'Participant') {
3253 $values += array('event_type_id' => CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $values['event_id'], 'event_type_id'));
3254 }
6a488035
TO
3255
3256 $formattedGroupTree = array();
5d6ac993
TO
3257 $dateTimeFields = array(
3258 'participant_register_date',
3259 'activity_date_time',
3260 'receive_date',
3261 'receipt_date',
3262 'cancel_date',
3263 'thankyou_date',
3264 'membership_start_date',
3265 'membership_end_date',
21dfd5f5 3266 'join_date',
5d6ac993 3267 );
6a488035
TO
3268 foreach ($fields as $name => $field) {
3269 $fldName = $isStandalone ? $name : "field[$componentId][$name]";
3270 if (in_array($name, $dateTimeFields)) {
3271 $timefldName = $isStandalone ? "{$name}_time" : "field[$componentId][{$name}_time]";
a7488080 3272 if (!empty($values[$name])) {
6a488035
TO
3273 list($defaults[$fldName], $defaults[$timefldName]) = CRM_Utils_Date::setDateDefaults($values[$name]);
3274 }
3275 }
3276 elseif (array_key_exists($name, $values)) {
3277 $defaults[$fldName] = $values[$name];
3278 }
3279 elseif ($name == 'participant_note') {
5d6ac993 3280 $noteDetails = CRM_Core_BAO_Note::getNote($componentId, 'civicrm_participant');
6a488035
TO
3281 $defaults[$fldName] = array_pop($noteDetails);
3282 }
3283 elseif (in_array($name, array(
5d6ac993
TO
3284 'financial_type',
3285 'payment_instrument',
3286 'participant_status',
21dfd5f5 3287 'participant_role',
5d6ac993 3288 ))) {
6a488035
TO
3289 $defaults[$fldName] = $values["{$name}_id"];
3290 }
3291 elseif ($name == 'membership_type') {
3292 // since membership_type field is a hierselect -
e7483cbe
J
3293 $defaults[$fldName][0]
3294 = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $values['membership_type_id'], 'member_of_contact_id', 'id');
6a488035
TO
3295 $defaults[$fldName][1] = $values['membership_type_id'];
3296 }
3297 elseif ($name == 'membership_status') {
3298 $defaults[$fldName] = $values['status_id'];
3299 }
3300 elseif ($customFieldInfo = CRM_Core_BAO_CustomField::getKeyID($name, TRUE)) {
3301 if (empty($formattedGroupTree)) {
3302 //get the groupTree as per subTypes.
3303 $groupTree = array();
3304 foreach ($componentSubType as $subType) {
3305 $subTree = CRM_Core_BAO_CustomGroup::getTree($componentBAOName, CRM_Core_DAO::$_nullObject,
3306 $componentId, 0, $values[$subType]
3307 );
3308 $groupTree = CRM_Utils_Array::crmArrayMerge($groupTree, $subTree);
3309 }
1273d77c 3310 $formattedGroupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, 1);
6a488035
TO
3311 CRM_Core_BAO_CustomGroup::setDefaults($formattedGroupTree, $defaults);
3312 }
3313
3314 //FIX ME: We need to loop defaults, but once we move to custom_1_x convention this code can be simplified.
3315 foreach ($defaults as $customKey => $customValue) {
3316 if ($customFieldDetails = CRM_Core_BAO_CustomField::getKeyID($customKey, TRUE)) {
3317 if ($name == 'custom_' . $customFieldDetails[0]) {
3318
3319 //hack to set default for checkbox
3320 //basically this is for weired field name like field[33][custom_19]
3321 //we are converting this field name to array structure and assign value.
3322 $skipValue = FALSE;
3323
3324 foreach ($formattedGroupTree as $tree) {
d623de42
M
3325 if (!empty($tree['fields'][$customFieldDetails[0]])) {
3326 if ('CheckBox' == CRM_Utils_Array::value('html_type', $tree['fields'][$customFieldDetails[0]])) {
3327 $skipValue = TRUE;
3328 $defaults['field'][$componentId][$name] = $customValue;
3329 break;
3330 }
3331 elseif (CRM_Utils_Array::value('data_type', $tree['fields'][$customFieldDetails[0]]) == 'Date') {
3332 $skipValue = TRUE;
6a488035 3333
d623de42
M
3334 // CRM-6681, $default contains formatted date, time values.
3335 $defaults[$fldName] = $customValue;
3336 if (!empty($defaults[$customKey . '_time'])) {
3337 $defaults['field'][$componentId][$name . '_time'] = $defaults[$customKey . '_time'];
3338 }
6a488035
TO
3339 }
3340 }
3341 }
3342
3343 if (!$skipValue || $isStandalone) {
3344 $defaults[$fldName] = $customValue;
3345 }
3346 unset($defaults[$customKey]);
3347 break;
3348 }
3349 }
3350 }
3351 }
3352 }
3353 }
3354
79ae07d9
CW
3355 /**
3356 * @param array|string $profiles - name of profile(s) to create links for
6a0b768e
TO
3357 * @param array $appendProfiles
3358 * Name of profile(s) to append to each link.
1fd63589
EM
3359 *
3360 * @return array
79ae07d9 3361 */
00be9182 3362 public static function getCreateLinks($profiles = '', $appendProfiles = array()) {
a4799f04
CW
3363 // Default to contact profiles
3364 if (!$profiles) {
3365 $profiles = array('new_individual', 'new_organization', 'new_household');
3366 }
79ae07d9 3367 $profiles = (array) $profiles;
1a90e0dd 3368 $toGet = array_merge($profiles, (array) $appendProfiles);
79ae07d9
CW
3369 $retrieved = civicrm_api3('uf_group', 'get', array(
3370 'name' => array('IN' => $toGet),
3371 'is_active' => 1,
3372 ));
3373 $links = $append = array();
3374 if (!empty($retrieved['values'])) {
5d6ac993 3375 foreach ($retrieved['values'] as $id => $profile) {
79ae07d9
CW
3376 if (in_array($profile['name'], $profiles)) {
3377 $links[] = array(
3378 'label' => $profile['title'],
a4799f04 3379 'url' => CRM_Utils_System::url('civicrm/profile/create', "reset=1&context=dialog&gid=$id",
5d6ac993 3380 NULL, NULL, FALSE, FALSE, TRUE),
a4799f04 3381 'type' => ucfirst(str_replace('new_', '', $profile['name'])),
79ae07d9
CW
3382 );
3383 }
3384 else {
3385 $append[] = $id;
3386 }
3387 }
3388 foreach ($append as $id) {
3389 foreach ($links as &$link) {
3390 $link['url'] .= ",$id";
3391 }
3392 }
3393 }
3394 return $links;
3395 }
3396
6a488035 3397 /**
fe482240 3398 * Retrieve groups of profiles.
6a488035 3399 *
6a0b768e
TO
3400 * @param int $profileID
3401 * Id of the profile.
6a488035 3402 *
a6c01b45
CW
3403 * @return array
3404 * returns array
6a488035 3405 */
00be9182 3406 public static function profileGroups($profileID) {
6a488035
TO
3407 $groupTypes = array();
3408 $profileTypes = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $profileID, 'group_type');
3409 if ($profileTypes) {
3410 $groupTypeParts = explode(CRM_Core_DAO::VALUE_SEPARATOR, $profileTypes);
3411 $groupTypes = explode(',', $groupTypeParts[0]);
3412 }
3413 return $groupTypes;
3414 }
3415
3416 /**
100fef9d 3417 * Alter contact params by filtering existing subscribed groups and returns
6a488035
TO
3418 * unsubscribed groups array for subscription.
3419 *
6a0b768e
TO
3420 * @param array $params
3421 * Contact params.
3422 * @param int $contactId
3423 * User contact id.
6a488035 3424 *
a6c01b45
CW
3425 * @return array
3426 * This contains array of groups for subscription
6a488035 3427 */
00be9182 3428 public static function getDoubleOptInGroupIds(&$params, $contactId = NULL) {
6a488035
TO
3429 $config = CRM_Core_Config::singleton();
3430 $subscribeGroupIds = array();
3431
3432 // process further only if profileDoubleOptIn enabled and if groups exist
3433 if (!array_key_exists('group', $params) ||
3434 !self::isProfileDoubleOptin() ||
3435 CRM_Utils_System::isNull($params['group'])
3436 ) {
3437 return $subscribeGroupIds;
3438 }
3439
3440 //check if contact email exist.
3441 $hasEmails = FALSE;
3442 foreach ($params as $name => $value) {
3443 if (strpos($name, 'email-') !== FALSE) {
3444 $hasEmails = TRUE;
3445 break;
3446 }
3447 }
3448
3449 //Proceed furthur only if email present
3450 if (!$hasEmails) {
3451 return $subscribeGroupIds;
3452 }
3453
3454 //do check for already subscriptions.
3455 $contactGroups = array();
3456 if ($contactId) {
3457 $query = "
3458SELECT group_id
3459 FROM civicrm_group_contact
3460 WHERE status = 'Added'
3461 AND contact_id = %1";
3462
3463 $dao = CRM_Core_DAO::executeQuery($query, array(1 => array($contactId, 'Integer')));
3464 while ($dao->fetch()) {
3465 $contactGroups[$dao->group_id] = $dao->group_id;
3466 }
3467 }
3468
3469 //since we don't have names, compare w/ label.
3470 $mailingListGroupType = array_search('Mailing List', CRM_Core_OptionGroup::values('group_type'));
3471
3472 //actual processing start.
3473 foreach ($params['group'] as $groupId => $isSelected) {
3474 //unset group those are not selected.
3475 if (!$isSelected) {
3476 unset($params['group'][$groupId]);
3477 continue;
3478 }
3479
3480 $groupTypes = explode(CRM_Core_DAO::VALUE_SEPARATOR,
3481 CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $groupId, 'group_type', 'id')
3482 );
3483 //get only mailing type group and unset it from params
3484 if (in_array($mailingListGroupType, $groupTypes) && !in_array($groupId, $contactGroups)) {
3485 $subscribeGroupIds[$groupId] = $groupId;
3486 unset($params['group'][$groupId]);
3487 }
3488 }
3489
3490 return $subscribeGroupIds;
3491 }
3492
3493 /**
fe482240 3494 * Check if we are rendering mixed profiles.
6a488035 3495 *
6a0b768e
TO
3496 * @param array $profileIds
3497 * Associated array of profile ids.
6a488035 3498 *
e7483cbe 3499 * @return bool
a6c01b45 3500 * true if profile is mixed
6a488035 3501 */
00be9182 3502 public static function checkForMixProfiles($profileIds) {
6a488035
TO
3503 $mixProfile = FALSE;
3504
3505 $contactTypes = array('Individual', 'Household', 'Organization');
3506 $subTypes = CRM_Contact_BAO_ContactType::subTypes();
3507
3508 $components = array('Contribution', 'Participant', 'Membership', 'Activity');
3509
3510 $typeCount = array('ctype' => array(), 'subtype' => array());
3511 foreach ($profileIds as $gid) {
3512 $profileType = CRM_Core_BAO_UFField::getProfileType($gid);
3513 // ignore profile of type Contact
3514 if ($profileType == 'Contact') {
3515 continue;
3516 }
3517 if (in_array($profileType, $contactTypes)) {
3518 if (!isset($typeCount['ctype'][$profileType])) {
3519 $typeCount['ctype'][$profileType] = 1;
3520 }
3521
3522 // check if we are rendering profile of different contact types
3523 if (count($typeCount['ctype']) == 2) {
3524 $mixProfile = TRUE;
3525 break;
3526 }
3527 }
3528 elseif (in_array($profileType, $components)) {
3529 $mixProfile = TRUE;
3530 break;
3531 }
3532 else {
3533 if (!isset($typeCount['subtype'][$profileType])) {
3534 $typeCount['subtype'][$profileType] = 1;
3535 }
3536 // check if we are rendering profile of different contact sub types
3537 if (count($typeCount['subtype']) == 2) {
3538 $mixProfile = TRUE;
3539 break;
3540 }
3541 }
3542 }
3543 return $mixProfile;
3544 }
3545
3546 /**
fe482240 3547 * Determine of we show overlay profile or not.
6a488035 3548 *
e7483cbe 3549 * @return bool
a6c01b45 3550 * true if profile should be shown else false
6a488035 3551 */
00be9182 3552 public static function showOverlayProfile() {
6a488035
TO
3553 $showOverlay = TRUE;
3554
3555 // get the id of overlay profile
3556 $overlayProfileId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', 'summary_overlay', 'id', 'name');
3557 $query = "SELECT count(id) FROM civicrm_uf_field WHERE uf_group_id = {$overlayProfileId} AND visibility IN ('Public Pages', 'Public Pages and Listings') ";
3558
3559 $count = CRM_Core_DAO::singleValueQuery($query);
3560
3561 //check if there are no public fields and use is anonymous
3562 $session = CRM_Core_Session::singleton();
3563 if (!$count && !$session->get('userID')) {
3564 $showOverlay = FALSE;
3565 }
3566
3567 return $showOverlay;
3568 }
3569
3570 /**
fe482240 3571 * Get group type values of the profile.
6a488035 3572 *
c490a46a
CW
3573 * @param int $profileId
3574 * @param string $groupType
1fd63589 3575 *
e7483cbe 3576 * @return array
a6c01b45 3577 * group type values
6a488035 3578 */
00be9182 3579 public static function groupTypeValues($profileId, $groupType = NULL) {
6a488035
TO
3580 $groupTypeValue = array();
3581 $groupTypes = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $profileId, 'group_type');
3582
3583 $groupTypeParts = explode(CRM_Core_DAO::VALUE_SEPARATOR, $groupTypes);
a7488080 3584 if (empty($groupTypeParts[1])) {
6a488035
TO
3585 return $groupTypeValue;
3586 }
3587 $participantExtends = array('ParticipantRole', 'ParticipantEventName', 'ParticipantEventType');
3588
3589 foreach (explode(',', $groupTypeParts[1]) as $groupTypeValues) {
3590 $values = array();
3591 $valueParts = explode(':', $groupTypeValues);
3592 if ($groupType &&
3593 ($valueParts[0] != "{$groupType}Type" ||
3594 ($groupType == 'Participant' &&
3595 !in_array($valueParts[0], $participantExtends)
3596 )
3597 )
3598 ) {
3599 continue;
3600 }
3601 foreach ($valueParts as $val) {
3602 if (CRM_Utils_Rule::integer($val)) {
3603 $values[$val] = $val;
3604 }
3605 }
3606 if (!empty($values)) {
3607 $typeName = substr($valueParts[0], 0, -4);
3608 if (in_array($valueParts[0], $participantExtends)) {
3609 $typeName = $valueParts[0];
3610 }
3611 $groupTypeValue[$typeName] = $values;
3612 }
3613 }
3614
3615 return $groupTypeValue;
3616 }
3617
b5c2afd0
EM
3618 /**
3619 * @return bool|object
3620 */
00be9182 3621 public static function isProfileDoubleOptin() {
6a488035
TO
3622 // check for double optin
3623 $config = CRM_Core_Config::singleton();
3624 if (in_array('CiviMail', $config->enableComponents)) {
aaffa79f 3625 return Civi::settings()->get('profile_double_optin');
6a488035
TO
3626 }
3627 return FALSE;
3628 }
3629
b5c2afd0
EM
3630 /**
3631 * @return bool|object
3632 */
00be9182 3633 public static function isProfileAddToGroupDoubleOptin() {
6a488035
TO
3634 // check for add to group double optin
3635 $config = CRM_Core_Config::singleton();
3636 if (in_array('CiviMail', $config->enableComponents)) {
aaffa79f 3637 return Civi::settings()->get('profile_add_to_group_double_optin');
6a488035
TO
3638 }
3639 return FALSE;
3640 }
3641
3642 /**
fe482240 3643 * Get profiles used for batch entry.
6a488035 3644 *
a6c01b45
CW
3645 * @return array
3646 * profileIds profile ids
6a488035 3647 */
00be9182 3648 public static function getBatchProfiles() {
6a488035
TO
3649 $query = "SELECT id
3650 FROM civicrm_uf_group
3651 WHERE name IN ('contribution_batch_entry', 'membership_batch_entry')";
5d6ac993 3652 $dao = CRM_Core_DAO::executeQuery($query);
6a488035 3653 $profileIds = array();
5d6ac993 3654 while ($dao->fetch()) {
6a488035
TO
3655 $profileIds[$dao->id] = $dao->id;
3656 }
3657 return $profileIds;
3658 }
3659
1fd63589
EM
3660 /**
3661 * @todo what do I do?
3662 * @param $source
3663 * @param $destination
3664 * @param bool $returnMultiSummaryFields
3665 *
3666 * @return array|null
3667 */
00be9182 3668 public static function shiftMultiRecordFields(&$source, &$destination, $returnMultiSummaryFields = FALSE) {
5d6ac993 3669 $multiSummaryFields = $returnMultiSummaryFields ? array() : NULL;
6a488035
TO
3670 foreach ($source as $field => $properties) {
3671 if (!CRM_Core_BAO_CustomField::getKeyID($field)) {
3672 continue;
3673 }
3674 if (CRM_Core_BAO_CustomField::isMultiRecordField($field)) {
3675 $destination[$field] = $properties;
3676 if ($returnMultiSummaryFields) {
3677 if ($properties['is_multi_summary']) {
3678 $multiSummaryFields[$field] = $properties;
3679 }
3680 }
3681 unset($source[$field]);
3682 }
3683 }
3684 return $multiSummaryFields;
3685 }
3686
3687 /**
fe482240 3688 * This is function is used to format pseudo fields.
6a488035 3689 *
6a0b768e
TO
3690 * @param array $fields
3691 * Associated array of profile fields.
6a488035 3692 *
6a488035 3693 */
00be9182 3694 public static function reformatProfileFields(&$fields) {
6a488035
TO
3695 //reformat fields array
3696 foreach ($fields as $name => $field) {
3697 //reformat phone and extension field
5d6ac993 3698 if (substr($field['name'], 0, 13) == 'phone_and_ext') {
6a488035
TO
3699 $fieldSuffix = str_replace('phone_and_ext-', '', $field['name']);
3700
3701 // retain existing element properties and just update and replace key
3702 CRM_Utils_Array::crmReplaceKey($fields, $name, "phone-{$fieldSuffix}");
3703 $fields["phone-{$fieldSuffix}"]['name'] = "phone-{$fieldSuffix}";
3704 $fields["phone-{$fieldSuffix}"]['where'] = 'civicrm_phone.phone';
3705
3706 // add additional phone extension field
3707 $fields["phone_ext-{$fieldSuffix}"] = $field;
5d6ac993 3708 $fields["phone_ext-{$fieldSuffix}"]['title'] = $field['title'] . ' - ' . ts('Ext.');
6a488035
TO
3709 $fields["phone_ext-{$fieldSuffix}"]['name'] = "phone_ext-{$fieldSuffix}";
3710 $fields["phone_ext-{$fieldSuffix}"]['where'] = 'civicrm_phone.phone_ext';
3711 $fields["phone_ext-{$fieldSuffix}"]['skipDisplay'] = 1;
3712 //ignore required for extension field
3713 $fields["phone_ext-{$fieldSuffix}"]['is_required'] = 0;
3714 }
3715 }
3716 }
96025800 3717
6a488035 3718}