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