reformat whitespace
[civicrm-core.git] / CRM / Report / Form / Contribute / OrganizationSummary.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
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. |
14 | |
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. |
19 | |
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 +--------------------------------------------------------------------+
27 */
28
29 /**
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2014
33 * $Id$
34 *
35 */
36 class CRM_Report_Form_Contribute_OrganizationSummary extends CRM_Report_Form {
37
38 protected $_addressField = FALSE;
39
40 protected $_emailField = FALSE;
41
42 public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report');
43
44 protected $_summary = NULL;
45
46 /**
47 *
48 */
49 /**
50 *
51 */
52 function __construct() {
53 self::validRelationships();
54 $config = CRM_Core_Config::singleton();
55 $campaignEnabled = in_array('CiviCampaign', $config->enableComponents);
56 if ($campaignEnabled) {
57 $getCampaigns = CRM_Campaign_BAO_Campaign::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
58 $this->activeCampaigns = $getCampaigns['campaigns'];
59 asort($this->activeCampaigns);
60 }
61
62 $this->_columns = array(
63 'civicrm_contact_organization' => array(
64 'dao' => 'CRM_Contact_DAO_Contact',
65 'fields' => array(
66 'organization_name' => array(
67 'title' => ts('Organization Name'),
68 'required' => TRUE,
69 'no_repeat' => TRUE,
70 ),
71 'id' => array(
72 'no_display' => TRUE,
73 'required' => TRUE,
74 ),
75 'contact_type' => array(
76 'title' => ts('Contact Type'),
77 ),
78 'contact_sub_type' => array(
79 'title' => ts('Contact SubType'),
80 ),
81 ),
82 'filters' => array(
83 'organization_name' => array('title' => ts('Organization Name')),
84 ),
85 'grouping' => 'organization-fields',
86 ),
87 'civicrm_relationship' => array(
88 'dao' => 'CRM_Contact_DAO_Relationship',
89 'fields' => array(
90 'relationship_type_id' => array(
91 'title' => ts('Relationship Type'),
92 ),
93 ),
94 'filters' => array(
95 'relationship_type_id' => array(
96 'title' => ts('Relationship Type'),
97 'type' => CRM_Utils_Type::T_INT,
98 'operatorType' => CRM_Report_Form::OP_SELECT,
99 'options' => $this->relationTypes,
100 'default' => array(1),
101 ),
102 ),
103 'grouping' => 'organization-fields',
104 ),
105 'civicrm_contact' => array(
106 'dao' => 'CRM_Contact_DAO_Contact',
107 'fields' => array(
108 'sort_name' => array(
109 'title' => ts('Contact Name'),
110 'required' => TRUE,
111 ),
112 'id' => array(
113 'no_display' => TRUE,
114 'required' => TRUE,
115 ),
116 ),
117 'grouping' => 'contact-fields',
118 ),
119 'civicrm_contribution' => array(
120 'dao' => 'CRM_Contribute_DAO_Contribution',
121 'fields' => array(
122 'total_amount' => array(
123 'title' => ts('Amount'),
124 'required' => TRUE,
125 ),
126 'id' => array(
127 'no_display' => TRUE,
128 'required' => TRUE,
129 ),
130 'contribution_status_id' => array(
131 'title' => 'Contribution Status',
132 'default' => TRUE,
133 ),
134 'check_number' => array(
135 'title' => ts('Check Number'),
136 ),
137 'currency' => array(
138 'required' => TRUE,
139 'no_display' => TRUE,
140 ),
141 'trxn_id' => NULL,
142 'receive_date' => array('default' => TRUE),
143 'receipt_date' => NULL,
144 ),
145 'filters' => array(
146 'receive_date' => array('operatorType' => CRM_Report_Form::OP_DATE),
147 'total_amount' => array('title' => ts('Amount Between')),
148 'currency' => array(
149 'title' => 'Currency',
150 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
151 'options' => CRM_Core_OptionGroup::values('currencies_enabled'),
152 'default' => NULL,
153 'type' => CRM_Utils_Type::T_STRING,
154 ),
155 'contribution_status_id' => array(
156 'title' => ts('Contribution Status'),
157 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
158 'options' => CRM_Contribute_PseudoConstant::contributionStatus(),
159 'default' => array(1),
160 ),
161 ),
162 'grouping' => 'contri-fields',
163 ),
164 'civicrm_address' => array(
165 'dao' => 'CRM_Core_DAO_Address',
166 'fields' => array(
167 'street_address' => NULL,
168 'city' => NULL,
169 'postal_code' => NULL,
170 'state_province_id' => array(
171 'title' => ts('State/Province'),
172 ),
173 'country_id' => array(
174 'title' => ts('Country'),
175 ),
176 ),
177 'grouping' => 'contact-fields',
178 ),
179 'civicrm_email' => array(
180 'dao' => 'CRM_Core_DAO_Email',
181 'fields' => array('email' => NULL),
182 'grouping' => 'contact-fields',
183 ),
184 );
185
186 if ($campaignEnabled && !empty($this->activeCampaigns)) {
187 $this->_columns['civicrm_contribution']['fields']['campaign_id'] = array(
188 'title' => 'Campaign',
189 'default' => 'false',
190 );
191 $this->_columns['civicrm_contribution']['filters']['campaign_id'] = array(
192 'title' => ts('Campaign'),
193 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
194 'options' => $this->activeCampaigns,
195 );
196 }
197
198 $this->_currencyColumn = 'civicrm_contribution_currency';
199 parent::__construct();
200 }
201
202 function preProcess() {
203 parent::preProcess();
204 }
205
206 function select() {
207 $this->_columnHeaders = $select = array();
208 foreach ($this->_columns as $tableName => $table) {
209 if (array_key_exists('fields', $table)) {
210 foreach ($table['fields'] as $fieldName => $field) {
211 if (!empty($field['required']) || !empty($this->_params['fields'][$fieldName])) {
212 if ($tableName == 'civicrm_address') {
213 $this->_addressField = TRUE;
214 }
215 elseif ($tableName == 'civicrm_email') {
216 $this->_emailField = TRUE;
217 }
218
219 if (!empty($field['statistics'])) {
220 foreach ($field['statistics'] as $stat => $label) {
221 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}_{$stat}";
222 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
223 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
224 $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}";
225 }
226 }
227 else {
228 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
229
230 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
231 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
232 }
233 }
234 }
235 }
236 }
237 $this->_select = "SELECT " . implode(', ', $select) . " ";
238 }
239
240 function from() {
241
242 $this->_from = NULL;
243 $this->_from = "
244 FROM civicrm_relationship {$this->_aliases['civicrm_relationship']}
245 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact_organization']} ON
246 ({$this->_aliases['civicrm_contact_organization']}.id = {$this->_aliases['civicrm_relationship']}.$this->orgContact AND {$this->_aliases['civicrm_contact_organization']}.contact_type='Organization')
247 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']} ON
248 ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_relationship']}.$this->otherContact )
249 {$this->_aclFrom}
250 INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} ON
251 ({$this->_aliases['civicrm_contribution']}.contact_id = {$this->_aliases['civicrm_relationship']}.$this->otherContact ) AND {$this->_aliases['civicrm_contribution']}.is_test = 0 ";
252
253 if ($this->_addressField) {
254 $this->_from .= "
255 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} ON
256 {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
257 {$this->_aliases['civicrm_address']}.is_primary = 1\n ";
258 }
259 if ($this->_emailField) {
260 $this->_from .= "
261 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} ON
262 {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
263 {$this->_aliases['civicrm_email']}.is_primary = 1\n ";
264 }
265 }
266
267 function where() {
268 $clauses = array();
269 foreach ($this->_columns as $tableName => $table) {
270 if (array_key_exists('filters', $table)) {
271 foreach ($table['filters'] as $fieldName => $field) {
272 $clause = NULL;
273 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
274 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
275 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
276 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
277
278 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
279 }
280 else {
281 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
282 if ($op) {
283 if ($fieldName == 'relationship_type_id') {
284 $clause = "{$this->_aliases['civicrm_relationship']}.relationship_type_id=" . $this->relationshipId;
285 }
286 else {
287 $clause = $this->whereClause($field,
288 $op,
289 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
290 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
291 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
292 );
293 }
294 }
295 }
296
297 if (!empty($clause)) {
298 $clauses[] = $clause;
299 }
300 }
301 }
302 }
303
304 if (empty($clauses)) {
305 $this->_where = "WHERE ( 1 )";
306 }
307 else {
308 $this->_where = "WHERE " . implode(' AND ', $clauses);
309 }
310
311 if ($this->_aclWhere) {
312 $this->_where .= " AND {$this->_aclWhere} ";
313 }
314 }
315
316 function groupBy() {
317 $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 ";
318 }
319
320 function orderBy() {
321 $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";
322 }
323
324 /**
325 * @param $rows
326 *
327 * @return array
328 */
329 function statistics(&$rows) {
330 $statistics = parent::statistics($rows);
331
332 //hack filter display for relationship type
333 $type = substr($this->_params['relationship_type_id_value'], -3);
334 foreach ($statistics['filters'] as $id => $value) {
335 if ($value['title'] == 'Relationship Type' && !empty($id)) {
336 $statistics['filters'][$id]['value'] = 'Is equal to ' .
337 $this->relationTypes[$this->relationshipId . '_' . $type];
338 }
339 }
340 return $statistics;
341 }
342
343 function postProcess() {
344
345 $this->beginPostProcess();
346 $getRelationship = $this->_params['relationship_type_id_value'];
347 $type = substr($getRelationship, -3);
348 $this->relationshipId = intval((substr($getRelationship, 0, strpos($getRelationship, '_'))));
349 if ($type == 'b_a') {
350 $this->orgContact = 'contact_id_b';
351 $this->otherContact = 'contact_id_a';
352 }
353 else {
354 $this->orgContact = 'contact_id_a';
355 $this->otherContact = 'contact_id_b';
356 }
357
358 $this->buildACLClause(array($this->_aliases['civicrm_contact'], $this->_aliases['civicrm_contact_organization']));
359 $sql = $this->buildQuery(TRUE);
360 $rows = array();
361
362 $this->buildRows($sql, $rows);
363 $this->formatDisplay($rows);
364 $this->doTemplateAssignment($rows);
365 $this->endPostProcess($rows);
366 }
367
368 function validRelationships() {
369 $this->relationTypes = $relationTypes = array();
370
371 $params = array('contact_type_b' => 'Organization', 'version' => 3);
372 $typesA = civicrm_api('relationship_type', 'get', $params);
373
374 if (empty($typesA['is_error'])) {
375 foreach ($typesA['values'] as $rel) {
376 $relationTypes[$rel['id']][$rel['id'] . '_b_a'] = $rel['label_b_a'];
377 }
378 }
379
380 $params = array('contact_type_a' => 'Organization', 'version' => 3);
381 $typesB = civicrm_api('relationship_type', 'get', $params);
382
383 if (empty($typesB['is_error'])) {
384 foreach ($typesB['values'] as $rel) {
385 $relationTypes[$rel['id']][$rel['id'] . '_a_b'] = $rel['label_a_b'];
386 }
387 }
388
389 ksort($relationTypes);
390 foreach ($relationTypes as $relationship) {
391 foreach ($relationship as $index => $label) {
392 $this->relationTypes[$index] = $label;
393 }
394 }
395 }
396
397 /**
398 * @param array $rows
399 */
400 function alterDisplay(&$rows) {
401 // custom code to alter rows
402 $type = substr($this->_params['relationship_type_id_value'], -3);
403
404 $entryFound = FALSE;
405 $flagOrganization = $flagContact = 0;
406
407 foreach ($rows as $rowNum => $row) {
408
409 //replace retionship id by relationship name
410 if (array_key_exists('civicrm_relationship_relationship_type_id', $row)) {
411 if ($value = $row['civicrm_relationship_relationship_type_id']) {
412 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = $this->relationTypes[$value . '_' . $type];
413 $entryFound = TRUE;
414 }
415 }
416
417 //remove duplicate Organization names
418 if (array_key_exists('civicrm_contact_organization_id', $row) && $this->_outputMode != 'csv') {
419 if ($value = $row['civicrm_contact_organization_id']) {
420 if ($rowNum == 0) {
421 $previousOrganization = $value;
422 }
423 else {
424 if ($previousOrganization == $value) {
425 $flagOrganization = 1;
426 $previousOrganization = $value;
427 }
428 else {
429 $flagOrganization = 0;
430 $previousOrganization = $value;
431 }
432 }
433
434 if ($flagOrganization == 1) {
435 foreach ($row as $colName => $colVal) {
436 if (in_array($colName, $this->_noRepeats)) {
437 unset($rows[$rowNum][$colName]);
438 }
439 }
440 }
441 $entryFound = TRUE;
442 }
443 }
444
445 // convert Organization display name to links
446 if (array_key_exists('civicrm_contact_organization_organization_name', $row) && !empty($rows[$rowNum]
447 ['civicrm_contact_organization_organization_name']) &&
448 array_key_exists('civicrm_contact_organization_id', $row)
449 ) {
450 $url = CRM_Utils_System::url('civicrm/contact/view',
451 'reset=1&cid=' .
452 $rows[$rowNum]['civicrm_contact_organization_id'],
453 $this->_absoluteUrl
454 );
455
456 $rows[$rowNum]['civicrm_contact_organization_organization_name_link'] = $url;
457 $rows[$rowNum]['civicrm_contact_organization_organization_name_hover'] = ts('View contact summary for this organization.');
458 }
459
460 //remove duplicate Contact names and relationship type
461 if (array_key_exists('civicrm_contact_id', $row) && $this->_outputMode != 'csv') {
462 if ($value = $row['civicrm_contact_id']) {
463 if ($rowNum == 0) {
464 $previousContact = $value;
465 }
466 else {
467 if ($previousContact == $value) {
468 $flagContact = 1;
469 $previousContact = $value;
470 }
471 else {
472 $flagContact = 0;
473 $previousContact = $value;
474 }
475 }
476
477 if ($flagContact == 1 && $flagOrganization == 1) {
478 $rows[$rowNum]['civicrm_contact_sort_name'] = "";
479 $rows[$rowNum]['civicrm_relationship_relationship_type_id'] = "";
480 }
481
482 $entryFound = TRUE;
483 }
484 }
485
486 if (array_key_exists('civicrm_contribution_contribution_status_id', $row)) {
487 if ($value = $row['civicrm_contribution_contribution_status_id']) {
488 $rows[$rowNum]['civicrm_contribution_contribution_status_id'] = CRM_Contribute_PseudoConstant::contributionStatus($value);
489 }
490 }
491
492 // handle state province
493 if (array_key_exists('civicrm_address_state_province_id', $row)) {
494 if ($value = $row['civicrm_address_state_province_id']) {
495 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($value, FALSE);
496 }
497 $entryFound = TRUE;
498 }
499
500 // handle country
501 if (array_key_exists('civicrm_address_country_id', $row)) {
502 if ($value = $row['civicrm_address_country_id']) {
503 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country($value, FALSE);
504 }
505 $entryFound = TRUE;
506 }
507
508 // convert Individual display name to links
509 if (array_key_exists('civicrm_contact_sort_name', $row) &&
510 $rows[$rowNum]['civicrm_contact_sort_name'] &&
511 array_key_exists('civicrm_contact_id', $row)
512 ) {
513 $url = CRM_Report_Utils_Report::getNextUrl('contribute/detail',
514 'reset=1&force=1&id_op=eq&id_value=' .
515 $row['civicrm_contact_id'],
516 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
517 );
518 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
519 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View contribution details for this individual');
520
521 $entryFound = TRUE;
522 }
523
524 // Contribution amount links to view contribution
525 if (($value = CRM_Utils_Array::value('civicrm_contribution_total_amount', $row)) &&
526 CRM_Core_Permission::check('access CiviContribute')
527 ) {
528 $url = CRM_Utils_System::url('civicrm/contact/view/contribution',
529 "reset=1&id=" . $row['civicrm_contribution_id'] . "&cid=" . $row['civicrm_contact_id'] .
530 "&action=view&context=contribution&selectedChild=contribute",
531 $this->_absoluteUrl
532 );
533 $rows[$rowNum]['civicrm_contribution_total_amount_link'] = $url;
534 $rows[$rowNum]['civicrm_contribution_total_amount_hover'] = ts('View this contribution.');
535 $entryFound = TRUE;
536 }
537
538 // convert campaign_id to campaign title
539 if (array_key_exists('civicrm_contribution_campaign_id', $row)) {
540 if ($value = $row['civicrm_contribution_campaign_id']) {
541 $rows[$rowNum]['civicrm_contribution_campaign_id'] = $this->activeCampaigns[$value];
542 $entryFound = TRUE;
543 }
544 }
545
546 // skip looking further in rows, if first row itself doesn't
547 if (!$entryFound) {
548 break;
549 }
550 $lastKey = $rowNum;
551 }
552 }
553 }
554