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