3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
35 class CRM_Report_Form_Contribute_OrganizationSummary
extends CRM_Report_Form
{
37 protected $_addressField = FALSE;
39 protected $_emailField = FALSE;
41 public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report');
43 protected $_summary = NULL;
46 * Organisation contact ie. 'contact_id_b' or 'contact_id_a'
49 protected $orgContact;
52 * Related Contact ie. 'contact_id_b' or 'contact_id_a'
55 protected $otherContact;
61 public function __construct() {
62 self
::validRelationships();
63 $config = CRM_Core_Config
::singleton();
64 $campaignEnabled = in_array('CiviCampaign', $config->enableComponents
);
65 if ($campaignEnabled) {
66 $getCampaigns = CRM_Campaign_BAO_Campaign
::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
67 $this->activeCampaigns
= $getCampaigns['campaigns'];
68 asort($this->activeCampaigns
);
71 $this->_columns
= array(
72 'civicrm_contact_organization' => array(
73 'dao' => 'CRM_Contact_DAO_Contact',
75 'organization_name' => array(
76 'title' => ts('Organization Name'),
84 'contact_type' => array(
85 'title' => ts('Contact Type'),
87 'contact_sub_type' => array(
88 'title' => ts('Contact Subtype'),
92 'organization_name' => array('title' => ts('Organization Name')),
94 'grouping' => 'organization-fields',
96 'civicrm_relationship' => array(
97 'dao' => 'CRM_Contact_DAO_Relationship',
99 'relationship_type_id' => array(
100 'title' => ts('Relationship Type'),
104 'relationship_type_id' => array(
105 'title' => ts('Relationship Type'),
106 'type' => CRM_Utils_Type
::T_INT
,
107 'operatorType' => CRM_Report_Form
::OP_SELECT
,
108 'options' => $this->relationTypes
,
109 'default' => key($this->relationTypes
),
112 'grouping' => 'organization-fields',
114 'civicrm_contact' => array(
115 'dao' => 'CRM_Contact_DAO_Contact',
117 'sort_name' => array(
118 'title' => ts('Contact Name'),
122 'no_display' => TRUE,
126 'grouping' => 'contact-fields',
128 'civicrm_contribution' => array(
129 'dao' => 'CRM_Contribute_DAO_Contribution',
131 'total_amount' => array(
132 'title' => ts('Amount'),
136 'no_display' => TRUE,
139 'contribution_status_id' => array(
140 'title' => 'Contribution Status',
143 'check_number' => array(
144 'title' => ts('Check Number'),
148 'no_display' => TRUE,
151 'receive_date' => array('default' => TRUE),
152 'receipt_date' => NULL,
155 'receive_date' => array('operatorType' => CRM_Report_Form
::OP_DATE
),
156 'total_amount' => array('title' => ts('Amount Between')),
158 'title' => 'Currency',
159 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
160 'options' => CRM_Core_OptionGroup
::values('currencies_enabled'),
162 'type' => CRM_Utils_Type
::T_STRING
,
164 'contribution_status_id' => array(
165 'title' => ts('Contribution Status'),
166 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
167 'options' => CRM_Contribute_PseudoConstant
::contributionStatus(),
168 'default' => array(1),
171 'grouping' => 'contri-fields',
173 'civicrm_address' => array(
174 'dao' => 'CRM_Core_DAO_Address',
176 'street_address' => NULL,
178 'postal_code' => NULL,
179 'state_province_id' => array(
180 'title' => ts('State/Province'),
182 'country_id' => array(
183 'title' => ts('Country'),
186 'grouping' => 'contact-fields',
188 'civicrm_email' => array(
189 'dao' => 'CRM_Core_DAO_Email',
190 'fields' => array('email' => NULL),
191 'grouping' => 'contact-fields',
195 if ($campaignEnabled && !empty($this->activeCampaigns
)) {
196 $this->_columns
['civicrm_contribution']['fields']['campaign_id'] = array(
197 'title' => 'Campaign',
198 'default' => 'false',
200 $this->_columns
['civicrm_contribution']['filters']['campaign_id'] = array(
201 'title' => ts('Campaign'),
202 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
203 'options' => $this->activeCampaigns
,
207 $this->_currencyColumn
= 'civicrm_contribution_currency';
208 parent
::__construct();
211 public function preProcess() {
212 parent
::preProcess();
215 public function select() {
216 $this->_columnHeaders
= $select = array();
217 foreach ($this->_columns
as $tableName => $table) {
218 if (array_key_exists('fields', $table)) {
219 foreach ($table['fields'] as $fieldName => $field) {
220 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
221 if ($tableName == 'civicrm_address') {
222 $this->_addressField
= TRUE;
224 elseif ($tableName == 'civicrm_email') {
225 $this->_emailField
= TRUE;
228 if (!empty($field['statistics'])) {
229 foreach ($field['statistics'] as $stat => $label) {
230 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}_{$stat}";
231 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
232 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
233 $this->_statFields
[] = "{$tableName}_{$fieldName}_{$stat}";
237 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
239 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
240 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
246 $this->_select
= "SELECT " . implode(', ', $select) . " ";
249 public function from() {
253 FROM civicrm_relationship {$this->_aliases['civicrm_relationship']}
254 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact_organization']} ON
255 ({$this->_aliases['civicrm_contact_organization']}.id = {$this->_aliases['civicrm_relationship']}.$this->orgContact AND {$this->_aliases['civicrm_contact_organization']}.contact_type='Organization')
256 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']} ON
257 ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_relationship']}.$this->otherContact )
259 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} ON
260 ({$this->_aliases['civicrm_contribution']}.contact_id = {$this->_aliases['civicrm_relationship']}.$this->otherContact ) AND {$this->_aliases['civicrm_contribution']}.is_test = 0 ";
262 if ($this->_addressField
) {
264 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} ON
265 {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
266 {$this->_aliases['civicrm_address']}.is_primary = 1\n ";
268 if ($this->_emailField
) {
270 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} ON
271 {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
272 {$this->_aliases['civicrm_email']}.is_primary = 1\n ";
276 public function where() {
278 foreach ($this->_columns
as $tableName => $table) {
279 if (array_key_exists('filters', $table)) {
280 foreach ($table['filters'] as $fieldName => $field) {
282 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
283 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
284 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
285 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
287 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
290 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
292 if ($fieldName == 'relationship_type_id') {
293 $clause = "{$this->_aliases['civicrm_relationship']}.relationship_type_id=" . $this->relationshipId
;
296 $clause = $this->whereClause($field,
298 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
299 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
300 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
306 if (!empty($clause)) {
307 $clauses[] = $clause;
313 if (empty($clauses)) {
314 $this->_where
= "WHERE ( 1 )";
317 $this->_where
= "WHERE " . implode(' AND ', $clauses);
320 if ($this->_aclWhere
) {
321 $this->_where
.= " AND {$this->_aclWhere} ";
325 public function groupBy() {
326 $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 ";
329 public function orderBy() {
330 $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";
338 public function statistics(&$rows) {
339 $statistics = parent
::statistics($rows);
341 //hack filter display for relationship type
342 $type = substr($this->_params
['relationship_type_id_value'], -3);
343 foreach ($statistics['filters'] as $id => $value) {
344 if ($value['title'] == 'Relationship Type' && !empty($id)) {
345 $statistics['filters'][$id]['value'] = 'Is equal to ' .
346 $this->relationTypes
[$this->relationshipId
. '_' . $type];
352 public function postProcess() {
353 $this->beginPostProcess();
354 $this->buildACLClause(array($this->_aliases
['civicrm_contact'], $this->_aliases
['civicrm_contact_organization']));
355 $sql = $this->buildQuery(TRUE);
357 $this->buildRows($sql, $rows);
358 $this->formatDisplay($rows);
359 $this->doTemplateAssignment($rows);
360 $this->endPostProcess($rows);
364 * Set variables to be accessed by API and form layer in processing
366 public function beginPostProcessCommon() {
367 $getRelationship = $this->_params
['relationship_type_id_value'];
368 $type = substr($getRelationship, -3);
369 $this->relationshipId
= intval((substr($getRelationship, 0, strpos($getRelationship, '_'))));
370 if ($type == 'b_a') {
371 $this->orgContact
= 'contact_id_b';
372 $this->otherContact
= 'contact_id_a';
375 $this->orgContact
= 'contact_id_a';
376 $this->otherContact
= 'contact_id_b';
380 public function validRelationships() {
381 $this->relationTypes
= $relationTypes = array();
383 $params = array('contact_type_b' => 'Organization', 'version' => 3);
384 $typesA = civicrm_api('relationship_type', 'get', $params);
386 if (empty($typesA['is_error'])) {
387 foreach ($typesA['values'] as $rel) {
388 $relationTypes[$rel['id']][$rel['id'] . '_b_a'] = $rel['label_b_a'];
392 $params = array('contact_type_a' => 'Organization', 'version' => 3);
393 $typesB = civicrm_api('relationship_type', 'get', $params);
395 if (empty($typesB['is_error'])) {
396 foreach ($typesB['values'] as $rel) {
397 $relationTypes[$rel['id']][$rel['id'] . '_a_b'] = $rel['label_a_b'];
401 ksort($relationTypes);
402 foreach ($relationTypes as $relationship) {
403 foreach ($relationship as $index => $label) {
404 $this->relationTypes
[$index] = $label;
412 public function alterDisplay(&$rows) {
413 // custom code to alter rows
414 $type = substr($this->_params
['relationship_type_id_value'], -3);
417 $flagOrganization = $flagContact = 0;
419 foreach ($rows as $rowNum => $row) {
421 //replace retionship id by relationship name
422 if (array_key_exists('civicrm_relationship_relationship_type_id', $row)) {
423 if ($value = $row['civicrm_relationship_relationship_type_id']) {
424 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = $this->relationTypes
[$value . '_' . $type];
429 //remove duplicate Organization names
430 if (array_key_exists('civicrm_contact_organization_id', $row) && $this->_outputMode
!= 'csv') {
431 if ($value = $row['civicrm_contact_organization_id']) {
433 $previousOrganization = $value;
436 if ($previousOrganization == $value) {
437 $flagOrganization = 1;
438 $previousOrganization = $value;
441 $flagOrganization = 0;
442 $previousOrganization = $value;
446 if ($flagOrganization == 1) {
447 foreach ($row as $colName => $colVal) {
448 if (in_array($colName, $this->_noRepeats
)) {
449 unset($rows[$rowNum][$colName]);
457 // convert Organization display name to links
458 if (array_key_exists('civicrm_contact_organization_organization_name', $row) && !empty($rows[$rowNum]['civicrm_contact_organization_organization_name']) &&
459 array_key_exists('civicrm_contact_organization_id', $row)
461 $url = CRM_Utils_System
::url('civicrm/contact/view',
463 $rows[$rowNum]['civicrm_contact_organization_id'],
467 $rows[$rowNum]['civicrm_contact_organization_organization_name_link'] = $url;
468 $rows[$rowNum]['civicrm_contact_organization_organization_name_hover'] = ts('View contact summary for this organization.');
471 //remove duplicate Contact names and relationship type
472 if (array_key_exists('civicrm_contact_id', $row) && $this->_outputMode
!= 'csv') {
473 if ($value = $row['civicrm_contact_id']) {
475 $previousContact = $value;
478 if ($previousContact == $value) {
480 $previousContact = $value;
484 $previousContact = $value;
488 if ($flagContact == 1 && $flagOrganization == 1) {
489 $rows[$rowNum]['civicrm_contact_sort_name'] = "";
490 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = "";
497 if (array_key_exists('civicrm_contribution_contribution_status_id', $row)) {
498 if ($value = $row['civicrm_contribution_contribution_status_id']) {
499 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = CRM_Contribute_PseudoConstant
::contributionStatus($value);
503 // handle state province
504 if (array_key_exists('civicrm_address_state_province_id', $row)) {
505 if ($value = $row['civicrm_address_state_province_id']) {
506 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($value, FALSE);
512 if (array_key_exists('civicrm_address_country_id', $row)) {
513 if ($value = $row['civicrm_address_country_id']) {
514 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
519 // convert Individual display name to links
520 if (array_key_exists('civicrm_contact_sort_name', $row) &&
521 $rows[$rowNum]['civicrm_contact_sort_name'] &&
522 array_key_exists('civicrm_contact_id', $row)
524 $url = CRM_Report_Utils_Report
::getNextUrl('contribute/detail',
525 'reset=1&force=1&id_op=eq&id_value=' .
526 $row['civicrm_contact_id'],
527 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
529 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
530 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View contribution details for this individual');
535 // Contribution amount links to view contribution
536 if (($value = CRM_Utils_Array
::value('civicrm_contribution_total_amount', $row)) &&
537 CRM_Core_Permission
::check('access CiviContribute')
539 $url = CRM_Utils_System
::url('civicrm/contact/view/contribution',
540 "reset=1&id=" . $row['civicrm_contribution_id'] . "&cid=" . $row['civicrm_contact_id'] .
541 "&action=view&context=contribution&selectedChild=contribute",
544 $rows[$rowNum]['civicrm_contribution_total_amount_link'] = $url;
545 $rows[$rowNum]['civicrm_contribution_total_amount_hover'] = ts('View this contribution.');
549 // convert campaign_id to campaign title
550 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
551 if ($value = $row['civicrm_contribution_campaign_id']) {
552 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns
[$value];
557 // skip looking further in rows, if first row itself doesn't