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