3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 (reference ) an assoc array of name/value pairs
53 * @param array $defaults (reference ) an assoc array to hold the flattened values
55 * @return CRM_Core_BAO_UFField object
59 static function retrieve(&$params, &$defaults) {
60 return CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_UFField', $params, $defaults);
66 * @param int $id id of uf_form
68 * @return string title
74 public static function getTitle($id) {
75 return CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFField', $groupId, 'title');
79 * Update the is_active flag in the db
81 * @param int $id id of the database record
82 * @param boolean $is_active value we want to set the is_active field
84 * @return Object DAO object on sucess, null otherwise
88 static function setIsActive($id, $is_active) {
89 //check if custom data profile field is disabled
91 if (CRM_Core_BAO_UFField
::checkUFStatus($id)) {
92 return CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_UFField', $id, 'is_active', $is_active);
95 CRM_Core_Session
::setStatus(ts('Cannot enable this UF field since the used custom field is disabled.'), ts('Check Custom Field'), 'error');
99 return CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_UFField', $id, 'is_active', $is_active);
104 * Delete the profile Field.
106 * @param int $id Field Id
114 public static function del($id) {
116 $field = new CRM_Core_DAO_UFField();
123 * Check duplicate for duplicate field in a group
125 * @param array $params an associative array with field and values
129 * @ids array $ids array that containd ids
134 public static function duplicateField($params, $ids) {
135 $ufField = new CRM_Core_DAO_UFField();
136 $ufField->uf_group_id
= CRM_Utils_Array
::value('uf_group', $ids);
137 $ufField->field_type
= $params['field_name'][0];
138 $ufField->field_name
= $params['field_name'][1];
139 if ($params['field_name'][1] == 'url') {
140 $ufField->website_type_id
= CRM_Utils_Array
::value(2, $params['field_name'], NULL);
143 $ufField->location_type_id
= (CRM_Utils_Array
::value(2, $params['field_name'])) ?
$params['field_name'][2] : 'NULL';
145 $ufField->phone_type_id
= CRM_Utils_Array
::value(3, $params['field_name']);
147 if (!empty($ids['uf_field'])) {
148 $ufField->whereAdd("id <> " . CRM_Utils_Array
::value('uf_field', $ids));
151 return $ufField->find(TRUE);
155 * Does profile consists of a multi-record custom field
157 public static function checkMultiRecordFieldExists($gId) {
158 $queryString = "SELECT f.field_name
159 FROM civicrm_uf_field f, civicrm_uf_group g
160 WHERE f.uf_group_id = g.id
161 AND g.id = %1 AND f.field_name LIKE 'custom%'";
162 $p = array(1 => array($gId, 'Integer'));
163 $dao = CRM_Core_DAO
::executeQuery($queryString, $p);
164 $customFieldIds = array();
165 $isMultiRecordFieldPresent = FALSE;
166 while ($dao->fetch()) {
167 if ($customId = CRM_Core_BAO_CustomField
::getKeyID($dao->field_name
)) {
168 if (is_numeric($customId)) {
169 $customFieldIds[] = $customId;
174 if (!empty($customFieldIds) && count($customFieldIds) == 1) {
175 $customFieldId = array_pop($customFieldIds);
176 $isMultiRecordFieldPresent = CRM_Core_BAO_CustomField
::isMultiRecordField($customFieldId);
178 elseif (count($customFieldIds) > 1) {
179 $customFieldIds = implode(", ", $customFieldIds);
182 FROM civicrm_custom_group cg
183 INNER JOIN civicrm_custom_field cf
184 ON cg.id = cf.custom_group_id
185 WHERE cf.id IN (" . $customFieldIds . ") AND is_multiple = 1 LIMIT 0,1";
187 $dao = CRM_Core_DAO
::executeQuery($queryString);
189 $isMultiRecordFieldPresent = ($dao->cgId
) ?
$dao->cgId
: FALSE;
193 return $isMultiRecordFieldPresent;
199 * @param array $params (reference) array containing the values submitted by the form
200 * @param array $ids array containing the id
202 * @return CRM_Core_BAO_UFField object
208 static function add(&$params, $ids = array()) {
209 // set values for uf field properties and save
210 $ufField = new CRM_Core_DAO_UFField();
211 $ufField->field_type
= $params['field_name'][0];
212 $ufField->field_name
= $params['field_name'][1];
214 //should not set location type id for Primary
215 $locationTypeId = NULL;
216 if ($params['field_name'][1] == 'url') {
217 $ufField->website_type_id
= CRM_Utils_Array
::value(2, $params['field_name']);
220 $locationTypeId = CRM_Utils_Array
::value(2, $params['field_name']);
221 $ufField->website_type_id
= NULL;
223 if ($locationTypeId) {
224 $ufField->location_type_id
= $locationTypeId;
227 $ufField->location_type_id
= 'null';
230 $ufField->phone_type_id
= CRM_Utils_Array
::value(3, $params['field_name'], 'NULL');
231 $ufField->listings_title
= CRM_Utils_Array
::value('listings_title', $params);
232 $ufField->visibility
= CRM_Utils_Array
::value('visibility', $params);
233 $ufField->help_pre
= CRM_Utils_Array
::value('help_pre', $params);
234 $ufField->help_post
= CRM_Utils_Array
::value('help_post', $params);
235 $ufField->label
= CRM_Utils_Array
::value('label', $params);
236 $ufField->is_required
= CRM_Utils_Array
::value('is_required', $params, FALSE);
237 $ufField->is_active
= CRM_Utils_Array
::value('is_active', $params, FALSE);
238 $ufField->in_selector
= CRM_Utils_Array
::value('in_selector', $params, FALSE);
239 $ufField->is_view
= CRM_Utils_Array
::value('is_view', $params, FALSE);
240 $ufField->is_registration
= CRM_Utils_Array
::value('is_registration', $params, FALSE);
241 $ufField->is_match
= CRM_Utils_Array
::value('is_match', $params, FALSE);
242 $ufField->is_searchable
= CRM_Utils_Array
::value('is_searchable', $params, FALSE);
243 $ufField->is_multi_summary
= CRM_Utils_Array
::value('is_multi_summary', $params, FALSE);
244 $ufField->weight
= CRM_Utils_Array
::value('weight', $params, 0);
246 // need the FKEY - uf group id
247 $ufField->uf_group_id
= CRM_Utils_Array
::value('uf_group', $ids, FALSE);
248 $ufField->id
= CRM_Utils_Array
::value('uf_field', $ids, FALSE);
250 return $ufField->save();
254 * Automatically determine one weight and modify others
256 * @param array $params UFField record, e.g. with 'weight', 'uf_group_id', and 'field_id'
259 public static function autoWeight($params) {
263 if (!empty($params['field_id'])) {
264 $oldWeight = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFField', $params['field_id'], 'weight', 'id');
266 $fieldValues = array('uf_group_id' => $params['group_id']);
267 return CRM_Utils_Weight
::updateOtherWeights('CRM_Core_DAO_UFField', $oldWeight, CRM_Utils_Array
::value('weight', $params, 0), $fieldValues);
271 * Enable/disable profile field given a custom field id
273 * @param int $customFieldId custom field id
274 * @param boolean $is_active set the is_active field
280 static function setUFField($customFieldId, $is_active) {
281 //find the profile id given custom field
282 $ufField = new CRM_Core_DAO_UFField();
283 $ufField->field_name
= "custom_" . $customFieldId;
286 while ($ufField->fetch()) {
287 //enable/ disable profile
288 CRM_Core_BAO_UFField
::setIsActive($ufField->id
, $is_active);
293 * Copy existing profile fields to
294 * new profile from the already built profile
296 * @param int $old_id from which we need to copy
297 * @param boolean $new_id in which to copy
303 static function copy($old_id, $new_id) {
304 $ufField = new CRM_Core_DAO_UFField();
305 $ufField->uf_group_id
= $old_id;
307 while ($ufField->fetch()) {
308 //copy the field records as it is on new ufgroup id
309 $ufField->uf_group_id
= $new_id;
316 * Delete profile field given a custom field
318 * @param int $customFieldId ID of the custom field to be deleted
325 static function delUFField($customFieldId) {
326 //find the profile id given custom field id
327 $ufField = new CRM_Core_DAO_UFField();
328 $ufField->field_name
= "custom_" . $customFieldId;
331 while ($ufField->fetch()) {
332 //enable/ disable profile
333 CRM_Core_BAO_UFField
::del($ufField->id
);
338 * Enable/disable profile field given a custom group id
340 * @param int $customGroupId custom group id
341 * @param boolean $is_active value we want to set the is_active field
347 static function setUFFieldStatus($customGroupId, $is_active) {
348 //find the profile id given custom group id
349 $queryString = "SELECT civicrm_custom_field.id as custom_field_id
350 FROM civicrm_custom_field, civicrm_custom_group
351 WHERE civicrm_custom_field.custom_group_id = civicrm_custom_group.id
352 AND civicrm_custom_group.id = %1";
353 $p = array(1 => array($customGroupId, 'Integer'));
354 $dao = CRM_Core_DAO
::executeQuery($queryString, $p);
356 while ($dao->fetch()) {
357 //enable/ disable profile
358 CRM_Core_BAO_UFField
::setUFField($dao->custom_field_id
, $is_active);
363 * Check the status of custom field used in uf fields
365 * @param int $UFFieldId
367 * @return boolean false if custom field are disabled else true
371 static function checkUFStatus($UFFieldId) {
372 $fieldName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFField', $UFFieldId, 'field_name');
373 // return if field is not a custom field
374 if (!$customFieldId = CRM_Core_BAO_CustomField
::getKeyID($fieldName)) {
378 $customField = new CRM_Core_DAO_CustomField();
379 $customField->id
= $customFieldId;
380 // if uf field is custom field
381 if ($customField->find(TRUE)) {
382 if (!$customField->is_active
) {
392 * Find out whether given profile group using Activity
393 * Profile fields with contact fields
395 static function checkContactActivityProfileType($ufGroupId) {
396 $ufGroup = new CRM_Core_DAO_UFGroup();
397 $ufGroup->id
= $ufGroupId;
398 $ufGroup->find(TRUE);
400 return self
::checkContactActivityProfileTypeByGroupType($ufGroup->group_type
);
405 * @param $ufGroupType
408 public static function checkContactActivityProfileTypeByGroupType($ufGroupType) {
409 $profileTypes = array();
411 $typeParts = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $ufGroupType);
412 $profileTypes = explode(',', $typeParts[0]);
415 if (empty($profileTypes)) {
418 $components = array('Contribution', 'Participant', 'Membership');
419 if (!in_array('Activity', $profileTypes)) {
422 elseif (count($profileTypes) == 1) {
426 if ($index = array_search('Contact', $profileTypes)) {
427 unset($profileTypes[$index]);
428 if (count($profileTypes) == 1) {
433 $contactTypes = array('Individual', 'Household', 'Organization');
434 $subTypes = CRM_Contact_BAO_ContactType
::subTypes();
436 $profileTypeComponent = array_intersect($components, $profileTypes);
437 if (!empty($profileTypeComponent) ||
438 count(array_intersect($contactTypes, $profileTypes)) > 1 ||
439 count(array_intersect($subTypes, $profileTypes)) > 1
448 * Find out whether given profile group uses $required
449 * and/or $optional profile types
451 * @param integer $ufGroupId profile id
452 * @param array $required array of types those are required
453 * @param array $optional array of types those are optional
455 * @return boolean $valid
458 static function checkValidProfileType($ufGroupId, $required, $optional = NULL) {
459 if (!is_array($required) ||
empty($required)) {
463 $ufGroup = new CRM_Core_DAO_UFGroup();
464 $ufGroup->id
= $ufGroupId;
465 $ufGroup->find(TRUE);
467 $profileTypes = array();
468 if ($ufGroup->group_type
) {
469 $typeParts = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $ufGroup->group_type
);
470 $profileTypes = explode(',', $typeParts[0]);
473 if (empty($profileTypes)) {
478 foreach ($required as $key => $val) {
479 if (!in_array($val, $profileTypes)) {
485 if ($valid && is_array($optional)) {
486 foreach ($optional as $key => $val) {
487 if (in_array($val, $profileTypes)) {
498 * Check for mix profile fields (eg: individual + other contact types)
500 * @param int $ufGroupId
502 * @return true for mix profile else false
506 static function checkProfileType($ufGroupId) {
507 $ufGroup = new CRM_Core_DAO_UFGroup();
508 $ufGroup->id
= $ufGroupId;
509 $ufGroup->find(TRUE);
511 $profileTypes = array();
512 if ($ufGroup->group_type
) {
513 $typeParts = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $ufGroup->group_type
);
514 $profileTypes = explode(',', $typeParts[0]);
517 //early return if new profile.
518 if (empty($profileTypes)) {
522 //we need to unset Contact
523 if (count($profileTypes) > 1) {
524 $index = array_search('Contact', $profileTypes);
525 if ($index !== FALSE) {
526 unset($profileTypes[$index]);
530 // suppress any subtypes if present
531 CRM_Contact_BAO_ContactType
::suppressSubTypes($profileTypes);
533 $contactTypes = array('Contact', 'Individual', 'Household', 'Organization');
534 $components = array('Contribution', 'Participant', 'Membership', 'Activity');
537 // check for mix profile condition
538 if (count($profileTypes) > 1) {
539 //check the there are any components include in profile
540 foreach ($components as $value) {
541 if (in_array($value, $profileTypes)) {
545 //check if there are more than one contact types included in profile
546 if (count($profileTypes) > 1) {
550 elseif (count($profileTypes) == 1) {
551 // note for subtype case count would be zero
552 $profileTypes = array_values($profileTypes);
553 if (!in_array($profileTypes[0], $contactTypes)) {
562 * Get the profile type (eg: individual/organization/household)
564 * @param int $ufGroupId uf group id
565 * @param boolean $returnMixType this is true, then field type of mix profile field is returned
566 * @param boolean $onlyPure true if only pure profiles are required
568 * @param bool $skipComponentType
570 * @return string profile group_type
574 * TODO Why is this function in this class? It seems to be about the UFGroup.
576 static function getProfileType($ufGroupId, $returnMixType = TRUE, $onlyPure = FALSE, $skipComponentType = FALSE) {
577 $ufGroup = new CRM_Core_DAO_UFGroup();
578 $ufGroup->id
= $ufGroupId;
579 $ufGroup->is_active
= 1;
581 $ufGroup->find(TRUE);
582 return self
::calculateProfileType($ufGroup->group_type
, $returnMixType, $onlyPure, $skipComponentType);
586 * Get the profile type (eg: individual/organization/household)
588 * @param string $ufGroupType
589 * @param boolean $returnMixType this is true, then field type of mix profile field is returned
590 * @param boolean $onlyPure true if only pure profiles are required
591 * @param bool $skipComponentType
593 * @return string profile group_type
597 * TODO Why is this function in this class? It seems to be about the UFGroup.
599 public static function calculateProfileType($ufGroupType, $returnMixType = TRUE, $onlyPure = FALSE, $skipComponentType= FALSE) {
601 $contactTypes = array('Contact', 'Individual', 'Household', 'Organization');
602 $subTypes = CRM_Contact_BAO_ContactType
::subTypes();
603 $components = array('Contribution', 'Participant', 'Membership', 'Activity');
605 $profileTypes = array();
607 $typeParts = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $ufGroupType);
608 $profileTypes = explode(',', $typeParts[0]);
612 if (count($profileTypes) == 1) {
613 return $profileTypes[0];
620 //we need to unset Contact
621 if (count($profileTypes) > 1) {
622 $index = array_search('Contact', $profileTypes);
623 if ($index !== FALSE) {
624 unset($profileTypes[$index]);
628 $profileType = $mixProfileType = NULL;
630 // this case handles pure profile
631 if (count($profileTypes) == 1) {
632 $profileType = array_pop($profileTypes);
635 //check the there are any components include in profile
636 $componentCount = array();
637 foreach ($components as $value) {
638 if (in_array($value, $profileTypes)) {
639 $componentCount[] = $value;
643 //check contact type included in profile
644 $contactTypeCount = array();
645 foreach ($contactTypes as $value) {
646 if (in_array($value, $profileTypes)) {
647 $contactTypeCount[] = $value;
651 $subTypeCount = array();
652 foreach ($subTypes as $value) {
653 if (in_array($value, $profileTypes)) {
654 $subTypeCount[] = $value;
657 if (!$skipComponentType && count($componentCount) == 1) {
658 $profileType = $componentCount[0];
660 elseif (count($componentCount) > 1) {
661 $mixProfileType = $componentCount[1];
663 elseif (count($subTypeCount) == 1) {
664 $profileType = $subTypeCount[0];
666 elseif (count($contactTypeCount) == 1) {
667 $profileType = $contactTypeCount[0];
669 elseif (count($subTypeCount) > 1) {
670 // this is mix subtype profiles
671 $mixProfileType = $subTypeCount[1];
673 elseif (count($contactTypeCount) > 1) {
674 // this is mix contact profiles
675 $mixProfileType = $contactTypeCount[1];
679 if ($mixProfileType) {
680 if ($returnMixType) {
681 return $mixProfileType;
693 * Check for mix profiles groups (eg: individual + other contact types)
697 * @return true for mix profile group else false
701 static function checkProfileGroupType($ctype) {
702 $ufGroup = new CRM_Core_DAO_UFGroup();
706 FROM civicrm_uf_group as ufg, civicrm_uf_join as ufj
707 WHERE ufg.id = ufj.uf_group_id
708 AND ufj.module = 'User Registration'
709 AND ufg.is_active = 1 ";
711 $ufGroup = CRM_Core_DAO
::executeQuery($query);
714 $validProfiles = array('Individual', 'Organization', 'Household', 'Contribution');
715 while ($ufGroup->fetch()) {
716 $profileType = self
::getProfileType($ufGroup->id
);
717 if (in_array($profileType, $validProfiles)) {
720 elseif ($profileType) {
729 * Check for searchable or in selector field for given profile.
731 * @param int $profileID
733 * @return boolean $result true/false.
735 static function checkSearchableORInSelector($profileID) {
743 From civicrm_uf_field
744 WHERE (in_selector = 1 OR is_searchable = 1)
745 AND uf_group_id = {$profileID}";
747 $ufFields = CRM_Core_DAO
::executeQuery($query);
748 while ($ufFields->fetch()) {
757 * Reset In selector and is searchable values for given $profileID.
759 * @param int $profileID
763 function resetInSelectorANDSearchable($profileID) {
767 $query = "UPDATE civicrm_uf_field SET in_selector = 0, is_searchable = 0 WHERE uf_group_id = {$profileID}";
768 CRM_Core_DAO
::executeQuery($query);
772 * Add fields to $profileAddressFields as appropriate.
773 * profileAddressFields is assigned to the template to tell it
774 * what fields are in the profile address
775 * that potentially should be copied to the Billing fields
776 * we want to give precedence to
778 * 2) then Primary designated as 'Primary
779 * 3) location_type is primary
780 * 4) if none of these apply then it just uses the first one
782 * as this will be used to
783 * transfer profile address data to billing fields
784 * http://issues.civicrm.org/jira/browse/CRM-5869
786 * @param string $key Field key - e.g. street_address-Primary, first_name
787 * @param array $profileAddressFields array of profile fields that relate to address fields
788 * @param array $profileFilter filter to apply to profile fields - expected usage is to only fill based on
789 * the bottom profile per CRM-13726
791 * @return bool Can the address block be hidden safe in the knowledge all fields are elsewhere collected (see CRM-15118)
793 static function assignAddressField($key, &$profileAddressFields, $profileFilter) {
794 $billing_id = CRM_Core_BAO_LocationType
::getBilling();
795 list($prefixName, $index) = CRM_Utils_System
::explode('-', $key, 2);
797 $profileFields = civicrm_api3('uf_field', 'get', array_merge($profileFilter,
798 array('is_active' => 1, 'return' => 'field_name, is_required', 'options' => array(
802 //check for valid fields ( fields that are present in billing block )
803 $validBillingFields = array(
808 'supplemental_address_1',
814 $requiredBillingFields = array_diff($validBillingFields, array('middle_name','supplemental_address_1'));
815 $validProfileFields = array();
816 $requiredProfileFields = array();
818 foreach ($profileFields['values'] as $field) {
819 if(in_array($field['field_name'], $validBillingFields)) {
820 $validProfileFields[] = $field['field_name'];
822 if ($field['is_required']) {
823 $requiredProfileFields[] = $field['field_name'];
827 if (!in_array($prefixName, $validProfileFields) ) {
831 if (!empty($index) && (
832 // it's empty so we set it OR
833 !CRM_Utils_array
::value($prefixName, $profileAddressFields)
834 //we are dealing with billing id (precedence)
835 ||
$index == $billing_id
836 // we are dealing with primary & billing not set
837 ||
($index == 'Primary' && $profileAddressFields[$prefixName] != $billing_id)
838 ||
($index == CRM_Core_BAO_LocationType
::getDefault()->id
839 && $profileAddressFields[$prefixName] != $billing_id
840 && $profileAddressFields[$prefixName] != 'Primary'
844 $profileAddressFields[$prefixName] = $index;
847 $potentiallyMissingRequiredFields = array_diff($requiredBillingFields, $requiredProfileFields);
848 CRM_Core_Resources
::singleton()->addSetting(array('billing' => array('billingProfileIsHideable' => empty($potentiallyMissingRequiredFields))));
852 * Get a list of fields which can be added to profiles
854 * @param int $gid: UF group ID
855 * @param array $defaults: Form defaults
856 * @return array, multidimensional; e.g. $result['FieldGroup']['field_name']['label']
859 public static function getAvailableFields($gid = NULL, $defaults = array()) {
861 'Contact' => array(),
862 'Individual' => CRM_Contact_BAO_Contact
::importableFields('Individual', FALSE, FALSE, TRUE, TRUE, TRUE),
863 'Household' => CRM_Contact_BAO_Contact
::importableFields('Household', FALSE, FALSE, TRUE, TRUE, TRUE),
864 'Organization' => CRM_Contact_BAO_Contact
::importableFields('Organization', FALSE, FALSE, TRUE, TRUE, TRUE),
867 // include hook injected fields
868 $fields['Contact'] = array_merge($fields['Contact'], CRM_Contact_BAO_Query_Hook
::singleton()->getFields());
870 // add current employer for individuals
871 $fields['Individual']['current_employer'] = array(
872 'name' => 'organization_name',
873 'title' => ts('Current Employer'),
876 $addressOptions = CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
877 'address_options', TRUE, NULL, TRUE
880 if (!$addressOptions['county']) {
881 unset($fields['Individual']['county'], $fields['Household']['county'], $fields['Organization']['county']);
884 // break out common contact fields array CRM-3037.
885 // from a UI perspective this makes very little sense
886 foreach ($fields['Individual'] as $key => $value) {
887 if (!empty($fields['Household'][$key]) && !empty($fields['Organization'][$key])) {
888 $fields['Contact'][$key] = $value;
889 unset($fields['Individual'][$key], $fields['Household'][$key], $fields['Organization'][$key]);
893 // Internal field not exposed to forms
894 unset($fields['Contact']['contact_type']);
895 unset($fields['Contact']['master_id']);
897 // convert phone extension in to psedo-field phone + phone extension
899 unset($fields['Contact']['phone_ext']);
901 $fields['Contact']['phone_and_ext'] = array(
902 'name' => 'phone_and_ext',
903 'title' => ts('Phone and Extension'),
904 'hasLocationType' => 1,
907 // include Subtypes For Profile
908 $subTypes = CRM_Contact_BAO_ContactType
::subTypeInfo();
909 foreach ($subTypes as $name => $val) {
910 //custom fields for sub type
911 $subTypeFields = CRM_Core_BAO_CustomField
::getFieldsForImport($name, FALSE, FALSE, FALSE, TRUE, TRUE);
912 if (array_key_exists($val['parent'], $fields)) {
913 $fields[$name] = $fields[$val['parent']] +
$subTypeFields;
916 $fields[$name] = $subTypeFields;
920 if (CRM_Core_Permission
::access('CiviContribute')) {
921 $contribFields = CRM_Contribute_BAO_Contribution
::getContributionFields(FALSE);
922 if (!empty($contribFields)) {
923 unset($contribFields['is_test']);
924 unset($contribFields['is_pay_later']);
925 unset($contribFields['contribution_id']);
926 $contribFields['contribution_note'] = array(
927 'name' => 'contribution_note',
928 'title' => ts('Contribution Note'),
930 $fields['Contribution'] = array_merge($contribFields, self
::getContribBatchEntryFields());
934 if (CRM_Core_Permission
::access('CiviEvent')) {
935 $participantFields = CRM_Event_BAO_Query
::getParticipantFields();
936 if ($participantFields) {
937 // Remove fields not supported by profiles
938 CRM_Utils_Array
::remove($participantFields,
939 'external_identifier',
941 'participant_contact_id',
942 'participant_role_id',
943 'participant_status_id',
944 'participant_is_test',
945 'participant_fee_level',
947 'participant_is_pay_later',
948 'participant_campaign'
950 if (isset($participantFields['participant_campaign_id'])) {
951 $participantFields['participant_campaign_id']['title'] = ts('Campaign');
953 $fields['Participant'] = $participantFields;
957 if (CRM_Core_Permission
::access('CiviMember')) {
958 $membershipFields = CRM_Member_BAO_Membership
::getMembershipFields();
959 // Remove fields not supported by profiles
960 CRM_Utils_Array
::remove($membershipFields,
962 'membership_type_id',
966 'member_is_pay_later'
968 if ($gid && CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $gid, 'name') == 'membership_batch_entry') {
969 $fields['Membership'] = array_merge($membershipFields, self
::getMemberBatchEntryFields());
972 $fields['Membership'] = $membershipFields;
976 if (CRM_Core_Permission
::access('CiviCase')) {
977 $caseFields = CRM_Case_BAO_Query
::getFields(TRUE);
978 $caseFields = array_merge($caseFields, CRM_Core_BAO_CustomField
::getFieldsForImport('Case'));
980 // Remove fields not supported by profiles
981 CRM_Utils_Array
::remove($caseFields,
991 $fields['Case'] = $caseFields;
994 $activityFields = CRM_Activity_BAO_Activity
::getProfileFields();
995 if ($activityFields) {
996 // campaign related fields.
997 if (isset($activityFields['activity_campaign_id'])) {
998 $activityFields['activity_campaign_id']['title'] = ts('Campaign');
1000 $fields['Activity'] = $activityFields;
1003 $fields['Formatting']['format_free_html_' . rand(1000, 9999)] = array(
1004 'name' => 'free_html',
1007 'title' => 'Free HTML',
1011 foreach ($fields as &$values) {
1012 $values = CRM_Utils_Array
::crmArraySortByField($values, 'title');
1015 //group selected and unwanted fields list
1016 $ufFields = $gid ? CRM_Core_BAO_UFGroup
::getFields($gid, FALSE, NULL, NULL, NULL, TRUE, NULL, TRUE) : array();
1017 $groupFieldList = array_merge($ufFields, array(
1019 'email_greeting_custom',
1020 'postal_greeting_custom',
1024 //unset selected fields
1025 foreach ($groupFieldList as $key => $value) {
1026 if (is_integer($key)) {
1027 unset($fields['Individual'][$value], $fields['Household'][$value], $fields['Organization'][$value]);
1030 if (!empty($defaults['field_name'])
1031 && $defaults['field_name']['0'] == $value['field_type']
1032 && $defaults['field_name']['1'] == $key
1036 unset($fields[$value['field_type']][$key]);
1043 * Get a list of fields which can be added to profiles
1045 * @param bool $force
1047 * @return array, multidimensional; e.g. $result['field_name']['label']
1050 public static function getAvailableFieldsFlat($force = FALSE) {
1051 // FIXME reset when data model changes
1052 static $result = NULL;
1053 if ($result === NULL ||
$force) {
1054 $fieldTree = self
::getAvailableFields();
1056 foreach ($fieldTree as $field_type => $fields) {
1057 foreach ($fields as $field_name => $field) {
1058 if (!isset($result[$field_name])) {
1059 $field['field_type'] = $field_type;
1060 $result[$field_name] = $field;
1069 * Determine whether the given field_name is valid
1071 * @param string $fieldName
1074 static function isValidFieldName($fieldName) {
1075 $availableFields = CRM_Core_BAO_UFField
::getAvailableFieldsFlat();
1076 return isset($availableFields[$fieldName]);
1080 * @return array|null
1082 static function getContribBatchEntryFields() {
1083 if (self
::$_contriBatchEntryFields === NULL) {
1084 self
::$_contriBatchEntryFields = array(
1085 'send_receipt' => array(
1086 'name' => 'send_receipt',
1087 'title' => ts('Send Receipt'),
1089 'soft_credit' => array(
1090 'name' => 'soft_credit',
1091 'title' => ts('Soft Credit'),
1093 'soft_credit_type' => array(
1094 'name' => 'soft_credit_type',
1095 'title' => ts('Soft Credit Type'),
1097 'product_name' => array(
1098 'name' => 'product_name',
1099 'title' => ts('Premiums'),
1101 'contribution_note' => array(
1102 'name' => 'contribution_note',
1103 'title' => ts('Contribution Note'),
1107 return self
::$_contriBatchEntryFields;
1111 * @return array|null
1113 public static function getMemberBatchEntryFields() {
1114 if (self
::$_memberBatchEntryFields === NULL) {
1115 self
::$_memberBatchEntryFields = array(
1116 'send_receipt' => array(
1117 'name' => 'send_receipt',
1118 'title' => ts('Send Receipt'),
1120 'soft_credit' => array(
1121 'name' => 'soft_credit',
1122 'title' => ts('Soft Credit'),
1124 'product_name' => array(
1125 'name' => 'product_name',
1126 'title' => ts('Premiums'),
1128 'financial_type' => array(
1129 'name' => 'financial_type',
1130 'title' => ts('Financial Type'),
1132 'total_amount' => array(
1133 'name' => 'total_amount',
1134 'title' => ts('Total Amount'),
1136 'receive_date' => array(
1137 'name' => 'receive_date',
1138 'title' => ts('Receive Date'),
1140 'payment_instrument' => array(
1141 'name' => 'payment_instrument',
1142 'title' => ts('Payment Instrument'),
1144 'contribution_status_id' => array(
1145 'name' => 'contribution_status_id',
1146 'title' => ts('Contribution Status'),
1150 return self
::$_memberBatchEntryFields;