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