3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
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() {
152 $this->_columnHeaders
= array();
153 foreach ($this->_columns
as $tableName => $table) {
154 if (array_key_exists('fields', $table)) {
155 foreach ($table['fields'] as $fieldName => $field) {
156 if (!empty($field['required']) ||
157 !empty($this->_params
['fields'][$fieldName])
159 // to include optional columns address and email, only if checked
160 if ($tableName == 'civicrm_address') {
161 $this->_addressField
= TRUE;
162 $this->_emailField
= TRUE;
164 elseif ($tableName == 'civicrm_email') {
165 $this->_emailField
= TRUE;
167 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
168 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = $field['type'];
169 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
174 $this->_select
= "SELECT " . implode(', ', $select) . " ";
178 * Generate from clause.
180 public function from() {
184 FROM civicrm_contact {$this->_aliases['civicrm_contact']}
185 INNER JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
186 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_membership']}.contact_id
187 LEFT JOIN civicrm_membership_type {$this->_aliases['civicrm_membership_type']}
188 ON {$this->_aliases['civicrm_membership']}.membership_type_id = {$this->_aliases['civicrm_membership_type']}.id
189 LEFT JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
190 ON {$this->_aliases['civicrm_membership']}.contact_id = {$this->_aliases['civicrm_contribution']}.contact_id
192 // Include address table if address column is to be included.
193 if ($this->_addressField
) {
194 $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";
197 // Include email table if email column is to be included.
198 if ($this->_emailField
) {
199 $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";
204 * Generate where clause.
206 * @todo this looks like it duplicates the parent & could go.
208 public function where() {
210 foreach ($this->_columns
as $tableName => $table) {
211 if (array_key_exists('filters', $table)) {
212 foreach ($table['filters'] as $fieldName => $field) {
214 if ($field['type'] & CRM_Utils_Type
::T_DATE
) {
215 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
216 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
217 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
219 if ($relative ||
$from ||
$to) {
220 $clause = $this->dateClause($field['name'], $relative, $from, $to);
224 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
226 $clause = $this->whereClause($field,
228 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
229 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
230 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
235 if (!empty($clause)) {
236 $clauses[] = $clause;
242 if (empty($clauses)) {
243 $this->_where
= "WHERE ( 1 ) ";
246 $this->_where
= "WHERE " . implode(' AND ', $clauses);
251 * Generate statistics (bottom section of the report).
257 public function statistics(&$rows) {
258 $statistics = array();
259 $statistics[] = array(
260 'title' => ts('Row(s) Listed'),
261 'value' => count($rows),
267 * Generate group by clause.
269 * @todo looks like a broken duplicate of the parent.
271 public function groupBy() {
272 $this->_groupBy
= "";
273 if (is_array($this->_params
['group_bys']) &&
274 !empty($this->_params
['group_bys'])
276 foreach ($this->_columns
as $tableName => $table) {
277 if (array_key_exists('group_bys', $table)) {
278 foreach ($table['group_bys'] as $fieldName => $field) {
279 if (!empty($this->_params
['group_bys'][$fieldName])) {
280 $this->_groupBy
[] = $field['dbAlias'];
286 if (!empty($this->_statFields
) &&
287 (($append && count($this->_groupBy
) <= 1) ||
(!$append))
289 $this->_rollup
= " WITH ROLLUP";
291 $this->_groupBy
= "GROUP BY " . implode(', ', $this->_groupBy
) .
292 " {$this->_rollup} ";
295 $this->_groupBy
= "GROUP BY contact.id";
300 * PostProcess function.
302 public function postProcess() {
303 $this->_params
= $this->controller
->exportValues($this->_name
);
304 if (empty($this->_params
) &&
307 $this->_params
= $this->_formValues
;
309 $this->_formValues
= $this->_params
;
311 $this->processReportMode();
321 $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}";
323 $dao = CRM_Core_DAO
::executeQuery($sql);
324 $rows = $graphRows = array();
326 while ($dao->fetch()) {
328 foreach ($this->_columnHeaders
as $key => $value) {
329 $row[$key] = $dao->$key;
332 if (!empty($this->_params
['charts']) &&
333 $row['civicrm_contribution_receive_date_subtotal']
335 $graphRows['receive_date'][] = $row['civicrm_contribution_receive_date_start'];
336 $graphRows[$this->_interval
][] = $row['civicrm_contribution_receive_date_interval'];
337 $graphRows['value'][] = $row['civicrm_contribution_total_amount_sum'];
343 $this->formatDisplay($rows);
345 $this->assign_by_ref('columnHeaders', $this->_columnHeaders
);
346 $this->assign_by_ref('rows', $rows);
347 $this->assign('statistics', $this->statistics($rows));
349 if (!empty($this->_params
['charts'])) {
355 unset($graphRows[$ignore][$count - 1]);
359 CRM_Utils_OpenFlashChart
::chart($graphRows, $this->_params
['charts'], $this->_interval
);
361 parent
::endPostProcess();
365 * Alter display of rows.
367 * Iterate through the rows retrieved via SQL and make changes for display purposes,
368 * such as rendering contacts as links.
371 * Rows generated by SQL, with an array for each row.
373 public function alterDisplay(&$rows) {
375 $checkList = array();
377 foreach ($rows as $rowNum => $row) {
379 if (!empty($this->_noRepeats
)) {
380 // not repeat contact display names if it matches with the one
383 $repeatFound = FALSE;
384 foreach ($row as $colName => $colVal) {
385 if (is_array($checkList[$colName]) &&
386 in_array($colVal, $checkList[$colName])
388 $rows[$rowNum][$colName] = "";
391 if (in_array($colName, $this->_noRepeats
)) {
392 $checkList[$colName][] = $colVal;
397 //handle the Membership Type Ids
398 if (array_key_exists('civicrm_membership_membership_type_id', $row)) {
399 if ($value = $row['civicrm_membership_membership_type_id']) {
400 $rows[$rowNum]['civicrm_membership_membership_type_id'] = CRM_Member_PseudoConstant
::membershipType($value, FALSE);
405 // handle state province
406 if (array_key_exists('civicrm_address_state_province_id', $row)) {
407 if ($value = $row['civicrm_address_state_province_id']) {
408 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($value, FALSE);
414 if (array_key_exists('civicrm_address_country_id', $row)) {
415 if ($value = $row['civicrm_address_country_id']) {
416 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
421 // convert display name to links
422 if (array_key_exists('civicrm_contact_sort_name', $row) &&
423 array_key_exists('civicrm_contact_id', $row)
425 $url = CRM_Utils_System
::url(
426 'civicrm/report/member/detail',
427 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
430 $rows[$rowNum]['civicrm_contact_sort_name']
431 = "<a href='$url'>" . $row["civicrm_contact_sort_name"] . '</a>';
435 // skip looking further in rows, if first row itself doesn't
436 // have the column we need