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