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_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;
47 * Organisation contact ie. 'contact_id_b' or 'contact_id_a'
50 protected $orgContact;
53 * Related Contact ie. 'contact_id_b' or 'contact_id_a'
56 protected $otherContact;
64 function __construct() {
65 self
::validRelationships();
66 $config = CRM_Core_Config
::singleton();
67 $campaignEnabled = in_array('CiviCampaign', $config->enableComponents
);
68 if ($campaignEnabled) {
69 $getCampaigns = CRM_Campaign_BAO_Campaign
::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
70 $this->activeCampaigns
= $getCampaigns['campaigns'];
71 asort($this->activeCampaigns
);
74 $this->_columns
= array(
75 'civicrm_contact_organization' => array(
76 'dao' => 'CRM_Contact_DAO_Contact',
78 'organization_name' => array(
79 'title' => ts('Organization Name'),
87 'contact_type' => array(
88 'title' => ts('Contact Type'),
90 'contact_sub_type' => array(
91 'title' => ts('Contact SubType'),
95 'organization_name' => array('title' => ts('Organization Name')),
97 'grouping' => 'organization-fields',
99 'civicrm_relationship' => array(
100 'dao' => 'CRM_Contact_DAO_Relationship',
102 'relationship_type_id' => array(
103 'title' => ts('Relationship Type'),
107 'relationship_type_id' => array(
108 'title' => ts('Relationship Type'),
109 'type' => CRM_Utils_Type
::T_INT
,
110 'operatorType' => CRM_Report_Form
::OP_SELECT
,
111 'options' => $this->relationTypes
,
112 'default' => key($this->relationTypes
),
115 'grouping' => 'organization-fields',
117 'civicrm_contact' => array(
118 'dao' => 'CRM_Contact_DAO_Contact',
120 'sort_name' => array(
121 'title' => ts('Contact Name'),
125 'no_display' => TRUE,
129 'grouping' => 'contact-fields',
131 'civicrm_contribution' => array(
132 'dao' => 'CRM_Contribute_DAO_Contribution',
134 'total_amount' => array(
135 'title' => ts('Amount'),
139 'no_display' => TRUE,
142 'contribution_status_id' => array(
143 'title' => 'Contribution Status',
146 'check_number' => array(
147 'title' => ts('Check Number'),
151 'no_display' => TRUE,
154 'receive_date' => array('default' => TRUE),
155 'receipt_date' => NULL,
158 'receive_date' => array('operatorType' => CRM_Report_Form
::OP_DATE
),
159 'total_amount' => array('title' => ts('Amount Between')),
161 'title' => 'Currency',
162 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
163 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
165 'type' => CRM_Utils_Type
::T_STRING
,
167 'contribution_status_id' => array(
168 'title' => ts('Contribution Status'),
169 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
170 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
171 'default' => array(1),
174 'grouping' => 'contri-fields',
176 'civicrm_address' => array(
177 'dao' => 'CRM_Core_DAO_Address',
179 'street_address' => NULL,
181 'postal_code' => NULL,
182 'state_province_id' => array(
183 'title' => ts('State/Province'),
185 'country_id' => array(
186 'title' => ts('Country'),
189 'grouping' => 'contact-fields',
191 'civicrm_email' => array(
192 'dao' => 'CRM_Core_DAO_Email',
193 'fields' => array('email' => NULL),
194 'grouping' => 'contact-fields',
198 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
199 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
200 'title' => 'Campaign',
201 'default' => 'false',
203 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array(
204 'title' => ts('Campaign'),
205 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
206 '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[] = "{$field['dbAlias']} 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_organization']} ON
258 ({$this->_aliases['civicrm_contact_organization']}.id = {$this->_aliases['civicrm_relationship']}.$this->orgContact AND {$this->_aliases['civicrm_contact_organization']}.contact_type='Organization')
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->orgContact, {$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_organization']}.organization_name, {$this->_aliases['civicrm_relationship']}.$this->orgContact, {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_relationship']}.$this->otherContact";
341 function statistics(&$rows) {
342 $statistics = parent
::statistics($rows);
344 //hack filter display for relationship type
345 $type = substr($this->_params
['relationship_type_id_value'], -3);
346 foreach ($statistics['filters'] as $id => $value) {
347 if ($value['title'] == 'Relationship Type' && !empty($id)) {
348 $statistics['filters'][$id]['value'] = 'Is equal to ' .
349 $this->relationTypes
[$this->relationshipId
. '_' . $type];
355 function postProcess() {
356 $this->beginPostProcess();
357 $this->buildACLClause(array($this->_aliases
['civicrm_contact'], $this->_aliases
['civicrm_contact_organization']));
358 $sql = $this->buildQuery(TRUE);
360 $this->buildRows($sql, $rows);
361 $this->formatDisplay($rows);
362 $this->doTemplateAssignment($rows);
363 $this->endPostProcess($rows);
367 * Set variables to be accessed by API and form layer in processing
369 function beginPostProcessCommon() {
370 $getRelationship = $this->_params
['relationship_type_id_value'];
371 $type = substr($getRelationship, -3);
372 $this->relationshipId
= intval((substr($getRelationship, 0, strpos($getRelationship, '_'))));
373 if ($type == 'b_a') {
374 $this->orgContact
= 'contact_id_b';
375 $this->otherContact
= 'contact_id_a';
378 $this->orgContact
= 'contact_id_a';
379 $this->otherContact
= 'contact_id_b';
383 function validRelationships() {
384 $this->relationTypes
= $relationTypes = array();
386 $params = array('contact_type_b' => 'Organization', 'version' => 3);
387 $typesA = civicrm_api('relationship_type', 'get', $params);
389 if (empty($typesA['is_error'])) {
390 foreach ($typesA['values'] as $rel) {
391 $relationTypes[$rel['id']][$rel['id'] . '_b_a'] = $rel['label_b_a'];
395 $params = array('contact_type_a' => 'Organization', 'version' => 3);
396 $typesB = civicrm_api('relationship_type', 'get', $params);
398 if (empty($typesB['is_error'])) {
399 foreach ($typesB['values'] as $rel) {
400 $relationTypes[$rel['id']][$rel['id'] . '_a_b'] = $rel['label_a_b'];
404 ksort($relationTypes);
405 foreach ($relationTypes as $relationship) {
406 foreach ($relationship as $index => $label) {
407 $this->relationTypes
[$index] = $label;
415 function alterDisplay(&$rows) {
416 // custom code to alter rows
417 $type = substr($this->_params
['relationship_type_id_value'], -3);
420 $flagOrganization = $flagContact = 0;
422 foreach ($rows as $rowNum => $row) {
424 //replace retionship id by relationship name
425 if (array_key_exists('civicrm_relationship_relationship_type_id', $row)) {
426 if ($value = $row['civicrm_relationship_relationship_type_id']) {
427 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = $this->relationTypes
[$value . '_' . $type];
432 //remove duplicate Organization names
433 if (array_key_exists('civicrm_contact_organization_id', $row) && $this->_outputMode
!= 'csv') {
434 if ($value = $row['civicrm_contact_organization_id']) {
436 $previousOrganization = $value;
439 if ($previousOrganization == $value) {
440 $flagOrganization = 1;
441 $previousOrganization = $value;
444 $flagOrganization = 0;
445 $previousOrganization = $value;
449 if ($flagOrganization == 1) {
450 foreach ($row as $colName => $colVal) {
451 if (in_array($colName, $this->_noRepeats
)) {
452 unset($rows[$rowNum][$colName]);
460 // convert Organization display name to links
461 if (array_key_exists('civicrm_contact_organization_organization_name', $row) && !empty($rows[$rowNum]
462 ['civicrm_contact_organization_organization_name']) &&
463 array_key_exists('civicrm_contact_organization_id', $row)
465 $url = CRM_Utils_System
::url('civicrm/contact/view',
467 $rows[$rowNum]['civicrm_contact_organization_id'],
471 $rows[$rowNum]['civicrm_contact_organization_organization_name_link'] = $url;
472 $rows[$rowNum]['civicrm_contact_organization_organization_name_hover'] = ts('View contact summary for this organization.');
475 //remove duplicate Contact names and relationship type
476 if (array_key_exists('civicrm_contact_id', $row) && $this->_outputMode
!= 'csv') {
477 if ($value = $row['civicrm_contact_id']) {
479 $previousContact = $value;
482 if ($previousContact == $value) {
484 $previousContact = $value;
488 $previousContact = $value;
492 if ($flagContact == 1 && $flagOrganization == 1) {
493 $rows[$rowNum]['civicrm_contact_sort_name'] = "";
494 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = "";
501 if (array_key_exists('civicrm_contribution_contribution_status_id', $row)) {
502 if ($value = $row['civicrm_contribution_contribution_status_id']) {
503 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = CRM_Contribute_PseudoConstant
::contributionStatus($value);
507 // handle state province
508 if (array_key_exists('civicrm_address_state_province_id', $row)) {
509 if ($value = $row['civicrm_address_state_province_id']) {
510 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($value, FALSE);
516 if (array_key_exists('civicrm_address_country_id', $row)) {
517 if ($value = $row['civicrm_address_country_id']) {
518 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
523 // convert Individual display name to links
524 if (array_key_exists('civicrm_contact_sort_name', $row) &&
525 $rows[$rowNum]['civicrm_contact_sort_name'] &&
526 array_key_exists('civicrm_contact_id', $row)
528 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
529 'reset=1&force=1&id_op=eq&id_value=' .
530 $row['civicrm_contact_id'],
531 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
533 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
534 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View contribution details for this individual');
539 // Contribution amount links to view contribution
540 if (($value = CRM_Utils_Array
::value('civicrm_contribution_total_amount', $row)) &&
541 CRM_Core_Permission
::check('access CiviContribute')
543 $url = CRM_Utils_System
::url('civicrm/contact/view/contribution',
544 "reset=1&id=" . $row['civicrm_contribution_id'] . "&cid=" . $row['civicrm_contact_id'] .
545 "&action=view&context=contribution&selectedChild=contribute",
548 $rows[$rowNum]['civicrm_contribution_total_amount_link'] = $url;
549 $rows[$rowNum]['civicrm_contribution_total_amount_hover'] = ts('View this contribution.');
553 // convert campaign_id to campaign title
554 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
555 if ($value = $row['civicrm_contribution_campaign_id']) {
556 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
561 // skip looking further in rows, if first row itself doesn't