3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
37 * This class contains function for UFField
40 class CRM_Core_BAO_UFField
extends CRM_Core_DAO_UFField
{
45 private static $_contriBatchEntryFields = NULL;
46 private static $_memberBatchEntryFields = NULL;
50 * Fetch object based on array of properties
52 * @param array $params
53 * (reference ) an assoc array of name/value pairs.
54 * @param array $defaults
55 * (reference ) an assoc array to hold the flattened values.
57 * @return CRM_Core_BAO_UFField object
60 public static function retrieve(&$params, &$defaults) {
61 return CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_UFField', $params, $defaults);
75 public static function getTitle($id) {
76 return CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFField', $groupId, 'title');
80 * Update the is_active flag in the db
83 * Id of the database record.
84 * @param bool $is_active
85 * Value we want to set the is_active field.
88 * DAO object on sucess, null otherwise
91 public static function setIsActive($id, $is_active) {
92 //check if custom data profile field is disabled
94 if (CRM_Core_BAO_UFField
::checkUFStatus($id)) {
95 return CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_UFField', $id, 'is_active', $is_active);
98 CRM_Core_Session
::setStatus(ts('Cannot enable this UF field since the used custom field is disabled.'), ts('Check Custom Field'), 'error');
102 return CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_UFField', $id, 'is_active', $is_active);
107 * Delete the profile Field.
116 public static function del($id) {
118 $field = new CRM_Core_DAO_UFField();
125 * Check duplicate for duplicate field in a group
127 * @param array $params
128 * An associative array with field and values.
132 * @ids array $ids array that containd ids
136 public static function duplicateField($params, $ids) {
137 $ufField = new CRM_Core_DAO_UFField();
138 $ufField->uf_group_id
= CRM_Utils_Array
::value('uf_group', $ids);
139 $ufField->field_type
= $params['field_name'][0];
140 $ufField->field_name
= $params['field_name'][1];
141 if ($params['field_name'][1] == 'url') {
142 $ufField->website_type_id
= CRM_Utils_Array
::value(2, $params['field_name'], NULL);
145 $ufField->location_type_id
= (CRM_Utils_Array
::value(2, $params['field_name'])) ?
$params['field_name'][2] : 'NULL';
147 $ufField->phone_type_id
= CRM_Utils_Array
::value(3, $params['field_name']);
149 if (!empty($ids['uf_field'])) {
150 $ufField->whereAdd("id <> " . CRM_Utils_Array
::value('uf_field', $ids));
153 return $ufField->find(TRUE);
157 * Does profile consists of a multi-record custom field
159 public static function checkMultiRecordFieldExists($gId) {
160 $queryString = "SELECT f.field_name
161 FROM civicrm_uf_field f, civicrm_uf_group g
162 WHERE f.uf_group_id = g.id
163 AND g.id = %1 AND f.field_name LIKE 'custom%'";
164 $p = array(1 => array($gId, 'Integer'));
165 $dao = CRM_Core_DAO
::executeQuery($queryString, $p);
166 $customFieldIds = array();
167 $isMultiRecordFieldPresent = FALSE;
168 while ($dao->fetch()) {
169 if ($customId = CRM_Core_BAO_CustomField
::getKeyID($dao->field_name
)) {
170 if (is_numeric($customId)) {
171 $customFieldIds[] = $customId;
176 if (!empty($customFieldIds) && count($customFieldIds) == 1) {
177 $customFieldId = array_pop($customFieldIds);
178 $isMultiRecordFieldPresent = CRM_Core_BAO_CustomField
::isMultiRecordField($customFieldId);
180 elseif (count($customFieldIds) > 1) {
181 $customFieldIds = implode(", ", $customFieldIds);
184 FROM civicrm_custom_group cg
185 INNER JOIN civicrm_custom_field cf
186 ON cg.id = cf.custom_group_id
187 WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1";
189 $dao = CRM_Core_DAO
::executeQuery($queryString);
191 $isMultiRecordFieldPresent = ($dao->cgId
) ?
$dao->cgId
: FALSE;
195 return $isMultiRecordFieldPresent;
201 * @param array $params
202 * (reference) array containing the values submitted by the form.
204 * Array containing the id.
206 * @return CRM_Core_BAO_UFField object
210 public static function add(&$params, $ids = array()) {
211 // set values for uf field properties and save
212 $ufField = new CRM_Core_DAO_UFField();
213 $ufField->field_type
= $params['field_name'][0];
214 $ufField->field_name
= $params['field_name'][1];
216 //should not set location type id for Primary
217 $locationTypeId = NULL;
218 if ($params['field_name'][1] == 'url') {
219 $ufField->website_type_id
= CRM_Utils_Array
::value(2, $params['field_name']);
222 $locationTypeId = CRM_Utils_Array
::value(2, $params['field_name']);
223 $ufField->website_type_id
= NULL;
225 if ($locationTypeId) {
226 $ufField->location_type_id
= $locationTypeId;
229 $ufField->location_type_id
= 'null';
232 $ufField->phone_type_id
= CRM_Utils_Array
::value(3, $params['field_name'], 'NULL');
233 $ufField->listings_title
= CRM_Utils_Array
::value('listings_title', $params);
234 $ufField->visibility
= CRM_Utils_Array
::value('visibility', $params);
235 $ufField->help_pre
= CRM_Utils_Array
::value('help_pre', $params);
236 $ufField->help_post
= CRM_Utils_Array
::value('help_post', $params);
237 $ufField->label
= CRM_Utils_Array
::value('label', $params);
238 $ufField->is_required
= CRM_Utils_Array
::value('is_required', $params, FALSE);
239 $ufField->is_active
= CRM_Utils_Array
::value('is_active', $params, FALSE);
240 $ufField->in_selector
= CRM_Utils_Array
::value('in_selector', $params, FALSE);
241 $ufField->is_view
= CRM_Utils_Array
::value('is_view', $params, FALSE);
242 $ufField->is_registration
= CRM_Utils_Array
::value('is_registration', $params, FALSE);
243 $ufField->is_match
= CRM_Utils_Array
::value('is_match', $params, FALSE);
244 $ufField->is_searchable
= CRM_Utils_Array
::value('is_searchable', $params, FALSE);
245 $ufField->is_multi_summary
= CRM_Utils_Array
::value('is_multi_summary', $params, FALSE);
246 $ufField->weight
= CRM_Utils_Array
::value('weight', $params, 0);
248 // need the FKEY - uf group id
249 $ufField->uf_group_id
= CRM_Utils_Array
::value('uf_group', $ids, FALSE);
250 $ufField->id
= CRM_Utils_Array
::value('uf_field', $ids, FALSE);
252 return $ufField->save();
256 * Automatically determine one weight and modify others
258 * @param array $params
259 * UFField record, e.g. with 'weight', 'uf_group_id', and 'field_id'.
262 public static function autoWeight($params) {
266 if (!empty($params['field_id'])) {
267 $oldWeight = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFField', $params['field_id'], 'weight', 'id');
269 $fieldValues = array('uf_group_id' => $params['group_id']);
270 return CRM_Utils_Weight
::updateOtherWeights('CRM_Core_DAO_UFField', $oldWeight, CRM_Utils_Array
::value('weight', $params, 0), $fieldValues);
274 * Enable/disable profile field given a custom field id
276 * @param int $customFieldId
278 * @param bool $is_active
279 * Set the is_active field.
284 public static function setUFField($customFieldId, $is_active) {
285 //find the profile id given custom field
286 $ufField = new CRM_Core_DAO_UFField();
287 $ufField->field_name
= "custom_" . $customFieldId;
290 while ($ufField->fetch()) {
291 //enable/ disable profile
292 CRM_Core_BAO_UFField
::setIsActive($ufField->id
, $is_active);
297 * Copy existing profile fields to
298 * new profile from the already built profile
301 * From which we need to copy.
302 * @param bool $new_id
308 public static function copy($old_id, $new_id) {
309 $ufField = new CRM_Core_DAO_UFField();
310 $ufField->uf_group_id
= $old_id;
312 while ($ufField->fetch()) {
313 //copy the field records as it is on new ufgroup id
314 $ufField->uf_group_id
= $new_id;
321 * Delete profile field given a custom field
323 * @param int $customFieldId
324 * ID of the custom field to be deleted.
330 public static function delUFField($customFieldId) {
331 //find the profile id given custom field id
332 $ufField = new CRM_Core_DAO_UFField();
333 $ufField->field_name
= "custom_" . $customFieldId;
336 while ($ufField->fetch()) {
337 //enable/ disable profile
338 CRM_Core_BAO_UFField
::del($ufField->id
);
343 * Enable/disable profile field given a custom group id
345 * @param int $customGroupId
347 * @param bool $is_active
348 * Value we want to set the is_active field.
353 public static function setUFFieldStatus($customGroupId, $is_active) {
354 //find the profile id given custom group id
355 $queryString = "SELECT civicrm_custom_field.id as custom_field_id
356 FROM civicrm_custom_field, civicrm_custom_group
357 WHERE civicrm_custom_field.custom_group_id = civicrm_custom_group.id
358 AND civicrm_custom_group.id = %1";
359 $p = array(1 => array($customGroupId, 'Integer'));
360 $dao = CRM_Core_DAO
::executeQuery($queryString, $p);
362 while ($dao->fetch()) {
363 //enable/ disable profile
364 CRM_Core_BAO_UFField
::setUFField($dao->custom_field_id
, $is_active);
369 * Check the status of custom field used in uf fields
371 * @param int $UFFieldId
374 * false if custom field are disabled else true
377 public static function checkUFStatus($UFFieldId) {
378 $fieldName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFField', $UFFieldId, 'field_name');
379 // return if field is not a custom field
380 if (!$customFieldId = CRM_Core_BAO_CustomField
::getKeyID($fieldName)) {
384 $customField = new CRM_Core_DAO_CustomField();
385 $customField->id
= $customFieldId;
386 // if uf field is custom field
387 if ($customField->find(TRUE)) {
388 if (!$customField->is_active
) {
398 * Find out whether given profile group using Activity
399 * Profile fields with contact fields
401 public static function checkContactActivityProfileType($ufGroupId) {
402 $ufGroup = new CRM_Core_DAO_UFGroup();
403 $ufGroup->id
= $ufGroupId;
404 $ufGroup->find(TRUE);
406 return self
::checkContactActivityProfileTypeByGroupType($ufGroup->group_type
);
411 * @param $ufGroupType
414 public static function checkContactActivityProfileTypeByGroupType($ufGroupType) {
415 $profileTypes = array();
417 $typeParts = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $ufGroupType);
418 $profileTypes = explode(',', $typeParts[0]);
421 if (empty($profileTypes)) {
424 $components = array('Contribution', 'Participant', 'Membership');
425 if (!in_array('Activity', $profileTypes)) {
428 elseif (count($profileTypes) == 1) {
432 if ($index = array_search('Contact', $profileTypes)) {
433 unset($profileTypes[$index]);
434 if (count($profileTypes) == 1) {
439 $contactTypes = array('Individual', 'Household', 'Organization');
440 $subTypes = CRM_Contact_BAO_ContactType
::subTypes();
442 $profileTypeComponent = array_intersect($components, $profileTypes);
443 if (!empty($profileTypeComponent) ||
444 count(array_intersect($contactTypes, $profileTypes)) > 1 ||
445 count(array_intersect($subTypes, $profileTypes)) > 1
454 * Find out whether given profile group uses $required
455 * and/or $optional profile types
457 * @param int $ufGroupId
459 * @param array $required
460 * Array of types those are required.
461 * @param array $optional
462 * 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
511 * @return 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 * TODO Why is this function in this class? It seems to be about the UFGroup.
587 public static function getProfileType($ufGroupId, $returnMixType = TRUE, $onlyPure = FALSE, $skipComponentType = FALSE) {
588 $ufGroup = new CRM_Core_DAO_UFGroup();
589 $ufGroup->id
= $ufGroupId;
590 $ufGroup->is_active
= 1;
592 $ufGroup->find(TRUE);
593 return self
::calculateProfileType($ufGroup->group_type
, $returnMixType, $onlyPure, $skipComponentType);
597 * Get the profile type (eg: individual/organization/household)
599 * @param string $ufGroupType
600 * @param bool $returnMixType
601 * This is true, then field type of mix profile field is returned.
602 * @param bool $onlyPure
603 * True if only pure profiles are required.
604 * @param bool $skipComponentType
610 * TODO Why is this function in this class? It seems to be about the UFGroup.
612 public static function calculateProfileType($ufGroupType, $returnMixType = TRUE, $onlyPure = FALSE, $skipComponentType = FALSE) {
614 $contactTypes = array('Contact', 'Individual', 'Household', 'Organization');
615 $subTypes = CRM_Contact_BAO_ContactType
::subTypes();
616 $components = array('Contribution', 'Participant', 'Membership', 'Activity');
618 $profileTypes = array();
620 $typeParts = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $ufGroupType);
621 $profileTypes = explode(',', $typeParts[0]);
625 if (count($profileTypes) == 1) {
626 return $profileTypes[0];
633 //we need to unset Contact
634 if (count($profileTypes) > 1) {
635 $index = array_search('Contact', $profileTypes);
636 if ($index !== FALSE) {
637 unset($profileTypes[$index]);
641 $profileType = $mixProfileType = NULL;
643 // this case handles pure profile
644 if (count($profileTypes) == 1) {
645 $profileType = array_pop($profileTypes);
648 //check the there are any components include in profile
649 $componentCount = array();
650 foreach ($components as $value) {
651 if (in_array($value, $profileTypes)) {
652 $componentCount[] = $value;
656 //check contact type included in profile
657 $contactTypeCount = array();
658 foreach ($contactTypes as $value) {
659 if (in_array($value, $profileTypes)) {
660 $contactTypeCount[] = $value;
664 $subTypeCount = array();
665 foreach ($subTypes as $value) {
666 if (in_array($value, $profileTypes)) {
667 $subTypeCount[] = $value;
670 if (!$skipComponentType && count($componentCount) == 1) {
671 $profileType = $componentCount[0];
673 elseif (count($componentCount) > 1) {
674 $mixProfileType = $componentCount[1];
676 elseif (count($subTypeCount) == 1) {
677 $profileType = $subTypeCount[0];
679 elseif (count($contactTypeCount) == 1) {
680 $profileType = $contactTypeCount[0];
682 elseif (count($subTypeCount) > 1) {
683 // this is mix subtype profiles
684 $mixProfileType = $subTypeCount[1];
686 elseif (count($contactTypeCount) > 1) {
687 // this is mix contact profiles
688 $mixProfileType = $contactTypeCount[1];
692 if ($mixProfileType) {
693 if ($returnMixType) {
694 return $mixProfileType;
706 * Check for mix profiles groups (eg: individual + other contact types)
710 * @return true for mix profile group else false
713 public static function checkProfileGroupType($ctype) {
714 $ufGroup = new CRM_Core_DAO_UFGroup();
718 FROM civicrm_uf_group as ufg, civicrm_uf_join as ufj
719 WHERE ufg.id = ufj.uf_group_id
720 AND ufj.module = 'User Registration'
721 AND ufg.is_active = 1 ";
723 $ufGroup = CRM_Core_DAO
::executeQuery($query);
726 $validProfiles = array('Individual', 'Organization', 'Household', 'Contribution');
727 while ($ufGroup->fetch()) {
728 $profileType = self
::getProfileType($ufGroup->id
);
729 if (in_array($profileType, $validProfiles)) {
732 elseif ($profileType) {
741 * Check for searchable or in selector field for given profile.
743 * @param int $profileID
747 public static function checkSearchableORInSelector($profileID) {
755 From civicrm_uf_field
756 WHERE (in_selector = 1 OR is_searchable = 1)
757 AND uf_group_id = {$profileID}";
759 $ufFields = CRM_Core_DAO
::executeQuery($query);
760 while ($ufFields->fetch()) {
769 * Reset In selector and is searchable values for given $profileID.
771 * @param int $profileID
775 public function resetInSelectorANDSearchable($profileID) {
779 $query = "UPDATE civicrm_uf_field SET in_selector = 0, is_searchable = 0 WHERE uf_group_id = {$profileID}";
780 CRM_Core_DAO
::executeQuery($query);
784 * Add fields to $profileAddressFields as appropriate.
785 * profileAddressFields is assigned to the template to tell it
786 * what fields are in the profile address
787 * that potentially should be copied to the Billing fields
788 * we want to give precedence to
790 * 2) then Primary designated as 'Primary
791 * 3) location_type is primary
792 * 4) if none of these apply then it just uses the first one
794 * as this will be used to
795 * transfer profile address data to billing fields
796 * http://issues.civicrm.org/jira/browse/CRM-5869
799 * Field key - e.g. street_address-Primary, first_name.
800 * @param array $profileAddressFields
801 * Array of profile fields that relate to address fields.
802 * @param array $profileFilter
803 * Filter to apply to profile fields - expected usage is to only fill based on.
804 * the bottom profile per CRM-13726
807 * Can the address block be hidden safe in the knowledge all fields are elsewhere collected (see CRM-15118)
809 public static function assignAddressField($key, &$profileAddressFields, $profileFilter) {
810 $billing_id = CRM_Core_BAO_LocationType
::getBilling();
811 list($prefixName, $index) = CRM_Utils_System
::explode('-', $key, 2);
813 $profileFields = civicrm_api3('uf_field', 'get', array_merge($profileFilter,
816 'return' => 'field_name, is_required',
822 //check for valid fields ( fields that are present in billing block )
823 $validBillingFields = array(
828 'supplemental_address_1',
834 $requiredBillingFields = array_diff($validBillingFields, array('middle_name', 'supplemental_address_1'));
835 $validProfileFields = array();
836 $requiredProfileFields = array();
838 foreach ($profileFields['values'] as $field) {
839 if (in_array($field['field_name'], $validBillingFields)) {
840 $validProfileFields[] = $field['field_name'];
842 if ($field['is_required']) {
843 $requiredProfileFields[] = $field['field_name'];
847 if (!in_array($prefixName, $validProfileFields)) {
851 if (!empty($index) && (
852 // it's empty so we set it OR
853 !CRM_Utils_array
::value($prefixName, $profileAddressFields)
854 //we are dealing with billing id (precedence)
855 ||
$index == $billing_id
856 // we are dealing with primary & billing not set
857 ||
($index == 'Primary' && $profileAddressFields[$prefixName] != $billing_id)
858 ||
($index == CRM_Core_BAO_LocationType
::getDefault()->id
859 && $profileAddressFields[$prefixName] != $billing_id
860 && $profileAddressFields[$prefixName] != 'Primary'
864 $profileAddressFields[$prefixName] = $index;
867 $potentiallyMissingRequiredFields = array_diff($requiredBillingFields, $requiredProfileFields);
868 CRM_Core_Resources
::singleton()
869 ->addSetting(array('billing' => array('billingProfileIsHideable' => empty($potentiallyMissingRequiredFields))));
873 * Get a list of fields which can be added to profiles
875 * @param int $gid : UF group ID
876 * @param array $defaults : Form defaults
877 * @return array, multidimensional; e.g. $result['FieldGroup']['field_name']['label']
880 public static function getAvailableFields($gid = NULL, $defaults = array()) {
882 'Contact' => array(),
883 'Individual' => CRM_Contact_BAO_Contact
::importableFields('Individual', FALSE, FALSE, TRUE, TRUE, TRUE),
884 'Household' => CRM_Contact_BAO_Contact
::importableFields('Household', FALSE, FALSE, TRUE, TRUE, TRUE),
885 'Organization' => CRM_Contact_BAO_Contact
::importableFields('Organization', FALSE, FALSE, TRUE, TRUE, TRUE),
888 // include hook injected fields
889 $fields['Contact'] = array_merge($fields['Contact'], CRM_Contact_BAO_Query_Hook
::singleton()->getFields());
891 // add current employer for individuals
892 $fields['Individual']['current_employer'] = array(
893 'name' => 'organization_name',
894 'title' => ts('Current Employer'),
897 $addressOptions = CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
898 'address_options', TRUE, NULL, TRUE
901 if (!$addressOptions['county']) {
902 unset($fields['Individual']['county'], $fields['Household']['county'], $fields['Organization']['county']);
905 // break out common contact fields array CRM-3037.
906 // from a UI perspective this makes very little sense
907 foreach ($fields['Individual'] as $key => $value) {
908 if (!empty($fields['Household'][$key]) && !empty($fields['Organization'][$key])) {
909 $fields['Contact'][$key] = $value;
910 unset($fields['Individual'][$key], $fields['Household'][$key], $fields['Organization'][$key]);
914 // Internal field not exposed to forms
915 unset($fields['Contact']['contact_type']);
916 unset($fields['Contact']['master_id']);
918 // convert phone extension in to psedo-field phone + phone extension
920 unset($fields['Contact']['phone_ext']);
922 $fields['Contact']['phone_and_ext'] = array(
923 'name' => 'phone_and_ext',
924 'title' => ts('Phone and Extension'),
925 'hasLocationType' => 1,
928 // include Subtypes For Profile
929 $subTypes = CRM_Contact_BAO_ContactType
::subTypeInfo();
930 foreach ($subTypes as $name => $val) {
931 //custom fields for sub type
932 $subTypeFields = CRM_Core_BAO_CustomField
::getFieldsForImport($name, FALSE, FALSE, FALSE, TRUE, TRUE);
933 if (array_key_exists($val['parent'], $fields)) {
934 $fields[$name] = $fields[$val['parent']] +
$subTypeFields;
937 $fields[$name] = $subTypeFields;
941 if (CRM_Core_Permission
::access('CiviContribute')) {
942 $contribFields = CRM_Contribute_BAO_Contribution
::getContributionFields(FALSE);
943 if (!empty($contribFields)) {
944 unset($contribFields['is_test']);
945 unset($contribFields['is_pay_later']);
946 unset($contribFields['contribution_id']);
947 $contribFields['contribution_note'] = array(
948 'name' => 'contribution_note',
949 'title' => ts('Contribution Note'),
951 $fields['Contribution'] = array_merge($contribFields, self
::getContribBatchEntryFields());
955 if (CRM_Core_Permission
::access('CiviEvent')) {
956 $participantFields = CRM_Event_BAO_Query
::getParticipantFields();
957 if ($participantFields) {
958 // Remove fields not supported by profiles
959 CRM_Utils_Array
::remove($participantFields,
960 'external_identifier',
962 'participant_contact_id',
963 'participant_role_id',
964 'participant_status_id',
965 'participant_is_test',
966 'participant_fee_level',
968 'participant_is_pay_later',
969 'participant_campaign'
971 if (isset($participantFields['participant_campaign_id'])) {
972 $participantFields['participant_campaign_id']['title'] = ts('Campaign');
974 $fields['Participant'] = $participantFields;
978 if (CRM_Core_Permission
::access('CiviMember')) {
979 $membershipFields = CRM_Member_BAO_Membership
::getMembershipFields();
980 // Remove fields not supported by profiles
981 CRM_Utils_Array
::remove($membershipFields,
983 'membership_type_id',
987 'member_is_pay_later'
989 if ($gid && CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $gid, 'name') == 'membership_batch_entry') {
990 $fields['Membership'] = array_merge($membershipFields, self
::getMemberBatchEntryFields());
993 $fields['Membership'] = $membershipFields;
997 if (CRM_Core_Permission
::access('CiviCase')) {
998 $caseFields = CRM_Case_BAO_Query
::getFields(TRUE);
999 $caseFields = array_merge($caseFields, CRM_Core_BAO_CustomField
::getFieldsForImport('Case'));
1001 // Remove fields not supported by profiles
1002 CRM_Utils_Array
::remove($caseFields,
1012 $fields['Case'] = $caseFields;
1015 $activityFields = CRM_Activity_BAO_Activity
::getProfileFields();
1016 if ($activityFields) {
1017 // campaign related fields.
1018 if (isset($activityFields['activity_campaign_id'])) {
1019 $activityFields['activity_campaign_id']['title'] = ts('Campaign');
1021 $fields['Activity'] = $activityFields;
1024 $fields['Formatting']['format_free_html_' . rand(1000, 9999)] = array(
1025 'name' => 'free_html',
1028 'title' => 'Free HTML',
1032 foreach ($fields as &$values) {
1033 $values = CRM_Utils_Array
::crmArraySortByField($values, 'title');
1036 //group selected and unwanted fields list
1037 $ufFields = $gid ? CRM_Core_BAO_UFGroup
::getFields($gid, FALSE, NULL, NULL, NULL, TRUE, NULL, TRUE) : array();
1038 $groupFieldList = array_merge($ufFields, array(
1040 'email_greeting_custom',
1041 'postal_greeting_custom',
1045 //unset selected fields
1046 foreach ($groupFieldList as $key => $value) {
1047 if (is_integer($key)) {
1048 unset($fields['Individual'][$value], $fields['Household'][$value], $fields['Organization'][$value]);
1051 if (!empty($defaults['field_name'])
1052 && $defaults['field_name']['0'] == $value['field_type']
1053 && $defaults['field_name']['1'] == $key
1057 unset($fields[$value['field_type']][$key]);
1064 * Get a list of fields which can be added to profiles
1066 * @param bool $force
1068 * @return array, multidimensional; e.g. $result['field_name']['label']
1071 public static function getAvailableFieldsFlat($force = FALSE) {
1072 // FIXME reset when data model changes
1073 static $result = NULL;
1074 if ($result === NULL ||
$force) {
1075 $fieldTree = self
::getAvailableFields();
1077 foreach ($fieldTree as $field_type => $fields) {
1078 foreach ($fields as $field_name => $field) {
1079 if (!isset($result[$field_name])) {
1080 $field['field_type'] = $field_type;
1081 $result[$field_name] = $field;
1090 * Determine whether the given field_name is valid
1092 * @param string $fieldName
1095 public static function isValidFieldName($fieldName) {
1096 $availableFields = CRM_Core_BAO_UFField
::getAvailableFieldsFlat();
1097 return isset($availableFields[$fieldName]);
1101 * @return array|null
1103 public static function getContribBatchEntryFields() {
1104 if (self
::$_contriBatchEntryFields === NULL) {
1105 self
::$_contriBatchEntryFields = array(
1106 'send_receipt' => array(
1107 'name' => 'send_receipt',
1108 'title' => ts('Send Receipt'),
1110 'soft_credit' => array(
1111 'name' => 'soft_credit',
1112 'title' => ts('Soft Credit'),
1114 'soft_credit_type' => array(
1115 'name' => 'soft_credit_type',
1116 'title' => ts('Soft Credit Type'),
1118 'product_name' => array(
1119 'name' => 'product_name',
1120 'title' => ts('Premiums'),
1122 'contribution_note' => array(
1123 'name' => 'contribution_note',
1124 'title' => ts('Contribution Note'),
1128 return self
::$_contriBatchEntryFields;
1132 * @return array|null
1134 public static function getMemberBatchEntryFields() {
1135 if (self
::$_memberBatchEntryFields === NULL) {
1136 self
::$_memberBatchEntryFields = array(
1137 'send_receipt' => array(
1138 'name' => 'send_receipt',
1139 'title' => ts('Send Receipt'),
1141 'soft_credit' => array(
1142 'name' => 'soft_credit',
1143 'title' => ts('Soft Credit'),
1145 'product_name' => array(
1146 'name' => 'product_name',
1147 'title' => ts('Premiums'),
1149 'financial_type' => array(
1150 'name' => 'financial_type',
1151 'title' => ts('Financial Type'),
1153 'total_amount' => array(
1154 'name' => 'total_amount',
1155 'title' => ts('Total Amount'),
1157 'receive_date' => array(
1158 'name' => 'receive_date',
1159 'title' => ts('Receive Date'),
1161 'payment_instrument' => array(
1162 'name' => 'payment_instrument',
1163 'title' => ts('Payment Instrument'),
1165 'contribution_status_id' => array(
1166 'name' => 'contribution_status_id',
1167 'title' => ts('Contribution Status'),
1171 return self
::$_memberBatchEntryFields;