CRM-18972: List of strings that cannot be localized cause the absence of ts() function
[civicrm-core.git] / CRM / Report / Form / Contribute / SoftCredit.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
32 * $Id$
33 *
34 */
35 class CRM_Report_Form_Contribute_SoftCredit extends CRM_Report_Form {
36
37 protected $_emailField = FALSE;
38 protected $_emailFieldCredit = FALSE;
39 protected $_phoneField = FALSE;
40 protected $_phoneFieldCredit = FALSE;
41 protected $_charts = array(
42 '' => 'Tabular',
43 'barChart' => 'Bar Chart',
44 'pieChart' => 'Pie Chart',
45 );
46
47 protected $_customGroupExtends = array(
48 'Contact',
49 'Individual',
50 'Contribution',
51 );
52
53 public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report');
54
55 /**
56 */
57 public function __construct() {
58
59 // Check if CiviCampaign is a) enabled and b) has active campaigns
60 $config = CRM_Core_Config::singleton();
61 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents);
62 if ($campaignEnabled) {
63 $getCampaigns = CRM_Campaign_BAO_Campaign::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
64 $this->activeCampaigns = $getCampaigns['campaigns'];
65 asort($this->activeCampaigns);
66 }
67
68 $this->_columns = array(
69 'civicrm_contact' => array(
70 'dao' => 'CRM_Contact_DAO_Contact',
71 'fields' => array(
72 'display_name_creditor' => array(
73 'title' => ts('Soft Credit Name'),
74 'name' => 'sort_name',
75 'alias' => 'contact_civireport',
76 'required' => TRUE,
77 'no_repeat' => TRUE,
78 ),
79 'id_creditor' => array(
80 'title' => ts('Soft Credit Id'),
81 'name' => 'id',
82 'alias' => 'contact_civireport',
83 'no_display' => TRUE,
84 'required' => TRUE,
85 ),
86 'display_name_constituent' => array(
87 'title' => ts('Contributor Name'),
88 'name' => 'sort_name',
89 'alias' => 'constituentname',
90 'required' => TRUE,
91 ),
92 'id_constituent' => array(
93 'title' => ts('Const Id'),
94 'name' => 'id',
95 'alias' => 'constituentname',
96 'no_display' => TRUE,
97 'required' => TRUE,
98 ),
99 'first_name' => array(
100 'title' => ts('First Name'),
101 ),
102 'middle_name' => array(
103 'title' => ts('Middle Name'),
104 ),
105 'last_name' => array(
106 'title' => ts('Last Name'),
107 ),
108 'gender_id' => array(
109 'title' => ts('Gender'),
110 ),
111 'birth_date' => array(
112 'title' => ts('Birth Date'),
113 ),
114 'age' => array(
115 'title' => ts('Age'),
116 'dbAlias' => 'TIMESTAMPDIFF(YEAR, contact_civireport.birth_date, CURDATE())',
117 ),
118 'contact_type' => array(
119 'title' => ts('Contact Type'),
120 ),
121 'contact_sub_type' => array(
122 'title' => ts('Contact Subtype'),
123 ),
124 ),
125 'grouping' => 'contact-fields',
126 'order_bys' => array(
127 'sort_name' => array(
128 'title' => ts('Last Name, First Name'),
129 'default' => '1',
130 'default_weight' => '0',
131 'default_order' => 'ASC',
132 ),
133 'first_name' => array(
134 'name' => 'first_name',
135 'title' => ts('First Name'),
136 ),
137 'gender_id' => array(
138 'name' => 'gender_id',
139 'title' => ts('Gender'),
140 ),
141 'birth_date' => array(
142 'name' => 'birth_date',
143 'title' => ts('Birth Date'),
144 ),
145 'age_at_event' => array(
146 'name' => 'age_at_event',
147 'title' => ts('Age at Event'),
148 ),
149 'contact_type' => array(
150 'title' => ts('Contact Type'),
151 ),
152 'contact_sub_type' => array(
153 'title' => ts('Contact Subtype'),
154 ),
155 ),
156 'filters' => array(
157 'sort_name' => array(
158 'name' => 'sort_name',
159 'title' => ts('Soft Credit Name'),
160 ),
161 'gender_id' => array(
162 'title' => ts('Gender'),
163 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
164 'options' => CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'),
165 ),
166 'birth_date' => array(
167 'title' => ts('Birth Date'),
168 'operatorType' => CRM_Report_Form::OP_DATE,
169 ),
170 'contact_type' => array(
171 'title' => ts('Contact Type'),
172 ),
173 'contact_sub_type' => array(
174 'title' => ts('Contact Subtype'),
175 ),
176 ),
177 ),
178 'civicrm_email' => array(
179 'dao' => 'CRM_Core_DAO_Email',
180 'fields' => array(
181 'email_creditor' => array(
182 'title' => ts('Soft Credit Email'),
183 'name' => 'email',
184 'alias' => 'emailcredit',
185 'default' => TRUE,
186 'no_repeat' => TRUE,
187 ),
188 'email_constituent' => array(
189 'title' => ts('Contributor\'s Email'),
190 'name' => 'email',
191 'alias' => 'emailconst',
192 ),
193 ),
194 'grouping' => 'contact-fields',
195 ),
196 'civicrm_phone' => array(
197 'dao' => 'CRM_Core_DAO_Phone',
198 'fields' => array(
199 'phone_creditor' => array(
200 'title' => ts('Soft Credit Phone'),
201 'name' => 'phone',
202 'alias' => 'pcredit',
203 'default' => TRUE,
204 ),
205 'phone_constituent' => array(
206 'title' => ts('Contributor\'s Phone'),
207 'name' => 'phone',
208 'alias' => 'pconst',
209 'no_repeat' => TRUE,
210 ),
211 ),
212 'grouping' => 'contact-fields',
213 ),
214 'civicrm_financial_type' => array(
215 'dao' => 'CRM_Financial_DAO_FinancialType',
216 'fields' => array('financial_type' => NULL),
217 'filters' => array(
218 'id' => array(
219 'name' => 'id',
220 'title' => ts('Financial Type'),
221 'type' => CRM_Utils_Type::T_INT,
222 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
223 'options' => CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes(),
224 ),
225 ),
226 'grouping' => 'softcredit-fields',
227 ),
228 'civicrm_contribution' => array(
229 'dao' => 'CRM_Contribute_DAO_Contribution',
230 'fields' => array(
231 'contribution_source' => NULL,
232 'currency' => array(
233 'required' => TRUE,
234 'no_display' => TRUE,
235 ),
236 ),
237 'grouping' => 'softcredit-fields',
238 'filters' => array(
239 'receive_date' => array('operatorType' => CRM_Report_Form::OP_DATE),
240 'currency' => array(
241 'title' => ts('Currency'),
242 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
243 'options' => CRM_Core_OptionGroup::values('currencies_enabled'),
244 'default' => NULL,
245 'type' => CRM_Utils_Type::T_STRING,
246 ),
247 'contribution_status_id' => array(
248 'title' => ts('Contribution Status'),
249 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
250 'options' => CRM_Contribute_PseudoConstant::contributionStatus(),
251 'default' => array(1),
252 ),
253 ),
254 ),
255 'civicrm_contribution_soft' => array(
256 'dao' => 'CRM_Contribute_DAO_ContributionSoft',
257 'fields' => array(
258 'contribution_id' => array(
259 'title' => ts('Contribution ID'),
260 'no_display' => TRUE,
261 'default' => TRUE,
262 ),
263 'amount' => array(
264 'title' => ts('Amount Statistics'),
265 'default' => TRUE,
266 'statistics' => array(
267 'sum' => ts('Aggregate Amount'),
268 'count' => ts('Contributions'),
269 'avg' => ts('Average'),
270 ),
271 ),
272 'id' => array(
273 'default' => TRUE,
274 'no_display' => TRUE,
275 ),
276 'soft_credit_type_id' => array('title' => ts('Soft Credit Type')),
277 ),
278 'filters' => array(
279 'soft_credit_type_id' => array(
280 'title' => ts('Soft Credit Type'),
281 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
282 'options' => CRM_Core_OptionGroup::values('soft_credit_type'),
283 'default' => NULL,
284 'type' => CRM_Utils_Type::T_STRING,
285 ),
286 'amount' => array(
287 'title' => ts('Soft Credit Amount'),
288 ),
289 ),
290 'grouping' => 'softcredit-fields',
291 ),
292 );
293
294 // If we have a campaign, build out the relevant elements
295 if ($campaignEnabled && !empty($this->activeCampaigns)) {
296 $this->_columns['civicrm_contribution']['fields']['campaign_id'] = array(
297 'title' => ts('Campaign'),
298 'default' => 'false',
299 );
300 $this->_columns['civicrm_contribution']['filters']['campaign_id'] = array(
301 'title' => ts('Campaign'),
302 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
303 'options' => $this->activeCampaigns,
304 'type' => CRM_Utils_Type::T_INT,
305 );
306 }
307
308 $this->_groupFilter = TRUE;
309 $this->_tagFilter = TRUE;
310
311 $this->_currencyColumn = 'civicrm_contribution_currency';
312 parent::__construct();
313 }
314
315 public function preProcess() {
316 parent::preProcess();
317 }
318
319 public function select() {
320 $select = array();
321 $this->_columnHeaders = array();
322 foreach ($this->_columns as $tableName => $table) {
323 if (array_key_exists('fields', $table)) {
324 foreach ($table['fields'] as $fieldName => $field) {
325 if (!empty($field['required']) ||
326 !empty($this->_params['fields'][$fieldName])
327 ) {
328
329 // include email column if set
330 if ($tableName == 'civicrm_email') {
331 $this->_emailField = TRUE;
332 $this->_emailFieldCredit = TRUE;
333 }
334 elseif ($tableName == 'civicrm_email_creditor') {
335 $this->_emailFieldCredit = TRUE;
336 }
337
338 // include phone columns if set
339 if ($tableName == 'civicrm_phone') {
340 $this->_phoneField = TRUE;
341 $this->_phoneFieldCredit = TRUE;
342 }
343 elseif ($tableName == 'civicrm_phone_creditor') {
344 $this->_phoneFieldCredit = TRUE;
345 }
346
347 // only include statistics columns if set
348 if (!empty($field['statistics'])) {
349 foreach ($field['statistics'] as $stat => $label) {
350 switch (strtolower($stat)) {
351 case 'sum':
352 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
353 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
354 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
355 $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}";
356 break;
357
358 case 'count':
359 $select[] = "COUNT({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
360 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type::T_INT;
361 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
362 $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}";
363 break;
364
365 case 'avg':
366 $select[] = "ROUND(AVG({$field['dbAlias']}),2) as {$tableName}_{$fieldName}_{$stat}";
367 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
368 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
369 $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}";
370 break;
371 }
372 }
373 }
374 else {
375 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
376 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
377 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
378 }
379 }
380 }
381 }
382 }
383 $this->selectClause = $select;
384
385 $this->_select = 'SELECT ' . implode(', ', $select) . ' ';
386 }
387
388 /**
389 * @param $fields
390 * @param $files
391 * @param $self
392 *
393 * @return array
394 */
395 public static function formRule($fields, $files, $self) {
396 $errors = $grouping = array();
397 return $errors;
398 }
399
400 public function from() {
401 $alias_constituent = 'constituentname';
402 $alias_creditor = 'contact_civireport';
403 $this->_from = "
404 FROM civicrm_contribution {$this->_aliases['civicrm_contribution']}
405 INNER JOIN civicrm_contribution_soft {$this->_aliases['civicrm_contribution_soft']}
406 ON {$this->_aliases['civicrm_contribution_soft']}.contribution_id =
407 {$this->_aliases['civicrm_contribution']}.id
408 INNER JOIN civicrm_contact {$alias_constituent}
409 ON {$this->_aliases['civicrm_contribution']}.contact_id =
410 {$alias_constituent}.id
411 LEFT JOIN civicrm_financial_type {$this->_aliases['civicrm_financial_type']}
412 ON {$this->_aliases['civicrm_contribution']}.financial_type_id =
413 {$this->_aliases['civicrm_financial_type']}.id
414 LEFT JOIN civicrm_contact {$alias_creditor}
415 ON {$this->_aliases['civicrm_contribution_soft']}.contact_id =
416 {$alias_creditor}.id
417 {$this->_aclFrom} ";
418
419 // include Constituent email field if email column is to be included
420 if ($this->_emailField) {
421 $alias = 'emailconst';
422 $this->_from .= "
423 LEFT JOIN civicrm_email {$alias}
424 ON {$alias_constituent}.id =
425 {$alias}.contact_id AND
426 {$alias}.is_primary = 1\n";
427 }
428
429 // include Creditors email field if email column is to be included
430 if ($this->_emailFieldCredit) {
431 $alias = 'emailcredit';
432 $this->_from .= "
433 LEFT JOIN civicrm_email {$alias}
434 ON {$alias_creditor}.id =
435 {$alias}.contact_id AND
436 {$alias}.is_primary = 1\n";
437 }
438
439 // include Constituents phone field if email column is to be included
440 if ($this->_phoneField) {
441 $alias = 'pconst';
442 $this->_from .= "
443 LEFT JOIN civicrm_phone {$alias}
444 ON {$alias_constituent}.id =
445 {$alias}.contact_id AND
446 {$alias}.is_primary = 1\n";
447 }
448
449 // include Creditors phone field if email column is to be included
450 if ($this->_phoneFieldCredit) {
451 $alias = 'pcredit';
452 $this->_from .= "
453 LEFT JOIN civicrm_phone pcredit
454 ON {$alias_creditor}.id =
455 {$alias}.contact_id AND
456 {$alias}.is_primary = 1\n";
457 }
458 }
459
460 public function groupBy() {
461 $this->_rollup = 'WITH ROLLUP';
462 $this->_select = CRM_Contact_BAO_Query::appendAnyValueToSelect($this->selectClause, array("{$this->_aliases['civicrm_contribution_soft']}.contact_id", "constituentname.id"));
463 $this->_groupBy = "
464 GROUP BY {$this->_aliases['civicrm_contribution_soft']}.contact_id, constituentname.id {$this->_rollup}";
465 }
466
467 public function where() {
468 parent::where();
469 $this->_where .= " AND {$this->_aliases['civicrm_contribution']}.is_test = 0 ";
470 }
471
472 /**
473 * @param $rows
474 *
475 * @return array
476 */
477 public function statistics(&$rows) {
478 $statistics = parent::statistics($rows);
479
480 $select = "
481 SELECT COUNT({$this->_aliases['civicrm_contribution_soft']}.amount ) as count,
482 SUM({$this->_aliases['civicrm_contribution_soft']}.amount ) as amount,
483 ROUND(AVG({$this->_aliases['civicrm_contribution_soft']}.amount), 2) as avg,
484 {$this->_aliases['civicrm_contribution']}.currency as currency
485 ";
486
487 $sql = "{$select} {$this->_from} {$this->_where}
488 GROUP BY {$this->_aliases['civicrm_contribution']}.currency
489 ";
490
491 $dao = CRM_Core_DAO::executeQuery($sql);
492 $count = 0;
493 $totalAmount = $average = array();
494 while ($dao->fetch()) {
495 $totalAmount[] = CRM_Utils_Money::format($dao->amount, $dao->currency) . '(' .
496 $dao->count . ')';
497 $average[] = CRM_Utils_Money::format($dao->avg, $dao->currency);
498 $count += $dao->count;
499 }
500 $statistics['counts']['amount'] = array(
501 'title' => ts('Total Amount'),
502 'value' => implode(', ', $totalAmount),
503 'type' => CRM_Utils_Type::T_STRING,
504 );
505 $statistics['counts']['count'] = array(
506 'title' => ts('Total Contributions'),
507 'value' => $count,
508 );
509 $statistics['counts']['avg'] = array(
510 'title' => ts('Average'),
511 'value' => implode(', ', $average),
512 'type' => CRM_Utils_Type::T_STRING,
513 );
514
515 return $statistics;
516 }
517
518 public function postProcess() {
519 $this->beginPostProcess();
520
521 $this->buildACLClause(array('constituentname', 'contact_civireport'));
522 $sql = $this->buildQuery();
523
524 $dao = CRM_Core_DAO::executeQuery($sql);
525 $rows = $graphRows = array();
526 $count = 0;
527 while ($dao->fetch()) {
528 $row = array();
529 foreach ($this->_columnHeaders as $key => $value) {
530 $row[$key] = $dao->$key;
531 }
532 $rows[] = $row;
533 }
534 $this->formatDisplay($rows);
535
536 // to hide the contact ID field from getting displayed
537 unset($this->_columnHeaders['civicrm_contact_id_constituent']);
538 unset($this->_columnHeaders['civicrm_contact_id_creditor']);
539
540 // assign variables to templates
541 $this->doTemplateAssignment($rows);
542 $this->endPostProcess($rows);
543 }
544
545 /**
546 * Alter display of rows.
547 *
548 * Iterate through the rows retrieved via SQL and make changes for display purposes,
549 * such as rendering contacts as links.
550 *
551 * @param array $rows
552 * Rows generated by SQL, with an array for each row.
553 */
554 public function alterDisplay(&$rows) {
555 $entryFound = FALSE;
556 $dispname_flag = $phone_flag = $email_flag = 0;
557 $prev_email = $prev_dispname = $prev_phone = NULL;
558
559 foreach ($rows as $rowNum => $row) {
560 // Link constituent (contributor) to contribution detail
561 if (array_key_exists('civicrm_contact_display_name_constituent', $row) &&
562 array_key_exists('civicrm_contact_id_constituent', $row)
563 ) {
564
565 $url = CRM_Report_Utils_Report::getNextUrl('contribute/detail',
566 'reset=1&force=1&id_op=eq&id_value=' .
567 $row['civicrm_contact_id_constituent'],
568 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
569 );
570 $rows[$rowNum]['civicrm_contact_display_name_constituent_link'] = $url;
571 $rows[$rowNum]['civicrm_contact_display_name_constituent_hover'] = ts('List all direct contribution(s) from this contact.');
572 $entryFound = TRUE;
573 }
574
575 // convert soft credit contact name to link
576 if (array_key_exists('civicrm_contact_display_name_creditor', $row) &&
577 !empty($rows[$rowNum]['civicrm_contact_display_name_creditor']) &&
578 array_key_exists('civicrm_contact_id_creditor', $row)
579 ) {
580 $url = CRM_Utils_System::url("civicrm/contact/view",
581 'reset=1&cid=' . $row['civicrm_contact_id_creditor'],
582 $this->_absoluteUrl
583 );
584 $rows[$rowNum]['civicrm_contact_display_name_creditor_link'] = $url;
585 $rows[$rowNum]['civicrm_contact_display_name_creditor_hover'] = ts("view contact summary");
586 }
587
588 // make subtotals look nicer
589 if (array_key_exists('civicrm_contact_id_constituent', $row) &&
590 !$row['civicrm_contact_id_constituent']
591 ) {
592 $this->fixSubTotalDisplay($rows[$rowNum], $this->_statFields);
593 $entryFound = TRUE;
594 }
595
596 // convert campaign_id to campaign title
597 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
598 if ($value = $row['civicrm_contribution_campaign_id']) {
599 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns[$value];
600 $entryFound = TRUE;
601 }
602 }
603
604 //convert soft_credit_type_id into label
605 if (array_key_exists('civicrm_contribution_soft_soft_credit_type_id', $rows[$rowNum])) {
606 $rows[$rowNum]['civicrm_contribution_soft_soft_credit_type_id'] = CRM_Core_PseudoConstant::getLabel(
607 'CRM_Contribute_BAO_ContributionSoft',
608 'soft_credit_type_id',
609 $row['civicrm_contribution_soft_soft_credit_type_id']
610 );
611 }
612
613 //handle gender
614 if (array_key_exists('civicrm_contact_gender_id', $row)) {
615 if ($value = $row['civicrm_contact_gender_id']) {
616 $gender = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id');
617 $rows[$rowNum]['civicrm_contact_gender_id'] = $gender[$value];
618 }
619 $entryFound = TRUE;
620 }
621
622 // display birthday in the configured custom format
623 if (array_key_exists('civicrm_contact_birth_date', $row)) {
624 $birthDate = $row['civicrm_contact_birth_date'];
625 if ($birthDate) {
626 $rows[$rowNum]['civicrm_contact_birth_date'] = CRM_Utils_Date::customFormat($birthDate, '%Y%m%d');
627 }
628 $entryFound = TRUE;
629 }
630
631 // skip looking further in rows, if first row itself doesn't
632 // have the column we need
633 if (!$entryFound) {
634 break;
635 }
636 }
637
638 $this->removeDuplicates($rows);
639 }
640
641 }