3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
33 class CRM_Report_Form_Membership_Summary
extends CRM_Report_Form
{
35 protected $_summary = NULL;
37 protected $_charts = array(
39 'barChart' => 'Bar Chart',
40 'pieChart' => 'Pie Chart',
44 * Constructor function.
46 public function __construct() {
47 // UI for selecting columns to appear in the report list
48 // array containing the columns, group_bys and filters build and provided to Form
49 $this->_columns
= array(
50 'civicrm_contact' => array(
51 'dao' => 'CRM_Contact_DAO_Contact',
54 'title' => ts('Member Name'),
64 'id' => array('title' => ts('Contact ID')),
65 'display_name' => array(
66 'title' => ts('Contact Name'),
69 'grouping' => 'contact-fields',
71 'civicrm_membership_type' => array(
72 'dao' => 'CRM_Member_DAO_MembershipType',
73 'grouping' => 'member-fields',
77 'title' => ts('Membership Types'),
78 'type' => CRM_Utils_Type
::T_INT + CRM_Utils_Type
::T_ENUM
,
79 'options' => CRM_Member_PseudoConstant
::membershipType(),
83 'civicrm_membership' => array(
84 'dao' => 'CRM_Member_DAO_Membership',
85 'grouping' => 'member-fields',
87 'membership_type_id' => array(
88 'title' => ts('Membership Type'),
92 'start_date' => array(
93 'title' => ts('Current Cycle Start Date'),
96 'title' => ts('Current Cycle End Date'),
100 'membership_type_id' => array('title' => ts('Membership Type')),
103 'join_date' => array('type' => CRM_Utils_Type
::T_DATE
),
106 'civicrm_address' => array(
107 'dao' => 'CRM_Core_DAO_Address',
109 'street_address' => NULL,
111 'postal_code' => NULL,
112 'state_province_id' => array(
113 'title' => ts('State/Province'),
115 'country_id' => array(
116 'title' => ts('Country'),
120 'grouping' => 'contact-fields',
122 'civicrm_email' => array(
123 'dao' => 'CRM_Core_DAO_Email',
124 'fields' => array('email' => NULL),
125 'grouping' => 'contact-fields',
127 'civicrm_contribution' => array(
128 'dao' => 'CRM_Contribute_DAO_Contribution',
130 'total_amount' => array(
131 'title' => ts('Contribution Amount'),
136 parent
::__construct();
140 * Pre-process function.
142 public function preProcess() {
143 $this->assign('reportTitle', ts('Membership Summary Report'));
144 parent
::preProcess();
148 * Generate select clause.
150 public function select() {
151 // @todo remove this in favour of just using parent.
153 $this->_columnHeaders
= array();
154 foreach ($this->_columns
as $tableName => $table) {
155 if (array_key_exists('fields', $table)) {
156 foreach ($table['fields'] as $fieldName => $field) {
157 if (!empty($field['required']) ||
158 !empty($this->_params
['fields'][$fieldName])
160 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
161 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = $field['type'];
162 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
167 $this->_select
= "SELECT " . implode(', ', $select) . " ";
171 * Generate from clause.
173 public function from() {
177 FROM civicrm_contact {$this->_aliases['civicrm_contact']}
178 INNER JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
179 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_membership']}.contact_id
180 LEFT JOIN civicrm_membership_type {$this->_aliases['civicrm_membership_type']}
181 ON {$this->_aliases['civicrm_membership']}.membership_type_id = {$this->_aliases['civicrm_membership_type']}.id
182 LEFT JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
183 ON {$this->_aliases['civicrm_membership']}.contact_id = {$this->_aliases['civicrm_contribution']}.contact_id
185 $this->joinAddressFromContact();
186 $this->joinEmailFromContact();
190 * Generate where clause.
192 * @todo this looks like it duplicates the parent & could go.
194 public function where() {
196 foreach ($this->_columns
as $tableName => $table) {
197 if (array_key_exists('filters', $table)) {
198 foreach ($table['filters'] as $fieldName => $field) {
200 if ($field['type'] & CRM_Utils_Type
::T_DATE
) {
201 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
202 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
203 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
205 if ($relative ||
$from ||
$to) {
206 $clause = $this->dateClause($field['name'], $relative, $from, $to);
210 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
212 $clause = $this->whereClause($field,
214 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
215 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
216 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
221 if (!empty($clause)) {
222 $clauses[] = $clause;
228 if (empty($clauses)) {
229 $this->_where
= "WHERE ( 1 ) ";
232 $this->_where
= "WHERE " . implode(' AND ', $clauses);
237 * Generate statistics (bottom section of the report).
243 public function statistics(&$rows) {
244 $statistics = array();
245 $statistics[] = array(
246 'title' => ts('Row(s) Listed'),
247 'value' => count($rows),
253 * Generate group by clause.
255 * @todo looks like a broken duplicate of the parent.
257 public function groupBy() {
258 $this->_groupBy
= "";
259 if (is_array($this->_params
['group_bys']) &&
260 !empty($this->_params
['group_bys'])
262 foreach ($this->_columns
as $tableName => $table) {
263 if (array_key_exists('group_bys', $table)) {
264 foreach ($table['group_bys'] as $fieldName => $field) {
265 if (!empty($this->_params
['group_bys'][$fieldName])) {
266 $this->_groupBy
[] = $field['dbAlias'];
272 if (!empty($this->_statFields
) &&
273 (($append && count($this->_groupBy
) <= 1) ||
(!$append))
275 $this->_rollup
= " WITH ROLLUP";
277 $this->_groupBy
= "GROUP BY " . implode(', ', $this->_groupBy
) .
278 " {$this->_rollup} ";
281 $this->_groupBy
= "GROUP BY contact.id";
286 * PostProcess function.
288 public function postProcess() {
289 $this->_params
= $this->controller
->exportValues($this->_name
);
290 if (empty($this->_params
) &&
293 $this->_params
= $this->_formValues
;
295 $this->_formValues
= $this->_params
;
297 $this->processReportMode();
307 $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}";
309 $dao = CRM_Core_DAO
::executeQuery($sql);
310 $rows = $graphRows = array();
312 while ($dao->fetch()) {
314 foreach ($this->_columnHeaders
as $key => $value) {
315 $row[$key] = $dao->$key;
318 if (!empty($this->_params
['charts']) &&
319 $row['civicrm_contribution_receive_date_subtotal']
321 $graphRows['receive_date'][] = $row['civicrm_contribution_receive_date_start'];
322 $graphRows[$this->_interval
][] = $row['civicrm_contribution_receive_date_interval'];
323 $graphRows['value'][] = $row['civicrm_contribution_total_amount_sum'];
329 $this->formatDisplay($rows);
331 $this->assign_by_ref('columnHeaders', $this->_columnHeaders
);
332 $this->assign_by_ref('rows', $rows);
333 $this->assign('statistics', $this->statistics($rows));
335 if (!empty($this->_params
['charts'])) {
341 unset($graphRows[$ignore][$count - 1]);
345 CRM_Utils_OpenFlashChart
::chart($graphRows, $this->_params
['charts'], $this->_interval
);
347 parent
::endPostProcess();
351 * Alter display of rows.
353 * Iterate through the rows retrieved via SQL and make changes for display purposes,
354 * such as rendering contacts as links.
357 * Rows generated by SQL, with an array for each row.
359 public function alterDisplay(&$rows) {
361 $checkList = array();
363 foreach ($rows as $rowNum => $row) {
365 if (!empty($this->_noRepeats
)) {
366 // not repeat contact display names if it matches with the one
369 $repeatFound = FALSE;
370 foreach ($row as $colName => $colVal) {
371 if (is_array($checkList[$colName]) &&
372 in_array($colVal, $checkList[$colName])
374 $rows[$rowNum][$colName] = "";
377 if (in_array($colName, $this->_noRepeats
)) {
378 $checkList[$colName][] = $colVal;
383 //handle the Membership Type Ids
384 if (array_key_exists('civicrm_membership_membership_type_id', $row)) {
385 if ($value = $row['civicrm_membership_membership_type_id']) {
386 $rows[$rowNum]['civicrm_membership_membership_type_id'] = CRM_Member_PseudoConstant
::membershipType($value, FALSE);
391 // handle state province
392 if (array_key_exists('civicrm_address_state_province_id', $row)) {
393 if ($value = $row['civicrm_address_state_province_id']) {
394 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($value, FALSE);
400 if (array_key_exists('civicrm_address_country_id', $row)) {
401 if ($value = $row['civicrm_address_country_id']) {
402 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
407 // convert display name to links
408 if (array_key_exists('civicrm_contact_sort_name', $row) &&
409 array_key_exists('civicrm_contact_id', $row)
411 $url = CRM_Utils_System
::url(
412 'civicrm/report/member/detail',
413 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
416 $rows[$rowNum]['civicrm_contact_sort_name']
417 = "<a href='$url'>" . $row["civicrm_contact_sort_name"] . '</a>';
421 // skip looking further in rows, if first row itself doesn't
422 // have the column we need