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