Merge pull request #9363 from colemanw/CRM-19560
[civicrm-core.git] / CRM / Report / Form / Walklist / Walklist.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
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
fa938177 31 * @copyright CiviCRM LLC (c) 2004-2016
6a488035
TO
32 */
33class CRM_Report_Form_Walklist_Walklist extends CRM_Report_Form {
34 protected $_addressField = FALSE;
35
36 protected $_emailField = FALSE;
37
38 protected $_phoneField = FALSE;
39
40 protected $_summary = NULL;
41
42 public $_drilldownReport = array('contact/detail' => 'Link to Detail Report');
43
44 protected $_customGroupExtends = array(
9d72cede
EM
45 'Contact',
46 'Individual',
47 'Household',
21dfd5f5 48 'Organization',
430ae6dd
TO
49 );
50
74cf4551 51 /**
73b448bf 52 * Class constructor.
74cf4551 53 */
00be9182 54 public function __construct() {
6a488035 55 $this->_columns = array(
9d72cede 56 'civicrm_contact' => array(
6a488035 57 'dao' => 'CRM_Contact_DAO_Contact',
9d72cede
EM
58 'fields' => array(
59 'id' => array(
60 'title' => ts('Contact ID'),
6a488035
TO
61 'no_display' => TRUE,
62 'required' => TRUE,
63 ),
9d72cede
EM
64 'sort_name' => array(
65 'title' => ts('Contact Name'),
6a488035
TO
66 'required' => TRUE,
67 'no_repeat' => TRUE,
68 ),
69 ),
9d72cede
EM
70 'filters' => array(
71 'sort_name' => array(
72 'title' => ts('Contact Name'),
6a488035
TO
73 'operator' => 'like',
74 ),
75 ),
76 'grouping' => 'contact-fields',
9d72cede
EM
77 'order_bys' => array(
78 'sort_name' => array(
79 'title' => ts('Contact Name'),
6a488035 80 'required' => TRUE,
21dfd5f5 81 ),
9d72cede 82 ),
6a488035 83 ),
9d72cede 84 'civicrm_address' => array(
6a488035 85 'dao' => 'CRM_Core_DAO_Address',
9d72cede
EM
86 'fields' => array(
87 'street_number' => array(
88 'title' => ts('Street Number'),
6a488035
TO
89 'type' => 1,
90 ),
91 'street_address' => NULL,
92 'city' => NULL,
93 'postal_code' => NULL,
9d72cede
EM
94 'state_province_id' => array(
95 'title' => ts('State/Province'),
6a488035 96 'default' => TRUE,
55f71fa7 97 'type' => CRM_Utils_Type::T_INT,
6a488035 98 ),
9d72cede
EM
99 'country_id' => array(
100 'title' => ts('Country'),
6a488035
TO
101 ),
102 ),
9d72cede
EM
103 'filters' => array(
104 'street_number' => array(
105 'title' => ts('Street Number'),
6a488035
TO
106 'type' => 1,
107 'name' => 'street_number',
108 ),
109 'street_address' => NULL,
110 'city' => NULL,
111 ),
112 'grouping' => 'location-fields',
113 ),
9d72cede 114 'civicrm_email' => array(
6a488035 115 'dao' => 'CRM_Core_DAO_Email',
9d72cede 116 'fields' => array('email' => array('default' => TRUE)),
6a488035
TO
117 'grouping' => 'location-fields',
118 ),
9d72cede 119 'civicrm_phone' => array(
6a488035 120 'dao' => 'CRM_Core_DAO_Phone',
9d72cede 121 'fields' => array('phone' => NULL),
6a488035
TO
122 'grouping' => 'location-fields',
123 ),
124 );
125 parent::__construct();
126 }
127
00be9182 128 public function preProcess() {
6a488035
TO
129 parent::preProcess();
130 }
131
00be9182 132 public function select() {
6a488035
TO
133 $select = array();
134
135 $this->_columnHeaders = array();
136 foreach ($this->_columns as $tableName => $table) {
137 foreach ($table['fields'] as $fieldName => $field) {
9d72cede
EM
138 if (!empty($field['required']) ||
139 !empty($this->_params['fields'][$fieldName])
140 ) {
6a488035
TO
141 if ($tableName == 'civicrm_address') {
142 $this->_addressField = TRUE;
143 }
144 elseif ($tableName == 'civicrm_email') {
145 $this->_emailField = TRUE;
146 }
147 elseif ($tableName == 'civicrm_phone') {
148 $this->_phoneField = TRUE;
149 }
150
151 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
152 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
153 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = $field['type'];
154 }
155 }
156 }
157
158 $this->_select = "SELECT " . implode(",\n", $select) . " ";
159 }
160
00be9182 161 public function from() {
6a488035
TO
162 $this->_from = NULL;
163
164 $this->_from = "
165FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
166";
167 if ($this->_addressField) {
168 $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";
169 }
170
171 if ($this->_emailField) {
172 $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";
173 }
174
175 if ($this->_phoneField) {
176 $this->_from .= "LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']} ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND {$this->_aliases['civicrm_phone']}.is_primary = 1\n";
177 }
178 }
179
00be9182 180 public function where() {
6a488035
TO
181 $clauses = array();
182 foreach ($this->_columns as $tableName => $table) {
183 if (array_key_exists('filters', $table)) {
184 foreach ($table['filters'] as $fieldName => $field) {
185 $clause = NULL;
186
187 if ($field['type'] & CRM_Utils_Type::T_DATE) {
188 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
9d72cede
EM
189 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
190 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
6a488035
TO
191
192 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
193 }
194 else {
195 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
196 if ($op) {
197 $clause = $this->whereClause($field,
198 $op,
199 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
200 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
201 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
202 );
203 }
204 }
205
206 if (!empty($clause)) {
207 $clauses[] = $clause;
208 }
209 }
210 }
211 }
212
213 if (empty($clauses)) {
214 $this->_where = "WHERE ( 1 ) ";
215 }
216 else {
217 $this->_where = "WHERE " . implode(' AND ', $clauses);
218 }
219
220 if ($this->_aclWhere) {
221 $this->_where .= " AND {$this->_aclWhere} ";
222 }
223 }
224
00be9182 225 public function orderBy() {
6a488035
TO
226 $this->_orderBy = "";
227 foreach ($this->_columns as $tableName => $table) {
228 if (array_key_exists('order_bys', $table)) {
229 foreach ($table['order_bys'] as $fieldName => $field) {
230 $this->_orderBy[] = $field['dbAlias'];
231 }
232 }
233 }
234 $this->_orderBy = "ORDER BY " . implode(', ', $this->_orderBy) . " ";
235 }
236
00be9182 237 public function postProcess() {
6a488035
TO
238 // get the acl clauses built before we assemble the query
239 $this->buildACLClause($this->_aliases['civicrm_contact']);
240 parent::postProcess();
241 }
242
74cf4551 243 /**
ced9bfed
EM
244 * Alter display of rows.
245 *
246 * Iterate through the rows retrieved via SQL and make changes for display purposes,
247 * such as rendering contacts as links.
248 *
249 * @param array $rows
250 * Rows generated by SQL, with an array for each row.
74cf4551 251 */
00be9182 252 public function alterDisplay(&$rows) {
6a488035
TO
253 $entryFound = FALSE;
254 foreach ($rows as $rowNum => $row) {
255 // handle state province
256 if (array_key_exists('civicrm_address_state_province_id', $row)) {
257 if ($value = $row['civicrm_address_state_province_id']) {
258 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvince($value);
259 }
260 $entryFound = TRUE;
261 }
262
263 // handle country
264 if (array_key_exists('civicrm_address_country_id', $row)) {
265 if ($value = $row['civicrm_address_country_id']) {
266 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country($value);
267 }
268 $entryFound = TRUE;
269 }
270
271 // convert display name to links
272 if (array_key_exists('civicrm_contact_sort_name', $row) &&
273 array_key_exists('civicrm_contact_id', $row)
274 ) {
275 $url = CRM_Report_Utils_Report::getNextUrl('contact/detail',
276 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
277 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
278 );
279 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
280 $entryFound = TRUE;
281 }
282
283 // skip looking further in rows, if first row itself doesn't
284 // have the column we need
285 if (!$entryFound) {
286 break;
287 }
288 }
289 }
96025800 290
6a488035 291}