Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-03-03-17-36-50
[civicrm-core.git] / CRM / Report / Form / Contact / CurrentEmployer.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.4 |
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 'contact_type' =>
98 array(
99 'title' => ts('Contact Type'),
100 ),
101 'contact_sub_type' =>
102 array(
103 'title' => ts('Contact SubType'),
104 ),
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 (!empty($field['required']) || !empty($this->_params['fields'][$fieldName])) {
210
211 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
212 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
213 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
214 }
215 }
216 }
217 }
218
219 $this->_select = "SELECT " . implode(', ', $select) . " ";
220 }
221
222 function from() {
223 $this->_from = "
224 FROM civicrm_contact {$this->_aliases['civicrm_contact']}
225
226 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_employer']}
227 ON {$this->_aliases['civicrm_employer']}.id={$this->_aliases['civicrm_contact']}.employer_id
228
229 {$this->_aclFrom}
230 LEFT JOIN civicrm_relationship {$this->_aliases['civicrm_relationship']}
231 ON ( {$this->_aliases['civicrm_relationship']}.contact_id_a={$this->_aliases['civicrm_contact']}.id
232 AND {$this->_aliases['civicrm_relationship']}.contact_id_b={$this->_aliases['civicrm_contact']}.employer_id
233 AND {$this->_aliases['civicrm_relationship']}.relationship_type_id=4)
234 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
235 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id
236 AND {$this->_aliases['civicrm_address']}.is_primary = 1 )
237
238 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
239 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id
240 AND {$this->_aliases['civicrm_email']}.is_primary = 1) ";
241 }
242
243 function where() {
244
245 $clauses = array();
246 foreach ($this->_columns as $tableName => $table) {
247 if (array_key_exists('filters', $table)) {
248 foreach ($table['filters'] as $fieldName => $field) {
249 $clause = NULL;
250 if (CRM_Utils_Array::value('operatorType', $field) & CRM_Report_Form::OP_DATE) {
251 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
252 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
253 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
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 {
280 $this->_where = "WHERE ({$this->_aliases['civicrm_contact']}.employer_id!='null') AND " . implode(' AND ', $clauses);
281 }
282
283 if ($this->_aclWhere) {
284 $this->_where .= " AND {$this->_aclWhere} ";
285 }
286 }
287
288 function groupBy() {
289 $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_employer']}.id,{$this->_aliases['civicrm_contact']}.id";
290 }
291
292 function orderBy() {
293 $this->_orderBy = "ORDER BY {$this->_aliases['civicrm_employer']}.organization_name, {$this->_aliases['civicrm_contact']}.display_name";
294 }
295
296 function postProcess() {
297 // get the acl clauses built before we assemble the query
298 $this->buildACLClause(array($this->_aliases['civicrm_contact'], $this->_aliases['civicrm_employer']));
299 parent::postProcess();
300 }
301
302 function alterDisplay(&$rows) {
303 // custom code to alter rows
304 $checkList = array();
305 $entryFound = FALSE;
306
307 foreach ($rows as $rowNum => $row) {
308
309 // convert employer name to links
310 if (array_key_exists('civicrm_employer_organization_name', $row) &&
311 array_key_exists('civicrm_employer_id', $row)
312 ) {
313 $url = CRM_Report_Utils_Report::getNextUrl('contact/detail',
314 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_employer_id'],
315 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
316 );
317 $rows[$rowNum]['civicrm_employer_organization_name_link'] = $url;
318 $entryFound = TRUE;
319 }
320
321 if (!empty($this->_noRepeats) && $this->_outputMode != 'csv') {
322 // not repeat contact display names if it matches with the one
323 // in previous row
324
325 foreach ($row as $colName => $colVal) {
326 if (!empty($checkList[$colName]) && is_array($checkList[$colName]) &&
327 in_array($colVal, $checkList[$colName])
328 ) {
329 $rows[$rowNum][$colName] = "";
330 }
331 if (in_array($colName, $this->_noRepeats)) {
332 $checkList[$colName][] = $colVal;
333 }
334 }
335 }
336
337 //handle gender
338 if (array_key_exists('civicrm_contact_gender_id', $row)) {
339 if ($value = $row['civicrm_contact_gender_id']) {
340 $gender = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id');
341 $rows[$rowNum]['civicrm_contact_gender_id'] = $gender[$value];
342 }
343 $entryFound = TRUE;
344 }
345
346 // convert employee name to links
347 if (array_key_exists('civicrm_contact_display_name', $row) &&
348 array_key_exists('civicrm_contact_id', $row)
349 ) {
350 $url = CRM_Report_Utils_Report::getNextUrl('contact/detail',
351 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
352 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
353 );
354 $rows[$rowNum]['civicrm_contact_display_name_link'] = $url;
355 $entryFound = TRUE;
356 }
357
358 // handle country
359 if (array_key_exists('civicrm_address_country_id', $row)) {
360 if ($value = $row['civicrm_address_country_id']) {
361 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country($value, FALSE);
362 }
363 $entryFound = TRUE;
364 }
365
366 if (array_key_exists('civicrm_address_state_province_id', $row)) {
367 if ($value = $row['civicrm_address_state_province_id']) {
368 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvince($value, FALSE);
369 }
370 $entryFound = TRUE;
371 }
372
373 // skip looking further in rows, if first row itself doesn't
374 // have the column we need
375 if (!$entryFound) {
376 break;
377 }
378 }
379 }
380 }
381