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