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