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(
58 'dao' => 'CRM_Contact_DAO_Contact',
62 array('title' => ts('Member Name'),
75 array('title' => ts('Contact ID')),
77 array('title' => ts('Contact Name'),
80 'grouping' => 'contact-fields',
82 'civicrm_membership_type' =>
84 'dao' => 'CRM_Member_DAO_MembershipType',
85 'grouping' => 'member-fields',
91 'title' => ts('Membership Types'),
92 'type' => CRM_Utils_Type
::T_INT + CRM_Utils_Type
::T_ENUM
,
93 'options' => CRM_Member_PseudoConstant
::membershipType(),
97 'civicrm_membership' =>
99 'dao' => 'CRM_Member_DAO_Membership',
100 'grouping' => 'member-fields',
103 'membership_type_id' =>
105 'title' => 'Membership Type',
109 'start_date' => array('title' => ts('Current Cycle Start Date'),
111 'end_date' => array('title' => ts('Current Cycle End Date'),
116 'membership_type_id' =>
117 array('title' => ts('Membership Type')),
122 array('type' => CRM_Utils_Type
::T_DATE
),
127 'dao' => 'CRM_Core_DAO_Address',
130 'street_address' => NULL,
132 'postal_code' => NULL,
133 'state_province_id' =>
134 array('title' => ts('State/Province'),
137 array('title' => ts('Country'),
141 'grouping' => 'contact-fields',
145 'dao' => 'CRM_Core_DAO_Email',
147 array('email' => NULL),
148 'grouping' => 'contact-fields',
150 'civicrm_contribution' =>
152 'dao' => 'CRM_Contribute_DAO_Contribution',
156 array('title' => ts('Contribution Amount'),
161 parent
::__construct();
164 function preProcess() {
165 $this->assign('reportTitle', ts('Membership Summary Report'));
166 parent
::preProcess();
172 function setDefaultValues() {
173 return parent
::setDefaultValues();
178 $this->_columnHeaders
= array();
179 foreach ($this->_columns
as $tableName => $table) {
180 if (array_key_exists('fields', $table)) {
181 foreach ($table['fields'] as $fieldName => $field) {
182 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
183 // to include optional columns address and email, only if checked
184 if ($tableName == 'civicrm_address') {
185 $this->_addressField
= TRUE;
186 $this->_emailField
= TRUE;
188 elseif ($tableName == 'civicrm_email') {
189 $this->_emailField
= TRUE;
191 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
192 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = $field['type'];
193 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'];
198 $this->_select
= "SELECT " . implode(', ', $select) . " ";
208 static function formRule($fields, $files, $self) {
209 $errors = $grouping = array();
210 //check for searching combination of dispaly columns and
220 FROM civicrm_contact {$this->_aliases['civicrm_contact']}
221 INNER JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
222 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_membership']}.contact_id
223 LEFT JOIN civicrm_membership_type {$this->_aliases['civicrm_membership_type']}
224 ON {$this->_aliases['civicrm_membership']}.membership_type_id = {$this->_aliases['civicrm_membership_type']}.id
225 LEFT JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
226 ON {$this->_aliases['civicrm_membership']}.contact_id = {$this->_aliases['civicrm_contribution']}.contact_id
228 // include address field if address column is to be included
229 if ($this->_addressField
) {
230 $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";
233 // include email field if email column is to be included
234 if ($this->_emailField
) {
235 $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";
241 foreach ($this->_columns
as $tableName => $table) {
242 if (array_key_exists('filters', $table)) {
243 foreach ($table['filters'] as $fieldName => $field) {
245 if ($field['type'] & CRM_Utils_Type
::T_DATE
) {
246 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
247 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
248 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
250 if ($relative ||
$from ||
$to) {
251 $clause = $this->dateClause($field['name'], $relative, $from, $to);
255 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
257 $clause = $this->whereClause($field,
259 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
260 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
261 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
266 if (!empty($clause)) {
267 $clauses[] = $clause;
273 if (empty($clauses)) {
274 $this->_where
= "WHERE ( 1 ) ";
277 $this->_where
= "WHERE " . implode(' AND ', $clauses);
286 function statistics(&$rows) {
287 $statistics = array();
288 $statistics[] = array('title' => ts('Row(s) Listed'),
289 'value' => count($rows),
295 $this->_groupBy
= "";
296 if (is_array($this->_params
['group_bys']) &&
297 !empty($this->_params
['group_bys'])
299 foreach ($this->_columns
as $tableName => $table) {
300 if (array_key_exists('group_bys', $table)) {
301 foreach ($table['group_bys'] as $fieldName => $field) {
302 if (!empty($this->_params
['group_bys'][$fieldName])) {
303 $this->_groupBy
[] = $field['dbAlias'];
309 if (!empty($this->_statFields
) &&
310 (($append && count($this->_groupBy
) <= 1) ||
(!$append))
312 $this->_rollup
= " WITH ROLLUP";
314 $this->_groupBy
= "GROUP BY " . implode(', ', $this->_groupBy
) . " {$this->_rollup} ";
317 $this->_groupBy
= "GROUP BY contact.id";
321 function postProcess() {
322 $this->_params
= $this->controller
->exportValues($this->_name
);
323 if (empty($this->_params
) &&
326 $this->_params
= $this->_formValues
;
328 $this->_formValues
= $this->_params
;
330 $this->processReportMode();
340 $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy}";
342 $dao = CRM_Core_DAO
::executeQuery($sql);
343 $rows = $graphRows = array();
345 while ($dao->fetch()) {
347 foreach ($this->_columnHeaders
as $key => $value) {
348 $row[$key] = $dao->$key;
351 if (!empty($this->_params
['charts']) &&
352 $row['civicrm_contribution_receive_date_subtotal']
354 $graphRows['receive_date'][] = $row['civicrm_contribution_receive_date_start'];
355 $graphRows[$this->_interval
][] = $row['civicrm_contribution_receive_date_interval'];
356 $graphRows['value'][] = $row['civicrm_contribution_total_amount_sum'];
362 $this->formatDisplay($rows);
364 $this->assign_by_ref('columnHeaders', $this->_columnHeaders
);
365 $this->assign_by_ref('rows', $rows);
366 $this->assign('statistics', $this->statistics($rows));
368 if (!empty($this->_params
['charts'])) {
370 'receive_date', $this->_interval
, 'value') as $ignore) {
371 unset($graphRows[$ignore][$count - 1]);
375 CRM_Utils_OpenFlashChart
::chart($graphRows, $this->_params
['charts'], $this->_interval
);
377 parent
::endPostProcess();
383 function alterDisplay(&$rows) {
384 // custom code to alter rows
386 $checkList = array();
388 foreach ($rows as $rowNum => $row) {
390 if (!empty($this->_noRepeats
)) {
391 // not repeat contact display names if it matches with the one
394 $repeatFound = FALSE;
395 foreach ($row as $colName => $colVal) {
396 if (is_array($checkList[$colName]) &&
397 in_array($colVal, $checkList[$colName])
399 $rows[$rowNum][$colName] = "";
402 if (in_array($colName, $this->_noRepeats
)) {
403 $checkList[$colName][] = $colVal;
408 //handle the Membership Type Ids
409 if (array_key_exists('civicrm_membership_membership_type_id', $row)) {
410 if ($value = $row['civicrm_membership_membership_type_id']) {
411 $rows[$rowNum]['civicrm_membership_membership_type_id'] = CRM_Member_PseudoConstant
::membershipType($value, FALSE);
416 // handle state province
417 if (array_key_exists('civicrm_address_state_province_id', $row)) {
418 if ($value = $row['civicrm_address_state_province_id']) {
419 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($value, FALSE);
425 if (array_key_exists('civicrm_address_country_id', $row)) {
426 if ($value = $row['civicrm_address_country_id']) {
427 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
432 // convert display name to links
433 if (array_key_exists('civicrm_contact_sort_name', $row) &&
434 array_key_exists('civicrm_contact_id', $row)
436 $url = CRM_Utils_System
::url('civicrm/report/member/detail',
437 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
440 $rows[$rowNum]['civicrm_contact_sort_name'] = "<a href='$url'>" . $row["civicrm_contact_sort_name"] . '</a>';
444 // skip looking further in rows, if first row itself doesn't
445 // have the column we need