Update version in comments
[civicrm-core.git] / CRM / Report / Form / Contact / Summary.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Report_Form_Contact_Summary extends CRM_Report_Form {
36
adda1ff2 37 public $_summary = NULL;
6a488035
TO
38
39 protected $_emailField = FALSE;
40
41 protected $_phoneField = FALSE;
42
43 protected $_customGroupExtends = array(
9d72cede
EM
44 'Contact',
45 'Individual',
46 'Household',
21dfd5f5 47 'Organization',
9d72cede 48 );
6a488035
TO
49
50 public $_drilldownReport = array('contact/detail' => 'Link to Detail Report');
51
74cf4551 52 /**
74cf4551 53 */
00be9182 54 public function __construct() {
6a488035
TO
55 $this->_autoIncludeIndexedFieldsAsOrderBys = 1;
56 $this->_columns = array(
7c550ca0
WA
57 'civicrm_contact' => array(
58 'dao' => 'CRM_Contact_DAO_Contact',
59 'fields' => array(
60 'sort_name' => array(
61 'title' => ts('Contact Name'),
62 'required' => TRUE,
63 'no_repeat' => TRUE,
64 ),
65 'first_name' => array(
66 'title' => ts('First Name'),
67 ),
68 'middle_name' => array(
69 'title' => ts('Middle Name'),
70 ),
71 'last_name' => array(
72 'title' => ts('Last Name'),
73 ),
74 'id' => array(
75 'no_display' => TRUE,
76 'required' => TRUE,
77 ),
78 'contact_type' => array(
79 'title' => ts('Contact Type'),
80 ),
81 'contact_sub_type' => array(
82 'title' => ts('Contact Subtype'),
83 ),
84 'gender_id' => array(
85 'title' => ts('Gender'),
86 ),
87 'birth_date' => array(
88 'title' => ts('Birth Date'),
89 ),
90 'age' => array(
91 'title' => ts('Age'),
92 'dbAlias' => 'TIMESTAMPDIFF(YEAR, contact_civireport.birth_date, CURDATE())',
c0568199 93 ),
6a488035 94 ),
7c550ca0
WA
95 'filters' => array(
96 'sort_name' => array('title' => ts('Contact Name')),
97 'source' => array(
98 'title' => ts('Contact Source'),
99 'type' => CRM_Utils_Type::T_STRING,
100 ),
101 'id' => array(
102 'title' => ts('Contact ID'),
103 'no_display' => TRUE,
104 ),
105 'gender_id' => array(
106 'title' => ts('Gender'),
107 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
108 'options' => CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'),
109 ),
110 'birth_date' => array(
111 'title' => ts('Birth Date'),
112 'operatorType' => CRM_Report_Form::OP_DATE,
113 ),
114 ),
115 'grouping' => 'contact-fields',
116 'order_bys' => array(
117 'sort_name' => array(
118 'title' => ts('Last Name, First Name'),
119 'default' => '1',
120 'default_weight' => '0',
121 'default_order' => 'ASC',
122 ),
123 'gender_id' => array(
124 'name' => 'gender_id',
125 'title' => ts('Gender'),
126 ),
127 'birth_date' => array(
128 'name' => 'birth_date',
129 'title' => ts('Birth Date'),
6a488035
TO
130 ),
131 ),
7c550ca0
WA
132 ),
133 'civicrm_email' => array(
134 'dao' => 'CRM_Core_DAO_Email',
135 'fields' => array(
136 'email' => array(
137 'title' => ts('Email'),
138 'no_repeat' => TRUE,
139 ),
140 ),
141 'grouping' => 'contact-fields',
142 'order_bys' => array(
143 'email' => array(
144 'title' => ts('Email'),
145 ),
146 ),
147 ),
148 'civicrm_phone' => array(
149 'dao' => 'CRM_Core_DAO_Phone',
150 'fields' => array(
151 'phone' => NULL,
152 'phone_ext' => array(
153 'title' => ts('Phone Extension'),
154 ),
6a488035 155 ),
7c550ca0
WA
156 'grouping' => 'contact-fields',
157 ),
158 ) + $this->getAddressColumns(array('group_by' => FALSE));
6a488035 159
16e2e80c 160 $this->_groupFilter = TRUE;
6a488035
TO
161 $this->_tagFilter = TRUE;
162 parent::__construct();
163 }
164
00be9182 165 public function preProcess() {
6a488035
TO
166 parent::preProcess();
167 }
168
00be9182 169 public function select() {
6a488035
TO
170 $select = array();
171 $this->_columnHeaders = array();
172 foreach ($this->_columns as $tableName => $table) {
173 if (array_key_exists('fields', $table)) {
174 foreach ($table['fields'] as $fieldName => $field) {
9d72cede
EM
175 if (!empty($field['required']) ||
176 !empty($this->_params['fields'][$fieldName])
177 ) {
6a488035
TO
178 if ($tableName == 'civicrm_email') {
179 $this->_emailField = TRUE;
180 }
181 elseif ($tableName == 'civicrm_phone') {
182 $this->_phoneField = TRUE;
183 }
184 elseif ($tableName == 'civicrm_country') {
185 $this->_countryField = TRUE;
186 }
187
188 $alias = "{$tableName}_{$fieldName}";
189 $select[] = "{$field['dbAlias']} as {$alias}";
190 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
191 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
192 $this->_selectAliases[] = $alias;
193 }
194 }
195 }
196 }
197
198 $this->_select = "SELECT " . implode(', ', $select) . " ";
199 }
200
74cf4551
EM
201 /**
202 * @param $fields
203 * @param $files
204 * @param $self
205 *
206 * @return array
207 */
00be9182 208 public static function formRule($fields, $files, $self) {
6a488035
TO
209 $errors = $grouping = array();
210 return $errors;
211 }
212
00be9182 213 public function from() {
6a488035
TO
214 $this->_from = "
215 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
2f4c2f5d 216 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
217 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
6a488035
TO
218 {$this->_aliases['civicrm_address']}.is_primary = 1 ) ";
219
220 if ($this->isTableSelected('civicrm_email')) {
221 $this->_from .= "
2f4c2f5d 222 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
6a488035
TO
223 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
224 {$this->_aliases['civicrm_email']}.is_primary = 1) ";
225 }
226
227 if ($this->_phoneField) {
228 $this->_from .= "
2f4c2f5d 229 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
230 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
6a488035
TO
231 {$this->_aliases['civicrm_phone']}.is_primary = 1 ";
232 }
233
234 if ($this->isTableSelected('civicrm_country')) {
235 $this->_from .= "
236 LEFT JOIN civicrm_country {$this->_aliases['civicrm_country']}
237 ON {$this->_aliases['civicrm_address']}.country_id = {$this->_aliases['civicrm_country']}.id AND
238 {$this->_aliases['civicrm_address']}.is_primary = 1 ";
239 }
240 }
241
00be9182 242 public function postProcess() {
6a488035
TO
243
244 $this->beginPostProcess();
245
246 // get the acl clauses built before we assemble the query
247 $this->buildACLClause($this->_aliases['civicrm_contact']);
248
249 $sql = $this->buildQuery(TRUE);
250
251 $rows = $graphRows = array();
252 $this->buildRows($sql, $rows);
253
254 $this->formatDisplay($rows);
255 $this->doTemplateAssignment($rows);
256 $this->endPostProcess($rows);
257 }
258
74cf4551
EM
259 /**
260 * @param $rows
decced8b
EM
261 *
262 * @return bool
74cf4551 263 */
9d72cede 264 private function _initBasicRow(&$rows, &$entryFound, $row, $rowId, $rowNum, $types) {
1fd5431f 265 if (!array_key_exists($rowId, $row)) {
266 return FALSE;
267 }
268
269 $value = $row[$rowId];
270 if ($value) {
271 $rows[$rowNum][$rowId] = $types[$value];
272 }
273 $entryFound = TRUE;
274 }
275
ced9bfed
EM
276 /**
277 * Alter display of rows.
278 *
279 * Iterate through the rows retrieved via SQL and make changes for display purposes,
280 * such as rendering contacts as links.
281 *
282 * @param array $rows
283 * Rows generated by SQL, with an array for each row.
284 */
00be9182 285 public function alterDisplay(&$rows) {
6a488035 286 $entryFound = FALSE;
1fd5431f 287
288 $genders = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id', array('localize' => TRUE));
289
6a488035
TO
290 foreach ($rows as $rowNum => $row) {
291 // make count columns point to detail report
292 // convert sort name to links
293 if (array_key_exists('civicrm_contact_sort_name', $row) &&
294 array_key_exists('civicrm_contact_id', $row)
295 ) {
296 $url = CRM_Report_Utils_Report::getNextUrl('contact/detail',
297 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
298 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
299 );
300 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
301 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Constituent Detail Report for this contact.");
302 $entryFound = TRUE;
303 }
304
305 if (array_key_exists('civicrm_address_state_province_id', $row)) {
306 if ($value = $row['civicrm_address_state_province_id']) {
307 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvince($value, FALSE);
308 }
309 $entryFound = TRUE;
310 }
311
cb1dc4f3 312 if (array_key_exists('civicrm_address_country_id', $row)) {
313 if ($value = $row['civicrm_address_country_id']) {
314 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country($value, FALSE);
315 }
316 $entryFound = TRUE;
317 }
318
1fd5431f 319 // handle gender id
320 $this->_initBasicRow($rows, $entryFound, $row, 'civicrm_contact_gender_id', $rowNum, $genders);
321
322 // display birthday in the configured custom format
323 if (array_key_exists('civicrm_contact_birth_date', $row)) {
324 $birthDate = $row['civicrm_contact_birth_date'];
325 if ($birthDate) {
326 $rows[$rowNum]['civicrm_contact_birth_date'] = CRM_Utils_Date::customFormat($birthDate, '%Y%m%d');
327 }
328 $entryFound = TRUE;
329 }
6a488035
TO
330
331 // skip looking further in rows, if first row itself doesn't
332 // have the column we need
333 if (!$entryFound) {
334 break;
335 }
336 }
337 }
96025800 338
6a488035 339}