Merge pull request #10466 from kryptothesuperdog/master
[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-2017 |
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-2017
32 */
33 class CRM_Report_Form_Contact_Summary extends CRM_Report_Form {
34
35 public $_summary = NULL;
36
37 protected $_emailField = FALSE;
38
39 protected $_phoneField = FALSE;
40
41 protected $_customGroupExtends = array(
42 'Contact',
43 'Individual',
44 'Household',
45 'Organization',
46 );
47
48 public $_drilldownReport = array('contact/detail' => 'Link to Detail Report');
49
50 /**
51 * This report has not been optimised for group filtering.
52 *
53 * The functionality for group filtering has been improved but not
54 * all reports have been adjusted to take care of it. This report has not
55 * and will run an inefficient query until fixed.
56 *
57 * CRM-19170
58 *
59 * @var bool
60 */
61 protected $groupFilterNotOptimised = TRUE;
62
63 /**
64 * Class constructor.
65 */
66 public function __construct() {
67 $this->_autoIncludeIndexedFieldsAsOrderBys = 1;
68 $this->_columns = array(
69 'civicrm_contact' => array(
70 'dao' => 'CRM_Contact_DAO_Contact',
71 'fields' => array_merge(
72 $this->getBasicContactFields(),
73 array(
74 'modified_date' => array(
75 'title' => ts('Modified Date'),
76 'default' => FALSE,
77 ),
78 )
79 ),
80 'filters' => $this->getBasicContactFilters(),
81 'grouping' => 'contact-fields',
82 'order_bys' => array(
83 'sort_name' => array(
84 'title' => ts('Last Name, First Name'),
85 'default' => '1',
86 'default_weight' => '0',
87 'default_order' => 'ASC',
88 ),
89 'first_name' => array(
90 'name' => 'first_name',
91 'title' => ts('First Name'),
92 ),
93 'gender_id' => array(
94 'name' => 'gender_id',
95 'title' => ts('Gender'),
96 ),
97 'birth_date' => array(
98 'name' => 'birth_date',
99 'title' => ts('Birth Date'),
100 ),
101 'contact_type' => array(
102 'title' => ts('Contact Type'),
103 ),
104 'contact_sub_type' => array(
105 'title' => ts('Contact Subtype'),
106 ),
107 ),
108 ),
109 'civicrm_email' => array(
110 'dao' => 'CRM_Core_DAO_Email',
111 'fields' => array(
112 'email' => array(
113 'title' => ts('Email'),
114 'no_repeat' => TRUE,
115 ),
116 ),
117 'grouping' => 'contact-fields',
118 'order_bys' => array(
119 'email' => array(
120 'title' => ts('Email'),
121 ),
122 ),
123 ),
124 'civicrm_phone' => array(
125 'dao' => 'CRM_Core_DAO_Phone',
126 'fields' => array(
127 'phone' => NULL,
128 'phone_ext' => array(
129 'title' => ts('Phone Extension'),
130 ),
131 ),
132 'grouping' => 'contact-fields',
133 ),
134 ) + $this->getAddressColumns(array('group_by' => FALSE));
135
136 $this->_groupFilter = TRUE;
137 $this->_tagFilter = TRUE;
138 parent::__construct();
139 }
140
141 public function preProcess() {
142 parent::preProcess();
143 }
144
145 public function select() {
146 $select = array();
147 $this->_columnHeaders = array();
148 foreach ($this->_columns as $tableName => $table) {
149 if (array_key_exists('fields', $table)) {
150 foreach ($table['fields'] as $fieldName => $field) {
151 if (!empty($field['required']) ||
152 !empty($this->_params['fields'][$fieldName])
153 ) {
154 if ($tableName == 'civicrm_email') {
155 $this->_emailField = TRUE;
156 }
157 elseif ($tableName == 'civicrm_phone') {
158 $this->_phoneField = TRUE;
159 }
160 elseif ($tableName == 'civicrm_country') {
161 $this->_countryField = TRUE;
162 }
163
164 $alias = "{$tableName}_{$fieldName}";
165 $select[] = "{$field['dbAlias']} as {$alias}";
166 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
167 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
168 $this->_selectAliases[] = $alias;
169 }
170 }
171 }
172 }
173
174 $this->_select = "SELECT " . implode(', ', $select) . " ";
175 }
176
177 /**
178 * @param $fields
179 * @param $files
180 * @param $self
181 *
182 * @return array
183 */
184 public static function formRule($fields, $files, $self) {
185 $errors = $grouping = array();
186 return $errors;
187 }
188
189 public function from() {
190 $this->_from = "
191 FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
192 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
193 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
194 {$this->_aliases['civicrm_address']}.is_primary = 1 ) ";
195
196 if ($this->isTableSelected('civicrm_email')) {
197 $this->_from .= "
198 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
199 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
200 {$this->_aliases['civicrm_email']}.is_primary = 1) ";
201 }
202
203 if ($this->_phoneField) {
204 $this->_from .= "
205 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
206 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
207 {$this->_aliases['civicrm_phone']}.is_primary = 1 ";
208 }
209
210 if ($this->isTableSelected('civicrm_country')) {
211 $this->_from .= "
212 LEFT JOIN civicrm_country {$this->_aliases['civicrm_country']}
213 ON {$this->_aliases['civicrm_address']}.country_id = {$this->_aliases['civicrm_country']}.id AND
214 {$this->_aliases['civicrm_address']}.is_primary = 1 ";
215 }
216 }
217
218 public function postProcess() {
219
220 $this->beginPostProcess();
221
222 // get the acl clauses built before we assemble the query
223 $this->buildACLClause($this->_aliases['civicrm_contact']);
224
225 $sql = $this->buildQuery(TRUE);
226
227 $rows = $graphRows = array();
228 $this->buildRows($sql, $rows);
229
230 $this->formatDisplay($rows);
231 $this->doTemplateAssignment($rows);
232 $this->endPostProcess($rows);
233 }
234
235 /**
236 * Alter display of rows.
237 *
238 * Iterate through the rows retrieved via SQL and make changes for display purposes,
239 * such as rendering contacts as links.
240 *
241 * @param array $rows
242 * Rows generated by SQL, with an array for each row.
243 */
244 public function alterDisplay(&$rows) {
245 $entryFound = FALSE;
246
247 foreach ($rows as $rowNum => $row) {
248 // make count columns point to detail report
249 // convert sort name to links
250 if (array_key_exists('civicrm_contact_sort_name', $row) &&
251 array_key_exists('civicrm_contact_id', $row)
252 ) {
253 $url = CRM_Report_Utils_Report::getNextUrl('contact/detail',
254 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
255 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
256 );
257 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
258 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts('View Contact Detail Report for this contact');
259 $entryFound = TRUE;
260 }
261
262 if (array_key_exists('civicrm_address_state_province_id', $row)) {
263 if ($value = $row['civicrm_address_state_province_id']) {
264 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvince($value, FALSE);
265 }
266 $entryFound = TRUE;
267 }
268
269 if (array_key_exists('civicrm_address_country_id', $row)) {
270 if ($value = $row['civicrm_address_country_id']) {
271 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country($value, FALSE);
272 }
273 $entryFound = TRUE;
274 }
275
276 // Handle ID to label conversion for contact fields
277 $entryFound = $this->alterDisplayContactFields($row, $rows, $rowNum, 'contact/summary', 'View Contact Summary') ? TRUE : $entryFound;
278
279 // display birthday in the configured custom format
280 if (array_key_exists('civicrm_contact_birth_date', $row)) {
281 $birthDate = $row['civicrm_contact_birth_date'];
282 if ($birthDate) {
283 $rows[$rowNum]['civicrm_contact_birth_date'] = CRM_Utils_Date::customFormat($birthDate, '%Y%m%d');
284 }
285 $entryFound = TRUE;
286 }
287
288 // skip looking further in rows, if first row itself doesn't
289 // have the column we need
290 if (!$entryFound) {
291 break;
292 }
293 }
294 }
295
296 }