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