3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
33 class CRM_Core_BAO_Mapping
extends CRM_Core_DAO_Mapping
{
38 public function __construct() {
39 parent
::__construct();
43 * Fetch object based on array of properties.
45 * @param array $params
46 * (reference ) an assoc array of name/value pairs.
47 * @param array $defaults
48 * (reference ) an assoc array to hold the flattened values.
51 * CRM_Core_DAO_Mapping object on success, otherwise NULL
53 public static function retrieve(&$params, &$defaults) {
54 $mapping = new CRM_Core_DAO_Mapping();
55 $mapping->copyValues($params);
56 if ($mapping->find(TRUE)) {
57 CRM_Core_DAO
::storeValues($mapping, $defaults);
71 public static function del($id) {
72 // delete from mapping_field table
73 $mappingField = new CRM_Core_DAO_MappingField();
74 $mappingField->mapping_id
= $id;
75 $mappingField->find();
76 while ($mappingField->fetch()) {
77 $mappingField->delete();
80 // delete from mapping table
81 $mapping = new CRM_Core_DAO_Mapping();
83 if ($mapping->find(TRUE)) {
84 $result = $mapping->delete();
91 * Takes an associative array and creates a contact object.
93 * The function extract all the params it needs to initialize the create a
94 * contact object. the params array could contain additional unused name/value
97 * @param array $params
98 * An array of name/value pairs.
101 * CRM_Core_DAO_Mapper object on success, otherwise NULL
103 public static function add($params) {
104 $mapping = new CRM_Core_DAO_Mapping();
105 $mapping->copyValues($params);
112 * Get the list of mappings.
114 * @param string $mappingTypeId
118 * array of mapping name
120 public static function getMappings($mappingTypeId) {
122 $mappingDAO = new CRM_Core_DAO_Mapping();
123 $mappingDAO->mapping_type_id
= $mappingTypeId;
126 while ($mappingDAO->fetch()) {
127 $mapping[$mappingDAO->id
] = $mappingDAO->name
;
134 * Get the mapping fields.
136 * @param int $mappingId
140 * array of mapping fields
142 public static function getMappingFields($mappingId) {
143 //mapping is to be loaded from database
144 $mapping = new CRM_Core_DAO_MappingField();
145 $mapping->mapping_id
= $mappingId;
146 $mapping->orderBy('column_number');
149 $mappingName = $mappingLocation = $mappingContactType = $mappingPhoneType = array();
150 $mappingImProvider = $mappingRelation = $mappingOperator = $mappingValue = $mappingWebsiteType = array();
151 while ($mapping->fetch()) {
152 $mappingName[$mapping->grouping
][$mapping->column_number
] = $mapping->name
;
153 $mappingContactType[$mapping->grouping
][$mapping->column_number
] = $mapping->contact_type
;
155 if (!empty($mapping->location_type_id
)) {
156 $mappingLocation[$mapping->grouping
][$mapping->column_number
] = $mapping->location_type_id
;
159 if (!empty($mapping->phone_type_id
)) {
160 $mappingPhoneType[$mapping->grouping
][$mapping->column_number
] = $mapping->phone_type_id
;
163 // get IM service provider type id from mapping fields
164 if (!empty($mapping->im_provider_id
)) {
165 $mappingImProvider[$mapping->grouping
][$mapping->column_number
] = $mapping->im_provider_id
;
168 if (!empty($mapping->website_type_id
)) {
169 $mappingWebsiteType[$mapping->grouping
][$mapping->column_number
] = $mapping->website_type_id
;
172 if (!empty($mapping->relationship_type_id
)) {
173 $mappingRelation[$mapping->grouping
][$mapping->column_number
] = "{$mapping->relationship_type_id}_{$mapping->relationship_direction}";
176 if (!empty($mapping->operator
)) {
177 $mappingOperator[$mapping->grouping
][$mapping->column_number
] = $mapping->operator
;
180 if (!empty($mapping->value
)) {
181 $mappingValue[$mapping->grouping
][$mapping->column_number
] = $mapping->value
;
199 * Check Duplicate Mapping Name.
201 * @param string $nameField
203 * @param string $mapTypeId
208 public static function checkMapping($nameField, $mapTypeId) {
209 $mapping = new CRM_Core_DAO_Mapping();
210 $mapping->name
= $nameField;
211 $mapping->mapping_type_id
= $mapTypeId;
212 return (bool) $mapping->find(TRUE);
216 * Function returns associated array of elements, that will be passed for search.
218 * @param int $smartGroupId
222 * associated array of elements
224 public static function getFormattedFields($smartGroupId) {
225 $returnFields = array();
227 //get the fields from mapping table
228 $dao = new CRM_Core_DAO_MappingField();
229 $dao->mapping_id
= $smartGroupId;
231 while ($dao->fetch()) {
232 $fldName = $dao->name
;
233 if ($dao->location_type_id
) {
234 $fldName .= "-{$dao->location_type_id}";
236 if ($dao->phone_type
) {
237 $fldName .= "-{$dao->phone_type}";
239 $returnFields[$fldName]['value'] = $dao->value
;
240 $returnFields[$fldName]['op'] = $dao->operator
;
241 $returnFields[$fldName]['grouping'] = $dao->grouping
;
243 return $returnFields;
247 * Build the mapping form.
249 * @param CRM_Core_Form $form
250 * @param string $mappingType
251 * (Export/Search Builder). (Import apparently used to use this but does no longer).
252 * @param int $mappingId
253 * @param int $columnNo
254 * @param int $blockCount
255 * (no of blocks shown).
256 * @param NULL $exportMode
258 public static function buildMappingForm(&$form, $mappingType, $mappingId, $columnNo, $blockCount, $exportMode = NULL) {
260 $hasLocationTypes = array();
261 $hasRelationTypes = array();
264 //get the saved mapping details
266 if ($mappingType == 'Export') {
267 $columnCount = array('1' => $columnNo);
268 $form->applyFilter('saveMappingName', 'trim');
270 //to save the current mappings
271 if (!isset($mappingId)) {
272 $saveDetailsName = ts('Save this field mapping');
273 $form->add('text', 'saveMappingName', ts('Name'));
274 $form->add('text', 'saveMappingDesc', ts('Description'));
277 $form->assign('loadedMapping', $mappingId);
279 $params = array('id' => $mappingId);
281 $mappingDetails = CRM_Core_BAO_Mapping
::retrieve($params, $temp);
283 $form->assign('savedName', $mappingDetails->name
);
285 $form->add('hidden', 'mappingId', $mappingId);
287 $form->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL);
288 $saveDetailsName = ts('Save as a new field mapping');
289 $form->add('text', 'saveMappingName', ts('Name'));
290 $form->add('text', 'saveMappingDesc', ts('Description'));
293 $form->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, array('onclick' => "showSaveDetails(this)"));
294 $form->addFormRule(array('CRM_Export_Form_Map', 'formRule'), $form->get('mappingTypeId'));
296 elseif ($mappingType == 'Search Builder') {
297 $columnCount = $columnNo;
298 $form->addElement('submit', 'addBlock', ts('Also include contacts where'),
299 array('class' => 'submit-link')
303 $contactType = array('Individual', 'Household', 'Organization');
304 foreach ($contactType as $value) {
305 if ($mappingType == 'Search Builder') {
306 // get multiple custom group fields in this context
307 $contactFields = CRM_Contact_BAO_Contact
::exportableFields($value, FALSE, FALSE, FALSE, TRUE);
310 $contactFields = CRM_Contact_BAO_Contact
::exportableFields($value, FALSE, TRUE);
312 $contactFields = array_merge($contactFields, CRM_Contact_BAO_Query_Hook
::singleton()->getFields());
314 // exclude the address options disabled in the Address Settings
315 $fields[$value] = CRM_Core_BAO_Address
::validateAddressOptions($contactFields);
316 ksort($fields[$value]);
317 if ($mappingType == 'Export') {
318 $relationships = array();
319 $relationshipTypes = CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, NULL, NULL, $value);
320 asort($relationshipTypes);
322 foreach ($relationshipTypes as $key => $var) {
323 list($type) = explode('_', $key);
325 $relationships[$key]['title'] = $var;
326 $relationships[$key]['headerPattern'] = '/' . preg_quote($var, '/') . '/';
327 $relationships[$key]['export'] = TRUE;
328 $relationships[$key]['relationship_type_id'] = $type;
329 $relationships[$key]['related'] = TRUE;
330 $relationships[$key]['hasRelationType'] = 1;
333 if (!empty($relationships)) {
334 $fields[$value] = array_merge($fields[$value],
335 array('related' => array('title' => ts('- related contact info -'))),
342 //get the current employer for mapping.
343 if ($mappingType == 'Export') {
344 $fields['Individual']['current_employer_id']['title'] = ts('Current Employer ID');
347 // add component fields
348 $compArray = array();
350 //we need to unset groups, tags, notes for component export
351 if ($exportMode != CRM_Export_Form_Select
::CONTACT_EXPORT
) {
357 unset($fields['Individual'][$value]);
358 unset($fields['Household'][$value]);
359 unset($fields['Organization'][$value]);
363 if ($mappingType == 'Search Builder') {
364 //build the common contact fields array.
365 $fields['Contact'] = array();
366 foreach ($fields['Individual'] as $key => $value) {
367 if (!empty($fields['Household'][$key]) && !empty($fields['Organization'][$key])) {
368 $fields['Contact'][$key] = $value;
369 unset($fields['Organization'][$key],
370 $fields['Household'][$key],
371 $fields['Individual'][$key]);
374 if (array_key_exists('note', $fields['Contact'])) {
375 $noteTitle = $fields['Contact']['note']['title'];
376 $fields['Contact']['note']['title'] = $noteTitle . ': ' . ts('Body and Subject');
377 $fields['Contact']['note_body'] = array('title' => $noteTitle . ': ' . ts('Body Only'), 'name' => 'note_body');
378 $fields['Contact']['note_subject'] = array(
379 'title' => $noteTitle . ': ' . ts('Subject Only'),
380 '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 $compArray['Contribution'] = ts('Contribution');
393 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::EVENT_EXPORT
)) {
394 if (CRM_Core_Permission
::access('CiviEvent')) {
395 $fields['Participant'] = CRM_Event_BAO_Participant
::exportableFields();
396 //get the component payment fields
397 if ($exportMode == CRM_Export_Form_Select
::EVENT_EXPORT
) {
398 $componentPaymentFields = array();
399 foreach (CRM_Export_BAO_Export
::componentPaymentFields() as $payField => $payTitle) {
400 $componentPaymentFields[$payField] = array('title' => $payTitle);
402 $fields['Participant'] = array_merge($fields['Participant'], $componentPaymentFields);
405 $compArray['Participant'] = ts('Participant');
409 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::MEMBER_EXPORT
)) {
410 if (CRM_Core_Permission
::access('CiviMember')) {
411 $fields['Membership'] = CRM_Member_BAO_Membership
::getMembershipFields($exportMode);
412 unset($fields['Membership']['membership_contact_id']);
413 $compArray['Membership'] = ts('Membership');
417 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::PLEDGE_EXPORT
)) {
418 if (CRM_Core_Permission
::access('CiviPledge')) {
419 $fields['Pledge'] = CRM_Pledge_BAO_Pledge
::exportableFields();
420 unset($fields['Pledge']['pledge_contact_id']);
421 $compArray['Pledge'] = ts('Pledge');
425 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::CASE_EXPORT
)) {
426 if (CRM_Core_Permission
::access('CiviCase')) {
427 $fields['Case'] = CRM_Case_BAO_Case
::exportableFields();
428 $compArray['Case'] = ts('Case');
430 $fields['Activity'] = CRM_Activity_BAO_Activity
::exportableFields('Case');
431 $compArray['Activity'] = ts('Case Activity');
433 unset($fields['Case']['case_contact_id']);
436 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::GRANT_EXPORT
)) {
437 if (CRM_Core_Permission
::access('CiviGrant')) {
438 $fields['Grant'] = CRM_Grant_BAO_Grant
::exportableFields();
439 unset($fields['Grant']['grant_contact_id']);
440 if ($mappingType == 'Search Builder') {
441 unset($fields['Grant']['grant_type_id']);
443 $compArray['Grant'] = ts('Grant');
447 if (($mappingType == 'Search Builder') ||
($exportMode == CRM_Export_Form_Select
::ACTIVITY_EXPORT
)) {
448 $fields['Activity'] = CRM_Activity_BAO_Activity
::exportableFields('Activity');
449 $compArray['Activity'] = ts('Activity');
452 //Contact Sub Type For export
453 $contactSubTypes = array();
454 $subTypes = CRM_Contact_BAO_ContactType
::subTypeInfo();
456 foreach ($subTypes as $subType => $val) {
457 //adding subtype specific relationships CRM-5256
458 $csRelationships = array();
460 if ($mappingType == 'Export') {
461 $subTypeRelationshipTypes
462 = CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, NULL, NULL, $val['parent'],
463 FALSE, 'label', TRUE, $subType);
465 foreach ($subTypeRelationshipTypes as $key => $var) {
466 if (!array_key_exists($key, $fields[$val['parent']])) {
467 list($type) = explode('_', $key);
469 $csRelationships[$key]['title'] = $var;
470 $csRelationships[$key]['headerPattern'] = '/' . preg_quote($var, '/') . '/';
471 $csRelationships[$key]['export'] = TRUE;
472 $csRelationships[$key]['relationship_type_id'] = $type;
473 $csRelationships[$key]['related'] = TRUE;
474 $csRelationships[$key]['hasRelationType'] = 1;
479 $fields[$subType] = $fields[$val['parent']] +
$csRelationships;
481 //custom fields for sub type
482 $subTypeFields = CRM_Core_BAO_CustomField
::getFieldsForImport($subType);
483 $fields[$subType] +
= $subTypeFields;
485 if (!empty($subTypeFields) ||
!empty($csRelationships)) {
486 $contactSubTypes[$subType] = $val['label'];
490 foreach ($fields as $key => $value) {
492 foreach ($value as $key1 => $value1) {
493 //CRM-2676, replacing the conflict for same custom field name from different custom group.
494 $customGroupName = self
::getCustomGroupName($key1);
496 if ($customGroupName) {
497 $relatedMapperFields[$key][$key1] = $mapperFields[$key][$key1] = $customGroupName . ': ' . $value1['title'];
500 $relatedMapperFields[$key][$key1] = $mapperFields[$key][$key1] = $value1['title'];
502 if (isset($value1['hasLocationType'])) {
503 $hasLocationTypes[$key][$key1] = $value1['hasLocationType'];
506 if (isset($value1['hasRelationType'])) {
507 $hasRelationTypes[$key][$key1] = $value1['hasRelationType'];
508 unset($relatedMapperFields[$key][$key1]);
512 if (array_key_exists('related', $relatedMapperFields[$key])) {
513 unset($relatedMapperFields[$key]['related']);
517 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
519 $defaultLocationType = CRM_Core_BAO_LocationType
::getDefault();
521 // FIXME: dirty hack to make the default option show up first. This
522 // avoids a mozilla browser bug with defaults on dynamically constructed
524 if ($defaultLocationType) {
525 $defaultLocation = $locationTypes[$defaultLocationType->id
];
526 unset($locationTypes[$defaultLocationType->id
]);
527 $locationTypes = array($defaultLocationType->id
=> $defaultLocation) +
$locationTypes;
530 $locationTypes = array(' ' => ts('Primary')) +
$locationTypes;
532 // since we need a hierarchical list to display contact types & subtypes,
533 // this is what we going to display in first selector
534 $contactTypes = CRM_Contact_BAO_ContactType
::getSelectElements(FALSE, FALSE);
535 if ($mappingType == 'Search Builder') {
536 $contactTypes = array('Contact' => ts('Contacts')) +
$contactTypes;
539 $sel1 = array('' => ts('- select record type -')) +
$contactTypes +
$compArray;
541 foreach ($sel1 as $key => $sel) {
543 // sort everything BUT the contactType which is sorted separately by
544 // an initial commit of CRM-13278 (check ksort above)
545 if (!in_array($key, $contactType)) {
546 asort($mapperFields[$key]);
548 $sel2[$key] = array('' => ts('- select field -')) +
$mapperFields[$key];
554 $phoneTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', 'phone_type_id');
555 $imProviders = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_IM', 'provider_id');
558 foreach ($sel1 as $k => $sel) {
560 foreach ($locationTypes as $key => $value) {
561 if (trim($key) != '') {
562 $sel4[$k]['phone'][$key] = &$phoneTypes;
563 $sel4[$k]['im'][$key] = &$imProviders;
569 foreach ($sel1 as $k => $sel) {
571 foreach ($mapperFields[$k] as $key => $value) {
572 if (isset($hasLocationTypes[$k][$key])) {
573 $sel3[$k][$key] = $locationTypes;
582 // Array for core fields and relationship custom data
583 $relationshipTypes = CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, NULL, NULL, NULL, TRUE);
585 if ($mappingType == 'Export') {
586 foreach ($sel1 as $k => $sel) {
588 foreach ($mapperFields[$k] as $field => $dontCare) {
589 if (isset($hasRelationTypes[$k][$field])) {
590 list($id, $first, $second) = explode('_', $field);
591 // FIX ME: For now let's not expose custom data related to relationship
592 $relationshipCustomFields = array();
593 //$relationshipCustomFields = self::getRelationTypeCustomGroupData( $id );
594 //asort($relationshipCustomFields);
596 $relationshipType = new CRM_Contact_BAO_RelationshipType();
597 $relationshipType->id
= $id;
598 if ($relationshipType->find(TRUE)) {
599 $direction = "contact_sub_type_$second";
600 if (isset($relationshipType->$direction)) {
601 $relatedFields = array_merge((array) $relatedMapperFields[$relationshipType->$direction], (array) $relationshipCustomFields);
604 $target_type = 'contact_type_' . $second;
605 $relatedFields = array_merge((array) $relatedMapperFields[$relationshipType->$target_type], (array) $relationshipCustomFields);
608 $relationshipType->free();
609 asort($relatedFields);
610 $sel5[$k][$field] = $relatedFields;
616 //Location Type for relationship fields
617 foreach ($sel5 as $k => $v) {
619 foreach ($v as $rel => $fields) {
620 foreach ($fields as $field => $fieldLabel) {
621 if (isset($hasLocationTypes[$k][$field])) {
622 $sel6[$k][$rel][$field] = $locationTypes;
629 //PhoneTypes for relationship fields
631 foreach ($sel6 as $k => $rel) {
633 foreach ($rel as $phonekey => $phonevalue) {
634 foreach ($locationTypes as $locType => $loc) {
635 if (trim($locType) != '') {
636 $sel7[$k][$phonekey]['phone'][$locType] = &$phoneTypes;
637 $sel7[$k][$phonekey]['im'][$locType] = &$imProviders;
645 //special fields that have location, hack for primary location
646 $specialFields = array(
648 'supplemental_address_1',
649 'supplemental_address_2',
652 'postal_code_suffix',
662 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 for ($x = 1; $x < $blockCount; $x++
) {
691 for ($i = 0; $i < $columnCount[$x]; $i++
) {
693 $sel = &$form->addElement('hierselect', "mapper[$x][$i]", ts('Mapper for Field %1', array(1 => $i)), NULL);
696 if (isset($mappingId)) {
697 $locationId = isset($mappingLocation[$x][$i]) ?
$mappingLocation[$x][$i] : 0;
698 if (isset($mappingName[$x][$i])) {
699 if (is_array($mapperFields[$mappingContactType[$x][$i]])) {
701 if (isset($mappingRelation[$x][$i])) {
702 $relLocationId = isset($mappingLocation[$x][$i]) ?
$mappingLocation[$x][$i] : 0;
703 if (!$relLocationId && in_array($mappingName[$x][$i], $specialFields)) {
704 $relLocationId = " ";
707 $relPhoneType = isset($mappingPhoneType[$x][$i]) ?
$mappingPhoneType[$x][$i] : NULL;
709 $defaults["mapper[$x][$i]"] = array(
710 $mappingContactType[$x][$i],
711 $mappingRelation[$x][$i],
714 $mappingName[$x][$i],
720 $noneArray[] = array($x, $i, 2);
722 if (!$phoneType && !$imProvider) {
723 $noneArray[] = array($x, $i, 3);
725 if (!$mappingName[$x][$i]) {
726 $noneArray[] = array($x, $i, 4);
728 if (!$relLocationId) {
729 $noneArray[] = array($x, $i, 5);
731 if (!$relPhoneType) {
732 $noneArray[] = array($x, $i, 6);
734 $noneArray[] = array($x, $i, 2);
737 $phoneType = isset($mappingPhoneType[$x][$i]) ?
$mappingPhoneType[$x][$i] : NULL;
738 $imProvider = isset($mappingImProvider[$x][$i]) ?
$mappingImProvider[$x][$i] : NULL;
739 if (!$locationId && in_array($mappingName[$x][$i], $specialFields)) {
743 $defaults["mapper[$x][$i]"] = array(
744 $mappingContactType[$x][$i],
745 $mappingName[$x][$i],
749 if (!$mappingName[$x][$i]) {
750 $noneArray[] = array($x, $i, 1);
753 $noneArray[] = array($x, $i, 2);
755 if (!$phoneType && !$imProvider) {
756 $noneArray[] = array($x, $i, 3);
759 $noneArray[] = array($x, $i, 4);
760 $noneArray[] = array($x, $i, 5);
761 $noneArray[] = array($x, $i, 6);
766 if (CRM_Utils_Array
::value($i, CRM_Utils_Array
::value($x, $mappingOperator))) {
767 $defaults["operator[$x][$i]"] = CRM_Utils_Array
::value($i, $mappingOperator[$x]);
770 if (CRM_Utils_Array
::value($i, CRM_Utils_Array
::value($x, $mappingValue))) {
771 $defaults["value[$x][$i]"] = CRM_Utils_Array
::value($i, $mappingValue[$x]);
776 //Fix for Search Builder
777 if ($mappingType == 'Export') {
784 $formValues = $form->exportValues();
786 if (empty($formValues)) {
787 // Incremented length for third select box(relationship type)
788 for ($k = 1; $k < $j; $k++
) {
789 $noneArray[] = array($x, $i, $k);
793 if (!empty($formValues['mapper'][$x])) {
794 foreach ($formValues['mapper'][$x] as $value) {
795 for ($k = 1; $k < $j; $k++
) {
796 if (!isset($formValues['mapper'][$x][$i][$k]) ||
797 (!$formValues['mapper'][$x][$i][$k])
799 $noneArray[] = array($x, $i, $k);
802 $nullArray[] = array($x, $i, $k);
808 for ($k = 1; $k < $j; $k++
) {
809 $noneArray[] = array($x, $i, $k);
814 //Fix for Search Builder
815 if ($mappingType == 'Export') {
816 if (!isset($mappingId) ||
$i >= count(reset($mappingName))) {
817 if (isset($formValues['mapper']) &&
818 isset($formValues['mapper'][$x][$i][1]) &&
819 array_key_exists($formValues['mapper'][$x][$i][1], $relationshipTypes)
821 $sel->setOptions(array($sel1, $sel2, $sel5, $sel6, $sel7, $sel3, $sel4));
824 $sel->setOptions(array($sel1, $sel2, $sel3, $sel4, $sel5, $sel6, $sel7));
828 $sel->setOptions(array($sel1, $sel2, $sel3, $sel4, $sel5, $sel6, $sel7));
832 $sel->setOptions(array($sel1, $sel2, $sel3, $sel4));
835 if ($mappingType == 'Search Builder') {
836 //CRM -2292, restricted array set
837 $operatorArray = array('' => ts('-operator-')) + CRM_Core_SelectValues
::getSearchBuilderOperators();
839 $form->add('select', "operator[$x][$i]", '', $operatorArray);
840 $form->add('text', "value[$x][$i]", '');
843 //end of columnCnt for
844 if ($mappingType == 'Search Builder') {
845 $title = ts('Another search field');
848 $title = ts('Select more fields');
851 $form->addElement('submit', "addMore[$x]", $title, array('class' => 'submit-link'));
855 $js = "<script type='text/javascript'>\n";
856 $formName = "document." . (($mappingType == 'Export') ?
'Map' : 'Builder');
857 if (!empty($nullArray)) {
858 $js .= "var nullArray = [";
861 foreach ($nullArray as $element) {
862 $key = "{$element[0]}, {$element[1]}, {$element[2]}";
863 if (!isset($seen[$key])) {
864 $elements[] = "[$key]";
868 $js .= implode(', ', $elements);
871 for (var i=0;i<nullArray.length;i++) {
872 if ( {$formName}['mapper['+nullArray[i][0]+']['+nullArray[i][1]+']['+nullArray[i][2]+']'] ) {
873 {$formName}['mapper['+nullArray[i][0]+']['+nullArray[i][1]+']['+nullArray[i][2]+']'].style.display = '';
878 if (!empty($noneArray)) {
879 $js .= "var noneArray = [";
882 foreach ($noneArray as $element) {
883 $key = "{$element[0]}, {$element[1]}, {$element[2]}";
884 if (!isset($seen[$key])) {
885 $elements[] = "[$key]";
889 $js .= implode(', ', $elements);
892 for (var i=0;i<noneArray.length;i++) {
893 if ( {$formName}['mapper['+noneArray[i][0]+']['+noneArray[i][1]+']['+noneArray[i][2]+']'] ) {
894 {$formName}['mapper['+noneArray[i][0]+']['+noneArray[i][1]+']['+noneArray[i][2]+']'].style.display = 'none';
899 $js .= "</script>\n";
901 $form->assign('initHideBoxes', $js);
902 $form->assign('columnCount', $columnCount);
903 $form->assign('blockCount', $blockCount);
904 $form->setDefaults($defaults);
906 $form->setDefaultAction('refresh');
910 * Function returns all custom fields with group title and
913 * @param int $relationshipTypeId
914 * Related relationship type id.
917 * all custom field titles
919 public function getRelationTypeCustomGroupData($relationshipTypeId) {
921 $customFields = CRM_Core_BAO_CustomField
::getFields('Relationship', NULL, NULL, $relationshipTypeId, NULL, NULL);
922 $groupTitle = array();
923 foreach ($customFields as $krelation => $vrelation) {
924 $groupTitle[$vrelation['label']] = $vrelation['groupTitle'] . '...' . $vrelation['label'];
931 * Function returns all Custom group Names.
933 * @param int $customfieldId
936 * @return null|string
937 * $customGroupName all custom group names
939 public static function getCustomGroupName($customfieldId) {
940 if ($customFieldId = CRM_Core_BAO_CustomField
::getKeyID($customfieldId)) {
941 $customGroupId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField', $customFieldId, 'custom_group_id');
942 $customGroupName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'title');
944 if (strlen($customGroupName) > 13) {
945 $customGroupName = substr($customGroupName, 0, 10) . '...';
948 return $customGroupName;
953 * Function returns associated array of elements, that will be passed for search
955 * @param array $params
956 * Associated array of submitted values.
958 * Row no of the fields.
962 * formatted associated array of elements
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];
997 if ($v[0] == 'Contribution' && substr($fldName, 0, 7) != 'custom_'
998 && substr($fldName, 0, 10) != 'financial_'
999 && substr($fldName, 0, 8) != 'payment_') {
1000 if (substr($fldName, 0, 13) != 'contribution_') {
1001 $fldName = 'contribution_' . $fldName;
1005 // CRM-14983: verify if values are comma separated convert to array
1006 if (!is_array($value) && strstr($params['operator'][$key][$k], 'IN')) {
1007 $value = explode(',', $value);
1008 $value = array($params['operator'][$key][$k] => $value);
1014 $params['operator'][$key][$k],
1023 $params['operator'][$key][$k],
1042 //add sortByCharacter values
1043 if (isset($params['sortByCharacter'])) {
1047 $params['sortByCharacter'],
1056 * @param array $params
1060 public static function &returnProperties(&$params) {
1062 'contact_type' => 1,
1063 'contact_sub_type' => 1,
1067 if (empty($params) ||
empty($params['mapper'])) {
1071 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
1072 foreach ($params['mapper'] as $key => $value) {
1073 foreach ($value as $k => $v) {
1075 if ($v[1] == 'groups' ||
$v[1] == 'tags') {
1079 if (isset($v[2]) && is_numeric($v[2])) {
1080 if (!array_key_exists('location', $fields)) {
1081 $fields['location'] = array();
1084 // make sure that we have a location fields and a location type for this
1085 $locationName = $locationTypes[$v[2]];
1086 if (!array_key_exists($locationName, $fields['location'])) {
1087 $fields['location'][$locationName] = array();
1088 $fields['location'][$locationName]['location_type'] = $v[2];
1091 if ($v[1] == 'phone' ||
$v[1] == 'email' ||
$v[1] == 'im') {
1092 // phone type handling
1094 $fields['location'][$locationName][$v[1] . "-" . $v[3]] = 1;
1097 $fields['location'][$locationName][$v[1]] = 1;
1101 $fields['location'][$locationName][$v[1]] = 1;
1115 * Save the mapping field info for search builder / export given the formvalues
1117 * @param array $params
1118 * Asscociated array of formvalues.
1119 * @param int $mappingId
1124 public static function saveMappingFields(&$params, $mappingId) {
1125 //delete mapping fields records for exixting mapping
1126 $mappingFields = new CRM_Core_DAO_MappingField();
1127 $mappingFields->mapping_id
= $mappingId;
1128 $mappingFields->delete();
1130 if (empty($params['mapper'])) {
1134 //save record in mapping field table
1135 foreach ($params['mapper'] as $key => $value) {
1137 foreach ($value as $k => $v) {
1139 if (!empty($v['1'])) {
1140 $saveMappingFields = new CRM_Core_DAO_MappingField();
1142 $saveMappingFields->mapping_id
= $mappingId;
1143 $saveMappingFields->name
= CRM_Utils_Array
::value('1', $v);
1144 $saveMappingFields->contact_type
= CRM_Utils_Array
::value('0', $v);
1145 $locationId = CRM_Utils_Array
::value('2', $v);
1146 $saveMappingFields->location_type_id
= is_numeric($locationId) ?
$locationId : NULL;
1148 if ($v[1] == 'phone') {
1149 $saveMappingFields->phone_type_id
= CRM_Utils_Array
::value('3', $v);
1151 elseif ($v[1] == 'im') {
1152 $saveMappingFields->im_provider_id
= CRM_Utils_Array
::value('3', $v);
1155 if (!empty($params['operator'])) {
1156 $saveMappingFields->operator
= CRM_Utils_Array
::value($k, $params['operator'][$key]);
1158 if (!empty($params['value'])) {
1159 $saveMappingFields->value
= CRM_Utils_Array
::value($k, $params['value'][$key]);
1161 // Handle mapping for 'related contact' fields
1162 if (count(explode('_', CRM_Utils_Array
::value('1', $v))) > 2) {
1163 list($id, $first, $second) = explode('_', CRM_Utils_Array
::value('1', $v));
1164 if (($first == 'a' && $second == 'b') ||
($first == 'b' && $second == 'a')) {
1166 if (!empty($v['2'])) {
1167 $saveMappingFields->name
= CRM_Utils_Array
::value('2', $v);
1169 elseif (!empty($v['4'])) {
1170 $saveMappingFields->name
= CRM_Utils_Array
::value('4', $v);
1173 if (is_numeric(CRM_Utils_Array
::value('3', $v))) {
1174 $locationTypeid = CRM_Utils_Array
::value('3', $v);
1176 elseif (is_numeric(CRM_Utils_Array
::value('5', $v))) {
1177 $locationTypeid = CRM_Utils_Array
::value('5', $v);
1180 if (is_numeric(CRM_Utils_Array
::value('4', $v))) {
1181 $phoneTypeid = CRM_Utils_Array
::value('4', $v);
1183 elseif (is_numeric(CRM_Utils_Array
::value('6', $v))) {
1184 $phoneTypeid = CRM_Utils_Array
::value('6', $v);
1187 $saveMappingFields->location_type_id
= is_numeric($locationTypeid) ?
$locationTypeid : NULL;
1188 $saveMappingFields->phone_type_id
= is_numeric($phoneTypeid) ?
$phoneTypeid : NULL;
1189 $saveMappingFields->relationship_type_id
= $id;
1190 $saveMappingFields->relationship_direction
= "{$first}_{$second}";
1194 $saveMappingFields->grouping
= $key;
1195 $saveMappingFields->column_number
= $colCnt;
1196 $saveMappingFields->save();
1198 $locationTypeid = $phoneTypeid = NULL;