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