CRM-12595 fix formatting in CRM/Report files
[civicrm-core.git] / CRM / Report / Form / Contribute / HouseholdSummary.php
CommitLineData
6a488035 1<?php
6a488035
TO
2
3/*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27*/
28
29/**
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2013
33 * $Id$
34 *
35 */
36class CRM_Report_Form_Contribute_HouseholdSummary extends CRM_Report_Form {
37
38 protected $_addressField = FALSE;
39
40 protected $_emailField = FALSE;
41
42 public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report');
43
eb3b4c04 44 protected $_summary = NULL;
2f4c2f5d 45
eb3b4c04 46 function __construct() {
6a488035
TO
47 self::validRelationships();
48
49 $config = CRM_Core_Config::singleton();
50 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents);
51 if ($campaignEnabled) {
52 $getCampaigns = CRM_Campaign_BAO_Campaign::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
53 $this->activeCampaigns = $getCampaigns['campaigns'];
54 asort($this->activeCampaigns);
55 }
56
57 $this->_columns = array(
58 'civicrm_contact_household' =>
59 array(
60 'dao' => 'CRM_Contact_DAO_Contact',
61 'fields' =>
62 array(
63 'household_name' =>
64 array('title' => ts('Household Name'),
65 'required' => TRUE,
66 ),
67 'id' =>
68 array(
69 'no_display' => TRUE,
70 'required' => TRUE,
71 ),
72 ),
73 'filters' =>
74 array(
75 'household_name' =>
76 array('title' => ts('Household Name')),
77 ),
78 'grouping' => 'household-fields',
79 ),
80 'civicrm_relationship' =>
81 array(
82 'dao' => 'CRM_Contact_DAO_Relationship',
83 'fields' =>
84 array(
85 'relationship_type_id' =>
86 array('title' => ts('Relationship Type'),
87 ),
88 ),
89 'filters' =>
90 array(
91 'relationship_type_id' =>
92 array(
93 'title' => ts('Relationship Type'),
94 'type' => CRM_Utils_Type::T_INT,
95 'operatorType' => CRM_Report_Form::OP_SELECT,
96 'options' => $this->relationTypes,
97 'default' => array(1),
98 ),
99 ),
100 'grouping' => 'household-fields',
101 ),
102 'civicrm_contact' =>
103 array(
104 'dao' => 'CRM_Contact_DAO_Contact',
105 'fields' =>
106 array(
107 'sort_name' =>
108 array('title' => ts('Contact Name'),
109 'required' => TRUE,
110 ),
111 'id' =>
112 array(
113 'no_display' => TRUE,
114 'required' => TRUE,
115 ),
116 ),
117 'grouping' => 'contact-fields',
118 ),
119 'civicrm_contribution' =>
120 array(
121 'dao' => 'CRM_Contribute_DAO_Contribution',
122 'fields' =>
123 array('total_amount' => array('title' => ts('Amount'),
124 'required' => TRUE,
125 ),
126 'id' => array(
127 'no_display' => TRUE,
128 'required' => TRUE,
129 ),
130 'contribution_status_id' => array(
131 'title' => 'Contribution Status',
132 'default' => TRUE,
133 ),
eb3b4c04 134 'currency' => array(
135 'required' => TRUE,
136 'no_display' => TRUE,
137 ),
6a488035
TO
138 'trxn_id' => NULL,
139 'receive_date' => array('default' => TRUE),
140 'receipt_date' => NULL,
141 ),
142 'filters' =>
143 array(
144 'receive_date' =>
145 array('operatorType' => CRM_Report_Form::OP_DATE),
146 'total_amount' =>
147 array('title' => ts('Amount Between')),
eb3b4c04 148 'currency' =>
149 array('title' => 'Currency',
150 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
151 'options' => CRM_Core_OptionGroup::values('currencies_enabled'),
d1b0931b 152 'default' => NULL,
eb3b4c04 153 'type' => CRM_Utils_Type::T_STRING,
154 ),
6a488035
TO
155 'contribution_status_id' =>
156 array('title' => ts('Contribution Status'),
157 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
158 'options' => CRM_Contribute_PseudoConstant::contributionStatus(),
159 'default' => array(1),
160 ),
161 ),
162 'grouping' => 'contri-fields',
163 ),
164 'civicrm_address' =>
165 array(
166 'dao' => 'CRM_Core_DAO_Address',
167 'fields' =>
168 array(
169 'street_address' => NULL,
170 'city' => NULL,
171 'postal_code' => NULL,
172 'state_province_id' =>
173 array('title' => ts('State/Province'),
174 ),
175 'country_id' =>
176 array('title' => ts('Country'),
177 ),
178 ),
179 'grouping' => 'contact-fields',
180 ),
181 'civicrm_email' =>
182 array(
183 'dao' => 'CRM_Core_DAO_Email',
184 'fields' =>
185 array('email' => NULL),
186 'grouping' => 'contact-fields',
187 ),
188 );
189
190 if ($campaignEnabled && !empty($this->activeCampaigns)) {
191 $this->_columns['civicrm_contribution']['fields']['campaign_id'] = array(
192 'title' => 'Campaign',
193 'default' => 'false',
194 );
195 $this->_columns['civicrm_contribution']['filters']['campaign_id'] = array('title' => ts('Campaign'),
196 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
197 'options' => $this->activeCampaigns,
198 );
199 }
eb3b4c04 200 $this->_currencyColumn = 'civicrm_contribution_currency';
6a488035
TO
201 parent::__construct();
202 }
203
204 function preProcess() {
205 parent::preProcess();
206 }
207
208 function select() {
209 $this->_columnHeaders = $select = array();
210 foreach ($this->_columns as $tableName => $table) {
211 if (array_key_exists('fields', $table)) {
212 foreach ($table['fields'] as $fieldName => $field) {
213 if (CRM_Utils_Array::value('required', $field) ||
214 CRM_Utils_Array::value($fieldName, $this->_params['fields'])
215 ) {
216 if ($tableName == 'civicrm_address') {
217 $this->_addressField = TRUE;
218 }
219 elseif ($tableName == 'civicrm_email') {
220 $this->_emailField = TRUE;
221 }
222
223 if (CRM_Utils_Array::value('statistics', $field)) {
224 foreach ($field['statistics'] as $stat => $label) {
225 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}_{$stat}";
226 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
227 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
228 $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}";
229 }
230 }
231 else {
232 $select[] = "{$table['alias']}.{$fieldName} as {$tableName}_{$fieldName}";
233
234 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
235 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
236 }
237 }
238 }
239 }
240 }
241 $this->_select = "SELECT " . implode(', ', $select) . " ";
242 }
243
244 function from() {
245
246 $this->_from = NULL;
247 $this->_from = "
39eb89f4
DL
248 FROM civicrm_relationship {$this->_aliases['civicrm_relationship']}
249 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact_household']} ON
6a488035 250 ({$this->_aliases['civicrm_contact_household']}.id = {$this->_aliases['civicrm_relationship']}.$this->householdContact AND {$this->_aliases['civicrm_contact_household']}.contact_type='Household')
39eb89f4
DL
251 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']} ON
252 ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_relationship']}.$this->otherContact )
6a488035
TO
253 {$this->_aclFrom}
254 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} ON
255 ({$this->_aliases['civicrm_contribution']}.contact_id = {$this->_aliases['civicrm_relationship']}.$this->otherContact ) AND {$this->_aliases['civicrm_contribution']}.is_test = 0 ";
256
257 if ($this->_addressField) {
39eb89f4
DL
258 $this->_from .= "
259 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} ON
260 {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
6a488035
TO
261 {$this->_aliases['civicrm_address']}.is_primary = 1\n ";
262 }
263 if ($this->_emailField) {
264 $this->_from .= "
39eb89f4
DL
265 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} ON
266 {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
6a488035
TO
267 {$this->_aliases['civicrm_email']}.is_primary = 1\n ";
268 }
269 }
270
271 function where() {
272 $clauses = array();
273 foreach ($this->_columns as $tableName => $table) {
274 if (array_key_exists('filters', $table)) {
275 foreach ($table['filters'] as $fieldName => $field) {
276 $clause = NULL;
277 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
278 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
279 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
280 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
281
282 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
283 }
284 else {
285 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
286 if ($op) {
287 if ($fieldName == 'relationship_type_id') {
288 $clause = "{$this->_aliases['civicrm_relationship']}.relationship_type_id=" . $this->relationshipId;
289 }
290 else {
291 $clause = $this->whereClause($field,
292 $op,
293 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
294 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
295 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
296 );
297 }
298 }
299 }
300
301 if (!empty($clause)) {
302 $clauses[] = $clause;
303 }
304 }
305 }
306 }
307
308 if (empty($clauses)) {
309 $this->_where = "WHERE ( 1 )";
310 }
311 else {
312 $this->_where = "WHERE " . implode(' AND ', $clauses);
313 }
314
315 if ($this->_aclWhere) {
316 $this->_where .= " AND {$this->_aclWhere} ";
317 }
318 }
319
320 function groupBy() {
321 $this->_groupBy = " GROUP BY {$this->_aliases['civicrm_relationship']}.$this->householdContact, {$this->_aliases['civicrm_relationship']}.$this->otherContact , {$this->_aliases['civicrm_contribution']}.id, {$this->_aliases['civicrm_relationship']}.relationship_type_id ";
322 }
323
324 function orderBy() {
325 $this->_orderBy = " ORDER BY {$this->_aliases['civicrm_contact_household']}.household_name, {$this->_aliases['civicrm_relationship']}.$this->householdContact, {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_relationship']}.$this->otherContact";
326 }
327
328 function statistics(&$rows) {
329 $statistics = parent::statistics($rows);
330
331 //hack filter display for relationship type
332 $type = substr($this->_params['relationship_type_id_value'], -3);
333 foreach ($statistics['filters'] as $id => $value) {
39eb89f4
DL
334 if (
335 $value['title'] == 'Relationship Type' &&
336 isset($this->relationTypes[$this->relationshipId . '_' . $type])
337 ) {
6a488035
TO
338 $statistics['filters'][$id]['value'] = 'Is equal to ' . $this->relationTypes[$this->relationshipId . '_' . $type];
339 }
340 }
341 return $statistics;
342 }
343
344 function postProcess() {
345
346 $this->beginPostProcess();
347 $getRelationship = $this->_params['relationship_type_id_value'];
348 $type = substr($getRelationship, -3);
349 $this->relationshipId = intval((substr($getRelationship, 0, strpos($getRelationship, '_'))));
350 if ($type == 'b_a') {
351 $this->householdContact = 'contact_id_b';
352 $this->otherContact = 'contact_id_a';
353 }
354 else {
355 $this->householdContact = 'contact_id_a';
356 $this->otherContact = 'contact_id_b';
357 }
358 $this->buildACLClause(array($this->_aliases['civicrm_contact'], $this->_aliases['civicrm_contact_household']));
359 $sql = $this->buildQuery(TRUE);
360 $rows = array();
361
362 $this->buildRows($sql, $rows);
363 $this->formatDisplay($rows);
364 $this->doTemplateAssignment($rows);
365 $this->endPostProcess($rows);
366 }
367
368 function validRelationships() {
369 $this->relationTypes = $relationTypes = array();
370
371 $params = array('contact_type_b' => 'Household', 'version' => 3);
39eb89f4 372 $typesA = civicrm_api('relationship_type', 'get', $params);
6a488035
TO
373 if (!CRM_Utils_Array::value('is_error', $typesA)) {
374 foreach ($typesA['values'] as $rel) {
375 $relationTypes[$rel['id']][$rel['id'] . '_b_a'] = $rel['label_b_a'];
376 }
377 }
378 $params = array('contact_type_a' => 'Household', 'version' => 3);
39eb89f4 379 $typesB = civicrm_api('relationship_type', 'get', $params);
6a488035
TO
380 if (!CRM_Utils_Array::value('is_error', $typesB)) {
381 foreach ($typesB['values'] as $rel) {
382 $relationTypes[$rel['id']][$rel['id'] . '_a_b'] = $rel['label_a_b'];
383 //$this->relationTypes[$rel['id'].'_a_b'] = $rel['label_a_b'];
384 }
385 }
386
387 ksort($relationTypes);
388 foreach ($relationTypes as $relationship) {
389 foreach ($relationship as $index => $label) {
390 $this->relationTypes[$index] = $label;
391 }
392 }
393 }
394
395 function alterDisplay(&$rows) {
396 // custom code to alter rows
397 $type = substr($this->_params['relationship_type_id_value'], -3);
398
399 $entryFound = FALSE;
400 $flagHousehold = $flagContact = 0;
6a488035
TO
401 foreach ($rows as $rowNum => $row) {
402
403 //replace retionship id by relationship name
404 if (array_key_exists('civicrm_relationship_relationship_type_id', $row)) {
405 if ($value = $row['civicrm_relationship_relationship_type_id']) {
406 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = $this->relationTypes[$value . '_' . $type];
407 $entryFound = TRUE;
408 }
409 }
410
411 //remove duplicate Organization names
412 if (array_key_exists('civicrm_contact_household_household_name', $row) && $this->_outputMode != 'csv') {
413 if ($value = $row['civicrm_contact_household_household_name']) {
414 if ($rowNum == 0) {
415 $priviousHousehold = $value;
416 }
417 else {
418 if ($priviousHousehold == $value) {
419 $flagHousehold = 1;
420 $priviousHousehold = $value;
421 }
422 else {
423 $flagHousehold = 0;
424 $priviousHousehold = $value;
425 }
426 }
427
428 if ($flagHousehold == 1) {
429 $rows[$rowNum]['civicrm_contact_household_household_name'] = "";
430 }
431 else {
432 $url = CRM_Utils_System::url('civicrm/contact/view',
433 'reset=1&cid=' . $rows[$rowNum]['civicrm_contact_household_id'],
434 $this->_absoluteUrl
435 );
436
437 $rows[$rowNum]['civicrm_contact_household_household_name'] = "<a href='$url' title='" . ts('View contact summary for this househould') . "'>" . $value . '</a>';
438 }
439 $entryFound = TRUE;
440 }
441 }
442
443 //remove duplicate Contact names and relationship type
444 if (array_key_exists('civicrm_contact_id', $row) && $this->_outputMode != 'csv') {
445 if ($value = $row['civicrm_contact_id']) {
446 if ($rowNum == 0) {
447 $priviousContact = $value;
448 }
449 else {
450 if ($priviousContact == $value) {
451 $flagContact = 1;
452 $priviousContact = $value;
453 }
454 else {
455 $flagContact = 0;
456 $priviousContact = $value;
457 }
458 }
459
460 if ($flagContact == 1 && $flagHousehold == 1) {
461 $rows[$rowNum]['civicrm_contact_sort_name'] = "";
462 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = "";
463 }
464
465 $entryFound = TRUE;
466 }
467 }
468
469 if (array_key_exists('civicrm_contribution_contribution_status_id', $row)) {
470 if ($value = $row['civicrm_contribution_contribution_status_id']) {
471 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = CRM_Contribute_PseudoConstant::contributionStatus($value);
472 }
473 }
474
475 // handle state province
476 if (array_key_exists('civicrm_address_state_province_id', $row)) {
477 if ($value = $row['civicrm_address_state_province_id']) {
478 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($value, FALSE);
479 }
480 $entryFound = TRUE;
481 }
482
483 // handle country
484 if (array_key_exists('civicrm_address_country_id', $row)) {
485 if ($value = $row['civicrm_address_country_id']) {
486 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country($value, FALSE);
487 }
488 $entryFound = TRUE;
489 }
490
491 // convert display name to links
492 if (array_key_exists('civicrm_contact_sort_name', $row) &&
493 $rows[$rowNum]['civicrm_contact_sort_name'] &&
494 array_key_exists('civicrm_contact_id', $row)
495 ) {
496 $url = CRM_Report_Utils_Report::getNextUrl('contribute/detail',
497 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
498 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
499 );
500 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
501 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View contribution details for this individual');
502
503 $entryFound = TRUE;
504 }
505
eb3b4c04 506 if (CRM_Utils_Array::value('civicrm_contribution_total_amount', $row)) {
507 $row['civicrm_contribution_total_amount'] = CRM_Utils_Money::format($row['civicrm_contribution_total_amount'], $row['civicrm_contribution_currency']);
508 }
509
6a488035
TO
510 // Contribution amount links to view contribution
511 if (($value = CRM_Utils_Array::value('civicrm_contribution_total_amount', $row)) &&
512 CRM_Core_Permission::check('access CiviContribute')
513 ) {
514 $url = CRM_Utils_System::url("civicrm/contact/view/contribution",
515 "reset=1&id=" . $row['civicrm_contribution_id'] . "&cid=" . $row['civicrm_contact_id'] . "&action=view&context=contribution&selectedChild=contribute",
516 $this->_absoluteUrl
517 );
518 $rows[$rowNum]['civicrm_contribution_total_amount_link'] = $url;
519 $rows[$rowNum]['civicrm_contribution_total_amount_hover'] = ts("View this contribution.");
520 $entryFound = TRUE;
521 }
522
523 // convert campaign_id to campaign title
524 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
525 if ($value = $row['civicrm_contribution_campaign_id']) {
526 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns[$value];
527 $entryFound = TRUE;
528 }
529 }
530
531 // skip looking further in rows, if first row itself doesn't
532 if (!$entryFound) {
533 break;
534 }
535 $lastKey = $rowNum;
536 }
537 }
538}
539