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 $ufField = CRM_Core_BAO_UFField
::add($params);
107 $fieldsType = CRM_Core_BAO_UFGroup
::calculateGroupType($groupId, TRUE);
108 CRM_Core_BAO_UFGroup
::updateGroupTypes($groupId, $fieldsType);
110 civicrm_api3('profile', 'getfields', array('cache_clear' => TRUE));
116 * Fetch object based on array of properties.
118 * @param array $params
119 * (reference ) an assoc array of name/value pairs.
120 * @param array $defaults
121 * (reference ) an assoc array to hold the flattened values.
123 * @return CRM_Core_BAO_UFField
125 public static function retrieve(&$params, &$defaults) {
126 return CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_UFField', $params, $defaults);
130 * Update the is_active flag in the db.
133 * Id of the database record.
134 * @param bool $is_active
135 * Value we want to set the is_active field.
138 * DAO object on success, null otherwise
140 public static function setIsActive($id, $is_active) {
141 //check if custom data profile field is disabled
143 if (CRM_Core_BAO_UFField
::checkUFStatus($id)) {
144 return CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_UFField', $id, 'is_active', $is_active);
147 CRM_Core_Session
::setStatus(ts('Cannot enable this UF field since the used custom field is disabled.'), ts('Check Custom Field'), 'error');
151 return CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_UFField', $id, 'is_active', $is_active);
156 * Delete the profile Field.
164 public static function del($id) {
166 $field = new CRM_Core_DAO_UFField();
173 * Check duplicate for duplicate field in a group.
175 * @param array $params
176 * An associative array with field and values.
180 public static function duplicateField($params) {
181 $ufField = new CRM_Core_DAO_UFField();
182 $ufField->uf_group_id
= CRM_Utils_Array
::value('uf_group_id', $params);
183 $ufField->field_type
= $params['field_type'];
184 $ufField->field_name
= $params['field_name'];
185 $ufField->website_type_id
= CRM_Utils_Array
::value('website_type_id', $params);
186 $ufField->location_type_id
= CRM_Utils_Array
::value('location_type_id', $params);
187 $ufField->phone_type_id
= CRM_Utils_Array
::value('phone_type_id', $params);;
189 if (!empty($params['id'])) {
190 $ufField->whereAdd("id <> " . $params['id']);
193 return ($ufField->find(TRUE) ?
1 : 0);
197 * Does profile consists of a multi-record custom field.
203 public static function checkMultiRecordFieldExists($gId) {
204 $queryString = "SELECT f.field_name
205 FROM civicrm_uf_field f, civicrm_uf_group g
206 WHERE f.uf_group_id = g.id
207 AND g.id = %1 AND f.field_name LIKE 'custom%'";
208 $p = array(1 => array($gId, 'Integer'));
209 $dao = CRM_Core_DAO
::executeQuery($queryString, $p);
210 $customFieldIds = array();
211 $isMultiRecordFieldPresent = FALSE;
212 while ($dao->fetch()) {
213 if ($customId = CRM_Core_BAO_CustomField
::getKeyID($dao->field_name
)) {
214 if (is_numeric($customId)) {
215 $customFieldIds[] = $customId;
220 if (!empty($customFieldIds) && count($customFieldIds) == 1) {
221 $customFieldId = array_pop($customFieldIds);
222 $isMultiRecordFieldPresent = CRM_Core_BAO_CustomField
::isMultiRecordField($customFieldId);
224 elseif (count($customFieldIds) > 1) {
225 $customFieldIds = implode(", ", $customFieldIds);
228 FROM civicrm_custom_group cg
229 INNER JOIN civicrm_custom_field cf
230 ON cg.id = cf.custom_group_id
231 WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1";
233 $dao = CRM_Core_DAO
::executeQuery($queryString);
235 $isMultiRecordFieldPresent = ($dao->cgId
) ?
$dao->cgId
: FALSE;
239 return $isMultiRecordFieldPresent;
245 * @param array $params
246 * (reference) array containing the values submitted by the form.
248 * @return CRM_Core_BAO_UFField
250 public static function add(&$params) {
251 // set values for uf field properties and save
252 $ufField = new CRM_Core_DAO_UFField();
253 $ufField->copyValues($params);
254 $ufField->field_type
= $params['field_name'][0];
255 $ufField->field_name
= $params['field_name'][1];
257 //should not set location type id for Primary
258 $locationTypeId = NULL;
259 if ($params['field_name'][1] == 'url') {
260 $ufField->website_type_id
= CRM_Utils_Array
::value(2, $params['field_name']);
263 $locationTypeId = CRM_Utils_Array
::value(2, $params['field_name']);
264 $ufField->website_type_id
= NULL;
266 if ($locationTypeId) {
267 $ufField->location_type_id
= $locationTypeId;
270 $ufField->location_type_id
= 'null';
273 $ufField->phone_type_id
= CRM_Utils_Array
::value(3, $params['field_name'], 'NULL');
275 return $ufField->save();
279 * Automatically determine one weight and modify others.
281 * @param array $params
282 * UFField record, e.g. with 'weight', 'uf_group_id', and 'field_id'.
285 public static function autoWeight($params) {
289 if (!empty($params['field_id'])) {
290 $oldWeight = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFField', $params['field_id'], 'weight', 'id');
292 $fieldValues = array('uf_group_id' => $params['group_id']);
293 return CRM_Utils_Weight
::updateOtherWeights('CRM_Core_DAO_UFField', $oldWeight, CRM_Utils_Array
::value('weight', $params, 0), $fieldValues);
297 * Enable/disable profile field given a custom field id
299 * @param int $customFieldId
301 * @param bool $is_active
302 * Set the is_active field.
304 public static function setUFField($customFieldId, $is_active) {
305 // Find the profile id given custom field.
306 $ufField = new CRM_Core_DAO_UFField();
307 $ufField->field_name
= "custom_" . $customFieldId;
310 while ($ufField->fetch()) {
311 // Enable/ disable profile.
312 CRM_Core_BAO_UFField
::setIsActive($ufField->id
, $is_active);
317 * Copy existing profile fields to
318 * new profile from the already built profile
321 * From which we need to copy.
322 * @param bool $new_id
325 public static function copy($old_id, $new_id) {
326 $ufField = new CRM_Core_DAO_UFField();
327 $ufField->uf_group_id
= $old_id;
329 while ($ufField->fetch()) {
330 //copy the field records as it is on new ufgroup id
331 $ufField->uf_group_id
= $new_id;
338 * Delete profile field given a custom field.
340 * @param int $customFieldId
341 * ID of the custom field to be deleted.
343 public static function delUFField($customFieldId) {
344 //find the profile id given custom field id
345 $ufField = new CRM_Core_DAO_UFField();
346 $ufField->field_name
= "custom_" . $customFieldId;
349 while ($ufField->fetch()) {
350 //enable/ disable profile
351 CRM_Core_BAO_UFField
::del($ufField->id
);
356 * Enable/disable profile field given a custom group id
358 * @param int $customGroupId
360 * @param bool $is_active
361 * Value we want to set the is_active field.
363 public static function setUFFieldStatus($customGroupId, $is_active) {
364 //find the profile id given custom group id
365 $queryString = "SELECT civicrm_custom_field.id as custom_field_id
366 FROM civicrm_custom_field, civicrm_custom_group
367 WHERE civicrm_custom_field.custom_group_id = civicrm_custom_group.id
368 AND civicrm_custom_group.id = %1";
369 $p = array(1 => array($customGroupId, 'Integer'));
370 $dao = CRM_Core_DAO
::executeQuery($queryString, $p);
372 while ($dao->fetch()) {
373 // Enable/ disable profile.
374 CRM_Core_BAO_UFField
::setUFField($dao->custom_field_id
, $is_active);
379 * Check the status of custom field used in uf fields.
381 * @param int $UFFieldId
384 * false if custom field are disabled else true
386 public static function checkUFStatus($UFFieldId) {
387 $fieldName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFField', $UFFieldId, 'field_name');
388 // return if field is not a custom field
389 if (!$customFieldId = CRM_Core_BAO_CustomField
::getKeyID($fieldName)) {
393 $customField = new CRM_Core_DAO_CustomField();
394 $customField->id
= $customFieldId;
395 // if uf field is custom field
396 if ($customField->find(TRUE)) {
397 if (!$customField->is_active
) {
407 * Find out whether given profile group using Activity
408 * Profile fields with contact fields
410 * @param int $ufGroupId
414 public static function checkContactActivityProfileType($ufGroupId) {
415 $ufGroup = new CRM_Core_DAO_UFGroup();
416 $ufGroup->id
= $ufGroupId;
417 $ufGroup->find(TRUE);
419 return self
::checkContactActivityProfileTypeByGroupType($ufGroup->group_type
);
424 * @param $ufGroupType
427 public static function checkContactActivityProfileTypeByGroupType($ufGroupType) {
428 $profileTypes = array();
430 $typeParts = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $ufGroupType);
431 $profileTypes = explode(',', $typeParts[0]);
434 if (empty($profileTypes)) {
437 $components = array('Contribution', 'Participant', 'Membership');
438 if (!in_array('Activity', $profileTypes)) {
441 elseif (count($profileTypes) == 1) {
445 if ($index = array_search('Contact', $profileTypes)) {
446 unset($profileTypes[$index]);
447 if (count($profileTypes) == 1) {
452 $contactTypes = array('Individual', 'Household', 'Organization');
453 $subTypes = CRM_Contact_BAO_ContactType
::subTypes();
455 $profileTypeComponent = array_intersect($components, $profileTypes);
456 if (!empty($profileTypeComponent) ||
457 count(array_intersect($contactTypes, $profileTypes)) > 1 ||
458 count(array_intersect($subTypes, $profileTypes)) > 1
467 * Find out whether given profile group uses $required
468 * and/or $optional profile types
470 * @param int $ufGroupId
472 * @param array $required
473 * Array of types those are required.
474 * @param array $optional
475 * Array of types those are optional.
479 public static function checkValidProfileType($ufGroupId, $required, $optional = NULL) {
480 if (!is_array($required) ||
empty($required)) {
484 $ufGroup = new CRM_Core_DAO_UFGroup();
485 $ufGroup->id
= $ufGroupId;
486 $ufGroup->find(TRUE);
488 $profileTypes = array();
489 if ($ufGroup->group_type
) {
490 $typeParts = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $ufGroup->group_type
);
491 $profileTypes = explode(',', $typeParts[0]);
494 if (empty($profileTypes)) {
499 foreach ($required as $key => $val) {
500 if (!in_array($val, $profileTypes)) {
506 if ($valid && is_array($optional)) {
507 foreach ($optional as $key => $val) {
508 if (in_array($val, $profileTypes)) {
519 * Check for mix profile fields (eg: individual + other contact types)
521 * @param int $ufGroupId
524 * true for mix profile else false
526 public static function checkProfileType($ufGroupId) {
527 $ufGroup = new CRM_Core_DAO_UFGroup();
528 $ufGroup->id
= $ufGroupId;
529 $ufGroup->find(TRUE);
531 $profileTypes = array();
532 if ($ufGroup->group_type
) {
533 $typeParts = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $ufGroup->group_type
);
534 $profileTypes = explode(',', $typeParts[0]);
537 //early return if new profile.
538 if (empty($profileTypes)) {
542 //we need to unset Contact
543 if (count($profileTypes) > 1) {
544 $index = array_search('Contact', $profileTypes);
545 if ($index !== FALSE) {
546 unset($profileTypes[$index]);
550 // suppress any subtypes if present
551 CRM_Contact_BAO_ContactType
::suppressSubTypes($profileTypes);
553 $contactTypes = array('Contact', 'Individual', 'Household', 'Organization');
554 $components = array('Contribution', 'Participant', 'Membership', 'Activity');
557 // check for mix profile condition
558 if (count($profileTypes) > 1) {
559 //check the there are any components include in profile
560 foreach ($components as $value) {
561 if (in_array($value, $profileTypes)) {
565 //check if there are more than one contact types included in profile
566 if (count($profileTypes) > 1) {
570 elseif (count($profileTypes) == 1) {
571 // note for subtype case count would be zero
572 $profileTypes = array_values($profileTypes);
573 if (!in_array($profileTypes[0], $contactTypes)) {
582 * Get the profile type (eg: individual/organization/household)
584 * @param int $ufGroupId
586 * @param bool $returnMixType
587 * This is true, then field type of mix profile field is returned.
588 * @param bool $onlyPure
589 * True if only pure profiles are required.
591 * @param bool $skipComponentType
597 public static function getProfileType($ufGroupId, $returnMixType = TRUE, $onlyPure = FALSE, $skipComponentType = FALSE) {
598 $ufGroup = new CRM_Core_DAO_UFGroup();
599 $ufGroup->id
= $ufGroupId;
600 $ufGroup->is_active
= 1;
602 $ufGroup->find(TRUE);
603 return self
::calculateProfileType($ufGroup->group_type
, $returnMixType, $onlyPure, $skipComponentType);
607 * Get the profile type (eg: individual/organization/household)
609 * @param string $ufGroupType
610 * @param bool $returnMixType
611 * This is true, then field type of mix profile field is returned.
612 * @param bool $onlyPure
613 * True if only pure profiles are required.
614 * @param bool $skipComponentType
616 * @return string profile group_type
619 public static function calculateProfileType($ufGroupType, $returnMixType = TRUE, $onlyPure = FALSE, $skipComponentType = FALSE) {
621 $contactTypes = array('Contact', 'Individual', 'Household', 'Organization');
622 $subTypes = CRM_Contact_BAO_ContactType
::subTypes();
623 $components = array('Contribution', 'Participant', 'Membership', 'Activity');
625 $profileTypes = array();
627 $typeParts = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $ufGroupType);
628 $profileTypes = explode(',', $typeParts[0]);
632 if (count($profileTypes) == 1) {
633 return $profileTypes[0];
640 //we need to unset Contact
641 if (count($profileTypes) > 1) {
642 $index = array_search('Contact', $profileTypes);
643 if ($index !== FALSE) {
644 unset($profileTypes[$index]);
648 $profileType = $mixProfileType = NULL;
650 // this case handles pure profile
651 if (count($profileTypes) == 1) {
652 $profileType = array_pop($profileTypes);
655 //check the there are any components include in profile
656 $componentCount = array();
657 foreach ($components as $value) {
658 if (in_array($value, $profileTypes)) {
659 $componentCount[] = $value;
663 //check contact type included in profile
664 $contactTypeCount = array();
665 foreach ($contactTypes as $value) {
666 if (in_array($value, $profileTypes)) {
667 $contactTypeCount[] = $value;
671 $subTypeCount = array();
672 foreach ($subTypes as $value) {
673 if (in_array($value, $profileTypes)) {
674 $subTypeCount[] = $value;
677 if (!$skipComponentType && count($componentCount) == 1) {
678 $profileType = $componentCount[0];
680 elseif (count($componentCount) > 1) {
681 $mixProfileType = $componentCount[1];
683 elseif (count($subTypeCount) == 1) {
684 $profileType = $subTypeCount[0];
686 elseif (count($contactTypeCount) == 1) {
687 $profileType = $contactTypeCount[0];
689 elseif (count($subTypeCount) > 1) {
690 // this is mix subtype profiles
691 $mixProfileType = $subTypeCount[1];
693 elseif (count($contactTypeCount) > 1) {
694 // this is mix contact profiles
695 $mixProfileType = $contactTypeCount[1];
699 if ($mixProfileType) {
700 if ($returnMixType) {
701 return $mixProfileType;
713 * Check for mix profiles groups (eg: individual + other contact types)
718 * true for mix profile group else false
720 public static function checkProfileGroupType($ctype) {
721 $ufGroup = new CRM_Core_DAO_UFGroup();
725 FROM civicrm_uf_group as ufg, civicrm_uf_join as ufj
726 WHERE ufg.id = ufj.uf_group_id
727 AND ufj.module = 'User Registration'
728 AND ufg.is_active = 1 ";
730 $ufGroup = CRM_Core_DAO
::executeQuery($query);
733 $validProfiles = array('Individual', 'Organization', 'Household', 'Contribution');
734 while ($ufGroup->fetch()) {
735 $profileType = self
::getProfileType($ufGroup->id
);
736 if (in_array($profileType, $validProfiles)) {
739 elseif ($profileType) {
748 * Check for searchable or in selector field for given profile.
750 * @param int $profileID
754 public static function checkSearchableORInSelector($profileID) {
762 From civicrm_uf_field
763 WHERE (in_selector = 1 OR is_searchable = 1)
764 AND uf_group_id = {$profileID}";
766 $ufFields = CRM_Core_DAO
::executeQuery($query);
767 while ($ufFields->fetch()) {
776 * Reset In selector and is searchable values for given $profileID.
778 * @param int $profileID
780 public function resetInSelectorANDSearchable($profileID) {
784 $query = "UPDATE civicrm_uf_field SET in_selector = 0, is_searchable = 0 WHERE uf_group_id = {$profileID}";
785 CRM_Core_DAO
::executeQuery($query);
789 * Add fields to $profileAddressFields as appropriate.
790 * profileAddressFields is assigned to the template to tell it
791 * what fields are in the profile address
792 * that potentially should be copied to the Billing fields
793 * we want to give precedence to
795 * 2) then Primary designated as 'Primary
796 * 3) location_type is primary
797 * 4) if none of these apply then it just uses the first one
799 * as this will be used to
800 * transfer profile address data to billing fields
801 * http://issues.civicrm.org/jira/browse/CRM-5869
804 * Field key - e.g. street_address-Primary, first_name.
805 * @param array $profileAddressFields
806 * Array of profile fields that relate to address fields.
807 * @param array $profileFilter
808 * Filter to apply to profile fields - expected usage is to only fill based on.
809 * the bottom profile per CRM-13726
812 * Can the address block be hidden safe in the knowledge all fields are elsewhere collected (see CRM-15118)
814 public static function assignAddressField($key, &$profileAddressFields, $profileFilter) {
815 $billing_id = CRM_Core_BAO_LocationType
::getBilling();
816 list($prefixName, $index) = CRM_Utils_System
::explode('-', $key, 2);
818 $profileFields = civicrm_api3('uf_field', 'get', array_merge($profileFilter,
821 'return' => 'field_name, is_required',
827 //check for valid fields ( fields that are present in billing block )
828 $validBillingFields = array(
833 'supplemental_address_1',
839 $requiredBillingFields = array_diff($validBillingFields, array('middle_name', 'supplemental_address_1'));
840 $validProfileFields = array();
841 $requiredProfileFields = array();
843 foreach ($profileFields['values'] as $field) {
844 if (in_array($field['field_name'], $validBillingFields)) {
845 $validProfileFields[] = $field['field_name'];
847 if (CRM_Utils_Array
::value('is_required', $field)) {
848 $requiredProfileFields[] = $field['field_name'];
852 if (!in_array($prefixName, $validProfileFields)) {
856 if (!empty($index) && (
857 // it's empty so we set it OR
858 !CRM_Utils_Array
::value($prefixName, $profileAddressFields)
859 //we are dealing with billing id (precedence)
860 ||
$index == $billing_id
861 // we are dealing with primary & billing not set
862 ||
($index == 'Primary' && $profileAddressFields[$prefixName] != $billing_id)
863 ||
($index == CRM_Core_BAO_LocationType
::getDefault()->id
864 && $profileAddressFields[$prefixName] != $billing_id
865 && $profileAddressFields[$prefixName] != 'Primary'
869 $profileAddressFields[$prefixName] = $index;
872 $potentiallyMissingRequiredFields = array_diff($requiredBillingFields, $requiredProfileFields);
873 CRM_Core_Resources
::singleton()
874 ->addSetting(array('billing' => array('billingProfileIsHideable' => empty($potentiallyMissingRequiredFields))));
878 * Get a list of fields which can be added to profiles.
880 * @param int $gid : UF group ID
881 * @param array $defaults : Form defaults
882 * @return array, multidimensional; e.g. $result['FieldGroup']['field_name']['label']
884 public static function getAvailableFields($gid = NULL, $defaults = array()) {
886 'Contact' => array(),
887 'Individual' => CRM_Contact_BAO_Contact
::importableFields('Individual', FALSE, FALSE, TRUE, TRUE, TRUE),
888 'Household' => CRM_Contact_BAO_Contact
::importableFields('Household', FALSE, FALSE, TRUE, TRUE, TRUE),
889 'Organization' => CRM_Contact_BAO_Contact
::importableFields('Organization', FALSE, FALSE, TRUE, TRUE, TRUE),
892 // include hook injected fields
893 $fields['Contact'] = array_merge($fields['Contact'], CRM_Contact_BAO_Query_Hook
::singleton()->getFields());
895 // add current employer for individuals
896 $fields['Individual']['current_employer'] = array(
897 'name' => 'organization_name',
898 'title' => ts('Current Employer'),
901 $addressOptions = CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
902 'address_options', TRUE, NULL, TRUE
905 if (!$addressOptions['county']) {
906 unset($fields['Individual']['county'], $fields['Household']['county'], $fields['Organization']['county']);
909 // break out common contact fields array CRM-3037.
910 // from a UI perspective this makes very little sense
911 foreach ($fields['Individual'] as $key => $value) {
912 if (!empty($fields['Household'][$key]) && !empty($fields['Organization'][$key])) {
913 $fields['Contact'][$key] = $value;
914 unset($fields['Individual'][$key], $fields['Household'][$key], $fields['Organization'][$key]);
918 // Internal field not exposed to forms
919 unset($fields['Contact']['contact_type']);
920 unset($fields['Contact']['master_id']);
922 // convert phone extension in to psedo-field phone + phone extension
924 unset($fields['Contact']['phone_ext']);
926 $fields['Contact']['phone_and_ext'] = array(
927 'name' => 'phone_and_ext',
928 'title' => ts('Phone and Extension'),
929 'hasLocationType' => 1,
932 // include Subtypes For Profile
933 $subTypes = CRM_Contact_BAO_ContactType
::subTypeInfo();
934 foreach ($subTypes as $name => $val) {
935 //custom fields for sub type
936 $subTypeFields = CRM_Core_BAO_CustomField
::getFieldsForImport($name, FALSE, FALSE, FALSE, TRUE, TRUE);
937 if (array_key_exists($val['parent'], $fields)) {
938 $fields[$name] = $fields[$val['parent']] +
$subTypeFields;
941 $fields[$name] = $subTypeFields;
945 if (CRM_Core_Permission
::access('CiviContribute')) {
946 $contribFields = CRM_Contribute_BAO_Contribution
::getContributionFields(FALSE);
947 if (!empty($contribFields)) {
948 unset($contribFields['is_test']);
949 unset($contribFields['is_pay_later']);
950 unset($contribFields['contribution_id']);
951 $contribFields['contribution_note'] = array(
952 'name' => 'contribution_note',
953 'title' => ts('Contribution Note'),
955 $fields['Contribution'] = array_merge($contribFields, self
::getContribBatchEntryFields());
959 if (CRM_Core_Permission
::access('CiviEvent')) {
960 $participantFields = CRM_Event_BAO_Query
::getParticipantFields();
961 if ($participantFields) {
962 // Remove fields not supported by profiles
963 CRM_Utils_Array
::remove($participantFields,
964 'external_identifier',
966 'participant_contact_id',
967 'participant_role_id',
968 'participant_status_id',
969 'participant_is_test',
970 'participant_fee_level',
972 'participant_is_pay_later',
973 'participant_campaign'
975 if (isset($participantFields['participant_campaign_id'])) {
976 $participantFields['participant_campaign_id']['title'] = ts('Campaign');
978 $fields['Participant'] = $participantFields;
982 if (CRM_Core_Permission
::access('CiviMember')) {
983 $membershipFields = CRM_Member_BAO_Membership
::getMembershipFields();
984 // Remove fields not supported by profiles
985 CRM_Utils_Array
::remove($membershipFields,
987 'membership_type_id',
991 'member_is_pay_later'
993 if ($gid && CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $gid, 'name') == 'membership_batch_entry') {
994 $fields['Membership'] = array_merge($membershipFields, self
::getMemberBatchEntryFields());
997 $fields['Membership'] = $membershipFields;
1001 if (CRM_Core_Permission
::access('CiviCase')) {
1002 $caseFields = CRM_Case_BAO_Query
::getFields(TRUE);
1003 $caseFields = array_merge($caseFields, CRM_Core_BAO_CustomField
::getFieldsForImport('Case'));
1005 // Remove fields not supported by profiles
1006 CRM_Utils_Array
::remove($caseFields,
1016 $fields['Case'] = $caseFields;
1019 $activityFields = CRM_Activity_BAO_Activity
::getProfileFields();
1020 if ($activityFields) {
1021 // campaign related fields.
1022 if (isset($activityFields['activity_campaign_id'])) {
1023 $activityFields['activity_campaign_id']['title'] = ts('Campaign');
1025 $fields['Activity'] = $activityFields;
1028 $fields['Formatting']['format_free_html_' . rand(1000, 9999)] = array(
1029 'name' => 'free_html',
1032 'title' => 'Free HTML',
1036 foreach ($fields as &$values) {
1037 $values = CRM_Utils_Array
::crmArraySortByField($values, 'title');
1040 //group selected and unwanted fields list
1041 $ufFields = $gid ? CRM_Core_BAO_UFGroup
::getFields($gid, FALSE, NULL, NULL, NULL, TRUE, NULL, TRUE) : array();
1042 $groupFieldList = array_merge($ufFields, array(
1044 'email_greeting_custom',
1045 'postal_greeting_custom',
1049 //unset selected fields
1050 foreach ($groupFieldList as $key => $value) {
1052 unset($fields['Individual'][$value], $fields['Household'][$value], $fields['Organization'][$value]);
1055 if (!empty($defaults['field_name'])
1056 && $defaults['field_name']['0'] == $value['field_type']
1057 && $defaults['field_name']['1'] == $key
1061 unset($fields[$value['field_type']][$key]);
1068 * Get a list of fields which can be added to profiles.
1070 * @param bool $force
1072 * @return array, multidimensional; e.g. $result['field_name']['label']
1074 public static function getAvailableFieldsFlat($force = FALSE) {
1075 // FIXME reset when data model changes
1076 static $result = NULL;
1077 if ($result === NULL ||
$force) {
1078 $fieldTree = self
::getAvailableFields();
1080 foreach ($fieldTree as $field_type => $fields) {
1081 foreach ($fields as $field_name => $field) {
1082 if (!isset($result[$field_name])) {
1083 $field['field_type'] = $field_type;
1084 $result[$field_name] = $field;
1093 * Determine whether the given field_name is valid.
1095 * @param string $fieldName
1098 public static function isValidFieldName($fieldName) {
1099 $availableFields = CRM_Core_BAO_UFField
::getAvailableFieldsFlat();
1100 return isset($availableFields[$fieldName]);
1104 * @return array|null
1106 public static function getContribBatchEntryFields() {
1107 if (self
::$_contriBatchEntryFields === NULL) {
1108 self
::$_contriBatchEntryFields = array(
1109 'send_receipt' => array(
1110 'name' => 'send_receipt',
1111 'title' => ts('Send Receipt'),
1113 'soft_credit' => array(
1114 'name' => 'soft_credit',
1115 'title' => ts('Soft Credit'),
1117 'soft_credit_type' => array(
1118 'name' => 'soft_credit_type',
1119 'title' => ts('Soft Credit Type'),
1121 'product_name' => array(
1122 'name' => 'product_name',
1123 'title' => ts('Premiums'),
1125 'contribution_note' => array(
1126 'name' => 'contribution_note',
1127 'title' => ts('Contribution Note'),
1129 'contribution_soft_credit_pcp_id' => array(
1130 'name' => 'contribution_soft_credit_pcp_id',
1131 'title' => ts('Personal Campaign Page'),
1135 return self
::$_contriBatchEntryFields;
1139 * @return array|null
1141 public static function getMemberBatchEntryFields() {
1142 if (self
::$_memberBatchEntryFields === NULL) {
1143 self
::$_memberBatchEntryFields = array(
1144 'send_receipt' => array(
1145 'name' => 'send_receipt',
1146 'title' => ts('Send Receipt'),
1148 'soft_credit' => array(
1149 'name' => 'soft_credit',
1150 'title' => ts('Soft Credit'),
1152 'product_name' => array(
1153 'name' => 'product_name',
1154 'title' => ts('Premiums'),
1156 'financial_type' => array(
1157 'name' => 'financial_type',
1158 'title' => ts('Financial Type'),
1160 'total_amount' => array(
1161 'name' => 'total_amount',
1162 'title' => ts('Total Amount'),
1164 'receive_date' => array(
1165 'name' => 'receive_date',
1166 'title' => ts('Date Received'),
1168 'payment_instrument' => array(
1169 'name' => 'payment_instrument',
1170 'title' => ts('Payment Method'),
1172 'contribution_status_id' => array(
1173 'name' => 'contribution_status_id',
1174 'title' => ts('Contribution Status'),
1177 'name' => 'contribution_trxn_id',
1178 'title' => ts('Contribution Transaction ID'),
1182 return self
::$_memberBatchEntryFields;