Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
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. | | |
13 | | | | |
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. | | |
18 | | | | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | class CRM_Core_BAO_Mapping extends CRM_Core_DAO_Mapping { | |
36 | ||
37 | /** | |
38 | * class constructor | |
39 | */ | |
40 | function __construct() { | |
41 | parent::__construct(); | |
42 | } | |
43 | ||
44 | /** | |
45 | * Takes a bunch of params that are needed to match certain criteria and | |
46 | * retrieves the relevant objects. Typically the valid params are only | |
47 | * contact_id. We'll tweak this function to be more full featured over a period | |
48 | * of time. This is the inverse function of create. It also stores all the retrieved | |
49 | * values in the default array | |
50 | * | |
51 | * @param array $params (reference ) an assoc array of name/value pairs | |
52 | * @param array $defaults (reference ) an assoc array to hold the flattened values | |
53 | * | |
54 | * @return object CRM_Core_DAO_Mapping object on success, otherwise null | |
55 | * @access public | |
56 | * @static | |
57 | */ | |
58 | static function retrieve(&$params, &$defaults) { | |
59 | $mapping = new CRM_Core_DAO_Mapping(); | |
60 | $mapping->copyValues($params); | |
61 | if ($mapping->find(TRUE)) { | |
62 | CRM_Core_DAO::storeValues($mapping, $defaults); | |
63 | return $mapping; | |
64 | } | |
65 | return NULL; | |
66 | } | |
67 | ||
68 | /** | |
69 | * Function to delete the mapping | |
70 | * | |
71 | * @param int $id mapping id | |
72 | * | |
73 | * @return boolean | |
74 | * @access public | |
75 | * @static | |
76 | * | |
77 | */ | |
78 | static function del($id) { | |
79 | // delete from mapping_field table | |
80 | $mappingField = new CRM_Core_DAO_MappingField(); | |
81 | $mappingField->mapping_id = $id; | |
82 | $mappingField->find(); | |
83 | while ($mappingField->fetch()) { | |
84 | $mappingField->delete(); | |
85 | } | |
86 | ||
87 | // delete from mapping table | |
88 | $mapping = new CRM_Core_DAO_Mapping(); | |
89 | $mapping->id = $id; | |
90 | $mapping->delete(); | |
91 | CRM_Core_Session::setStatus(ts('Selected mapping has been deleted successfully.'), ts('Deleted'), 'success'); | |
92 | ||
93 | return TRUE; | |
94 | } | |
95 | ||
96 | /** | |
97 | * takes an associative array and creates a contact object | |
98 | * | |
99 | * The function extract all the params it needs to initialize the create a | |
100 | * contact object. the params array could contain additional unused name/value | |
101 | * pairs | |
102 | * | |
103 | * @param array $params (reference) an assoc array of name/value pairs | |
104 | * | |
105 | * @return object CRM_Core_DAO_Mapper object on success, otherwise null | |
106 | * @access public | |
107 | * @static | |
108 | */ | |
109 | static function add(&$params) { | |
110 | $mapping = new CRM_Core_DAO_Mapping(); | |
111 | $mapping->copyValues($params); | |
112 | $mapping->save(); | |
113 | ||
114 | return $mapping; | |
115 | } | |
116 | ||
117 | /** | |
118 | * function to get the list of mappings | |
119 | * | |
120 | * @params string $mappingTypeId mapping type id | |
121 | * | |
122 | * @return array $mapping array of mapping name | |
123 | * @access public | |
124 | * @static | |
125 | */ | |
126 | static function getMappings($mappingTypeId) { | |
127 | $mapping = array(); | |
128 | $mappingDAO = new CRM_Core_DAO_Mapping(); | |
129 | $mappingDAO->mapping_type_id = $mappingTypeId; | |
130 | $mappingDAO->find(); | |
131 | ||
132 | while ($mappingDAO->fetch()) { | |
133 | $mapping[$mappingDAO->id] = $mappingDAO->name; | |
134 | } | |
135 | ||
136 | return $mapping; | |
137 | } | |
138 | ||
139 | /** | |
140 | * function to get the mapping fields | |
141 | * | |
142 | * @params int $mappingId mapping id | |
143 | * | |
144 | * @return array $mappingFields array of mapping fields | |
145 | * @access public | |
146 | * @static | |
147 | * | |
148 | */ | |
149 | static function getMappingFields($mappingId) { | |
150 | //mapping is to be loaded from database | |
151 | $mapping = new CRM_Core_DAO_MappingField(); | |
152 | $mapping->mapping_id = $mappingId; | |
153 | $mapping->orderBy('column_number'); | |
154 | $mapping->find(); | |
155 | ||
156 | $mappingName = $mappingLocation = $mappingContactType = $mappingPhoneType = array(); | |
157 | $mappingImProvider = $mappingRelation = $mappingOperator = $mappingValue = $mappingWebsiteType = array(); | |
158 | while ($mapping->fetch()) { | |
159 | $mappingName[$mapping->grouping][$mapping->column_number] = $mapping->name; | |
160 | $mappingContactType[$mapping->grouping][$mapping->column_number] = $mapping->contact_type; | |
161 | ||
162 | if (!empty($mapping->location_type_id)) { | |
163 | $mappingLocation[$mapping->grouping][$mapping->column_number] = $mapping->location_type_id; | |
164 | } | |
165 | ||
166 | if (!empty($mapping->phone_type_id)) { | |
167 | $mappingPhoneType[$mapping->grouping][$mapping->column_number] = $mapping->phone_type_id; | |
168 | } | |
169 | ||
170 | // get IM service provider type id from mapping fields | |
171 | if (!empty($mapping->im_provider_id)) { | |
172 | $mappingImProvider[$mapping->grouping][$mapping->column_number] = $mapping->im_provider_id; | |
173 | } | |
174 | ||
175 | if (!empty($mapping->website_type_id)) { | |
176 | $mappingWebsiteType[$mapping->grouping][$mapping->column_number] = $mapping->website_type_id; | |
177 | } | |
178 | ||
179 | if (!empty($mapping->relationship_type_id)) { | |
180 | $mappingRelation[$mapping->grouping][$mapping->column_number] = "{$mapping->relationship_type_id}_{$mapping->relationship_direction}"; | |
181 | } | |
182 | ||
183 | if (!empty($mapping->operator)) { | |
184 | $mappingOperator[$mapping->grouping][$mapping->column_number] = $mapping->operator; | |
185 | } | |
186 | ||
187 | if (!empty($mapping->value)) { | |
188 | $mappingValue[$mapping->grouping][$mapping->column_number] = $mapping->value; | |
189 | } | |
190 | } | |
191 | ||
192 | return array( | |
193 | $mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, | |
194 | $mappingImProvider, $mappingRelation, $mappingOperator, $mappingValue, $mappingWebsiteType, | |
195 | ); | |
196 | } | |
197 | ||
198 | /** | |
199 | *function to check Duplicate Mapping Name | |
200 | * | |
201 | * @params $nameField string mapping Name | |
202 | * | |
203 | * @params $mapTypeId string mapping Type | |
204 | * | |
205 | * @return boolean | |
206 | * | |
207 | */ | |
208 | static function checkMapping($nameField, $mapTypeId) { | |
209 | $mapping = new CRM_Core_DAO_Mapping(); | |
210 | $mapping->name = $nameField; | |
211 | $mapping->mapping_type_id = $mapTypeId; | |
212 | if ($mapping->find(TRUE)) { | |
213 | return TRUE; | |
214 | } | |
215 | else { | |
216 | return FALSE; | |
217 | } | |
218 | } | |
219 | ||
220 | /** | |
221 | * Function returns associated array of elements, that will be passed for search | |
222 | * | |
223 | * @params int $smartGroupId smart group id | |
224 | * | |
225 | * @return $returnFields associated array of elements | |
226 | * | |
227 | * @static | |
228 | * @public | |
229 | */ | |
230 | static function getFormattedFields($smartGroupId) { | |
231 | $returnFields = array(); | |
232 | ||
233 | //get the fields from mapping table | |
234 | $dao = new CRM_Core_DAO_MappingField(); | |
235 | $dao->mapping_id = $smartGroupId; | |
236 | $dao->find(); | |
237 | while ($dao->fetch()) { | |
238 | $fldName = $dao->name; | |
239 | if ($dao->location_type_id) { | |
240 | $fldName .= "-{$dao->location_type_id}"; | |
241 | } | |
242 | if ($dao->phone_type) { | |
243 | $fldName .= "-{$dao->phone_type}"; | |
244 | } | |
245 | $returnFields[$fldName]['value'] = $dao->value; | |
246 | $returnFields[$fldName]['op'] = $dao->operator; | |
247 | $returnFields[$fldName]['grouping'] = $dao->grouping; | |
248 | } | |
249 | return $returnFields; | |
250 | } | |
251 | ||
252 | /** | |
253 | * Function to build the mapping form | |
254 | * | |
255 | * @params object $form form object | |
256 | * @params string $mappingType mapping type (Export/Import/Search Builder) | |
257 | * @params int $mappingId mapping id | |
258 | * @params mixed $columnCount column count is int for and array for search builder | |
259 | * @params int $blockCount block count (no of blocks shown) | |
260 | * | |
261 | * @return none | |
262 | * @access public | |
263 | * @static | |
264 | */ | |
265 | static function buildMappingForm(&$form, $mappingType = 'Export', $mappingId = NULL, $columnNo, $blockCount = 3, $exportMode = NULL) { | |
266 | if ($mappingType == 'Export') { | |
267 | $name = "Map"; | |
268 | $columnCount = array('1' => $columnNo); | |
269 | } | |
270 | elseif ($mappingType == 'Search Builder') { | |
271 | $name = "Builder"; | |
272 | $columnCount = $columnNo; | |
273 | } | |
274 | ||
275 | //get the saved mapping details | |
276 | ||
277 | if ($mappingType == 'Export') { | |
278 | $form->applyFilter('saveMappingName', 'trim'); | |
279 | ||
280 | //to save the current mappings | |
281 | if (!isset($mappingId)) { | |
282 | $saveDetailsName = ts('Save this field mapping'); | |
283 | $form->add('text', 'saveMappingName', ts('Name')); | |
284 | $form->add('text', 'saveMappingDesc', ts('Description')); | |
285 | } | |
286 | else { | |
287 | $form->assign('loadedMapping', $mappingId); | |
288 | ||
289 | $params = array('id' => $mappingId); | |
290 | $temp = array(); | |
291 | $mappingDetails = CRM_Core_BAO_Mapping::retrieve($params, $temp); | |
292 | ||
293 | $form->assign('savedName', $mappingDetails->name); | |
294 | ||
295 | $form->add('hidden', 'mappingId', $mappingId); | |
296 | ||
297 | $form->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL); | |
298 | $saveDetailsName = ts('Save as a new field mapping'); | |
299 | $form->add('text', 'saveMappingName', ts('Name')); | |
300 | $form->add('text', 'saveMappingDesc', ts('Description')); | |
301 | } | |
302 | ||
303 | $form->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, array('onclick' => "showSaveDetails(this)")); | |
304 | $form->addFormRule(array('CRM_Export_Form_Map', 'formRule'), $form->get('mappingTypeId')); | |
305 | } | |
306 | elseif ($mappingType == 'Search Builder') { | |
307 | $form->addElement('submit', 'addBlock', ts('Also include contacts where'), | |
308 | array('class' => 'submit-link') | |
309 | ); | |
310 | } | |
311 | ||
312 | $defaults = array(); | |
313 | $hasLocationTypes = array(); | |
314 | $hasRelationTypes = array(); | |
315 | $fields = array(); | |
316 | ||
317 | if ($mappingType == 'Export') { | |
318 | $required = TRUE; | |
319 | } | |
320 | elseif ($mappingType == 'Search Builder') { | |
321 | $required = FALSE; | |
322 | } | |
323 | ||
324 | $contactType = array('Individual', 'Household', 'Organization'); | |
325 | foreach ($contactType as $value) { | |
326 | $contactFields = CRM_Contact_BAO_Contact::exportableFields($value, FALSE, $required); | |
327 | // exclude the address options disabled in the Address Settings | |
328 | $fields[$value] = CRM_Core_BAO_Address::validateAddressOptions($contactFields); | |
329 | ||
330 | if ($mappingType == 'Export') { | |
331 | $relationships = array(); | |
332 | $relationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, $value); | |
333 | asort($relationshipTypes); | |
334 | ||
335 | foreach ($relationshipTypes as $key => $var) { | |
336 | list($type) = explode('_', $key); | |
337 | ||
338 | $relationships[$key]['title'] = $var; | |
339 | $relationships[$key]['headerPattern'] = '/' . preg_quote($var, '/') . '/'; | |
340 | $relationships[$key]['export'] = TRUE; | |
341 | $relationships[$key]['relationship_type_id'] = $type; | |
342 | $relationships[$key]['related'] = TRUE; | |
343 | $relationships[$key]['hasRelationType'] = 1; | |
344 | } | |
345 | ||
346 | if (!empty($relationships)) { | |
347 | $fields[$value] = array_merge($fields[$value], | |
348 | array('related' => array('title' => ts('- related contact info -'))), | |
349 | $relationships | |
350 | ); | |
351 | } | |
352 | } | |
353 | } | |
354 | ||
355 | //get the current employer for mapping. | |
356 | if ($required) { | |
357 | $fields['Individual']['current_employer']['title'] = ts('Current Employer'); | |
358 | } | |
359 | ||
360 | // add component fields | |
361 | $compArray = array(); | |
362 | ||
363 | //we need to unset groups, tags, notes for component export | |
364 | if ($exportMode != CRM_Export_Form_Select::CONTACT_EXPORT) { | |
365 | foreach (array( | |
366 | 'groups', 'tags', 'notes') as $value) { | |
367 | unset($fields['Individual'][$value]); | |
368 | unset($fields['Household'][$value]); | |
369 | unset($fields['Organization'][$value]); | |
370 | } | |
371 | } | |
372 | ||
373 | if ($mappingType == 'Search Builder') { | |
374 | //build the common contact fields array. | |
375 | $fields['Contact'] = array(); | |
376 | foreach ($fields['Individual'] as $key => $value) { | |
377 | if (CRM_Utils_Array::value($key, $fields['Household']) && | |
378 | CRM_Utils_Array::value($key, $fields['Organization']) | |
379 | ) { | |
380 | $fields['Contact'][$key] = $value; | |
381 | unset($fields['Organization'][$key], | |
382 | $fields['Household'][$key], | |
383 | $fields['Individual'][$key]); | |
384 | } | |
385 | } | |
386 | if (array_key_exists('note', $fields['Contact'])) { | |
387 | $noteTitle = $fields['Contact']['note']['title']; | |
388 | $fields['Contact']['note']['title'] = $noteTitle . ': ' . ts('Body and Subject'); | |
389 | $fields['Contact']['note_body'] = array( 'title' => $noteTitle . ': ' . ts('Body only'), 'name' => 'note_body' ); | |
390 | $fields['Contact']['note_subject'] = array( 'title' => $noteTitle . ': ' . ts('Subject only'), 'name' => 'note_subject' ); | |
391 | } | |
392 | } | |
393 | ||
394 | if (($mappingType == 'Search Builder') || ($exportMode == CRM_Export_Form_Select::CONTRIBUTE_EXPORT)) { | |
395 | if (CRM_Core_Permission::access('CiviContribute')) { | |
396 | $fields['Contribution'] = CRM_Contribute_BAO_Contribution::exportableFields(); | |
397 | unset($fields['Contribution']['contribution_contact_id']); | |
398 | unset($fields['Contribution']['contribution_status_id']); | |
399 | $compArray['Contribution'] = ts('Contribution'); | |
400 | } | |
401 | } | |
402 | ||
403 | if (($mappingType == 'Search Builder') || ($exportMode == CRM_Export_Form_Select::EVENT_EXPORT)) { | |
404 | if (CRM_Core_Permission::access('CiviEvent')) { | |
405 | $fields['Participant'] = CRM_Event_BAO_Participant::exportableFields(); | |
d77aba4b AS |
406 | //get the component payment fields |
407 | if ($exportMode == CRM_Export_Form_Select::EVENT_EXPORT) { | |
408 | require_once 'CRM/Export/BAO/Export.php'; | |
409 | $componentPaymentFields = array(); | |
410 | foreach (CRM_Export_BAO_Export::componentPaymentFields() as $payField => $payTitle) { | |
411 | $componentPaymentFields[$payField] = array('title' => $payTitle); | |
412 | } | |
413 | $fields['Participant'] = array_merge($fields['Participant'], $componentPaymentFields); | |
414 | } | |
415 | ||
6a488035 TO |
416 | unset($fields['Participant']['participant_contact_id']); |
417 | $compArray['Participant'] = ts('Participant'); | |
418 | } | |
419 | } | |
420 | ||
421 | if (($mappingType == 'Search Builder') || ($exportMode == CRM_Export_Form_Select::MEMBER_EXPORT)) { | |
422 | if (CRM_Core_Permission::access('CiviMember')) { | |
423 | $fields['Membership'] = CRM_Member_BAO_Membership::getMembershipFields($exportMode); | |
424 | unset($fields['Membership']['membership_contact_id']); | |
425 | $compArray['Membership'] = ts('Membership'); | |
426 | } | |
427 | } | |
428 | ||
429 | if (($mappingType == 'Search Builder') || ($exportMode == CRM_Export_Form_Select::PLEDGE_EXPORT)) { | |
430 | if (CRM_Core_Permission::access('CiviPledge')) { | |
431 | $fields['Pledge'] = CRM_Pledge_BAO_Pledge::exportableFields(); | |
432 | unset($fields['Pledge']['pledge_contact_id']); | |
433 | $compArray['Pledge'] = ts('Pledge'); | |
434 | } | |
435 | } | |
436 | ||
437 | if (($mappingType == 'Search Builder') || ($exportMode == CRM_Export_Form_Select::CASE_EXPORT)) { | |
438 | if (CRM_Core_Permission::access('CiviCase')) { | |
439 | $fields['Case'] = CRM_Case_BAO_Case::exportableFields(); | |
440 | $compArray['Case'] = ts('Case'); | |
441 | ||
442 | $fields['Activity'] = CRM_Activity_BAO_Activity::exportableFields('Case'); | |
443 | $compArray['Activity'] = ts('Case Activity'); | |
444 | ||
445 | unset($fields['Case']['case_contact_id']); | |
446 | } | |
447 | } | |
448 | if (($mappingType == 'Search Builder') || ($exportMode == CRM_Export_Form_Select::GRANT_EXPORT)) { | |
449 | if (CRM_Core_Permission::access('CiviGrant')) { | |
450 | $fields['Grant'] = CRM_Grant_BAO_Grant::exportableFields(); | |
451 | unset($fields['Grant']['grant_contact_id']); | |
452 | $compArray['Grant'] = ts('Grant'); | |
453 | } | |
454 | } | |
455 | ||
456 | if (($mappingType == 'Search Builder') || ($exportMode == CRM_Export_Form_Select::ACTIVITY_EXPORT)) { | |
457 | $fields['Activity'] = CRM_Activity_BAO_Activity::exportableFields('Activity'); | |
458 | $compArray['Activity'] = ts('Activity'); | |
459 | } | |
460 | ||
461 | //Contact Sub Type For export | |
462 | $contactSubTypes = array(); | |
463 | $subTypes = CRM_Contact_BAO_ContactType::subTypeInfo(); | |
464 | ||
465 | foreach ($subTypes as $subType => $val) { | |
466 | //adding subtype specific relationships CRM-5256 | |
467 | $csRelationships = array(); | |
468 | ||
469 | if ($mappingType == 'Export') { | |
470 | $subTypeRelationshipTypes = | |
471 | CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, $val['parent'], | |
472 | FALSE, 'label', TRUE, $subType); | |
473 | ||
474 | foreach ($subTypeRelationshipTypes as $key => $var) { | |
475 | if (!array_key_exists($key, $fields[$val['parent']])) { | |
476 | list($type) = explode('_', $key); | |
477 | ||
478 | $csRelationships[$key]['title'] = $var; | |
479 | $csRelationships[$key]['headerPattern'] = '/' . preg_quote($var, '/') . '/'; | |
480 | $csRelationships[$key]['export'] = TRUE; | |
481 | $csRelationships[$key]['relationship_type_id'] = $type; | |
482 | $csRelationships[$key]['related'] = TRUE; | |
483 | $csRelationships[$key]['hasRelationType'] = 1; | |
484 | } | |
485 | } | |
486 | } | |
487 | ||
488 | $fields[$subType] = $fields[$val['parent']] + $csRelationships; | |
489 | ||
490 | //custom fields for sub type | |
491 | $subTypeFields = CRM_Core_BAO_CustomField::getFieldsForImport($subType); | |
492 | $fields[$subType] += $subTypeFields; | |
493 | ||
494 | if (!empty($subTypeFields) || !empty($csRelationships)) { | |
495 | $contactSubTypes[$subType] = $val['label']; | |
496 | } | |
497 | } | |
498 | ||
499 | unset($subTypes); | |
500 | ||
501 | foreach ($fields as $key => $value) { | |
502 | ||
503 | foreach ($value as $key1 => $value1) { | |
504 | //CRM-2676, replacing the conflict for same custom field name from different custom group. | |
505 | $customGroupName = self::getCustomGroupName($key1); | |
506 | ||
507 | if ($customGroupName) { | |
508 | $relatedMapperFields[$key][$key1] = $mapperFields[$key][$key1] = $customGroupName . ': ' . $value1['title']; | |
509 | } | |
510 | else { | |
511 | $relatedMapperFields[$key][$key1] = $mapperFields[$key][$key1] = $value1['title']; | |
512 | } | |
513 | if (isset($value1['hasLocationType'])) { | |
514 | $hasLocationTypes[$key][$key1] = $value1['hasLocationType']; | |
515 | } | |
516 | ||
517 | if (isset($value1['hasRelationType'])) { | |
518 | $hasRelationTypes[$key][$key1] = $value1['hasRelationType']; | |
519 | unset($relatedMapperFields[$key][$key1]); | |
520 | } | |
521 | } | |
522 | ||
523 | if (array_key_exists('related', $relatedMapperFields[$key])) { | |
524 | unset($relatedMapperFields[$key]['related']); | |
525 | } | |
526 | } | |
527 | ||
528 | $mapperKeys = array_keys($mapperFields); | |
529 | ||
530 | $locationTypes = CRM_Core_PseudoConstant::locationType(); | |
531 | ||
532 | $defaultLocationType = CRM_Core_BAO_LocationType::getDefault(); | |
533 | ||
534 | /* FIXME: dirty hack to make the default option show up first. This | |
535 | * avoids a mozilla browser bug with defaults on dynamically constructed | |
536 | * selector widgets. */ | |
537 | ||
538 | ||
539 | if ($defaultLocationType) { | |
540 | $defaultLocation = $locationTypes[$defaultLocationType->id]; | |
541 | unset($locationTypes[$defaultLocationType->id]); | |
542 | $locationTypes = array( | |
543 | $defaultLocationType->id => $defaultLocation) + $locationTypes; | |
544 | } | |
545 | ||
546 | $locationTypes = array( | |
547 | ' ' => ts('Primary')) + $locationTypes; | |
548 | ||
549 | // since we need a hierarchical list to display contact types & subtypes, | |
550 | // this is what we going to display in first selector | |
551 | $contactTypes = CRM_Contact_BAO_ContactType::getSelectElements(FALSE, FALSE); | |
552 | if ($mappingType == 'Search Builder') { | |
553 | $contactTypes = array('Contact' => ts('Contacts')) + $contactTypes; | |
554 | } | |
555 | ||
556 | $sel1 = | |
557 | array('' => ts('- select record type -')) + $contactTypes + $compArray; | |
558 | ||
559 | foreach ($sel1 as $key => $sel) { | |
560 | if ($key) { | |
561 | asort($mapperFields[$key]); | |
562 | $sel2[$key] = array('' => ts('- select field -')) + $mapperFields[$key]; | |
563 | } | |
564 | } | |
565 | ||
566 | $sel3[''] = NULL; | |
567 | $sel5[''] = NULL; | |
568 | $phoneTypes = CRM_Core_PseudoConstant::phoneType(); | |
569 | $imProviders = CRM_Core_PseudoConstant::IMProvider(); | |
570 | asort($phoneTypes); | |
571 | ||
572 | foreach ($sel1 as $k => $sel) { | |
573 | if ($k) { | |
574 | foreach ($locationTypes as $key => $value) { | |
575 | if (trim($key) != '') { | |
576 | $sel4[$k]['phone'][$key] = &$phoneTypes; | |
577 | $sel4[$k]['im'][$key] = &$imProviders; | |
578 | } | |
579 | } | |
580 | } | |
581 | } | |
582 | ||
583 | foreach ($sel1 as $k => $sel) { | |
584 | if ($k) { | |
585 | foreach ($mapperFields[$k] as $key => $value) { | |
586 | if (isset($hasLocationTypes[$k][$key])) { | |
587 | $sel3[$k][$key] = $locationTypes; | |
588 | } | |
589 | else { | |
590 | $sel3[$key] = NULL; | |
591 | } | |
592 | } | |
593 | } | |
594 | } | |
595 | ||
596 | //Array for core fields and relationship custom data | |
597 | ||
598 | $relationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, NULL, TRUE); | |
599 | ||
600 | if ($mappingType == 'Export') { | |
601 | foreach ($sel1 as $k => $sel) { | |
602 | if ($k) { | |
603 | foreach ($mapperFields[$k] as $field => $dontCare) { | |
604 | if (isset($hasRelationTypes[$k][$field])) { | |
605 | list($id, $first, $second) = explode('_', $field); | |
606 | // FIX ME: For now let's not expose custom data related to relationship | |
607 | $relationshipCustomFields = array(); | |
608 | //$relationshipCustomFields = self::getRelationTypeCustomGroupData( $id ); | |
609 | //asort( $relationshipCustomFields ) ; | |
610 | ||
611 | $relationshipType = new CRM_Contact_BAO_RelationshipType(); | |
612 | $relationshipType->id = $id; | |
613 | if ($relationshipType->find(TRUE)) { | |
614 | $direction = "contact_sub_type_$second"; | |
615 | if (isset($relationshipType->$direction)) { | |
616 | $relatedFields = array_merge((array)$relatedMapperFields[$relationshipType->$direction], (array)$relationshipCustomFields); | |
617 | } | |
618 | else { | |
619 | $target_type = 'contact_type_' . $second; | |
620 | $relatedFields = array_merge((array)$relatedMapperFields[$relationshipType->$target_type], (array)$relationshipCustomFields); | |
621 | } | |
622 | } | |
623 | $relationshipType->free(); | |
624 | asort($relatedFields); | |
625 | $sel5[$k][$field] = $relatedFields; | |
626 | } | |
627 | } | |
628 | } | |
629 | } | |
630 | ||
631 | //Location Type for relationship fields | |
632 | ||
633 | foreach ($sel5 as $k => $v) { | |
634 | if ($v) { | |
635 | foreach ($v as $rel => $fields) { | |
636 | foreach ($fields as $field => $fieldLabel) { | |
637 | if (isset($hasLocationTypes[$k][$field])) { | |
638 | $sel6[$k][$rel][$field] = $locationTypes; | |
639 | } | |
640 | } | |
641 | } | |
642 | } | |
643 | } | |
644 | ||
645 | //PhoneTypes for relationship fields | |
646 | $sel7[''] = NULL; | |
647 | foreach ($sel6 as $k => $rel) { | |
648 | if ($k) { | |
649 | foreach ($rel as $phonekey => $phonevalue) { | |
650 | foreach ($locationTypes as $locType => $loc) { | |
651 | if (trim($locType) != '') { | |
652 | $sel7[$k][$phonekey]['phone'][$locType] = &$phoneTypes; | |
653 | $sel7[$k][$phonekey]['im'][$locType] = &$imProviders; | |
654 | } | |
655 | } | |
656 | } | |
657 | } | |
658 | } | |
659 | } | |
660 | ||
661 | //special fields that have location, hack for primary location | |
662 | $specialFields = array( | |
663 | 'street_address', 'supplemental_address_1', 'supplemental_address_2', | |
664 | 'city', 'postal_code', 'postal_code_suffix', 'geo_code_1', 'geo_code_2', | |
665 | 'state_province', 'country', 'phone', 'email', 'im', | |
666 | ); | |
667 | ||
668 | if (isset($mappingId)) { | |
669 | $colCnt = 0; | |
670 | ||
671 | list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingImProvider, | |
672 | $mappingRelation, $mappingOperator, $mappingValue | |
673 | ) = CRM_Core_BAO_Mapping::getMappingFields($mappingId); | |
674 | ||
675 | $blkCnt = count($mappingName); | |
676 | if ($blkCnt >= $blockCount) { | |
677 | $blockCount = $blkCnt + 1; | |
678 | } | |
679 | for ($x = 1; $x < $blockCount; $x++) { | |
680 | if (isset($mappingName[$x])) { | |
681 | $colCnt = count($mappingName[$x]); | |
682 | if ($colCnt >= $columnCount[$x]) { | |
683 | $columnCount[$x] = $colCnt; | |
684 | } | |
685 | } | |
686 | } | |
687 | } | |
688 | ||
689 | $form->_blockCount = $blockCount; | |
690 | $form->_columnCount = $columnCount; | |
691 | ||
692 | $form->set('blockCount', $form->_blockCount); | |
693 | $form->set('columnCount', $form->_columnCount); | |
694 | ||
695 | $defaults = $noneArray = $nullArray = array(); | |
696 | ||
697 | //used to warn for mismatch column count or mismatch mapping | |
698 | $warning = 0; | |
699 | for ($x = 1; $x < $blockCount; $x++) { | |
700 | ||
701 | for ($i = 0; $i < $columnCount[$x]; $i++) { | |
702 | ||
703 | $sel = &$form->addElement('hierselect', "mapper[$x][$i]", ts('Mapper for Field %1', array(1 => $i)), NULL); | |
704 | $jsSet = FALSE; | |
705 | ||
706 | if (isset($mappingId)) { | |
707 | $locationId = isset($mappingLocation[$x][$i]) ? $mappingLocation[$x][$i] : 0; | |
708 | if (isset($mappingName[$x][$i])) { | |
709 | if (is_array($mapperFields[$mappingContactType[$x][$i]])) { | |
710 | ||
711 | if (isset($mappingRelation[$x][$i])) { | |
712 | ||
713 | $contactDetails = strtolower(str_replace(" ", "_", $mappingName[$x][$i])); | |
714 | $relLocationId = isset($mappingLocation[$x][$i]) ? $mappingLocation[$x][$i] : 0; | |
715 | if (!$relLocationId && in_array($mappingName[$x][$i], $specialFields)) { | |
716 | $relLocationId = " "; | |
717 | } | |
718 | ||
719 | $relPhoneType = isset($mappingPhoneType[$x][$i]) ? $mappingPhoneType[$x][$i] : NULL; | |
720 | ||
721 | $defaults["mapper[$x][$i]"] = array( | |
722 | $mappingContactType[$x][$i], | |
723 | $mappingRelation[$x][$i], | |
724 | $locationId, | |
725 | $phoneType, | |
726 | $mappingName[$x][$i], | |
727 | $relLocationId, | |
728 | $relPhoneType, | |
729 | ); | |
730 | ||
731 | if (!$locationId) { | |
732 | $noneArray[] = array($x, $i, 2); | |
733 | } | |
734 | if (!$phoneType && !$imProvider) { | |
735 | $noneArray[] = array($x, $i, 3); | |
736 | } | |
737 | if (!$mappingName[$x][$i]) { | |
738 | $noneArray[] = array($x, $i, 4); | |
739 | } | |
740 | if (!$relLocationId) { | |
741 | $noneArray[] = array($x, $i, 5); | |
742 | } | |
743 | if (!$relPhoneType) { | |
744 | $noneArray[] = array($x, $i, 6); | |
745 | } | |
746 | $noneArray[] = array($x, $i, 2); | |
747 | } | |
748 | else { | |
749 | $phoneType = isset($mappingPhoneType[$x][$i]) ? $mappingPhoneType[$x][$i] : NULL; | |
750 | $imProvider = isset($mappingImProvider[$x][$i]) ? $mappingImProvider[$x][$i] : NULL; | |
751 | if (!$locationId && in_array($mappingName[$x][$i], $specialFields)) { | |
752 | $locationId = " "; | |
753 | } | |
754 | ||
755 | $defaults["mapper[$x][$i]"] = array( | |
756 | $mappingContactType[$x][$i], | |
757 | $mappingName[$x][$i], | |
758 | $locationId, | |
759 | $phoneType, | |
760 | ); | |
761 | if (!$mappingName[$x][$i]) { | |
762 | $noneArray[] = array($x, $i, 1); | |
763 | } | |
764 | if (!$locationId) { | |
765 | $noneArray[] = array($x, $i, 2); | |
766 | } | |
767 | if (!$phoneType && !$imProvider) { | |
768 | $noneArray[] = array($x, $i, 3); | |
769 | } | |
770 | ||
771 | $noneArray[] = array($x, $i, 4); | |
772 | $noneArray[] = array($x, $i, 5); | |
773 | $noneArray[] = array($x, $i, 6); | |
774 | } | |
775 | ||
776 | $jsSet = TRUE; | |
777 | ||
778 | if (CRM_Utils_Array::value($i, CRM_Utils_Array::value($x, $mappingOperator))) { | |
779 | $defaults["operator[$x][$i]"] = CRM_Utils_Array::value($i, $mappingOperator[$x]); | |
780 | } | |
781 | ||
782 | if (CRM_Utils_Array::value($i, CRM_Utils_Array::value($x, $mappingValue))) { | |
783 | $defaults["value[$x][$i]"] = CRM_Utils_Array::value($i, $mappingValue[$x]); | |
784 | } | |
785 | } | |
786 | } | |
787 | } | |
788 | //Fix for Search Builder | |
789 | if ($mappingType == 'Export') { | |
790 | $j = 7; | |
791 | } | |
792 | else { | |
793 | $j = 4; | |
794 | } | |
795 | ||
796 | $formValues = $form->exportValues(); | |
797 | if (!$jsSet) { | |
798 | if (empty($formValues)) { | |
799 | // Incremented length for third select box(relationship type) | |
800 | for ($k = 1; $k < $j; $k++) { | |
801 | $noneArray[] = array($x, $i, $k); | |
802 | } | |
803 | } | |
804 | else { | |
805 | if (!empty($formValues['mapper'][$x])) { | |
806 | foreach ($formValues['mapper'][$x] as $value) { | |
807 | for ($k = 1; $k < $j; $k++) { | |
808 | if (!isset($formValues['mapper'][$x][$i][$k]) || | |
809 | (!$formValues['mapper'][$x][$i][$k]) | |
810 | ) { | |
811 | $noneArray[] = array($x, $i, $k); | |
812 | } | |
813 | else { | |
814 | $nullArray[] = array($x, $i, $k); | |
815 | } | |
816 | } | |
817 | } | |
818 | } | |
819 | else { | |
820 | for ($k = 1; $k < $j; $k++) { | |
821 | $noneArray[] = array($x, $i, $k); | |
822 | } | |
823 | } | |
824 | } | |
825 | } | |
826 | //Fix for Search Builder | |
827 | if ($mappingType == 'Export') { | |
828 | if (!isset($mappingId)) { | |
829 | if (isset($formValues['mapper']) && | |
830 | isset($formValues['mapper'][$x][$i][1]) && | |
831 | array_key_exists($formValues['mapper'][$x][$i][1], $relationshipTypes) | |
832 | ) { | |
833 | $sel->setOptions(array($sel1, $sel2, $sel5, $sel6, $sel7, $sel3, $sel4)); | |
834 | } | |
835 | else { | |
836 | $sel->setOptions(array($sel1, $sel2, $sel3, $sel4, $sel5, $sel6, $sel7)); | |
837 | } | |
838 | } | |
839 | else { | |
840 | $sel->setOptions(array($sel1, $sel2, $sel3, $sel4, $sel5, $sel6, $sel7)); | |
841 | } | |
842 | } | |
843 | else { | |
844 | $sel->setOptions(array($sel1, $sel2, $sel3, $sel4)); | |
845 | } | |
846 | ||
847 | if ($mappingType == 'Search Builder') { | |
848 | //CRM -2292, restricted array set | |
849 | $operatorArray = array( | |
850 | '' => ts('-operator-'), | |
851 | '=' => '=', | |
852 | '!=' => '!=', | |
853 | '>' => '>', | |
854 | '<' => '<', | |
855 | '>=' => '>=', | |
856 | '<=' => '<=', | |
857 | 'IN' => 'IN', | |
858 | 'LIKE' => 'LIKE', | |
859 | 'RLIKE' => 'RLIKE', | |
860 | 'IS EMPTY' => 'IS EMPTY', | |
861 | 'IS NOT EMPTY' => 'IS NOT EMPTY', | |
862 | 'IS NULL' => 'IS NULL', | |
863 | 'IS NOT NULL' => 'IS NOT NULL', | |
864 | ); | |
865 | ||
866 | $form->add('select', "operator[$x][$i]", '', $operatorArray); | |
867 | $form->add('text', "value[$x][$i]", ''); | |
868 | } | |
869 | } | |
870 | //end of columnCnt for | |
871 | if ($mappingType == 'Search Builder') { | |
872 | $title = ts('Another search field'); | |
873 | } | |
874 | else { | |
875 | $title = ts('Select more fields'); | |
876 | } | |
877 | ||
878 | $form->addElement('submit', "addMore[$x]", $title, array('class' => 'submit-link')); | |
879 | } | |
880 | //end of block for | |
881 | ||
882 | $js = "<script type='text/javascript'>\n"; | |
883 | $formName = "document.{$name}"; | |
884 | if (!empty($nullArray)) { | |
885 | $js .= "var nullArray = ["; | |
886 | $elements = array(); | |
887 | $seen = array(); | |
888 | foreach ($nullArray as $element) { | |
889 | $key = "{$element[0]}, {$element[1]}, {$element[2]}"; | |
890 | if (!isset($seen[$key])) { | |
891 | $elements[] = "[$key]"; | |
892 | $seen[$key] = 1; | |
893 | } | |
894 | } | |
895 | $js .= implode(', ', $elements); | |
896 | $js .= "]"; | |
897 | $js .= " | |
898 | for(var i=0;i<nullArray.length;i++) { | |
899 | if ( {$formName}['mapper['+nullArray[i][0]+']['+nullArray[i][1]+']['+nullArray[i][2]+']'] ) { | |
900 | {$formName}['mapper['+nullArray[i][0]+']['+nullArray[i][1]+']['+nullArray[i][2]+']'].style.display = ''; | |
901 | } | |
902 | } | |
903 | "; | |
904 | } | |
905 | if (!empty($noneArray)) { | |
906 | $js .= "var noneArray = ["; | |
907 | $elements = array(); | |
908 | $seen = array(); | |
909 | foreach ($noneArray as $element) { | |
910 | $key = "{$element[0]}, {$element[1]}, {$element[2]}"; | |
911 | if (!isset($seen[$key])) { | |
912 | $elements[] = "[$key]"; | |
913 | $seen[$key] = 1; | |
914 | } | |
915 | } | |
916 | $js .= implode(', ', $elements); | |
917 | $js .= "]"; | |
918 | $js .= " | |
919 | for(var i=0;i<noneArray.length;i++) { | |
920 | if ( {$formName}['mapper['+noneArray[i][0]+']['+noneArray[i][1]+']['+noneArray[i][2]+']'] ) { | |
921 | {$formName}['mapper['+noneArray[i][0]+']['+noneArray[i][1]+']['+noneArray[i][2]+']'].style.display = 'none'; | |
922 | } | |
923 | } | |
924 | "; | |
925 | } | |
926 | $js .= "</script>\n"; | |
927 | ||
928 | $form->assign('initHideBoxes', $js); | |
929 | $form->assign('columnCount', $columnCount); | |
930 | $form->assign('blockCount', $blockCount); | |
931 | $form->setDefaults($defaults); | |
932 | ||
933 | $form->setDefaultAction('refresh'); | |
934 | } | |
935 | ||
936 | /* Function returns all custom fields with group title and | |
937 | * field label | |
938 | * @relationshipTypeId related relationship type id | |
939 | * @return $groupTitle all custom field titles | |
940 | */ | |
941 | function getRelationTypeCustomGroupData($relationshipTypeId) { | |
942 | ||
943 | $customFields = CRM_Core_BAO_CustomField::getFields('Relationship', NULL, NULL, $relationshipTypeId, NULL, NULL); | |
944 | $groupTitle = array(); | |
945 | foreach ($customFields as $krelation => $vrelation) { | |
946 | $groupTitle[$vrelation['label']] = $vrelation['groupTitle'] . '...' . $vrelation['label']; | |
947 | } | |
948 | return $groupTitle; | |
949 | } | |
950 | ||
951 | ||
952 | /** | |
953 | * Function returns all Custom group Names | |
954 | * | |
955 | * @param customfieldId related custom field id | |
956 | * @return $customGroupName all custom group names | |
957 | * @static | |
958 | */ | |
959 | static function getCustomGroupName($customfieldId) { | |
960 | if ($customFieldId = CRM_Core_BAO_CustomField::getKeyID($customfieldId)) { | |
961 | $customGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $customFieldId, 'custom_group_id'); | |
962 | $customGroupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'title'); | |
963 | ||
964 | if (strlen($customGroupName) > 13) { | |
965 | $customGroupName = substr($customGroupName, 0, 10) . '...'; | |
966 | } | |
967 | ||
968 | return $customGroupName; | |
969 | } | |
970 | } | |
971 | ||
972 | /** | |
973 | * Function returns associated array of elements, that will be passed for search | |
974 | * | |
975 | * @params array $params associated array of submitted values | |
976 | * @params boolean $row row no of the fields | |
977 | * | |
978 | * @return $returnFields formatted associated array of elements | |
979 | * | |
980 | * @static | |
981 | * @public | |
982 | */ | |
983 | static function &formattedFields(&$params, $row = FALSE) { | |
984 | $fields = array(); | |
985 | ||
986 | if (empty($params) || !isset($params['mapper'])) { | |
987 | return $fields; | |
988 | } | |
989 | ||
990 | $types = array('Individual', 'Organization', 'Household'); | |
991 | foreach ($params['mapper'] as $key => $value) { | |
992 | $contactType = NULL; | |
993 | foreach ($value as $k => $v) { | |
994 | if (in_array($v[0], $types)) { | |
995 | if ($contactType && $contactType != $v[0]) { | |
996 | CRM_Core_Error::fatal(ts("Cannot have two clauses with different types: %1, %2", | |
997 | array(1 => $contactType, 2 => $v[0]) | |
998 | )); | |
999 | } | |
1000 | $contactType = $v[0]; | |
1001 | } | |
1002 | if (CRM_Utils_Array::value('1', $v)) { | |
1003 | $fldName = $v[1]; | |
1004 | $v2 = CRM_Utils_Array::value('2', $v); | |
1005 | if ($v2 && trim($v2)) { | |
1006 | $fldName .= "-{$v[2]}"; | |
1007 | } | |
1008 | ||
1009 | $v3 = CRM_Utils_Array::value('3', $v); | |
1010 | if ($v3 && trim($v3)) { | |
1011 | $fldName .= "-{$v[3]}"; | |
1012 | } | |
1013 | ||
1014 | $value = $params['value'][$key][$k]; | |
1015 | if ($fldName == 'group' || $fldName == 'tag') { | |
1016 | $value = trim($value); | |
1017 | $value = str_replace('(', '', $value); | |
1018 | $value = str_replace(')', '', $value); | |
1019 | ||
1020 | $v = explode(',', $value); | |
1021 | $value = array(); | |
1022 | foreach ($v as $i) { | |
1023 | $value[$i] = 1; | |
1024 | } | |
1025 | } | |
1026 | ||
289c8051 PN |
1027 | if ($v[0] == 'Contribution' && substr($fldName, 0, 7) != 'custom_' |
1028 | && substr($fldName, 0, 10) != 'financial_') { | |
6a488035 TO |
1029 | if (substr($fldName, 0, 13) != 'contribution_') { |
1030 | $fldName = 'contribution_' . $fldName; | |
1031 | } | |
1032 | } | |
1033 | ||
1034 | if ($row) { | |
1035 | $fields[] = array( | |
1036 | $fldName, | |
1037 | $params['operator'][$key][$k], | |
1038 | $value, | |
1039 | $key, | |
1040 | $k, | |
1041 | ); | |
1042 | } | |
1043 | else { | |
1044 | $fields[] = array( | |
1045 | $fldName, | |
1046 | $params['operator'][$key][$k], | |
1047 | $value, | |
1048 | $key, | |
1049 | 0, | |
1050 | ); | |
1051 | } | |
1052 | } | |
1053 | } | |
1054 | if ($contactType) { | |
1055 | $fields[] = array( | |
1056 | 'contact_type', | |
1057 | '=', | |
1058 | $contactType, | |
1059 | $key, | |
1060 | 0, | |
1061 | ); | |
1062 | } | |
1063 | } | |
1064 | ||
1065 | //add sortByCharacter values | |
1066 | if (isset($params['sortByCharacter'])) { | |
1067 | $fields[] = array( | |
1068 | 'sortByCharacter', | |
1069 | '=', | |
1070 | $params['sortByCharacter'], | |
1071 | 0, | |
1072 | 0, | |
1073 | ); | |
1074 | } | |
1075 | ||
1076 | ||
1077 | return $fields; | |
1078 | } | |
1079 | ||
1080 | static function &returnProperties(&$params) { | |
1081 | $fields = array( | |
1082 | 'contact_type' => 1, | |
1083 | 'contact_sub_type' => 1, | |
1084 | 'sort_name' => 1, | |
1085 | ); | |
1086 | ||
1087 | if (empty($params) || empty($params['mapper'])) { | |
1088 | return $fields; | |
1089 | } | |
1090 | ||
1091 | $locationTypes = CRM_Core_PseudoConstant::locationType(); | |
1092 | foreach ($params['mapper'] as $key => $value) { | |
1093 | foreach ($value as $k => $v) { | |
1094 | if (isset($v[1])) { | |
1095 | if ($v[1] == 'groups' || $v[1] == 'tags') { | |
1096 | continue; | |
1097 | } | |
1098 | ||
1099 | if (isset($v[2]) && is_numeric($v[2])) { | |
1100 | if (!array_key_exists('location', $fields)) { | |
1101 | $fields['location'] = array(); | |
1102 | } | |
1103 | ||
1104 | // make sure that we have a location fields and a location type for this | |
1105 | $locationName = $locationTypes[$v[2]]; | |
1106 | if (!array_key_exists($locationName, $fields['location'])) { | |
1107 | $fields['location'][$locationName] = array(); | |
1108 | $fields['location'][$locationName]['location_type'] = $v[2]; | |
1109 | } | |
1110 | ||
1111 | if ($v[1] == 'phone' || $v[1] == 'email' || $v[1] == 'im') { | |
1112 | // phone type handling | |
1113 | if (isset($v[3])) { | |
1114 | $fields['location'][$locationName][$v[1] . "-" . $v[3]] = 1; | |
1115 | } | |
1116 | else { | |
1117 | $fields['location'][$locationName][$v[1]] = 1; | |
1118 | } | |
1119 | } | |
1120 | else { | |
1121 | $fields['location'][$locationName][$v[1]] = 1; | |
1122 | } | |
1123 | } | |
1124 | else { | |
1125 | $fields[$v[1]] = 1; | |
1126 | } | |
1127 | } | |
1128 | } | |
1129 | } | |
1130 | ||
1131 | return $fields; | |
1132 | } | |
1133 | ||
1134 | /** | |
1135 | * save the mapping field info for search builder / export given the formvalues | |
1136 | * | |
1137 | * @param array $params asscociated array of formvalues | |
1138 | * @param int $mappingId mapping id | |
1139 | * | |
1140 | * @return null | |
1141 | * @static | |
1142 | * @access public | |
1143 | */ | |
1144 | static function saveMappingFields(&$params, $mappingId) { | |
1145 | //delete mapping fields records for exixting mapping | |
1146 | $mappingFields = new CRM_Core_DAO_MappingField(); | |
1147 | $mappingFields->mapping_id = $mappingId; | |
1148 | $mappingFields->delete(); | |
1149 | ||
1150 | if (empty($params['mapper'])) { | |
1151 | return; | |
1152 | } | |
1153 | ||
1154 | //save record in mapping field table | |
1155 | foreach ($params['mapper'] as $key => $value) { | |
1156 | $colCnt = 0; | |
1157 | foreach ($value as $k => $v) { | |
1158 | ||
1159 | if (CRM_Utils_Array::value('1', $v)) { | |
1160 | $saveMappingFields = new CRM_Core_DAO_MappingField(); | |
1161 | ||
1162 | $saveMappingFields->mapping_id = $mappingId; | |
1163 | $saveMappingFields->name = CRM_Utils_Array::value('1', $v); | |
1164 | $saveMappingFields->contact_type = CRM_Utils_Array::value('0', $v); | |
1165 | $locationId = CRM_Utils_Array::value('2', $v); | |
1166 | $saveMappingFields->location_type_id = is_numeric($locationId) ? $locationId : NULL; | |
1167 | ||
1168 | if ($v[1] == 'phone') { | |
1169 | $saveMappingFields->phone_type_id = CRM_Utils_Array::value('3', $v); | |
1170 | } | |
1171 | elseif ($v[1] == 'im') { | |
1172 | $saveMappingFields->im_provider_id = CRM_Utils_Array::value('3', $v); | |
1173 | } | |
1174 | ||
1175 | if (CRM_Utils_Array::value('operator', $params)) { | |
1176 | $saveMappingFields->operator = CRM_Utils_Array::value($k, $params['operator'][$key]); | |
1177 | } | |
1178 | if (CRM_Utils_Array::value('value', $params)) { | |
1179 | $saveMappingFields->value = CRM_Utils_Array::value($k, $params['value'][$key]); | |
1180 | } | |
1181 | // Handle mapping for 'related contact' fields | |
1182 | if (count(explode('_', CRM_Utils_Array::value('1', $v))) > 2) { | |
1183 | list($id, $first, $second) = explode('_', CRM_Utils_Array::value('1', $v)); | |
1184 | if (($first == 'a' && $second == 'b') || ($first == 'b' && $second == 'a')) { | |
1185 | ||
1186 | if (CRM_Utils_Array::value('2', $v)) { | |
1187 | $saveMappingFields->name = CRM_Utils_Array::value('2', $v); | |
1188 | } | |
1189 | elseif (CRM_Utils_Array::value('4', $v)) { | |
1190 | $saveMappingFields->name = CRM_Utils_Array::value('4', $v); | |
1191 | } | |
1192 | ||
1193 | if (is_numeric(CRM_Utils_Array::value('3', $v))) { | |
1194 | $locationTypeid = CRM_Utils_Array::value('3', $v); | |
1195 | } | |
1196 | elseif (is_numeric(CRM_Utils_Array::value('5', $v))) { | |
1197 | $locationTypeid = CRM_Utils_Array::value('5', $v); | |
1198 | } | |
1199 | ||
1200 | if (is_numeric(CRM_Utils_Array::value('4', $v))) { | |
1201 | $phoneTypeid = CRM_Utils_Array::value('4', $v); | |
1202 | } | |
1203 | elseif (is_numeric(CRM_Utils_Array::value('6', $v))) { | |
1204 | $phoneTypeid = CRM_Utils_Array::value('6', $v); | |
1205 | } | |
1206 | ||
1207 | $saveMappingFields->location_type_id = is_numeric($locationTypeid) ? $locationTypeid : NULL; | |
1208 | $saveMappingFields->phone_type_id = is_numeric($phoneTypeid) ? $phoneTypeid : NULL; | |
1209 | $saveMappingFields->relationship_type_id = $id; | |
1210 | $saveMappingFields->relationship_direction = "{$first}_{$second}"; | |
1211 | } | |
1212 | } | |
1213 | ||
1214 | $saveMappingFields->grouping = $key; | |
1215 | $saveMappingFields->column_number = $colCnt; | |
1216 | $saveMappingFields->save(); | |
1217 | $colCnt++; | |
1218 | $locationTypeid = $phoneTypeid = NULL; | |
1219 | } | |
1220 | } | |
1221 | } | |
1222 | } | |
1223 | } | |
1224 |