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.
52 * @return object 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);
75 public static function del($id) {
76 // delete from mapping_field table
77 $mappingField = new CRM_Core_DAO_MappingField();
78 $mappingField->mapping_id
= $id;
79 $mappingField->find();
80 while ($mappingField->fetch()) {
81 $mappingField->delete();
84 // delete from mapping table
85 $mapping = new CRM_Core_DAO_Mapping();
88 CRM_Core_Session
::setStatus(ts('Selected mapping has been deleted successfully.'), ts('Deleted'), 'success');
94 * Takes an associative array and creates a contact object
96 * The function extract all the params it needs to initialize the create a
97 * contact object. the params array could contain additional unused name/value
100 * @param array $params
101 * An array of name/value pairs.
103 * @return object CRM_Core_DAO_Mapper object on success, otherwise null
106 public static function add($params) {
107 $mapping = new CRM_Core_DAO_Mapping();
108 $mapping->copyValues($params);
115 * Get the list of mappings
117 * @param string $mappingTypeId
120 * @return array $mapping array of mapping name
123 public static function getMappings($mappingTypeId) {
125 $mappingDAO = new CRM_Core_DAO_Mapping();
126 $mappingDAO->mapping_type_id
= $mappingTypeId;
129 while ($mappingDAO->fetch()) {
130 $mapping[$mappingDAO->id
] = $mappingDAO->name
;
137 * Get the mapping fields
139 * @param int $mappingId
142 * @return array $mappingFields array of mapping fields
145 public static function getMappingFields($mappingId) {
146 //mapping is to be loaded from database
147 $mapping = new CRM_Core_DAO_MappingField();
148 $mapping->mapping_id
= $mappingId;
149 $mapping->orderBy('column_number');
152 $mappingName = $mappingLocation = $mappingContactType = $mappingPhoneType = array();
153 $mappingImProvider = $mappingRelation = $mappingOperator = $mappingValue = $mappingWebsiteType = array();
154 while ($mapping->fetch()) {
155 $mappingName[$mapping->grouping
][$mapping->column_number
] = $mapping->name
;
156 $mappingContactType[$mapping->grouping
][$mapping->column_number
] = $mapping->contact_type
;
158 if (!empty($mapping->location_type_id
)) {
159 $mappingLocation[$mapping->grouping
][$mapping->column_number
] = $mapping->location_type_id
;
162 if (!empty($mapping->phone_type_id
)) {
163 $mappingPhoneType[$mapping->grouping
][$mapping->column_number
] = $mapping->phone_type_id
;
166 // get IM service provider type id from mapping fields
167 if (!empty($mapping->im_provider_id
)) {
168 $mappingImProvider[$mapping->grouping
][$mapping->column_number
] = $mapping->im_provider_id
;
171 if (!empty($mapping->website_type_id
)) {
172 $mappingWebsiteType[$mapping->grouping
][$mapping->column_number
] = $mapping->website_type_id
;
175 if (!empty($mapping->relationship_type_id
)) {
176 $mappingRelation[$mapping->grouping
][$mapping->column_number
] = "{$mapping->relationship_type_id}_{$mapping->relationship_direction}";
179 if (!empty($mapping->operator
)) {
180 $mappingOperator[$mapping->grouping
][$mapping->column_number
] = $mapping->operator
;
183 if (!empty($mapping->value
)) {
184 $mappingValue[$mapping->grouping
][$mapping->column_number
] = $mapping->value
;
189 $mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType,
190 $mappingImProvider, $mappingRelation, $mappingOperator, $mappingValue, $mappingWebsiteType,
195 * Check Duplicate Mapping Name
198 * String mapping Name.
200 * String mapping Type.
204 public static function checkMapping($nameField, $mapTypeId) {
205 $mapping = new CRM_Core_DAO_Mapping();
206 $mapping->name
= $nameField;
207 $mapping->mapping_type_id
= $mapTypeId;
208 return (bool) $mapping->find(TRUE);
212 * Function returns associated array of elements, that will be passed for search
214 * @param int $smartGroupId
217 * @return array $returnFields associated array of elements@static
219 public static function getFormattedFields($smartGroupId) {
220 $returnFields = array();
222 //get the fields from mapping table
223 $dao = new CRM_Core_DAO_MappingField();
224 $dao->mapping_id
= $smartGroupId;
226 while ($dao->fetch()) {
227 $fldName = $dao->name
;
228 if ($dao->location_type_id
) {
229 $fldName .= "-{$dao->location_type_id}";
231 if ($dao->phone_type
) {
232 $fldName .= "-{$dao->phone_type}";
234 $returnFields[$fldName]['value'] = $dao->value
;
235 $returnFields[$fldName]['op'] = $dao->operator
;
236 $returnFields[$fldName]['grouping'] = $dao->grouping
;
238 return $returnFields;
242 * Build the mapping form
244 * @param CRM_Core_Form $form
245 * @param string $mappingType
246 * (Export/Import/Search Builder).
247 * @param int $mappingId
248 * @param int $columnNo
249 * @param int $blockCount
250 * (no of blocks shown).
251 * @param null $exportMode
256 public static function buildMappingForm(&$form, $mappingType = 'Export', $mappingId = NULL, $columnNo, $blockCount = 3, $exportMode = NULL) {
257 if ($mappingType == 'Export') {
259 $columnCount = array('1' => $columnNo);
261 elseif ($mappingType == 'Search Builder') {
263 $columnCount = $columnNo;
266 //get the saved mapping details
268 if ($mappingType == 'Export') {
269 $form->applyFilter('saveMappingName', 'trim');
271 //to save the current mappings
272 if (!isset($mappingId)) {
273 $saveDetailsName = ts('Save this field mapping');
274 $form->add('text', 'saveMappingName', ts('Name'));
275 $form->add('text', 'saveMappingDesc', ts('Description'));
278 $form->assign('loadedMapping', $mappingId);
280 $params = array('id' => $mappingId);
282 $mappingDetails = CRM_Core_BAO_Mapping
::retrieve($params, $temp);
284 $form->assign('savedName', $mappingDetails->name
);
286 $form->add('hidden', 'mappingId', $mappingId);
288 $form->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL);
289 $saveDetailsName = ts('Save as a new field mapping');
290 $form->add('text', 'saveMappingName', ts('Name'));
291 $form->add('text', 'saveMappingDesc', ts('Description'));
294 $form->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, array('onclick' => "showSaveDetails(this)"));
295 $form->addFormRule(array('CRM_Export_Form_Map', 'formRule'), $form->get('mappingTypeId'));
297 elseif ($mappingType == 'Search Builder') {
298 $form->addElement('submit', 'addBlock', ts('Also include contacts where'),
299 array('class' => 'submit-link')
304 $hasLocationTypes = array();
305 $hasRelationTypes = array();
308 if ($mappingType == 'Export') {
311 elseif ($mappingType == 'Search Builder') {
315 $contactType = array('Individual', 'Household', 'Organization');
316 foreach ($contactType as $value) {
317 $contactFields = CRM_Contact_BAO_Contact
::exportableFields($value, FALSE, $required);
318 $contactFields = array_merge($contactFields, CRM_Contact_BAO_Query_Hook
::singleton()->getFields());
320 // exclude the address options disabled in the Address Settings
321 $fields[$value] = CRM_Core_BAO_Address
::validateAddressOptions($contactFields);
322 ksort($fields[$value]);
323 if ($mappingType == 'Export') {
324 $relationships = array();
325 $relationshipTypes = CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, NULL, NULL, $value);
326 asort($relationshipTypes);
328 foreach ($relationshipTypes as $key => $var) {
329 list($type) = explode('_', $key);
331 $relationships[$key]['title'] = $var;
332 $relationships[$key]['headerPattern'] = '/' . preg_quote($var, '/') . '/';
333 $relationships[$key]['export'] = TRUE;
334 $relationships[$key]['relationship_type_id'] = $type;
335 $relationships[$key]['related'] = TRUE;
336 $relationships[$key]['hasRelationType'] = 1;
339 if (!empty($relationships)) {
340 $fields[$value] = array_merge($fields[$value],
341 array('related' => array('title' => ts('- related contact info -'))),
348 //get the current employer for mapping.
350 $fields['Individual']['current_employer']['title'] = ts('Current Employer');
353 // add component fields
354 $compArray = array();
356 //we need to unset groups, tags, notes for component export
357 if ($exportMode != CRM_Export_Form_Select
::CONTACT_EXPORT
) {
359 'groups', 'tags', 'notes') as $value) {
360 unset($fields['Individual'][$value]);
361 unset($fields['Household'][$value]);
362 unset($fields['Organization'][$value]);
366 if ($mappingType == 'Search Builder') {
367 //build the common contact fields array.
368 $fields['Contact'] = array();
369 foreach ($fields['Individual'] as $key => $value) {
370 if (!empty($fields['Household'][$key]) && !empty($fields['Organization'][$key])) {
371 $fields['Contact'][$key] = $value;
372 unset($fields['Organization'][$key],
373 $fields['Household'][$key],
374 $fields['Individual'][$key]);
377 if (array_key_exists('note', $fields['Contact'])) {
378 $noteTitle = $fields['Contact']['note']['title'];
379 $fields['Contact']['note']['title'] = $noteTitle . ': ' . ts('Body and Subject');
380 $fields['Contact']['note_body'] = array('title' => $noteTitle . ': ' . ts('Body Only'), 'name' => 'note_body');
381 $fields['Contact']['note_subject'] = array('title' => $noteTitle . ': ' . ts('Subject Only'), 'name' => 'note_subject');
385 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::CONTRIBUTE_EXPORT
)) {
386 if (CRM_Core_Permission
::access('CiviContribute')) {
387 $fields['Contribution'] = CRM_Contribute_BAO_Contribution
::exportableFields();
388 unset($fields['Contribution']['contribution_contact_id']);
389 unset($fields['Contribution']['contribution_status_id']);
390 $compArray['Contribution'] = ts('Contribution');
394 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::EVENT_EXPORT
)) {
395 if (CRM_Core_Permission
::access('CiviEvent')) {
396 $fields['Participant'] = CRM_Event_BAO_Participant
::exportableFields();
397 //get the component payment fields
398 if ($exportMode == CRM_Export_Form_Select
::EVENT_EXPORT
) {
399 require_once 'CRM/Export/BAO/Export.php';
400 $componentPaymentFields = array();
401 foreach (CRM_Export_BAO_Export
::componentPaymentFields() as $payField => $payTitle) {
402 $componentPaymentFields[$payField] = array('title' => $payTitle);
404 $fields['Participant'] = array_merge($fields['Participant'], $componentPaymentFields);
407 unset($fields['Participant']['participant_contact_id']);
408 $compArray['Participant'] = ts('Participant');
412 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::MEMBER_EXPORT
)) {
413 if (CRM_Core_Permission
::access('CiviMember')) {
414 $fields['Membership'] = CRM_Member_BAO_Membership
::getMembershipFields($exportMode);
415 unset($fields['Membership']['membership_contact_id']);
416 $compArray['Membership'] = ts('Membership');
420 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::PLEDGE_EXPORT
)) {
421 if (CRM_Core_Permission
::access('CiviPledge')) {
422 $fields['Pledge'] = CRM_Pledge_BAO_Pledge
::exportableFields();
423 unset($fields['Pledge']['pledge_contact_id']);
424 $compArray['Pledge'] = ts('Pledge');
428 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::CASE_EXPORT
)) {
429 if (CRM_Core_Permission
::access('CiviCase')) {
430 $fields['Case'] = CRM_Case_BAO_Case
::exportableFields();
431 $compArray['Case'] = ts('Case');
433 $fields['Activity'] = CRM_Activity_BAO_Activity
::exportableFields('Case');
434 $compArray['Activity'] = ts('Case Activity');
436 unset($fields['Case']['case_contact_id']);
439 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::GRANT_EXPORT
)) {
440 if (CRM_Core_Permission
::access('CiviGrant')) {
441 $fields['Grant'] = CRM_Grant_BAO_Grant
::exportableFields();
442 unset($fields['Grant']['grant_contact_id']);
443 if ($mappingType == 'Search Builder') {
444 unset($fields['Grant']['grant_type_id']);
446 $compArray['Grant'] = ts('Grant');
450 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::ACTIVITY_EXPORT
)) {
451 $fields['Activity'] = CRM_Activity_BAO_Activity
::exportableFields('Activity');
452 $compArray['Activity'] = ts('Activity');
455 //Contact Sub Type For export
456 $contactSubTypes = array();
457 $subTypes = CRM_Contact_BAO_ContactType
::subTypeInfo();
459 foreach ($subTypes as $subType => $val) {
460 //adding subtype specific relationships CRM-5256
461 $csRelationships = array();
463 if ($mappingType == 'Export') {
464 $subTypeRelationshipTypes =
465 CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, NULL, NULL, $val['parent'],
466 FALSE, 'label', TRUE, $subType);
468 foreach ($subTypeRelationshipTypes as $key => $var) {
469 if (!array_key_exists($key, $fields[$val['parent']])) {
470 list($type) = explode('_', $key);
472 $csRelationships[$key]['title'] = $var;
473 $csRelationships[$key]['headerPattern'] = '/' . preg_quote($var, '/') . '/';
474 $csRelationships[$key]['export'] = TRUE;
475 $csRelationships[$key]['relationship_type_id'] = $type;
476 $csRelationships[$key]['related'] = TRUE;
477 $csRelationships[$key]['hasRelationType'] = 1;
482 $fields[$subType] = $fields[$val['parent']] +
$csRelationships;
484 //custom fields for sub type
485 $subTypeFields = CRM_Core_BAO_CustomField
::getFieldsForImport($subType);
486 $fields[$subType] +
= $subTypeFields;
488 if (!empty($subTypeFields) ||
!empty($csRelationships)) {
489 $contactSubTypes[$subType] = $val['label'];
495 foreach ($fields as $key => $value) {
497 foreach ($value as $key1 => $value1) {
498 //CRM-2676, replacing the conflict for same custom field name from different custom group.
499 $customGroupName = self
::getCustomGroupName($key1);
501 if ($customGroupName) {
502 $relatedMapperFields[$key][$key1] = $mapperFields[$key][$key1] = $customGroupName . ': ' . $value1['title'];
505 $relatedMapperFields[$key][$key1] = $mapperFields[$key][$key1] = $value1['title'];
507 if (isset($value1['hasLocationType'])) {
508 $hasLocationTypes[$key][$key1] = $value1['hasLocationType'];
511 if (isset($value1['hasRelationType'])) {
512 $hasRelationTypes[$key][$key1] = $value1['hasRelationType'];
513 unset($relatedMapperFields[$key][$key1]);
517 if (array_key_exists('related', $relatedMapperFields[$key])) {
518 unset($relatedMapperFields[$key]['related']);
522 $mapperKeys = array_keys($mapperFields);
524 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
526 $defaultLocationType = CRM_Core_BAO_LocationType
::getDefault();
528 // FIXME: dirty hack to make the default option show up first. This
529 // avoids a mozilla browser bug with defaults on dynamically constructed
531 if ($defaultLocationType) {
532 $defaultLocation = $locationTypes[$defaultLocationType->id
];
533 unset($locationTypes[$defaultLocationType->id
]);
534 $locationTypes = array($defaultLocationType->id
=> $defaultLocation) +
$locationTypes;
537 $locationTypes = array(' ' => ts('Primary')) +
$locationTypes;
539 // since we need a hierarchical list to display contact types & subtypes,
540 // this is what we going to display in first selector
541 $contactTypes = CRM_Contact_BAO_ContactType
::getSelectElements(FALSE, FALSE);
542 if ($mappingType == 'Search Builder') {
543 $contactTypes = array('Contact' => ts('Contacts')) +
$contactTypes;
546 $sel1 = array('' => ts('- select record type -')) +
$contactTypes +
$compArray;
548 foreach ($sel1 as $key => $sel) {
550 // sort everything BUT the contactType which is sorted seperately by
551 // an initial commit of CRM-13278 (check ksort above)
552 if (!in_array($key, $contactType)) {
553 asort($mapperFields[$key]);
555 $sel2[$key] = array('' => ts('- select field -')) +
$mapperFields[$key];
561 $phoneTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', 'phone_type_id');
562 $imProviders = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_IM', 'provider_id');
565 foreach ($sel1 as $k => $sel) {
567 foreach ($locationTypes as $key => $value) {
568 if (trim($key) != '') {
569 $sel4[$k]['phone'][$key] = &$phoneTypes;
570 $sel4[$k]['im'][$key] = &$imProviders;
576 foreach ($sel1 as $k => $sel) {
578 foreach ($mapperFields[$k] as $key => $value) {
579 if (isset($hasLocationTypes[$k][$key])) {
580 $sel3[$k][$key] = $locationTypes;
589 // Array for core fields and relationship custom data
590 $relationshipTypes = CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, NULL, NULL, NULL, TRUE);
592 if ($mappingType == 'Export') {
593 foreach ($sel1 as $k => $sel) {
595 foreach ($mapperFields[$k] as $field => $dontCare) {
596 if (isset($hasRelationTypes[$k][$field])) {
597 list($id, $first, $second) = explode('_', $field);
598 // FIX ME: For now let's not expose custom data related to relationship
599 $relationshipCustomFields = array();
600 //$relationshipCustomFields = self::getRelationTypeCustomGroupData( $id );
601 //asort( $relationshipCustomFields ) ;
603 $relationshipType = new CRM_Contact_BAO_RelationshipType();
604 $relationshipType->id
= $id;
605 if ($relationshipType->find(TRUE)) {
606 $direction = "contact_sub_type_$second";
607 if (isset($relationshipType->$direction)) {
608 $relatedFields = array_merge((array)$relatedMapperFields[$relationshipType->$direction], (array)$relationshipCustomFields);
611 $target_type = 'contact_type_' . $second;
612 $relatedFields = array_merge((array)$relatedMapperFields[$relationshipType->$target_type], (array)$relationshipCustomFields);
615 $relationshipType->free();
616 asort($relatedFields);
617 $sel5[$k][$field] = $relatedFields;
623 //Location Type for relationship fields
624 foreach ($sel5 as $k => $v) {
626 foreach ($v as $rel => $fields) {
627 foreach ($fields as $field => $fieldLabel) {
628 if (isset($hasLocationTypes[$k][$field])) {
629 $sel6[$k][$rel][$field] = $locationTypes;
636 //PhoneTypes for relationship fields
638 foreach ($sel6 as $k => $rel) {
640 foreach ($rel as $phonekey => $phonevalue) {
641 foreach ($locationTypes as $locType => $loc) {
642 if (trim($locType) != '') {
643 $sel7[$k][$phonekey]['phone'][$locType] = &$phoneTypes;
644 $sel7[$k][$phonekey]['im'][$locType] = &$imProviders;
652 //special fields that have location, hack for primary location
653 $specialFields = array(
654 'street_address', 'supplemental_address_1', 'supplemental_address_2',
655 'city', 'postal_code', 'postal_code_suffix', 'geo_code_1', 'geo_code_2',
656 'state_province', 'country', 'phone', 'email', 'im',
659 if (isset($mappingId)) {
662 list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingImProvider,
663 $mappingRelation, $mappingOperator, $mappingValue
664 ) = CRM_Core_BAO_Mapping
::getMappingFields($mappingId);
666 $blkCnt = count($mappingName);
667 if ($blkCnt >= $blockCount) {
668 $blockCount = $blkCnt +
1;
670 for ($x = 1; $x < $blockCount; $x++
) {
671 if (isset($mappingName[$x])) {
672 $colCnt = count($mappingName[$x]);
673 if ($colCnt >= $columnCount[$x]) {
674 $columnCount[$x] = $colCnt;
680 $form->_blockCount
= $blockCount;
681 $form->_columnCount
= $columnCount;
683 $form->set('blockCount', $form->_blockCount
);
684 $form->set('columnCount', $form->_columnCount
);
686 $defaults = $noneArray = $nullArray = array();
688 //used to warn for mismatch column count or mismatch mapping
690 for ($x = 1; $x < $blockCount; $x++
) {
692 for ($i = 0; $i < $columnCount[$x]; $i++
) {
694 $sel = &$form->addElement('hierselect', "mapper[$x][$i]", ts('Mapper for Field %1', array(1 => $i)), NULL);
697 if (isset($mappingId)) {
698 $locationId = isset($mappingLocation[$x][$i]) ?
$mappingLocation[$x][$i] : 0;
699 if (isset($mappingName[$x][$i])) {
700 if (is_array($mapperFields[$mappingContactType[$x][$i]])) {
702 if (isset($mappingRelation[$x][$i])) {
704 $contactDetails = strtolower(str_replace(" ", "_", $mappingName[$x][$i]));
705 $relLocationId = isset($mappingLocation[$x][$i]) ?
$mappingLocation[$x][$i] : 0;
706 if (!$relLocationId && in_array($mappingName[$x][$i], $specialFields)) {
707 $relLocationId = " ";
710 $relPhoneType = isset($mappingPhoneType[$x][$i]) ?
$mappingPhoneType[$x][$i] : NULL;
712 $defaults["mapper[$x][$i]"] = array(
713 $mappingContactType[$x][$i],
714 $mappingRelation[$x][$i],
717 $mappingName[$x][$i],
723 $noneArray[] = array($x, $i, 2);
725 if (!$phoneType && !$imProvider) {
726 $noneArray[] = array($x, $i, 3);
728 if (!$mappingName[$x][$i]) {
729 $noneArray[] = array($x, $i, 4);
731 if (!$relLocationId) {
732 $noneArray[] = array($x, $i, 5);
734 if (!$relPhoneType) {
735 $noneArray[] = array($x, $i, 6);
737 $noneArray[] = array($x, $i, 2);
740 $phoneType = isset($mappingPhoneType[$x][$i]) ?
$mappingPhoneType[$x][$i] : NULL;
741 $imProvider = isset($mappingImProvider[$x][$i]) ?
$mappingImProvider[$x][$i] : NULL;
742 if (!$locationId && in_array($mappingName[$x][$i], $specialFields)) {
746 $defaults["mapper[$x][$i]"] = array(
747 $mappingContactType[$x][$i],
748 $mappingName[$x][$i],
752 if (!$mappingName[$x][$i]) {
753 $noneArray[] = array($x, $i, 1);
756 $noneArray[] = array($x, $i, 2);
758 if (!$phoneType && !$imProvider) {
759 $noneArray[] = array($x, $i, 3);
762 $noneArray[] = array($x, $i, 4);
763 $noneArray[] = array($x, $i, 5);
764 $noneArray[] = array($x, $i, 6);
769 if (CRM_Utils_Array
::value($i, CRM_Utils_Array
::value($x, $mappingOperator))) {
770 $defaults["operator[$x][$i]"] = CRM_Utils_Array
::value($i, $mappingOperator[$x]);
773 if (CRM_Utils_Array
::value($i, CRM_Utils_Array
::value($x, $mappingValue))) {
774 $defaults["value[$x][$i]"] = CRM_Utils_Array
::value($i, $mappingValue[$x]);
779 //Fix for Search Builder
780 if ($mappingType == 'Export') {
787 $formValues = $form->exportValues();
789 if (empty($formValues)) {
790 // Incremented length for third select box(relationship type)
791 for ($k = 1; $k < $j; $k++
) {
792 $noneArray[] = array($x, $i, $k);
796 if (!empty($formValues['mapper'][$x])) {
797 foreach ($formValues['mapper'][$x] as $value) {
798 for ($k = 1; $k < $j; $k++
) {
799 if (!isset($formValues['mapper'][$x][$i][$k]) ||
800 (!$formValues['mapper'][$x][$i][$k])
802 $noneArray[] = array($x, $i, $k);
805 $nullArray[] = array($x, $i, $k);
811 for ($k = 1; $k < $j; $k++
) {
812 $noneArray[] = array($x, $i, $k);
817 //Fix for Search Builder
818 if ($mappingType == 'Export') {
819 if (!isset($mappingId) ||
$i >= count(reset($mappingName))) {
820 if (isset($formValues['mapper']) &&
821 isset($formValues['mapper'][$x][$i][1]) &&
822 array_key_exists($formValues['mapper'][$x][$i][1], $relationshipTypes)
824 $sel->setOptions(array($sel1, $sel2, $sel5, $sel6, $sel7, $sel3, $sel4));
827 $sel->setOptions(array($sel1, $sel2, $sel3, $sel4, $sel5, $sel6, $sel7));
831 $sel->setOptions(array($sel1, $sel2, $sel3, $sel4, $sel5, $sel6, $sel7));
835 $sel->setOptions(array($sel1, $sel2, $sel3, $sel4));
838 if ($mappingType == 'Search Builder') {
839 //CRM -2292, restricted array set
840 $operatorArray = array('' => ts('-operator-')) + CRM_Core_SelectValues
::getSearchBuilderOperators();
842 $form->add('select', "operator[$x][$i]", '', $operatorArray);
843 $form->add('text', "value[$x][$i]", '');
846 //end of columnCnt for
847 if ($mappingType == 'Search Builder') {
848 $title = ts('Another search field');
851 $title = ts('Select more fields');
854 $form->addElement('submit', "addMore[$x]", $title, array('class' => 'submit-link'));
858 $js = "<script type='text/javascript'>\n";
859 $formName = "document.{$name}";
860 if (!empty($nullArray)) {
861 $js .= "var nullArray = [";
864 foreach ($nullArray as $element) {
865 $key = "{$element[0]}, {$element[1]}, {$element[2]}";
866 if (!isset($seen[$key])) {
867 $elements[] = "[$key]";
871 $js .= implode(', ', $elements);
874 for (var i=0;i<nullArray.length;i++) {
875 if ( {$formName}['mapper['+nullArray[i][0]+']['+nullArray[i][1]+']['+nullArray[i][2]+']'] ) {
876 {$formName}['mapper['+nullArray[i][0]+']['+nullArray[i][1]+']['+nullArray[i][2]+']'].style.display = '';
881 if (!empty($noneArray)) {
882 $js .= "var noneArray = [";
885 foreach ($noneArray as $element) {
886 $key = "{$element[0]}, {$element[1]}, {$element[2]}";
887 if (!isset($seen[$key])) {
888 $elements[] = "[$key]";
892 $js .= implode(', ', $elements);
895 for (var i=0;i<noneArray.length;i++) {
896 if ( {$formName}['mapper['+noneArray[i][0]+']['+noneArray[i][1]+']['+noneArray[i][2]+']'] ) {
897 {$formName}['mapper['+noneArray[i][0]+']['+noneArray[i][1]+']['+noneArray[i][2]+']'].style.display = 'none';
902 $js .= "</script>\n";
904 $form->assign('initHideBoxes', $js);
905 $form->assign('columnCount', $columnCount);
906 $form->assign('blockCount', $blockCount);
907 $form->setDefaults($defaults);
909 $form->setDefaultAction('refresh');
913 * Function returns all custom fields with group title and
916 * @param int $relationshipTypeId
917 * Related relationship type id.
919 * @return array all custom field titles
921 public function getRelationTypeCustomGroupData($relationshipTypeId) {
923 $customFields = CRM_Core_BAO_CustomField
::getFields('Relationship', NULL, NULL, $relationshipTypeId, NULL, NULL);
924 $groupTitle = array();
925 foreach ($customFields as $krelation => $vrelation) {
926 $groupTitle[$vrelation['label']] = $vrelation['groupTitle'] . '...' . $vrelation['label'];
933 * Function returns all Custom group Names
935 * @param int $customfieldId
938 * @return null|string $customGroupName all custom group names@static
940 public static function getCustomGroupName($customfieldId) {
941 if ($customFieldId = CRM_Core_BAO_CustomField
::getKeyID($customfieldId)) {
942 $customGroupId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField', $customFieldId, 'custom_group_id');
943 $customGroupName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'title');
945 if (strlen($customGroupName) > 13) {
946 $customGroupName = substr($customGroupName, 0, 10) . '...';
949 return $customGroupName;
954 * Function returns associated array of elements, that will be passed for search
956 * @param array $params
957 * Associated array of submitted values.
959 * Row no of the fields.
962 * @return array $returnFields formatted associated array of elements@static
964 public static function formattedFields(&$params, $row = FALSE) {
967 if (empty($params) ||
!isset($params['mapper'])) {
971 $types = array('Individual', 'Organization', 'Household');
972 foreach ($params['mapper'] as $key => $value) {
974 foreach ($value as $k => $v) {
975 if (in_array($v[0], $types)) {
976 if ($contactType && $contactType != $v[0]) {
977 CRM_Core_Error
::fatal(ts("Cannot have two clauses with different types: %1, %2",
978 array(1 => $contactType, 2 => $v[0])
981 $contactType = $v[0];
983 if (!empty($v['1'])) {
985 $v2 = CRM_Utils_Array
::value('2', $v);
986 if ($v2 && trim($v2)) {
987 $fldName .= "-{$v[2]}";
990 $v3 = CRM_Utils_Array
::value('3', $v);
991 if ($v3 && trim($v3)) {
992 $fldName .= "-{$v[3]}";
995 $value = $params['value'][$key][$k];
996 if ($fldName == 'group' ||
$fldName == 'tag') {
997 $value = trim($value);
998 $value = str_replace('(', '', $value);
999 $value = str_replace(')', '', $value);
1001 $v = explode(',', $value);
1003 foreach ($v as $i) {
1008 if ($v[0] == 'Contribution' && substr($fldName, 0, 7) != 'custom_'
1009 && substr($fldName, 0, 10) != 'financial_') {
1010 if (substr($fldName, 0, 13) != 'contribution_') {
1011 $fldName = 'contribution_' . $fldName;
1015 // CRM-14563: we store checkbox, multi-select and adv-multi select custom field using separator, hence it
1016 // needs special handling.
1017 if ($cfID = CRM_Core_BAO_CustomField
::getKeyID($v[1])) {
1018 $isCustomField = TRUE;
1019 $customFieldType = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField', $cfID, 'html_type');
1020 $specialHTMLType = array(
1024 'Multi-Select State/Province',
1025 'Multi-Select Country'
1028 // override the operator to handle separator ( note: this might have some performance issues )
1029 if (in_array($customFieldType, $specialHTMLType)) {
1030 // FIX ME: != and few other operators are not handled
1031 $specialOperators = array('=', 'IN', 'LIKE');
1033 if (in_array($params['operator'][$key][$k], $specialOperators)) {
1034 $params['operator'][$key][$k] = 'RLIKE';
1039 // CRM-14983: verify if values are comma separated convert to array
1040 if (!is_array($value) && (strpos($value,',') !== false ||
strstr($value, '(')) && empty($isCustomField) && $params['operator'][$key][$k] == 'IN') {
1041 preg_match('#\((.*?)\)#', $value, $match);
1042 $tmpArray = explode(',', $match[1]);
1043 $value = array_combine(array_values($tmpArray),array_values($tmpArray));
1049 $params['operator'][$key][$k],
1058 $params['operator'][$key][$k],
1077 //add sortByCharacter values
1078 if (isset($params['sortByCharacter'])) {
1082 $params['sortByCharacter'],
1093 * @param array $params
1097 public static function &returnProperties(&$params) {
1099 'contact_type' => 1,
1100 'contact_sub_type' => 1,
1104 if (empty($params) ||
empty($params['mapper'])) {
1108 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
1109 foreach ($params['mapper'] as $key => $value) {
1110 foreach ($value as $k => $v) {
1112 if ($v[1] == 'groups' ||
$v[1] == 'tags') {
1116 if (isset($v[2]) && is_numeric($v[2])) {
1117 if (!array_key_exists('location', $fields)) {
1118 $fields['location'] = array();
1121 // make sure that we have a location fields and a location type for this
1122 $locationName = $locationTypes[$v[2]];
1123 if (!array_key_exists($locationName, $fields['location'])) {
1124 $fields['location'][$locationName] = array();
1125 $fields['location'][$locationName]['location_type'] = $v[2];
1128 if ($v[1] == 'phone' ||
$v[1] == 'email' ||
$v[1] == 'im') {
1129 // phone type handling
1131 $fields['location'][$locationName][$v[1] . "-" . $v[3]] = 1;
1134 $fields['location'][$locationName][$v[1]] = 1;
1138 $fields['location'][$locationName][$v[1]] = 1;
1152 * Save the mapping field info for search builder / export given the formvalues
1154 * @param array $params
1155 * Asscociated array of formvalues.
1156 * @param int $mappingId
1162 public static function saveMappingFields(&$params, $mappingId) {
1163 //delete mapping fields records for exixting mapping
1164 $mappingFields = new CRM_Core_DAO_MappingField();
1165 $mappingFields->mapping_id
= $mappingId;
1166 $mappingFields->delete();
1168 if (empty($params['mapper'])) {
1172 //save record in mapping field table
1173 foreach ($params['mapper'] as $key => $value) {
1175 foreach ($value as $k => $v) {
1177 if (!empty($v['1'])) {
1178 $saveMappingFields = new CRM_Core_DAO_MappingField();
1180 $saveMappingFields->mapping_id
= $mappingId;
1181 $saveMappingFields->name
= CRM_Utils_Array
::value('1', $v);
1182 $saveMappingFields->contact_type
= CRM_Utils_Array
::value('0', $v);
1183 $locationId = CRM_Utils_Array
::value('2', $v);
1184 $saveMappingFields->location_type_id
= is_numeric($locationId) ?
$locationId : NULL;
1186 if ($v[1] == 'phone') {
1187 $saveMappingFields->phone_type_id
= CRM_Utils_Array
::value('3', $v);
1189 elseif ($v[1] == 'im') {
1190 $saveMappingFields->im_provider_id
= CRM_Utils_Array
::value('3', $v);
1193 if (!empty($params['operator'])) {
1194 $saveMappingFields->operator
= CRM_Utils_Array
::value($k, $params['operator'][$key]);
1196 if (!empty($params['value'])) {
1197 $saveMappingFields->value
= CRM_Utils_Array
::value($k, $params['value'][$key]);
1199 // Handle mapping for 'related contact' fields
1200 if (count(explode('_', CRM_Utils_Array
::value('1', $v))) > 2) {
1201 list($id, $first, $second) = explode('_', CRM_Utils_Array
::value('1', $v));
1202 if (($first == 'a' && $second == 'b') ||
($first == 'b' && $second == 'a')) {
1204 if (!empty($v['2'])) {
1205 $saveMappingFields->name
= CRM_Utils_Array
::value('2', $v);
1207 elseif (!empty($v['4'])) {
1208 $saveMappingFields->name
= CRM_Utils_Array
::value('4', $v);
1211 if (is_numeric(CRM_Utils_Array
::value('3', $v))) {
1212 $locationTypeid = CRM_Utils_Array
::value('3', $v);
1214 elseif (is_numeric(CRM_Utils_Array
::value('5', $v))) {
1215 $locationTypeid = CRM_Utils_Array
::value('5', $v);
1218 if (is_numeric(CRM_Utils_Array
::value('4', $v))) {
1219 $phoneTypeid = CRM_Utils_Array
::value('4', $v);
1221 elseif (is_numeric(CRM_Utils_Array
::value('6', $v))) {
1222 $phoneTypeid = CRM_Utils_Array
::value('6', $v);
1225 $saveMappingFields->location_type_id
= is_numeric($locationTypeid) ?
$locationTypeid : NULL;
1226 $saveMappingFields->phone_type_id
= is_numeric($phoneTypeid) ?
$phoneTypeid : NULL;
1227 $saveMappingFields->relationship_type_id
= $id;
1228 $saveMappingFields->relationship_direction
= "{$first}_{$second}";
1232 $saveMappingFields->grouping
= $key;
1233 $saveMappingFields->column_number
= $colCnt;
1234 $saveMappingFields->save();
1236 $locationTypeid = $phoneTypeid = NULL;