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