3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
35 class CRM_Core_BAO_Mapping
extends CRM_Core_DAO_Mapping
{
40 function __construct() {
41 parent
::__construct();
45 * Takes a bunch of params that are needed to match certain criteria and
46 * retrieves the relevant objects. Typically the valid params are only
47 * contact_id. We'll tweak this function to be more full featured over a period
48 * of time. This is the inverse function of create. It also stores all the retrieved
49 * values in the default array
51 * @param array $params (reference ) an assoc array of name/value pairs
52 * @param array $defaults (reference ) an assoc array to hold the flattened values
54 * @return object CRM_Core_DAO_Mapping object on success, otherwise null
58 static function retrieve(&$params, &$defaults) {
59 $mapping = new CRM_Core_DAO_Mapping();
60 $mapping->copyValues($params);
61 if ($mapping->find(TRUE)) {
62 CRM_Core_DAO
::storeValues($mapping, $defaults);
69 * Function to delete the mapping
71 * @param int $id mapping id
78 static function del($id) {
79 // delete from mapping_field table
80 $mappingField = new CRM_Core_DAO_MappingField();
81 $mappingField->mapping_id
= $id;
82 $mappingField->find();
83 while ($mappingField->fetch()) {
84 $mappingField->delete();
87 // delete from mapping table
88 $mapping = new CRM_Core_DAO_Mapping();
91 CRM_Core_Session
::setStatus(ts('Selected mapping has been deleted successfully.'), ts('Deleted'), 'success');
97 * takes an associative array and creates a contact object
99 * The function extract all the params it needs to initialize the create a
100 * contact object. the params array could contain additional unused name/value
103 * @param array $params (reference) an assoc array of name/value pairs
105 * @return object CRM_Core_DAO_Mapper object on success, otherwise null
109 static function add(&$params) {
110 $mapping = new CRM_Core_DAO_Mapping();
111 $mapping->copyValues($params);
118 * function to get the list of mappings
120 * @params string $mappingTypeId mapping type id
122 * @return array $mapping array of mapping name
126 static function getMappings($mappingTypeId) {
128 $mappingDAO = new CRM_Core_DAO_Mapping();
129 $mappingDAO->mapping_type_id
= $mappingTypeId;
132 while ($mappingDAO->fetch()) {
133 $mapping[$mappingDAO->id
] = $mappingDAO->name
;
140 * function to get the mapping fields
142 * @params int $mappingId mapping id
144 * @return array $mappingFields array of mapping fields
149 static function getMappingFields($mappingId) {
150 //mapping is to be loaded from database
151 $mapping = new CRM_Core_DAO_MappingField();
152 $mapping->mapping_id
= $mappingId;
153 $mapping->orderBy('column_number');
156 $mappingName = $mappingLocation = $mappingContactType = $mappingPhoneType = array();
157 $mappingImProvider = $mappingRelation = $mappingOperator = $mappingValue = $mappingWebsiteType = array();
158 while ($mapping->fetch()) {
159 $mappingName[$mapping->grouping
][$mapping->column_number
] = $mapping->name
;
160 $mappingContactType[$mapping->grouping
][$mapping->column_number
] = $mapping->contact_type
;
162 if (!empty($mapping->location_type_id
)) {
163 $mappingLocation[$mapping->grouping
][$mapping->column_number
] = $mapping->location_type_id
;
166 if (!empty($mapping->phone_type_id
)) {
167 $mappingPhoneType[$mapping->grouping
][$mapping->column_number
] = $mapping->phone_type_id
;
170 // get IM service provider type id from mapping fields
171 if (!empty($mapping->im_provider_id
)) {
172 $mappingImProvider[$mapping->grouping
][$mapping->column_number
] = $mapping->im_provider_id
;
175 if (!empty($mapping->website_type_id
)) {
176 $mappingWebsiteType[$mapping->grouping
][$mapping->column_number
] = $mapping->website_type_id
;
179 if (!empty($mapping->relationship_type_id
)) {
180 $mappingRelation[$mapping->grouping
][$mapping->column_number
] = "{$mapping->relationship_type_id}_{$mapping->relationship_direction}";
183 if (!empty($mapping->operator
)) {
184 $mappingOperator[$mapping->grouping
][$mapping->column_number
] = $mapping->operator
;
187 if (!empty($mapping->value
)) {
188 $mappingValue[$mapping->grouping
][$mapping->column_number
] = $mapping->value
;
193 $mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType,
194 $mappingImProvider, $mappingRelation, $mappingOperator, $mappingValue, $mappingWebsiteType,
199 *function to check Duplicate Mapping Name
201 * @params $nameField string mapping Name
203 * @params $mapTypeId string mapping Type
208 static function checkMapping($nameField, $mapTypeId) {
209 $mapping = new CRM_Core_DAO_Mapping();
210 $mapping->name
= $nameField;
211 $mapping->mapping_type_id
= $mapTypeId;
212 if ($mapping->find(TRUE)) {
221 * Function returns associated array of elements, that will be passed for search
223 * @params int $smartGroupId smart group id
225 * @return $returnFields associated array of elements
230 static function getFormattedFields($smartGroupId) {
231 $returnFields = array();
233 //get the fields from mapping table
234 $dao = new CRM_Core_DAO_MappingField();
235 $dao->mapping_id
= $smartGroupId;
237 while ($dao->fetch()) {
238 $fldName = $dao->name
;
239 if ($dao->location_type_id
) {
240 $fldName .= "-{$dao->location_type_id}";
242 if ($dao->phone_type
) {
243 $fldName .= "-{$dao->phone_type}";
245 $returnFields[$fldName]['value'] = $dao->value
;
246 $returnFields[$fldName]['op'] = $dao->operator
;
247 $returnFields[$fldName]['grouping'] = $dao->grouping
;
249 return $returnFields;
253 * Function to build the mapping form
255 * @params object $form form object
256 * @params string $mappingType mapping type (Export/Import/Search Builder)
257 * @params int $mappingId mapping id
258 * @params mixed $columnCount column count is int for and array for search builder
259 * @params int $blockCount block count (no of blocks shown)
265 static function buildMappingForm(&$form, $mappingType = 'Export', $mappingId = NULL, $columnNo, $blockCount = 3, $exportMode = NULL) {
266 if ($mappingType == 'Export') {
268 $columnCount = array('1' => $columnNo);
270 elseif ($mappingType == 'Search Builder') {
272 $columnCount = $columnNo;
275 //get the saved mapping details
277 if ($mappingType == 'Export') {
278 $form->applyFilter('saveMappingName', 'trim');
280 //to save the current mappings
281 if (!isset($mappingId)) {
282 $saveDetailsName = ts('Save this field mapping');
283 $form->add('text', 'saveMappingName', ts('Name'));
284 $form->add('text', 'saveMappingDesc', ts('Description'));
287 $form->assign('loadedMapping', $mappingId);
289 $params = array('id' => $mappingId);
291 $mappingDetails = CRM_Core_BAO_Mapping
::retrieve($params, $temp);
293 $form->assign('savedName', $mappingDetails->name
);
295 $form->add('hidden', 'mappingId', $mappingId);
297 $form->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL);
298 $saveDetailsName = ts('Save as a new field mapping');
299 $form->add('text', 'saveMappingName', ts('Name'));
300 $form->add('text', 'saveMappingDesc', ts('Description'));
303 $form->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, array('onclick' => "showSaveDetails(this)"));
304 $form->addFormRule(array('CRM_Export_Form_Map', 'formRule'), $form->get('mappingTypeId'));
306 elseif ($mappingType == 'Search Builder') {
307 $form->addElement('submit', 'addBlock', ts('Also include contacts where'),
308 array('class' => 'submit-link')
313 $hasLocationTypes = array();
314 $hasRelationTypes = array();
317 if ($mappingType == 'Export') {
320 elseif ($mappingType == 'Search Builder') {
324 $contactType = array('Individual', 'Household', 'Organization');
325 foreach ($contactType as $value) {
326 $contactFields = CRM_Contact_BAO_Contact
::exportableFields($value, FALSE, $required);
327 // exclude the address options disabled in the Address Settings
328 $fields[$value] = CRM_Core_BAO_Address
::validateAddressOptions($contactFields);
330 if ($mappingType == 'Export') {
331 $relationships = array();
332 $relationshipTypes = CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, NULL, NULL, $value);
333 asort($relationshipTypes);
335 foreach ($relationshipTypes as $key => $var) {
336 list($type) = explode('_', $key);
338 $relationships[$key]['title'] = $var;
339 $relationships[$key]['headerPattern'] = '/' . preg_quote($var, '/') . '/';
340 $relationships[$key]['export'] = TRUE;
341 $relationships[$key]['relationship_type_id'] = $type;
342 $relationships[$key]['related'] = TRUE;
343 $relationships[$key]['hasRelationType'] = 1;
346 if (!empty($relationships)) {
347 $fields[$value] = array_merge($fields[$value],
348 array('related' => array('title' => ts('- related contact info -'))),
355 //get the current employer for mapping.
357 $fields['Individual']['current_employer']['title'] = ts('Current Employer');
360 // add component fields
361 $compArray = array();
363 //we need to unset groups, tags, notes for component export
364 if ($exportMode != CRM_Export_Form_Select
::CONTACT_EXPORT
) {
366 'groups', 'tags', 'notes') as $value) {
367 unset($fields['Individual'][$value]);
368 unset($fields['Household'][$value]);
369 unset($fields['Organization'][$value]);
373 if ($mappingType == 'Search Builder') {
374 //build the common contact fields array.
375 $fields['Contact'] = array();
376 foreach ($fields['Individual'] as $key => $value) {
377 if (CRM_Utils_Array
::value($key, $fields['Household']) &&
378 CRM_Utils_Array
::value($key, $fields['Organization'])
380 $fields['Contact'][$key] = $value;
381 unset($fields['Organization'][$key],
382 $fields['Household'][$key],
383 $fields['Individual'][$key]);
386 if (array_key_exists('note', $fields['Contact'])) {
387 $noteTitle = $fields['Contact']['note']['title'];
388 $fields['Contact']['note']['title'] = $noteTitle . ': ' . ts('Body and Subject');
389 $fields['Contact']['note_body'] = array( 'title' => $noteTitle . ': ' . ts('Body only'), 'name' => 'note_body' );
390 $fields['Contact']['note_subject'] = array( 'title' => $noteTitle . ': ' . ts('Subject only'), 'name' => 'note_subject' );
394 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::CONTRIBUTE_EXPORT
)) {
395 if (CRM_Core_Permission
::access('CiviContribute')) {
396 $fields['Contribution'] = CRM_Contribute_BAO_Contribution
::exportableFields();
397 unset($fields['Contribution']['contribution_contact_id']);
398 unset($fields['Contribution']['contribution_status_id']);
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 require_once 'CRM/Export/BAO/Export.php';
409 $componentPaymentFields = array();
410 foreach (CRM_Export_BAO_Export
::componentPaymentFields() as $payField => $payTitle) {
411 $componentPaymentFields[$payField] = array('title' => $payTitle);
413 $fields['Participant'] = array_merge($fields['Participant'], $componentPaymentFields);
416 unset($fields['Participant']['participant_contact_id']);
417 $compArray['Participant'] = ts('Participant');
421 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::MEMBER_EXPORT
)) {
422 if (CRM_Core_Permission
::access('CiviMember')) {
423 $fields['Membership'] = CRM_Member_BAO_Membership
::getMembershipFields($exportMode);
424 unset($fields['Membership']['membership_contact_id']);
425 $compArray['Membership'] = ts('Membership');
429 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::PLEDGE_EXPORT
)) {
430 if (CRM_Core_Permission
::access('CiviPledge')) {
431 $fields['Pledge'] = CRM_Pledge_BAO_Pledge
::exportableFields();
432 unset($fields['Pledge']['pledge_contact_id']);
433 $compArray['Pledge'] = ts('Pledge');
437 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::CASE_EXPORT
)) {
438 if (CRM_Core_Permission
::access('CiviCase')) {
439 $fields['Case'] = CRM_Case_BAO_Case
::exportableFields();
440 $compArray['Case'] = ts('Case');
442 $fields['Activity'] = CRM_Activity_BAO_Activity
::exportableFields('Case');
443 $compArray['Activity'] = ts('Case Activity');
445 unset($fields['Case']['case_contact_id']);
448 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::GRANT_EXPORT
)) {
449 if (CRM_Core_Permission
::access('CiviGrant')) {
450 $fields['Grant'] = CRM_Grant_BAO_Grant
::exportableFields();
451 unset($fields['Grant']['grant_contact_id']);
452 $compArray['Grant'] = ts('Grant');
456 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::ACTIVITY_EXPORT
)) {
457 $fields['Activity'] = CRM_Activity_BAO_Activity
::exportableFields('Activity');
458 $compArray['Activity'] = ts('Activity');
461 //Contact Sub Type For export
462 $contactSubTypes = array();
463 $subTypes = CRM_Contact_BAO_ContactType
::subTypeInfo();
465 foreach ($subTypes as $subType => $val) {
466 //adding subtype specific relationships CRM-5256
467 $csRelationships = array();
469 if ($mappingType == 'Export') {
470 $subTypeRelationshipTypes =
471 CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, NULL, NULL, $val['parent'],
472 FALSE, 'label', TRUE, $subType);
474 foreach ($subTypeRelationshipTypes as $key => $var) {
475 if (!array_key_exists($key, $fields[$val['parent']])) {
476 list($type) = explode('_', $key);
478 $csRelationships[$key]['title'] = $var;
479 $csRelationships[$key]['headerPattern'] = '/' . preg_quote($var, '/') . '/';
480 $csRelationships[$key]['export'] = TRUE;
481 $csRelationships[$key]['relationship_type_id'] = $type;
482 $csRelationships[$key]['related'] = TRUE;
483 $csRelationships[$key]['hasRelationType'] = 1;
488 $fields[$subType] = $fields[$val['parent']] +
$csRelationships;
490 //custom fields for sub type
491 $subTypeFields = CRM_Core_BAO_CustomField
::getFieldsForImport($subType);
492 $fields[$subType] +
= $subTypeFields;
494 if (!empty($subTypeFields) ||
!empty($csRelationships)) {
495 $contactSubTypes[$subType] = $val['label'];
501 foreach ($fields as $key => $value) {
503 foreach ($value as $key1 => $value1) {
504 //CRM-2676, replacing the conflict for same custom field name from different custom group.
505 $customGroupName = self
::getCustomGroupName($key1);
507 if ($customGroupName) {
508 $relatedMapperFields[$key][$key1] = $mapperFields[$key][$key1] = $customGroupName . ': ' . $value1['title'];
511 $relatedMapperFields[$key][$key1] = $mapperFields[$key][$key1] = $value1['title'];
513 if (isset($value1['hasLocationType'])) {
514 $hasLocationTypes[$key][$key1] = $value1['hasLocationType'];
517 if (isset($value1['hasRelationType'])) {
518 $hasRelationTypes[$key][$key1] = $value1['hasRelationType'];
519 unset($relatedMapperFields[$key][$key1]);
523 if (array_key_exists('related', $relatedMapperFields[$key])) {
524 unset($relatedMapperFields[$key]['related']);
528 $mapperKeys = array_keys($mapperFields);
530 $locationTypes = CRM_Core_PseudoConstant
::locationType();
532 $defaultLocationType = CRM_Core_BAO_LocationType
::getDefault();
534 /* FIXME: dirty hack to make the default option show up first. This
535 * avoids a mozilla browser bug with defaults on dynamically constructed
536 * selector widgets. */
539 if ($defaultLocationType) {
540 $defaultLocation = $locationTypes[$defaultLocationType->id
];
541 unset($locationTypes[$defaultLocationType->id
]);
542 $locationTypes = array(
543 $defaultLocationType->id
=> $defaultLocation) +
$locationTypes;
546 $locationTypes = array(
547 ' ' => ts('Primary')) +
$locationTypes;
549 // since we need a hierarchical list to display contact types & subtypes,
550 // this is what we going to display in first selector
551 $contactTypes = CRM_Contact_BAO_ContactType
::getSelectElements(FALSE, FALSE);
552 if ($mappingType == 'Search Builder') {
553 $contactTypes = array('Contact' => ts('Contacts')) +
$contactTypes;
557 array('' => ts('- select record type -')) +
$contactTypes +
$compArray;
559 foreach ($sel1 as $key => $sel) {
561 asort($mapperFields[$key]);
562 $sel2[$key] = array('' => ts('- select field -')) +
$mapperFields[$key];
568 $phoneTypes = CRM_Core_PseudoConstant
::phoneType();
569 $imProviders = CRM_Core_PseudoConstant
::IMProvider();
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
598 $relationshipTypes = CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, NULL, NULL, NULL, TRUE);
600 if ($mappingType == 'Export') {
601 foreach ($sel1 as $k => $sel) {
603 foreach ($mapperFields[$k] as $field => $dontCare) {
604 if (isset($hasRelationTypes[$k][$field])) {
605 list($id, $first, $second) = explode('_', $field);
606 // FIX ME: For now let's not expose custom data related to relationship
607 $relationshipCustomFields = array();
608 //$relationshipCustomFields = self::getRelationTypeCustomGroupData( $id );
609 //asort( $relationshipCustomFields ) ;
611 $relationshipType = new CRM_Contact_BAO_RelationshipType();
612 $relationshipType->id
= $id;
613 if ($relationshipType->find(TRUE)) {
614 $direction = "contact_sub_type_$second";
615 if (isset($relationshipType->$direction)) {
616 $relatedFields = array_merge((array)$relatedMapperFields[$relationshipType->$direction], (array)$relationshipCustomFields);
619 $target_type = 'contact_type_' . $second;
620 $relatedFields = array_merge((array)$relatedMapperFields[$relationshipType->$target_type], (array)$relationshipCustomFields);
623 $relationshipType->free();
624 asort($relatedFields);
625 $sel5[$k][$field] = $relatedFields;
631 //Location Type for relationship fields
633 foreach ($sel5 as $k => $v) {
635 foreach ($v as $rel => $fields) {
636 foreach ($fields as $field => $fieldLabel) {
637 if (isset($hasLocationTypes[$k][$field])) {
638 $sel6[$k][$rel][$field] = $locationTypes;
645 //PhoneTypes for relationship fields
647 foreach ($sel6 as $k => $rel) {
649 foreach ($rel as $phonekey => $phonevalue) {
650 foreach ($locationTypes as $locType => $loc) {
651 if (trim($locType) != '') {
652 $sel7[$k][$phonekey]['phone'][$locType] = &$phoneTypes;
653 $sel7[$k][$phonekey]['im'][$locType] = &$imProviders;
661 //special fields that have location, hack for primary location
662 $specialFields = array(
663 'street_address', 'supplemental_address_1', 'supplemental_address_2',
664 'city', 'postal_code', 'postal_code_suffix', 'geo_code_1', 'geo_code_2',
665 'state_province', 'country', 'phone', 'email', 'im',
668 if (isset($mappingId)) {
671 list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingImProvider,
672 $mappingRelation, $mappingOperator, $mappingValue
673 ) = CRM_Core_BAO_Mapping
::getMappingFields($mappingId);
675 $blkCnt = count($mappingName);
676 if ($blkCnt >= $blockCount) {
677 $blockCount = $blkCnt +
1;
679 for ($x = 1; $x < $blockCount; $x++
) {
680 if (isset($mappingName[$x])) {
681 $colCnt = count($mappingName[$x]);
682 if ($colCnt >= $columnCount[$x]) {
683 $columnCount[$x] = $colCnt;
689 $form->_blockCount
= $blockCount;
690 $form->_columnCount
= $columnCount;
692 $form->set('blockCount', $form->_blockCount
);
693 $form->set('columnCount', $form->_columnCount
);
695 $defaults = $noneArray = $nullArray = array();
697 //used to warn for mismatch column count or mismatch mapping
699 for ($x = 1; $x < $blockCount; $x++
) {
701 for ($i = 0; $i < $columnCount[$x]; $i++
) {
703 $sel = &$form->addElement('hierselect', "mapper[$x][$i]", ts('Mapper for Field %1', array(1 => $i)), NULL);
706 if (isset($mappingId)) {
707 $locationId = isset($mappingLocation[$x][$i]) ?
$mappingLocation[$x][$i] : 0;
708 if (isset($mappingName[$x][$i])) {
709 if (is_array($mapperFields[$mappingContactType[$x][$i]])) {
711 if (isset($mappingRelation[$x][$i])) {
713 $contactDetails = strtolower(str_replace(" ", "_", $mappingName[$x][$i]));
714 $relLocationId = isset($mappingLocation[$x][$i]) ?
$mappingLocation[$x][$i] : 0;
715 if (!$relLocationId && in_array($mappingName[$x][$i], $specialFields)) {
716 $relLocationId = " ";
719 $relPhoneType = isset($mappingPhoneType[$x][$i]) ?
$mappingPhoneType[$x][$i] : NULL;
721 $defaults["mapper[$x][$i]"] = array(
722 $mappingContactType[$x][$i],
723 $mappingRelation[$x][$i],
726 $mappingName[$x][$i],
732 $noneArray[] = array($x, $i, 2);
734 if (!$phoneType && !$imProvider) {
735 $noneArray[] = array($x, $i, 3);
737 if (!$mappingName[$x][$i]) {
738 $noneArray[] = array($x, $i, 4);
740 if (!$relLocationId) {
741 $noneArray[] = array($x, $i, 5);
743 if (!$relPhoneType) {
744 $noneArray[] = array($x, $i, 6);
746 $noneArray[] = array($x, $i, 2);
749 $phoneType = isset($mappingPhoneType[$x][$i]) ?
$mappingPhoneType[$x][$i] : NULL;
750 $imProvider = isset($mappingImProvider[$x][$i]) ?
$mappingImProvider[$x][$i] : NULL;
751 if (!$locationId && in_array($mappingName[$x][$i], $specialFields)) {
755 $defaults["mapper[$x][$i]"] = array(
756 $mappingContactType[$x][$i],
757 $mappingName[$x][$i],
761 if (!$mappingName[$x][$i]) {
762 $noneArray[] = array($x, $i, 1);
765 $noneArray[] = array($x, $i, 2);
767 if (!$phoneType && !$imProvider) {
768 $noneArray[] = array($x, $i, 3);
771 $noneArray[] = array($x, $i, 4);
772 $noneArray[] = array($x, $i, 5);
773 $noneArray[] = array($x, $i, 6);
778 if (CRM_Utils_Array
::value($i, CRM_Utils_Array
::value($x, $mappingOperator))) {
779 $defaults["operator[$x][$i]"] = CRM_Utils_Array
::value($i, $mappingOperator[$x]);
782 if (CRM_Utils_Array
::value($i, CRM_Utils_Array
::value($x, $mappingValue))) {
783 $defaults["value[$x][$i]"] = CRM_Utils_Array
::value($i, $mappingValue[$x]);
788 //Fix for Search Builder
789 if ($mappingType == 'Export') {
796 $formValues = $form->exportValues();
798 if (empty($formValues)) {
799 // Incremented length for third select box(relationship type)
800 for ($k = 1; $k < $j; $k++
) {
801 $noneArray[] = array($x, $i, $k);
805 if (!empty($formValues['mapper'][$x])) {
806 foreach ($formValues['mapper'][$x] as $value) {
807 for ($k = 1; $k < $j; $k++
) {
808 if (!isset($formValues['mapper'][$x][$i][$k]) ||
809 (!$formValues['mapper'][$x][$i][$k])
811 $noneArray[] = array($x, $i, $k);
814 $nullArray[] = array($x, $i, $k);
820 for ($k = 1; $k < $j; $k++
) {
821 $noneArray[] = array($x, $i, $k);
826 //Fix for Search Builder
827 if ($mappingType == 'Export') {
828 if (!isset($mappingId)) {
829 if (isset($formValues['mapper']) &&
830 isset($formValues['mapper'][$x][$i][1]) &&
831 array_key_exists($formValues['mapper'][$x][$i][1], $relationshipTypes)
833 $sel->setOptions(array($sel1, $sel2, $sel5, $sel6, $sel7, $sel3, $sel4));
836 $sel->setOptions(array($sel1, $sel2, $sel3, $sel4, $sel5, $sel6, $sel7));
840 $sel->setOptions(array($sel1, $sel2, $sel3, $sel4, $sel5, $sel6, $sel7));
844 $sel->setOptions(array($sel1, $sel2, $sel3, $sel4));
847 if ($mappingType == 'Search Builder') {
848 //CRM -2292, restricted array set
849 $operatorArray = array(
850 '' => ts('-operator-'),
860 'IS EMPTY' => 'IS EMPTY',
861 'IS NOT EMPTY' => 'IS NOT EMPTY',
862 'IS NULL' => 'IS NULL',
863 'IS NOT NULL' => 'IS NOT NULL',
866 $form->add('select', "operator[$x][$i]", '', $operatorArray);
867 $form->add('text', "value[$x][$i]", '');
870 //end of columnCnt for
871 if ($mappingType == 'Search Builder') {
872 $title = ts('Another search field');
875 $title = ts('Select more fields');
878 $form->addElement('submit', "addMore[$x]", $title, array('class' => 'submit-link'));
882 $js = "<script type='text/javascript'>\n";
883 $formName = "document.{$name}";
884 if (!empty($nullArray)) {
885 $js .= "var nullArray = [";
888 foreach ($nullArray as $element) {
889 $key = "{$element[0]}, {$element[1]}, {$element[2]}";
890 if (!isset($seen[$key])) {
891 $elements[] = "[$key]";
895 $js .= implode(', ', $elements);
898 for(var i=0;i<nullArray.length;i++) {
899 if ( {$formName}['mapper['+nullArray[i][0]+']['+nullArray[i][1]+']['+nullArray[i][2]+']'] ) {
900 {$formName}['mapper['+nullArray[i][0]+']['+nullArray[i][1]+']['+nullArray[i][2]+']'].style.display = '';
905 if (!empty($noneArray)) {
906 $js .= "var noneArray = [";
909 foreach ($noneArray as $element) {
910 $key = "{$element[0]}, {$element[1]}, {$element[2]}";
911 if (!isset($seen[$key])) {
912 $elements[] = "[$key]";
916 $js .= implode(', ', $elements);
919 for(var i=0;i<noneArray.length;i++) {
920 if ( {$formName}['mapper['+noneArray[i][0]+']['+noneArray[i][1]+']['+noneArray[i][2]+']'] ) {
921 {$formName}['mapper['+noneArray[i][0]+']['+noneArray[i][1]+']['+noneArray[i][2]+']'].style.display = 'none';
926 $js .= "</script>\n";
928 $form->assign('initHideBoxes', $js);
929 $form->assign('columnCount', $columnCount);
930 $form->assign('blockCount', $blockCount);
931 $form->setDefaults($defaults);
933 $form->setDefaultAction('refresh');
936 /* Function returns all custom fields with group title and
938 * @relationshipTypeId related relationship type id
939 * @return $groupTitle all custom field titles
941 function getRelationTypeCustomGroupData($relationshipTypeId) {
943 $customFields = CRM_Core_BAO_CustomField
::getFields('Relationship', NULL, NULL, $relationshipTypeId, NULL, NULL);
944 $groupTitle = array();
945 foreach ($customFields as $krelation => $vrelation) {
946 $groupTitle[$vrelation['label']] = $vrelation['groupTitle'] . '...' . $vrelation['label'];
953 * Function returns all Custom group Names
955 * @param customfieldId related custom field id
956 * @return $customGroupName all custom group names
959 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 * @params array $params associated array of submitted values
976 * @params boolean $row row no of the fields
978 * @return $returnFields formatted associated array of elements
983 static function &formattedFields(&$params, $row = FALSE) {
986 if (empty($params) ||
!isset($params['mapper'])) {
990 $types = array('Individual', 'Organization', 'Household');
991 foreach ($params['mapper'] as $key => $value) {
993 foreach ($value as $k => $v) {
994 if (in_array($v[0], $types)) {
995 if ($contactType && $contactType != $v[0]) {
996 CRM_Core_Error
::fatal(ts("Cannot have two clauses with different types: %1, %2",
997 array(1 => $contactType, 2 => $v[0])
1000 $contactType = $v[0];
1002 if (CRM_Utils_Array
::value('1', $v)) {
1004 $v2 = CRM_Utils_Array
::value('2', $v);
1005 if ($v2 && trim($v2)) {
1006 $fldName .= "-{$v[2]}";
1009 $v3 = CRM_Utils_Array
::value('3', $v);
1010 if ($v3 && trim($v3)) {
1011 $fldName .= "-{$v[3]}";
1014 $value = $params['value'][$key][$k];
1015 if ($fldName == 'group' ||
$fldName == 'tag') {
1016 $value = trim($value);
1017 $value = str_replace('(', '', $value);
1018 $value = str_replace(')', '', $value);
1020 $v = explode(',', $value);
1022 foreach ($v as $i) {
1027 if ($v[0] == 'Contribution' && substr($fldName, 0, 7) != 'custom_') {
1028 if (substr($fldName, 0, 13) != 'contribution_') {
1029 $fldName = 'contribution_' . $fldName;
1036 $params['operator'][$key][$k],
1045 $params['operator'][$key][$k],
1064 //add sortByCharacter values
1065 if (isset($params['sortByCharacter'])) {
1069 $params['sortByCharacter'],
1079 static function &returnProperties(&$params) {
1081 'contact_type' => 1,
1082 'contact_sub_type' => 1,
1086 if (empty($params) ||
empty($params['mapper'])) {
1090 $locationTypes = CRM_Core_PseudoConstant
::locationType();
1091 foreach ($params['mapper'] as $key => $value) {
1092 foreach ($value as $k => $v) {
1094 if ($v[1] == 'groups' ||
$v[1] == 'tags') {
1098 if (isset($v[2]) && is_numeric($v[2])) {
1099 if (!array_key_exists('location', $fields)) {
1100 $fields['location'] = array();
1103 // make sure that we have a location fields and a location type for this
1104 $locationName = $locationTypes[$v[2]];
1105 if (!array_key_exists($locationName, $fields['location'])) {
1106 $fields['location'][$locationName] = array();
1107 $fields['location'][$locationName]['location_type'] = $v[2];
1110 if ($v[1] == 'phone' ||
$v[1] == 'email' ||
$v[1] == 'im') {
1111 // phone type handling
1113 $fields['location'][$locationName][$v[1] . "-" . $v[3]] = 1;
1116 $fields['location'][$locationName][$v[1]] = 1;
1120 $fields['location'][$locationName][$v[1]] = 1;
1134 * save the mapping field info for search builder / export given the formvalues
1136 * @param array $params asscociated array of formvalues
1137 * @param int $mappingId mapping id
1143 static function saveMappingFields(&$params, $mappingId) {
1144 //delete mapping fields records for exixting mapping
1145 $mappingFields = new CRM_Core_DAO_MappingField();
1146 $mappingFields->mapping_id
= $mappingId;
1147 $mappingFields->delete();
1149 if (empty($params['mapper'])) {
1153 //save record in mapping field table
1154 foreach ($params['mapper'] as $key => $value) {
1156 foreach ($value as $k => $v) {
1158 if (CRM_Utils_Array
::value('1', $v)) {
1159 $saveMappingFields = new CRM_Core_DAO_MappingField();
1161 $saveMappingFields->mapping_id
= $mappingId;
1162 $saveMappingFields->name
= CRM_Utils_Array
::value('1', $v);
1163 $saveMappingFields->contact_type
= CRM_Utils_Array
::value('0', $v);
1164 $locationId = CRM_Utils_Array
::value('2', $v);
1165 $saveMappingFields->location_type_id
= is_numeric($locationId) ?
$locationId : NULL;
1167 if ($v[1] == 'phone') {
1168 $saveMappingFields->phone_type_id
= CRM_Utils_Array
::value('3', $v);
1170 elseif ($v[1] == 'im') {
1171 $saveMappingFields->im_provider_id
= CRM_Utils_Array
::value('3', $v);
1174 if (CRM_Utils_Array
::value('operator', $params)) {
1175 $saveMappingFields->operator
= CRM_Utils_Array
::value($k, $params['operator'][$key]);
1177 if (CRM_Utils_Array
::value('value', $params)) {
1178 $saveMappingFields->value
= CRM_Utils_Array
::value($k, $params['value'][$key]);
1180 // Handle mapping for 'related contact' fields
1181 if (count(explode('_', CRM_Utils_Array
::value('1', $v))) > 2) {
1182 list($id, $first, $second) = explode('_', CRM_Utils_Array
::value('1', $v));
1183 if (($first == 'a' && $second == 'b') ||
($first == 'b' && $second == 'a')) {
1185 if (CRM_Utils_Array
::value('2', $v)) {
1186 $saveMappingFields->name
= CRM_Utils_Array
::value('2', $v);
1188 elseif (CRM_Utils_Array
::value('4', $v)) {
1189 $saveMappingFields->name
= CRM_Utils_Array
::value('4', $v);
1192 if (is_numeric(CRM_Utils_Array
::value('3', $v))) {
1193 $locationTypeid = CRM_Utils_Array
::value('3', $v);
1195 elseif (is_numeric(CRM_Utils_Array
::value('5', $v))) {
1196 $locationTypeid = CRM_Utils_Array
::value('5', $v);
1199 if (is_numeric(CRM_Utils_Array
::value('4', $v))) {
1200 $phoneTypeid = CRM_Utils_Array
::value('4', $v);
1202 elseif (is_numeric(CRM_Utils_Array
::value('6', $v))) {
1203 $phoneTypeid = CRM_Utils_Array
::value('6', $v);
1206 $saveMappingFields->location_type_id
= is_numeric($locationTypeid) ?
$locationTypeid : NULL;
1207 $saveMappingFields->phone_type_id
= is_numeric($phoneTypeid) ?
$phoneTypeid : NULL;
1208 $saveMappingFields->relationship_type_id
= $id;
1209 $saveMappingFields->relationship_direction
= "{$first}_{$second}";
1213 $saveMappingFields->grouping
= $key;
1214 $saveMappingFields->column_number
= $colCnt;
1215 $saveMappingFields->save();
1217 $locationTypeid = $phoneTypeid = NULL;