4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2014
36 class CRM_Report_Form_Membership_Summary
extends CRM_Report_Form
{
38 protected $_summary = NULL;
40 protected $_charts = array(
42 'barChart' => 'Bar Chart',
43 'pieChart' => 'Pie Chart',
52 function __construct() {
53 // UI for selecting columns to appear in the report list
54 // array conatining the columns, group_bys and filters build and provided to Form
55 $this->_columns
= array(
56 'civicrm_contact' => array(
57 'dao' => 'CRM_Contact_DAO_Contact',
60 'title' => ts('Member Name'),
70 'id' => array('title' => ts('Contact ID')),
71 'display_name' => array(
72 'title' => ts('Contact Name'),
75 'grouping' => 'contact-fields',
77 'civicrm_membership_type' => array(
78 'dao' => 'CRM_Member_DAO_MembershipType',
79 'grouping' => 'member-fields',
83 'title' => ts('Membership Types'),
84 'type' => CRM_Utils_Type
::T_INT + CRM_Utils_Type
::T_ENUM
,
85 'options' => CRM_Member_PseudoConstant
::membershipType(),
89 'civicrm_membership' => array(
90 'dao' => 'CRM_Member_DAO_Membership',
91 'grouping' => 'member-fields',
93 'membership_type_id' => array(
94 'title' => 'Membership Type',
98 'start_date' => array(
99 'title' => ts('Current Cycle Start Date'),
102 'title' => ts('Current Cycle End Date'),
105 'group_bys' => array(
106 'membership_type_id' => array('title' => ts('Membership Type')),
109 'join_date' => array('type' => CRM_Utils_Type
::T_DATE
),
112 'civicrm_address' => array(
113 'dao' => 'CRM_Core_DAO_Address',
115 'street_address' => NULL,
117 'postal_code' => NULL,
118 'state_province_id' => array(
119 'title' => ts('State/Province'),
121 'country_id' => array(
122 'title' => ts('Country'),
126 'grouping' => 'contact-fields',
128 'civicrm_email' => array(
129 'dao' => 'CRM_Core_DAO_Email',
130 'fields' => array('email' => NULL),
131 'grouping' => 'contact-fields',
133 'civicrm_contribution' => array(
134 'dao' => 'CRM_Contribute_DAO_Contribution',
136 'total_amount' => array(
137 'title' => ts('Contribution Amount'),
142 parent
::__construct();
145 function preProcess() {
146 $this->assign('reportTitle', ts('Membership Summary Report'));
147 parent
::preProcess();
153 function setDefaultValues() {
154 return parent
::setDefaultValues();
159 $this->_columnHeaders
= array();
160 foreach ($this->_columns
as $tableName => $table) {
161 if (array_key_exists('fields', $table)) {
162 foreach ($table['fields'] as $fieldName => $field) {
163 if (!empty($field['required']) ||
164 !empty($this->_params
['fields'][$fieldName])
166 // to include optional columns address and email, only if checked
167 if ($tableName == 'civicrm_address') {
168 $this->_addressField
= TRUE;
169 $this->_emailField
= TRUE;
171 elseif ($tableName == 'civicrm_email') {
172 $this->_emailField
= TRUE;
174 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
175 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = $field['type'];
176 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
181 $this->_select
= "SELECT " . implode(', ', $select) . " ";
191 static function formRule($fields, $files, $self) {
192 $errors = $grouping = array();
193 //check for searching combination of dispaly columns and
203 FROM civicrm_contact {$this->_aliases['civicrm_contact']}
204 INNER JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
205 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_membership']}.contact_id
206 LEFT JOIN civicrm_membership_type {$this->_aliases['civicrm_membership_type']}
207 ON {$this->_aliases['civicrm_membership']}.membership_type_id = {$this->_aliases['civicrm_membership_type']}.id
208 LEFT JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
209 ON {$this->_aliases['civicrm_membership']}.contact_id = {$this->_aliases['civicrm_contribution']}.contact_id
211 // include address field if address column is to be included
212 if ($this->_addressField
) {
213 $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";
216 // include email field if email column is to be included
217 if ($this->_emailField
) {
218 $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";
224 foreach ($this->_columns
as $tableName => $table) {
225 if (array_key_exists('filters', $table)) {
226 foreach ($table['filters'] as $fieldName => $field) {
228 if ($field['type'] & CRM_Utils_Type
::T_DATE
) {
229 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
230 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
231 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
233 if ($relative ||
$from ||
$to) {
234 $clause = $this->dateClause($field['name'], $relative, $from, $to);
238 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
240 $clause = $this->whereClause($field,
242 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
243 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
244 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
249 if (!empty($clause)) {
250 $clauses[] = $clause;
256 if (empty($clauses)) {
257 $this->_where
= "WHERE ( 1 ) ";
260 $this->_where
= "WHERE " . implode(' AND ', $clauses);
269 function statistics(&$rows) {
270 $statistics = array();
271 $statistics[] = array(
272 'title' => ts('Row(s) Listed'),
273 'value' => count($rows),
279 $this->_groupBy
= "";
280 if (is_array($this->_params
['group_bys']) &&
281 !empty($this->_params
['group_bys'])
283 foreach ($this->_columns
as $tableName => $table) {
284 if (array_key_exists('group_bys', $table)) {
285 foreach ($table['group_bys'] as $fieldName => $field) {
286 if (!empty($this->_params
['group_bys'][$fieldName])) {
287 $this->_groupBy
[] = $field['dbAlias'];
293 if (!empty($this->_statFields
) &&
294 (($append && count($this->_groupBy
) <= 1) ||
(!$append))
296 $this->_rollup
= " WITH ROLLUP";
298 $this->_groupBy
= "GROUP BY " . implode(', ', $this->_groupBy
) .
299 " {$this->_rollup} ";
302 $this->_groupBy
= "GROUP BY contact.id";
306 function postProcess() {
307 $this->_params
= $this->controller
->exportValues($this->_name
);
308 if (empty($this->_params
) &&
311 $this->_params
= $this->_formValues
;
313 $this->_formValues
= $this->_params
;
315 $this->processReportMode();
325 $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}";
327 $dao = CRM_Core_DAO
::executeQuery($sql);
328 $rows = $graphRows = array();
330 while ($dao->fetch()) {
332 foreach ($this->_columnHeaders
as $key => $value) {
333 $row[$key] = $dao->$key;
336 if (!empty($this->_params
['charts']) &&
337 $row['civicrm_contribution_receive_date_subtotal']
339 $graphRows['receive_date'][] = $row['civicrm_contribution_receive_date_start'];
340 $graphRows[$this->_interval
][] = $row['civicrm_contribution_receive_date_interval'];
341 $graphRows['value'][] = $row['civicrm_contribution_total_amount_sum'];
347 $this->formatDisplay($rows);
349 $this->assign_by_ref('columnHeaders', $this->_columnHeaders
);
350 $this->assign_by_ref('rows', $rows);
351 $this->assign('statistics', $this->statistics($rows));
353 if (!empty($this->_params
['charts'])) {
359 unset($graphRows[$ignore][$count - 1]);
363 CRM_Utils_OpenFlashChart
::chart($graphRows, $this->_params
['charts'], $this->_interval
);
365 parent
::endPostProcess();
371 function alterDisplay(&$rows) {
372 // custom code to alter rows
374 $checkList = array();
376 foreach ($rows as $rowNum => $row) {
378 if (!empty($this->_noRepeats
)) {
379 // not repeat contact display names if it matches with the one
382 $repeatFound = FALSE;
383 foreach ($row as $colName => $colVal) {
384 if (is_array($checkList[$colName]) &&
385 in_array($colVal, $checkList[$colName])
387 $rows[$rowNum][$colName] = "";
390 if (in_array($colName, $this->_noRepeats
)) {
391 $checkList[$colName][] = $colVal;
396 //handle the Membership Type Ids
397 if (array_key_exists('civicrm_membership_membership_type_id', $row)) {
398 if ($value = $row['civicrm_membership_membership_type_id']) {
399 $rows[$rowNum]['civicrm_membership_membership_type_id'] = CRM_Member_PseudoConstant
::membershipType($value, FALSE);
404 // handle state province
405 if (array_key_exists('civicrm_address_state_province_id', $row)) {
406 if ($value = $row['civicrm_address_state_province_id']) {
407 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($value, FALSE);
413 if (array_key_exists('civicrm_address_country_id', $row)) {
414 if ($value = $row['civicrm_address_country_id']) {
415 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
420 // convert display name to links
421 if (array_key_exists('civicrm_contact_sort_name', $row) &&
422 array_key_exists('civicrm_contact_id', $row)
424 $url = CRM_Utils_System
::url('civicrm/report/member/detail',
425 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
428 $rows[$rowNum]['civicrm_contact_sort_name'] =
429 "<a href='$url'>" . $row["civicrm_contact_sort_name"] . '</a>';
433 // skip looking further in rows, if first row itself doesn't
434 // have the column we need