3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2017
35 * This class contains function for UFField.
37 class CRM_Core_BAO_UFField
extends CRM_Core_DAO_UFField
{
42 private static $_contriBatchEntryFields = NULL;
43 private static $_memberBatchEntryFields = NULL;
46 * Create UFField object.
48 * @param array $params
49 * Array per getfields metadata.
51 * @return \CRM_Core_BAO_UFField
52 * @throws \API_Exception
54 public static function create(&$params) {
55 // CRM-14756: kind of a hack-ish fix. If the user gives the id, uf_group_id is retrieved and then set.
56 if (isset($params['id'])) {
57 $groupId = civicrm_api3('UFField', 'getvalue', array(
58 'return' => 'uf_group_id',
59 'id' => $params['id'],
63 $groupId = CRM_Utils_Array
::value('uf_group_id', $params);
66 $field_name = CRM_Utils_Array
::value('field_name', $params);
68 if (strpos($field_name, 'formatting') !== 0 && !CRM_Core_BAO_UFField
::isValidFieldName($field_name)) {
69 throw new API_Exception('The field_name is not valid');
72 if (!(CRM_Utils_Array
::value('group_id', $params))) {
73 $params['group_id'] = $groupId;
76 $fieldId = CRM_Utils_Array
::value('id', $params);
77 if (!empty($fieldId)) {
78 $UFField = new CRM_Core_BAO_UFField();
79 $UFField->id
= $fieldId;
80 if ($UFField->find(TRUE)) {
81 if (!(CRM_Utils_Array
::value('group_id', $params))) {
82 // this copied here from previous api function - not sure if required
83 $params['group_id'] = $UFField->uf_group_id
;
87 throw new API_Exception("there is no field for this fieldId");
90 $params['uf_group_id'] = $params['group_id'];
92 if (CRM_Core_BAO_UFField
::duplicateField($params)) {
93 throw new API_Exception("The field was not added. It already exists in this profile.");
96 // @todo fix BAO to be less weird.
97 $field_type = CRM_Utils_Array
::value('field_type', $params);
98 $location_type_id = CRM_Utils_Array
::value('location_type_id', $params, CRM_Utils_Array
::value('website_type_id', $params));
99 $phone_type = CRM_Utils_Array
::value('phone_type_id', $params, CRM_Utils_Array
::value('phone_type', $params));
100 $params['field_name'] = array($field_type, $field_name, $location_type_id, $phone_type);
101 //@todo why is this even optional? Surely weight should just be 'managed' ??
102 if (CRM_Utils_Array
::value('option.autoweight', $params, TRUE)) {
103 $params['weight'] = CRM_Core_BAO_UFField
::autoWeight($params);
105 // set values for uf field properties and save
106 $ufField = new CRM_Core_DAO_UFField();
107 $ufField->copyValues($params);
108 $ufField->field_type
= $params['field_name'][0];
109 $ufField->field_name
= $params['field_name'][1];
111 //should not set location type id for Primary
112 $locationTypeId = NULL;
113 if ($params['field_name'][1] == 'url') {
114 $ufField->website_type_id
= CRM_Utils_Array
::value(2, $params['field_name']);
117 $locationTypeId = CRM_Utils_Array
::value(2, $params['field_name']);
118 $ufField->website_type_id
= NULL;
120 if ($locationTypeId) {
121 $ufField->location_type_id
= $locationTypeId;
124 $ufField->location_type_id
= 'null';
127 $ufField->phone_type_id
= CRM_Utils_Array
::value(3, $params['field_name'], 'NULL');
131 $fieldsType = CRM_Core_BAO_UFGroup
::calculateGroupType($groupId, TRUE);
132 CRM_Core_BAO_UFGroup
::updateGroupTypes($groupId, $fieldsType);
134 civicrm_api3('profile', 'getfields', array('cache_clear' => TRUE));
140 * Fetch object based on array of properties.
142 * @param array $params
143 * (reference ) an assoc array of name/value pairs.
144 * @param array $defaults
145 * (reference ) an assoc array to hold the flattened values.
147 * @return CRM_Core_BAO_UFField
149 public static function retrieve(&$params, &$defaults) {
150 return CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_UFField', $params, $defaults);
154 * Update the is_active flag in the db.
157 * Id of the database record.
158 * @param bool $is_active
159 * Value we want to set the is_active field.
162 * DAO object on success, null otherwise
164 public static function setIsActive($id, $is_active) {
165 //check if custom data profile field is disabled
167 if (CRM_Core_BAO_UFField
::checkUFStatus($id)) {
168 return CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_UFField', $id, 'is_active', $is_active);
171 CRM_Core_Session
::setStatus(ts('Cannot enable this UF field since the used custom field is disabled.'), ts('Check Custom Field'), 'error');
175 return CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_UFField', $id, 'is_active', $is_active);
180 * Delete the profile Field.
188 public static function del($id) {
190 $field = new CRM_Core_DAO_UFField();
197 * Check duplicate for duplicate field in a group.
199 * @param array $params
200 * An associative array with field and values.
204 public static function duplicateField($params) {
205 $ufField = new CRM_Core_DAO_UFField();
206 $ufField->uf_group_id
= CRM_Utils_Array
::value('uf_group_id', $params);
207 $ufField->field_type
= $params['field_type'];
208 $ufField->field_name
= $params['field_name'];
209 $ufField->website_type_id
= CRM_Utils_Array
::value('website_type_id', $params);
210 $ufField->location_type_id
= CRM_Utils_Array
::value('location_type_id', $params);
211 $ufField->phone_type_id
= CRM_Utils_Array
::value('phone_type_id', $params);;
213 if (!empty($params['id'])) {
214 $ufField->whereAdd("id <> " . $params['id']);
217 return ($ufField->find(TRUE) ?
1 : 0);
221 * Does profile consists of a multi-record custom field.
227 public static function checkMultiRecordFieldExists($gId) {
228 $queryString = "SELECT f.field_name
229 FROM civicrm_uf_field f, civicrm_uf_group g
230 WHERE f.uf_group_id = g.id
231 AND g.id = %1 AND f.field_name LIKE 'custom%'";
232 $p = array(1 => array($gId, 'Integer'));
233 $dao = CRM_Core_DAO
::executeQuery($queryString, $p);
234 $customFieldIds = array();
235 $isMultiRecordFieldPresent = FALSE;
236 while ($dao->fetch()) {
237 if ($customId = CRM_Core_BAO_CustomField
::getKeyID($dao->field_name
)) {
238 if (is_numeric($customId)) {
239 $customFieldIds[] = $customId;
244 if (!empty($customFieldIds) && count($customFieldIds) == 1) {
245 $customFieldId = array_pop($customFieldIds);
246 $isMultiRecordFieldPresent = CRM_Core_BAO_CustomField
::isMultiRecordField($customFieldId);
248 elseif (count($customFieldIds) > 1) {
249 $customFieldIds = implode(", ", $customFieldIds);
252 FROM civicrm_custom_group cg
253 INNER JOIN civicrm_custom_field cf
254 ON cg.id = cf.custom_group_id
255 WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1";
257 $dao = CRM_Core_DAO
::executeQuery($queryString);
259 $isMultiRecordFieldPresent = ($dao->cgId
) ?
$dao->cgId
: FALSE;
263 return $isMultiRecordFieldPresent;
267 * Automatically determine one weight and modify others.
269 * @param array $params
270 * UFField record, e.g. with 'weight', 'uf_group_id', and 'field_id'.
273 public static function autoWeight($params) {
277 if (!empty($params['field_id'])) {
278 $oldWeight = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFField', $params['field_id'], 'weight', 'id');
280 $fieldValues = array('uf_group_id' => $params['group_id']);
281 return CRM_Utils_Weight
::updateOtherWeights('CRM_Core_DAO_UFField', $oldWeight, CRM_Utils_Array
::value('weight', $params, 0), $fieldValues);
285 * Enable/disable profile field given a custom field id
287 * @param int $customFieldId
289 * @param bool $is_active
290 * Set the is_active field.
292 public static function setUFField($customFieldId, $is_active) {
293 // Find the profile id given custom field.
294 $ufField = new CRM_Core_DAO_UFField();
295 $ufField->field_name
= "custom_" . $customFieldId;
298 while ($ufField->fetch()) {
299 // Enable/ disable profile.
300 CRM_Core_BAO_UFField
::setIsActive($ufField->id
, $is_active);
305 * Copy existing profile fields to
306 * new profile from the already built profile
309 * From which we need to copy.
310 * @param bool $new_id
313 public static function copy($old_id, $new_id) {
314 $ufField = new CRM_Core_DAO_UFField();
315 $ufField->uf_group_id
= $old_id;
317 while ($ufField->fetch()) {
318 //copy the field records as it is on new ufgroup id
319 $ufField->uf_group_id
= $new_id;
326 * Delete profile field given a custom field.
328 * @param int $customFieldId
329 * ID of the custom field to be deleted.
331 public static function delUFField($customFieldId) {
332 //find the profile id given custom field id
333 $ufField = new CRM_Core_DAO_UFField();
334 $ufField->field_name
= "custom_" . $customFieldId;
337 while ($ufField->fetch()) {
338 //enable/ disable profile
339 CRM_Core_BAO_UFField
::del($ufField->id
);
344 * Enable/disable profile field given a custom group id
346 * @param int $customGroupId
348 * @param bool $is_active
349 * Value we want to set the is_active field.
351 public static function setUFFieldStatus($customGroupId, $is_active) {
352 //find the profile id given custom group id
353 $queryString = "SELECT civicrm_custom_field.id as custom_field_id
354 FROM civicrm_custom_field, civicrm_custom_group
355 WHERE civicrm_custom_field.custom_group_id = civicrm_custom_group.id
356 AND civicrm_custom_group.id = %1";
357 $p = array(1 => array($customGroupId, 'Integer'));
358 $dao = CRM_Core_DAO
::executeQuery($queryString, $p);
360 while ($dao->fetch()) {
361 // Enable/ disable profile.
362 CRM_Core_BAO_UFField
::setUFField($dao->custom_field_id
, $is_active);
367 * Check the status of custom field used in uf fields.
369 * @param int $UFFieldId
372 * false if custom field are disabled else true
374 public static function checkUFStatus($UFFieldId) {
375 $fieldName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFField', $UFFieldId, 'field_name');
376 // return if field is not a custom field
377 if (!$customFieldId = CRM_Core_BAO_CustomField
::getKeyID($fieldName)) {
381 $customField = new CRM_Core_DAO_CustomField();
382 $customField->id
= $customFieldId;
383 // if uf field is custom field
384 if ($customField->find(TRUE)) {
385 if (!$customField->is_active
) {
395 * Find out whether given profile group using Activity
396 * Profile fields with contact fields
398 * @param int $ufGroupId
402 public static function checkContactActivityProfileType($ufGroupId) {
403 $ufGroup = new CRM_Core_DAO_UFGroup();
404 $ufGroup->id
= $ufGroupId;
405 $ufGroup->find(TRUE);
407 return self
::checkContactActivityProfileTypeByGroupType($ufGroup->group_type
);
412 * @param $ufGroupType
415 public static function checkContactActivityProfileTypeByGroupType($ufGroupType) {
416 $profileTypes = array();
418 $typeParts = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $ufGroupType);
419 $profileTypes = explode(',', $typeParts[0]);
422 if (empty($profileTypes)) {
425 $components = array('Contribution', 'Participant', 'Membership');
426 if (!in_array('Activity', $profileTypes)) {
429 elseif (count($profileTypes) == 1) {
433 if ($index = array_search('Contact', $profileTypes)) {
434 unset($profileTypes[$index]);
435 if (count($profileTypes) == 1) {
440 $contactTypes = array('Individual', 'Household', 'Organization');
441 $subTypes = CRM_Contact_BAO_ContactType
::subTypes();
443 $profileTypeComponent = array_intersect($components, $profileTypes);
444 if (!empty($profileTypeComponent) ||
445 count(array_intersect($contactTypes, $profileTypes)) > 1 ||
446 count(array_intersect($subTypes, $profileTypes)) > 1
455 * Find out whether given profile group uses $required
456 * and/or $optional profile types
458 * @param int $ufGroupId
460 * @param array $required
461 * Array of types those are required.
462 * @param array $optional
463 * Array of types those are optional.
467 public static function checkValidProfileType($ufGroupId, $required, $optional = NULL) {
468 if (!is_array($required) ||
empty($required)) {
472 $ufGroup = new CRM_Core_DAO_UFGroup();
473 $ufGroup->id
= $ufGroupId;
474 $ufGroup->find(TRUE);
476 $profileTypes = array();
477 if ($ufGroup->group_type
) {
478 $typeParts = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $ufGroup->group_type
);
479 $profileTypes = explode(',', $typeParts[0]);
482 if (empty($profileTypes)) {
487 foreach ($required as $key => $val) {
488 if (!in_array($val, $profileTypes)) {
494 if ($valid && is_array($optional)) {
495 foreach ($optional as $key => $val) {
496 if (in_array($val, $profileTypes)) {
507 * Check for mix profile fields (eg: individual + other contact types)
509 * @param int $ufGroupId
512 * true for mix profile else false
514 public static function checkProfileType($ufGroupId) {
515 $ufGroup = new CRM_Core_DAO_UFGroup();
516 $ufGroup->id
= $ufGroupId;
517 $ufGroup->find(TRUE);
519 $profileTypes = array();
520 if ($ufGroup->group_type
) {
521 $typeParts = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $ufGroup->group_type
);
522 $profileTypes = explode(',', $typeParts[0]);
525 //early return if new profile.
526 if (empty($profileTypes)) {
530 //we need to unset Contact
531 if (count($profileTypes) > 1) {
532 $index = array_search('Contact', $profileTypes);
533 if ($index !== FALSE) {
534 unset($profileTypes[$index]);
538 // suppress any subtypes if present
539 CRM_Contact_BAO_ContactType
::suppressSubTypes($profileTypes);
541 $contactTypes = array('Contact', 'Individual', 'Household', 'Organization');
542 $components = array('Contribution', 'Participant', 'Membership', 'Activity');
545 // check for mix profile condition
546 if (count($profileTypes) > 1) {
547 //check the there are any components include in profile
548 foreach ($components as $value) {
549 if (in_array($value, $profileTypes)) {
553 //check if there are more than one contact types included in profile
554 if (count($profileTypes) > 1) {
558 elseif (count($profileTypes) == 1) {
559 // note for subtype case count would be zero
560 $profileTypes = array_values($profileTypes);
561 if (!in_array($profileTypes[0], $contactTypes)) {
570 * Get the profile type (eg: individual/organization/household)
572 * @param int $ufGroupId
574 * @param bool $returnMixType
575 * This is true, then field type of mix profile field is returned.
576 * @param bool $onlyPure
577 * True if only pure profiles are required.
579 * @param bool $skipComponentType
585 public static function getProfileType($ufGroupId, $returnMixType = TRUE, $onlyPure = FALSE, $skipComponentType = FALSE) {
586 $ufGroup = new CRM_Core_DAO_UFGroup();
587 $ufGroup->id
= $ufGroupId;
588 $ufGroup->is_active
= 1;
590 $ufGroup->find(TRUE);
591 return self
::calculateProfileType($ufGroup->group_type
, $returnMixType, $onlyPure, $skipComponentType);
595 * Get the profile type (eg: individual/organization/household)
597 * @param string $ufGroupType
598 * @param bool $returnMixType
599 * This is true, then field type of mix profile field is returned.
600 * @param bool $onlyPure
601 * True if only pure profiles are required.
602 * @param bool $skipComponentType
604 * @return string profile group_type
607 public static function calculateProfileType($ufGroupType, $returnMixType = TRUE, $onlyPure = FALSE, $skipComponentType = FALSE) {
609 $contactTypes = array('Contact', 'Individual', 'Household', 'Organization');
610 $subTypes = CRM_Contact_BAO_ContactType
::subTypes();
611 $components = array('Contribution', 'Participant', 'Membership', 'Activity');
613 $profileTypes = array();
615 $typeParts = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $ufGroupType);
616 $profileTypes = explode(',', $typeParts[0]);
620 if (count($profileTypes) == 1) {
621 return $profileTypes[0];
628 //we need to unset Contact
629 if (count($profileTypes) > 1) {
630 $index = array_search('Contact', $profileTypes);
631 if ($index !== FALSE) {
632 unset($profileTypes[$index]);
636 $profileType = $mixProfileType = NULL;
638 // this case handles pure profile
639 if (count($profileTypes) == 1) {
640 $profileType = array_pop($profileTypes);
643 //check the there are any components include in profile
644 $componentCount = array();
645 foreach ($components as $value) {
646 if (in_array($value, $profileTypes)) {
647 $componentCount[] = $value;
651 //check contact type included in profile
652 $contactTypeCount = array();
653 foreach ($contactTypes as $value) {
654 if (in_array($value, $profileTypes)) {
655 $contactTypeCount[] = $value;
659 $subTypeCount = array();
660 foreach ($subTypes as $value) {
661 if (in_array($value, $profileTypes)) {
662 $subTypeCount[] = $value;
665 if (!$skipComponentType && count($componentCount) == 1) {
666 $profileType = $componentCount[0];
668 elseif (count($componentCount) > 1) {
669 $mixProfileType = $componentCount[1];
671 elseif (count($subTypeCount) == 1) {
672 $profileType = $subTypeCount[0];
674 elseif (count($contactTypeCount) == 1) {
675 $profileType = $contactTypeCount[0];
677 elseif (count($subTypeCount) > 1) {
678 // this is mix subtype profiles
679 $mixProfileType = $subTypeCount[1];
681 elseif (count($contactTypeCount) > 1) {
682 // this is mix contact profiles
683 $mixProfileType = $contactTypeCount[1];
687 if ($mixProfileType) {
688 if ($returnMixType) {
689 return $mixProfileType;
701 * Check for mix profiles groups (eg: individual + other contact types)
706 * true for mix profile group else false
708 public static function checkProfileGroupType($ctype) {
709 $ufGroup = new CRM_Core_DAO_UFGroup();
713 FROM civicrm_uf_group as ufg, civicrm_uf_join as ufj
714 WHERE ufg.id = ufj.uf_group_id
715 AND ufj.module = 'User Registration'
716 AND ufg.is_active = 1 ";
718 $ufGroup = CRM_Core_DAO
::executeQuery($query);
721 $validProfiles = array('Individual', 'Organization', 'Household', 'Contribution');
722 while ($ufGroup->fetch()) {
723 $profileType = self
::getProfileType($ufGroup->id
);
724 if (in_array($profileType, $validProfiles)) {
727 elseif ($profileType) {
736 * Check for searchable or in selector field for given profile.
738 * @param int $profileID
742 public static function checkSearchableORInSelector($profileID) {
750 From civicrm_uf_field
751 WHERE (in_selector = 1 OR is_searchable = 1)
752 AND uf_group_id = {$profileID}";
754 $ufFields = CRM_Core_DAO
::executeQuery($query);
755 while ($ufFields->fetch()) {
764 * Reset In selector and is searchable values for given $profileID.
766 * @param int $profileID
768 public function resetInSelectorANDSearchable($profileID) {
772 $query = "UPDATE civicrm_uf_field SET in_selector = 0, is_searchable = 0 WHERE uf_group_id = {$profileID}";
773 CRM_Core_DAO
::executeQuery($query);
777 * Add fields to $profileAddressFields as appropriate.
778 * profileAddressFields is assigned to the template to tell it
779 * what fields are in the profile address
780 * that potentially should be copied to the Billing fields
781 * we want to give precedence to
783 * 2) then Primary designated as 'Primary
784 * 3) location_type is primary
785 * 4) if none of these apply then it just uses the first one
787 * as this will be used to
788 * transfer profile address data to billing fields
789 * http://issues.civicrm.org/jira/browse/CRM-5869
792 * Field key - e.g. street_address-Primary, first_name.
793 * @param array $profileAddressFields
794 * Array of profile fields that relate to address fields.
795 * @param array $profileFilter
796 * Filter to apply to profile fields - expected usage is to only fill based on.
797 * the bottom profile per CRM-13726
800 * Can the address block be hidden safe in the knowledge all fields are elsewhere collected (see CRM-15118)
802 public static function assignAddressField($key, &$profileAddressFields, $profileFilter) {
803 $billing_id = CRM_Core_BAO_LocationType
::getBilling();
804 list($prefixName, $index) = CRM_Utils_System
::explode('-', $key, 2);
806 $profileFields = civicrm_api3('uf_field', 'get', array_merge($profileFilter,
809 'return' => 'field_name, is_required',
815 //check for valid fields ( fields that are present in billing block )
816 $validBillingFields = array(
821 'supplemental_address_1',
827 $requiredBillingFields = array_diff($validBillingFields, array('middle_name', 'supplemental_address_1'));
828 $validProfileFields = array();
829 $requiredProfileFields = array();
831 foreach ($profileFields['values'] as $field) {
832 if (in_array($field['field_name'], $validBillingFields)) {
833 $validProfileFields[] = $field['field_name'];
835 if (CRM_Utils_Array
::value('is_required', $field)) {
836 $requiredProfileFields[] = $field['field_name'];
840 if (!in_array($prefixName, $validProfileFields)) {
844 if (!empty($index) && (
845 // it's empty so we set it OR
846 !CRM_Utils_Array
::value($prefixName, $profileAddressFields)
847 //we are dealing with billing id (precedence)
848 ||
$index == $billing_id
849 // we are dealing with primary & billing not set
850 ||
($index == 'Primary' && $profileAddressFields[$prefixName] != $billing_id)
851 ||
($index == CRM_Core_BAO_LocationType
::getDefault()->id
852 && $profileAddressFields[$prefixName] != $billing_id
853 && $profileAddressFields[$prefixName] != 'Primary'
857 $profileAddressFields[$prefixName] = $index;
860 $potentiallyMissingRequiredFields = array_diff($requiredBillingFields, $requiredProfileFields);
861 CRM_Core_Resources
::singleton()
862 ->addSetting(array('billing' => array('billingProfileIsHideable' => empty($potentiallyMissingRequiredFields))));
866 * Get a list of fields which can be added to profiles.
868 * @param int $gid : UF group ID
869 * @param array $defaults : Form defaults
870 * @return array, multidimensional; e.g. $result['FieldGroup']['field_name']['label']
872 public static function getAvailableFields($gid = NULL, $defaults = array()) {
874 'Contact' => array(),
875 'Individual' => CRM_Contact_BAO_Contact
::importableFields('Individual', FALSE, FALSE, TRUE, TRUE, TRUE),
876 'Household' => CRM_Contact_BAO_Contact
::importableFields('Household', FALSE, FALSE, TRUE, TRUE, TRUE),
877 'Organization' => CRM_Contact_BAO_Contact
::importableFields('Organization', FALSE, FALSE, TRUE, TRUE, TRUE),
880 // include hook injected fields
881 $fields['Contact'] = array_merge($fields['Contact'], CRM_Contact_BAO_Query_Hook
::singleton()->getFields());
883 // add current employer for individuals
884 $fields['Individual']['current_employer'] = array(
885 'name' => 'organization_name',
886 'title' => ts('Current Employer'),
889 $addressOptions = CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
890 'address_options', TRUE, NULL, TRUE
893 if (!$addressOptions['county']) {
894 unset($fields['Individual']['county'], $fields['Household']['county'], $fields['Organization']['county']);
897 // break out common contact fields array CRM-3037.
898 // from a UI perspective this makes very little sense
899 foreach ($fields['Individual'] as $key => $value) {
900 if (!empty($fields['Household'][$key]) && !empty($fields['Organization'][$key])) {
901 $fields['Contact'][$key] = $value;
902 unset($fields['Individual'][$key], $fields['Household'][$key], $fields['Organization'][$key]);
906 // Internal field not exposed to forms
907 unset($fields['Contact']['contact_type']);
908 unset($fields['Contact']['master_id']);
910 // convert phone extension in to psedo-field phone + phone extension
912 unset($fields['Contact']['phone_ext']);
914 $fields['Contact']['phone_and_ext'] = array(
915 'name' => 'phone_and_ext',
916 'title' => ts('Phone and Extension'),
917 'hasLocationType' => 1,
920 // include Subtypes For Profile
921 $subTypes = CRM_Contact_BAO_ContactType
::subTypeInfo();
922 foreach ($subTypes as $name => $val) {
923 //custom fields for sub type
924 $subTypeFields = CRM_Core_BAO_CustomField
::getFieldsForImport($name, FALSE, FALSE, FALSE, TRUE, TRUE);
925 if (array_key_exists($val['parent'], $fields)) {
926 $fields[$name] = $fields[$val['parent']] +
$subTypeFields;
929 $fields[$name] = $subTypeFields;
933 if (CRM_Core_Permission
::access('CiviContribute')) {
934 $contribFields = CRM_Contribute_BAO_Contribution
::getContributionFields(FALSE);
935 if (!empty($contribFields)) {
936 unset($contribFields['is_test']);
937 unset($contribFields['is_pay_later']);
938 unset($contribFields['contribution_id']);
939 $contribFields['contribution_note'] = array(
940 'name' => 'contribution_note',
941 'title' => ts('Contribution Note'),
943 $fields['Contribution'] = array_merge($contribFields, self
::getContribBatchEntryFields());
947 if (CRM_Core_Permission
::access('CiviEvent')) {
948 $participantFields = CRM_Event_BAO_Query
::getParticipantFields();
949 if ($participantFields) {
950 // Remove fields not supported by profiles
951 CRM_Utils_Array
::remove($participantFields,
952 'external_identifier',
954 'participant_contact_id',
955 'participant_role_id',
956 'participant_status_id',
957 'participant_is_test',
958 'participant_fee_level',
960 'participant_is_pay_later',
961 'participant_campaign'
963 if (isset($participantFields['participant_campaign_id'])) {
964 $participantFields['participant_campaign_id']['title'] = ts('Campaign');
966 $fields['Participant'] = $participantFields;
970 if (CRM_Core_Permission
::access('CiviMember')) {
971 $membershipFields = CRM_Member_BAO_Membership
::getMembershipFields();
972 // Remove fields not supported by profiles
973 CRM_Utils_Array
::remove($membershipFields,
975 'membership_type_id',
979 'member_is_pay_later'
981 if ($gid && CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $gid, 'name') == 'membership_batch_entry') {
982 $fields['Membership'] = array_merge($membershipFields, self
::getMemberBatchEntryFields());
985 $fields['Membership'] = $membershipFields;
989 if (CRM_Core_Permission
::access('CiviCase')) {
990 $caseFields = CRM_Case_BAO_Query
::getFields(TRUE);
991 $caseFields = array_merge($caseFields, CRM_Core_BAO_CustomField
::getFieldsForImport('Case'));
993 // Remove fields not supported by profiles
994 CRM_Utils_Array
::remove($caseFields,
1004 $fields['Case'] = $caseFields;
1007 $activityFields = CRM_Activity_BAO_Activity
::getProfileFields();
1008 if ($activityFields) {
1009 // campaign related fields.
1010 if (isset($activityFields['activity_campaign_id'])) {
1011 $activityFields['activity_campaign_id']['title'] = ts('Campaign');
1013 $fields['Activity'] = $activityFields;
1016 $fields['Formatting']['format_free_html_' . rand(1000, 9999)] = array(
1017 'name' => 'free_html',
1020 'title' => 'Free HTML',
1024 foreach ($fields as &$values) {
1025 $values = CRM_Utils_Array
::crmArraySortByField($values, 'title');
1028 //group selected and unwanted fields list
1029 $ufFields = $gid ? CRM_Core_BAO_UFGroup
::getFields($gid, FALSE, NULL, NULL, NULL, TRUE, NULL, TRUE) : array();
1030 $groupFieldList = array_merge($ufFields, array(
1032 'email_greeting_custom',
1033 'postal_greeting_custom',
1037 //unset selected fields
1038 foreach ($groupFieldList as $key => $value) {
1040 unset($fields['Individual'][$value], $fields['Household'][$value], $fields['Organization'][$value]);
1043 if (!empty($defaults['field_name'])
1044 && $defaults['field_name']['0'] == $value['field_type']
1045 && $defaults['field_name']['1'] == $key
1049 unset($fields[$value['field_type']][$key]);
1056 * Get a list of fields which can be added to profiles.
1058 * @param bool $force
1060 * @return array, multidimensional; e.g. $result['field_name']['label']
1062 public static function getAvailableFieldsFlat($force = FALSE) {
1063 // FIXME reset when data model changes
1064 static $result = NULL;
1065 if ($result === NULL ||
$force) {
1066 $fieldTree = self
::getAvailableFields();
1068 foreach ($fieldTree as $field_type => $fields) {
1069 foreach ($fields as $field_name => $field) {
1070 if (!isset($result[$field_name])) {
1071 $field['field_type'] = $field_type;
1072 $result[$field_name] = $field;
1081 * Determine whether the given field_name is valid.
1083 * @param string $fieldName
1086 public static function isValidFieldName($fieldName) {
1087 $availableFields = CRM_Core_BAO_UFField
::getAvailableFieldsFlat();
1088 return isset($availableFields[$fieldName]);
1092 * @return array|null
1094 public static function getContribBatchEntryFields() {
1095 if (self
::$_contriBatchEntryFields === NULL) {
1096 self
::$_contriBatchEntryFields = array(
1097 'send_receipt' => array(
1098 'name' => 'send_receipt',
1099 'title' => ts('Send Receipt'),
1101 'soft_credit' => array(
1102 'name' => 'soft_credit',
1103 'title' => ts('Soft Credit'),
1105 'soft_credit_type' => array(
1106 'name' => 'soft_credit_type',
1107 'title' => ts('Soft Credit Type'),
1109 'product_name' => array(
1110 'name' => 'product_name',
1111 'title' => ts('Premiums'),
1113 'contribution_note' => array(
1114 'name' => 'contribution_note',
1115 'title' => ts('Contribution Note'),
1117 'contribution_soft_credit_pcp_id' => array(
1118 'name' => 'contribution_soft_credit_pcp_id',
1119 'title' => ts('Personal Campaign Page'),
1123 return self
::$_contriBatchEntryFields;
1127 * @return array|null
1129 public static function getMemberBatchEntryFields() {
1130 if (self
::$_memberBatchEntryFields === NULL) {
1131 self
::$_memberBatchEntryFields = array(
1132 'send_receipt' => array(
1133 'name' => 'send_receipt',
1134 'title' => ts('Send Receipt'),
1136 'soft_credit' => array(
1137 'name' => 'soft_credit',
1138 'title' => ts('Soft Credit'),
1140 'product_name' => array(
1141 'name' => 'product_name',
1142 'title' => ts('Premiums'),
1144 'financial_type' => array(
1145 'name' => 'financial_type',
1146 'title' => ts('Financial Type'),
1148 'total_amount' => array(
1149 'name' => 'total_amount',
1150 'title' => ts('Total Amount'),
1152 'receive_date' => array(
1153 'name' => 'receive_date',
1154 'title' => ts('Date Received'),
1156 'payment_instrument' => array(
1157 'name' => 'payment_instrument',
1158 'title' => ts('Payment Method'),
1160 'contribution_status_id' => array(
1161 'name' => 'contribution_status_id',
1162 'title' => ts('Contribution Status'),
1165 'name' => 'contribution_trxn_id',
1166 'title' => ts('Contribution Transaction ID'),
1170 return self
::$_memberBatchEntryFields;