3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class contains function for UFField.
21 class CRM_Core_BAO_UFField
extends CRM_Core_DAO_UFField
{
27 private static $_contriBatchEntryFields = NULL;
28 private static $_memberBatchEntryFields = NULL;
31 * Create UFField object.
33 * @param array $params
34 * Array per getfields metadata.
36 * @return \CRM_Core_BAO_UFField
37 * @throws \API_Exception
39 public static function create($params) {
40 $id = $params['id'] ??
NULL;
42 $op = empty($id) ?
'create' : 'edit';
43 CRM_Utils_Hook
::pre('UFField', $op, $id, $params);
44 // Merge in data from existing field
46 $UFField = new CRM_Core_BAO_UFField();
47 $UFField->id
= $params['id'];
48 if ($UFField->find(TRUE)) {
49 $defaults = $UFField->toArray();
50 // This will be calculated based on field name
51 unset($defaults['field_type']);
55 throw new API_Exception("UFFIeld id {$params['id']} not found.");
59 // Validate field_name
60 if (strpos($params['field_name'], 'formatting') !== 0 && !CRM_Core_BAO_UFField
::isValidFieldName($params['field_name'])) {
61 throw new API_Exception('The field_name is not valid');
64 // Supply default label if not set
65 if (empty($id) && !isset($params['label'])) {
66 $params['label'] = self
::getAvailableFieldTitles()[$params['field_name']];
69 // Supply field_type if not set
70 if (empty($params['field_type']) && strpos($params['field_name'], 'formatting') !== 0) {
71 $params['field_type'] = CRM_Utils_Array
::pathGet(self
::getAvailableFieldsFlat(), [$params['field_name'], 'field_type']);
73 elseif (empty($params['field_type'])) {
74 $params['field_type'] = 'Formatting';
77 // Generate unique name for formatting fields
78 if ($params['field_name'] === 'formatting') {
79 $params['field_name'] = 'formatting_' . substr(uniqid(), -4);
82 if (self
::duplicateField($params)) {
83 throw new API_Exception("The field was not added. It already exists in this profile.");
86 //@todo why is this even optional? Surely weight should just be 'managed' ??
87 if (CRM_Utils_Array
::value('option.autoweight', $params, TRUE)) {
88 $params['weight'] = CRM_Core_BAO_UFField
::autoWeight($params);
91 // Set values for uf field properties and save
92 $ufField = new CRM_Core_DAO_UFField();
93 $ufField->copyValues($params);
95 if ($params['field_name'] == 'url') {
96 $ufField->location_type_id
= 'null';
99 $ufField->website_type_id
= 'null';
101 if (!strstr($params['field_name'], 'phone')) {
102 $ufField->phone_type_id
= 'null';
107 $fieldsType = CRM_Core_BAO_UFGroup
::calculateGroupType($ufField->uf_group_id
, TRUE);
108 CRM_Core_BAO_UFGroup
::updateGroupTypes($ufField->uf_group_id
, $fieldsType);
110 CRM_Utils_Hook
::post('UFField', $op, $ufField->id
, $ufField);
112 civicrm_api3('profile', 'getfields', ['cache_clear' => TRUE]);
117 * Fetch object based on array of properties.
119 * @param array $params
120 * (reference ) an assoc array of name/value pairs.
121 * @param array $defaults
122 * (reference ) an assoc array to hold the flattened values.
124 * @return CRM_Core_BAO_UFField
126 public static function retrieve(&$params, &$defaults) {
127 return CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_UFField', $params, $defaults);
131 * Update the is_active flag in the db.
134 * Id of the database record.
135 * @param bool $is_active
136 * Value we want to set the is_active field.
139 * true if we found and updated the object, else false
141 public static function setIsActive($id, $is_active) {
142 //check if custom data profile field is disabled
144 if (CRM_Core_BAO_UFField
::checkUFStatus($id)) {
145 return CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_UFField', $id, 'is_active', $is_active);
148 CRM_Core_Session
::setStatus(ts('Cannot enable this UF field since the used custom field is disabled.'), ts('Check Custom Field'), 'error');
152 return CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_UFField', $id, 'is_active', $is_active);
157 * Delete the profile Field.
163 public static function del($id) {
164 return (bool) self
::deleteRecord(['id' => $id]);
168 * Check duplicate for duplicate field in a group.
170 * @param array $params
171 * An associative array with field and values.
175 public static function duplicateField($params) {
176 $ufField = new CRM_Core_DAO_UFField();
177 $ufField->uf_group_id
= $params['uf_group_id'] ??
NULL;
178 $ufField->field_type
= $params['field_type'] ??
NULL;
179 $ufField->field_name
= $params['field_name'] ??
NULL;
180 $ufField->website_type_id
= $params['website_type_id'] ??
NULL;
181 if (is_null(CRM_Utils_Array
::value('location_type_id', $params, ''))) {
182 // primary location type have NULL value in DB
183 $ufField->whereAdd("location_type_id IS NULL");
186 $ufField->location_type_id
= $params['location_type_id'] ??
NULL;
188 $ufField->phone_type_id
= $params['phone_type_id'] ??
NULL;
190 if (!empty($params['id'])) {
191 $ufField->whereAdd("id <> " . $params['id']);
194 return (bool) $ufField->find(TRUE);
198 * Does profile consists of a multi-record custom field.
204 public static function checkMultiRecordFieldExists($gId) {
205 $queryString = "SELECT f.field_name
206 FROM civicrm_uf_field f, civicrm_uf_group g
207 WHERE f.uf_group_id = g.id
208 AND g.id = %1 AND f.field_name LIKE 'custom%'";
209 $p = [1 => [$gId, 'Integer']];
210 $dao = CRM_Core_DAO
::executeQuery($queryString, $p);
211 $customFieldIds = [];
212 $isMultiRecordFieldPresent = FALSE;
213 while ($dao->fetch()) {
214 if ($customId = CRM_Core_BAO_CustomField
::getKeyID($dao->field_name
)) {
215 if (is_numeric($customId)) {
216 $customFieldIds[] = $customId;
221 if (!empty($customFieldIds) && count($customFieldIds) == 1) {
222 $customFieldId = array_pop($customFieldIds);
223 $isMultiRecordFieldPresent = CRM_Core_BAO_CustomField
::isMultiRecordField($customFieldId);
225 elseif (count($customFieldIds) > 1) {
226 $customFieldIds = implode(", ", $customFieldIds);
229 FROM civicrm_custom_group cg
230 INNER JOIN civicrm_custom_field cf
231 ON cg.id = cf.custom_group_id
232 WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1";
234 $dao = CRM_Core_DAO
::executeQuery($queryString);
236 $isMultiRecordFieldPresent = ($dao->cgId
) ?
$dao->cgId
: FALSE;
240 return $isMultiRecordFieldPresent;
244 * Automatically determine one weight and modify others.
246 * @param array $params
247 * UFField record, e.g. with 'weight', 'uf_group_id', and 'field_id'.
250 public static function autoWeight($params) {
254 if (!empty($params['field_id']) ||
!empty($params['id'])) {
255 $oldWeight = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFField', !empty($params['id']) ?
$params['id'] : $params['field_id'], 'weight', 'id');
257 $fieldValues = ['uf_group_id' => !empty($params['uf_group_id']) ?
$params['uf_group_id'] : $params['group_id']];
258 return CRM_Utils_Weight
::updateOtherWeights('CRM_Core_DAO_UFField', $oldWeight, CRM_Utils_Array
::value('weight', $params, 0), $fieldValues);
262 * Enable/disable profile field given a custom field id
264 * @param int $customFieldId
266 * @param bool $is_active
267 * Set the is_active field.
269 public static function setUFField($customFieldId, $is_active) {
270 // Find the profile id given custom field.
271 $ufField = new CRM_Core_DAO_UFField();
272 $ufField->field_name
= "custom_" . $customFieldId;
275 while ($ufField->fetch()) {
276 // Enable/ disable profile.
277 CRM_Core_BAO_UFField
::setIsActive($ufField->id
, $is_active);
282 * Copy existing profile fields to
283 * new profile from the already built profile
286 * From which we need to copy.
287 * @param bool $new_id
290 public static function copy($old_id, $new_id) {
291 $ufField = new CRM_Core_DAO_UFField();
292 $ufField->uf_group_id
= $old_id;
294 while ($ufField->fetch()) {
295 //copy the field records as it is on new ufgroup id
296 $ufField->uf_group_id
= $new_id;
303 * Delete profile field given a custom field.
305 * @param int $customFieldId
306 * ID of the custom field to be deleted.
308 public static function delUFField($customFieldId) {
309 //find the profile id given custom field id
310 $ufField = new CRM_Core_DAO_UFField();
311 $ufField->field_name
= "custom_" . $customFieldId;
314 while ($ufField->fetch()) {
315 //enable/ disable profile
316 CRM_Core_BAO_UFField
::del($ufField->id
);
321 * Enable/disable profile field given a custom group id
323 * @param int $customGroupId
325 * @param bool $is_active
326 * Value we want to set the is_active field.
328 public static function setUFFieldStatus($customGroupId, $is_active) {
329 //find the profile id given custom group id
330 $queryString = "SELECT civicrm_custom_field.id as custom_field_id
331 FROM civicrm_custom_field, civicrm_custom_group
332 WHERE civicrm_custom_field.custom_group_id = civicrm_custom_group.id
333 AND civicrm_custom_group.id = %1";
334 $p = [1 => [$customGroupId, 'Integer']];
335 $dao = CRM_Core_DAO
::executeQuery($queryString, $p);
337 while ($dao->fetch()) {
338 // Enable/ disable profile.
339 CRM_Core_BAO_UFField
::setUFField($dao->custom_field_id
, $is_active);
344 * Check the status of custom field used in uf fields.
346 * @param int $UFFieldId
349 * false if custom field are disabled else true
351 public static function checkUFStatus($UFFieldId) {
352 $fieldName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFField', $UFFieldId, 'field_name');
353 // return if field is not a custom field
354 if (!$customFieldId = CRM_Core_BAO_CustomField
::getKeyID($fieldName)) {
358 $customField = new CRM_Core_DAO_CustomField();
359 $customField->id
= $customFieldId;
360 // if uf field is custom field
361 if ($customField->find(TRUE)) {
362 if (!$customField->is_active
) {
372 * Find out whether given profile group using Activity
373 * Profile fields with contact fields
375 * @param int $ufGroupId
379 public static function checkContactActivityProfileType($ufGroupId) {
380 $ufGroup = new CRM_Core_DAO_UFGroup();
381 $ufGroup->id
= $ufGroupId;
382 $ufGroup->find(TRUE);
384 return self
::checkContactActivityProfileTypeByGroupType($ufGroup->group_type
);
389 * @param $ufGroupType
392 public static function checkContactActivityProfileTypeByGroupType($ufGroupType) {
395 $typeParts = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $ufGroupType);
396 $profileTypes = explode(',', $typeParts[0]);
399 if (empty($profileTypes)) {
402 $components = ['Contribution', 'Participant', 'Membership'];
403 if (!in_array('Activity', $profileTypes)) {
406 elseif (count($profileTypes) == 1) {
410 if ($index = array_search('Contact', $profileTypes)) {
411 unset($profileTypes[$index]);
412 if (count($profileTypes) == 1) {
417 $contactTypes = ['Individual', 'Household', 'Organization'];
418 $subTypes = CRM_Contact_BAO_ContactType
::subTypes();
420 $profileTypeComponent = array_intersect($components, $profileTypes);
421 if (!empty($profileTypeComponent) ||
422 count(array_intersect($contactTypes, $profileTypes)) > 1 ||
423 count(array_intersect($subTypes, $profileTypes)) > 1
432 * Find out whether given profile group uses $required
433 * and/or $optional profile types
435 * @param int $ufGroupId
437 * @param array $required
438 * Array of types those are required.
439 * @param array $optional
440 * Array of types those are optional.
444 public static function checkValidProfileType($ufGroupId, $required, $optional = NULL) {
445 if (!is_array($required) ||
empty($required)) {
449 $ufGroup = new CRM_Core_DAO_UFGroup();
450 $ufGroup->id
= $ufGroupId;
451 $ufGroup->find(TRUE);
454 if ($ufGroup->group_type
) {
455 $typeParts = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $ufGroup->group_type
);
456 $profileTypes = explode(',', $typeParts[0]);
459 if (empty($profileTypes)) {
464 foreach ($required as $key => $val) {
465 if (!in_array($val, $profileTypes)) {
471 if ($valid && is_array($optional)) {
472 foreach ($optional as $key => $val) {
473 if (in_array($val, $profileTypes)) {
484 * Check for mix profile fields (eg: individual + other contact types)
486 * @param int $ufGroupId
489 * true for mix profile else false
491 public static function checkProfileType($ufGroupId) {
492 $ufGroup = new CRM_Core_DAO_UFGroup();
493 $ufGroup->id
= $ufGroupId;
494 $ufGroup->find(TRUE);
497 if ($ufGroup->group_type
) {
498 $typeParts = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $ufGroup->group_type
);
499 $profileTypes = explode(',', $typeParts[0]);
502 //early return if new profile.
503 if (empty($profileTypes)) {
507 //we need to unset Contact
508 if (count($profileTypes) > 1) {
509 $index = array_search('Contact', $profileTypes);
510 if ($index !== FALSE) {
511 unset($profileTypes[$index]);
515 // suppress any subtypes if present
516 CRM_Contact_BAO_ContactType
::suppressSubTypes($profileTypes);
518 $contactTypes = ['Contact', 'Individual', 'Household', 'Organization'];
519 $components = ['Contribution', 'Participant', 'Membership', 'Activity'];
522 // check for mix profile condition
523 if (count($profileTypes) > 1) {
524 //check the there are any components include in profile
525 foreach ($components as $value) {
526 if (in_array($value, $profileTypes)) {
530 //check if there are more than one contact types included in profile
531 if (count($profileTypes) > 1) {
535 elseif (count($profileTypes) == 1) {
536 // note for subtype case count would be zero
537 $profileTypes = array_values($profileTypes);
538 if (!in_array($profileTypes[0], $contactTypes)) {
547 * Get the profile type (eg: individual/organization/household)
549 * @param int $ufGroupId
551 * @param bool $returnMixType
552 * This is true, then field type of mix profile field is returned.
553 * @param bool $onlyPure
554 * True if only pure profiles are required.
556 * @param bool $skipComponentType
562 public static function getProfileType($ufGroupId, $returnMixType = TRUE, $onlyPure = FALSE, $skipComponentType = FALSE) {
563 $ufGroup = new CRM_Core_DAO_UFGroup();
564 $ufGroup->id
= $ufGroupId;
565 $ufGroup->is_active
= 1;
567 $ufGroup->find(TRUE);
568 return self
::calculateProfileType($ufGroup->group_type
, $returnMixType, $onlyPure, $skipComponentType);
572 * Get the profile type (eg: individual/organization/household)
574 * @param string $ufGroupType
575 * @param bool $returnMixType
576 * This is true, then field type of mix profile field is returned.
577 * @param bool $onlyPure
578 * True if only pure profiles are required.
579 * @param bool $skipComponentType
581 * @return string profile group_type
584 public static function calculateProfileType($ufGroupType, $returnMixType = TRUE, $onlyPure = FALSE, $skipComponentType = FALSE) {
586 $contactTypes = ['Contact', 'Individual', 'Household', 'Organization'];
587 $subTypes = CRM_Contact_BAO_ContactType
::subTypes();
588 $components = ['Contribution', 'Participant', 'Membership', 'Activity'];
592 $typeParts = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $ufGroupType);
593 $profileTypes = explode(',', $typeParts[0]);
597 if (count($profileTypes) == 1) {
598 return $profileTypes[0];
605 //we need to unset Contact
606 if (count($profileTypes) > 1) {
607 $index = array_search('Contact', $profileTypes);
608 if ($index !== FALSE) {
609 unset($profileTypes[$index]);
613 $profileType = $mixProfileType = NULL;
615 // this case handles pure profile
616 if (count($profileTypes) == 1) {
617 $profileType = array_pop($profileTypes);
620 //check the there are any components include in profile
621 $componentCount = [];
622 foreach ($components as $value) {
623 if (in_array($value, $profileTypes)) {
624 $componentCount[] = $value;
628 //check contact type included in profile
629 $contactTypeCount = [];
630 foreach ($contactTypes as $value) {
631 if (in_array($value, $profileTypes)) {
632 $contactTypeCount[] = $value;
637 foreach ($subTypes as $value) {
638 if (in_array($value, $profileTypes)) {
639 $subTypeCount[] = $value;
642 if (!$skipComponentType && count($componentCount) == 1) {
643 $profileType = $componentCount[0];
645 elseif (count($componentCount) > 1) {
646 $mixProfileType = $componentCount[1];
648 elseif (count($subTypeCount) == 1) {
649 $profileType = $subTypeCount[0];
651 elseif (count($contactTypeCount) == 1) {
652 $profileType = $contactTypeCount[0];
654 elseif (count($subTypeCount) > 1) {
655 // this is mix subtype profiles
656 $mixProfileType = $subTypeCount[1];
658 elseif (count($contactTypeCount) > 1) {
659 // this is mix contact profiles
660 $mixProfileType = $contactTypeCount[1];
664 if ($mixProfileType) {
665 if ($returnMixType) {
666 return $mixProfileType;
678 * Check for mix profiles groups (eg: individual + other contact types)
683 * true for mix profile group else false
685 public static function checkProfileGroupType($ctype) {
686 $ufGroup = new CRM_Core_DAO_UFGroup();
690 FROM civicrm_uf_group as ufg, civicrm_uf_join as ufj
691 WHERE ufg.id = ufj.uf_group_id
692 AND ufj.module = 'User Registration'
693 AND ufg.is_active = 1 ";
695 $ufGroup = CRM_Core_DAO
::executeQuery($query);
698 $validProfiles = ['Individual', 'Organization', 'Household', 'Contribution'];
699 while ($ufGroup->fetch()) {
700 $profileType = self
::getProfileType($ufGroup->id
);
701 if (in_array($profileType, $validProfiles)) {
704 elseif ($profileType) {
713 * Check for searchable or in selector field for given profile.
715 * @param int $profileID
719 public static function checkSearchableORInSelector($profileID) {
727 From civicrm_uf_field
728 WHERE (in_selector = 1 OR is_searchable = 1)
729 AND uf_group_id = {$profileID}";
731 $ufFields = CRM_Core_DAO
::executeQuery($query);
732 while ($ufFields->fetch()) {
741 * Reset In selector and is searchable values for given $profileID.
743 * @param int $profileID
745 public function resetInSelectorANDSearchable($profileID) {
749 $query = "UPDATE civicrm_uf_field SET in_selector = 0, is_searchable = 0 WHERE uf_group_id = {$profileID}";
750 CRM_Core_DAO
::executeQuery($query);
754 * Add fields to $profileAddressFields as appropriate.
755 * profileAddressFields is assigned to the template to tell it
756 * what fields are in the profile address
757 * that potentially should be copied to the Billing fields
758 * we want to give precedence to
760 * 2) then Primary designated as 'Primary
761 * 3) location_type is primary
762 * 4) if none of these apply then it just uses the first one
764 * as this will be used to
765 * transfer profile address data to billing fields
766 * http://issues.civicrm.org/jira/browse/CRM-5869
769 * Field key - e.g. street_address-Primary, first_name.
770 * @param array $profileAddressFields
771 * Array of profile fields that relate to address fields.
772 * @param array $profileFilter
773 * Filter to apply to profile fields - expected usage is to only fill based on.
774 * the bottom profile per CRM-13726
777 * Can the address block be hidden safe in the knowledge all fields are elsewhere collected (see CRM-15118)
779 public static function assignAddressField($key, &$profileAddressFields, $profileFilter) {
780 $billing_id = CRM_Core_BAO_LocationType
::getBilling();
781 list($prefixName, $index) = CRM_Utils_System
::explode('-', $key, 2);
783 $profileFields = civicrm_api3('uf_field', 'get', array_merge($profileFilter,
786 'return' => 'field_name, is_required',
792 //check for valid fields ( fields that are present in billing block )
793 $validBillingFields = [
798 'supplemental_address_1',
804 $requiredBillingFields = array_diff($validBillingFields, ['middle_name', 'supplemental_address_1']);
805 $validProfileFields = [];
806 $requiredProfileFields = [];
808 foreach ($profileFields['values'] as $field) {
809 if (in_array($field['field_name'], $validBillingFields)) {
810 $validProfileFields[] = $field['field_name'];
812 if (!empty($field['is_required'])) {
813 $requiredProfileFields[] = $field['field_name'];
817 if (!in_array($prefixName, $validProfileFields)) {
821 if (!empty($index) && (
822 // it's empty so we set it OR
823 !CRM_Utils_Array
::value($prefixName, $profileAddressFields)
824 //we are dealing with billing id (precedence)
825 ||
$index == $billing_id
826 // we are dealing with primary & billing not set
827 ||
($index == 'Primary' && $profileAddressFields[$prefixName] != $billing_id)
828 ||
($index == CRM_Core_BAO_LocationType
::getDefault()->id
829 && $profileAddressFields[$prefixName] != $billing_id
830 && $profileAddressFields[$prefixName] != 'Primary'
834 $profileAddressFields[$prefixName] = $index;
837 $potentiallyMissingRequiredFields = array_diff($requiredBillingFields, $requiredProfileFields);
838 CRM_Core_Resources
::singleton()
839 ->addSetting(['billing' => ['billingProfileIsHideable' => empty($potentiallyMissingRequiredFields)]]);
843 * Get a list of fields which can be added to profiles.
845 * @param int $gid : UF group ID
846 * @param array $defaults : Form defaults
847 * @return array, multidimensional; e.g. $result['FieldGroup']['field_name']['label']
849 public static function getAvailableFields($gid = NULL, $defaults = []) {
852 'Individual' => CRM_Contact_BAO_Contact
::importableFields('Individual', FALSE, FALSE, TRUE, TRUE, TRUE),
853 'Household' => CRM_Contact_BAO_Contact
::importableFields('Household', FALSE, FALSE, TRUE, TRUE, TRUE),
854 'Organization' => CRM_Contact_BAO_Contact
::importableFields('Organization', FALSE, FALSE, TRUE, TRUE, TRUE),
857 // include hook injected fields
858 $fields['Contact'] = array_merge($fields['Contact'], CRM_Contact_BAO_Query_Hook
::singleton()->getFields());
860 // add current employer for individuals
861 $fields['Individual']['current_employer'] = [
862 'name' => 'organization_name',
863 'title' => ts('Current Employer'),
866 $addressOptions = CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
867 'address_options', TRUE, NULL, TRUE
870 if (empty($addressOptions['county'])) {
871 unset($fields['Individual']['county'], $fields['Household']['county'], $fields['Organization']['county']);
874 // break out common contact fields array CRM-3037.
875 // from a UI perspective this makes very little sense
876 foreach ($fields['Individual'] as $key => $value) {
877 if (!empty($fields['Household'][$key]) && !empty($fields['Organization'][$key])) {
878 $fields['Contact'][$key] = $value;
879 unset($fields['Individual'][$key], $fields['Household'][$key], $fields['Organization'][$key]);
883 // Internal field not exposed to forms
884 unset($fields['Contact']['contact_type']);
885 unset($fields['Contact']['master_id']);
887 // convert phone extension in to psedo-field phone + phone extension
889 unset($fields['Contact']['phone_ext']);
891 $fields['Contact']['phone_and_ext'] = [
892 'name' => 'phone_and_ext',
893 'title' => ts('Phone and Extension'),
894 'hasLocationType' => 1,
897 // include Subtypes For Profile
898 $subTypes = CRM_Contact_BAO_ContactType
::subTypeInfo();
899 foreach ($subTypes as $name => $val) {
900 //custom fields for sub type
901 $subTypeFields = CRM_Core_BAO_CustomField
::getFieldsForImport($name, FALSE, FALSE, FALSE, TRUE, TRUE);
902 if (array_key_exists($val['parent'], $fields)) {
903 $fields[$name] = $fields[$val['parent']] +
$subTypeFields;
906 $fields[$name] = $subTypeFields;
910 if (CRM_Core_Permission
::access('CiviContribute')) {
911 $contribFields = CRM_Contribute_BAO_Contribution
::getContributionFields(FALSE);
912 if (!empty($contribFields)) {
913 unset($contribFields['is_test']);
914 unset($contribFields['is_pay_later']);
915 unset($contribFields['contribution_id']);
916 $contribFields['contribution_note'] = [
917 'name' => 'contribution_note',
918 'title' => ts('Contribution Note'),
920 $fields['Contribution'] = array_merge($contribFields, self
::getContribBatchEntryFields());
924 if (CRM_Core_Permission
::access('CiviEvent')) {
925 $participantFields = CRM_Event_BAO_Query
::getParticipantFields();
926 if ($participantFields) {
927 // Remove fields not supported by profiles
928 CRM_Utils_Array
::remove($participantFields,
929 'external_identifier',
931 'participant_contact_id',
932 'participant_role_id',
933 'participant_status_id',
934 'participant_is_test',
935 'participant_fee_level',
937 'participant_is_pay_later',
938 'participant_campaign'
940 if (isset($participantFields['participant_campaign_id'])) {
941 $participantFields['participant_campaign_id']['title'] = ts('Campaign');
943 $fields['Participant'] = $participantFields;
947 if (CRM_Core_Permission
::access('CiviMember')) {
948 $membershipFields = CRM_Member_BAO_Membership
::getMembershipFields();
949 // Remove fields not supported by profiles
950 CRM_Utils_Array
::remove($membershipFields,
952 'membership_type_id',
955 'member_is_override',
956 'status_override_end_date',
958 'member_is_pay_later'
960 if ($gid && CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $gid, 'name') == 'membership_batch_entry') {
961 $fields['Membership'] = array_merge($membershipFields, self
::getMemberBatchEntryFields());
964 $fields['Membership'] = $membershipFields;
968 if (CRM_Core_Permission
::access('CiviCase')) {
969 $caseFields = CRM_Case_BAO_Query
::getFields(TRUE);
970 $caseFields = array_merge($caseFields, CRM_Core_BAO_CustomField
::getFieldsForImport('Case'));
972 // Remove fields not supported by profiles
973 CRM_Utils_Array
::remove($caseFields,
980 $fields['Case'] = $caseFields;
983 $activityFields = CRM_Activity_BAO_Activity
::getProfileFields();
984 if ($activityFields) {
985 // campaign related fields.
986 if (isset($activityFields['activity_campaign_id'])) {
987 $activityFields['activity_campaign_id']['title'] = ts('Campaign');
989 $fields['Activity'] = $activityFields;
992 $fields['Formatting']['format_free_html_' . rand(1000, 9999)] = [
993 'name' => 'free_html',
996 'title' => 'Free HTML',
1000 foreach ($fields as &$values) {
1001 $values = CRM_Utils_Array
::crmArraySortByField($values, 'title');
1004 //group selected and unwanted fields list
1005 $ufFields = $gid ? CRM_Core_BAO_UFGroup
::getFields($gid, FALSE, NULL, NULL, NULL, TRUE, NULL, TRUE) : [];
1006 $groupFieldList = array_merge($ufFields, [
1008 'email_greeting_custom',
1009 'postal_greeting_custom',
1013 //unset selected fields
1014 foreach ($groupFieldList as $key => $value) {
1016 unset($fields['Individual'][$value], $fields['Household'][$value], $fields['Organization'][$value]);
1019 if (!empty($defaults['field_name'])
1020 && $defaults['field_name']['0'] == $value['field_type']
1021 && $defaults['field_name']['1'] == $key
1025 unset($fields[$value['field_type']][$key]);
1028 // Allow extensions to alter the array of entity => fields permissible in a CiviCRM Profile.
1029 CRM_Utils_Hook
::alterUFFields($fields);
1034 * Get a list of fields which can be added to profiles.
1036 * @param bool $force
1039 * e.g. $result['field_name']['label']
1041 public static function getAvailableFieldsFlat($force = FALSE) {
1042 if (!isset(Civi
::$statics['UFFieldsFlat']) ||
$force) {
1043 Civi
::$statics['UFFieldsFlat'] = [];
1044 foreach (self
::getAvailableFields() as $fieldType => $fields) {
1045 foreach ($fields as $fieldName => $field) {
1046 if (!isset(Civi
::$statics['UFFieldsFlat'][$fieldName])) {
1047 $field['field_type'] = $fieldType;
1048 Civi
::$statics['UFFieldsFlat'][$fieldName] = $field;
1053 return Civi
::$statics['UFFieldsFlat'];
1057 * Get a list of fields which can be added to profiles in the format [name => title]
1061 public static function getAvailableFieldTitles() {
1062 $fields = self
::getAvailableFieldsFlat();
1063 $fields['formatting'] = ['title' => ts('Formatting')];
1064 return CRM_Utils_Array
::collect('title', $fields);
1068 * Determine whether the given field_name is valid.
1070 * @param string $fieldName
1073 public static function isValidFieldName($fieldName) {
1074 $availableFields = CRM_Core_BAO_UFField
::getAvailableFieldsFlat();
1075 return isset($availableFields[$fieldName]);
1079 * @return array|null
1081 public static function getContribBatchEntryFields() {
1082 if (self
::$_contriBatchEntryFields === NULL) {
1083 self
::$_contriBatchEntryFields = [
1085 'name' => 'send_receipt',
1086 'title' => ts('Send Receipt'),
1089 'name' => 'soft_credit',
1090 'title' => ts('Soft Credit'),
1092 'soft_credit_type' => [
1093 'name' => 'soft_credit_type',
1094 'title' => ts('Soft Credit Type'),
1097 'name' => 'product_name',
1098 'title' => ts('Premiums'),
1100 'contribution_note' => [
1101 'name' => 'contribution_note',
1102 'title' => ts('Contribution Note'),
1104 'contribution_soft_credit_pcp_id' => [
1105 'name' => 'contribution_soft_credit_pcp_id',
1106 'title' => ts('Personal Campaign Page'),
1110 return self
::$_contriBatchEntryFields;
1114 * @return array|null
1116 public static function getMemberBatchEntryFields() {
1117 if (self
::$_memberBatchEntryFields === NULL) {
1118 self
::$_memberBatchEntryFields = [
1120 'name' => 'send_receipt',
1121 'title' => ts('Send Receipt'),
1124 'name' => 'soft_credit',
1125 'title' => ts('Soft Credit'),
1128 'name' => 'product_name',
1129 'title' => ts('Premiums'),
1131 'financial_type' => [
1132 'name' => 'financial_type',
1133 'title' => ts('Financial Type'),
1136 'name' => 'total_amount',
1137 'title' => ts('Total Amount'),
1140 'name' => 'receive_date',
1141 'title' => ts('Date Received'),
1143 'payment_instrument' => [
1144 'name' => 'payment_instrument',
1145 'title' => ts('Payment Method'),
1147 'contribution_status_id' => [
1148 'name' => 'contribution_status_id',
1149 'title' => ts('Contribution Status'),
1152 'name' => 'contribution_trxn_id',
1153 'title' => ts('Contribution Transaction ID'),
1157 return self
::$_memberBatchEntryFields;