4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2014
36 class CRM_Report_Form_Contribute_HouseholdSummary
extends CRM_Report_Form
{
38 protected $_addressField = FALSE;
40 protected $_emailField = FALSE;
42 public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report');
44 protected $_summary = NULL;
46 function __construct() {
47 self
::validRelationships();
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
);
57 $this->_columns
= array(
58 'civicrm_contact_household' =>
60 'dao' => 'CRM_Contact_DAO_Contact',
64 array('title' => ts('Household Name'),
74 'title' => ts('Contact Type'),
78 'title' => ts('Contact SubType'),
84 array('title' => ts('Household Name')),
86 'grouping' => 'household-fields',
88 'civicrm_relationship' =>
90 'dao' => 'CRM_Contact_DAO_Relationship',
93 'relationship_type_id' =>
94 array('title' => ts('Relationship Type'),
99 'relationship_type_id' =>
101 'title' => ts('Relationship Type'),
102 'type' => CRM_Utils_Type
::T_INT
,
103 'operatorType' => CRM_Report_Form
::OP_SELECT
,
104 'options' => $this->relationTypes
,
105 'default' => array(1),
108 'grouping' => 'household-fields',
112 'dao' => 'CRM_Contact_DAO_Contact',
116 array('title' => ts('Contact Name'),
121 'no_display' => TRUE,
125 'grouping' => 'contact-fields',
127 'civicrm_contribution' =>
129 'dao' => 'CRM_Contribute_DAO_Contribution',
131 array('total_amount' => array('title' => ts('Amount'),
135 'no_display' => TRUE,
138 'contribution_status_id' => array(
139 'title' => 'Contribution Status',
142 'check_number' => array('title' => ts('Check Number'),
146 'no_display' => TRUE,
149 'receive_date' => array('default' => TRUE),
150 'receipt_date' => NULL,
155 array('operatorType' => CRM_Report_Form
::OP_DATE
),
157 array('title' => ts('Amount Between')),
159 array('title' => 'Currency',
160 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
161 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
163 'type' => CRM_Utils_Type
::T_STRING
,
165 'contribution_status_id' =>
166 array('title' => ts('Contribution Status'),
167 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
168 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
169 'default' => array(1),
172 'grouping' => 'contri-fields',
176 'dao' => 'CRM_Core_DAO_Address',
179 'street_address' => NULL,
181 'postal_code' => NULL,
182 'state_province_id' =>
183 array('title' => ts('State/Province'),
186 array('title' => ts('Country'),
189 'grouping' => 'contact-fields',
193 'dao' => 'CRM_Core_DAO_Email',
195 array('email' => NULL),
196 'grouping' => 'contact-fields',
200 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
201 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
202 'title' => 'Campaign',
203 'default' => 'false',
205 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array('title' => ts('Campaign'),
206 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
207 'options' => $this->activeCampaigns
,
210 $this->_currencyColumn
= 'civicrm_contribution_currency';
211 parent
::__construct();
214 function preProcess() {
215 parent
::preProcess();
219 $this->_columnHeaders
= $select = array();
220 foreach ($this->_columns
as $tableName => $table) {
221 if (array_key_exists('fields', $table)) {
222 foreach ($table['fields'] as $fieldName => $field) {
223 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
224 if ($tableName == 'civicrm_address') {
225 $this->_addressField
= TRUE;
227 elseif ($tableName == 'civicrm_email') {
228 $this->_emailField
= TRUE;
231 if (!empty($field['statistics'])) {
232 foreach ($field['statistics'] as $stat => $label) {
233 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}_{$stat}";
234 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
235 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
236 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
240 $select[] = "{$table['alias']}.{$fieldName} as {$tableName}_{$fieldName}";
242 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
243 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
249 $this->_select
= "SELECT " . implode(', ', $select) . " ";
256 FROM civicrm_relationship {$this->_aliases['civicrm_relationship']}
257 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact_household']} ON
258 ({$this->_aliases['civicrm_contact_household']}.id = {$this->_aliases['civicrm_relationship']}.$this->householdContact AND {$this->_aliases['civicrm_contact_household']}.contact_type='Household')
259 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']} ON
260 ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_relationship']}.$this->otherContact )
262 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} ON
263 ({$this->_aliases['civicrm_contribution']}.contact_id = {$this->_aliases['civicrm_relationship']}.$this->otherContact ) AND {$this->_aliases['civicrm_contribution']}.is_test = 0 ";
265 if ($this->_addressField
) {
267 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} ON
268 {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
269 {$this->_aliases['civicrm_address']}.is_primary = 1\n ";
271 if ($this->_emailField
) {
273 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} ON
274 {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
275 {$this->_aliases['civicrm_email']}.is_primary = 1\n ";
281 foreach ($this->_columns
as $tableName => $table) {
282 if (array_key_exists('filters', $table)) {
283 foreach ($table['filters'] as $fieldName => $field) {
285 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
286 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
287 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
288 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
290 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
293 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
295 if ($fieldName == 'relationship_type_id') {
296 $clause = "{$this->_aliases['civicrm_relationship']}.relationship_type_id=" . $this->relationshipId
;
299 $clause = $this->whereClause($field,
301 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
302 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
303 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
309 if (!empty($clause)) {
310 $clauses[] = $clause;
316 if (empty($clauses)) {
317 $this->_where
= "WHERE ( 1 )";
320 $this->_where
= "WHERE " . implode(' AND ', $clauses);
323 if ($this->_aclWhere
) {
324 $this->_where
.= " AND {$this->_aclWhere} ";
329 $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 ";
333 $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";
336 function statistics(&$rows) {
337 $statistics = parent
::statistics($rows);
339 //hack filter display for relationship type
340 $type = substr($this->_params
['relationship_type_id_value'], -3);
341 foreach ($statistics['filters'] as $id => $value) {
343 $value['title'] == 'Relationship Type' &&
344 isset($this->relationTypes
[$this->relationshipId
. '_' . $type])
346 $statistics['filters'][$id]['value'] = 'Is equal to ' . $this->relationTypes
[$this->relationshipId
. '_' . $type];
352 function postProcess() {
354 $this->beginPostProcess();
355 $getRelationship = $this->_params
['relationship_type_id_value'];
356 $type = substr($getRelationship, -3);
357 $this->relationshipId
= intval((substr($getRelationship, 0, strpos($getRelationship, '_'))));
358 if ($type == 'b_a') {
359 $this->householdContact
= 'contact_id_b';
360 $this->otherContact
= 'contact_id_a';
363 $this->householdContact
= 'contact_id_a';
364 $this->otherContact
= 'contact_id_b';
366 $this->buildACLClause(array($this->_aliases
['civicrm_contact'], $this->_aliases
['civicrm_contact_household']));
367 $sql = $this->buildQuery(TRUE);
370 $this->buildRows($sql, $rows);
371 $this->formatDisplay($rows);
372 $this->doTemplateAssignment($rows);
373 $this->endPostProcess($rows);
376 function validRelationships() {
377 $this->relationTypes
= $relationTypes = array();
379 $params = array('contact_type_b' => 'Household', 'version' => 3);
380 $typesA = civicrm_api('relationship_type', 'get', $params);
381 if (empty($typesA['is_error'])) {
382 foreach ($typesA['values'] as $rel) {
383 $relationTypes[$rel['id']][$rel['id'] . '_b_a'] = $rel['label_b_a'];
386 $params = array('contact_type_a' => 'Household', 'version' => 3);
387 $typesB = civicrm_api('relationship_type', 'get', $params);
388 if (empty($typesB['is_error'])) {
389 foreach ($typesB['values'] as $rel) {
390 $relationTypes[$rel['id']][$rel['id'] . '_a_b'] = $rel['label_a_b'];
391 //$this->relationTypes[$rel['id'].'_a_b'] = $rel['label_a_b'];
395 ksort($relationTypes);
396 foreach ($relationTypes as $relationship) {
397 foreach ($relationship as $index => $label) {
398 $this->relationTypes
[$index] = $label;
403 function alterDisplay(&$rows) {
404 // custom code to alter rows
405 $type = substr($this->_params
['relationship_type_id_value'], -3);
408 $flagHousehold = $flagContact = 0;
409 foreach ($rows as $rowNum => $row) {
411 //replace retionship id by relationship name
412 if (array_key_exists('civicrm_relationship_relationship_type_id', $row)) {
413 if ($value = $row['civicrm_relationship_relationship_type_id']) {
414 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = $this->relationTypes
[$value . '_' . $type];
419 //remove duplicate Organization names
420 if (array_key_exists('civicrm_contact_household_household_name', $row) && $this->_outputMode
!= 'csv') {
421 if ($value = $row['civicrm_contact_household_household_name']) {
423 $priviousHousehold = $value;
426 if ($priviousHousehold == $value) {
428 $priviousHousehold = $value;
432 $priviousHousehold = $value;
436 if ($flagHousehold == 1) {
437 $rows[$rowNum]['civicrm_contact_household_household_name'] = "";
440 $url = CRM_Utils_System
::url('civicrm/contact/view',
441 'reset=1&cid=' . $rows[$rowNum]['civicrm_contact_household_id'],
445 $rows[$rowNum]['civicrm_contact_household_household_name'] = "<a href='$url' title='" . ts('View contact summary for this househould') . "'>" . $value . '</a>';
451 //remove duplicate Contact names and relationship type
452 if (array_key_exists('civicrm_contact_id', $row) && $this->_outputMode
!= 'csv') {
453 if ($value = $row['civicrm_contact_id']) {
455 $priviousContact = $value;
458 if ($priviousContact == $value) {
460 $priviousContact = $value;
464 $priviousContact = $value;
468 if ($flagContact == 1 && $flagHousehold == 1) {
469 $rows[$rowNum]['civicrm_contact_sort_name'] = "";
470 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = "";
477 if (array_key_exists('civicrm_contribution_contribution_status_id', $row)) {
478 if ($value = $row['civicrm_contribution_contribution_status_id']) {
479 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = CRM_Contribute_PseudoConstant
::contributionStatus($value);
483 // handle state province
484 if (array_key_exists('civicrm_address_state_province_id', $row)) {
485 if ($value = $row['civicrm_address_state_province_id']) {
486 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($value, FALSE);
492 if (array_key_exists('civicrm_address_country_id', $row)) {
493 if ($value = $row['civicrm_address_country_id']) {
494 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
499 // convert display name to links
500 if (array_key_exists('civicrm_contact_sort_name', $row) &&
501 $rows[$rowNum]['civicrm_contact_sort_name'] &&
502 array_key_exists('civicrm_contact_id', $row)
504 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
505 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
506 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
508 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
509 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View contribution details for this individual');
514 if (!empty($row['civicrm_contribution_total_amount'])) {
515 $row['civicrm_contribution_total_amount'] = CRM_Utils_Money
::format($row['civicrm_contribution_total_amount'], $row['civicrm_contribution_currency']);
518 // Contribution amount links to view contribution
519 if (($value = CRM_Utils_Array
::value('civicrm_contribution_total_amount', $row)) &&
520 CRM_Core_Permission
::check('access CiviContribute')
522 $url = CRM_Utils_System
::url("civicrm/contact/view/contribution",
523 "reset=1&id=" . $row['civicrm_contribution_id'] . "&cid=" . $row['civicrm_contact_id'] . "&action=view&context=contribution&selectedChild=contribute",
526 $rows[$rowNum]['civicrm_contribution_total_amount_link'] = $url;
527 $rows[$rowNum]['civicrm_contribution_total_amount_hover'] = ts("View this contribution.");
531 // convert campaign_id to campaign title
532 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
533 if ($value = $row['civicrm_contribution_campaign_id']) {
534 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
539 // skip looking further in rows, if first row itself doesn't