Merge pull request #2845 from elcapo/activity-contact-api
[civicrm-core.git] / CRM / Report / Form / Membership / Summary.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_Membership_Summary extends CRM_Report_Form {
37
38 protected $_summary = NULL;
39
40 protected $_charts = array(
41 '' => 'Tabular',
42 'barChart' => 'Bar Chart',
43 'pieChart' => 'Pie Chart',
44 );
45
46 function __construct() {
47 // UI for selecting columns to appear in the report list
48 // array conatining the columns, group_bys and filters build and provided to Form
49 $this->_columns = array(
50 'civicrm_contact' =>
51 array(
52 'dao' => 'CRM_Contact_DAO_Contact',
53 'fields' =>
54 array(
55 'sort_name' =>
56 array('title' => ts('Member Name'),
57 'no_repeat' => TRUE,
58 'required' => TRUE,
59 ),
60 'id' =>
61 array(
62 'no_display' => TRUE,
63 'required' => TRUE,
64 ),
65 ),
66 'group_bys' =>
67 array(
68 'id' =>
69 array('title' => ts('Contact ID')),
70 'display_name' =>
71 array('title' => ts('Contact Name'),
72 ),
73 ),
74 'grouping' => 'contact-fields',
75 ),
76 'civicrm_membership_type' =>
77 array(
78 'dao' => 'CRM_Member_DAO_MembershipType',
79 'grouping' => 'member-fields',
80 'filters' =>
81 array(
82 'gid' =>
83 array(
84 'name' => 'id',
85 'title' => ts('Membership Types'),
86 'type' => CRM_Utils_Type::T_INT + CRM_Utils_Type::T_ENUM,
87 'options' => CRM_Member_PseudoConstant::membershipType(),
88 ),
89 ),
90 ),
91 'civicrm_membership' =>
92 array(
93 'dao' => 'CRM_Member_DAO_Membership',
94 'grouping' => 'member-fields',
95 'fields' =>
96 array(
97 'membership_type_id' =>
98 array(
99 'title' => 'Membership Type',
100 'required' => TRUE,
101 ),
102 'join_date' => NULL,
103 'start_date' => array('title' => ts('Current Cycle Start Date'),
104 ),
105 'end_date' => array('title' => ts('Current Cycle End Date'),
106 ),
107 ),
108 'group_bys' =>
109 array(
110 'membership_type_id' =>
111 array('title' => ts('Membership Type')),
112 ),
113 'filters' =>
114 array(
115 'join_date' =>
116 array('type' => CRM_Utils_Type::T_DATE),
117 ),
118 ),
119 'civicrm_address' =>
120 array(
121 'dao' => 'CRM_Core_DAO_Address',
122 'fields' =>
123 array(
124 'street_address' => NULL,
125 'city' => NULL,
126 'postal_code' => NULL,
127 'state_province_id' =>
128 array('title' => ts('State/Province'),
129 ),
130 'country_id' =>
131 array('title' => ts('Country'),
132 'default' => TRUE,
133 ),
134 ),
135 'grouping' => 'contact-fields',
136 ),
137 'civicrm_email' =>
138 array(
139 'dao' => 'CRM_Core_DAO_Email',
140 'fields' =>
141 array('email' => NULL),
142 'grouping' => 'contact-fields',
143 ),
144 'civicrm_contribution' =>
145 array(
146 'dao' => 'CRM_Contribute_DAO_Contribution',
147 'filters' =>
148 array(
149 'total_amount' =>
150 array('title' => ts('Contribution Amount'),
151 ),
152 ),
153 ),
154 );
155 parent::__construct();
156 }
157
158 function preProcess() {
159 $this->assign('reportTitle', ts('Membership Summary Report'));
160 parent::preProcess();
161 }
162
163 function setDefaultValues() {
164 return parent::setDefaultValues();
165 }
166
167 function select() {
168 $select = array();
169 $this->_columnHeaders = array();
170 foreach ($this->_columns as $tableName => $table) {
171 if (array_key_exists('fields', $table)) {
172 foreach ($table['fields'] as $fieldName => $field) {
173 if (!empty($field['required']) || !empty($this->_params['fields'][$fieldName])) {
174 // to include optional columns address and email, only if checked
175 if ($tableName == 'civicrm_address') {
176 $this->_addressField = TRUE;
177 $this->_emailField = TRUE;
178 }
179 elseif ($tableName == 'civicrm_email') {
180 $this->_emailField = TRUE;
181 }
182 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
183 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = $field['type'];
184 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
185 }
186 }
187 }
188 }
189 $this->_select = "SELECT " . implode(', ', $select) . " ";
190 }
191
192 static function formRule($fields, $files, $self) {
193 $errors = $grouping = array();
194 //check for searching combination of dispaly columns and
195 //grouping criteria
196
197 return $errors;
198 }
199
200 function from() {
201 $this->_from = NULL;
202
203 $this->_from = "
204 FROM civicrm_contact {$this->_aliases['civicrm_contact']}
205 INNER JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
206 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_membership']}.contact_id
207 LEFT JOIN civicrm_membership_type {$this->_aliases['civicrm_membership_type']}
208 ON {$this->_aliases['civicrm_membership']}.membership_type_id = {$this->_aliases['civicrm_membership_type']}.id
209 LEFT JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
210 ON {$this->_aliases['civicrm_membership']}.contact_id = {$this->_aliases['civicrm_contribution']}.contact_id
211 ";
212 // include address field if address column is to be included
213 if ($this->_addressField) {
214 $this->_from .= "LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND {$this->_aliases['civicrm_address']}.is_primary = 1\n";
215 }
216
217 // include email field if email column is to be included
218 if ($this->_emailField) {
219 $this->_from .= "LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND {$this->_aliases['civicrm_email']}.is_primary = 1\n";
220 }
221 }
222
223 function where() {
224 $clauses = array();
225 foreach ($this->_columns as $tableName => $table) {
226 if (array_key_exists('filters', $table)) {
227 foreach ($table['filters'] as $fieldName => $field) {
228 $clause = NULL;
229 if ($field['type'] & CRM_Utils_Type::T_DATE) {
230 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
231 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
232 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
233
234 if ($relative || $from || $to) {
235 $clause = $this->dateClause($field['name'], $relative, $from, $to);
236 }
237 }
238 else {
239 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
240 if ($op) {
241 $clause = $this->whereClause($field,
242 $op,
243 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
244 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
245 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
246 );
247 }
248 }
249
250 if (!empty($clause)) {
251 $clauses[] = $clause;
252 }
253 }
254 }
255 }
256
257 if (empty($clauses)) {
258 $this->_where = "WHERE ( 1 ) ";
259 }
260 else {
261 $this->_where = "WHERE " . implode(' AND ', $clauses);
262 }
263 }
264
265 function statistics(&$rows) {
266 $statistics = array();
267 $statistics[] = array('title' => ts('Row(s) Listed'),
268 'value' => count($rows),
269 );
270 return $statistics;
271 }
272
273 function groupBy() {
274 $this->_groupBy = "";
275 if (is_array($this->_params['group_bys']) &&
276 !empty($this->_params['group_bys'])
277 ) {
278 foreach ($this->_columns as $tableName => $table) {
279 if (array_key_exists('group_bys', $table)) {
280 foreach ($table['group_bys'] as $fieldName => $field) {
281 if (!empty($this->_params['group_bys'][$fieldName])) {
282 $this->_groupBy[] = $field['dbAlias'];
283 }
284 }
285 }
286 }
287
288 if (!empty($this->_statFields) &&
289 (($append && count($this->_groupBy) <= 1) || (!$append))
290 ) {
291 $this->_rollup = " WITH ROLLUP";
292 }
293 $this->_groupBy = "GROUP BY " . implode(', ', $this->_groupBy) . " {$this->_rollup} ";
294 }
295 else {
296 $this->_groupBy = "GROUP BY contact.id";
297 }
298 }
299
300 function postProcess() {
301 $this->_params = $this->controller->exportValues($this->_name);
302 if (empty($this->_params) &&
303 $this->_force
304 ) {
305 $this->_params = $this->_formValues;
306 }
307 $this->_formValues = $this->_params;
308
309 $this->processReportMode();
310
311 $this->select();
312
313 $this->from();
314
315 $this->where();
316
317 $this->groupBy();
318
319 $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}";
320
321 $dao = CRM_Core_DAO::executeQuery($sql);
322 $rows = $graphRows = array();
323 $count = 0;
324 while ($dao->fetch()) {
325 $row = array();
326 foreach ($this->_columnHeaders as $key => $value) {
327 $row[$key] = $dao->$key;
328 }
329
330 if (!empty($this->_params['charts']) &&
331 $row['civicrm_contribution_receive_date_subtotal']
332 ) {
333 $graphRows['receive_date'][] = $row['civicrm_contribution_receive_date_start'];
334 $graphRows[$this->_interval][] = $row['civicrm_contribution_receive_date_interval'];
335 $graphRows['value'][] = $row['civicrm_contribution_total_amount_sum'];
336 $count++;
337 }
338
339 $rows[] = $row;
340 }
341 $this->formatDisplay($rows);
342
343 $this->assign_by_ref('columnHeaders', $this->_columnHeaders);
344 $this->assign_by_ref('rows', $rows);
345 $this->assign('statistics', $this->statistics($rows));
346
347 if (!empty($this->_params['charts'])) {
348 foreach (array(
349 'receive_date', $this->_interval, 'value') as $ignore) {
350 unset($graphRows[$ignore][$count - 1]);
351 }
352
353 // build chart.
354 CRM_Utils_OpenFlashChart::chart($graphRows, $this->_params['charts'], $this->_interval);
355 }
356 parent::endPostProcess();
357 }
358
359 function alterDisplay(&$rows) {
360 // custom code to alter rows
361 $entryFound = FALSE;
362 $checkList = array();
363
364 foreach ($rows as $rowNum => $row) {
365
366 if (!empty($this->_noRepeats)) {
367 // not repeat contact display names if it matches with the one
368 // in previous row
369
370 $repeatFound = FALSE;
371 foreach ($row as $colName => $colVal) {
372 if (is_array($checkList[$colName]) &&
373 in_array($colVal, $checkList[$colName])
374 ) {
375 $rows[$rowNum][$colName] = "";
376 $repeatFound = TRUE;
377 }
378 if (in_array($colName, $this->_noRepeats)) {
379 $checkList[$colName][] = $colVal;
380 }
381 }
382 }
383
384 //handle the Membership Type Ids
385 if (array_key_exists('civicrm_membership_membership_type_id', $row)) {
386 if ($value = $row['civicrm_membership_membership_type_id']) {
387 $rows[$rowNum]['civicrm_membership_membership_type_id'] = CRM_Member_PseudoConstant::membershipType($value, FALSE);
388 }
389 $entryFound = TRUE;
390 }
391
392 // handle state province
393 if (array_key_exists('civicrm_address_state_province_id', $row)) {
394 if ($value = $row['civicrm_address_state_province_id']) {
395 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($value, FALSE);
396 }
397 $entryFound = TRUE;
398 }
399
400 // handle country
401 if (array_key_exists('civicrm_address_country_id', $row)) {
402 if ($value = $row['civicrm_address_country_id']) {
403 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country($value, FALSE);
404 }
405 $entryFound = TRUE;
406 }
407
408 // convert display name to links
409 if (array_key_exists('civicrm_contact_sort_name', $row) &&
410 array_key_exists('civicrm_contact_id', $row)
411 ) {
412 $url = CRM_Utils_System::url('civicrm/report/member/detail',
413 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
414 $this->_absoluteUrl
415 );
416 $rows[$rowNum]['civicrm_contact_sort_name'] = "<a href='$url'>" . $row["civicrm_contact_sort_name"] . '</a>';
417 $entryFound = TRUE;
418 }
419
420 // skip looking further in rows, if first row itself doesn't
421 // have the column we need
422 if (!$entryFound) {
423 break;
424 }
425 }
426 }
427 }
428