3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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;
47 * Fetch object based on array of properties.
49 * @param array $params
50 * (reference ) an assoc array of name/value pairs.
51 * @param array $defaults
52 * (reference ) an assoc array to hold the flattened values.
54 * @return CRM_Core_BAO_UFField
56 public static function retrieve(&$params, &$defaults) {
57 return CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_UFField', $params, $defaults);
70 public static function getTitle($id) {
71 return CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFField', $groupId, 'title');
75 * Update the is_active flag in the db.
78 * Id of the database record.
79 * @param bool $is_active
80 * Value we want to set the is_active field.
83 * DAO object on success, null otherwise
85 public static function setIsActive($id, $is_active) {
86 //check if custom data profile field is disabled
88 if (CRM_Core_BAO_UFField
::checkUFStatus($id)) {
89 return CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_UFField', $id, 'is_active', $is_active);
92 CRM_Core_Session
::setStatus(ts('Cannot enable this UF field since the used custom field is disabled.'), ts('Check Custom Field'), 'error');
96 return CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_UFField', $id, 'is_active', $is_active);
101 * Delete the profile Field.
109 public static function del($id) {
111 $field = new CRM_Core_DAO_UFField();
118 * Check duplicate for duplicate field in a group.
120 * @param array $params
121 * An associative array with field and values.
125 * @ids array $ids array that containd ids
128 public static function duplicateField($params, $ids) {
129 $ufField = new CRM_Core_DAO_UFField();
130 $ufField->uf_group_id
= CRM_Utils_Array
::value('uf_group', $ids);
131 $ufField->field_type
= $params['field_name'][0];
132 $ufField->field_name
= $params['field_name'][1];
133 if ($params['field_name'][1] == 'url') {
134 $ufField->website_type_id
= CRM_Utils_Array
::value(2, $params['field_name'], NULL);
137 $ufField->location_type_id
= (CRM_Utils_Array
::value(2, $params['field_name'])) ?
$params['field_name'][2] : 'NULL';
139 $ufField->phone_type_id
= CRM_Utils_Array
::value(3, $params['field_name']);
141 if (!empty($ids['uf_field'])) {
142 $ufField->whereAdd("id <> " . CRM_Utils_Array
::value('uf_field', $ids));
145 return $ufField->find(TRUE);
149 * Does profile consists of a multi-record custom field
151 public static function checkMultiRecordFieldExists($gId) {
152 $queryString = "SELECT f.field_name
153 FROM civicrm_uf_field f, civicrm_uf_group g
154 WHERE f.uf_group_id = g.id
155 AND g.id = %1 AND f.field_name LIKE 'custom%'";
156 $p = array(1 => array($gId, 'Integer'));
157 $dao = CRM_Core_DAO
::executeQuery($queryString, $p);
158 $customFieldIds = array();
159 $isMultiRecordFieldPresent = FALSE;
160 while ($dao->fetch()) {
161 if ($customId = CRM_Core_BAO_CustomField
::getKeyID($dao->field_name
)) {
162 if (is_numeric($customId)) {
163 $customFieldIds[] = $customId;
168 if (!empty($customFieldIds) && count($customFieldIds) == 1) {
169 $customFieldId = array_pop($customFieldIds);
170 $isMultiRecordFieldPresent = CRM_Core_BAO_CustomField
::isMultiRecordField($customFieldId);
172 elseif (count($customFieldIds) > 1) {
173 $customFieldIds = implode(", ", $customFieldIds);
176 FROM civicrm_custom_group cg
177 INNER JOIN civicrm_custom_field cf
178 ON cg.id = cf.custom_group_id
179 WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1";
181 $dao = CRM_Core_DAO
::executeQuery($queryString);
183 $isMultiRecordFieldPresent = ($dao->cgId
) ?
$dao->cgId
: FALSE;
187 return $isMultiRecordFieldPresent;
193 * @param array $params
194 * (reference) array containing the values submitted by the form.
196 * Array containing the id.
198 * @return CRM_Core_BAO_UFField
201 public static function add(&$params, $ids = array()) {
202 // set values for uf field properties and save
203 $ufField = new CRM_Core_DAO_UFField();
204 $ufField->field_type
= $params['field_name'][0];
205 $ufField->field_name
= $params['field_name'][1];
207 //should not set location type id for Primary
208 $locationTypeId = NULL;
209 if ($params['field_name'][1] == 'url') {
210 $ufField->website_type_id
= CRM_Utils_Array
::value(2, $params['field_name']);
213 $locationTypeId = CRM_Utils_Array
::value(2, $params['field_name']);
214 $ufField->website_type_id
= NULL;
216 if ($locationTypeId) {
217 $ufField->location_type_id
= $locationTypeId;
220 $ufField->location_type_id
= 'null';
223 $ufField->phone_type_id
= CRM_Utils_Array
::value(3, $params['field_name'], 'NULL');
224 $ufField->listings_title
= CRM_Utils_Array
::value('listings_title', $params);
225 $ufField->visibility
= CRM_Utils_Array
::value('visibility', $params);
226 $ufField->help_pre
= CRM_Utils_Array
::value('help_pre', $params);
227 $ufField->help_post
= CRM_Utils_Array
::value('help_post', $params);
228 $ufField->label
= CRM_Utils_Array
::value('label', $params);
229 $ufField->is_required
= CRM_Utils_Array
::value('is_required', $params, FALSE);
230 $ufField->is_active
= CRM_Utils_Array
::value('is_active', $params, FALSE);
231 $ufField->in_selector
= CRM_Utils_Array
::value('in_selector', $params, FALSE);
232 $ufField->is_view
= CRM_Utils_Array
::value('is_view', $params, FALSE);
233 $ufField->is_registration
= CRM_Utils_Array
::value('is_registration', $params, FALSE);
234 $ufField->is_match
= CRM_Utils_Array
::value('is_match', $params, FALSE);
235 $ufField->is_searchable
= CRM_Utils_Array
::value('is_searchable', $params, FALSE);
236 $ufField->is_multi_summary
= CRM_Utils_Array
::value('is_multi_summary', $params, FALSE);
237 $ufField->weight
= CRM_Utils_Array
::value('weight', $params, 0);
239 // need the FKEY - uf group id
240 $ufField->uf_group_id
= CRM_Utils_Array
::value('uf_group', $ids, FALSE);
241 $ufField->id
= CRM_Utils_Array
::value('uf_field', $ids, FALSE);
243 return $ufField->save();
247 * Automatically determine one weight and modify others.
249 * @param array $params
250 * UFField record, e.g. with 'weight', 'uf_group_id', and 'field_id'.
253 public static function autoWeight($params) {
257 if (!empty($params['field_id'])) {
258 $oldWeight = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFField', $params['field_id'], 'weight', 'id');
260 $fieldValues = array('uf_group_id' => $params['group_id']);
261 return CRM_Utils_Weight
::updateOtherWeights('CRM_Core_DAO_UFField', $oldWeight, CRM_Utils_Array
::value('weight', $params, 0), $fieldValues);
265 * Enable/disable profile field given a custom field id
267 * @param int $customFieldId
269 * @param bool $is_active
270 * Set the is_active field.
272 public static function setUFField($customFieldId, $is_active) {
273 //find the profile id given custom field
274 $ufField = new CRM_Core_DAO_UFField();
275 $ufField->field_name
= "custom_" . $customFieldId;
278 while ($ufField->fetch()) {
279 //enable/ disable profile
280 CRM_Core_BAO_UFField
::setIsActive($ufField->id
, $is_active);
285 * Copy existing profile fields to
286 * new profile from the already built profile
289 * From which we need to copy.
290 * @param bool $new_id
293 public static function copy($old_id, $new_id) {
294 $ufField = new CRM_Core_DAO_UFField();
295 $ufField->uf_group_id
= $old_id;
297 while ($ufField->fetch()) {
298 //copy the field records as it is on new ufgroup id
299 $ufField->uf_group_id
= $new_id;
306 * Delete profile field given a custom field.
308 * @param int $customFieldId
309 * ID of the custom field to be deleted.
311 public static function delUFField($customFieldId) {
312 //find the profile id given custom field id
313 $ufField = new CRM_Core_DAO_UFField();
314 $ufField->field_name
= "custom_" . $customFieldId;
317 while ($ufField->fetch()) {
318 //enable/ disable profile
319 CRM_Core_BAO_UFField
::del($ufField->id
);
324 * Enable/disable profile field given a custom group id
326 * @param int $customGroupId
328 * @param bool $is_active
329 * Value we want to set the is_active field.
331 public static function setUFFieldStatus($customGroupId, $is_active) {
332 //find the profile id given custom group id
333 $queryString = "SELECT civicrm_custom_field.id as custom_field_id
334 FROM civicrm_custom_field, civicrm_custom_group
335 WHERE civicrm_custom_field.custom_group_id = civicrm_custom_group.id
336 AND civicrm_custom_group.id = %1";
337 $p = array(1 => array($customGroupId, 'Integer'));
338 $dao = CRM_Core_DAO
::executeQuery($queryString, $p);
340 while ($dao->fetch()) {
341 //enable/ disable profile
342 CRM_Core_BAO_UFField
::setUFField($dao->custom_field_id
, $is_active);
347 * Check the status of custom field used in uf fields.
349 * @param int $UFFieldId
352 * false if custom field are disabled else true
354 public static function checkUFStatus($UFFieldId) {
355 $fieldName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFField', $UFFieldId, 'field_name');
356 // return if field is not a custom field
357 if (!$customFieldId = CRM_Core_BAO_CustomField
::getKeyID($fieldName)) {
361 $customField = new CRM_Core_DAO_CustomField();
362 $customField->id
= $customFieldId;
363 // if uf field is custom field
364 if ($customField->find(TRUE)) {
365 if (!$customField->is_active
) {
375 * Find out whether given profile group using Activity
376 * Profile fields with contact fields
378 public static function checkContactActivityProfileType($ufGroupId) {
379 $ufGroup = new CRM_Core_DAO_UFGroup();
380 $ufGroup->id
= $ufGroupId;
381 $ufGroup->find(TRUE);
383 return self
::checkContactActivityProfileTypeByGroupType($ufGroup->group_type
);
388 * @param $ufGroupType
391 public static function checkContactActivityProfileTypeByGroupType($ufGroupType) {
392 $profileTypes = array();
394 $typeParts = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $ufGroupType);
395 $profileTypes = explode(',', $typeParts[0]);
398 if (empty($profileTypes)) {
401 $components = array('Contribution', 'Participant', 'Membership');
402 if (!in_array('Activity', $profileTypes)) {
405 elseif (count($profileTypes) == 1) {
409 if ($index = array_search('Contact', $profileTypes)) {
410 unset($profileTypes[$index]);
411 if (count($profileTypes) == 1) {
416 $contactTypes = array('Individual', 'Household', 'Organization');
417 $subTypes = CRM_Contact_BAO_ContactType
::subTypes();
419 $profileTypeComponent = array_intersect($components, $profileTypes);
420 if (!empty($profileTypeComponent) ||
421 count(array_intersect($contactTypes, $profileTypes)) > 1 ||
422 count(array_intersect($subTypes, $profileTypes)) > 1
431 * Find out whether given profile group uses $required
432 * and/or $optional profile types
434 * @param int $ufGroupId
436 * @param array $required
437 * Array of types those are required.
438 * @param array $optional
439 * Array of types those are optional.
443 public static function checkValidProfileType($ufGroupId, $required, $optional = NULL) {
444 if (!is_array($required) ||
empty($required)) {
448 $ufGroup = new CRM_Core_DAO_UFGroup();
449 $ufGroup->id
= $ufGroupId;
450 $ufGroup->find(TRUE);
452 $profileTypes = array();
453 if ($ufGroup->group_type
) {
454 $typeParts = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $ufGroup->group_type
);
455 $profileTypes = explode(',', $typeParts[0]);
458 if (empty($profileTypes)) {
463 foreach ($required as $key => $val) {
464 if (!in_array($val, $profileTypes)) {
470 if ($valid && is_array($optional)) {
471 foreach ($optional as $key => $val) {
472 if (in_array($val, $profileTypes)) {
483 * Check for mix profile fields (eg: individual + other contact types)
485 * @param int $ufGroupId
488 * true for mix profile else false
490 public static function checkProfileType($ufGroupId) {
491 $ufGroup = new CRM_Core_DAO_UFGroup();
492 $ufGroup->id
= $ufGroupId;
493 $ufGroup->find(TRUE);
495 $profileTypes = array();
496 if ($ufGroup->group_type
) {
497 $typeParts = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $ufGroup->group_type
);
498 $profileTypes = explode(',', $typeParts[0]);
501 //early return if new profile.
502 if (empty($profileTypes)) {
506 //we need to unset Contact
507 if (count($profileTypes) > 1) {
508 $index = array_search('Contact', $profileTypes);
509 if ($index !== FALSE) {
510 unset($profileTypes[$index]);
514 // suppress any subtypes if present
515 CRM_Contact_BAO_ContactType
::suppressSubTypes($profileTypes);
517 $contactTypes = array('Contact', 'Individual', 'Household', 'Organization');
518 $components = array('Contribution', 'Participant', 'Membership', 'Activity');
521 // check for mix profile condition
522 if (count($profileTypes) > 1) {
523 //check the there are any components include in profile
524 foreach ($components as $value) {
525 if (in_array($value, $profileTypes)) {
529 //check if there are more than one contact types included in profile
530 if (count($profileTypes) > 1) {
534 elseif (count($profileTypes) == 1) {
535 // note for subtype case count would be zero
536 $profileTypes = array_values($profileTypes);
537 if (!in_array($profileTypes[0], $contactTypes)) {
546 * Get the profile type (eg: individual/organization/household)
548 * @param int $ufGroupId
550 * @param bool $returnMixType
551 * This is true, then field type of mix profile field is returned.
552 * @param bool $onlyPure
553 * True if only pure profiles are required.
555 * @param bool $skipComponentType
561 public static function getProfileType($ufGroupId, $returnMixType = TRUE, $onlyPure = FALSE, $skipComponentType = FALSE) {
562 $ufGroup = new CRM_Core_DAO_UFGroup();
563 $ufGroup->id
= $ufGroupId;
564 $ufGroup->is_active
= 1;
566 $ufGroup->find(TRUE);
567 return self
::calculateProfileType($ufGroup->group_type
, $returnMixType, $onlyPure, $skipComponentType);
571 * Get the profile type (eg: individual/organization/household)
573 * @param string $ufGroupType
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.
578 * @param bool $skipComponentType
580 * @return string profile group_type
583 public static function calculateProfileType($ufGroupType, $returnMixType = TRUE, $onlyPure = FALSE, $skipComponentType = FALSE) {
585 $contactTypes = array('Contact', 'Individual', 'Household', 'Organization');
586 $subTypes = CRM_Contact_BAO_ContactType
::subTypes();
587 $components = array('Contribution', 'Participant', 'Membership', 'Activity');
589 $profileTypes = array();
591 $typeParts = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $ufGroupType);
592 $profileTypes = explode(',', $typeParts[0]);
596 if (count($profileTypes) == 1) {
597 return $profileTypes[0];
604 //we need to unset Contact
605 if (count($profileTypes) > 1) {
606 $index = array_search('Contact', $profileTypes);
607 if ($index !== FALSE) {
608 unset($profileTypes[$index]);
612 $profileType = $mixProfileType = NULL;
614 // this case handles pure profile
615 if (count($profileTypes) == 1) {
616 $profileType = array_pop($profileTypes);
619 //check the there are any components include in profile
620 $componentCount = array();
621 foreach ($components as $value) {
622 if (in_array($value, $profileTypes)) {
623 $componentCount[] = $value;
627 //check contact type included in profile
628 $contactTypeCount = array();
629 foreach ($contactTypes as $value) {
630 if (in_array($value, $profileTypes)) {
631 $contactTypeCount[] = $value;
635 $subTypeCount = array();
636 foreach ($subTypes as $value) {
637 if (in_array($value, $profileTypes)) {
638 $subTypeCount[] = $value;
641 if (!$skipComponentType && count($componentCount) == 1) {
642 $profileType = $componentCount[0];
644 elseif (count($componentCount) > 1) {
645 $mixProfileType = $componentCount[1];
647 elseif (count($subTypeCount) == 1) {
648 $profileType = $subTypeCount[0];
650 elseif (count($contactTypeCount) == 1) {
651 $profileType = $contactTypeCount[0];
653 elseif (count($subTypeCount) > 1) {
654 // this is mix subtype profiles
655 $mixProfileType = $subTypeCount[1];
657 elseif (count($contactTypeCount) > 1) {
658 // this is mix contact profiles
659 $mixProfileType = $contactTypeCount[1];
663 if ($mixProfileType) {
664 if ($returnMixType) {
665 return $mixProfileType;
677 * Check for mix profiles groups (eg: individual + other contact types)
682 * true for mix profile group else false
684 public static function checkProfileGroupType($ctype) {
685 $ufGroup = new CRM_Core_DAO_UFGroup();
689 FROM civicrm_uf_group as ufg, civicrm_uf_join as ufj
690 WHERE ufg.id = ufj.uf_group_id
691 AND ufj.module = 'User Registration'
692 AND ufg.is_active = 1 ";
694 $ufGroup = CRM_Core_DAO
::executeQuery($query);
697 $validProfiles = array('Individual', 'Organization', 'Household', 'Contribution');
698 while ($ufGroup->fetch()) {
699 $profileType = self
::getProfileType($ufGroup->id
);
700 if (in_array($profileType, $validProfiles)) {
703 elseif ($profileType) {
712 * Check for searchable or in selector field for given profile.
714 * @param int $profileID
718 public static function checkSearchableORInSelector($profileID) {
726 From civicrm_uf_field
727 WHERE (in_selector = 1 OR is_searchable = 1)
728 AND uf_group_id = {$profileID}";
730 $ufFields = CRM_Core_DAO
::executeQuery($query);
731 while ($ufFields->fetch()) {
740 * Reset In selector and is searchable values for given $profileID.
742 * @param int $profileID
744 public function resetInSelectorANDSearchable($profileID) {
748 $query = "UPDATE civicrm_uf_field SET in_selector = 0, is_searchable = 0 WHERE uf_group_id = {$profileID}";
749 CRM_Core_DAO
::executeQuery($query);
753 * Add fields to $profileAddressFields as appropriate.
754 * profileAddressFields is assigned to the template to tell it
755 * what fields are in the profile address
756 * that potentially should be copied to the Billing fields
757 * we want to give precedence to
759 * 2) then Primary designated as 'Primary
760 * 3) location_type is primary
761 * 4) if none of these apply then it just uses the first one
763 * as this will be used to
764 * transfer profile address data to billing fields
765 * http://issues.civicrm.org/jira/browse/CRM-5869
768 * Field key - e.g. street_address-Primary, first_name.
769 * @param array $profileAddressFields
770 * Array of profile fields that relate to address fields.
771 * @param array $profileFilter
772 * Filter to apply to profile fields - expected usage is to only fill based on.
773 * the bottom profile per CRM-13726
776 * Can the address block be hidden safe in the knowledge all fields are elsewhere collected (see CRM-15118)
778 public static function assignAddressField($key, &$profileAddressFields, $profileFilter) {
779 $billing_id = CRM_Core_BAO_LocationType
::getBilling();
780 list($prefixName, $index) = CRM_Utils_System
::explode('-', $key, 2);
782 $profileFields = civicrm_api3('uf_field', 'get', array_merge($profileFilter,
785 'return' => 'field_name, is_required',
791 //check for valid fields ( fields that are present in billing block )
792 $validBillingFields = array(
797 'supplemental_address_1',
803 $requiredBillingFields = array_diff($validBillingFields, array('middle_name', 'supplemental_address_1'));
804 $validProfileFields = array();
805 $requiredProfileFields = array();
807 foreach ($profileFields['values'] as $field) {
808 if (in_array($field['field_name'], $validBillingFields)) {
809 $validProfileFields[] = $field['field_name'];
811 if ($field['is_required']) {
812 $requiredProfileFields[] = $field['field_name'];
816 if (!in_array($prefixName, $validProfileFields)) {
820 if (!empty($index) && (
821 // it's empty so we set it OR
822 !CRM_Utils_array
::value($prefixName, $profileAddressFields)
823 //we are dealing with billing id (precedence)
824 ||
$index == $billing_id
825 // we are dealing with primary & billing not set
826 ||
($index == 'Primary' && $profileAddressFields[$prefixName] != $billing_id)
827 ||
($index == CRM_Core_BAO_LocationType
::getDefault()->id
828 && $profileAddressFields[$prefixName] != $billing_id
829 && $profileAddressFields[$prefixName] != 'Primary'
833 $profileAddressFields[$prefixName] = $index;
836 $potentiallyMissingRequiredFields = array_diff($requiredBillingFields, $requiredProfileFields);
837 CRM_Core_Resources
::singleton()
838 ->addSetting(array('billing' => array('billingProfileIsHideable' => empty($potentiallyMissingRequiredFields))));
842 * Get a list of fields which can be added to profiles.
844 * @param int $gid : UF group ID
845 * @param array $defaults : Form defaults
846 * @return array, multidimensional; e.g. $result['FieldGroup']['field_name']['label']
848 public static function getAvailableFields($gid = NULL, $defaults = array()) {
850 'Contact' => array(),
851 'Individual' => CRM_Contact_BAO_Contact
::importableFields('Individual', FALSE, FALSE, TRUE, TRUE, TRUE),
852 'Household' => CRM_Contact_BAO_Contact
::importableFields('Household', FALSE, FALSE, TRUE, TRUE, TRUE),
853 'Organization' => CRM_Contact_BAO_Contact
::importableFields('Organization', FALSE, FALSE, TRUE, TRUE, TRUE),
856 // include hook injected fields
857 $fields['Contact'] = array_merge($fields['Contact'], CRM_Contact_BAO_Query_Hook
::singleton()->getFields());
859 // add current employer for individuals
860 $fields['Individual']['current_employer'] = array(
861 'name' => 'organization_name',
862 'title' => ts('Current Employer'),
865 $addressOptions = CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
866 'address_options', TRUE, NULL, TRUE
869 if (!$addressOptions['county']) {
870 unset($fields['Individual']['county'], $fields['Household']['county'], $fields['Organization']['county']);
873 // break out common contact fields array CRM-3037.
874 // from a UI perspective this makes very little sense
875 foreach ($fields['Individual'] as $key => $value) {
876 if (!empty($fields['Household'][$key]) && !empty($fields['Organization'][$key])) {
877 $fields['Contact'][$key] = $value;
878 unset($fields['Individual'][$key], $fields['Household'][$key], $fields['Organization'][$key]);
882 // Internal field not exposed to forms
883 unset($fields['Contact']['contact_type']);
884 unset($fields['Contact']['master_id']);
886 // convert phone extension in to psedo-field phone + phone extension
888 unset($fields['Contact']['phone_ext']);
890 $fields['Contact']['phone_and_ext'] = array(
891 'name' => 'phone_and_ext',
892 'title' => ts('Phone and Extension'),
893 'hasLocationType' => 1,
896 // include Subtypes For Profile
897 $subTypes = CRM_Contact_BAO_ContactType
::subTypeInfo();
898 foreach ($subTypes as $name => $val) {
899 //custom fields for sub type
900 $subTypeFields = CRM_Core_BAO_CustomField
::getFieldsForImport($name, FALSE, FALSE, FALSE, TRUE, TRUE);
901 if (array_key_exists($val['parent'], $fields)) {
902 $fields[$name] = $fields[$val['parent']] +
$subTypeFields;
905 $fields[$name] = $subTypeFields;
909 if (CRM_Core_Permission
::access('CiviContribute')) {
910 $contribFields = CRM_Contribute_BAO_Contribution
::getContributionFields(FALSE);
911 if (!empty($contribFields)) {
912 unset($contribFields['is_test']);
913 unset($contribFields['is_pay_later']);
914 unset($contribFields['contribution_id']);
915 $contribFields['contribution_note'] = array(
916 'name' => 'contribution_note',
917 'title' => ts('Contribution Note'),
919 $fields['Contribution'] = array_merge($contribFields, self
::getContribBatchEntryFields());
923 if (CRM_Core_Permission
::access('CiviEvent')) {
924 $participantFields = CRM_Event_BAO_Query
::getParticipantFields();
925 if ($participantFields) {
926 // Remove fields not supported by profiles
927 CRM_Utils_Array
::remove($participantFields,
928 'external_identifier',
930 'participant_contact_id',
931 'participant_role_id',
932 'participant_status_id',
933 'participant_is_test',
934 'participant_fee_level',
936 'participant_is_pay_later',
937 'participant_campaign'
939 if (isset($participantFields['participant_campaign_id'])) {
940 $participantFields['participant_campaign_id']['title'] = ts('Campaign');
942 $fields['Participant'] = $participantFields;
946 if (CRM_Core_Permission
::access('CiviMember')) {
947 $membershipFields = CRM_Member_BAO_Membership
::getMembershipFields();
948 // Remove fields not supported by profiles
949 CRM_Utils_Array
::remove($membershipFields,
951 'membership_type_id',
955 'member_is_pay_later'
957 if ($gid && CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $gid, 'name') == 'membership_batch_entry') {
958 $fields['Membership'] = array_merge($membershipFields, self
::getMemberBatchEntryFields());
961 $fields['Membership'] = $membershipFields;
965 if (CRM_Core_Permission
::access('CiviCase')) {
966 $caseFields = CRM_Case_BAO_Query
::getFields(TRUE);
967 $caseFields = array_merge($caseFields, CRM_Core_BAO_CustomField
::getFieldsForImport('Case'));
969 // Remove fields not supported by profiles
970 CRM_Utils_Array
::remove($caseFields,
980 $fields['Case'] = $caseFields;
983 $activityFields = CRM_Activity_BAO_Activity
::getProfileFields();
984 if ($activityFields) {
985 // campaign related fields.
986 if (isset($activityFields['activity_campaign_id'])) {
987 $activityFields['activity_campaign_id']['title'] = ts('Campaign');
989 $fields['Activity'] = $activityFields;
992 $fields['Formatting']['format_free_html_' . rand(1000, 9999)] = array(
993 'name' => 'free_html',
996 'title' => 'Free HTML',
1000 foreach ($fields as &$values) {
1001 $values = CRM_Utils_Array
::crmArraySortByField($values, 'title');
1004 //group selected and unwanted fields list
1005 $ufFields = $gid ? CRM_Core_BAO_UFGroup
::getFields($gid, FALSE, NULL, NULL, NULL, TRUE, NULL, TRUE) : array();
1006 $groupFieldList = array_merge($ufFields, array(
1008 'email_greeting_custom',
1009 'postal_greeting_custom',
1013 //unset selected fields
1014 foreach ($groupFieldList as $key => $value) {
1016 unset($fields['Individual'][$value], $fields['Household'][$value], $fields['Organization'][$value]);
1019 if (!empty($defaults['field_name'])
1020 && $defaults['field_name']['0'] == $value['field_type']
1021 && $defaults['field_name']['1'] == $key
1025 unset($fields[$value['field_type']][$key]);
1032 * Get a list of fields which can be added to profiles.
1034 * @param bool $force
1036 * @return array, multidimensional; e.g. $result['field_name']['label']
1038 public static function getAvailableFieldsFlat($force = FALSE) {
1039 // FIXME reset when data model changes
1040 static $result = NULL;
1041 if ($result === NULL ||
$force) {
1042 $fieldTree = self
::getAvailableFields();
1044 foreach ($fieldTree as $field_type => $fields) {
1045 foreach ($fields as $field_name => $field) {
1046 if (!isset($result[$field_name])) {
1047 $field['field_type'] = $field_type;
1048 $result[$field_name] = $field;
1057 * Determine whether the given field_name is valid.
1059 * @param string $fieldName
1062 public static function isValidFieldName($fieldName) {
1063 $availableFields = CRM_Core_BAO_UFField
::getAvailableFieldsFlat();
1064 return isset($availableFields[$fieldName]);
1068 * @return array|null
1070 public static function getContribBatchEntryFields() {
1071 if (self
::$_contriBatchEntryFields === NULL) {
1072 self
::$_contriBatchEntryFields = array(
1073 'send_receipt' => array(
1074 'name' => 'send_receipt',
1075 'title' => ts('Send Receipt'),
1077 'soft_credit' => array(
1078 'name' => 'soft_credit',
1079 'title' => ts('Soft Credit'),
1081 'soft_credit_type' => array(
1082 'name' => 'soft_credit_type',
1083 'title' => ts('Soft Credit Type'),
1085 'product_name' => array(
1086 'name' => 'product_name',
1087 'title' => ts('Premiums'),
1089 'contribution_note' => array(
1090 'name' => 'contribution_note',
1091 'title' => ts('Contribution Note'),
1095 return self
::$_contriBatchEntryFields;
1099 * @return array|null
1101 public static function getMemberBatchEntryFields() {
1102 if (self
::$_memberBatchEntryFields === NULL) {
1103 self
::$_memberBatchEntryFields = array(
1104 'send_receipt' => array(
1105 'name' => 'send_receipt',
1106 'title' => ts('Send Receipt'),
1108 'soft_credit' => array(
1109 'name' => 'soft_credit',
1110 'title' => ts('Soft Credit'),
1112 'product_name' => array(
1113 'name' => 'product_name',
1114 'title' => ts('Premiums'),
1116 'financial_type' => array(
1117 'name' => 'financial_type',
1118 'title' => ts('Financial Type'),
1120 'total_amount' => array(
1121 'name' => 'total_amount',
1122 'title' => ts('Total Amount'),
1124 'receive_date' => array(
1125 'name' => 'receive_date',
1126 'title' => ts('Date Received'),
1128 'payment_instrument' => array(
1129 'name' => 'payment_instrument',
1130 'title' => ts('Payment Method'),
1132 'contribution_status_id' => array(
1133 'name' => 'contribution_status_id',
1134 'title' => ts('Contribution Status'),
1138 return self
::$_memberBatchEntryFields;