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