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
35 class CRM_Core_BAO_Mapping
extends CRM_Core_DAO_Mapping
{
40 public function __construct() {
41 parent
::__construct();
45 * Fetch object based on array of properties.
47 * @param array $params
48 * (reference ) an assoc array of name/value pairs.
49 * @param array $defaults
50 * (reference ) an assoc array to hold the flattened values.
53 * CRM_Core_DAO_Mapping object on success, otherwise NULL
55 public static function retrieve(&$params, &$defaults) {
56 $mapping = new CRM_Core_DAO_Mapping();
57 $mapping->copyValues($params);
58 if ($mapping->find(TRUE)) {
59 CRM_Core_DAO
::storeValues($mapping, $defaults);
73 public static function del($id) {
74 // delete from mapping_field table
75 $mappingField = new CRM_Core_DAO_MappingField();
76 $mappingField->mapping_id
= $id;
77 $mappingField->find();
78 while ($mappingField->fetch()) {
79 $mappingField->delete();
82 // delete from mapping table
83 $mapping = new CRM_Core_DAO_Mapping();
86 CRM_Core_Session
::setStatus(ts('Selected mapping has been deleted successfully.'), ts('Deleted'), 'success');
92 * Takes an associative array and creates a contact object.
94 * The function extract all the params it needs to initialize the create a
95 * contact object. the params array could contain additional unused name/value
98 * @param array $params
99 * An array of name/value pairs.
102 * CRM_Core_DAO_Mapper object on success, otherwise NULL
104 public static function add($params) {
105 $mapping = new CRM_Core_DAO_Mapping();
106 $mapping->copyValues($params);
113 * Get the list of mappings.
115 * @param string $mappingTypeId
119 * array of mapping name
121 public static function getMappings($mappingTypeId) {
123 $mappingDAO = new CRM_Core_DAO_Mapping();
124 $mappingDAO->mapping_type_id
= $mappingTypeId;
127 while ($mappingDAO->fetch()) {
128 $mapping[$mappingDAO->id
] = $mappingDAO->name
;
135 * Get the mapping fields.
137 * @param int $mappingId
141 * array of mapping fields
143 public static function getMappingFields($mappingId) {
144 //mapping is to be loaded from database
145 $mapping = new CRM_Core_DAO_MappingField();
146 $mapping->mapping_id
= $mappingId;
147 $mapping->orderBy('column_number');
150 $mappingName = $mappingLocation = $mappingContactType = $mappingPhoneType = array();
151 $mappingImProvider = $mappingRelation = $mappingOperator = $mappingValue = $mappingWebsiteType = array();
152 while ($mapping->fetch()) {
153 $mappingName[$mapping->grouping
][$mapping->column_number
] = $mapping->name
;
154 $mappingContactType[$mapping->grouping
][$mapping->column_number
] = $mapping->contact_type
;
156 if (!empty($mapping->location_type_id
)) {
157 $mappingLocation[$mapping->grouping
][$mapping->column_number
] = $mapping->location_type_id
;
160 if (!empty($mapping->phone_type_id
)) {
161 $mappingPhoneType[$mapping->grouping
][$mapping->column_number
] = $mapping->phone_type_id
;
164 // get IM service provider type id from mapping fields
165 if (!empty($mapping->im_provider_id
)) {
166 $mappingImProvider[$mapping->grouping
][$mapping->column_number
] = $mapping->im_provider_id
;
169 if (!empty($mapping->website_type_id
)) {
170 $mappingWebsiteType[$mapping->grouping
][$mapping->column_number
] = $mapping->website_type_id
;
173 if (!empty($mapping->relationship_type_id
)) {
174 $mappingRelation[$mapping->grouping
][$mapping->column_number
] = "{$mapping->relationship_type_id}_{$mapping->relationship_direction}";
177 if (!empty($mapping->operator
)) {
178 $mappingOperator[$mapping->grouping
][$mapping->column_number
] = $mapping->operator
;
181 if (!empty($mapping->value
)) {
182 $mappingValue[$mapping->grouping
][$mapping->column_number
] = $mapping->value
;
200 * Check Duplicate Mapping Name.
202 * @param string $nameField
204 * @param string $mapTypeId
209 public static function checkMapping($nameField, $mapTypeId) {
210 $mapping = new CRM_Core_DAO_Mapping();
211 $mapping->name
= $nameField;
212 $mapping->mapping_type_id
= $mapTypeId;
213 return (bool) $mapping->find(TRUE);
217 * Function returns associated array of elements, that will be passed for search
219 * @param int $smartGroupId
223 * associated array of elements
225 public static function getFormattedFields($smartGroupId) {
226 $returnFields = array();
228 //get the fields from mapping table
229 $dao = new CRM_Core_DAO_MappingField();
230 $dao->mapping_id
= $smartGroupId;
232 while ($dao->fetch()) {
233 $fldName = $dao->name
;
234 if ($dao->location_type_id
) {
235 $fldName .= "-{$dao->location_type_id}";
237 if ($dao->phone_type
) {
238 $fldName .= "-{$dao->phone_type}";
240 $returnFields[$fldName]['value'] = $dao->value
;
241 $returnFields[$fldName]['op'] = $dao->operator
;
242 $returnFields[$fldName]['grouping'] = $dao->grouping
;
244 return $returnFields;
248 * Build the mapping form.
250 * @param CRM_Core_Form $form
251 * @param string $mappingType
252 * (Export/Import/Search Builder).
253 * @param int $mappingId
254 * @param int $columnNo
255 * @param int $blockCount
256 * (no of blocks shown).
257 * @param NULL $exportMode
261 public static function buildMappingForm(&$form, $mappingType = 'Export', $mappingId = NULL, $columnNo, $blockCount = 3, $exportMode = NULL) {
262 if ($mappingType == 'Export') {
264 $columnCount = array('1' => $columnNo);
266 elseif ($mappingType == 'Search Builder') {
268 $columnCount = $columnNo;
271 //get the saved mapping details
273 if ($mappingType == 'Export') {
274 $form->applyFilter('saveMappingName', 'trim');
276 //to save the current mappings
277 if (!isset($mappingId)) {
278 $saveDetailsName = ts('Save this field mapping');
279 $form->add('text', 'saveMappingName', ts('Name'));
280 $form->add('text', 'saveMappingDesc', ts('Description'));
283 $form->assign('loadedMapping', $mappingId);
285 $params = array('id' => $mappingId);
287 $mappingDetails = CRM_Core_BAO_Mapping
::retrieve($params, $temp);
289 $form->assign('savedName', $mappingDetails->name
);
291 $form->add('hidden', 'mappingId', $mappingId);
293 $form->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL);
294 $saveDetailsName = ts('Save as a new field mapping');
295 $form->add('text', 'saveMappingName', ts('Name'));
296 $form->add('text', 'saveMappingDesc', ts('Description'));
299 $form->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, array('onclick' => "showSaveDetails(this)"));
300 $form->addFormRule(array('CRM_Export_Form_Map', 'formRule'), $form->get('mappingTypeId'));
302 elseif ($mappingType == 'Search Builder') {
303 $form->addElement('submit', 'addBlock', ts('Also include contacts where'),
304 array('class' => 'submit-link')
309 $hasLocationTypes = array();
310 $hasRelationTypes = array();
313 if ($mappingType == 'Export') {
316 elseif ($mappingType == 'Search Builder') {
320 $contactType = array('Individual', 'Household', 'Organization');
321 foreach ($contactType as $value) {
322 if ($mappingType == 'Search Builder') {
323 // get multiple custom group fields in this context
324 $contactFields = CRM_Contact_BAO_Contact
::exportableFields($value, FALSE, $required, FALSE, TRUE);
327 $contactFields = CRM_Contact_BAO_Contact
::exportableFields($value, FALSE, $required);
329 $contactFields = array_merge($contactFields, CRM_Contact_BAO_Query_Hook
::singleton()->getFields());
331 // exclude the address options disabled in the Address Settings
332 $fields[$value] = CRM_Core_BAO_Address
::validateAddressOptions($contactFields);
333 ksort($fields[$value]);
334 if ($mappingType == 'Export') {
335 $relationships = array();
336 $relationshipTypes = CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, NULL, NULL, $value);
337 asort($relationshipTypes);
339 foreach ($relationshipTypes as $key => $var) {
340 list($type) = explode('_', $key);
342 $relationships[$key]['title'] = $var;
343 $relationships[$key]['headerPattern'] = '/' . preg_quote($var, '/') . '/';
344 $relationships[$key]['export'] = TRUE;
345 $relationships[$key]['relationship_type_id'] = $type;
346 $relationships[$key]['related'] = TRUE;
347 $relationships[$key]['hasRelationType'] = 1;
350 if (!empty($relationships)) {
351 $fields[$value] = array_merge($fields[$value],
352 array('related' => array('title' => ts('- related contact info -'))),
359 //get the current employer for mapping.
361 $fields['Individual']['current_employer']['title'] = ts('Current Employer');
364 // add component fields
365 $compArray = array();
367 //we need to unset groups, tags, notes for component export
368 if ($exportMode != CRM_Export_Form_Select
::CONTACT_EXPORT
) {
374 unset($fields['Individual'][$value]);
375 unset($fields['Household'][$value]);
376 unset($fields['Organization'][$value]);
380 if ($mappingType == 'Search Builder') {
381 //build the common contact fields array.
382 $fields['Contact'] = array();
383 foreach ($fields['Individual'] as $key => $value) {
384 if (!empty($fields['Household'][$key]) && !empty($fields['Organization'][$key])) {
385 $fields['Contact'][$key] = $value;
386 unset($fields['Organization'][$key],
387 $fields['Household'][$key],
388 $fields['Individual'][$key]);
391 if (array_key_exists('note', $fields['Contact'])) {
392 $noteTitle = $fields['Contact']['note']['title'];
393 $fields['Contact']['note']['title'] = $noteTitle . ': ' . ts('Body and Subject');
394 $fields['Contact']['note_body'] = array('title' => $noteTitle . ': ' . ts('Body Only'), 'name' => 'note_body');
395 $fields['Contact']['note_subject'] = array(
396 'title' => $noteTitle . ': ' . ts('Subject Only'),
397 'name' => 'note_subject',
402 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::CONTRIBUTE_EXPORT
)) {
403 if (CRM_Core_Permission
::access('CiviContribute')) {
404 $fields['Contribution'] = CRM_Contribute_BAO_Contribution
::exportableFields();
405 foreach (array('contribution_contact_id', 'contribution_soft_credit_name', 'contribution_soft_credit_amount', 'contribution_soft_credit_type', 'contribution_soft_credit_contribution_id') as $element) {
406 unset($fields['Contribution'][$element]);
408 $compArray['Contribution'] = ts('Contribution');
412 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::EVENT_EXPORT
)) {
413 if (CRM_Core_Permission
::access('CiviEvent')) {
414 $fields['Participant'] = CRM_Event_BAO_Participant
::exportableFields();
415 //get the component payment fields
416 if ($exportMode == CRM_Export_Form_Select
::EVENT_EXPORT
) {
417 require_once 'CRM/Export/BAO/Export.php';
418 $componentPaymentFields = array();
419 foreach (CRM_Export_BAO_Export
::componentPaymentFields() as $payField => $payTitle) {
420 $componentPaymentFields[$payField] = array('title' => $payTitle);
422 $fields['Participant'] = array_merge($fields['Participant'], $componentPaymentFields);
425 $compArray['Participant'] = ts('Participant');
429 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::MEMBER_EXPORT
)) {
430 if (CRM_Core_Permission
::access('CiviMember')) {
431 $fields['Membership'] = CRM_Member_BAO_Membership
::getMembershipFields($exportMode);
432 unset($fields['Membership']['membership_contact_id']);
433 $compArray['Membership'] = ts('Membership');
437 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::PLEDGE_EXPORT
)) {
438 if (CRM_Core_Permission
::access('CiviPledge')) {
439 $fields['Pledge'] = CRM_Pledge_BAO_Pledge
::exportableFields();
440 unset($fields['Pledge']['pledge_contact_id']);
441 $compArray['Pledge'] = ts('Pledge');
445 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::CASE_EXPORT
)) {
446 if (CRM_Core_Permission
::access('CiviCase')) {
447 $fields['Case'] = CRM_Case_BAO_Case
::exportableFields();
448 $compArray['Case'] = ts('Case');
450 $fields['Activity'] = CRM_Activity_BAO_Activity
::exportableFields('Case');
451 $compArray['Activity'] = ts('Case Activity');
453 unset($fields['Case']['case_contact_id']);
456 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::GRANT_EXPORT
)) {
457 if (CRM_Core_Permission
::access('CiviGrant')) {
458 $fields['Grant'] = CRM_Grant_BAO_Grant
::exportableFields();
459 unset($fields['Grant']['grant_contact_id']);
460 if ($mappingType == 'Search Builder') {
461 unset($fields['Grant']['grant_type_id']);
463 $compArray['Grant'] = ts('Grant');
467 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::ACTIVITY_EXPORT
)) {
468 $fields['Activity'] = CRM_Activity_BAO_Activity
::exportableFields('Activity');
469 $compArray['Activity'] = ts('Activity');
472 //Contact Sub Type For export
473 $contactSubTypes = array();
474 $subTypes = CRM_Contact_BAO_ContactType
::subTypeInfo();
476 foreach ($subTypes as $subType => $val) {
477 //adding subtype specific relationships CRM-5256
478 $csRelationships = array();
480 if ($mappingType == 'Export') {
481 $subTypeRelationshipTypes
482 = CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, NULL, NULL, $val['parent'],
483 FALSE, 'label', TRUE, $subType);
485 foreach ($subTypeRelationshipTypes as $key => $var) {
486 if (!array_key_exists($key, $fields[$val['parent']])) {
487 list($type) = explode('_', $key);
489 $csRelationships[$key]['title'] = $var;
490 $csRelationships[$key]['headerPattern'] = '/' . preg_quote($var, '/') . '/';
491 $csRelationships[$key]['export'] = TRUE;
492 $csRelationships[$key]['relationship_type_id'] = $type;
493 $csRelationships[$key]['related'] = TRUE;
494 $csRelationships[$key]['hasRelationType'] = 1;
499 $fields[$subType] = $fields[$val['parent']] +
$csRelationships;
501 //custom fields for sub type
502 $subTypeFields = CRM_Core_BAO_CustomField
::getFieldsForImport($subType);
503 $fields[$subType] +
= $subTypeFields;
505 if (!empty($subTypeFields) ||
!empty($csRelationships)) {
506 $contactSubTypes[$subType] = $val['label'];
512 foreach ($fields as $key => $value) {
514 foreach ($value as $key1 => $value1) {
515 //CRM-2676, replacing the conflict for same custom field name from different custom group.
516 $customGroupName = self
::getCustomGroupName($key1);
518 if ($customGroupName) {
519 $relatedMapperFields[$key][$key1] = $mapperFields[$key][$key1] = $customGroupName . ': ' . $value1['title'];
522 $relatedMapperFields[$key][$key1] = $mapperFields[$key][$key1] = $value1['title'];
524 if (isset($value1['hasLocationType'])) {
525 $hasLocationTypes[$key][$key1] = $value1['hasLocationType'];
528 if (isset($value1['hasRelationType'])) {
529 $hasRelationTypes[$key][$key1] = $value1['hasRelationType'];
530 unset($relatedMapperFields[$key][$key1]);
534 if (array_key_exists('related', $relatedMapperFields[$key])) {
535 unset($relatedMapperFields[$key]['related']);
539 $mapperKeys = array_keys($mapperFields);
541 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
543 $defaultLocationType = CRM_Core_BAO_LocationType
::getDefault();
545 // FIXME: dirty hack to make the default option show up first. This
546 // avoids a mozilla browser bug with defaults on dynamically constructed
548 if ($defaultLocationType) {
549 $defaultLocation = $locationTypes[$defaultLocationType->id
];
550 unset($locationTypes[$defaultLocationType->id
]);
551 $locationTypes = array($defaultLocationType->id
=> $defaultLocation) +
$locationTypes;
554 $locationTypes = array(' ' => ts('Primary')) +
$locationTypes;
556 // since we need a hierarchical list to display contact types & subtypes,
557 // this is what we going to display in first selector
558 $contactTypes = CRM_Contact_BAO_ContactType
::getSelectElements(FALSE, FALSE);
559 if ($mappingType == 'Search Builder') {
560 $contactTypes = array('Contact' => ts('Contacts')) +
$contactTypes;
563 $sel1 = array('' => ts('- select record type -')) +
$contactTypes +
$compArray;
565 foreach ($sel1 as $key => $sel) {
567 // sort everything BUT the contactType which is sorted seperately by
568 // an initial commit of CRM-13278 (check ksort above)
569 if (!in_array($key, $contactType)) {
570 asort($mapperFields[$key]);
572 $sel2[$key] = array('' => ts('- select field -')) +
$mapperFields[$key];
578 $phoneTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', 'phone_type_id');
579 $imProviders = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_IM', 'provider_id');
582 foreach ($sel1 as $k => $sel) {
584 foreach ($locationTypes as $key => $value) {
585 if (trim($key) != '') {
586 $sel4[$k]['phone'][$key] = &$phoneTypes;
587 $sel4[$k]['im'][$key] = &$imProviders;
593 foreach ($sel1 as $k => $sel) {
595 foreach ($mapperFields[$k] as $key => $value) {
596 if (isset($hasLocationTypes[$k][$key])) {
597 $sel3[$k][$key] = $locationTypes;
606 // Array for core fields and relationship custom data
607 $relationshipTypes = CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, NULL, NULL, NULL, TRUE);
609 if ($mappingType == 'Export') {
610 foreach ($sel1 as $k => $sel) {
612 foreach ($mapperFields[$k] as $field => $dontCare) {
613 if (isset($hasRelationTypes[$k][$field])) {
614 list($id, $first, $second) = explode('_', $field);
615 // FIX ME: For now let's not expose custom data related to relationship
616 $relationshipCustomFields = array();
617 //$relationshipCustomFields = self::getRelationTypeCustomGroupData( $id );
618 //asort($relationshipCustomFields);
620 $relationshipType = new CRM_Contact_BAO_RelationshipType();
621 $relationshipType->id
= $id;
622 if ($relationshipType->find(TRUE)) {
623 $direction = "contact_sub_type_$second";
624 if (isset($relationshipType->$direction)) {
625 $relatedFields = array_merge((array) $relatedMapperFields[$relationshipType->$direction], (array) $relationshipCustomFields);
628 $target_type = 'contact_type_' . $second;
629 $relatedFields = array_merge((array) $relatedMapperFields[$relationshipType->$target_type], (array) $relationshipCustomFields);
632 $relationshipType->free();
633 asort($relatedFields);
634 $sel5[$k][$field] = $relatedFields;
640 //Location Type for relationship fields
641 foreach ($sel5 as $k => $v) {
643 foreach ($v as $rel => $fields) {
644 foreach ($fields as $field => $fieldLabel) {
645 if (isset($hasLocationTypes[$k][$field])) {
646 $sel6[$k][$rel][$field] = $locationTypes;
653 //PhoneTypes for relationship fields
655 foreach ($sel6 as $k => $rel) {
657 foreach ($rel as $phonekey => $phonevalue) {
658 foreach ($locationTypes as $locType => $loc) {
659 if (trim($locType) != '') {
660 $sel7[$k][$phonekey]['phone'][$locType] = &$phoneTypes;
661 $sel7[$k][$phonekey]['im'][$locType] = &$imProviders;
669 //special fields that have location, hack for primary location
670 $specialFields = array(
672 'supplemental_address_1',
673 'supplemental_address_2',
676 'postal_code_suffix',
686 if (isset($mappingId)) {
689 list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingImProvider,
690 $mappingRelation, $mappingOperator, $mappingValue
691 ) = CRM_Core_BAO_Mapping
::getMappingFields($mappingId);
693 $blkCnt = count($mappingName);
694 if ($blkCnt >= $blockCount) {
695 $blockCount = $blkCnt +
1;
697 for ($x = 1; $x < $blockCount; $x++
) {
698 if (isset($mappingName[$x])) {
699 $colCnt = count($mappingName[$x]);
700 if ($colCnt >= $columnCount[$x]) {
701 $columnCount[$x] = $colCnt;
707 $form->_blockCount
= $blockCount;
708 $form->_columnCount
= $columnCount;
710 $form->set('blockCount', $form->_blockCount
);
711 $form->set('columnCount', $form->_columnCount
);
713 $defaults = $noneArray = $nullArray = array();
715 //used to warn for mismatch column count or mismatch mapping
717 for ($x = 1; $x < $blockCount; $x++
) {
719 for ($i = 0; $i < $columnCount[$x]; $i++
) {
721 $sel = &$form->addElement('hierselect', "mapper[$x][$i]", ts('Mapper for Field %1', array(1 => $i)), NULL);
724 if (isset($mappingId)) {
725 $locationId = isset($mappingLocation[$x][$i]) ?
$mappingLocation[$x][$i] : 0;
726 if (isset($mappingName[$x][$i])) {
727 if (is_array($mapperFields[$mappingContactType[$x][$i]])) {
729 if (isset($mappingRelation[$x][$i])) {
731 $contactDetails = strtolower(str_replace(" ", "_", $mappingName[$x][$i]));
732 $relLocationId = isset($mappingLocation[$x][$i]) ?
$mappingLocation[$x][$i] : 0;
733 if (!$relLocationId && in_array($mappingName[$x][$i], $specialFields)) {
734 $relLocationId = " ";
737 $relPhoneType = isset($mappingPhoneType[$x][$i]) ?
$mappingPhoneType[$x][$i] : NULL;
739 $defaults["mapper[$x][$i]"] = array(
740 $mappingContactType[$x][$i],
741 $mappingRelation[$x][$i],
744 $mappingName[$x][$i],
750 $noneArray[] = array($x, $i, 2);
752 if (!$phoneType && !$imProvider) {
753 $noneArray[] = array($x, $i, 3);
755 if (!$mappingName[$x][$i]) {
756 $noneArray[] = array($x, $i, 4);
758 if (!$relLocationId) {
759 $noneArray[] = array($x, $i, 5);
761 if (!$relPhoneType) {
762 $noneArray[] = array($x, $i, 6);
764 $noneArray[] = array($x, $i, 2);
767 $phoneType = isset($mappingPhoneType[$x][$i]) ?
$mappingPhoneType[$x][$i] : NULL;
768 $imProvider = isset($mappingImProvider[$x][$i]) ?
$mappingImProvider[$x][$i] : NULL;
769 if (!$locationId && in_array($mappingName[$x][$i], $specialFields)) {
773 $defaults["mapper[$x][$i]"] = array(
774 $mappingContactType[$x][$i],
775 $mappingName[$x][$i],
779 if (!$mappingName[$x][$i]) {
780 $noneArray[] = array($x, $i, 1);
783 $noneArray[] = array($x, $i, 2);
785 if (!$phoneType && !$imProvider) {
786 $noneArray[] = array($x, $i, 3);
789 $noneArray[] = array($x, $i, 4);
790 $noneArray[] = array($x, $i, 5);
791 $noneArray[] = array($x, $i, 6);
796 if (CRM_Utils_Array
::value($i, CRM_Utils_Array
::value($x, $mappingOperator))) {
797 $defaults["operator[$x][$i]"] = CRM_Utils_Array
::value($i, $mappingOperator[$x]);
800 if (CRM_Utils_Array
::value($i, CRM_Utils_Array
::value($x, $mappingValue))) {
801 $defaults["value[$x][$i]"] = CRM_Utils_Array
::value($i, $mappingValue[$x]);
806 //Fix for Search Builder
807 if ($mappingType == 'Export') {
814 $formValues = $form->exportValues();
816 if (empty($formValues)) {
817 // Incremented length for third select box(relationship type)
818 for ($k = 1; $k < $j; $k++
) {
819 $noneArray[] = array($x, $i, $k);
823 if (!empty($formValues['mapper'][$x])) {
824 foreach ($formValues['mapper'][$x] as $value) {
825 for ($k = 1; $k < $j; $k++
) {
826 if (!isset($formValues['mapper'][$x][$i][$k]) ||
827 (!$formValues['mapper'][$x][$i][$k])
829 $noneArray[] = array($x, $i, $k);
832 $nullArray[] = array($x, $i, $k);
838 for ($k = 1; $k < $j; $k++
) {
839 $noneArray[] = array($x, $i, $k);
844 //Fix for Search Builder
845 if ($mappingType == 'Export') {
846 if (!isset($mappingId) ||
$i >= count(reset($mappingName))) {
847 if (isset($formValues['mapper']) &&
848 isset($formValues['mapper'][$x][$i][1]) &&
849 array_key_exists($formValues['mapper'][$x][$i][1], $relationshipTypes)
851 $sel->setOptions(array($sel1, $sel2, $sel5, $sel6, $sel7, $sel3, $sel4));
854 $sel->setOptions(array($sel1, $sel2, $sel3, $sel4, $sel5, $sel6, $sel7));
858 $sel->setOptions(array($sel1, $sel2, $sel3, $sel4, $sel5, $sel6, $sel7));
862 $sel->setOptions(array($sel1, $sel2, $sel3, $sel4));
865 if ($mappingType == 'Search Builder') {
866 //CRM -2292, restricted array set
867 $operatorArray = array('' => ts('-operator-')) + CRM_Core_SelectValues
::getSearchBuilderOperators();
869 $form->add('select', "operator[$x][$i]", '', $operatorArray);
870 $form->add('text', "value[$x][$i]", '');
873 //end of columnCnt for
874 if ($mappingType == 'Search Builder') {
875 $title = ts('Another search field');
878 $title = ts('Select more fields');
881 $form->addElement('submit', "addMore[$x]", $title, array('class' => 'submit-link'));
885 $js = "<script type='text/javascript'>\n";
886 $formName = "document.{$name}";
887 if (!empty($nullArray)) {
888 $js .= "var nullArray = [";
891 foreach ($nullArray as $element) {
892 $key = "{$element[0]}, {$element[1]}, {$element[2]}";
893 if (!isset($seen[$key])) {
894 $elements[] = "[$key]";
898 $js .= implode(', ', $elements);
901 for (var i=0;i<nullArray.length;i++) {
902 if ( {$formName}['mapper['+nullArray[i][0]+']['+nullArray[i][1]+']['+nullArray[i][2]+']'] ) {
903 {$formName}['mapper['+nullArray[i][0]+']['+nullArray[i][1]+']['+nullArray[i][2]+']'].style.display = '';
908 if (!empty($noneArray)) {
909 $js .= "var noneArray = [";
912 foreach ($noneArray as $element) {
913 $key = "{$element[0]}, {$element[1]}, {$element[2]}";
914 if (!isset($seen[$key])) {
915 $elements[] = "[$key]";
919 $js .= implode(', ', $elements);
922 for (var i=0;i<noneArray.length;i++) {
923 if ( {$formName}['mapper['+noneArray[i][0]+']['+noneArray[i][1]+']['+noneArray[i][2]+']'] ) {
924 {$formName}['mapper['+noneArray[i][0]+']['+noneArray[i][1]+']['+noneArray[i][2]+']'].style.display = 'none';
929 $js .= "</script>\n";
931 $form->assign('initHideBoxes', $js);
932 $form->assign('columnCount', $columnCount);
933 $form->assign('blockCount', $blockCount);
934 $form->setDefaults($defaults);
936 $form->setDefaultAction('refresh');
940 * Function returns all custom fields with group title and
943 * @param int $relationshipTypeId
944 * Related relationship type id.
947 * all custom field titles
949 public function getRelationTypeCustomGroupData($relationshipTypeId) {
951 $customFields = CRM_Core_BAO_CustomField
::getFields('Relationship', NULL, NULL, $relationshipTypeId, NULL, NULL);
952 $groupTitle = array();
953 foreach ($customFields as $krelation => $vrelation) {
954 $groupTitle[$vrelation['label']] = $vrelation['groupTitle'] . '...' . $vrelation['label'];
961 * Function returns all Custom group Names.
963 * @param int $customfieldId
966 * @return null|string
967 * $customGroupName all custom group names
969 public static function getCustomGroupName($customfieldId) {
970 if ($customFieldId = CRM_Core_BAO_CustomField
::getKeyID($customfieldId)) {
971 $customGroupId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField', $customFieldId, 'custom_group_id');
972 $customGroupName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'title');
974 if (strlen($customGroupName) > 13) {
975 $customGroupName = substr($customGroupName, 0, 10) . '...';
978 return $customGroupName;
983 * Function returns associated array of elements, that will be passed for search
985 * @param array $params
986 * Associated array of submitted values.
988 * Row no of the fields.
992 * formatted associated array of elements
994 public static function formattedFields(&$params, $row = FALSE) {
997 if (empty($params) ||
!isset($params['mapper'])) {
1001 $types = array('Individual', 'Organization', 'Household');
1002 foreach ($params['mapper'] as $key => $value) {
1003 $contactType = NULL;
1004 foreach ($value as $k => $v) {
1005 if (in_array($v[0], $types)) {
1006 if ($contactType && $contactType != $v[0]) {
1007 CRM_Core_Error
::fatal(ts("Cannot have two clauses with different types: %1, %2",
1008 array(1 => $contactType, 2 => $v[0])
1011 $contactType = $v[0];
1013 if (!empty($v['1'])) {
1015 $v2 = CRM_Utils_Array
::value('2', $v);
1016 if ($v2 && trim($v2)) {
1017 $fldName .= "-{$v[2]}";
1020 $v3 = CRM_Utils_Array
::value('3', $v);
1021 if ($v3 && trim($v3)) {
1022 $fldName .= "-{$v[3]}";
1025 $value = $params['value'][$key][$k];
1026 if ($fldName == 'group' ||
$fldName == 'tag') {
1027 $value = trim($value);
1028 $value = str_replace('(', '', $value);
1029 $value = str_replace(')', '', $value);
1031 $v = explode(',', $value);
1033 foreach ($v as $i) {
1038 if ($v[0] == 'Contribution' && substr($fldName, 0, 7) != 'custom_'
1039 && substr($fldName, 0, 10) != 'financial_'
1040 && substr($fldName, 0, 8) != 'payment_') {
1041 if (substr($fldName, 0, 13) != 'contribution_') {
1042 $fldName = 'contribution_' . $fldName;
1046 // CRM-14563: we store checkbox, multi-select and adv-multi select custom field using separator, hence it
1047 // needs special handling.
1048 if ($cfID = CRM_Core_BAO_CustomField
::getKeyID($v[1])) {
1049 $isCustomField = TRUE;
1050 $customFieldType = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField', $cfID, 'html_type');
1051 $specialHTMLType = array(
1055 'Multi-Select State/Province',
1056 'Multi-Select Country',
1059 // override the operator to handle separator ( note: this might have some performance issues )
1060 if (in_array($customFieldType, $specialHTMLType)) {
1061 // FIX ME: != and few other operators are not handled
1062 $specialOperators = array('=', 'IN', 'LIKE');
1064 if (in_array($params['operator'][$key][$k], $specialOperators)) {
1065 $params['operator'][$key][$k] = 'RLIKE';
1070 // CRM-14983: verify if values are comma separated convert to array
1071 if (!is_array($value) && (strpos($value, ',') !== FALSE ||
strstr($value, '(')) && empty($isCustomField) && $params['operator'][$key][$k] == 'IN') {
1072 preg_match('#\((.*?)\)#', $value, $match);
1073 $tmpArray = explode(',', $match[1]);
1074 $value = array_combine(array_values($tmpArray), array_values($tmpArray));
1080 $params['operator'][$key][$k],
1089 $params['operator'][$key][$k],
1108 //add sortByCharacter values
1109 if (isset($params['sortByCharacter'])) {
1113 $params['sortByCharacter'],
1122 * @param array $params
1126 public static function &returnProperties(&$params) {
1128 'contact_type' => 1,
1129 'contact_sub_type' => 1,
1133 if (empty($params) ||
empty($params['mapper'])) {
1137 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
1138 foreach ($params['mapper'] as $key => $value) {
1139 foreach ($value as $k => $v) {
1141 if ($v[1] == 'groups' ||
$v[1] == 'tags') {
1145 if (isset($v[2]) && is_numeric($v[2])) {
1146 if (!array_key_exists('location', $fields)) {
1147 $fields['location'] = array();
1150 // make sure that we have a location fields and a location type for this
1151 $locationName = $locationTypes[$v[2]];
1152 if (!array_key_exists($locationName, $fields['location'])) {
1153 $fields['location'][$locationName] = array();
1154 $fields['location'][$locationName]['location_type'] = $v[2];
1157 if ($v[1] == 'phone' ||
$v[1] == 'email' ||
$v[1] == 'im') {
1158 // phone type handling
1160 $fields['location'][$locationName][$v[1] . "-" . $v[3]] = 1;
1163 $fields['location'][$locationName][$v[1]] = 1;
1167 $fields['location'][$locationName][$v[1]] = 1;
1181 * Save the mapping field info for search builder / export given the formvalues
1183 * @param array $params
1184 * Asscociated array of formvalues.
1185 * @param int $mappingId
1190 public static function saveMappingFields(&$params, $mappingId) {
1191 //delete mapping fields records for exixting mapping
1192 $mappingFields = new CRM_Core_DAO_MappingField();
1193 $mappingFields->mapping_id
= $mappingId;
1194 $mappingFields->delete();
1196 if (empty($params['mapper'])) {
1200 //save record in mapping field table
1201 foreach ($params['mapper'] as $key => $value) {
1203 foreach ($value as $k => $v) {
1205 if (!empty($v['1'])) {
1206 $saveMappingFields = new CRM_Core_DAO_MappingField();
1208 $saveMappingFields->mapping_id
= $mappingId;
1209 $saveMappingFields->name
= CRM_Utils_Array
::value('1', $v);
1210 $saveMappingFields->contact_type
= CRM_Utils_Array
::value('0', $v);
1211 $locationId = CRM_Utils_Array
::value('2', $v);
1212 $saveMappingFields->location_type_id
= is_numeric($locationId) ?
$locationId : NULL;
1214 if ($v[1] == 'phone') {
1215 $saveMappingFields->phone_type_id
= CRM_Utils_Array
::value('3', $v);
1217 elseif ($v[1] == 'im') {
1218 $saveMappingFields->im_provider_id
= CRM_Utils_Array
::value('3', $v);
1221 if (!empty($params['operator'])) {
1222 $saveMappingFields->operator
= CRM_Utils_Array
::value($k, $params['operator'][$key]);
1224 if (!empty($params['value'])) {
1225 $saveMappingFields->value
= CRM_Utils_Array
::value($k, $params['value'][$key]);
1227 // Handle mapping for 'related contact' fields
1228 if (count(explode('_', CRM_Utils_Array
::value('1', $v))) > 2) {
1229 list($id, $first, $second) = explode('_', CRM_Utils_Array
::value('1', $v));
1230 if (($first == 'a' && $second == 'b') ||
($first == 'b' && $second == 'a')) {
1232 if (!empty($v['2'])) {
1233 $saveMappingFields->name
= CRM_Utils_Array
::value('2', $v);
1235 elseif (!empty($v['4'])) {
1236 $saveMappingFields->name
= CRM_Utils_Array
::value('4', $v);
1239 if (is_numeric(CRM_Utils_Array
::value('3', $v))) {
1240 $locationTypeid = CRM_Utils_Array
::value('3', $v);
1242 elseif (is_numeric(CRM_Utils_Array
::value('5', $v))) {
1243 $locationTypeid = CRM_Utils_Array
::value('5', $v);
1246 if (is_numeric(CRM_Utils_Array
::value('4', $v))) {
1247 $phoneTypeid = CRM_Utils_Array
::value('4', $v);
1249 elseif (is_numeric(CRM_Utils_Array
::value('6', $v))) {
1250 $phoneTypeid = CRM_Utils_Array
::value('6', $v);
1253 $saveMappingFields->location_type_id
= is_numeric($locationTypeid) ?
$locationTypeid : NULL;
1254 $saveMappingFields->phone_type_id
= is_numeric($phoneTypeid) ?
$phoneTypeid : NULL;
1255 $saveMappingFields->relationship_type_id
= $id;
1256 $saveMappingFields->relationship_direction
= "{$first}_{$second}";
1260 $saveMappingFields->grouping
= $key;
1261 $saveMappingFields->column_number
= $colCnt;
1262 $saveMappingFields->save();
1264 $locationTypeid = $phoneTypeid = NULL;