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