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); | |
eb1e3589 DS |
327 | $contactFields = array_merge($contactFields, CRM_Contact_BAO_Query_Hook::singleton()->getFields()); |
328 | ||
6a488035 TO |
329 | // exclude the address options disabled in the Address Settings |
330 | $fields[$value] = CRM_Core_BAO_Address::validateAddressOptions($contactFields); | |
331 | ||
332 | if ($mappingType == 'Export') { | |
333 | $relationships = array(); | |
334 | $relationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, $value); | |
335 | asort($relationshipTypes); | |
336 | ||
337 | foreach ($relationshipTypes as $key => $var) { | |
338 | list($type) = explode('_', $key); | |
339 | ||
340 | $relationships[$key]['title'] = $var; | |
341 | $relationships[$key]['headerPattern'] = '/' . preg_quote($var, '/') . '/'; | |
342 | $relationships[$key]['export'] = TRUE; | |
343 | $relationships[$key]['relationship_type_id'] = $type; | |
344 | $relationships[$key]['related'] = TRUE; | |
345 | $relationships[$key]['hasRelationType'] = 1; | |
346 | } | |
347 | ||
348 | if (!empty($relationships)) { | |
349 | $fields[$value] = array_merge($fields[$value], | |
350 | array('related' => array('title' => ts('- related contact info -'))), | |
351 | $relationships | |
352 | ); | |
353 | } | |
354 | } | |
355 | } | |
356 | ||
357 | //get the current employer for mapping. | |
358 | if ($required) { | |
359 | $fields['Individual']['current_employer']['title'] = ts('Current Employer'); | |
360 | } | |
361 | ||
362 | // add component fields | |
363 | $compArray = array(); | |
364 | ||
365 | //we need to unset groups, tags, notes for component export | |
366 | if ($exportMode != CRM_Export_Form_Select::CONTACT_EXPORT) { | |
367 | foreach (array( | |
368 | 'groups', 'tags', 'notes') as $value) { | |
369 | unset($fields['Individual'][$value]); | |
370 | unset($fields['Household'][$value]); | |
371 | unset($fields['Organization'][$value]); | |
372 | } | |
373 | } | |
374 | ||
375 | if ($mappingType == 'Search Builder') { | |
376 | //build the common contact fields array. | |
377 | $fields['Contact'] = array(); | |
378 | foreach ($fields['Individual'] as $key => $value) { | |
379 | if (CRM_Utils_Array::value($key, $fields['Household']) && | |
380 | CRM_Utils_Array::value($key, $fields['Organization']) | |
381 | ) { | |
382 | $fields['Contact'][$key] = $value; | |
383 | unset($fields['Organization'][$key], | |
384 | $fields['Household'][$key], | |
385 | $fields['Individual'][$key]); | |
386 | } | |
387 | } | |
388 | if (array_key_exists('note', $fields['Contact'])) { | |
389 | $noteTitle = $fields['Contact']['note']['title']; | |
390 | $fields['Contact']['note']['title'] = $noteTitle . ': ' . ts('Body and Subject'); | |
391 | $fields['Contact']['note_body'] = array( 'title' => $noteTitle . ': ' . ts('Body only'), 'name' => 'note_body' ); | |
392 | $fields['Contact']['note_subject'] = array( 'title' => $noteTitle . ': ' . ts('Subject only'), 'name' => 'note_subject' ); | |
393 | } | |
394 | } | |
395 | ||
396 | if (($mappingType == 'Search Builder') || ($exportMode == CRM_Export_Form_Select::CONTRIBUTE_EXPORT)) { | |
397 | if (CRM_Core_Permission::access('CiviContribute')) { | |
398 | $fields['Contribution'] = CRM_Contribute_BAO_Contribution::exportableFields(); | |
399 | unset($fields['Contribution']['contribution_contact_id']); | |
400 | unset($fields['Contribution']['contribution_status_id']); | |
401 | $compArray['Contribution'] = ts('Contribution'); | |
402 | } | |
403 | } | |
404 | ||
405 | if (($mappingType == 'Search Builder') || ($exportMode == CRM_Export_Form_Select::EVENT_EXPORT)) { | |
406 | if (CRM_Core_Permission::access('CiviEvent')) { | |
407 | $fields['Participant'] = CRM_Event_BAO_Participant::exportableFields(); | |
d77aba4b AS |
408 | //get the component payment fields |
409 | if ($exportMode == CRM_Export_Form_Select::EVENT_EXPORT) { | |
410 | require_once 'CRM/Export/BAO/Export.php'; | |
411 | $componentPaymentFields = array(); | |
412 | foreach (CRM_Export_BAO_Export::componentPaymentFields() as $payField => $payTitle) { | |
413 | $componentPaymentFields[$payField] = array('title' => $payTitle); | |
414 | } | |
415 | $fields['Participant'] = array_merge($fields['Participant'], $componentPaymentFields); | |
416 | } | |
417 | ||
6a488035 TO |
418 | unset($fields['Participant']['participant_contact_id']); |
419 | $compArray['Participant'] = ts('Participant'); | |
420 | } | |
421 | } | |
422 | ||
423 | if (($mappingType == 'Search Builder') || ($exportMode == CRM_Export_Form_Select::MEMBER_EXPORT)) { | |
424 | if (CRM_Core_Permission::access('CiviMember')) { | |
425 | $fields['Membership'] = CRM_Member_BAO_Membership::getMembershipFields($exportMode); | |
426 | unset($fields['Membership']['membership_contact_id']); | |
427 | $compArray['Membership'] = ts('Membership'); | |
428 | } | |
429 | } | |
430 | ||
431 | if (($mappingType == 'Search Builder') || ($exportMode == CRM_Export_Form_Select::PLEDGE_EXPORT)) { | |
432 | if (CRM_Core_Permission::access('CiviPledge')) { | |
433 | $fields['Pledge'] = CRM_Pledge_BAO_Pledge::exportableFields(); | |
434 | unset($fields['Pledge']['pledge_contact_id']); | |
435 | $compArray['Pledge'] = ts('Pledge'); | |
436 | } | |
437 | } | |
438 | ||
439 | if (($mappingType == 'Search Builder') || ($exportMode == CRM_Export_Form_Select::CASE_EXPORT)) { | |
440 | if (CRM_Core_Permission::access('CiviCase')) { | |
441 | $fields['Case'] = CRM_Case_BAO_Case::exportableFields(); | |
442 | $compArray['Case'] = ts('Case'); | |
443 | ||
444 | $fields['Activity'] = CRM_Activity_BAO_Activity::exportableFields('Case'); | |
445 | $compArray['Activity'] = ts('Case Activity'); | |
446 | ||
447 | unset($fields['Case']['case_contact_id']); | |
448 | } | |
449 | } | |
450 | if (($mappingType == 'Search Builder') || ($exportMode == CRM_Export_Form_Select::GRANT_EXPORT)) { | |
451 | if (CRM_Core_Permission::access('CiviGrant')) { | |
452 | $fields['Grant'] = CRM_Grant_BAO_Grant::exportableFields(); | |
453 | unset($fields['Grant']['grant_contact_id']); | |
454 | $compArray['Grant'] = ts('Grant'); | |
455 | } | |
456 | } | |
457 | ||
458 | if (($mappingType == 'Search Builder') || ($exportMode == CRM_Export_Form_Select::ACTIVITY_EXPORT)) { | |
459 | $fields['Activity'] = CRM_Activity_BAO_Activity::exportableFields('Activity'); | |
460 | $compArray['Activity'] = ts('Activity'); | |
461 | } | |
462 | ||
463 | //Contact Sub Type For export | |
464 | $contactSubTypes = array(); | |
465 | $subTypes = CRM_Contact_BAO_ContactType::subTypeInfo(); | |
466 | ||
467 | foreach ($subTypes as $subType => $val) { | |
468 | //adding subtype specific relationships CRM-5256 | |
469 | $csRelationships = array(); | |
470 | ||
471 | if ($mappingType == 'Export') { | |
8ef12e64 | 472 | $subTypeRelationshipTypes = |
6a488035 TO |
473 | CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, $val['parent'], |
474 | FALSE, 'label', TRUE, $subType); | |
8ef12e64 | 475 | |
6a488035 TO |
476 | foreach ($subTypeRelationshipTypes as $key => $var) { |
477 | if (!array_key_exists($key, $fields[$val['parent']])) { | |
478 | list($type) = explode('_', $key); | |
8ef12e64 | 479 | |
6a488035 TO |
480 | $csRelationships[$key]['title'] = $var; |
481 | $csRelationships[$key]['headerPattern'] = '/' . preg_quote($var, '/') . '/'; | |
482 | $csRelationships[$key]['export'] = TRUE; | |
483 | $csRelationships[$key]['relationship_type_id'] = $type; | |
484 | $csRelationships[$key]['related'] = TRUE; | |
485 | $csRelationships[$key]['hasRelationType'] = 1; | |
486 | } | |
487 | } | |
488 | } | |
489 | ||
490 | $fields[$subType] = $fields[$val['parent']] + $csRelationships; | |
491 | ||
492 | //custom fields for sub type | |
493 | $subTypeFields = CRM_Core_BAO_CustomField::getFieldsForImport($subType); | |
494 | $fields[$subType] += $subTypeFields; | |
495 | ||
496 | if (!empty($subTypeFields) || !empty($csRelationships)) { | |
497 | $contactSubTypes[$subType] = $val['label']; | |
498 | } | |
499 | } | |
500 | ||
501 | unset($subTypes); | |
502 | ||
503 | foreach ($fields as $key => $value) { | |
504 | ||
505 | foreach ($value as $key1 => $value1) { | |
506 | //CRM-2676, replacing the conflict for same custom field name from different custom group. | |
507 | $customGroupName = self::getCustomGroupName($key1); | |
508 | ||
509 | if ($customGroupName) { | |
510 | $relatedMapperFields[$key][$key1] = $mapperFields[$key][$key1] = $customGroupName . ': ' . $value1['title']; | |
511 | } | |
512 | else { | |
513 | $relatedMapperFields[$key][$key1] = $mapperFields[$key][$key1] = $value1['title']; | |
514 | } | |
515 | if (isset($value1['hasLocationType'])) { | |
516 | $hasLocationTypes[$key][$key1] = $value1['hasLocationType']; | |
517 | } | |
518 | ||
519 | if (isset($value1['hasRelationType'])) { | |
520 | $hasRelationTypes[$key][$key1] = $value1['hasRelationType']; | |
521 | unset($relatedMapperFields[$key][$key1]); | |
522 | } | |
523 | } | |
524 | ||
525 | if (array_key_exists('related', $relatedMapperFields[$key])) { | |
526 | unset($relatedMapperFields[$key]['related']); | |
527 | } | |
528 | } | |
529 | ||
530 | $mapperKeys = array_keys($mapperFields); | |
531 | ||
b2b0530a | 532 | $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); |
6a488035 TO |
533 | |
534 | $defaultLocationType = CRM_Core_BAO_LocationType::getDefault(); | |
535 | ||
536 | /* FIXME: dirty hack to make the default option show up first. This | |
537 | * avoids a mozilla browser bug with defaults on dynamically constructed | |
538 | * selector widgets. */ | |
539 | ||
540 | ||
541 | if ($defaultLocationType) { | |
542 | $defaultLocation = $locationTypes[$defaultLocationType->id]; | |
543 | unset($locationTypes[$defaultLocationType->id]); | |
544 | $locationTypes = array( | |
545 | $defaultLocationType->id => $defaultLocation) + $locationTypes; | |
546 | } | |
547 | ||
548 | $locationTypes = array( | |
549 | ' ' => ts('Primary')) + $locationTypes; | |
550 | ||
551 | // since we need a hierarchical list to display contact types & subtypes, | |
552 | // this is what we going to display in first selector | |
553 | $contactTypes = CRM_Contact_BAO_ContactType::getSelectElements(FALSE, FALSE); | |
554 | if ($mappingType == 'Search Builder') { | |
555 | $contactTypes = array('Contact' => ts('Contacts')) + $contactTypes; | |
556 | } | |
557 | ||
558 | $sel1 = | |
559 | array('' => ts('- select record type -')) + $contactTypes + $compArray; | |
560 | ||
561 | foreach ($sel1 as $key => $sel) { | |
562 | if ($key) { | |
563 | asort($mapperFields[$key]); | |
564 | $sel2[$key] = array('' => ts('- select field -')) + $mapperFields[$key]; | |
565 | } | |
566 | } | |
567 | ||
568 | $sel3[''] = NULL; | |
569 | $sel5[''] = NULL; | |
b4f964d9 | 570 | $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'); |
e7e657f0 | 571 | $imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id'); |
6a488035 TO |
572 | asort($phoneTypes); |
573 | ||
574 | foreach ($sel1 as $k => $sel) { | |
575 | if ($k) { | |
576 | foreach ($locationTypes as $key => $value) { | |
577 | if (trim($key) != '') { | |
578 | $sel4[$k]['phone'][$key] = &$phoneTypes; | |
579 | $sel4[$k]['im'][$key] = &$imProviders; | |
580 | } | |
581 | } | |
582 | } | |
583 | } | |
584 | ||
585 | foreach ($sel1 as $k => $sel) { | |
586 | if ($k) { | |
587 | foreach ($mapperFields[$k] as $key => $value) { | |
588 | if (isset($hasLocationTypes[$k][$key])) { | |
589 | $sel3[$k][$key] = $locationTypes; | |
590 | } | |
591 | else { | |
592 | $sel3[$key] = NULL; | |
593 | } | |
594 | } | |
595 | } | |
596 | } | |
597 | ||
598 | //Array for core fields and relationship custom data | |
599 | ||
600 | $relationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, NULL, TRUE); | |
601 | ||
602 | if ($mappingType == 'Export') { | |
603 | foreach ($sel1 as $k => $sel) { | |
604 | if ($k) { | |
605 | foreach ($mapperFields[$k] as $field => $dontCare) { | |
606 | if (isset($hasRelationTypes[$k][$field])) { | |
607 | list($id, $first, $second) = explode('_', $field); | |
608 | // FIX ME: For now let's not expose custom data related to relationship | |
609 | $relationshipCustomFields = array(); | |
610 | //$relationshipCustomFields = self::getRelationTypeCustomGroupData( $id ); | |
611 | //asort( $relationshipCustomFields ) ; | |
612 | ||
613 | $relationshipType = new CRM_Contact_BAO_RelationshipType(); | |
614 | $relationshipType->id = $id; | |
615 | if ($relationshipType->find(TRUE)) { | |
616 | $direction = "contact_sub_type_$second"; | |
617 | if (isset($relationshipType->$direction)) { | |
618 | $relatedFields = array_merge((array)$relatedMapperFields[$relationshipType->$direction], (array)$relationshipCustomFields); | |
619 | } | |
620 | else { | |
621 | $target_type = 'contact_type_' . $second; | |
622 | $relatedFields = array_merge((array)$relatedMapperFields[$relationshipType->$target_type], (array)$relationshipCustomFields); | |
623 | } | |
624 | } | |
625 | $relationshipType->free(); | |
626 | asort($relatedFields); | |
627 | $sel5[$k][$field] = $relatedFields; | |
628 | } | |
629 | } | |
630 | } | |
631 | } | |
632 | ||
633 | //Location Type for relationship fields | |
634 | ||
635 | foreach ($sel5 as $k => $v) { | |
636 | if ($v) { | |
637 | foreach ($v as $rel => $fields) { | |
638 | foreach ($fields as $field => $fieldLabel) { | |
639 | if (isset($hasLocationTypes[$k][$field])) { | |
640 | $sel6[$k][$rel][$field] = $locationTypes; | |
641 | } | |
642 | } | |
643 | } | |
644 | } | |
645 | } | |
646 | ||
647 | //PhoneTypes for relationship fields | |
648 | $sel7[''] = NULL; | |
649 | foreach ($sel6 as $k => $rel) { | |
650 | if ($k) { | |
651 | foreach ($rel as $phonekey => $phonevalue) { | |
652 | foreach ($locationTypes as $locType => $loc) { | |
653 | if (trim($locType) != '') { | |
654 | $sel7[$k][$phonekey]['phone'][$locType] = &$phoneTypes; | |
655 | $sel7[$k][$phonekey]['im'][$locType] = &$imProviders; | |
656 | } | |
657 | } | |
658 | } | |
659 | } | |
660 | } | |
661 | } | |
662 | ||
663 | //special fields that have location, hack for primary location | |
664 | $specialFields = array( | |
665 | 'street_address', 'supplemental_address_1', 'supplemental_address_2', | |
666 | 'city', 'postal_code', 'postal_code_suffix', 'geo_code_1', 'geo_code_2', | |
667 | 'state_province', 'country', 'phone', 'email', 'im', | |
668 | ); | |
669 | ||
670 | if (isset($mappingId)) { | |
671 | $colCnt = 0; | |
672 | ||
673 | list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingImProvider, | |
674 | $mappingRelation, $mappingOperator, $mappingValue | |
675 | ) = CRM_Core_BAO_Mapping::getMappingFields($mappingId); | |
676 | ||
677 | $blkCnt = count($mappingName); | |
678 | if ($blkCnt >= $blockCount) { | |
679 | $blockCount = $blkCnt + 1; | |
680 | } | |
681 | for ($x = 1; $x < $blockCount; $x++) { | |
682 | if (isset($mappingName[$x])) { | |
683 | $colCnt = count($mappingName[$x]); | |
684 | if ($colCnt >= $columnCount[$x]) { | |
685 | $columnCount[$x] = $colCnt; | |
686 | } | |
687 | } | |
688 | } | |
689 | } | |
690 | ||
691 | $form->_blockCount = $blockCount; | |
692 | $form->_columnCount = $columnCount; | |
693 | ||
694 | $form->set('blockCount', $form->_blockCount); | |
695 | $form->set('columnCount', $form->_columnCount); | |
696 | ||
697 | $defaults = $noneArray = $nullArray = array(); | |
698 | ||
699 | //used to warn for mismatch column count or mismatch mapping | |
700 | $warning = 0; | |
701 | for ($x = 1; $x < $blockCount; $x++) { | |
702 | ||
703 | for ($i = 0; $i < $columnCount[$x]; $i++) { | |
704 | ||
705 | $sel = &$form->addElement('hierselect', "mapper[$x][$i]", ts('Mapper for Field %1', array(1 => $i)), NULL); | |
706 | $jsSet = FALSE; | |
707 | ||
708 | if (isset($mappingId)) { | |
709 | $locationId = isset($mappingLocation[$x][$i]) ? $mappingLocation[$x][$i] : 0; | |
710 | if (isset($mappingName[$x][$i])) { | |
711 | if (is_array($mapperFields[$mappingContactType[$x][$i]])) { | |
712 | ||
713 | if (isset($mappingRelation[$x][$i])) { | |
714 | ||
715 | $contactDetails = strtolower(str_replace(" ", "_", $mappingName[$x][$i])); | |
716 | $relLocationId = isset($mappingLocation[$x][$i]) ? $mappingLocation[$x][$i] : 0; | |
717 | if (!$relLocationId && in_array($mappingName[$x][$i], $specialFields)) { | |
718 | $relLocationId = " "; | |
719 | } | |
720 | ||
721 | $relPhoneType = isset($mappingPhoneType[$x][$i]) ? $mappingPhoneType[$x][$i] : NULL; | |
722 | ||
723 | $defaults["mapper[$x][$i]"] = array( | |
724 | $mappingContactType[$x][$i], | |
725 | $mappingRelation[$x][$i], | |
726 | $locationId, | |
727 | $phoneType, | |
728 | $mappingName[$x][$i], | |
729 | $relLocationId, | |
730 | $relPhoneType, | |
731 | ); | |
732 | ||
733 | if (!$locationId) { | |
734 | $noneArray[] = array($x, $i, 2); | |
735 | } | |
736 | if (!$phoneType && !$imProvider) { | |
737 | $noneArray[] = array($x, $i, 3); | |
738 | } | |
739 | if (!$mappingName[$x][$i]) { | |
740 | $noneArray[] = array($x, $i, 4); | |
741 | } | |
742 | if (!$relLocationId) { | |
743 | $noneArray[] = array($x, $i, 5); | |
744 | } | |
745 | if (!$relPhoneType) { | |
746 | $noneArray[] = array($x, $i, 6); | |
747 | } | |
748 | $noneArray[] = array($x, $i, 2); | |
749 | } | |
750 | else { | |
751 | $phoneType = isset($mappingPhoneType[$x][$i]) ? $mappingPhoneType[$x][$i] : NULL; | |
752 | $imProvider = isset($mappingImProvider[$x][$i]) ? $mappingImProvider[$x][$i] : NULL; | |
753 | if (!$locationId && in_array($mappingName[$x][$i], $specialFields)) { | |
754 | $locationId = " "; | |
755 | } | |
756 | ||
757 | $defaults["mapper[$x][$i]"] = array( | |
758 | $mappingContactType[$x][$i], | |
759 | $mappingName[$x][$i], | |
760 | $locationId, | |
761 | $phoneType, | |
762 | ); | |
763 | if (!$mappingName[$x][$i]) { | |
764 | $noneArray[] = array($x, $i, 1); | |
765 | } | |
766 | if (!$locationId) { | |
767 | $noneArray[] = array($x, $i, 2); | |
768 | } | |
769 | if (!$phoneType && !$imProvider) { | |
770 | $noneArray[] = array($x, $i, 3); | |
771 | } | |
772 | ||
773 | $noneArray[] = array($x, $i, 4); | |
774 | $noneArray[] = array($x, $i, 5); | |
775 | $noneArray[] = array($x, $i, 6); | |
776 | } | |
777 | ||
778 | $jsSet = TRUE; | |
779 | ||
780 | if (CRM_Utils_Array::value($i, CRM_Utils_Array::value($x, $mappingOperator))) { | |
781 | $defaults["operator[$x][$i]"] = CRM_Utils_Array::value($i, $mappingOperator[$x]); | |
782 | } | |
783 | ||
784 | if (CRM_Utils_Array::value($i, CRM_Utils_Array::value($x, $mappingValue))) { | |
785 | $defaults["value[$x][$i]"] = CRM_Utils_Array::value($i, $mappingValue[$x]); | |
786 | } | |
787 | } | |
788 | } | |
789 | } | |
790 | //Fix for Search Builder | |
791 | if ($mappingType == 'Export') { | |
792 | $j = 7; | |
793 | } | |
794 | else { | |
795 | $j = 4; | |
796 | } | |
797 | ||
798 | $formValues = $form->exportValues(); | |
799 | if (!$jsSet) { | |
800 | if (empty($formValues)) { | |
801 | // Incremented length for third select box(relationship type) | |
802 | for ($k = 1; $k < $j; $k++) { | |
803 | $noneArray[] = array($x, $i, $k); | |
804 | } | |
805 | } | |
806 | else { | |
807 | if (!empty($formValues['mapper'][$x])) { | |
808 | foreach ($formValues['mapper'][$x] as $value) { | |
809 | for ($k = 1; $k < $j; $k++) { | |
810 | if (!isset($formValues['mapper'][$x][$i][$k]) || | |
811 | (!$formValues['mapper'][$x][$i][$k]) | |
812 | ) { | |
813 | $noneArray[] = array($x, $i, $k); | |
814 | } | |
815 | else { | |
816 | $nullArray[] = array($x, $i, $k); | |
817 | } | |
818 | } | |
819 | } | |
820 | } | |
821 | else { | |
822 | for ($k = 1; $k < $j; $k++) { | |
823 | $noneArray[] = array($x, $i, $k); | |
824 | } | |
825 | } | |
826 | } | |
827 | } | |
828 | //Fix for Search Builder | |
829 | if ($mappingType == 'Export') { | |
830 | if (!isset($mappingId)) { | |
831 | if (isset($formValues['mapper']) && | |
832 | isset($formValues['mapper'][$x][$i][1]) && | |
833 | array_key_exists($formValues['mapper'][$x][$i][1], $relationshipTypes) | |
834 | ) { | |
835 | $sel->setOptions(array($sel1, $sel2, $sel5, $sel6, $sel7, $sel3, $sel4)); | |
836 | } | |
837 | else { | |
838 | $sel->setOptions(array($sel1, $sel2, $sel3, $sel4, $sel5, $sel6, $sel7)); | |
839 | } | |
840 | } | |
841 | else { | |
842 | $sel->setOptions(array($sel1, $sel2, $sel3, $sel4, $sel5, $sel6, $sel7)); | |
843 | } | |
844 | } | |
845 | else { | |
846 | $sel->setOptions(array($sel1, $sel2, $sel3, $sel4)); | |
847 | } | |
848 | ||
849 | if ($mappingType == 'Search Builder') { | |
850 | //CRM -2292, restricted array set | |
851 | $operatorArray = array( | |
852 | '' => ts('-operator-'), | |
853 | '=' => '=', | |
854 | '!=' => '!=', | |
855 | '>' => '>', | |
856 | '<' => '<', | |
857 | '>=' => '>=', | |
858 | '<=' => '<=', | |
859 | 'IN' => 'IN', | |
860 | 'LIKE' => 'LIKE', | |
861 | 'RLIKE' => 'RLIKE', | |
862 | 'IS EMPTY' => 'IS EMPTY', | |
863 | 'IS NOT EMPTY' => 'IS NOT EMPTY', | |
864 | 'IS NULL' => 'IS NULL', | |
865 | 'IS NOT NULL' => 'IS NOT NULL', | |
866 | ); | |
867 | ||
868 | $form->add('select', "operator[$x][$i]", '', $operatorArray); | |
869 | $form->add('text', "value[$x][$i]", ''); | |
870 | } | |
871 | } | |
872 | //end of columnCnt for | |
873 | if ($mappingType == 'Search Builder') { | |
874 | $title = ts('Another search field'); | |
875 | } | |
876 | else { | |
877 | $title = ts('Select more fields'); | |
878 | } | |
879 | ||
880 | $form->addElement('submit', "addMore[$x]", $title, array('class' => 'submit-link')); | |
881 | } | |
882 | //end of block for | |
883 | ||
884 | $js = "<script type='text/javascript'>\n"; | |
885 | $formName = "document.{$name}"; | |
886 | if (!empty($nullArray)) { | |
887 | $js .= "var nullArray = ["; | |
888 | $elements = array(); | |
889 | $seen = array(); | |
890 | foreach ($nullArray as $element) { | |
891 | $key = "{$element[0]}, {$element[1]}, {$element[2]}"; | |
892 | if (!isset($seen[$key])) { | |
893 | $elements[] = "[$key]"; | |
894 | $seen[$key] = 1; | |
895 | } | |
896 | } | |
897 | $js .= implode(', ', $elements); | |
898 | $js .= "]"; | |
899 | $js .= " | |
900 | for(var i=0;i<nullArray.length;i++) { | |
901 | if ( {$formName}['mapper['+nullArray[i][0]+']['+nullArray[i][1]+']['+nullArray[i][2]+']'] ) { | |
902 | {$formName}['mapper['+nullArray[i][0]+']['+nullArray[i][1]+']['+nullArray[i][2]+']'].style.display = ''; | |
903 | } | |
904 | } | |
905 | "; | |
906 | } | |
907 | if (!empty($noneArray)) { | |
908 | $js .= "var noneArray = ["; | |
909 | $elements = array(); | |
910 | $seen = array(); | |
911 | foreach ($noneArray as $element) { | |
912 | $key = "{$element[0]}, {$element[1]}, {$element[2]}"; | |
913 | if (!isset($seen[$key])) { | |
914 | $elements[] = "[$key]"; | |
915 | $seen[$key] = 1; | |
916 | } | |
917 | } | |
918 | $js .= implode(', ', $elements); | |
919 | $js .= "]"; | |
920 | $js .= " | |
921 | for(var i=0;i<noneArray.length;i++) { | |
922 | if ( {$formName}['mapper['+noneArray[i][0]+']['+noneArray[i][1]+']['+noneArray[i][2]+']'] ) { | |
923 | {$formName}['mapper['+noneArray[i][0]+']['+noneArray[i][1]+']['+noneArray[i][2]+']'].style.display = 'none'; | |
924 | } | |
925 | } | |
926 | "; | |
927 | } | |
928 | $js .= "</script>\n"; | |
929 | ||
930 | $form->assign('initHideBoxes', $js); | |
931 | $form->assign('columnCount', $columnCount); | |
932 | $form->assign('blockCount', $blockCount); | |
933 | $form->setDefaults($defaults); | |
934 | ||
935 | $form->setDefaultAction('refresh'); | |
936 | } | |
937 | ||
938 | /* Function returns all custom fields with group title and | |
939 | * field label | |
940 | * @relationshipTypeId related relationship type id | |
941 | * @return $groupTitle all custom field titles | |
942 | */ | |
943 | function getRelationTypeCustomGroupData($relationshipTypeId) { | |
944 | ||
945 | $customFields = CRM_Core_BAO_CustomField::getFields('Relationship', NULL, NULL, $relationshipTypeId, NULL, NULL); | |
946 | $groupTitle = array(); | |
947 | foreach ($customFields as $krelation => $vrelation) { | |
948 | $groupTitle[$vrelation['label']] = $vrelation['groupTitle'] . '...' . $vrelation['label']; | |
949 | } | |
950 | return $groupTitle; | |
951 | } | |
952 | ||
953 | ||
954 | /** | |
955 | * Function returns all Custom group Names | |
956 | * | |
957 | * @param customfieldId related custom field id | |
958 | * @return $customGroupName all custom group names | |
959 | * @static | |
960 | */ | |
961 | static function getCustomGroupName($customfieldId) { | |
962 | if ($customFieldId = CRM_Core_BAO_CustomField::getKeyID($customfieldId)) { | |
963 | $customGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $customFieldId, 'custom_group_id'); | |
964 | $customGroupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'title'); | |
965 | ||
966 | if (strlen($customGroupName) > 13) { | |
967 | $customGroupName = substr($customGroupName, 0, 10) . '...'; | |
968 | } | |
969 | ||
970 | return $customGroupName; | |
971 | } | |
972 | } | |
973 | ||
974 | /** | |
975 | * Function returns associated array of elements, that will be passed for search | |
976 | * | |
977 | * @params array $params associated array of submitted values | |
978 | * @params boolean $row row no of the fields | |
979 | * | |
980 | * @return $returnFields formatted associated array of elements | |
981 | * | |
982 | * @static | |
983 | * @public | |
984 | */ | |
985 | static function &formattedFields(&$params, $row = FALSE) { | |
986 | $fields = array(); | |
987 | ||
988 | if (empty($params) || !isset($params['mapper'])) { | |
989 | return $fields; | |
990 | } | |
991 | ||
992 | $types = array('Individual', 'Organization', 'Household'); | |
993 | foreach ($params['mapper'] as $key => $value) { | |
994 | $contactType = NULL; | |
995 | foreach ($value as $k => $v) { | |
996 | if (in_array($v[0], $types)) { | |
997 | if ($contactType && $contactType != $v[0]) { | |
998 | CRM_Core_Error::fatal(ts("Cannot have two clauses with different types: %1, %2", | |
999 | array(1 => $contactType, 2 => $v[0]) | |
1000 | )); | |
1001 | } | |
1002 | $contactType = $v[0]; | |
1003 | } | |
1004 | if (CRM_Utils_Array::value('1', $v)) { | |
1005 | $fldName = $v[1]; | |
1006 | $v2 = CRM_Utils_Array::value('2', $v); | |
1007 | if ($v2 && trim($v2)) { | |
1008 | $fldName .= "-{$v[2]}"; | |
1009 | } | |
1010 | ||
1011 | $v3 = CRM_Utils_Array::value('3', $v); | |
1012 | if ($v3 && trim($v3)) { | |
1013 | $fldName .= "-{$v[3]}"; | |
1014 | } | |
1015 | ||
1016 | $value = $params['value'][$key][$k]; | |
1017 | if ($fldName == 'group' || $fldName == 'tag') { | |
1018 | $value = trim($value); | |
1019 | $value = str_replace('(', '', $value); | |
1020 | $value = str_replace(')', '', $value); | |
1021 | ||
1022 | $v = explode(',', $value); | |
1023 | $value = array(); | |
1024 | foreach ($v as $i) { | |
1025 | $value[$i] = 1; | |
1026 | } | |
1027 | } | |
1028 | ||
8ef12e64 | 1029 | if ($v[0] == 'Contribution' && substr($fldName, 0, 7) != 'custom_' |
289c8051 | 1030 | && substr($fldName, 0, 10) != 'financial_') { |
6a488035 TO |
1031 | if (substr($fldName, 0, 13) != 'contribution_') { |
1032 | $fldName = 'contribution_' . $fldName; | |
1033 | } | |
1034 | } | |
1035 | ||
1036 | if ($row) { | |
1037 | $fields[] = array( | |
1038 | $fldName, | |
1039 | $params['operator'][$key][$k], | |
1040 | $value, | |
1041 | $key, | |
1042 | $k, | |
1043 | ); | |
1044 | } | |
1045 | else { | |
1046 | $fields[] = array( | |
1047 | $fldName, | |
1048 | $params['operator'][$key][$k], | |
1049 | $value, | |
1050 | $key, | |
1051 | 0, | |
1052 | ); | |
1053 | } | |
1054 | } | |
1055 | } | |
1056 | if ($contactType) { | |
1057 | $fields[] = array( | |
1058 | 'contact_type', | |
1059 | '=', | |
1060 | $contactType, | |
1061 | $key, | |
1062 | 0, | |
1063 | ); | |
1064 | } | |
1065 | } | |
1066 | ||
1067 | //add sortByCharacter values | |
1068 | if (isset($params['sortByCharacter'])) { | |
1069 | $fields[] = array( | |
1070 | 'sortByCharacter', | |
1071 | '=', | |
1072 | $params['sortByCharacter'], | |
1073 | 0, | |
1074 | 0, | |
1075 | ); | |
1076 | } | |
1077 | ||
1078 | ||
1079 | return $fields; | |
1080 | } | |
1081 | ||
1082 | static function &returnProperties(&$params) { | |
1083 | $fields = array( | |
1084 | 'contact_type' => 1, | |
1085 | 'contact_sub_type' => 1, | |
1086 | 'sort_name' => 1, | |
1087 | ); | |
1088 | ||
1089 | if (empty($params) || empty($params['mapper'])) { | |
1090 | return $fields; | |
1091 | } | |
1092 | ||
b2b0530a | 1093 | $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); |
6a488035 TO |
1094 | foreach ($params['mapper'] as $key => $value) { |
1095 | foreach ($value as $k => $v) { | |
1096 | if (isset($v[1])) { | |
1097 | if ($v[1] == 'groups' || $v[1] == 'tags') { | |
1098 | continue; | |
1099 | } | |
1100 | ||
1101 | if (isset($v[2]) && is_numeric($v[2])) { | |
1102 | if (!array_key_exists('location', $fields)) { | |
1103 | $fields['location'] = array(); | |
1104 | } | |
1105 | ||
1106 | // make sure that we have a location fields and a location type for this | |
1107 | $locationName = $locationTypes[$v[2]]; | |
1108 | if (!array_key_exists($locationName, $fields['location'])) { | |
1109 | $fields['location'][$locationName] = array(); | |
1110 | $fields['location'][$locationName]['location_type'] = $v[2]; | |
1111 | } | |
1112 | ||
1113 | if ($v[1] == 'phone' || $v[1] == 'email' || $v[1] == 'im') { | |
1114 | // phone type handling | |
1115 | if (isset($v[3])) { | |
1116 | $fields['location'][$locationName][$v[1] . "-" . $v[3]] = 1; | |
1117 | } | |
1118 | else { | |
1119 | $fields['location'][$locationName][$v[1]] = 1; | |
1120 | } | |
1121 | } | |
1122 | else { | |
1123 | $fields['location'][$locationName][$v[1]] = 1; | |
1124 | } | |
1125 | } | |
1126 | else { | |
1127 | $fields[$v[1]] = 1; | |
1128 | } | |
1129 | } | |
1130 | } | |
1131 | } | |
1132 | ||
1133 | return $fields; | |
1134 | } | |
1135 | ||
1136 | /** | |
1137 | * save the mapping field info for search builder / export given the formvalues | |
1138 | * | |
1139 | * @param array $params asscociated array of formvalues | |
1140 | * @param int $mappingId mapping id | |
1141 | * | |
1142 | * @return null | |
1143 | * @static | |
1144 | * @access public | |
1145 | */ | |
1146 | static function saveMappingFields(&$params, $mappingId) { | |
1147 | //delete mapping fields records for exixting mapping | |
1148 | $mappingFields = new CRM_Core_DAO_MappingField(); | |
1149 | $mappingFields->mapping_id = $mappingId; | |
1150 | $mappingFields->delete(); | |
1151 | ||
1152 | if (empty($params['mapper'])) { | |
1153 | return; | |
1154 | } | |
1155 | ||
1156 | //save record in mapping field table | |
1157 | foreach ($params['mapper'] as $key => $value) { | |
1158 | $colCnt = 0; | |
1159 | foreach ($value as $k => $v) { | |
1160 | ||
1161 | if (CRM_Utils_Array::value('1', $v)) { | |
1162 | $saveMappingFields = new CRM_Core_DAO_MappingField(); | |
1163 | ||
1164 | $saveMappingFields->mapping_id = $mappingId; | |
1165 | $saveMappingFields->name = CRM_Utils_Array::value('1', $v); | |
1166 | $saveMappingFields->contact_type = CRM_Utils_Array::value('0', $v); | |
1167 | $locationId = CRM_Utils_Array::value('2', $v); | |
1168 | $saveMappingFields->location_type_id = is_numeric($locationId) ? $locationId : NULL; | |
1169 | ||
1170 | if ($v[1] == 'phone') { | |
1171 | $saveMappingFields->phone_type_id = CRM_Utils_Array::value('3', $v); | |
1172 | } | |
1173 | elseif ($v[1] == 'im') { | |
1174 | $saveMappingFields->im_provider_id = CRM_Utils_Array::value('3', $v); | |
1175 | } | |
1176 | ||
1177 | if (CRM_Utils_Array::value('operator', $params)) { | |
8ef12e64 | 1178 | $saveMappingFields->operator = CRM_Utils_Array::value($k, $params['operator'][$key]); |
6a488035 TO |
1179 | } |
1180 | if (CRM_Utils_Array::value('value', $params)) { | |
1181 | $saveMappingFields->value = CRM_Utils_Array::value($k, $params['value'][$key]); | |
1182 | } | |
1183 | // Handle mapping for 'related contact' fields | |
1184 | if (count(explode('_', CRM_Utils_Array::value('1', $v))) > 2) { | |
1185 | list($id, $first, $second) = explode('_', CRM_Utils_Array::value('1', $v)); | |
1186 | if (($first == 'a' && $second == 'b') || ($first == 'b' && $second == 'a')) { | |
1187 | ||
1188 | if (CRM_Utils_Array::value('2', $v)) { | |
1189 | $saveMappingFields->name = CRM_Utils_Array::value('2', $v); | |
1190 | } | |
1191 | elseif (CRM_Utils_Array::value('4', $v)) { | |
1192 | $saveMappingFields->name = CRM_Utils_Array::value('4', $v); | |
1193 | } | |
1194 | ||
1195 | if (is_numeric(CRM_Utils_Array::value('3', $v))) { | |
1196 | $locationTypeid = CRM_Utils_Array::value('3', $v); | |
1197 | } | |
1198 | elseif (is_numeric(CRM_Utils_Array::value('5', $v))) { | |
1199 | $locationTypeid = CRM_Utils_Array::value('5', $v); | |
1200 | } | |
1201 | ||
1202 | if (is_numeric(CRM_Utils_Array::value('4', $v))) { | |
1203 | $phoneTypeid = CRM_Utils_Array::value('4', $v); | |
1204 | } | |
1205 | elseif (is_numeric(CRM_Utils_Array::value('6', $v))) { | |
1206 | $phoneTypeid = CRM_Utils_Array::value('6', $v); | |
1207 | } | |
1208 | ||
1209 | $saveMappingFields->location_type_id = is_numeric($locationTypeid) ? $locationTypeid : NULL; | |
1210 | $saveMappingFields->phone_type_id = is_numeric($phoneTypeid) ? $phoneTypeid : NULL; | |
1211 | $saveMappingFields->relationship_type_id = $id; | |
1212 | $saveMappingFields->relationship_direction = "{$first}_{$second}"; | |
1213 | } | |
1214 | } | |
1215 | ||
1216 | $saveMappingFields->grouping = $key; | |
1217 | $saveMappingFields->column_number = $colCnt; | |
1218 | $saveMappingFields->save(); | |
1219 | $colCnt++; | |
1220 | $locationTypeid = $phoneTypeid = NULL; | |
1221 | } | |
1222 | } | |
1223 | } | |
1224 | } | |
1225 | } | |
1226 |