5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
12 | CiviCRM is free software; you can copy, modify, and distribute it |
13 | under the terms of the GNU Affero General Public License |
14 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
16 | CiviCRM is distributed in the hope that it will be useful, but |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | See the GNU Affero General Public License for more details. |
21 | You should have received a copy of the GNU Affero General Public |
22 | License and the CiviCRM Licensing Exception along |
23 | with this program; if not, contact CiviCRM LLC |
24 | at info[AT]civicrm[DOT]org. If you have questions about the |
25 | GNU Affero General Public License or the licensing of CiviCRM, |
26 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
27 +--------------------------------------------------------------------+
33 * @copyright CiviCRM LLC (c) 2004-2013
37 class CRM_Report_Form_Contribute_HouseholdSummary
extends CRM_Report_Form
{
39 protected $_addressField = FALSE;
41 protected $_emailField = FALSE;
43 public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report');
45 protected $_summary = NULL;
47 function __construct() {
48 self
::validRelationships();
50 $config = CRM_Core_Config
::singleton();
51 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents
);
52 if ($campaignEnabled) {
53 $getCampaigns = CRM_Campaign_BAO_Campaign
::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
54 $this->activeCampaigns
= $getCampaigns['campaigns'];
55 asort($this->activeCampaigns
);
58 $this->_columns
= array(
59 'civicrm_contact_household' =>
61 'dao' => 'CRM_Contact_DAO_Contact',
65 array('title' => ts('Household Name'),
77 array('title' => ts('Household Name')),
79 'grouping' => 'household-fields',
81 'civicrm_relationship' =>
83 'dao' => 'CRM_Contact_DAO_Relationship',
86 'relationship_type_id' =>
87 array('title' => ts('Relationship Type'),
92 'relationship_type_id' =>
94 'title' => ts('Relationship Type'),
95 'type' => CRM_Utils_Type
::T_INT
,
96 'operatorType' => CRM_Report_Form
::OP_SELECT
,
97 'options' => $this->relationTypes
,
98 'default' => array(1),
101 'grouping' => 'household-fields',
105 'dao' => 'CRM_Contact_DAO_Contact',
109 array('title' => ts('Contact Name'),
114 'no_display' => TRUE,
118 'grouping' => 'contact-fields',
120 'civicrm_contribution' =>
122 'dao' => 'CRM_Contribute_DAO_Contribution',
124 array('total_amount' => array('title' => ts('Amount'),
128 'no_display' => TRUE,
131 'contribution_status_id' => array(
132 'title' => 'Contribution Status',
137 'no_display' => TRUE,
140 'receive_date' => array('default' => TRUE),
141 'receipt_date' => NULL,
146 array('operatorType' => CRM_Report_Form
::OP_DATE
),
148 array('title' => ts('Amount Between')),
150 array('title' => 'Currency',
151 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
152 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
154 'type' => CRM_Utils_Type
::T_STRING
,
156 'contribution_status_id' =>
157 array('title' => ts('Contribution Status'),
158 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
159 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
160 'default' => array(1),
163 'grouping' => 'contri-fields',
167 'dao' => 'CRM_Core_DAO_Address',
170 'street_address' => NULL,
172 'postal_code' => NULL,
173 'state_province_id' =>
174 array('title' => ts('State/Province'),
177 array('title' => ts('Country'),
180 'grouping' => 'contact-fields',
184 'dao' => 'CRM_Core_DAO_Email',
186 array('email' => NULL),
187 'grouping' => 'contact-fields',
191 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
192 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
193 'title' => 'Campaign',
194 'default' => 'false',
196 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array('title' => ts('Campaign'),
197 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
198 'options' => $this->activeCampaigns
,
201 $this->_currencyColumn
= 'civicrm_contribution_currency';
202 parent
::__construct();
205 function preProcess() {
206 parent
::preProcess();
210 $this->_columnHeaders
= $select = array();
211 foreach ($this->_columns
as $tableName => $table) {
212 if (array_key_exists('fields', $table)) {
213 foreach ($table['fields'] as $fieldName => $field) {
214 if (CRM_Utils_Array
::value('required', $field) ||
215 CRM_Utils_Array
::value($fieldName, $this->_params
['fields'])
217 if ($tableName == 'civicrm_address') {
218 $this->_addressField
= TRUE;
220 elseif ($tableName == 'civicrm_email') {
221 $this->_emailField
= TRUE;
224 if (CRM_Utils_Array
::value('statistics', $field)) {
225 foreach ($field['statistics'] as $stat => $label) {
226 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}_{$stat}";
227 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
228 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
229 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
233 $select[] = "{$table['alias']}.{$fieldName} as {$tableName}_{$fieldName}";
235 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
236 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
242 $this->_select
= "SELECT " . implode(', ', $select) . " ";
249 FROM civicrm_relationship {$this->_aliases['civicrm_relationship']}
250 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact_household']} ON
251 ({$this->_aliases['civicrm_contact_household']}.id = {$this->_aliases['civicrm_relationship']}.$this->householdContact AND {$this->_aliases['civicrm_contact_household']}.contact_type='Household')
252 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']} ON
253 ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_relationship']}.$this->otherContact )
255 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} ON
256 ({$this->_aliases['civicrm_contribution']}.contact_id = {$this->_aliases['civicrm_relationship']}.$this->otherContact ) AND {$this->_aliases['civicrm_contribution']}.is_test = 0 ";
258 if ($this->_addressField
) {
260 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} ON
261 {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
262 {$this->_aliases['civicrm_address']}.is_primary = 1\n ";
264 if ($this->_emailField
) {
266 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} ON
267 {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
268 {$this->_aliases['civicrm_email']}.is_primary = 1\n ";
274 foreach ($this->_columns
as $tableName => $table) {
275 if (array_key_exists('filters', $table)) {
276 foreach ($table['filters'] as $fieldName => $field) {
278 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
279 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
280 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
281 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
283 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
286 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
288 if ($fieldName == 'relationship_type_id') {
289 $clause = "{$this->_aliases['civicrm_relationship']}.relationship_type_id=" . $this->relationshipId
;
292 $clause = $this->whereClause($field,
294 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
295 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
296 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
302 if (!empty($clause)) {
303 $clauses[] = $clause;
309 if (empty($clauses)) {
310 $this->_where
= "WHERE ( 1 )";
313 $this->_where
= "WHERE " . implode(' AND ', $clauses);
316 if ($this->_aclWhere
) {
317 $this->_where
.= " AND {$this->_aclWhere} ";
322 $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 ";
326 $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";
329 function statistics(&$rows) {
330 $statistics = parent
::statistics($rows);
332 //hack filter display for relationship type
333 $type = substr($this->_params
['relationship_type_id_value'], -3);
334 foreach ($statistics['filters'] as $id => $value) {
336 $value['title'] == 'Relationship Type' &&
337 isset($this->relationTypes
[$this->relationshipId
. '_' . $type])
339 $statistics['filters'][$id]['value'] = 'Is equal to ' . $this->relationTypes
[$this->relationshipId
. '_' . $type];
345 function postProcess() {
347 $this->beginPostProcess();
348 $getRelationship = $this->_params
['relationship_type_id_value'];
349 $type = substr($getRelationship, -3);
350 $this->relationshipId
= intval((substr($getRelationship, 0, strpos($getRelationship, '_'))));
351 if ($type == 'b_a') {
352 $this->householdContact
= 'contact_id_b';
353 $this->otherContact
= 'contact_id_a';
356 $this->householdContact
= 'contact_id_a';
357 $this->otherContact
= 'contact_id_b';
359 $this->buildACLClause(array($this->_aliases
['civicrm_contact'], $this->_aliases
['civicrm_contact_household']));
360 $sql = $this->buildQuery(TRUE);
363 $this->buildRows($sql, $rows);
364 $this->formatDisplay($rows);
365 $this->doTemplateAssignment($rows);
366 $this->endPostProcess($rows);
369 function validRelationships() {
370 $this->relationTypes
= $relationTypes = array();
372 $params = array('contact_type_b' => 'Household', 'version' => 3);
373 $typesA = civicrm_api('relationship_type', 'get', $params);
374 if (!CRM_Utils_Array
::value('is_error', $typesA)) {
375 foreach ($typesA['values'] as $rel) {
376 $relationTypes[$rel['id']][$rel['id'] . '_b_a'] = $rel['label_b_a'];
379 $params = array('contact_type_a' => 'Household', 'version' => 3);
380 $typesB = civicrm_api('relationship_type', 'get', $params);
381 if (!CRM_Utils_Array
::value('is_error', $typesB)) {
382 foreach ($typesB['values'] as $rel) {
383 $relationTypes[$rel['id']][$rel['id'] . '_a_b'] = $rel['label_a_b'];
384 //$this->relationTypes[$rel['id'].'_a_b'] = $rel['label_a_b'];
388 ksort($relationTypes);
389 foreach ($relationTypes as $relationship) {
390 foreach ($relationship as $index => $label) {
391 $this->relationTypes
[$index] = $label;
396 function alterDisplay(&$rows) {
397 // custom code to alter rows
398 $type = substr($this->_params
['relationship_type_id_value'], -3);
401 $flagHousehold = $flagContact = 0;
402 foreach ($rows as $rowNum => $row) {
404 //replace retionship id by relationship name
405 if (array_key_exists('civicrm_relationship_relationship_type_id', $row)) {
406 if ($value = $row['civicrm_relationship_relationship_type_id']) {
407 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = $this->relationTypes
[$value . '_' . $type];
412 //remove duplicate Organization names
413 if (array_key_exists('civicrm_contact_household_household_name', $row) && $this->_outputMode
!= 'csv') {
414 if ($value = $row['civicrm_contact_household_household_name']) {
416 $priviousHousehold = $value;
419 if ($priviousHousehold == $value) {
421 $priviousHousehold = $value;
425 $priviousHousehold = $value;
429 if ($flagHousehold == 1) {
430 $rows[$rowNum]['civicrm_contact_household_household_name'] = "";
433 $url = CRM_Utils_System
::url('civicrm/contact/view',
434 'reset=1&cid=' . $rows[$rowNum]['civicrm_contact_household_id'],
438 $rows[$rowNum]['civicrm_contact_household_household_name'] = "<a href='$url' title='" . ts('View contact summary for this househould') . "'>" . $value . '</a>';
444 //remove duplicate Contact names and relationship type
445 if (array_key_exists('civicrm_contact_id', $row) && $this->_outputMode
!= 'csv') {
446 if ($value = $row['civicrm_contact_id']) {
448 $priviousContact = $value;
451 if ($priviousContact == $value) {
453 $priviousContact = $value;
457 $priviousContact = $value;
461 if ($flagContact == 1 && $flagHousehold == 1) {
462 $rows[$rowNum]['civicrm_contact_sort_name'] = "";
463 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = "";
470 if (array_key_exists('civicrm_contribution_contribution_status_id', $row)) {
471 if ($value = $row['civicrm_contribution_contribution_status_id']) {
472 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = CRM_Contribute_PseudoConstant
::contributionStatus($value);
476 // handle state province
477 if (array_key_exists('civicrm_address_state_province_id', $row)) {
478 if ($value = $row['civicrm_address_state_province_id']) {
479 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($value, FALSE);
485 if (array_key_exists('civicrm_address_country_id', $row)) {
486 if ($value = $row['civicrm_address_country_id']) {
487 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
492 // convert display name to links
493 if (array_key_exists('civicrm_contact_sort_name', $row) &&
494 $rows[$rowNum]['civicrm_contact_sort_name'] &&
495 array_key_exists('civicrm_contact_id', $row)
497 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
498 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
499 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
501 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
502 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View contribution details for this individual');
507 if (CRM_Utils_Array
::value('civicrm_contribution_total_amount', $row)) {
508 $row['civicrm_contribution_total_amount'] = CRM_Utils_Money
::format($row['civicrm_contribution_total_amount'], $row['civicrm_contribution_currency']);
511 // Contribution amount links to view contribution
512 if (($value = CRM_Utils_Array
::value('civicrm_contribution_total_amount', $row)) &&
513 CRM_Core_Permission
::check('access CiviContribute')
515 $url = CRM_Utils_System
::url("civicrm/contact/view/contribution",
516 "reset=1&id=" . $row['civicrm_contribution_id'] . "&cid=" . $row['civicrm_contact_id'] . "&action=view&context=contribution&selectedChild=contribute",
519 $rows[$rowNum]['civicrm_contribution_total_amount_link'] = $url;
520 $rows[$rowNum]['civicrm_contribution_total_amount_hover'] = ts("View this contribution.");
524 // convert campaign_id to campaign title
525 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
526 if ($value = $row['civicrm_contribution_campaign_id']) {
527 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
532 // skip looking further in rows, if first row itself doesn't