remove newly introduced trailing spaces & tabs, new windows line-breaks also present...
[civicrm-core.git] / CRM / Report / Form / Member / Summary.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35 class CRM_Report_Form_Member_Summary extends CRM_Report_Form {
36
37 protected $_summary = NULL;
38 protected $_interval = NULL;
39 protected $_charts = array(
40 '' => 'Tabular',
41 'barChart' => 'Bar Chart',
42 'pieChart' => 'Pie Chart',
43 );
44 protected $_add2groupSupported = FALSE;
45
46 protected $_customGroupExtends = array('Membership');
47 protected $_customGroupGroupBy = FALSE;
48 public $_drilldownReport = array('member/detail' => 'Link to Detail Report');
49
50 function __construct() {
51
52 // UI for selecting columns to appear in the report list
53 // Array containing the columns, group_bys and filters build and provided to Form
54
55 // Check if CiviCampaign is a) enabled and b) has active campaigns
56 $config = CRM_Core_Config::singleton();
57 $campaignEnabled = in_array("CiviCampaign", $config->enableComponents);
58 if ($campaignEnabled) {
59 $getCampaigns = CRM_Campaign_BAO_Campaign::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
60 $this->activeCampaigns = $getCampaigns['campaigns'];
61 asort($this->activeCampaigns);
62 }
63
64 $this->_columns = array(
65 'civicrm_membership' =>
66 array(
67 'dao' => 'CRM_Member_DAO_MembershipType',
68 'grouping' => 'member-fields',
69 'fields' =>
70 array(
71 'membership_type_id' =>
72 array(
73 'title' => 'Membership Type',
74 'required' => TRUE,
75 ),
76 ),
77 'filters' =>
78 array(
79 'join_date' =>
80 array('title' => ts('Member Since'),
81 'type' => CRM_Utils_Type::T_DATE,
82 'operatorType' => CRM_Report_Form::OP_DATE,
83 ),
84 'membership_start_date' =>
85 array(
86 'name' => 'start_date',
87 'title' => ts('Membership Start Date'),
88 'type' => CRM_Utils_Type::T_DATE,
89 'operatorType' => CRM_Report_Form::OP_DATE,
90 ),
91 'membership_end_date' =>
92 array(
93 'name' => 'end_date',
94 'title' => ts('Membership End Date'),
95 'type' => CRM_Utils_Type::T_DATE,
96 'operatorType' => CRM_Report_Form::OP_DATE,
97 ),
98 'owner_membership_id' =>
99 array('title' => ts('Membership Owner ID'),
100 'operatorType' => CRM_Report_Form::OP_INT,
101 ),
102 'membership_type_id' =>
103 array('title' => ts('Membership Type'),
104 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
105 'options' => CRM_Member_PseudoConstant::membershipType(),
106 ),
107 'status_id' =>
108 array('title' => ts('Membership Status'),
109 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
110 'options' => CRM_Member_PseudoConstant::membershipStatus(NULL, NULL, 'label'),
111 ),
112 ),
113 'group_bys' =>
114 array(
115 'join_date' =>
116 array('title' => ts('Member Since'),
117 'default' => TRUE,
118 'frequency' => TRUE,
119 'chart' => TRUE,
120 'type' => 12,
121 ),
122 'membership_type_id' =>
123 array(
124 'title' => 'Membership Type',
125 'default' => TRUE,
126 'chart' => TRUE,
127 ),
128 ),
129 ),
130 'civicrm_contact' =>
131 array(
132 'dao' => 'CRM_Contact_DAO_Contact',
133 'fields' =>
134 array(
135 'contact_id' =>
136 array(
137 'no_display' => TRUE,
138 ),
139 ),
140 ),
141 'civicrm_contribution' =>
142 array(
143 'dao' => 'CRM_Contribute_DAO_Contribution',
144 'fields' =>
145 array(
146 'currency' =>
147 array('required' => TRUE,
148 'no_display' => TRUE,
149 ),
150 'total_amount' =>
151 array('title' => ts('Amount Statistics'),
152 'required' => TRUE,
153 'statistics' =>
154 array('sum' => ts('Total Payments Made'),
155 'count' => ts('Contribution Count'),
156 'avg' => ts('Average'),
157 ),
158 ),
159 ),
160 'filters' =>
161 array(
162 'currency' =>
163 array('title' => 'Currency',
164 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
165 'options' => CRM_Core_OptionGroup::values('currencies_enabled'),
166 'default' => NULL,
167 'type' => CRM_Utils_Type::T_STRING,
168 ),
169 'contribution_status_id' =>
170 array('title' => ts('Contribution Status'),
171 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
172 'options' => CRM_Contribute_PseudoConstant::contributionStatus(),
173 ),
174 ),
175 'grouping' => 'member-fields',
176 ),
177 );
178 $this->_tagFilter = TRUE;
179
180 // If we have a campaign, build out the relevant elements
181 if ($campaignEnabled && !empty($this->activeCampaigns)) {
182 $this->_columns['civicrm_membership']['fields']['campaign_id'] = array(
183 'title' => 'Campaign',
184 'default' => 'false',
185 );
186 $this->_columns['civicrm_membership']['filters']['campaign_id'] = array('title' => ts('Campaign'),
187 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
188 'options' => $this->activeCampaigns,
189 );
190 $this->_columns['civicrm_membership']['grouping']['campaign_id'] = 'contri-fields';
191 $this->_columns['civicrm_membership']['group_bys']['campaign_id'] = array('title' => ts('Campaign'));
192 }
193
194
195 $this->_groupFilter = TRUE;
196 $this->_currencyColumn = 'civicrm_contribution_currency';
197 parent::__construct();
198 }
199
200 function select() {
201 $select = array();
202 $groupBys = FALSE;
203 $this->_columnHeaders = array();
204 $select[] = " COUNT( DISTINCT {$this->_aliases['civicrm_membership']}.id ) as civicrm_membership_member_count";
205 $select['joinDate'] = " {$this->_aliases['civicrm_membership']}.join_date as civicrm_membership_member_join_date";
206 $this->_columnHeaders["civicrm_membership_member_join_date"] = array('title' => ts('Member Since'),
207 'type' => CRM_Utils_Type::T_DATE,
208 );
209 foreach ($this->_columns as $tableName => $table) {
210 if (array_key_exists('group_bys', $table)) {
211 foreach ($table['group_bys'] as $fieldName => $field) {
212 if (CRM_Utils_Array::value($fieldName, $this->_params['group_bys'])) {
213
214 switch (CRM_Utils_Array::value($fieldName, $this->_params['group_bys_freq'])) {
215 case 'YEARWEEK':
216 $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL WEEKDAY({$field['dbAlias']}) DAY) AS {$tableName}_{$fieldName}_start";
217
218 $select[] = "YEARWEEK({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
219 $select[] = "WEEKOFYEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
220 $field['title'] = 'Week';
221 break;
222
223 case 'YEAR':
224 $select[] = "MAKEDATE(YEAR({$field['dbAlias']}), 1) AS {$tableName}_{$fieldName}_start";
225 $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
226 $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
227 $field['title'] = 'Year';
228 break;
229
230 case 'MONTH':
231 $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL (DAYOFMONTH({$field['dbAlias']})-1) DAY) as {$tableName}_{$fieldName}_start";
232 $select[] = "MONTH({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
233 $select[] = "MONTHNAME({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
234 $field['title'] = 'Month';
235 break;
236
237 case 'QUARTER':
238 $select[] = "STR_TO_DATE(CONCAT( 3 * QUARTER( {$field['dbAlias']} ) -2 , '/', '1', '/', YEAR( {$field['dbAlias']} ) ), '%m/%d/%Y') AS {$tableName}_{$fieldName}_start";
239 $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
240 $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
241 $field['title'] = 'Quarter';
242 break;
243 }
244 if (CRM_Utils_Array::value($fieldName, $this->_params['group_bys_freq'])) {
245 $this->_interval = $field['title'];
246 $this->_columnHeaders["{$tableName}_{$fieldName}_start"]['title'] = $field['title'] . ' Beginning';
247 $this->_columnHeaders["{$tableName}_{$fieldName}_start"]['type'] = $field['type'];
248 $this->_columnHeaders["{$tableName}_{$fieldName}_start"]['group_by'] = $this->_params['group_bys_freq'][$fieldName];
249
250 // just to make sure these values are transfered to rows.
251 // since we need that for calculation purpose,
252 // e.g making subtotals look nicer or graphs
253 $this->_columnHeaders["{$tableName}_{$fieldName}_interval"] = array('no_display' => TRUE);
254 $this->_columnHeaders["{$tableName}_{$fieldName}_subtotal"] = array('no_display' => TRUE);
255 }
256 $groupBys = TRUE;
257 }
258 }
259 }
260 // end of select
261
262 if (array_key_exists('fields', $table)) {
263 foreach ($table['fields'] as $fieldName => $field) {
264 if (CRM_Utils_Array::value('required', $field) ||
265 CRM_Utils_Array::value($fieldName, $this->_params['fields'])
266 ) {
267
268 // only include statistics columns if set
269 if (CRM_Utils_Array::value('statistics', $field)) {
270 $this->_statFields[] = 'civicrm_membership_member_count';
271 foreach ($field['statistics'] as $stat => $label) {
272 switch (strtolower($stat)) {
273 case 'sum':
274 $select[] = "IFNULL(SUM({$field['dbAlias']}), 0) as {$tableName}_{$fieldName}_{$stat}";
275 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
276 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
277 $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}";
278 break;
279
280 case 'count':
281 $select[] = "COUNT({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
282 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type::T_INT;
283 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
284 $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}";
285 break;
286
287 case 'avg':
288 $select[] = "IFNULL(ROUND(AVG({$field['dbAlias']}),2), 0) as {$tableName}_{$fieldName}_{$stat}";
289 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
290 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
291 $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}";
292 break;
293 }
294 }
295 }
296 elseif ($fieldName == 'membership_type_id') {
297 if (!CRM_Utils_Array::value('membership_type_id', $this->_params['group_bys']) &&
298 CRM_Utils_Array::value('join_date', $this->_params['group_bys'])
299 ) {
300 $select[] = "GROUP_CONCAT(DISTINCT {$field['dbAlias']} ORDER BY {$field['dbAlias']} ) as {$tableName}_{$fieldName}";
301 }
302 else {
303 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
304 }
305 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
306 $this->_columnHeaders["{$tableName}_{$fieldName}"]['operatorType'] = CRM_Utils_Array::value('operatorType', $field);
307 }
308 else {
309 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
310 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
311 $this->_columnHeaders["{$tableName}_{$fieldName}"]['operatorType'] = CRM_Utils_Array::value('operatorType', $field);
312 }
313 }
314 }
315 }
316 $this->_columnHeaders["civicrm_membership_member_count"] = array('title' => ts('Member Count'),
317 'type' => CRM_Utils_Type::T_INT,
318 );
319 }
320 //If grouping is availabled then remove join date from field
321 if ($groupBys) {
322 unset($select['joinDate']);
323 unset($this->_columnHeaders["civicrm_membership_member_join_date"]);
324 }
325 $this->_select = "SELECT " . implode(', ', $select) . " ";
326 }
327
328 function from() {
329 $this->_from = "
330 FROM civicrm_membership {$this->_aliases['civicrm_membership']}
331
332 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']} ON ( {$this->_aliases['civicrm_membership']}.contact_id = {$this->_aliases['civicrm_contact']}.id )
333
334 LEFT JOIN civicrm_membership_status
335 ON ({$this->_aliases['civicrm_membership']}.status_id = civicrm_membership_status.id )
336 LEFT JOIN civicrm_membership_payment payment
337 ON ( {$this->_aliases['civicrm_membership']}.id = payment.membership_id )
338 LEFT JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
339 ON payment.contribution_id = {$this->_aliases['civicrm_contribution']}.id";
340 }
341 // end of from
342
343 function where() {
344 $clauses = array();
345 foreach ($this->_columns as $tableName => $table) {
346 if (array_key_exists('filters', $table)) {
347 foreach ($table['filters'] as $fieldName => $field) {
348 $clause = NULL;
349
350 if ($field['operatorType'] & CRM_Utils_Type::T_DATE) {
351 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
352 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
353 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
354
355 if ($relative || $from || $to) {
356 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
357 }
358 }
359 else {
360 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
361 if ($op) {
362 $clause = $this->whereClause($field,
363 $op,
364 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
365 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
366 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
367 );
368 }
369 }
370 if (!empty($clause)) {
371 $clauses[$fieldName] = $clause;
372 }
373 }
374 }
375 }
376
377 if (!empty($clauses)) {
378 $this->_where = "WHERE {$this->_aliases['civicrm_membership']}.is_test = 0 AND " . implode(' AND ', $clauses);
379 }
380 else {
381 $this->_where = "WHERE {$this->_aliases['civicrm_membership']}.is_test = 0";
382 }
383 }
384
385 function groupBy() {
386 $this->_groupBy = "";
387 if (is_array($this->_params['group_bys']) &&
388 !empty($this->_params['group_bys'])
389 ) {
390 foreach ($this->_columns as $tableName => $table) {
391 if (array_key_exists('group_bys', $table)) {
392 foreach ($table['group_bys'] as $fieldName => $field) {
393 if (CRM_Utils_Array::value($fieldName, $this->_params['group_bys'])) {
394 if (CRM_Utils_Array::value('chart', $field)) {
395 $this->assign('chartSupported', TRUE);
396 }
397 if (CRM_Utils_Array::value('frequency', $table['group_bys'][$fieldName]) &&
398 CRM_Utils_Array::value($fieldName, $this->_params['group_bys_freq'])
399 ) {
400
401 $append = "YEAR({$field['dbAlias']}),";
402 if (in_array(strtolower($this->_params['group_bys_freq'][$fieldName]),
403 array('year')
404 )) {
405 $append = '';
406 }
407 $this->_groupBy[] = "$append {$this->_params['group_bys_freq'][$fieldName]}({$field['dbAlias']})";
408 $append = TRUE;
409 }
410 else {
411 $this->_groupBy[] = $field['dbAlias'];
412 }
413 }
414 }
415 }
416 }
417
418 $this->_rollup = ' WITH ROLLUP';
419 $this->_groupBy = 'GROUP BY ' . implode(', ', $this->_groupBy) . " {$this->_rollup} ";
420 }
421 else {
422 $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_membership']}.join_date";
423 }
424 }
425
426 function statistics(&$rows) {
427 $statistics = parent::statistics($rows);
428 $select = "
429 SELECT COUNT({$this->_aliases['civicrm_contribution']}.total_amount ) as count,
430 IFNULL(SUM({$this->_aliases['civicrm_contribution']}.total_amount ), 0) as amount,
431 IFNULL(ROUND(AVG({$this->_aliases['civicrm_contribution']}.total_amount), 2),0) as avg,
432 COUNT( DISTINCT {$this->_aliases['civicrm_membership']}.id ) as memberCount,
433 {$this->_aliases['civicrm_contribution']}.currency as currency
434 ";
435
436 $sql = "{$select} {$this->_from} {$this->_where}
437 GROUP BY {$this->_aliases['civicrm_contribution']}.currency
438 ";
439
440 $dao = CRM_Core_DAO::executeQuery($sql);
441
442 $totalAmount = $average = array();
443 $count = $memberCount = 0;
444 while ($dao->fetch()) {
445 $totalAmount[] = CRM_Utils_Money::format($dao->amount, $dao->currency)."(".$dao->count.")";
446 $average[] = CRM_Utils_Money::format($dao->avg, $dao->currency);
447 $count += $dao->count;
448 $memberCount += $dao->memberCount;
449 }
450 $statistics['counts']['amount'] = array(
451 'title' => ts('Total Amount'),
452 'value' => implode(', ', $totalAmount),
453 'type' => CRM_Utils_Type::T_STRING,
454 );
455 $statistics['counts']['count'] = array(
456 'title' => ts('Total Donations'),
457 'value' => $count,
458 );
459 $statistics['counts']['memberCount'] = array(
460 'title' => ts('Total Members'),
461 'value' => $memberCount,
462 );
463 $statistics['counts']['avg'] = array(
464 'title' => ts('Average'),
465 'value' => implode(', ', $average),
466 'type' => CRM_Utils_Type::T_STRING,
467 );
468
469 if (!(int)$statistics['counts']['amount']['value']) {
470 //if total amount is zero then hide Chart Options
471 $this->assign('chartSupported', FALSE);
472 }
473
474 return $statistics;
475 }
476
477 function postProcess() {
478 parent::postProcess();
479 }
480
481 function buildChart(&$rows) {
482 $graphRows = array();
483 $count = 0;
484 $membershipTypeValues = CRM_Member_PseudoConstant::membershipType();
485 $isMembershipType = CRM_Utils_Array::value('membership_type_id', $this->_params['group_bys']);
486 $isJoiningDate = CRM_Utils_Array::value('join_date', $this->_params['group_bys']);
487 if (CRM_Utils_Array::value('charts', $this->_params)) {
488 foreach ($rows as $key => $row) {
489 if (!($row['civicrm_membership_join_date_subtotal'] &&
490 $row['civicrm_membership_membership_type_id']
491 )) {
492 continue;
493 }
494 if ($isMembershipType) {
495 $join_date = CRM_Utils_Array::value('civicrm_membership_join_date_start', $row);
496 $displayInterval = CRM_Utils_Array::value('civicrm_membership_join_date_interval', $row);
497 if ($join_date) {
498 list($year, $month) = explode('-', $join_date);
499 }
500 if (CRM_Utils_Array::value('civicrm_membership_join_date_subtotal', $row)) {
501
502 switch ($this->_interval) {
503 case 'Month':
504 $displayRange = $displayInterval . ' ' . $year;
505 break;
506
507 case 'Quarter':
508 $displayRange = 'Quarter ' . $displayInterval . ' of ' . $year;
509 break;
510
511 case 'Week':
512 $displayRange = 'Week ' . $displayInterval . ' of ' . $year;
513 break;
514
515 case 'Year':
516 $displayRange = $year;
517 break;
518 }
519 $membershipType = $displayRange . "-" . $membershipTypeValues[$row['civicrm_membership_membership_type_id']];
520 }
521 else {
522
523 $membershipType = $membershipTypeValues[$row['civicrm_membership_membership_type_id']];
524 }
525
526 $interval[$membershipType] = $membershipType;
527 $display[$membershipType] = $row['civicrm_contribution_total_amount_sum'];
528 }
529 else {
530 $graphRows['receive_date'][] = CRM_Utils_Array::value('civicrm_membership_join_date_start', $row);
531 $graphRows[$this->_interval][] = CRM_Utils_Array::value('civicrm_membership_join_date_interval', $row);
532 $graphRows['value'][] = $row['civicrm_contribution_total_amount_sum'];
533 $count++;
534 }
535 }
536
537 // build chart.
538 if ($isMembershipType) {
539 $graphRows['value'] = $display;
540 $chartInfo = array(
541 'legend' => 'Membership Summary',
542 'xname' => 'Member Since / Member Type',
543 'yname' => 'Fees',
544 );
545 CRM_Utils_OpenFlashChart::reportChart($graphRows, $this->_params['charts'], $interval, $chartInfo);
546 }
547 else {
548 CRM_Utils_OpenFlashChart::chart($graphRows, $this->_params['charts'], $this->_interval);
549 }
550 }
551 $this->assign('chartType', $this->_params['charts']);
552 }
553
554 function alterDisplay(&$rows) {
555 // custom code to alter rows
556 $entryFound = FALSE;
557 foreach ($rows as $rowNum => $row) {
558 // make count columns point to detail report
559 if (CRM_Utils_Array::value('join_date', $this->_params['group_bys']) &&
560 CRM_Utils_Array::value('civicrm_membership_join_date_start', $row) &&
561 $row['civicrm_membership_join_date_start'] &&
562 $row['civicrm_membership_join_date_subtotal']
563 ) {
564
565 $dateStart = CRM_Utils_Date::customFormat($row['civicrm_membership_join_date_start'], '%Y%m%d');
566 $endDate = new DateTime($dateStart);
567 $dateEnd = array();
568
569 list($dateEnd['Y'], $dateEnd['M'], $dateEnd['d']) = explode(':', $endDate->format('Y:m:d'));
570
571 switch (strtolower($this->_params['group_bys_freq']['join_date'])) {
572 case 'month':
573 $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'] + 1,
574 $dateEnd['d'] - 1, $dateEnd['Y']
575 ));
576 break;
577
578 case 'year':
579 $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'],
580 $dateEnd['d'] - 1, $dateEnd['Y'] + 1
581 ));
582 break;
583
584 case 'yearweek':
585 $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'],
586 $dateEnd['d'] + 6, $dateEnd['Y']
587 ));
588 break;
589
590 case 'quarter':
591 $dateEnd = date("Ymd", mktime(0, 0, 0, $dateEnd['M'] + 3,
592 $dateEnd['d'] - 1, $dateEnd['Y']
593 ));
594 break;
595 }
596 $typeUrl = '';
597 if (CRM_Utils_Array::value('membership_type_id', $this->_params['group_bys']) &&
598 $typeID = $row['civicrm_membership_membership_type_id']
599 ) {
600 $typeUrl = "&tid_op=in&tid_value={$typeID}";
601 }
602 $statusUrl = '';
603 if (!empty($this->_params['status_id_value'])) {
604 $statusUrl = "&sid_op=in&sid_value=" . implode(",", $this->_params['status_id_value']);
605 }
606 $url = CRM_Report_Utils_Report::getNextUrl('member/detail',
607 "reset=1&force=1&join_date_from={$dateStart}&join_date_to={$dateEnd}{$typeUrl}{$statusUrl}",
608 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
609 );
610 $row['civicrm_membership_join_date_start'] = CRM_Utils_Date::format($row['civicrm_membership_join_date_start']);
611 $rows[$rowNum]['civicrm_membership_join_date_start_link'] = $url;
612 $rows[$rowNum]['civicrm_membership_join_date_start_hover'] = ts("Lists Summary of Memberships for this date unit.");
613
614 $entryFound = TRUE;
615 }
616
617 // handle Membership Types
618 if (array_key_exists('civicrm_membership_membership_type_id', $row)) {
619 if ($value = $row['civicrm_membership_membership_type_id']) {
620 $value = explode(',', $value);
621 foreach ($value as $key => $id) {
622 $value[$key] = CRM_Member_PseudoConstant::membershipType($id, FALSE);
623 }
624 $rows[$rowNum]['civicrm_membership_membership_type_id'] = implode(' , ', $value);
625 }
626 $entryFound = TRUE;
627 }
628
629 // make subtotals look nicer
630 if (array_key_exists('civicrm_membership_join_date_subtotal', $row) &&
631 !$row['civicrm_membership_join_date_subtotal']
632 ) {
633 $this->fixSubTotalDisplay($rows[$rowNum], $this->_statFields);
634 $entryFound = TRUE;
635 }
636 elseif (array_key_exists('civicrm_membership_join_date_subtotal', $row) &&
637 $row['civicrm_membership_join_date_subtotal'] &&
638 !$row['civicrm_membership_membership_type_id']
639 ) {
640 $this->fixSubTotalDisplay($rows[$rowNum], $this->_statFields, FALSE);
641 $rows[$rowNum]['civicrm_membership_membership_type_id'] = '<b>SubTotal</b>';
642 $entryFound = TRUE;
643 }
644
645
646 // If using campaigns, convert campaign_id to campaign title
647 if (array_key_exists('civicrm_membership_campaign_id', $row)) {
648 if ($value = $row['civicrm_membership_campaign_id']) {
649 $rows[$rowNum]['civicrm_membership_campaign_id'] = $this->activeCampaigns[$value];
650 }
651 $entryFound = TRUE;
652 }
653
654 // skip looking further in rows, if first row itself doesn't
655 // have the column we need
656 if (!$entryFound) {
657 break;
658 }
659 }
660 }
661 }
662