Merge pull request #8887 from seamuslee001/CRM-19229
[civicrm-core.git] / CRM / Report / Form / Contact / CurrentEmployer.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
32 */
33 class CRM_Report_Form_Contact_CurrentEmployer extends CRM_Report_Form {
34
35 /**
36 * This report has not been optimised for group filtering.
37 *
38 * The functionality for group filtering has been improved but not
39 * all reports have been adjusted to take care of it. This report has not
40 * and will run an inefficient query until fixed.
41 *
42 * CRM-19170
43 *
44 * @var bool
45 */
46 protected $groupFilterNotOptimised = TRUE;
47
48 protected $_summary = NULL;
49
50 protected $_customGroupExtends = array(
51 'Contact',
52 'Individual',
53 );
54
55 public $_drilldownReport = array('contact/detail' => 'Link to Detail Report');
56
57 /**
58 * Class constructor.
59 */
60 public function __construct() {
61
62 $this->_columns = array(
63 'civicrm_employer' => array(
64 'dao' => 'CRM_Contact_DAO_Contact',
65 'fields' => array(
66 'organization_name' => array(
67 'title' => ts('Employer Name'),
68 'required' => TRUE,
69 'no_repeat' => TRUE,
70 ),
71 'id' => array(
72 'no_display' => TRUE,
73 'required' => TRUE,
74 ),
75 ),
76 'filters' => array(
77 'organization_name' => array(
78 'title' => ts('Employer Name'),
79 'operatorType' => CRM_Report_Form::OP_STRING,
80 ),
81 ),
82 ),
83 'civicrm_contact' => array(
84 'dao' => 'CRM_Contact_DAO_Contact',
85 'fields' => array(
86 'sort_name' => array(
87 'title' => ts('Employee Name'),
88 'required' => TRUE,
89 ),
90 'first_name' => array(
91 'title' => ts('First Name'),
92 ),
93 'middle_name' => array(
94 'title' => ts('Middle Name'),
95 ),
96 'last_name' => array(
97 'title' => ts('Last Name'),
98 ),
99 'id' => array(
100 'no_display' => TRUE,
101 'required' => TRUE,
102 ),
103 'job_title' => array(
104 'title' => ts('Job Title'),
105 'default' => TRUE,
106 ),
107 'gender_id' => array(
108 'title' => ts('Gender'),
109 ),
110 'birth_date' => array(
111 'title' => ts('Birth Date'),
112 ),
113 'age' => array(
114 'title' => ts('Age'),
115 'dbAlias' => 'TIMESTAMPDIFF(YEAR, contact_civireport.birth_date, CURDATE())',
116 ),
117 'contact_type' => array(
118 'title' => ts('Contact Type'),
119 ),
120 'contact_sub_type' => array(
121 'title' => ts('Contact Subtype'),
122 ),
123 ),
124 'filters' => array_merge($this->getBasicContactFilters(), array('sort_name' => array('title' => ts('Employee Name')))),
125 'grouping' => 'contact-fields',
126 ),
127 'civicrm_relationship' => array(
128 'dao' => 'CRM_Contact_DAO_Relationship',
129 'fields' => array(
130 'start_date' => array(
131 'title' => ts('Employee Since'),
132 'default' => TRUE,
133 ),
134 ),
135 'filters' => array(
136 'start_date' => array(
137 'title' => ts('Employee Since'),
138 'operatorType' => CRM_Report_Form::OP_DATE,
139 'type' => CRM_Utils_Type::T_DATE,
140 ),
141 ),
142 ),
143 'civicrm_phone' => array(
144 'dao' => 'CRM_Core_DAO_Phone',
145 'grouping' => 'contact-fields',
146 'fields' => array(
147 'phone' => array(
148 'title' => ts('Phone'),
149 'default' => TRUE,
150 ),
151 ),
152 ),
153 'civicrm_email' => array(
154 'dao' => 'CRM_Core_DAO_Email',
155 'grouping' => 'contact-fields',
156 'fields' => array(
157 'email' => array(
158 'title' => ts('Email'),
159 'default' => TRUE,
160 ),
161 ),
162 ),
163 'civicrm_address' => array(
164 'dao' => 'CRM_Core_DAO_Address',
165 'grouping' => 'contact-fields',
166 'fields' => array(
167 'street_address' => NULL,
168 'city' => NULL,
169 'postal_code' => NULL,
170 'state_province_id' => array(
171 'title' => ts('State/Province'),
172 ),
173 'country_id' => array(
174 'title' => ts('Country'),
175 ),
176 ),
177 'filters' => array(
178 'country_id' => array(
179 'title' => ts('Country'),
180 'type' => CRM_Utils_Type::T_INT,
181 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
182 'options' => CRM_Core_PseudoConstant::country(NULL, FALSE),
183 ),
184 'state_province_id' => array(
185 'title' => ts('State/Province'),
186 'type' => CRM_Utils_Type::T_INT,
187 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
188 'options' => CRM_Core_PseudoConstant::stateProvince(),
189 ),
190 ),
191 ),
192 'civicrm_group' => array(
193 'dao' => 'CRM_Contact_DAO_Group',
194 'alias' => 'cgroup',
195 'filters' => array(
196 'gid' => array(
197 'name' => 'group_id',
198 'title' => ts('Group'),
199 'type' => CRM_Utils_Type::T_INT,
200 'group' => TRUE,
201 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
202 'options' => CRM_Core_PseudoConstant::staticGroup(),
203 ),
204 ),
205 ),
206 );
207
208 $this->_tagFilter = TRUE;
209 parent::__construct();
210 }
211
212 public function preProcess() {
213 parent::preProcess();
214 }
215
216 public function select() {
217
218 $select = $this->_columnHeaders = array();
219
220 foreach ($this->_columns as $tableName => $table) {
221 if (array_key_exists('fields', $table)) {
222 foreach ($table['fields'] as $fieldName => $field) {
223 if (!empty($field['required']) ||
224 !empty($this->_params['fields'][$fieldName])
225 ) {
226
227 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
228 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
229 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
230 }
231 }
232 }
233 }
234 $this->_selectClauses = $select;
235
236 $this->_select = "SELECT " . implode(', ', $select) . " ";
237 }
238
239 public function from() {
240 $this->_from = "
241 FROM civicrm_contact {$this->_aliases['civicrm_contact']}
242
243 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_employer']}
244 ON {$this->_aliases['civicrm_employer']}.id={$this->_aliases['civicrm_contact']}.employer_id
245
246 {$this->_aclFrom}
247 LEFT JOIN civicrm_relationship {$this->_aliases['civicrm_relationship']}
248 ON ( {$this->_aliases['civicrm_relationship']}.contact_id_a={$this->_aliases['civicrm_contact']}.id
249 AND {$this->_aliases['civicrm_relationship']}.contact_id_b={$this->_aliases['civicrm_contact']}.employer_id
250 AND {$this->_aliases['civicrm_relationship']}.relationship_type_id=4)
251 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
252 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id
253 AND {$this->_aliases['civicrm_address']}.is_primary = 1 )
254
255 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
256 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id
257 AND {$this->_aliases['civicrm_phone']}.is_primary = 1)
258 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
259 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id
260 AND {$this->_aliases['civicrm_email']}.is_primary = 1) ";
261 }
262
263 public function where() {
264
265 $clauses = array();
266 foreach ($this->_columns as $tableName => $table) {
267 if (array_key_exists('filters', $table)) {
268 foreach ($table['filters'] as $fieldName => $field) {
269 $clause = NULL;
270 if (CRM_Utils_Array::value('operatorType', $field) & CRM_Report_Form::OP_DATE
271 ) {
272 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
273 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
274 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
275
276 $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to, $field['type']);
277 }
278 else {
279 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
280 if ($op) {
281 $clause = $this->whereClause($field,
282 $op,
283 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
284 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
285 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
286 );
287 }
288 }
289
290 if (!empty($clause)) {
291 $clauses[$fieldName] = $clause;
292 }
293 }
294 }
295 }
296
297 if (empty($clauses)) {
298 $this->_where = "WHERE {$this->_aliases['civicrm_contact']}.employer_id!='null' ";
299 }
300 else {
301 $this->_where = "WHERE ({$this->_aliases['civicrm_contact']}.employer_id!='null') AND " . implode(' AND ', $clauses);
302 }
303
304 if ($this->_aclWhere) {
305 $this->_where .= " AND {$this->_aclWhere} ";
306 }
307 }
308
309 public function groupBy() {
310 $groupBy = array(
311 "{$this->_aliases['civicrm_employer']}.id",
312 "{$this->_aliases['civicrm_contact']}.id",
313 );
314
315 $this->_groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, $groupBy);
316 }
317
318 public function orderBy() {
319 $this->_orderBy = "ORDER BY {$this->_aliases['civicrm_employer']}.organization_name, {$this->_aliases['civicrm_contact']}.display_name";
320 }
321
322 public function postProcess() {
323 // get the acl clauses built before we assemble the query
324 $this->buildACLClause(array(
325 $this->_aliases['civicrm_contact'],
326 $this->_aliases['civicrm_employer'],
327 ));
328 parent::postProcess();
329 }
330
331 /**
332 * Alter display of rows.
333 *
334 * Iterate through the rows retrieved via SQL and make changes for display purposes,
335 * such as rendering contacts as links.
336 *
337 * @param array $rows
338 * Rows generated by SQL, with an array for each row.
339 */
340 public function alterDisplay(&$rows) {
341 $checkList = array();
342 $entryFound = FALSE;
343
344 foreach ($rows as $rowNum => $row) {
345
346 // convert employer name to links
347 if (array_key_exists('civicrm_employer_organization_name', $row) &&
348 array_key_exists('civicrm_employer_id', $row)
349 ) {
350 $url = CRM_Report_Utils_Report::getNextUrl('contact/detail',
351 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_employer_id'],
352 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
353 );
354 $rows[$rowNum]['civicrm_employer_organization_name_link'] = $url;
355 $entryFound = TRUE;
356 }
357
358 if (!empty($this->_noRepeats) && $this->_outputMode != 'csv') {
359 // not repeat contact display names if it matches with the one
360 // in previous row
361
362 foreach ($row as $colName => $colVal) {
363 if (!empty($checkList[$colName]) && is_array($checkList[$colName]) &&
364 in_array($colVal, $checkList[$colName])
365 ) {
366 $rows[$rowNum][$colName] = "";
367 }
368 if (in_array($colName, $this->_noRepeats)) {
369 $checkList[$colName][] = $colVal;
370 }
371 }
372 }
373
374 //handle gender
375 if (array_key_exists('civicrm_contact_gender_id', $row)) {
376 if ($value = $row['civicrm_contact_gender_id']) {
377 $gender = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id');
378 $rows[$rowNum]['civicrm_contact_gender_id'] = $gender[$value];
379 }
380 $entryFound = TRUE;
381 }
382
383 // display birthday in the configured custom format
384 if (array_key_exists('civicrm_contact_birth_date', $row)) {
385 $birthDate = $row['civicrm_contact_birth_date'];
386 if ($birthDate) {
387 $rows[$rowNum]['civicrm_contact_birth_date'] = CRM_Utils_Date::customFormat($birthDate, '%Y%m%d');
388 }
389 $entryFound = TRUE;
390 }
391
392 // convert employee name to links
393 if (array_key_exists('civicrm_contact_display_name', $row) &&
394 array_key_exists('civicrm_contact_id', $row)
395 ) {
396 $url = CRM_Report_Utils_Report::getNextUrl('contact/detail',
397 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
398 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
399 );
400 $rows[$rowNum]['civicrm_contact_display_name_link'] = $url;
401 $entryFound = TRUE;
402 }
403
404 // handle country
405 if (array_key_exists('civicrm_address_country_id', $row)) {
406 if ($value = $row['civicrm_address_country_id']) {
407 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country($value, FALSE);
408 }
409 $entryFound = TRUE;
410 }
411
412 if (array_key_exists('civicrm_address_state_province_id', $row)) {
413 if ($value = $row['civicrm_address_state_province_id']) {
414 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvince($value, FALSE);
415 }
416 $entryFound = TRUE;
417 }
418
419 // skip looking further in rows, if first row itself doesn't
420 // have the column we need
421 if (!$entryFound) {
422 break;
423 }
424 }
425 }
426
427 }