3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
34 class CRM_Report_Form_Membership_Summary
extends CRM_Report_Form
{
36 protected $_summary = NULL;
38 protected $_charts = array(
40 'barChart' => 'Bar Chart',
41 'pieChart' => 'Pie Chart',
45 * Constructor function.
47 public function __construct() {
48 // UI for selecting columns to appear in the report list
49 // array containing the columns, group_bys and filters build and provided to Form
50 $this->_columns
= array(
51 'civicrm_contact' => array(
52 'dao' => 'CRM_Contact_DAO_Contact',
55 'title' => ts('Member Name'),
65 'id' => array('title' => ts('Contact ID')),
66 'display_name' => array(
67 'title' => ts('Contact Name'),
70 'grouping' => 'contact-fields',
72 'civicrm_membership_type' => array(
73 'dao' => 'CRM_Member_DAO_MembershipType',
74 'grouping' => 'member-fields',
78 'title' => ts('Membership Types'),
79 'type' => CRM_Utils_Type
::T_INT + CRM_Utils_Type
::T_ENUM
,
80 'options' => CRM_Member_PseudoConstant
::membershipType(),
84 'civicrm_membership' => array(
85 'dao' => 'CRM_Member_DAO_Membership',
86 'grouping' => 'member-fields',
88 'membership_type_id' => array(
89 'title' => 'Membership Type',
93 'start_date' => array(
94 'title' => ts('Current Cycle Start Date'),
97 'title' => ts('Current Cycle End Date'),
100 'group_bys' => array(
101 'membership_type_id' => array('title' => ts('Membership Type')),
104 'join_date' => array('type' => CRM_Utils_Type
::T_DATE
),
107 'civicrm_address' => array(
108 'dao' => 'CRM_Core_DAO_Address',
110 'street_address' => NULL,
112 'postal_code' => NULL,
113 'state_province_id' => array(
114 'title' => ts('State/Province'),
116 'country_id' => array(
117 'title' => ts('Country'),
121 'grouping' => 'contact-fields',
123 'civicrm_email' => array(
124 'dao' => 'CRM_Core_DAO_Email',
125 'fields' => array('email' => NULL),
126 'grouping' => 'contact-fields',
128 'civicrm_contribution' => array(
129 'dao' => 'CRM_Contribute_DAO_Contribution',
131 'total_amount' => array(
132 'title' => ts('Contribution Amount'),
137 parent
::__construct();
141 * Pre-process function.
143 public function preProcess() {
144 $this->assign('reportTitle', ts('Membership Summary Report'));
145 parent
::preProcess();
149 * Set default values.
154 public function setDefaultValues() {
155 return parent
::setDefaultValues();
159 * Generate select clause.
161 public function select() {
163 $this->_columnHeaders
= array();
164 foreach ($this->_columns
as $tableName => $table) {
165 if (array_key_exists('fields', $table)) {
166 foreach ($table['fields'] as $fieldName => $field) {
167 if (!empty($field['required']) ||
168 !empty($this->_params
['fields'][$fieldName])
170 // to include optional columns address and email, only if checked
171 if ($tableName == 'civicrm_address') {
172 $this->_addressField
= TRUE;
173 $this->_emailField
= TRUE;
175 elseif ($tableName == 'civicrm_email') {
176 $this->_emailField
= TRUE;
178 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
179 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = $field['type'];
180 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
185 $this->_select
= "SELECT " . implode(', ', $select) . " ";
197 public static function formRule($fields, $files, $self) {
198 $errors = $grouping = array();
199 //check for searching combination of dispaly columns and
205 public function from() {
209 FROM civicrm_contact {$this->_aliases['civicrm_contact']}
210 INNER JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
211 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_membership']}.contact_id
212 LEFT JOIN civicrm_membership_type {$this->_aliases['civicrm_membership_type']}
213 ON {$this->_aliases['civicrm_membership']}.membership_type_id = {$this->_aliases['civicrm_membership_type']}.id
214 LEFT JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
215 ON {$this->_aliases['civicrm_membership']}.contact_id = {$this->_aliases['civicrm_contribution']}.contact_id
217 // Include address table if address column is to be included.
218 if ($this->_addressField
) {
219 $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";
222 // Include email table if email column is to be included.
223 if ($this->_emailField
) {
224 $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";
228 public function where() {
230 foreach ($this->_columns
as $tableName => $table) {
231 if (array_key_exists('filters', $table)) {
232 foreach ($table['filters'] as $fieldName => $field) {
234 if ($field['type'] & CRM_Utils_Type
::T_DATE
) {
235 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
236 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
237 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
239 if ($relative ||
$from ||
$to) {
240 $clause = $this->dateClause($field['name'], $relative, $from, $to);
244 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
246 $clause = $this->whereClause($field,
248 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
249 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
250 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
255 if (!empty($clause)) {
256 $clauses[] = $clause;
262 if (empty($clauses)) {
263 $this->_where
= "WHERE ( 1 ) ";
266 $this->_where
= "WHERE " . implode(' AND ', $clauses);
271 * Generate statistics (bottom section of the report).
277 public function statistics(&$rows) {
278 $statistics = array();
279 $statistics[] = array(
280 'title' => ts('Row(s) Listed'),
281 'value' => count($rows),
286 public function groupBy() {
287 $this->_groupBy
= "";
288 if (is_array($this->_params
['group_bys']) &&
289 !empty($this->_params
['group_bys'])
291 foreach ($this->_columns
as $tableName => $table) {
292 if (array_key_exists('group_bys', $table)) {
293 foreach ($table['group_bys'] as $fieldName => $field) {
294 if (!empty($this->_params
['group_bys'][$fieldName])) {
295 $this->_groupBy
[] = $field['dbAlias'];
301 if (!empty($this->_statFields
) &&
302 (($append && count($this->_groupBy
) <= 1) ||
(!$append))
304 $this->_rollup
= " WITH ROLLUP";
306 $this->_groupBy
= "GROUP BY " . implode(', ', $this->_groupBy
) .
307 " {$this->_rollup} ";
310 $this->_groupBy
= "GROUP BY contact.id";
314 public function postProcess() {
315 $this->_params
= $this->controller
->exportValues($this->_name
);
316 if (empty($this->_params
) &&
319 $this->_params
= $this->_formValues
;
321 $this->_formValues
= $this->_params
;
323 $this->processReportMode();
333 $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}";
335 $dao = CRM_Core_DAO
::executeQuery($sql);
336 $rows = $graphRows = array();
338 while ($dao->fetch()) {
340 foreach ($this->_columnHeaders
as $key => $value) {
341 $row[$key] = $dao->$key;
344 if (!empty($this->_params
['charts']) &&
345 $row['civicrm_contribution_receive_date_subtotal']
347 $graphRows['receive_date'][] = $row['civicrm_contribution_receive_date_start'];
348 $graphRows[$this->_interval
][] = $row['civicrm_contribution_receive_date_interval'];
349 $graphRows['value'][] = $row['civicrm_contribution_total_amount_sum'];
355 $this->formatDisplay($rows);
357 $this->assign_by_ref('columnHeaders', $this->_columnHeaders
);
358 $this->assign_by_ref('rows', $rows);
359 $this->assign('statistics', $this->statistics($rows));
361 if (!empty($this->_params
['charts'])) {
367 unset($graphRows[$ignore][$count - 1]);
371 CRM_Utils_OpenFlashChart
::chart($graphRows, $this->_params
['charts'], $this->_interval
);
373 parent
::endPostProcess();
377 * Alter display of rows.
379 * Iterate through the rows retrieved via SQL and make changes for display purposes,
380 * such as rendering contacts as links.
383 * Rows generated by SQL, with an array for each row.
385 public function alterDisplay(&$rows) {
387 $checkList = array();
389 foreach ($rows as $rowNum => $row) {
391 if (!empty($this->_noRepeats
)) {
392 // not repeat contact display names if it matches with the one
395 $repeatFound = FALSE;
396 foreach ($row as $colName => $colVal) {
397 if (is_array($checkList[$colName]) &&
398 in_array($colVal, $checkList[$colName])
400 $rows[$rowNum][$colName] = "";
403 if (in_array($colName, $this->_noRepeats
)) {
404 $checkList[$colName][] = $colVal;
409 //handle the Membership Type Ids
410 if (array_key_exists('civicrm_membership_membership_type_id', $row)) {
411 if ($value = $row['civicrm_membership_membership_type_id']) {
412 $rows[$rowNum]['civicrm_membership_membership_type_id'] = CRM_Member_PseudoConstant
::membershipType($value, FALSE);
417 // handle state province
418 if (array_key_exists('civicrm_address_state_province_id', $row)) {
419 if ($value = $row['civicrm_address_state_province_id']) {
420 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($value, FALSE);
426 if (array_key_exists('civicrm_address_country_id', $row)) {
427 if ($value = $row['civicrm_address_country_id']) {
428 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
433 // convert display name to links
434 if (array_key_exists('civicrm_contact_sort_name', $row) &&
435 array_key_exists('civicrm_contact_id', $row)
437 $url = CRM_Utils_System
::url(
438 'civicrm/report/member/detail',
439 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
442 $rows[$rowNum]['civicrm_contact_sort_name']
443 = "<a href='$url'>" . $row["civicrm_contact_sort_name"] . '</a>';
447 // skip looking further in rows, if first row itself doesn't
448 // have the column we need