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
197 * @param string $nameField
199 * @param string $mapTypeId
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
220 public static function getFormattedFields($smartGroupId) {
221 $returnFields = array();
223 //get the fields from mapping table
224 $dao = new CRM_Core_DAO_MappingField();
225 $dao->mapping_id
= $smartGroupId;
227 while ($dao->fetch()) {
228 $fldName = $dao->name
;
229 if ($dao->location_type_id
) {
230 $fldName .= "-{$dao->location_type_id}";
232 if ($dao->phone_type
) {
233 $fldName .= "-{$dao->phone_type}";
235 $returnFields[$fldName]['value'] = $dao->value
;
236 $returnFields[$fldName]['op'] = $dao->operator
;
237 $returnFields[$fldName]['grouping'] = $dao->grouping
;
239 return $returnFields;
243 * Build the mapping form
245 * @param CRM_Core_Form $form
246 * @param string $mappingType
247 * (Export/Import/Search Builder).
248 * @param int $mappingId
249 * @param int $columnNo
250 * @param int $blockCount
251 * (no of blocks shown).
252 * @param NULL $exportMode
257 public static function buildMappingForm(&$form, $mappingType = 'Export', $mappingId = NULL, $columnNo, $blockCount = 3, $exportMode = NULL) {
258 if ($mappingType == 'Export') {
260 $columnCount = array('1' => $columnNo);
262 elseif ($mappingType == 'Search Builder') {
264 $columnCount = $columnNo;
267 //get the saved mapping details
269 if ($mappingType == 'Export') {
270 $form->applyFilter('saveMappingName', 'trim');
272 //to save the current mappings
273 if (!isset($mappingId)) {
274 $saveDetailsName = ts('Save this field mapping');
275 $form->add('text', 'saveMappingName', ts('Name'));
276 $form->add('text', 'saveMappingDesc', ts('Description'));
279 $form->assign('loadedMapping', $mappingId);
281 $params = array('id' => $mappingId);
283 $mappingDetails = CRM_Core_BAO_Mapping
::retrieve($params, $temp);
285 $form->assign('savedName', $mappingDetails->name
);
287 $form->add('hidden', 'mappingId', $mappingId);
289 $form->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL);
290 $saveDetailsName = ts('Save as a new field mapping');
291 $form->add('text', 'saveMappingName', ts('Name'));
292 $form->add('text', 'saveMappingDesc', ts('Description'));
295 $form->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, array('onclick' => "showSaveDetails(this)"));
296 $form->addFormRule(array('CRM_Export_Form_Map', 'formRule'), $form->get('mappingTypeId'));
298 elseif ($mappingType == 'Search Builder') {
299 $form->addElement('submit', 'addBlock', ts('Also include contacts where'),
300 array('class' => 'submit-link')
305 $hasLocationTypes = array();
306 $hasRelationTypes = array();
309 if ($mappingType == 'Export') {
312 elseif ($mappingType == 'Search Builder') {
316 $contactType = array('Individual', 'Household', 'Organization');
317 foreach ($contactType as $value) {
318 $contactFields = CRM_Contact_BAO_Contact
::exportableFields($value, FALSE, $required);
319 $contactFields = array_merge($contactFields, CRM_Contact_BAO_Query_Hook
::singleton()->getFields());
321 // exclude the address options disabled in the Address Settings
322 $fields[$value] = CRM_Core_BAO_Address
::validateAddressOptions($contactFields);
323 ksort($fields[$value]);
324 if ($mappingType == 'Export') {
325 $relationships = array();
326 $relationshipTypes = CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, NULL, NULL, $value);
327 asort($relationshipTypes);
329 foreach ($relationshipTypes as $key => $var) {
330 list($type) = explode('_', $key);
332 $relationships[$key]['title'] = $var;
333 $relationships[$key]['headerPattern'] = '/' . preg_quote($var, '/') . '/';
334 $relationships[$key]['export'] = TRUE;
335 $relationships[$key]['relationship_type_id'] = $type;
336 $relationships[$key]['related'] = TRUE;
337 $relationships[$key]['hasRelationType'] = 1;
340 if (!empty($relationships)) {
341 $fields[$value] = array_merge($fields[$value],
342 array('related' => array('title' => ts('- related contact info -'))),
349 //get the current employer for mapping.
351 $fields['Individual']['current_employer']['title'] = ts('Current Employer');
354 // add component fields
355 $compArray = array();
357 //we need to unset groups, tags, notes for component export
358 if ($exportMode != CRM_Export_Form_Select
::CONTACT_EXPORT
) {
360 'groups', 'tags', 'notes') as $value) {
361 unset($fields['Individual'][$value]);
362 unset($fields['Household'][$value]);
363 unset($fields['Organization'][$value]);
367 if ($mappingType == 'Search Builder') {
368 //build the common contact fields array.
369 $fields['Contact'] = array();
370 foreach ($fields['Individual'] as $key => $value) {
371 if (!empty($fields['Household'][$key]) && !empty($fields['Organization'][$key])) {
372 $fields['Contact'][$key] = $value;
373 unset($fields['Organization'][$key],
374 $fields['Household'][$key],
375 $fields['Individual'][$key]);
378 if (array_key_exists('note', $fields['Contact'])) {
379 $noteTitle = $fields['Contact']['note']['title'];
380 $fields['Contact']['note']['title'] = $noteTitle . ': ' . ts('Body and Subject');
381 $fields['Contact']['note_body'] = array('title' => $noteTitle . ': ' . ts('Body Only'), 'name' => 'note_body');
382 $fields['Contact']['note_subject'] = array('title' => $noteTitle . ': ' . ts('Subject Only'), 'name' => 'note_subject');
386 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::CONTRIBUTE_EXPORT
)) {
387 if (CRM_Core_Permission
::access('CiviContribute')) {
388 $fields['Contribution'] = CRM_Contribute_BAO_Contribution
::exportableFields();
389 unset($fields['Contribution']['contribution_contact_id']);
390 unset($fields['Contribution']['contribution_status_id']);
391 $compArray['Contribution'] = ts('Contribution');
395 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::EVENT_EXPORT
)) {
396 if (CRM_Core_Permission
::access('CiviEvent')) {
397 $fields['Participant'] = CRM_Event_BAO_Participant
::exportableFields();
398 //get the component payment fields
399 if ($exportMode == CRM_Export_Form_Select
::EVENT_EXPORT
) {
400 require_once 'CRM/Export/BAO/Export.php';
401 $componentPaymentFields = array();
402 foreach (CRM_Export_BAO_Export
::componentPaymentFields() as $payField => $payTitle) {
403 $componentPaymentFields[$payField] = array('title' => $payTitle);
405 $fields['Participant'] = array_merge($fields['Participant'], $componentPaymentFields);
408 unset($fields['Participant']['participant_contact_id']);
409 $compArray['Participant'] = ts('Participant');
413 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::MEMBER_EXPORT
)) {
414 if (CRM_Core_Permission
::access('CiviMember')) {
415 $fields['Membership'] = CRM_Member_BAO_Membership
::getMembershipFields($exportMode);
416 unset($fields['Membership']['membership_contact_id']);
417 $compArray['Membership'] = ts('Membership');
421 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::PLEDGE_EXPORT
)) {
422 if (CRM_Core_Permission
::access('CiviPledge')) {
423 $fields['Pledge'] = CRM_Pledge_BAO_Pledge
::exportableFields();
424 unset($fields['Pledge']['pledge_contact_id']);
425 $compArray['Pledge'] = ts('Pledge');
429 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::CASE_EXPORT
)) {
430 if (CRM_Core_Permission
::access('CiviCase')) {
431 $fields['Case'] = CRM_Case_BAO_Case
::exportableFields();
432 $compArray['Case'] = ts('Case');
434 $fields['Activity'] = CRM_Activity_BAO_Activity
::exportableFields('Case');
435 $compArray['Activity'] = ts('Case Activity');
437 unset($fields['Case']['case_contact_id']);
440 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::GRANT_EXPORT
)) {
441 if (CRM_Core_Permission
::access('CiviGrant')) {
442 $fields['Grant'] = CRM_Grant_BAO_Grant
::exportableFields();
443 unset($fields['Grant']['grant_contact_id']);
444 if ($mappingType == 'Search Builder') {
445 unset($fields['Grant']['grant_type_id']);
447 $compArray['Grant'] = ts('Grant');
451 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::ACTIVITY_EXPORT
)) {
452 $fields['Activity'] = CRM_Activity_BAO_Activity
::exportableFields('Activity');
453 $compArray['Activity'] = ts('Activity');
456 //Contact Sub Type For export
457 $contactSubTypes = array();
458 $subTypes = CRM_Contact_BAO_ContactType
::subTypeInfo();
460 foreach ($subTypes as $subType => $val) {
461 //adding subtype specific relationships CRM-5256
462 $csRelationships = array();
464 if ($mappingType == 'Export') {
465 $subTypeRelationshipTypes =
466 CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, NULL, NULL, $val['parent'],
467 FALSE, 'label', TRUE, $subType);
469 foreach ($subTypeRelationshipTypes as $key => $var) {
470 if (!array_key_exists($key, $fields[$val['parent']])) {
471 list($type) = explode('_', $key);
473 $csRelationships[$key]['title'] = $var;
474 $csRelationships[$key]['headerPattern'] = '/' . preg_quote($var, '/') . '/';
475 $csRelationships[$key]['export'] = TRUE;
476 $csRelationships[$key]['relationship_type_id'] = $type;
477 $csRelationships[$key]['related'] = TRUE;
478 $csRelationships[$key]['hasRelationType'] = 1;
483 $fields[$subType] = $fields[$val['parent']] +
$csRelationships;
485 //custom fields for sub type
486 $subTypeFields = CRM_Core_BAO_CustomField
::getFieldsForImport($subType);
487 $fields[$subType] +
= $subTypeFields;
489 if (!empty($subTypeFields) ||
!empty($csRelationships)) {
490 $contactSubTypes[$subType] = $val['label'];
496 foreach ($fields as $key => $value) {
498 foreach ($value as $key1 => $value1) {
499 //CRM-2676, replacing the conflict for same custom field name from different custom group.
500 $customGroupName = self
::getCustomGroupName($key1);
502 if ($customGroupName) {
503 $relatedMapperFields[$key][$key1] = $mapperFields[$key][$key1] = $customGroupName . ': ' . $value1['title'];
506 $relatedMapperFields[$key][$key1] = $mapperFields[$key][$key1] = $value1['title'];
508 if (isset($value1['hasLocationType'])) {
509 $hasLocationTypes[$key][$key1] = $value1['hasLocationType'];
512 if (isset($value1['hasRelationType'])) {
513 $hasRelationTypes[$key][$key1] = $value1['hasRelationType'];
514 unset($relatedMapperFields[$key][$key1]);
518 if (array_key_exists('related', $relatedMapperFields[$key])) {
519 unset($relatedMapperFields[$key]['related']);
523 $mapperKeys = array_keys($mapperFields);
525 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
527 $defaultLocationType = CRM_Core_BAO_LocationType
::getDefault();
529 // FIXME: dirty hack to make the default option show up first. This
530 // avoids a mozilla browser bug with defaults on dynamically constructed
532 if ($defaultLocationType) {
533 $defaultLocation = $locationTypes[$defaultLocationType->id
];
534 unset($locationTypes[$defaultLocationType->id
]);
535 $locationTypes = array($defaultLocationType->id
=> $defaultLocation) +
$locationTypes;
538 $locationTypes = array(' ' => ts('Primary')) +
$locationTypes;
540 // since we need a hierarchical list to display contact types & subtypes,
541 // this is what we going to display in first selector
542 $contactTypes = CRM_Contact_BAO_ContactType
::getSelectElements(FALSE, FALSE);
543 if ($mappingType == 'Search Builder') {
544 $contactTypes = array('Contact' => ts('Contacts')) +
$contactTypes;
547 $sel1 = array('' => ts('- select record type -')) +
$contactTypes +
$compArray;
549 foreach ($sel1 as $key => $sel) {
551 // sort everything BUT the contactType which is sorted seperately by
552 // an initial commit of CRM-13278 (check ksort above)
553 if (!in_array($key, $contactType)) {
554 asort($mapperFields[$key]);
556 $sel2[$key] = array('' => ts('- select field -')) +
$mapperFields[$key];
562 $phoneTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', 'phone_type_id');
563 $imProviders = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_IM', 'provider_id');
566 foreach ($sel1 as $k => $sel) {
568 foreach ($locationTypes as $key => $value) {
569 if (trim($key) != '') {
570 $sel4[$k]['phone'][$key] = &$phoneTypes;
571 $sel4[$k]['im'][$key] = &$imProviders;
577 foreach ($sel1 as $k => $sel) {
579 foreach ($mapperFields[$k] as $key => $value) {
580 if (isset($hasLocationTypes[$k][$key])) {
581 $sel3[$k][$key] = $locationTypes;
590 // Array for core fields and relationship custom data
591 $relationshipTypes = CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, NULL, NULL, NULL, TRUE);
593 if ($mappingType == 'Export') {
594 foreach ($sel1 as $k => $sel) {
596 foreach ($mapperFields[$k] as $field => $dontCare) {
597 if (isset($hasRelationTypes[$k][$field])) {
598 list($id, $first, $second) = explode('_', $field);
599 // FIX ME: For now let's not expose custom data related to relationship
600 $relationshipCustomFields = array();
601 //$relationshipCustomFields = self::getRelationTypeCustomGroupData( $id );
602 //asort($relationshipCustomFields);
604 $relationshipType = new CRM_Contact_BAO_RelationshipType();
605 $relationshipType->id
= $id;
606 if ($relationshipType->find(TRUE)) {
607 $direction = "contact_sub_type_$second";
608 if (isset($relationshipType->$direction)) {
609 $relatedFields = array_merge((array)$relatedMapperFields[$relationshipType->$direction], (array)$relationshipCustomFields);
612 $target_type = 'contact_type_' . $second;
613 $relatedFields = array_merge((array)$relatedMapperFields[$relationshipType->$target_type], (array)$relationshipCustomFields);
616 $relationshipType->free();
617 asort($relatedFields);
618 $sel5[$k][$field] = $relatedFields;
624 //Location Type for relationship fields
625 foreach ($sel5 as $k => $v) {
627 foreach ($v as $rel => $fields) {
628 foreach ($fields as $field => $fieldLabel) {
629 if (isset($hasLocationTypes[$k][$field])) {
630 $sel6[$k][$rel][$field] = $locationTypes;
637 //PhoneTypes for relationship fields
639 foreach ($sel6 as $k => $rel) {
641 foreach ($rel as $phonekey => $phonevalue) {
642 foreach ($locationTypes as $locType => $loc) {
643 if (trim($locType) != '') {
644 $sel7[$k][$phonekey]['phone'][$locType] = &$phoneTypes;
645 $sel7[$k][$phonekey]['im'][$locType] = &$imProviders;
653 //special fields that have location, hack for primary location
654 $specialFields = array(
655 'street_address', 'supplemental_address_1', 'supplemental_address_2',
656 'city', 'postal_code', 'postal_code_suffix', 'geo_code_1', 'geo_code_2',
657 'state_province', 'country', 'phone', 'email', 'im',
660 if (isset($mappingId)) {
663 list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingImProvider,
664 $mappingRelation, $mappingOperator, $mappingValue
665 ) = CRM_Core_BAO_Mapping
::getMappingFields($mappingId);
667 $blkCnt = count($mappingName);
668 if ($blkCnt >= $blockCount) {
669 $blockCount = $blkCnt +
1;
671 for ($x = 1; $x < $blockCount; $x++
) {
672 if (isset($mappingName[$x])) {
673 $colCnt = count($mappingName[$x]);
674 if ($colCnt >= $columnCount[$x]) {
675 $columnCount[$x] = $colCnt;
681 $form->_blockCount
= $blockCount;
682 $form->_columnCount
= $columnCount;
684 $form->set('blockCount', $form->_blockCount
);
685 $form->set('columnCount', $form->_columnCount
);
687 $defaults = $noneArray = $nullArray = array();
689 //used to warn for mismatch column count or mismatch mapping
691 for ($x = 1; $x < $blockCount; $x++
) {
693 for ($i = 0; $i < $columnCount[$x]; $i++
) {
695 $sel = &$form->addElement('hierselect', "mapper[$x][$i]", ts('Mapper for Field %1', array(1 => $i)), NULL);
698 if (isset($mappingId)) {
699 $locationId = isset($mappingLocation[$x][$i]) ?
$mappingLocation[$x][$i] : 0;
700 if (isset($mappingName[$x][$i])) {
701 if (is_array($mapperFields[$mappingContactType[$x][$i]])) {
703 if (isset($mappingRelation[$x][$i])) {
705 $contactDetails = strtolower(str_replace(" ", "_", $mappingName[$x][$i]));
706 $relLocationId = isset($mappingLocation[$x][$i]) ?
$mappingLocation[$x][$i] : 0;
707 if (!$relLocationId && in_array($mappingName[$x][$i], $specialFields)) {
708 $relLocationId = " ";
711 $relPhoneType = isset($mappingPhoneType[$x][$i]) ?
$mappingPhoneType[$x][$i] : NULL;
713 $defaults["mapper[$x][$i]"] = array(
714 $mappingContactType[$x][$i],
715 $mappingRelation[$x][$i],
718 $mappingName[$x][$i],
724 $noneArray[] = array($x, $i, 2);
726 if (!$phoneType && !$imProvider) {
727 $noneArray[] = array($x, $i, 3);
729 if (!$mappingName[$x][$i]) {
730 $noneArray[] = array($x, $i, 4);
732 if (!$relLocationId) {
733 $noneArray[] = array($x, $i, 5);
735 if (!$relPhoneType) {
736 $noneArray[] = array($x, $i, 6);
738 $noneArray[] = array($x, $i, 2);
741 $phoneType = isset($mappingPhoneType[$x][$i]) ?
$mappingPhoneType[$x][$i] : NULL;
742 $imProvider = isset($mappingImProvider[$x][$i]) ?
$mappingImProvider[$x][$i] : NULL;
743 if (!$locationId && in_array($mappingName[$x][$i], $specialFields)) {
747 $defaults["mapper[$x][$i]"] = array(
748 $mappingContactType[$x][$i],
749 $mappingName[$x][$i],
753 if (!$mappingName[$x][$i]) {
754 $noneArray[] = array($x, $i, 1);
757 $noneArray[] = array($x, $i, 2);
759 if (!$phoneType && !$imProvider) {
760 $noneArray[] = array($x, $i, 3);
763 $noneArray[] = array($x, $i, 4);
764 $noneArray[] = array($x, $i, 5);
765 $noneArray[] = array($x, $i, 6);
770 if (CRM_Utils_Array
::value($i, CRM_Utils_Array
::value($x, $mappingOperator))) {
771 $defaults["operator[$x][$i]"] = CRM_Utils_Array
::value($i, $mappingOperator[$x]);
774 if (CRM_Utils_Array
::value($i, CRM_Utils_Array
::value($x, $mappingValue))) {
775 $defaults["value[$x][$i]"] = CRM_Utils_Array
::value($i, $mappingValue[$x]);
780 //Fix for Search Builder
781 if ($mappingType == 'Export') {
788 $formValues = $form->exportValues();
790 if (empty($formValues)) {
791 // Incremented length for third select box(relationship type)
792 for ($k = 1; $k < $j; $k++
) {
793 $noneArray[] = array($x, $i, $k);
797 if (!empty($formValues['mapper'][$x])) {
798 foreach ($formValues['mapper'][$x] as $value) {
799 for ($k = 1; $k < $j; $k++
) {
800 if (!isset($formValues['mapper'][$x][$i][$k]) ||
801 (!$formValues['mapper'][$x][$i][$k])
803 $noneArray[] = array($x, $i, $k);
806 $nullArray[] = array($x, $i, $k);
812 for ($k = 1; $k < $j; $k++
) {
813 $noneArray[] = array($x, $i, $k);
818 //Fix for Search Builder
819 if ($mappingType == 'Export') {
820 if (!isset($mappingId) ||
$i >= count(reset($mappingName))) {
821 if (isset($formValues['mapper']) &&
822 isset($formValues['mapper'][$x][$i][1]) &&
823 array_key_exists($formValues['mapper'][$x][$i][1], $relationshipTypes)
825 $sel->setOptions(array($sel1, $sel2, $sel5, $sel6, $sel7, $sel3, $sel4));
828 $sel->setOptions(array($sel1, $sel2, $sel3, $sel4, $sel5, $sel6, $sel7));
832 $sel->setOptions(array($sel1, $sel2, $sel3, $sel4, $sel5, $sel6, $sel7));
836 $sel->setOptions(array($sel1, $sel2, $sel3, $sel4));
839 if ($mappingType == 'Search Builder') {
840 //CRM -2292, restricted array set
841 $operatorArray = array('' => ts('-operator-')) + CRM_Core_SelectValues
::getSearchBuilderOperators();
843 $form->add('select', "operator[$x][$i]", '', $operatorArray);
844 $form->add('text', "value[$x][$i]", '');
847 //end of columnCnt for
848 if ($mappingType == 'Search Builder') {
849 $title = ts('Another search field');
852 $title = ts('Select more fields');
855 $form->addElement('submit', "addMore[$x]", $title, array('class' => 'submit-link'));
859 $js = "<script type='text/javascript'>\n";
860 $formName = "document.{$name}";
861 if (!empty($nullArray)) {
862 $js .= "var nullArray = [";
865 foreach ($nullArray as $element) {
866 $key = "{$element[0]}, {$element[1]}, {$element[2]}";
867 if (!isset($seen[$key])) {
868 $elements[] = "[$key]";
872 $js .= implode(', ', $elements);
875 for (var i=0;i<nullArray.length;i++) {
876 if ( {$formName}['mapper['+nullArray[i][0]+']['+nullArray[i][1]+']['+nullArray[i][2]+']'] ) {
877 {$formName}['mapper['+nullArray[i][0]+']['+nullArray[i][1]+']['+nullArray[i][2]+']'].style.display = '';
882 if (!empty($noneArray)) {
883 $js .= "var noneArray = [";
886 foreach ($noneArray as $element) {
887 $key = "{$element[0]}, {$element[1]}, {$element[2]}";
888 if (!isset($seen[$key])) {
889 $elements[] = "[$key]";
893 $js .= implode(', ', $elements);
896 for (var i=0;i<noneArray.length;i++) {
897 if ( {$formName}['mapper['+noneArray[i][0]+']['+noneArray[i][1]+']['+noneArray[i][2]+']'] ) {
898 {$formName}['mapper['+noneArray[i][0]+']['+noneArray[i][1]+']['+noneArray[i][2]+']'].style.display = 'none';
903 $js .= "</script>\n";
905 $form->assign('initHideBoxes', $js);
906 $form->assign('columnCount', $columnCount);
907 $form->assign('blockCount', $blockCount);
908 $form->setDefaults($defaults);
910 $form->setDefaultAction('refresh');
914 * Function returns all custom fields with group title and
917 * @param int $relationshipTypeId
918 * Related relationship type id.
920 * @return array all custom field titles
922 public function getRelationTypeCustomGroupData($relationshipTypeId) {
924 $customFields = CRM_Core_BAO_CustomField
::getFields('Relationship', NULL, NULL, $relationshipTypeId, NULL, NULL);
925 $groupTitle = array();
926 foreach ($customFields as $krelation => $vrelation) {
927 $groupTitle[$vrelation['label']] = $vrelation['groupTitle'] . '...' . $vrelation['label'];
934 * Function returns all Custom group Names
936 * @param int $customfieldId
939 * @return null|string $customGroupName all custom group names
942 public static function getCustomGroupName($customfieldId) {
943 if ($customFieldId = CRM_Core_BAO_CustomField
::getKeyID($customfieldId)) {
944 $customGroupId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField', $customFieldId, 'custom_group_id');
945 $customGroupName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'title');
947 if (strlen($customGroupName) > 13) {
948 $customGroupName = substr($customGroupName, 0, 10) . '...';
951 return $customGroupName;
956 * Function returns associated array of elements, that will be passed for search
958 * @param array $params
959 * Associated array of submitted values.
961 * Row no of the fields.
964 * @return array $returnFields formatted associated array of elements
967 public static function formattedFields(&$params, $row = FALSE) {
970 if (empty($params) ||
!isset($params['mapper'])) {
974 $types = array('Individual', 'Organization', 'Household');
975 foreach ($params['mapper'] as $key => $value) {
977 foreach ($value as $k => $v) {
978 if (in_array($v[0], $types)) {
979 if ($contactType && $contactType != $v[0]) {
980 CRM_Core_Error
::fatal(ts("Cannot have two clauses with different types: %1, %2",
981 array(1 => $contactType, 2 => $v[0])
984 $contactType = $v[0];
986 if (!empty($v['1'])) {
988 $v2 = CRM_Utils_Array
::value('2', $v);
989 if ($v2 && trim($v2)) {
990 $fldName .= "-{$v[2]}";
993 $v3 = CRM_Utils_Array
::value('3', $v);
994 if ($v3 && trim($v3)) {
995 $fldName .= "-{$v[3]}";
998 $value = $params['value'][$key][$k];
999 if ($fldName == 'group' ||
$fldName == 'tag') {
1000 $value = trim($value);
1001 $value = str_replace('(', '', $value);
1002 $value = str_replace(')', '', $value);
1004 $v = explode(',', $value);
1006 foreach ($v as $i) {
1011 if ($v[0] == 'Contribution' && substr($fldName, 0, 7) != 'custom_'
1012 && substr($fldName, 0, 10) != 'financial_') {
1013 if (substr($fldName, 0, 13) != 'contribution_') {
1014 $fldName = 'contribution_' . $fldName;
1018 // CRM-14563: we store checkbox, multi-select and adv-multi select custom field using separator, hence it
1019 // needs special handling.
1020 if ($cfID = CRM_Core_BAO_CustomField
::getKeyID($v[1])) {
1021 $isCustomField = TRUE;
1022 $customFieldType = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField', $cfID, 'html_type');
1023 $specialHTMLType = array(
1027 'Multi-Select State/Province',
1028 'Multi-Select Country',
1031 // override the operator to handle separator ( note: this might have some performance issues )
1032 if (in_array($customFieldType, $specialHTMLType)) {
1033 // FIX ME: != and few other operators are not handled
1034 $specialOperators = array('=', 'IN', 'LIKE');
1036 if (in_array($params['operator'][$key][$k], $specialOperators)) {
1037 $params['operator'][$key][$k] = 'RLIKE';
1042 // CRM-14983: verify if values are comma separated convert to array
1043 if (!is_array($value) && (strpos($value,',') !== FALSE ||
strstr($value, '(')) && empty($isCustomField) && $params['operator'][$key][$k] == 'IN') {
1044 preg_match('#\((.*?)\)#', $value, $match);
1045 $tmpArray = explode(',', $match[1]);
1046 $value = array_combine(array_values($tmpArray),array_values($tmpArray));
1052 $params['operator'][$key][$k],
1061 $params['operator'][$key][$k],
1080 //add sortByCharacter values
1081 if (isset($params['sortByCharacter'])) {
1085 $params['sortByCharacter'],
1096 * @param array $params
1100 public static function &returnProperties(&$params) {
1102 'contact_type' => 1,
1103 'contact_sub_type' => 1,
1107 if (empty($params) ||
empty($params['mapper'])) {
1111 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
1112 foreach ($params['mapper'] as $key => $value) {
1113 foreach ($value as $k => $v) {
1115 if ($v[1] == 'groups' ||
$v[1] == 'tags') {
1119 if (isset($v[2]) && is_numeric($v[2])) {
1120 if (!array_key_exists('location', $fields)) {
1121 $fields['location'] = array();
1124 // make sure that we have a location fields and a location type for this
1125 $locationName = $locationTypes[$v[2]];
1126 if (!array_key_exists($locationName, $fields['location'])) {
1127 $fields['location'][$locationName] = array();
1128 $fields['location'][$locationName]['location_type'] = $v[2];
1131 if ($v[1] == 'phone' ||
$v[1] == 'email' ||
$v[1] == 'im') {
1132 // phone type handling
1134 $fields['location'][$locationName][$v[1] . "-" . $v[3]] = 1;
1137 $fields['location'][$locationName][$v[1]] = 1;
1141 $fields['location'][$locationName][$v[1]] = 1;
1155 * Save the mapping field info for search builder / export given the formvalues
1157 * @param array $params
1158 * Asscociated array of formvalues.
1159 * @param int $mappingId
1165 public static function saveMappingFields(&$params, $mappingId) {
1166 //delete mapping fields records for exixting mapping
1167 $mappingFields = new CRM_Core_DAO_MappingField();
1168 $mappingFields->mapping_id
= $mappingId;
1169 $mappingFields->delete();
1171 if (empty($params['mapper'])) {
1175 //save record in mapping field table
1176 foreach ($params['mapper'] as $key => $value) {
1178 foreach ($value as $k => $v) {
1180 if (!empty($v['1'])) {
1181 $saveMappingFields = new CRM_Core_DAO_MappingField();
1183 $saveMappingFields->mapping_id
= $mappingId;
1184 $saveMappingFields->name
= CRM_Utils_Array
::value('1', $v);
1185 $saveMappingFields->contact_type
= CRM_Utils_Array
::value('0', $v);
1186 $locationId = CRM_Utils_Array
::value('2', $v);
1187 $saveMappingFields->location_type_id
= is_numeric($locationId) ?
$locationId : NULL;
1189 if ($v[1] == 'phone') {
1190 $saveMappingFields->phone_type_id
= CRM_Utils_Array
::value('3', $v);
1192 elseif ($v[1] == 'im') {
1193 $saveMappingFields->im_provider_id
= CRM_Utils_Array
::value('3', $v);
1196 if (!empty($params['operator'])) {
1197 $saveMappingFields->operator
= CRM_Utils_Array
::value($k, $params['operator'][$key]);
1199 if (!empty($params['value'])) {
1200 $saveMappingFields->value
= CRM_Utils_Array
::value($k, $params['value'][$key]);
1202 // Handle mapping for 'related contact' fields
1203 if (count(explode('_', CRM_Utils_Array
::value('1', $v))) > 2) {
1204 list($id, $first, $second) = explode('_', CRM_Utils_Array
::value('1', $v));
1205 if (($first == 'a' && $second == 'b') ||
($first == 'b' && $second == 'a')) {
1207 if (!empty($v['2'])) {
1208 $saveMappingFields->name
= CRM_Utils_Array
::value('2', $v);
1210 elseif (!empty($v['4'])) {
1211 $saveMappingFields->name
= CRM_Utils_Array
::value('4', $v);
1214 if (is_numeric(CRM_Utils_Array
::value('3', $v))) {
1215 $locationTypeid = CRM_Utils_Array
::value('3', $v);
1217 elseif (is_numeric(CRM_Utils_Array
::value('5', $v))) {
1218 $locationTypeid = CRM_Utils_Array
::value('5', $v);
1221 if (is_numeric(CRM_Utils_Array
::value('4', $v))) {
1222 $phoneTypeid = CRM_Utils_Array
::value('4', $v);
1224 elseif (is_numeric(CRM_Utils_Array
::value('6', $v))) {
1225 $phoneTypeid = CRM_Utils_Array
::value('6', $v);
1228 $saveMappingFields->location_type_id
= is_numeric($locationTypeid) ?
$locationTypeid : NULL;
1229 $saveMappingFields->phone_type_id
= is_numeric($phoneTypeid) ?
$phoneTypeid : NULL;
1230 $saveMappingFields->relationship_type_id
= $id;
1231 $saveMappingFields->relationship_direction
= "{$first}_{$second}";
1235 $saveMappingFields->grouping
= $key;
1236 $saveMappingFields->column_number
= $colCnt;
1237 $saveMappingFields->save();
1239 $locationTypeid = $phoneTypeid = NULL;