6003a964 |
1 | <?php |
2 | /* |
3 | +--------------------------------------------------------------------+ |
4 | | CiviCRM version 5 | |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | |
9 | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
11 | | under the terms of the GNU Affero General Public License | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
13 | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
17 | | See the GNU Affero General Public License for more details. | |
18 | | | |
19 | | You should have received a copy of the GNU Affero General Public | |
20 | | License and the CiviCRM Licensing Exception along | |
21 | | with this program; if not, contact CiviCRM LLC | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
25 | +--------------------------------------------------------------------+ |
26 | */ |
27 | |
28 | /** |
29 | * |
30 | * @package CRM |
31 | * @copyright CiviCRM LLC (c) 2004-2018 |
32 | */ |
33 | |
34 | /** |
35 | * Class CRM_Export_BAO_ExportProcessor |
36 | * |
37 | * Class to handle logic of export. |
38 | */ |
39 | class CRM_Export_BAO_ExportProcessor { |
40 | |
41 | /** |
42 | * @var int |
43 | */ |
44 | protected $queryMode; |
45 | |
46 | /** |
47 | * @var int |
48 | */ |
49 | protected $exportMode; |
50 | |
adabfa40 |
51 | /** |
52 | * Array of fields in the main query. |
53 | * |
54 | * @var array |
55 | */ |
56 | protected $queryFields = []; |
57 | |
71464b73 |
58 | /** |
59 | * Either AND or OR. |
60 | * |
61 | * @var string |
62 | */ |
63 | protected $queryOperator; |
64 | |
d41ab886 |
65 | /** |
66 | * Requested output fields. |
67 | * |
68 | * If set to NULL then it is 'primary fields only' |
69 | * which actually means pretty close to all fields! |
70 | * |
71 | * @var array|null |
72 | */ |
73 | protected $requestedFields; |
74 | |
b7db6051 |
75 | /** |
76 | * Is the contact being merged into a single household. |
77 | * |
78 | * @var bool |
79 | */ |
80 | protected $isMergeSameHousehold; |
81 | |
944ed388 |
82 | /** |
83 | * Key representing the head of household in the relationship array. |
84 | * |
85 | * e.g. ['8_b_a' => 'Household Member Is', '8_a_b = 'Household Member Of'.....] |
86 | * |
87 | * @var |
88 | */ |
89 | protected $relationshipTypes = []; |
90 | |
704e3e9a |
91 | /** |
92 | * Array of properties to retrieve for relationships. |
93 | * |
94 | * @var array |
95 | */ |
96 | protected $relationshipReturnProperties = []; |
97 | |
ce12a9e0 |
98 | /** |
99 | * Get return properties by relationship. |
100 | * @return array |
101 | */ |
102 | public function getRelationshipReturnProperties() { |
103 | return $this->relationshipReturnProperties; |
104 | } |
105 | |
106 | /** |
107 | * Export values for related contacts. |
108 | * |
109 | * @var array |
110 | */ |
111 | protected $relatedContactValues = []; |
112 | |
c66a5741 |
113 | /** |
114 | * @var array |
115 | */ |
116 | protected $returnProperties = []; |
117 | |
71464b73 |
118 | /** |
119 | * CRM_Export_BAO_ExportProcessor constructor. |
120 | * |
121 | * @param int $exportMode |
d41ab886 |
122 | * @param array|NULL $requestedFields |
71464b73 |
123 | * @param string $queryOperator |
b7db6051 |
124 | * @param bool $isMergeSameHousehold |
71464b73 |
125 | */ |
b7db6051 |
126 | public function __construct($exportMode, $requestedFields, $queryOperator, $isMergeSameHousehold = FALSE) { |
71464b73 |
127 | $this->setExportMode($exportMode); |
128 | $this->setQueryMode(); |
129 | $this->setQueryOperator($queryOperator); |
d41ab886 |
130 | $this->setRequestedFields($requestedFields); |
944ed388 |
131 | $this->setRelationshipTypes(); |
b7db6051 |
132 | $this->setIsMergeSameHousehold($isMergeSameHousehold); |
d41ab886 |
133 | } |
134 | |
135 | /** |
136 | * @return array|null |
137 | */ |
138 | public function getRequestedFields() { |
139 | return $this->requestedFields; |
140 | } |
141 | |
142 | /** |
143 | * @param array|null $requestedFields |
144 | */ |
145 | public function setRequestedFields($requestedFields) { |
146 | $this->requestedFields = $requestedFields; |
71464b73 |
147 | } |
148 | |
c66a5741 |
149 | /** |
150 | * @return array |
151 | */ |
152 | public function getReturnProperties() { |
153 | return $this->returnProperties; |
154 | } |
155 | |
156 | /** |
157 | * @param array $returnProperties |
158 | */ |
159 | public function setReturnProperties($returnProperties) { |
160 | $this->returnProperties = $returnProperties; |
161 | } |
162 | |
944ed388 |
163 | /** |
164 | * @return array |
165 | */ |
166 | public function getRelationshipTypes() { |
167 | return $this->relationshipTypes; |
168 | } |
169 | |
170 | /** |
171 | */ |
172 | public function setRelationshipTypes() { |
173 | $this->relationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType( |
174 | NULL, |
175 | NULL, |
176 | NULL, |
177 | NULL, |
178 | TRUE, |
179 | 'name', |
180 | FALSE |
181 | ); |
182 | } |
183 | |
ce12a9e0 |
184 | /** |
185 | * Set the value for a relationship type field. |
186 | * |
187 | * In this case we are building up an array of properties for a related contact. |
188 | * |
189 | * These may be used for direct exporting or for merge to household depending on the |
190 | * options selected. |
191 | * |
192 | * @param string $relationshipType |
193 | * @param int $contactID |
194 | * @param string $field |
195 | * @param string $value |
196 | */ |
197 | public function setRelationshipValue($relationshipType, $contactID, $field, $value) { |
198 | $this->relatedContactValues[$relationshipType][$contactID][$field] = $value; |
199 | } |
200 | |
201 | /** |
202 | * Get the value for a relationship type field. |
203 | * |
204 | * In this case we are building up an array of properties for a related contact. |
205 | * |
206 | * These may be used for direct exporting or for merge to household depending on the |
207 | * options selected. |
208 | * |
209 | * @param string $relationshipType |
210 | * @param int $contactID |
211 | * @param string $field |
212 | * |
213 | * @return string |
214 | */ |
215 | public function getRelationshipValue($relationshipType, $contactID, $field) { |
216 | return isset($this->relatedContactValues[$relationshipType][$contactID][$field]) ? $this->relatedContactValues[$relationshipType][$contactID][$field] : ''; |
217 | } |
218 | |
b7db6051 |
219 | /** |
220 | * @return bool |
221 | */ |
222 | public function isMergeSameHousehold() { |
223 | return $this->isMergeSameHousehold; |
224 | } |
225 | |
226 | /** |
227 | * @param bool $isMergeSameHousehold |
228 | */ |
229 | public function setIsMergeSameHousehold($isMergeSameHousehold) { |
230 | $this->isMergeSameHousehold = $isMergeSameHousehold; |
231 | } |
232 | |
233 | /** |
234 | * Return relationship types for household merge. |
235 | * |
236 | * @return mixed |
237 | */ |
238 | public function getHouseholdRelationshipTypes() { |
239 | if (!$this->isMergeSameHousehold()) { |
240 | return []; |
241 | } |
242 | return [ |
243 | CRM_Utils_Array::key('Household Member of', $this->getRelationshipTypes()), |
244 | CRM_Utils_Array::key('Head of Household for', $this->getRelationshipTypes()), |
245 | ]; |
246 | } |
944ed388 |
247 | |
248 | /** |
249 | * @param $fieldName |
250 | * @return bool |
251 | */ |
252 | public function isRelationshipTypeKey($fieldName) { |
253 | return array_key_exists($fieldName, $this->relationshipTypes); |
254 | } |
255 | |
b7db6051 |
256 | |
257 | /** |
258 | * @param $fieldName |
259 | * @return bool |
260 | */ |
261 | public function isHouseholdMergeRelationshipTypeKey($fieldName) { |
262 | return in_array($fieldName, $this->getHouseholdRelationshipTypes()); |
263 | } |
264 | |
71464b73 |
265 | /** |
266 | * @return string |
267 | */ |
268 | public function getQueryOperator() { |
269 | return $this->queryOperator; |
270 | } |
271 | |
272 | /** |
273 | * @param string $queryOperator |
274 | */ |
275 | public function setQueryOperator($queryOperator) { |
276 | $this->queryOperator = $queryOperator; |
277 | } |
278 | |
adabfa40 |
279 | /** |
280 | * @return array |
281 | */ |
282 | public function getQueryFields() { |
283 | return $this->queryFields; |
284 | } |
285 | |
286 | /** |
287 | * @param array $queryFields |
288 | */ |
289 | public function setQueryFields($queryFields) { |
290 | $this->queryFields = $queryFields; |
291 | } |
292 | |
6003a964 |
293 | /** |
294 | * @return int |
295 | */ |
296 | public function getQueryMode() { |
297 | return $this->queryMode; |
298 | } |
299 | |
300 | /** |
301 | * Set the query mode based on the export mode. |
302 | */ |
303 | public function setQueryMode() { |
304 | |
305 | switch ($this->getExportMode()) { |
306 | case CRM_Export_Form_Select::CONTRIBUTE_EXPORT: |
307 | $this->queryMode = CRM_Contact_BAO_Query::MODE_CONTRIBUTE; |
308 | break; |
309 | |
310 | case CRM_Export_Form_Select::EVENT_EXPORT: |
311 | $this->queryMode = CRM_Contact_BAO_Query::MODE_EVENT; |
312 | break; |
313 | |
314 | case CRM_Export_Form_Select::MEMBER_EXPORT: |
315 | $this->queryMode = CRM_Contact_BAO_Query::MODE_MEMBER; |
316 | break; |
317 | |
318 | case CRM_Export_Form_Select::PLEDGE_EXPORT: |
319 | $this->queryMode = CRM_Contact_BAO_Query::MODE_PLEDGE; |
320 | break; |
321 | |
322 | case CRM_Export_Form_Select::CASE_EXPORT: |
323 | $this->queryMode = CRM_Contact_BAO_Query::MODE_CASE; |
324 | break; |
325 | |
326 | case CRM_Export_Form_Select::GRANT_EXPORT: |
327 | $this->queryMode = CRM_Contact_BAO_Query::MODE_GRANT; |
328 | break; |
329 | |
330 | case CRM_Export_Form_Select::ACTIVITY_EXPORT: |
331 | $this->queryMode = CRM_Contact_BAO_Query::MODE_ACTIVITY; |
332 | break; |
333 | |
334 | default: |
335 | $this->queryMode = CRM_Contact_BAO_Query::MODE_CONTACTS; |
336 | } |
337 | } |
338 | |
339 | /** |
340 | * @return int |
341 | */ |
342 | public function getExportMode() { |
343 | return $this->exportMode; |
344 | } |
345 | |
346 | /** |
347 | * @param int $exportMode |
348 | */ |
349 | public function setExportMode($exportMode) { |
350 | $this->exportMode = $exportMode; |
351 | } |
352 | |
29034a98 |
353 | /** |
354 | * Get the name for the export file. |
355 | * |
356 | * @return string |
357 | */ |
358 | public function getExportFileName() { |
359 | switch ($this->getExportMode()) { |
360 | case CRM_Export_Form_Select::CONTACT_EXPORT: |
361 | return ts('CiviCRM Contact Search'); |
362 | |
363 | case CRM_Export_Form_Select::CONTRIBUTE_EXPORT: |
364 | return ts('CiviCRM Contribution Search'); |
365 | |
366 | case CRM_Export_Form_Select::MEMBER_EXPORT: |
367 | return ts('CiviCRM Member Search'); |
368 | |
369 | case CRM_Export_Form_Select::EVENT_EXPORT: |
370 | return ts('CiviCRM Participant Search'); |
371 | |
372 | case CRM_Export_Form_Select::PLEDGE_EXPORT: |
373 | return ts('CiviCRM Pledge Search'); |
374 | |
375 | case CRM_Export_Form_Select::CASE_EXPORT: |
376 | return ts('CiviCRM Case Search'); |
377 | |
378 | case CRM_Export_Form_Select::GRANT_EXPORT: |
379 | return ts('CiviCRM Grant Search'); |
380 | |
381 | case CRM_Export_Form_Select::ACTIVITY_EXPORT: |
382 | return ts('CiviCRM Activity Search'); |
383 | |
384 | default: |
385 | // Legacy code suggests the value could be 'financial' - ie. something |
386 | // other than what should be accepted. However, I suspect that this line is |
387 | // never hit. |
388 | return ts('CiviCRM Search'); |
389 | } |
390 | } |
391 | |
adabfa40 |
392 | /** |
393 | * @param $params |
394 | * @param $order |
adabfa40 |
395 | * @param $returnProperties |
396 | * @return array |
397 | */ |
71464b73 |
398 | public function runQuery($params, $order, $returnProperties) { |
adabfa40 |
399 | $query = new CRM_Contact_BAO_Query($params, $returnProperties, NULL, |
400 | FALSE, FALSE, $this->getQueryMode(), |
71464b73 |
401 | FALSE, TRUE, TRUE, NULL, $this->getQueryOperator() |
adabfa40 |
402 | ); |
403 | |
404 | //sort by state |
405 | //CRM-15301 |
406 | $query->_sort = $order; |
407 | list($select, $from, $where, $having) = $query->query(); |
408 | $this->setQueryFields($query->_fields); |
409 | return array($query, $select, $from, $where, $having); |
410 | } |
411 | |
ce14544c |
412 | /** |
413 | * Get array of fields to return, over & above those defined in the main contact exportable fields. |
414 | * |
415 | * These include export mode specific fields & some fields apparently required as 'exportableFields' |
416 | * but not returned by the function of the same name. |
417 | * |
418 | * @return array |
419 | * Array of fields to return in the format ['field_name' => 1,...] |
420 | */ |
421 | public function getAdditionalReturnProperties() { |
422 | |
423 | $missing = [ |
424 | 'location_type', |
425 | 'im_provider', |
426 | 'phone_type_id', |
427 | 'provider_id', |
428 | 'current_employer', |
429 | ]; |
430 | if ($this->getQueryMode() === CRM_Contact_BAO_Query::MODE_CONTACTS) { |
431 | $componentSpecificFields = []; |
432 | } |
433 | else { |
434 | $componentSpecificFields = CRM_Contact_BAO_Query::defaultReturnProperties($this->getQueryMode()); |
435 | } |
436 | if ($this->getQueryMode() === CRM_Contact_BAO_Query::MODE_PLEDGE) { |
437 | $componentSpecificFields = array_merge($componentSpecificFields, CRM_Pledge_BAO_Query::extraReturnProperties($this->getQueryMode())); |
d28b6cf2 |
438 | unset($componentSpecificFields['contribution_status_id']); |
439 | unset($componentSpecificFields['pledge_status_id']); |
440 | unset($componentSpecificFields['pledge_payment_status_id']); |
ce14544c |
441 | } |
442 | if ($this->getQueryMode() === CRM_Contact_BAO_Query::MODE_CASE) { |
443 | $componentSpecificFields = array_merge($componentSpecificFields, CRM_Case_BAO_Query::extraReturnProperties($this->getQueryMode())); |
444 | } |
445 | if ($this->getQueryMode() === CRM_Contact_BAO_Query::MODE_CONTRIBUTE) { |
446 | $componentSpecificFields = array_merge($componentSpecificFields, CRM_Contribute_BAO_Query::softCreditReturnProperties(TRUE)); |
d28b6cf2 |
447 | unset($componentSpecificFields['contribution_status_id']); |
ce14544c |
448 | } |
449 | return array_merge(array_fill_keys($missing, 1), $componentSpecificFields); |
450 | } |
451 | |
d41ab886 |
452 | /** |
453 | * Should payment fields be appended to the export. |
454 | * |
455 | * (This is pretty hacky so hopefully this function won't last long - notice |
456 | * how obviously it should be part of the above function!). |
457 | */ |
458 | public function isExportPaymentFields() { |
459 | if ($this->getRequestedFields() === NULL |
f065c170 |
460 | && in_array($this->getQueryMode(), [ |
d41ab886 |
461 | CRM_Contact_BAO_Query::MODE_EVENT, |
462 | CRM_Contact_BAO_Query::MODE_MEMBER, |
463 | CRM_Contact_BAO_Query::MODE_PLEDGE, |
464 | ])) { |
465 | return TRUE; |
466 | } |
c66a5741 |
467 | elseif ($this->isExportSpecifiedPaymentFields()) { |
468 | return TRUE; |
469 | } |
d41ab886 |
470 | return FALSE; |
471 | } |
472 | |
c66a5741 |
473 | /** |
474 | * Has specific payment fields been requested (as opposed to via all fields). |
475 | * |
476 | * If specific fields have been requested then they get added at various points. |
477 | * |
478 | * @return bool |
479 | */ |
480 | public function isExportSpecifiedPaymentFields() { |
481 | if ($this->getRequestedFields() !== NULL && $this->hasRequestedComponentPaymentFields()) { |
482 | return TRUE; |
483 | } |
484 | } |
485 | |
d41ab886 |
486 | /** |
487 | * Get the name of the id field in the table that connects contributions to the export entity. |
488 | */ |
489 | public function getPaymentTableID() { |
490 | if ($this->getRequestedFields() === NULL) { |
491 | $mapping = [ |
492 | CRM_Contact_BAO_Query::MODE_EVENT => 'participant_id', |
493 | CRM_Contact_BAO_Query::MODE_MEMBER => 'membership_id', |
494 | CRM_Contact_BAO_Query::MODE_PLEDGE => 'pledge_payment_id', |
495 | ]; |
496 | return isset($mapping[$this->getQueryMode()]) ? $mapping[$this->getQueryMode()] : ''; |
497 | } |
c66a5741 |
498 | elseif ($this->hasRequestedComponentPaymentFields()) { |
499 | return 'participant_id'; |
500 | } |
d41ab886 |
501 | return FALSE; |
502 | } |
503 | |
c66a5741 |
504 | /** |
505 | * Have component payment fields been requested. |
506 | * |
507 | * @return bool |
508 | */ |
509 | protected function hasRequestedComponentPaymentFields() { |
510 | if ($this->getQueryMode() === CRM_Contact_BAO_Query::MODE_EVENT) { |
511 | $participantPaymentFields = array_intersect_key($this->getComponentPaymentFields(), $this->getReturnProperties()); |
512 | if (!empty($participantPaymentFields)) { |
513 | return TRUE; |
514 | } |
515 | } |
d41ab886 |
516 | return FALSE; |
517 | } |
518 | |
c66a5741 |
519 | /** |
520 | * Get fields that indicate payment fields have been requested for a component. |
521 | * |
0e32ed68 |
522 | * Ideally this should be protected but making it temporarily public helps refactoring.. |
523 | * |
c66a5741 |
524 | * @return array |
525 | */ |
0e32ed68 |
526 | public function getComponentPaymentFields() { |
c66a5741 |
527 | return [ |
528 | 'componentPaymentField_total_amount' => ts('Total Amount'), |
529 | 'componentPaymentField_contribution_status' => ts('Contribution Status'), |
530 | 'componentPaymentField_received_date' => ts('Date Received'), |
531 | 'componentPaymentField_payment_instrument' => ts('Payment Method'), |
532 | 'componentPaymentField_transaction_id' => ts('Transaction ID'), |
533 | ]; |
534 | } |
535 | |
05ad310f |
536 | /** |
537 | * Get headers for payment fields. |
538 | * |
539 | * Returns an array of contribution fields when the entity supports payment fields and specific fields |
540 | * are not specified. This is a transitional function for refactoring legacy code. |
541 | */ |
542 | public function getPaymentHeaders() { |
543 | if ($this->isExportPaymentFields() && !$this->isExportSpecifiedPaymentFields()) { |
544 | return $this->getcomponentPaymentFields(); |
545 | } |
546 | return []; |
547 | } |
548 | |
d41ab886 |
549 | /** |
550 | * Get the default properties when not specified. |
551 | * |
552 | * In the UI this appears as 'Primary fields only' but in practice it's |
553 | * most of the kitchen sink and the hallway closet thrown in. |
554 | * |
555 | * Since CRM-952 custom fields are excluded, but no other form of mercy is shown. |
556 | * |
557 | * @return array |
558 | */ |
559 | public function getDefaultReturnProperties() { |
560 | $returnProperties = []; |
561 | $fields = CRM_Contact_BAO_Contact::exportableFields('All', TRUE, TRUE); |
704e3e9a |
562 | $skippedFields = ($this->getQueryMode() === CRM_Contact_BAO_Query::MODE_CONTACTS) ? [] : [ |
563 | 'groups', |
564 | 'tags', |
565 | 'notes' |
566 | ]; |
d41ab886 |
567 | |
568 | foreach ($fields as $key => $var) { |
569 | if ($key && (substr($key, 0, 6) != 'custom') && !in_array($key, $skippedFields)) { |
570 | $returnProperties[$key] = 1; |
571 | } |
572 | } |
573 | $returnProperties = array_merge($returnProperties, $this->getAdditionalReturnProperties()); |
574 | return $returnProperties; |
575 | } |
576 | |
704e3e9a |
577 | /** |
578 | * Add the field to relationship return properties & return it. |
579 | * |
580 | * This function is doing both setting & getting which is yuck but it is an interim |
581 | * refactor. |
582 | * |
583 | * @param array $value |
584 | * @param string $relationshipKey |
585 | * |
586 | * @return array |
587 | */ |
588 | public function setRelationshipReturnProperties($value, $relationshipKey) { |
704e3e9a |
589 | $relPhoneTypeId = $relIMProviderId = NULL; |
590 | if (!empty($value[2])) { |
591 | $relationField = CRM_Utils_Array::value(2, $value); |
592 | if (trim(CRM_Utils_Array::value(3, $value))) { |
593 | $relLocTypeId = CRM_Utils_Array::value(3, $value); |
594 | } |
595 | else { |
596 | $relLocTypeId = 'Primary'; |
597 | } |
598 | |
599 | if ($relationField == 'phone') { |
600 | $relPhoneTypeId = CRM_Utils_Array::value(4, $value); |
601 | } |
602 | elseif ($relationField == 'im') { |
603 | $relIMProviderId = CRM_Utils_Array::value(4, $value); |
604 | } |
605 | } |
606 | elseif (!empty($value[4])) { |
607 | $relationField = CRM_Utils_Array::value(4, $value); |
608 | $relLocTypeId = CRM_Utils_Array::value(5, $value); |
609 | if ($relationField == 'phone') { |
610 | $relPhoneTypeId = CRM_Utils_Array::value(6, $value); |
611 | } |
612 | elseif ($relationField == 'im') { |
613 | $relIMProviderId = CRM_Utils_Array::value(6, $value); |
614 | } |
615 | } |
616 | if (in_array($relationField, $this->getValidLocationFields()) && is_numeric($relLocTypeId)) { |
a16a432a |
617 | $locationName = CRM_Core_PseudoConstant::getName('CRM_Core_BAO_Address', 'location_type_id', $relLocTypeId); |
704e3e9a |
618 | if ($relPhoneTypeId) { |
a16a432a |
619 | $this->relationshipReturnProperties[$relationshipKey]['location'][$locationName]['phone-' . $relPhoneTypeId] = 1; |
704e3e9a |
620 | } |
621 | elseif ($relIMProviderId) { |
a16a432a |
622 | $this->relationshipReturnProperties[$relationshipKey]['location'][$locationName]['im-' . $relIMProviderId] = 1; |
704e3e9a |
623 | } |
624 | else { |
a16a432a |
625 | $this->relationshipReturnProperties[$relationshipKey]['location'][$locationName][$relationField] = 1; |
704e3e9a |
626 | } |
627 | } |
628 | else { |
629 | $this->relationshipReturnProperties[$relationshipKey][$relationField] = 1; |
630 | } |
631 | return $this->relationshipReturnProperties[$relationshipKey]; |
632 | } |
633 | |
ce12a9e0 |
634 | /** |
635 | * Add the main return properties to the household merge properties if needed for merging. |
636 | * |
637 | * If we are using household merge we need to add these to the relationship properties to |
638 | * be retrieved. |
639 | * |
640 | * @param $returnProperties |
641 | */ |
642 | public function setHouseholdMergeReturnProperties($returnProperties) { |
643 | foreach ($this->getHouseholdRelationshipTypes() as $householdRelationshipType) { |
644 | $this->relationshipReturnProperties[$householdRelationshipType] = $returnProperties; |
645 | } |
646 | } |
647 | |
704e3e9a |
648 | /** |
649 | * Get the default location fields to request. |
650 | * |
651 | * @return array |
652 | */ |
653 | public function getValidLocationFields() { |
654 | return [ |
655 | 'street_address', |
656 | 'supplemental_address_1', |
657 | 'supplemental_address_2', |
658 | 'supplemental_address_3', |
659 | 'city', |
660 | 'postal_code', |
661 | 'postal_code_suffix', |
662 | 'geo_code_1', |
663 | 'geo_code_2', |
664 | 'state_province', |
665 | 'country', |
666 | 'phone', |
667 | 'email', |
668 | 'im', |
669 | ]; |
670 | } |
671 | |
aa3a113b |
672 | /** |
c8adad81 |
673 | * Get the sql column definition for the given field. |
674 | * |
aa3a113b |
675 | * @param $field |
aa3a113b |
676 | * |
677 | * @return mixed |
678 | */ |
c8adad81 |
679 | public function getSqlColumnDefinition($field) { |
680 | $fieldName = $this->getMungedFieldName($field); |
aa3a113b |
681 | |
682 | // early exit for master_id, CRM-12100 |
683 | // in the DB it is an ID, but in the export, we retrive the display_name of the master record |
684 | // also for current_employer, CRM-16939 |
685 | if ($fieldName == 'master_id' || $fieldName == 'current_employer') { |
686 | return "$fieldName varchar(128)"; |
687 | } |
688 | |
689 | if (substr($fieldName, -11) == 'campaign_id') { |
690 | // CRM-14398 |
691 | return "$fieldName varchar(128)"; |
692 | } |
693 | |
694 | $queryFields = $this->getQueryFields(); |
695 | $lookUp = ['prefix_id', 'suffix_id']; |
696 | // set the sql columns |
697 | if (isset($queryFields[$field]['type'])) { |
698 | switch ($queryFields[$field]['type']) { |
699 | case CRM_Utils_Type::T_INT: |
700 | case CRM_Utils_Type::T_BOOLEAN: |
701 | if (in_array($field, $lookUp)) { |
702 | return "$fieldName varchar(255)"; |
703 | } |
704 | else { |
705 | return "$fieldName varchar(16)"; |
706 | } |
707 | |
708 | case CRM_Utils_Type::T_STRING: |
709 | if (isset($queryFields[$field]['maxlength'])) { |
710 | return "$fieldName varchar({$queryFields[$field]['maxlength']})"; |
711 | } |
712 | else { |
713 | return "$fieldName varchar(255)"; |
714 | } |
715 | |
716 | case CRM_Utils_Type::T_TEXT: |
717 | case CRM_Utils_Type::T_LONGTEXT: |
718 | case CRM_Utils_Type::T_BLOB: |
719 | case CRM_Utils_Type::T_MEDIUMBLOB: |
720 | return "$fieldName longtext"; |
721 | |
722 | case CRM_Utils_Type::T_FLOAT: |
723 | case CRM_Utils_Type::T_ENUM: |
724 | case CRM_Utils_Type::T_DATE: |
725 | case CRM_Utils_Type::T_TIME: |
726 | case CRM_Utils_Type::T_TIMESTAMP: |
727 | case CRM_Utils_Type::T_MONEY: |
728 | case CRM_Utils_Type::T_EMAIL: |
729 | case CRM_Utils_Type::T_URL: |
730 | case CRM_Utils_Type::T_CCNUM: |
731 | default: |
732 | return "$fieldName varchar(32)"; |
733 | } |
734 | } |
735 | else { |
736 | if (substr($fieldName, -3, 3) == '_id') { |
737 | return "$fieldName varchar(255)"; |
738 | } |
739 | elseif (substr($fieldName, -5, 5) == '_note') { |
740 | return "$fieldName text"; |
741 | } |
742 | else { |
743 | $changeFields = [ |
744 | 'groups', |
745 | 'tags', |
746 | 'notes', |
747 | ]; |
748 | |
749 | if (in_array($fieldName, $changeFields)) { |
750 | return "$fieldName text"; |
751 | } |
752 | else { |
753 | // set the sql columns for custom data |
754 | if (isset($queryFields[$field]['data_type'])) { |
755 | |
756 | switch ($queryFields[$field]['data_type']) { |
757 | case 'String': |
758 | // May be option labels, which could be up to 512 characters |
759 | $length = max(512, CRM_Utils_Array::value('text_length', $queryFields[$field])); |
760 | return "$fieldName varchar($length)"; |
761 | |
762 | case 'Country': |
763 | case 'StateProvince': |
764 | case 'Link': |
765 | return "$fieldName varchar(255)"; |
766 | |
767 | case 'Memo': |
768 | return "$fieldName text"; |
769 | |
770 | default: |
771 | return "$fieldName varchar(255)"; |
772 | } |
773 | } |
774 | else { |
775 | return "$fieldName text"; |
776 | } |
777 | } |
778 | } |
779 | } |
780 | } |
781 | |
c8adad81 |
782 | /** |
783 | * Get the munged field name. |
784 | * |
785 | * @param string $field |
786 | * @return string |
787 | */ |
788 | public function getMungedFieldName($field) { |
789 | $fieldName = CRM_Utils_String::munge(strtolower($field), '_', 64); |
790 | if ($fieldName == 'id') { |
791 | $fieldName = 'civicrm_primary_id'; |
792 | } |
793 | return $fieldName; |
794 | } |
795 | |
6003a964 |
796 | } |