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