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