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