3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 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->delete();
79 // delete from mapping table
80 $mapping = new CRM_Core_DAO_Mapping();
83 CRM_Core_Session
::setStatus(ts('Selected mapping has been deleted successfully.'), ts('Deleted'), 'success');
89 * Takes an associative array and creates a contact object.
91 * The function extract all the params it needs to initialize the create a
92 * contact object. the params array could contain additional unused name/value
95 * @param array $params
96 * An array of name/value pairs.
99 * CRM_Core_DAO_Mapper object on success, otherwise NULL
101 public static function add($params) {
102 $mapping = new CRM_Core_DAO_Mapping();
103 $mapping->copyValues($params);
110 * Get the list of mappings.
112 * @param string $mappingTypeId
116 * array of mapping name
118 public static function getMappings($mappingTypeId) {
120 $mappingDAO = new CRM_Core_DAO_Mapping();
121 $mappingDAO->mapping_type_id
= $mappingTypeId;
124 while ($mappingDAO->fetch()) {
125 $mapping[$mappingDAO->id
] = $mappingDAO->name
;
132 * Get the mapping fields.
134 * @param int $mappingId
138 * array of mapping fields
140 public static function getMappingFields($mappingId) {
141 //mapping is to be loaded from database
142 $mapping = new CRM_Core_DAO_MappingField();
143 $mapping->mapping_id
= $mappingId;
144 $mapping->orderBy('column_number');
147 $mappingName = $mappingLocation = $mappingContactType = $mappingPhoneType = array();
148 $mappingImProvider = $mappingRelation = $mappingOperator = $mappingValue = $mappingWebsiteType = array();
149 while ($mapping->fetch()) {
150 $mappingName[$mapping->grouping
][$mapping->column_number
] = $mapping->name
;
151 $mappingContactType[$mapping->grouping
][$mapping->column_number
] = $mapping->contact_type
;
153 if (!empty($mapping->location_type_id
)) {
154 $mappingLocation[$mapping->grouping
][$mapping->column_number
] = $mapping->location_type_id
;
157 if (!empty($mapping->phone_type_id
)) {
158 $mappingPhoneType[$mapping->grouping
][$mapping->column_number
] = $mapping->phone_type_id
;
161 // get IM service provider type id from mapping fields
162 if (!empty($mapping->im_provider_id
)) {
163 $mappingImProvider[$mapping->grouping
][$mapping->column_number
] = $mapping->im_provider_id
;
166 if (!empty($mapping->website_type_id
)) {
167 $mappingWebsiteType[$mapping->grouping
][$mapping->column_number
] = $mapping->website_type_id
;
170 if (!empty($mapping->relationship_type_id
)) {
171 $mappingRelation[$mapping->grouping
][$mapping->column_number
] = "{$mapping->relationship_type_id}_{$mapping->relationship_direction}";
174 if (!empty($mapping->operator
)) {
175 $mappingOperator[$mapping->grouping
][$mapping->column_number
] = $mapping->operator
;
178 if (!empty($mapping->value
)) {
179 $mappingValue[$mapping->grouping
][$mapping->column_number
] = $mapping->value
;
197 * Check Duplicate Mapping Name.
199 * @param string $nameField
201 * @param string $mapTypeId
206 public static function checkMapping($nameField, $mapTypeId) {
207 $mapping = new CRM_Core_DAO_Mapping();
208 $mapping->name
= $nameField;
209 $mapping->mapping_type_id
= $mapTypeId;
210 return (bool) $mapping->find(TRUE);
214 * Function returns associated array of elements, that will be passed for search
216 * @param int $smartGroupId
220 * associated array of elements
222 public static function getFormattedFields($smartGroupId) {
223 $returnFields = array();
225 //get the fields from mapping table
226 $dao = new CRM_Core_DAO_MappingField();
227 $dao->mapping_id
= $smartGroupId;
229 while ($dao->fetch()) {
230 $fldName = $dao->name
;
231 if ($dao->location_type_id
) {
232 $fldName .= "-{$dao->location_type_id}";
234 if ($dao->phone_type
) {
235 $fldName .= "-{$dao->phone_type}";
237 $returnFields[$fldName]['value'] = $dao->value
;
238 $returnFields[$fldName]['op'] = $dao->operator
;
239 $returnFields[$fldName]['grouping'] = $dao->grouping
;
241 return $returnFields;
245 * Build the mapping form.
247 * @param CRM_Core_Form $form
248 * @param string $mappingType
249 * (Export/Import/Search Builder).
250 * @param int $mappingId
251 * @param int $columnNo
252 * @param int $blockCount
253 * (no of blocks shown).
254 * @param NULL $exportMode
258 public static function buildMappingForm(&$form, $mappingType = 'Export', $mappingId = NULL, $columnNo, $blockCount = 3, $exportMode = NULL) {
259 if ($mappingType == 'Export') {
261 $columnCount = array('1' => $columnNo);
263 elseif ($mappingType == 'Search Builder') {
265 $columnCount = $columnNo;
268 //get the saved mapping details
270 if ($mappingType == 'Export') {
271 $form->applyFilter('saveMappingName', 'trim');
273 //to save the current mappings
274 if (!isset($mappingId)) {
275 $saveDetailsName = ts('Save this field mapping');
276 $form->add('text', 'saveMappingName', ts('Name'));
277 $form->add('text', 'saveMappingDesc', ts('Description'));
280 $form->assign('loadedMapping', $mappingId);
282 $params = array('id' => $mappingId);
284 $mappingDetails = CRM_Core_BAO_Mapping
::retrieve($params, $temp);
286 $form->assign('savedName', $mappingDetails->name
);
288 $form->add('hidden', 'mappingId', $mappingId);
290 $form->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL);
291 $saveDetailsName = ts('Save as a new field mapping');
292 $form->add('text', 'saveMappingName', ts('Name'));
293 $form->add('text', 'saveMappingDesc', ts('Description'));
296 $form->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, array('onclick' => "showSaveDetails(this)"));
297 $form->addFormRule(array('CRM_Export_Form_Map', 'formRule'), $form->get('mappingTypeId'));
299 elseif ($mappingType == 'Search Builder') {
300 $form->addElement('submit', 'addBlock', ts('Also include contacts where'),
301 array('class' => 'submit-link')
306 $hasLocationTypes = array();
307 $hasRelationTypes = array();
310 if ($mappingType == 'Export') {
313 elseif ($mappingType == 'Search Builder') {
317 $contactType = array('Individual', 'Household', 'Organization');
318 foreach ($contactType as $value) {
319 $contactFields = CRM_Contact_BAO_Contact
::exportableFields($value, FALSE, $required);
320 $contactFields = array_merge($contactFields, CRM_Contact_BAO_Query_Hook
::singleton()->getFields());
322 // exclude the address options disabled in the Address Settings
323 $fields[$value] = CRM_Core_BAO_Address
::validateAddressOptions($contactFields);
324 ksort($fields[$value]);
325 if ($mappingType == 'Export') {
326 $relationships = array();
327 $relationshipTypes = CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, NULL, NULL, $value);
328 asort($relationshipTypes);
330 foreach ($relationshipTypes as $key => $var) {
331 list($type) = explode('_', $key);
333 $relationships[$key]['title'] = $var;
334 $relationships[$key]['headerPattern'] = '/' . preg_quote($var, '/') . '/';
335 $relationships[$key]['export'] = TRUE;
336 $relationships[$key]['relationship_type_id'] = $type;
337 $relationships[$key]['related'] = TRUE;
338 $relationships[$key]['hasRelationType'] = 1;
341 if (!empty($relationships)) {
342 $fields[$value] = array_merge($fields[$value],
343 array('related' => array('title' => ts('- related contact info -'))),
350 //get the current employer for mapping.
352 $fields['Individual']['current_employer']['title'] = ts('Current Employer');
355 // add component fields
356 $compArray = array();
358 //we need to unset groups, tags, notes for component export
359 if ($exportMode != CRM_Export_Form_Select
::CONTACT_EXPORT
) {
365 unset($fields['Individual'][$value]);
366 unset($fields['Household'][$value]);
367 unset($fields['Organization'][$value]);
371 if ($mappingType == 'Search Builder') {
372 //build the common contact fields array.
373 $fields['Contact'] = array();
374 foreach ($fields['Individual'] as $key => $value) {
375 if (!empty($fields['Household'][$key]) && !empty($fields['Organization'][$key])) {
376 $fields['Contact'][$key] = $value;
377 unset($fields['Organization'][$key],
378 $fields['Household'][$key],
379 $fields['Individual'][$key]);
382 if (array_key_exists('note', $fields['Contact'])) {
383 $noteTitle = $fields['Contact']['note']['title'];
384 $fields['Contact']['note']['title'] = $noteTitle . ': ' . ts('Body and Subject');
385 $fields['Contact']['note_body'] = array('title' => $noteTitle . ': ' . ts('Body Only'), 'name' => 'note_body');
386 $fields['Contact']['note_subject'] = array(
387 'title' => $noteTitle . ': ' . ts('Subject Only'),
388 'name' => 'note_subject',
393 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::CONTRIBUTE_EXPORT
)) {
394 if (CRM_Core_Permission
::access('CiviContribute')) {
395 $fields['Contribution'] = CRM_Contribute_BAO_Contribution
::exportableFields();
396 foreach (array('contribution_contact_id', 'contribution_soft_credit_name', 'contribution_soft_credit_amount', 'contribution_soft_credit_type', 'contribution_soft_credit_contribution_id') as $element) {
397 unset($fields['Contribution'][$element]);
399 $compArray['Contribution'] = ts('Contribution');
403 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::EVENT_EXPORT
)) {
404 if (CRM_Core_Permission
::access('CiviEvent')) {
405 $fields['Participant'] = CRM_Event_BAO_Participant
::exportableFields();
406 //get the component payment fields
407 if ($exportMode == CRM_Export_Form_Select
::EVENT_EXPORT
) {
408 $componentPaymentFields = array();
409 foreach (CRM_Export_BAO_Export
::componentPaymentFields() as $payField => $payTitle) {
410 $componentPaymentFields[$payField] = array('title' => $payTitle);
412 $fields['Participant'] = array_merge($fields['Participant'], $componentPaymentFields);
415 $compArray['Participant'] = ts('Participant');
419 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::MEMBER_EXPORT
)) {
420 if (CRM_Core_Permission
::access('CiviMember')) {
421 $fields['Membership'] = CRM_Member_BAO_Membership
::getMembershipFields($exportMode);
422 unset($fields['Membership']['membership_contact_id']);
423 $compArray['Membership'] = ts('Membership');
427 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::PLEDGE_EXPORT
)) {
428 if (CRM_Core_Permission
::access('CiviPledge')) {
429 $fields['Pledge'] = CRM_Pledge_BAO_Pledge
::exportableFields();
430 unset($fields['Pledge']['pledge_contact_id']);
431 $compArray['Pledge'] = ts('Pledge');
435 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::CASE_EXPORT
)) {
436 if (CRM_Core_Permission
::access('CiviCase')) {
437 $fields['Case'] = CRM_Case_BAO_Case
::exportableFields();
438 $compArray['Case'] = ts('Case');
440 $fields['Activity'] = CRM_Activity_BAO_Activity
::exportableFields('Case');
441 $compArray['Activity'] = ts('Case Activity');
443 unset($fields['Case']['case_contact_id']);
446 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::GRANT_EXPORT
)) {
447 if (CRM_Core_Permission
::access('CiviGrant')) {
448 $fields['Grant'] = CRM_Grant_BAO_Grant
::exportableFields();
449 unset($fields['Grant']['grant_contact_id']);
450 if ($mappingType == 'Search Builder') {
451 unset($fields['Grant']['grant_type_id']);
453 $compArray['Grant'] = ts('Grant');
457 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::ACTIVITY_EXPORT
)) {
458 $fields['Activity'] = CRM_Activity_BAO_Activity
::exportableFields('Activity');
459 $compArray['Activity'] = ts('Activity');
462 //Contact Sub Type For export
463 $contactSubTypes = array();
464 $subTypes = CRM_Contact_BAO_ContactType
::subTypeInfo();
466 foreach ($subTypes as $subType => $val) {
467 //adding subtype specific relationships CRM-5256
468 $csRelationships = array();
470 if ($mappingType == 'Export') {
471 $subTypeRelationshipTypes
472 = CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, NULL, NULL, $val['parent'],
473 FALSE, 'label', TRUE, $subType);
475 foreach ($subTypeRelationshipTypes as $key => $var) {
476 if (!array_key_exists($key, $fields[$val['parent']])) {
477 list($type) = explode('_', $key);
479 $csRelationships[$key]['title'] = $var;
480 $csRelationships[$key]['headerPattern'] = '/' . preg_quote($var, '/') . '/';
481 $csRelationships[$key]['export'] = TRUE;
482 $csRelationships[$key]['relationship_type_id'] = $type;
483 $csRelationships[$key]['related'] = TRUE;
484 $csRelationships[$key]['hasRelationType'] = 1;
489 $fields[$subType] = $fields[$val['parent']] +
$csRelationships;
491 //custom fields for sub type
492 $subTypeFields = CRM_Core_BAO_CustomField
::getFieldsForImport($subType);
493 $fields[$subType] +
= $subTypeFields;
495 if (!empty($subTypeFields) ||
!empty($csRelationships)) {
496 $contactSubTypes[$subType] = $val['label'];
502 foreach ($fields as $key => $value) {
504 foreach ($value as $key1 => $value1) {
505 //CRM-2676, replacing the conflict for same custom field name from different custom group.
506 $customGroupName = self
::getCustomGroupName($key1);
508 if ($customGroupName) {
509 $relatedMapperFields[$key][$key1] = $mapperFields[$key][$key1] = $customGroupName . ': ' . $value1['title'];
512 $relatedMapperFields[$key][$key1] = $mapperFields[$key][$key1] = $value1['title'];
514 if (isset($value1['hasLocationType'])) {
515 $hasLocationTypes[$key][$key1] = $value1['hasLocationType'];
518 if (isset($value1['hasRelationType'])) {
519 $hasRelationTypes[$key][$key1] = $value1['hasRelationType'];
520 unset($relatedMapperFields[$key][$key1]);
524 if (array_key_exists('related', $relatedMapperFields[$key])) {
525 unset($relatedMapperFields[$key]['related']);
529 $mapperKeys = array_keys($mapperFields);
531 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
533 $defaultLocationType = CRM_Core_BAO_LocationType
::getDefault();
535 // FIXME: dirty hack to make the default option show up first. This
536 // avoids a mozilla browser bug with defaults on dynamically constructed
538 if ($defaultLocationType) {
539 $defaultLocation = $locationTypes[$defaultLocationType->id
];
540 unset($locationTypes[$defaultLocationType->id
]);
541 $locationTypes = array($defaultLocationType->id
=> $defaultLocation) +
$locationTypes;
544 $locationTypes = array(' ' => ts('Primary')) +
$locationTypes;
546 // since we need a hierarchical list to display contact types & subtypes,
547 // this is what we going to display in first selector
548 $contactTypes = CRM_Contact_BAO_ContactType
::getSelectElements(FALSE, FALSE);
549 if ($mappingType == 'Search Builder') {
550 $contactTypes = array('Contact' => ts('Contacts')) +
$contactTypes;
553 $sel1 = array('' => ts('- select record type -')) +
$contactTypes +
$compArray;
555 foreach ($sel1 as $key => $sel) {
557 // sort everything BUT the contactType which is sorted seperately by
558 // an initial commit of CRM-13278 (check ksort above)
559 if (!in_array($key, $contactType)) {
560 asort($mapperFields[$key]);
562 $sel2[$key] = array('' => ts('- select field -')) +
$mapperFields[$key];
568 $phoneTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', 'phone_type_id');
569 $imProviders = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_IM', 'provider_id');
572 foreach ($sel1 as $k => $sel) {
574 foreach ($locationTypes as $key => $value) {
575 if (trim($key) != '') {
576 $sel4[$k]['phone'][$key] = &$phoneTypes;
577 $sel4[$k]['im'][$key] = &$imProviders;
583 foreach ($sel1 as $k => $sel) {
585 foreach ($mapperFields[$k] as $key => $value) {
586 if (isset($hasLocationTypes[$k][$key])) {
587 $sel3[$k][$key] = $locationTypes;
596 // Array for core fields and relationship custom data
597 $relationshipTypes = CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, NULL, NULL, NULL, TRUE);
599 if ($mappingType == 'Export') {
600 foreach ($sel1 as $k => $sel) {
602 foreach ($mapperFields[$k] as $field => $dontCare) {
603 if (isset($hasRelationTypes[$k][$field])) {
604 list($id, $first, $second) = explode('_', $field);
605 // FIX ME: For now let's not expose custom data related to relationship
606 $relationshipCustomFields = array();
607 //$relationshipCustomFields = self::getRelationTypeCustomGroupData( $id );
608 //asort($relationshipCustomFields);
610 $relationshipType = new CRM_Contact_BAO_RelationshipType();
611 $relationshipType->id
= $id;
612 if ($relationshipType->find(TRUE)) {
613 $direction = "contact_sub_type_$second";
614 $target_type = 'contact_type_' . $second;
615 if (isset($relationshipType->$direction)) {
616 $relatedFields = array_merge((array) $relatedMapperFields[$relationshipType->$direction], (array) $relationshipCustomFields);
618 elseif (isset($relationshipType->$target_type)) {
619 $relatedFields = array_merge((array) $relatedMapperFields[$relationshipType->$target_type], (array) $relationshipCustomFields);
622 $relationshipType->free();
623 asort($relatedFields);
624 $sel5[$k][$field] = $relatedFields;
630 //Location Type for relationship fields
631 foreach ($sel5 as $k => $v) {
633 foreach ($v as $rel => $fields) {
634 foreach ($fields as $field => $fieldLabel) {
635 if (isset($hasLocationTypes[$k][$field])) {
636 $sel6[$k][$rel][$field] = $locationTypes;
643 //PhoneTypes for relationship fields
645 foreach ($sel6 as $k => $rel) {
647 foreach ($rel as $phonekey => $phonevalue) {
648 foreach ($locationTypes as $locType => $loc) {
649 if (trim($locType) != '') {
650 $sel7[$k][$phonekey]['phone'][$locType] = &$phoneTypes;
651 $sel7[$k][$phonekey]['im'][$locType] = &$imProviders;
659 //special fields that have location, hack for primary location
660 $specialFields = array(
662 'supplemental_address_1',
663 'supplemental_address_2',
666 'postal_code_suffix',
676 if (isset($mappingId)) {
679 list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingImProvider,
680 $mappingRelation, $mappingOperator, $mappingValue
681 ) = CRM_Core_BAO_Mapping
::getMappingFields($mappingId);
683 $blkCnt = count($mappingName);
684 if ($blkCnt >= $blockCount) {
685 $blockCount = $blkCnt +
1;
687 for ($x = 1; $x < $blockCount; $x++
) {
688 if (isset($mappingName[$x])) {
689 $colCnt = count($mappingName[$x]);
690 if ($colCnt >= $columnCount[$x]) {
691 $columnCount[$x] = $colCnt;
697 $form->_blockCount
= $blockCount;
698 $form->_columnCount
= $columnCount;
700 $form->set('blockCount', $form->_blockCount
);
701 $form->set('columnCount', $form->_columnCount
);
703 $defaults = $noneArray = $nullArray = array();
705 //used to warn for mismatch column count or mismatch mapping
707 for ($x = 1; $x < $blockCount; $x++
) {
709 for ($i = 0; $i < $columnCount[$x]; $i++
) {
711 $sel = &$form->addElement('hierselect', "mapper[$x][$i]", ts('Mapper for Field %1', array(1 => $i)), NULL);
714 if (isset($mappingId)) {
715 $locationId = isset($mappingLocation[$x][$i]) ?
$mappingLocation[$x][$i] : 0;
716 if (isset($mappingName[$x][$i])) {
717 if (is_array($mapperFields[$mappingContactType[$x][$i]])) {
719 if (isset($mappingRelation[$x][$i])) {
721 $contactDetails = strtolower(str_replace(" ", "_", $mappingName[$x][$i]));
722 $relLocationId = isset($mappingLocation[$x][$i]) ?
$mappingLocation[$x][$i] : 0;
723 if (!$relLocationId && in_array($mappingName[$x][$i], $specialFields)) {
724 $relLocationId = " ";
727 $relPhoneType = isset($mappingPhoneType[$x][$i]) ?
$mappingPhoneType[$x][$i] : NULL;
729 $defaults["mapper[$x][$i]"] = array(
730 $mappingContactType[$x][$i],
731 $mappingRelation[$x][$i],
734 $mappingName[$x][$i],
740 $noneArray[] = array($x, $i, 2);
742 if (!$phoneType && !$imProvider) {
743 $noneArray[] = array($x, $i, 3);
745 if (!$mappingName[$x][$i]) {
746 $noneArray[] = array($x, $i, 4);
748 if (!$relLocationId) {
749 $noneArray[] = array($x, $i, 5);
751 if (!$relPhoneType) {
752 $noneArray[] = array($x, $i, 6);
754 $noneArray[] = array($x, $i, 2);
757 $phoneType = isset($mappingPhoneType[$x][$i]) ?
$mappingPhoneType[$x][$i] : NULL;
758 $imProvider = isset($mappingImProvider[$x][$i]) ?
$mappingImProvider[$x][$i] : NULL;
759 if (!$locationId && in_array($mappingName[$x][$i], $specialFields)) {
763 $defaults["mapper[$x][$i]"] = array(
764 $mappingContactType[$x][$i],
765 $mappingName[$x][$i],
769 if (!$mappingName[$x][$i]) {
770 $noneArray[] = array($x, $i, 1);
773 $noneArray[] = array($x, $i, 2);
775 if (!$phoneType && !$imProvider) {
776 $noneArray[] = array($x, $i, 3);
779 $noneArray[] = array($x, $i, 4);
780 $noneArray[] = array($x, $i, 5);
781 $noneArray[] = array($x, $i, 6);
786 if (CRM_Utils_Array
::value($i, CRM_Utils_Array
::value($x, $mappingOperator))) {
787 $defaults["operator[$x][$i]"] = CRM_Utils_Array
::value($i, $mappingOperator[$x]);
790 if (CRM_Utils_Array
::value($i, CRM_Utils_Array
::value($x, $mappingValue))) {
791 $defaults["value[$x][$i]"] = CRM_Utils_Array
::value($i, $mappingValue[$x]);
796 //Fix for Search Builder
797 if ($mappingType == 'Export') {
804 $formValues = $form->exportValues();
806 if (empty($formValues)) {
807 // Incremented length for third select box(relationship type)
808 for ($k = 1; $k < $j; $k++
) {
809 $noneArray[] = array($x, $i, $k);
813 if (!empty($formValues['mapper'][$x])) {
814 foreach ($formValues['mapper'][$x] as $value) {
815 for ($k = 1; $k < $j; $k++
) {
816 if (!isset($formValues['mapper'][$x][$i][$k]) ||
817 (!$formValues['mapper'][$x][$i][$k])
819 $noneArray[] = array($x, $i, $k);
822 $nullArray[] = array($x, $i, $k);
828 for ($k = 1; $k < $j; $k++
) {
829 $noneArray[] = array($x, $i, $k);
834 //Fix for Search Builder
835 if ($mappingType == 'Export') {
836 if (!isset($mappingId) ||
$i >= count(reset($mappingName))) {
837 if (isset($formValues['mapper']) &&
838 isset($formValues['mapper'][$x][$i][1]) &&
839 array_key_exists($formValues['mapper'][$x][$i][1], $relationshipTypes)
841 $sel->setOptions(array($sel1, $sel2, $sel5, $sel6, $sel7, $sel3, $sel4));
844 $sel->setOptions(array($sel1, $sel2, $sel3, $sel4, $sel5, $sel6, $sel7));
848 $sel->setOptions(array($sel1, $sel2, $sel3, $sel4, $sel5, $sel6, $sel7));
852 $sel->setOptions(array($sel1, $sel2, $sel3, $sel4));
855 if ($mappingType == 'Search Builder') {
856 //CRM -2292, restricted array set
857 $operatorArray = array('' => ts('-operator-')) + CRM_Core_SelectValues
::getSearchBuilderOperators();
859 $form->add('select', "operator[$x][$i]", '', $operatorArray);
860 $form->add('text', "value[$x][$i]", '');
863 //end of columnCnt for
864 if ($mappingType == 'Search Builder') {
865 $title = ts('Another search field');
868 $title = ts('Select more fields');
871 $form->addElement('submit', "addMore[$x]", $title, array('class' => 'submit-link'));
875 $js = "<script type='text/javascript'>\n";
876 $formName = "document.{$name}";
877 if (!empty($nullArray)) {
878 $js .= "var nullArray = [";
881 foreach ($nullArray as $element) {
882 $key = "{$element[0]}, {$element[1]}, {$element[2]}";
883 if (!isset($seen[$key])) {
884 $elements[] = "[$key]";
888 $js .= implode(', ', $elements);
891 for (var i=0;i<nullArray.length;i++) {
892 if ( {$formName}['mapper['+nullArray[i][0]+']['+nullArray[i][1]+']['+nullArray[i][2]+']'] ) {
893 {$formName}['mapper['+nullArray[i][0]+']['+nullArray[i][1]+']['+nullArray[i][2]+']'].style.display = '';
898 if (!empty($noneArray)) {
899 $js .= "var noneArray = [";
902 foreach ($noneArray as $element) {
903 $key = "{$element[0]}, {$element[1]}, {$element[2]}";
904 if (!isset($seen[$key])) {
905 $elements[] = "[$key]";
909 $js .= implode(', ', $elements);
912 for (var i=0;i<noneArray.length;i++) {
913 if ( {$formName}['mapper['+noneArray[i][0]+']['+noneArray[i][1]+']['+noneArray[i][2]+']'] ) {
914 {$formName}['mapper['+noneArray[i][0]+']['+noneArray[i][1]+']['+noneArray[i][2]+']'].style.display = 'none';
919 $js .= "</script>\n";
921 $form->assign('initHideBoxes', $js);
922 $form->assign('columnCount', $columnCount);
923 $form->assign('blockCount', $blockCount);
924 $form->setDefaults($defaults);
926 $form->setDefaultAction('refresh');
930 * Function returns all custom fields with group title and
933 * @param int $relationshipTypeId
934 * Related relationship type id.
937 * all custom field titles
939 public function getRelationTypeCustomGroupData($relationshipTypeId) {
941 $customFields = CRM_Core_BAO_CustomField
::getFields('Relationship', NULL, NULL, $relationshipTypeId, NULL, NULL);
942 $groupTitle = array();
943 foreach ($customFields as $krelation => $vrelation) {
944 $groupTitle[$vrelation['label']] = $vrelation['groupTitle'] . '...' . $vrelation['label'];
951 * Function returns all Custom group Names.
953 * @param int $customfieldId
956 * @return null|string
957 * $customGroupName all custom group names
959 public static function getCustomGroupName($customfieldId) {
960 if ($customFieldId = CRM_Core_BAO_CustomField
::getKeyID($customfieldId)) {
961 $customGroupId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField', $customFieldId, 'custom_group_id');
962 $customGroupName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'title');
964 if (strlen($customGroupName) > 13) {
965 $customGroupName = substr($customGroupName, 0, 10) . '...';
968 return $customGroupName;
973 * Function returns associated array of elements, that will be passed for search
975 * @param array $params
976 * Associated array of submitted values.
978 * Row no of the fields.
982 * formatted associated array of elements
984 public static function formattedFields(&$params, $row = FALSE) {
987 if (empty($params) ||
!isset($params['mapper'])) {
991 $types = array('Individual', 'Organization', 'Household');
992 foreach ($params['mapper'] as $key => $value) {
994 foreach ($value as $k => $v) {
995 if (in_array($v[0], $types)) {
996 if ($contactType && $contactType != $v[0]) {
997 CRM_Core_Error
::fatal(ts("Cannot have two clauses with different types: %1, %2",
998 array(1 => $contactType, 2 => $v[0])
1001 $contactType = $v[0];
1003 if (!empty($v['1'])) {
1005 $v2 = CRM_Utils_Array
::value('2', $v);
1006 if ($v2 && trim($v2)) {
1007 $fldName .= "-{$v[2]}";
1010 $v3 = CRM_Utils_Array
::value('3', $v);
1011 if ($v3 && trim($v3)) {
1012 $fldName .= "-{$v[3]}";
1015 $value = $params['value'][$key][$k];
1016 if ($fldName == 'group' ||
$fldName == 'tag') {
1017 $value = trim($value);
1019 $v = explode(',', $value);
1021 foreach ($v as $i) {
1026 if ($v[0] == 'Contribution' && substr($fldName, 0, 7) != 'custom_'
1027 && substr($fldName, 0, 10) != 'financial_'
1028 && substr($fldName, 0, 8) != 'payment_') {
1029 if (substr($fldName, 0, 13) != 'contribution_') {
1030 $fldName = 'contribution_' . $fldName;
1034 // CRM-14983: verify if values are comma separated convert to array
1035 if (!is_array($value) && strstr($params['operator'][$key][$k], 'IN')) {
1036 $value = explode(',', $value);
1037 $value = array($params['operator'][$key][$k] => $value);
1043 $params['operator'][$key][$k],
1052 $params['operator'][$key][$k],
1071 //add sortByCharacter values
1072 if (isset($params['sortByCharacter'])) {
1076 $params['sortByCharacter'],
1085 * @param array $params
1089 public static function &returnProperties(&$params) {
1091 'contact_type' => 1,
1092 'contact_sub_type' => 1,
1096 if (empty($params) ||
empty($params['mapper'])) {
1100 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
1101 foreach ($params['mapper'] as $key => $value) {
1102 foreach ($value as $k => $v) {
1104 if ($v[1] == 'groups' ||
$v[1] == 'tags') {
1108 if (isset($v[2]) && is_numeric($v[2])) {
1109 if (!array_key_exists('location', $fields)) {
1110 $fields['location'] = array();
1113 // make sure that we have a location fields and a location type for this
1114 $locationName = $locationTypes[$v[2]];
1115 if (!array_key_exists($locationName, $fields['location'])) {
1116 $fields['location'][$locationName] = array();
1117 $fields['location'][$locationName]['location_type'] = $v[2];
1120 if ($v[1] == 'phone' ||
$v[1] == 'email' ||
$v[1] == 'im') {
1121 // phone type handling
1123 $fields['location'][$locationName][$v[1] . "-" . $v[3]] = 1;
1126 $fields['location'][$locationName][$v[1]] = 1;
1130 $fields['location'][$locationName][$v[1]] = 1;
1144 * Save the mapping field info for search builder / export given the formvalues
1146 * @param array $params
1147 * Asscociated array of formvalues.
1148 * @param int $mappingId
1153 public static function saveMappingFields(&$params, $mappingId) {
1154 //delete mapping fields records for exixting mapping
1155 $mappingFields = new CRM_Core_DAO_MappingField();
1156 $mappingFields->mapping_id
= $mappingId;
1157 $mappingFields->delete();
1159 if (empty($params['mapper'])) {
1163 //save record in mapping field table
1164 foreach ($params['mapper'] as $key => $value) {
1166 foreach ($value as $k => $v) {
1168 if (!empty($v['1'])) {
1169 $saveMappingFields = new CRM_Core_DAO_MappingField();
1171 $saveMappingFields->mapping_id
= $mappingId;
1172 $saveMappingFields->name
= CRM_Utils_Array
::value('1', $v);
1173 $saveMappingFields->contact_type
= CRM_Utils_Array
::value('0', $v);
1174 $locationId = CRM_Utils_Array
::value('2', $v);
1175 $saveMappingFields->location_type_id
= is_numeric($locationId) ?
$locationId : NULL;
1177 if ($v[1] == 'phone') {
1178 $saveMappingFields->phone_type_id
= CRM_Utils_Array
::value('3', $v);
1180 elseif ($v[1] == 'im') {
1181 $saveMappingFields->im_provider_id
= CRM_Utils_Array
::value('3', $v);
1184 if (!empty($params['operator'])) {
1185 $saveMappingFields->operator
= CRM_Utils_Array
::value($k, $params['operator'][$key]);
1187 if (!empty($params['value'])) {
1188 $saveMappingFields->value
= CRM_Utils_Array
::value($k, $params['value'][$key]);
1190 // Handle mapping for 'related contact' fields
1191 if (count(explode('_', CRM_Utils_Array
::value('1', $v))) > 2) {
1192 list($id, $first, $second) = explode('_', CRM_Utils_Array
::value('1', $v));
1193 if (($first == 'a' && $second == 'b') ||
($first == 'b' && $second == 'a')) {
1195 if (!empty($v['2'])) {
1196 $saveMappingFields->name
= CRM_Utils_Array
::value('2', $v);
1198 elseif (!empty($v['4'])) {
1199 $saveMappingFields->name
= CRM_Utils_Array
::value('4', $v);
1202 if (is_numeric(CRM_Utils_Array
::value('3', $v))) {
1203 $locationTypeid = CRM_Utils_Array
::value('3', $v);
1205 elseif (is_numeric(CRM_Utils_Array
::value('5', $v))) {
1206 $locationTypeid = CRM_Utils_Array
::value('5', $v);
1209 if (is_numeric(CRM_Utils_Array
::value('4', $v))) {
1210 $phoneTypeid = CRM_Utils_Array
::value('4', $v);
1212 elseif (is_numeric(CRM_Utils_Array
::value('6', $v))) {
1213 $phoneTypeid = CRM_Utils_Array
::value('6', $v);
1216 $saveMappingFields->location_type_id
= is_numeric($locationTypeid) ?
$locationTypeid : NULL;
1217 $saveMappingFields->phone_type_id
= is_numeric($phoneTypeid) ?
$phoneTypeid : NULL;
1218 $saveMappingFields->relationship_type_id
= $id;
1219 $saveMappingFields->relationship_direction
= "{$first}_{$second}";
1223 $saveMappingFields->grouping
= $key;
1224 $saveMappingFields->column_number
= $colCnt;
1225 $saveMappingFields->save();
1227 $locationTypeid = $phoneTypeid = NULL;