4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.4 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
36 class CRM_Report_Form_Contribute_OrganizationSummary
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();
48 $config = CRM_Core_Config
::singleton();
49 $campaignEnabled = in_array('CiviCampaign', $config->enableComponents
);
50 if ($campaignEnabled) {
51 $getCampaigns = CRM_Campaign_BAO_Campaign
::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
52 $this->activeCampaigns
= $getCampaigns['campaigns'];
53 asort($this->activeCampaigns
);
56 $this->_columns
= array(
57 'civicrm_contact_organization' =>
59 'dao' => 'CRM_Contact_DAO_Contact',
62 'organization_name' =>
63 array('title' => ts('Organization Name'),
74 'title' => ts('Contact Type'),
78 'title' => ts('Contact SubType'),
83 'organization_name' =>
84 array('title' => ts('Organization Name')),
86 'grouping' => 'organization-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' => 'organization-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
,
211 $this->_currencyColumn
= 'civicrm_contribution_currency';
212 parent
::__construct();
215 function preProcess() {
216 parent
::preProcess();
220 $this->_columnHeaders
= $select = array();
221 foreach ($this->_columns
as $tableName => $table) {
222 if (array_key_exists('fields', $table)) {
223 foreach ($table['fields'] as $fieldName => $field) {
224 if (CRM_Utils_Array
::value('required', $field) ||
225 CRM_Utils_Array
::value($fieldName, $this->_params
['fields'])
227 if ($tableName == 'civicrm_address') {
228 $this->_addressField
= TRUE;
230 elseif ($tableName == 'civicrm_email') {
231 $this->_emailField
= TRUE;
234 if (CRM_Utils_Array
::value('statistics', $field)) {
235 foreach ($field['statistics'] as $stat => $label) {
236 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}_{$stat}";
237 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
238 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
239 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
243 $select[] = "{$table['alias']}.{$fieldName} as {$tableName}_{$fieldName}";
245 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
246 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
252 $this->_select
= "SELECT " . implode(', ', $select) . " ";
259 FROM civicrm_relationship {$this->_aliases['civicrm_relationship']}
260 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact_organization']} ON
261 ({$this->_aliases['civicrm_contact_organization']}.id = {$this->_aliases['civicrm_relationship']}.$this->orgContact AND {$this->_aliases['civicrm_contact_organization']}.contact_type='Organization')
262 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']} ON
263 ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_relationship']}.$this->otherContact )
265 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} ON
266 ({$this->_aliases['civicrm_contribution']}.contact_id = {$this->_aliases['civicrm_relationship']}.$this->otherContact ) AND {$this->_aliases['civicrm_contribution']}.is_test = 0 ";
268 if ($this->_addressField
) {
270 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} ON
271 {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
272 {$this->_aliases['civicrm_address']}.is_primary = 1\n ";
274 if ($this->_emailField
) {
276 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} ON
277 {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
278 {$this->_aliases['civicrm_email']}.is_primary = 1\n ";
284 foreach ($this->_columns
as $tableName => $table) {
285 if (array_key_exists('filters', $table)) {
286 foreach ($table['filters'] as $fieldName => $field) {
288 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
289 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
290 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
291 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
293 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
296 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
298 if ($fieldName == 'relationship_type_id') {
299 $clause = "{$this->_aliases['civicrm_relationship']}.relationship_type_id=" . $this->relationshipId
;
302 $clause = $this->whereClause($field,
304 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
305 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
306 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
312 if (!empty($clause)) {
313 $clauses[] = $clause;
319 if (empty($clauses)) {
320 $this->_where
= "WHERE ( 1 )";
323 $this->_where
= "WHERE " . implode(' AND ', $clauses);
326 if ($this->_aclWhere
) {
327 $this->_where
.= " AND {$this->_aclWhere} ";
332 $this->_groupBy
= " GROUP BY {$this->_aliases['civicrm_relationship']}.$this->orgContact, {$this->_aliases['civicrm_relationship']}.$this->otherContact , {$this->_aliases['civicrm_contribution']}.id, {$this->_aliases['civicrm_relationship']}.relationship_type_id ";
336 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_contact_organization']}.organization_name, {$this->_aliases['civicrm_relationship']}.$this->orgContact, {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_relationship']}.$this->otherContact";
339 function statistics(&$rows) {
340 $statistics = parent
::statistics($rows);
342 //hack filter display for relationship type
343 $type = substr($this->_params
['relationship_type_id_value'], -3);
344 foreach ($statistics['filters'] as $id => $value) {
345 if ($value['title'] == 'Relationship 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->orgContact
= 'contact_id_b';
360 $this->otherContact
= 'contact_id_a';
363 $this->orgContact
= 'contact_id_a';
364 $this->otherContact
= 'contact_id_b';
367 $this->buildACLClause(array($this->_aliases
['civicrm_contact'], $this->_aliases
['civicrm_contact_organization']));
368 $sql = $this->buildQuery(TRUE);
371 $this->buildRows($sql, $rows);
372 $this->formatDisplay($rows);
373 $this->doTemplateAssignment($rows);
374 $this->endPostProcess($rows);
377 function validRelationships() {
378 $this->relationTypes
= $relationTypes = array();
380 $params = array('contact_type_b' => 'Organization', 'version' => 3);
381 $typesA = &civicrm_api('relationship_type', 'get', $params);
383 if (!CRM_Utils_Array
::value('is_error', $typesA)) {
384 foreach ($typesA['values'] as $rel) {
385 $relationTypes[$rel['id']][$rel['id'] . '_b_a'] = $rel['label_b_a'];
389 $params = array('contact_type_a' => 'Organization', 'version' => 3);
390 $typesB = &civicrm_api('relationship_type', 'get', $params);
392 if (!CRM_Utils_Array
::value('is_error', $typesB)) {
393 foreach ($typesB['values'] as $rel) {
394 $relationTypes[$rel['id']][$rel['id'] . '_a_b'] = $rel['label_a_b'];
398 ksort($relationTypes);
399 foreach ($relationTypes as $relationship) {
400 foreach ($relationship as $index => $label) {
401 $this->relationTypes
[$index] = $label;
406 function alterDisplay(&$rows) {
407 // custom code to alter rows
408 $type = substr($this->_params
['relationship_type_id_value'], -3);
411 $flagOrganization = $flagContact = 0;
413 foreach ($rows as $rowNum => $row) {
415 //replace retionship id by relationship name
416 if (array_key_exists('civicrm_relationship_relationship_type_id', $row)) {
417 if ($value = $row['civicrm_relationship_relationship_type_id']) {
418 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = $this->relationTypes
[$value . '_' . $type];
423 //remove duplicate Organization names
424 if (array_key_exists('civicrm_contact_organization_id', $row) && $this->_outputMode
!= 'csv') {
425 if ($value = $row['civicrm_contact_organization_id']) {
427 $previousOrganization = $value;
430 if ($previousOrganization == $value) {
431 $flagOrganization = 1;
432 $previousOrganization = $value;
435 $flagOrganization = 0;
436 $previousOrganization = $value;
440 if ($flagOrganization == 1) {
441 foreach ($row as $colName => $colVal) {
442 if (in_array($colName, $this->_noRepeats
)) {
443 unset($rows[$rowNum][$colName]);
451 // convert Organization display name to links
452 if (array_key_exists('civicrm_contact_organization_organization_name', $row) &&
453 CRM_Utils_Array
::value('civicrm_contact_organization_organization_name',
456 array_key_exists('civicrm_contact_organization_id', $row)
458 $url = CRM_Utils_System
::url('civicrm/contact/view',
460 $rows[$rowNum]['civicrm_contact_organization_id'],
464 $rows[$rowNum]['civicrm_contact_organization_organization_name_link'] = $url;
465 $rows[$rowNum]['civicrm_contact_organization_organization_name_hover'] = ts('View contact summary for this organization.');
468 //remove duplicate Contact names and relationship type
469 if (array_key_exists('civicrm_contact_id', $row) && $this->_outputMode
!= 'csv') {
470 if ($value = $row['civicrm_contact_id']) {
472 $previousContact = $value;
475 if ($previousContact == $value) {
477 $previousContact = $value;
481 $previousContact = $value;
485 if ($flagContact == 1 && $flagOrganization == 1) {
486 $rows[$rowNum]['civicrm_contact_sort_name'] = "";
487 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = "";
494 if (array_key_exists('civicrm_contribution_contribution_status_id', $row)) {
495 if ($value = $row['civicrm_contribution_contribution_status_id']) {
496 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = CRM_Contribute_PseudoConstant
::contributionStatus($value);
500 // handle state province
501 if (array_key_exists('civicrm_address_state_province_id', $row)) {
502 if ($value = $row['civicrm_address_state_province_id']) {
503 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($value, FALSE);
509 if (array_key_exists('civicrm_address_country_id', $row)) {
510 if ($value = $row['civicrm_address_country_id']) {
511 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
516 // convert Individual display name to links
517 if (array_key_exists('civicrm_contact_sort_name', $row) &&
518 $rows[$rowNum]['civicrm_contact_sort_name'] &&
519 array_key_exists('civicrm_contact_id', $row)
521 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
522 'reset=1&force=1&id_op=eq&id_value=' .
523 $row['civicrm_contact_id'],
524 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
526 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
527 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View contribution details for this individual');
532 // Contribution amount links to view contribution
533 if (($value = CRM_Utils_Array
::value('civicrm_contribution_total_amount', $row)) &&
534 CRM_Core_Permission
::check('access CiviContribute')
536 $url = CRM_Utils_System
::url('civicrm/contact/view/contribution',
537 "reset=1&id=" . $row['civicrm_contribution_id'] . "&cid=" . $row['civicrm_contact_id'] . "&action=view&context=contribution&selectedChild=contribute",
540 $rows[$rowNum]['civicrm_contribution_total_amount_link'] = $url;
541 $rows[$rowNum]['civicrm_contribution_total_amount_hover'] = ts('View this contribution.');
545 // convert campaign_id to campaign title
546 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
547 if ($value = $row['civicrm_contribution_campaign_id']) {
548 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
553 // skip looking further in rows, if first row itself doesn't