Merge pull request #2775 from ErichBSchulz/patch-3
[civicrm-core.git] / CRM / Report / Form / Contact / Relationship.php
CommitLineData
6a488035 1<?php
6a488035
TO
2
3/*
4 +--------------------------------------------------------------------+
06b69b18 5 | CiviCRM version 4.5 |
6a488035 6 +--------------------------------------------------------------------+
06b69b18 7 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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
06b69b18 32 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
33 * $Id$
34 *
35 */
36class CRM_Report_Form_Contact_Relationship extends CRM_Report_Form {
37
38 protected $_summary = NULL;
39 protected $_emailField_a = FALSE;
40 protected $_emailField_b = FALSE;
41 protected $_customGroupExtends = array(
2f4c2f5d 42 'Relationship');
6a488035 43 public $_drilldownReport = array('contact/detail' => 'Link to Detail Report');
2f4c2f5d 44
6a488035
TO
45 function __construct() {
46
47 $contact_type = CRM_Contact_BAO_ContactType::getSelectElements(FALSE, TRUE, '_');
48
49 $this->_columns = array(
50 'civicrm_contact' =>
51 array(
52 'dao' => 'CRM_Contact_DAO_Contact',
53 'fields' =>
54 array(
55 'sort_name_a' =>
56 array('title' => ts('Contact A'),
57 'name' => 'sort_name',
58 'required' => TRUE,
59 ),
60 'id' =>
61 array(
62 'no_display' => TRUE,
63 'required' => TRUE,
64 ),
30f85891
RN
65 'contact_type_a' =>
66 array(
67 'title' => ts('Contact Type (Contact A)'),
68 'name' => 'contact_type',
69 ),
70 'contact_sub_type_a' =>
71 array(
72 'title' => ts('Contact SubType (Contact A)'),
73 'name' => 'contact_sub_type',
74 ),
6a488035
TO
75 ),
76 'filters' =>
77 array(
78 'sort_name_a' =>
79 array('title' => ts('Contact A'),
80 'name' => 'sort_name',
81 'operator' => 'like',
82 'type' => CRM_Report_Form::OP_STRING,
83 ),
84 ),
85 'grouping' => 'conact_a_fields',
86 ),
87 'civicrm_contact_b' =>
88 array(
89 'dao' => 'CRM_Contact_DAO_Contact',
90 'alias' => 'contact_b',
91 'fields' =>
92 array(
93 'sort_name_b' =>
94 array('title' => ts('Contact B'),
95 'name' => 'sort_name',
96 'required' => TRUE,
97 ),
98 'id' =>
99 array(
100 'no_display' => TRUE,
101 'required' => TRUE,
102 ),
30f85891
RN
103 'contact_type_b' =>
104 array(
105 'title' => ts('Contact Type (Contact B)'),
106 'name' => 'contact_type',
107 ),
108 'contact_sub_type_b' =>
109 array(
110 'title' => ts('Contact SubType (Contact B)'),
111 'name' => 'contact_sub_type',
112 ),
6a488035
TO
113 ),
114 'filters' =>
115 array(
116 'sort_name_b' =>
117 array('title' => ts('Contact B'),
118 'name' => 'sort_name',
119 'operator' => 'like',
120 'type' => CRM_Report_Form::OP_STRING,
121 ),
122 ),
123 'grouping' => 'conact_b_fields',
124 ),
125 'civicrm_email' =>
126 array(
127 'dao' => 'CRM_Core_DAO_Email',
128 'fields' =>
129 array(
130 'email_a' =>
131 array('title' => ts('Email of Contact A'),
132 'name' => 'email',
133 ),
134 ),
135 'grouping' => 'conact_a_fields',
136 ),
137 'civicrm_email_b' =>
138 array(
139 'dao' => 'CRM_Core_DAO_Email',
140 'alias' => 'email_b',
141 'fields' =>
142 array(
143 'email_b' =>
144 array('title' => ts('Email of Contact B'),
145 'name' => 'email',
146 ),
147 ),
148 'grouping' => 'conact_b_fields',
149 ),
150 'civicrm_relationship_type' =>
151 array(
152 'dao' => 'CRM_Contact_DAO_RelationshipType',
153 'fields' =>
154 array(
155 'label_a_b' =>
156 array('title' => ts('Relationship A-B '),
157 'default' => TRUE,
158 ),
159 'label_b_a' =>
160 array('title' => ts('Relationship B-A '),
161 'default' => TRUE,
162 ),
163 ),
164 'filters' =>
165 array(
166 'contact_type_a' =>
167 array('title' => ts('Contact Type A'),
168 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
169 'options' => $contact_type,
170 'type' => CRM_Utils_Type::T_STRING,
171 ),
172 'contact_type_b' =>
173 array('title' => ts('Contact Type B'),
174 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
175 'options' => $contact_type,
176 'type' => CRM_Utils_Type::T_STRING,
177 ),
178 ),
179 'grouping' => 'relation-fields',
180 ),
181 'civicrm_relationship' =>
182 array(
183 'dao' => 'CRM_Contact_DAO_Relationship',
184 'fields' =>
185 array(
186 'start_date' =>
187 array('title' => ts('Relationship Start Date'),
188 ),
189 'end_date' =>
190 array('title' => ts('Relationship End Date'),
191 ),
192 'description' =>
193 array('title' => ts('Description'),
194 ),
195 ),
196 'filters' =>
197 array(
198 'is_active' =>
199 array('title' => ts('Relationship Status'),
200 'operatorType' => CRM_Report_Form::OP_SELECT,
201 'options' =>
202 array(
203 '' => '- Any -',
204 1 => 'Active',
205 0 => 'Inactive',
206 ),
207 'type' => CRM_Utils_Type::T_INT,
208 ),
209 'relationship_type_id' =>
210 array('title' => ts('Relationship'),
211 'operatorType' => CRM_Report_Form::OP_SELECT,
212 'options' =>
213 array(
214 '' => '- any relationship type -') +
215 CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, 'null', NULL, NULL, TRUE),
216 'type' => CRM_Utils_Type::T_INT,
217 ),
218 ),
219 'grouping' => 'relation-fields',
220 ),
221 'civicrm_address' =>
222 array(
223 'dao' => 'CRM_Core_DAO_Address',
224 'filters' =>
225 array(
226 'country_id' =>
227 array('title' => ts('Country'),
228 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
229 'options' => CRM_Core_PseudoConstant::country(),
230 ),
231 'state_province_id' =>
232 array('title' => ts('State/Province'),
233 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
234 'options' => CRM_Core_PseudoConstant::stateProvince(),
235 ),
236 ),
237 'grouping' => 'contact-fields',
238 ),
239 'civicrm_group' =>
240 array(
241 'dao' => 'CRM_Contact_DAO_Group',
242 'alias' => 'cgroup',
243 'filters' =>
244 array(
245 'gid' =>
246 array(
247 'name' => 'group_id',
248 'title' => ts('Group'),
249 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
250 'group' => TRUE,
251 'options' => CRM_Core_PseudoConstant::group(),
252 ),
253 ),
254 ),
255 );
256
257 $this->_tagFilter = TRUE;
258 parent::__construct();
259 }
260
261 function preProcess() {
262 parent::preProcess();
263 }
264
265 function select() {
266 $select = $this->_columnHeaders = array();
267 foreach ($this->_columns as $tableName => $table) {
268 if (array_key_exists('fields', $table)) {
269 foreach ($table['fields'] as $fieldName => $field) {
8cc574cf 270 if (!empty($field['required']) || !empty($this->_params['fields'][$fieldName])) {
6a488035
TO
271
272 if ($fieldName == 'email_a') {
273 $this->_emailField_a = TRUE;
274 }
275 if ($fieldName == 'email_b') {
276 $this->_emailField_b = TRUE;
277 }
278 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
279 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
280 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
281 }
282 }
283 }
284 }
285
286 $this->_select = "SELECT " . implode(', ', $select) . " ";
287 }
288
289 function from() {
290 $this->_from = "
291 FROM civicrm_relationship {$this->_aliases['civicrm_relationship']}
2f4c2f5d 292
6a488035 293 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
2f4c2f5d 294 ON ( {$this->_aliases['civicrm_relationship']}.contact_id_a =
6a488035
TO
295 {$this->_aliases['civicrm_contact']}.id )
296
297 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact_b']}
2f4c2f5d 298 ON ( {$this->_aliases['civicrm_relationship']}.contact_id_b =
6a488035
TO
299 {$this->_aliases['civicrm_contact_b']}.id )
300
301 {$this->_aclFrom} ";
302
303 if (!empty($this->_params['country_id_value']) ||
304 !empty($this->_params['state_province_id_value'])
305 ) {
306 $this->_from .= "
2f4c2f5d 307 INNER JOIN civicrm_address {$this->_aliases['civicrm_address']}
6a488035
TO
308 ON (( {$this->_aliases['civicrm_address']}.contact_id =
309 {$this->_aliases['civicrm_contact']}.id OR
310 {$this->_aliases['civicrm_address']}.contact_id =
2f4c2f5d 311 {$this->_aliases['civicrm_contact_b']}.id ) AND
6a488035
TO
312 {$this->_aliases['civicrm_address']}.is_primary = 1 ) ";
313 }
314
315 $this->_from .= "
316 INNER JOIN civicrm_relationship_type {$this->_aliases['civicrm_relationship_type']}
2f4c2f5d 317 ON ( {$this->_aliases['civicrm_relationship']}.relationship_type_id =
6a488035
TO
318 {$this->_aliases['civicrm_relationship_type']}.id ) ";
319
320 // include Email Field
321 if ($this->_emailField_a) {
2f4c2f5d 322 $this->_from .= "
6a488035
TO
323 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
324 ON ( {$this->_aliases['civicrm_contact']}.id =
2f4c2f5d 325 {$this->_aliases['civicrm_email']}.contact_id AND
6a488035
TO
326 {$this->_aliases['civicrm_email']}.is_primary = 1 )";
327 }
328 if ($this->_emailField_b) {
2f4c2f5d 329 $this->_from .= "
330 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email_b']}
6a488035 331 ON ( {$this->_aliases['civicrm_contact_b']}.id =
2f4c2f5d 332 {$this->_aliases['civicrm_email_b']}.contact_id AND
6a488035
TO
333 {$this->_aliases['civicrm_email_b']}.is_primary = 1 )";
334 }
335 }
336
337 function where() {
338 $whereClauses = $havingClauses = array();
339 foreach ($this->_columns as $tableName => $table) {
340 if (array_key_exists('filters', $table)) {
341 foreach ($table['filters'] as $fieldName => $field) {
342
343 $clause = NULL;
344 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
345 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
346 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
347 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
348
349 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
350 }
351 else {
352 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
353 if ($op) {
354
355 if ($tableName == 'civicrm_relationship_type' &&
356 ($fieldName == 'contact_type_a' || $fieldName == 'contact_type_b')
357 ) {
358 $cTypes = CRM_Utils_Array::value("{$fieldName}_value", $this->_params);
359 $contactTypes = $contactSubTypes = array();
360 if (!empty($cTypes)) {
361 foreach ($cTypes as $ctype) {
362 $getTypes = CRM_Utils_System::explode('_', $ctype, 2);
363 if ($getTypes[1] && !in_array($getTypes[1], $contactSubTypes)) {
364 $contactSubTypes[] = $getTypes[1];
365 }
366 elseif ($getTypes[0] && !in_array($getTypes[0], $contactTypes)) {
367 $contactTypes[] = $getTypes[0];
368 }
369 }
370 }
371
372 if (!empty($contactTypes)) {
373 $clause = $this->whereClause($field,
374 $op,
375 $contactTypes,
376 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
377 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
378 );
379 }
380
381 if (!empty($contactSubTypes)) {
382 if ($fieldName == 'contact_type_a') {
383 $field['name'] = 'contact_sub_type_a';
384 }
385 else {
386 $field['name'] = 'contact_sub_type_b';
387 }
388 $field['dbAlias'] = $field['alias'] . '.' . $field['name'];
389 $subTypeClause = $this->whereClause($field,
390 $op,
391 $contactSubTypes,
392 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
393 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
394 );
395 if ($clause) {
396 $clause = '(' . $clause . ' OR ' . $subTypeClause . ')';
397 }
398 else {
399 $clause = $subTypeClause;
400 }
401 }
402 }
403 else {
404
405 $clause = $this->whereClause($field,
406 $op,
407 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
408 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
409 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
410 );
411 }
412 }
413 }
414
415 if (!empty($clause)) {
a7488080 416 if (!empty($field['having'])) {
6a488035
TO
417 $havingClauses[] = $clause;
418 }
419 else {
420 $whereClauses[] = $clause;
421 }
422 }
423 }
424 }
425 }
426
427 if (empty($whereClauses)) {
428 $this->_where = 'WHERE ( 1 ) ';
429 $this->_having = '';
430 }
431 else {
432 $this->_where = 'WHERE ' . implode(' AND ', $whereClauses);
433 }
434
435 if ($this->_aclWhere) {
436 $this->_where .= " AND {$this->_aclWhere} ";
437 }
438
439 if (!empty($havingClauses)) {
440 // use this clause to construct group by clause.
441 $this->_having = 'HAVING ' . implode(' AND ', $havingClauses);
442 }
443 }
444
445 function statistics(&$rows) {
446 $statistics = parent::statistics($rows);
447
448 $isStatusFilter = FALSE;
449 $relStatus = NULL;
450 if (CRM_Utils_Array::value('is_active_value', $this->_params) == '1') {
451 $relStatus = 'Is equal to Active';
452 }
453 elseif (CRM_Utils_Array::value('is_active_value', $this->_params) == '0') {
454 $relStatus = 'Is equal to Inactive';
455 }
a7488080 456 if (!empty($statistics['filters'])) {
6a488035
TO
457 foreach ($statistics['filters'] as $id => $value) {
458 //for displaying relationship type filter
459 if ($value['title'] == 'Relationship') {
460 $relTypes = CRM_Core_PseudoConstant::relationshipType();
461 $statistics['filters'][$id]['value'] = 'Is equal to ' . $relTypes[$this->_params['relationship_type_id_value']]['label_' . $this->relationType];
462 }
463
464 //for displaying relationship status
465 if ($value['title'] == 'Relationship Status') {
466 $isStatusFilter = TRUE;
467 $statistics['filters'][$id]['value'] = $relStatus;
468 }
469 }
470 }
471 //for displaying relationship status
472 if (!$isStatusFilter && $relStatus) {
473 $statistics['filters'][] = array(
474 'title' => 'Relationship Status',
475 'value' => $relStatus,
476 );
477 }
478 return $statistics;
479 }
480
481 function groupBy() {
482 $this->_groupBy = " ";
483 $groupBy = array();
484 if ($this->relationType == 'a_b') {
485 $groupBy[] = " {$this->_aliases['civicrm_contact']}.id";
486 }
487 elseif ($this->relationType == 'b_a') {
488 $groupBy[] = " {$this->_aliases['civicrm_contact_b']}.id";
489 }
490
491 if (!empty($groupBy)) {
492 $this->_groupBy = " GROUP BY " . implode(', ', $groupBy) . " , {$this->_aliases['civicrm_relationship']}.id ";
493 }
494 else {
495 $this->_groupBy = " GROUP BY {$this->_aliases['civicrm_relationship']}.id ";
496 }
497 }
498
499 function orderBy() {
500 $this->_orderBy = " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact_b']}.sort_name ";
501 }
502
503 function postProcess() {
504 $this->beginPostProcess();
505
506 $this->relationType = NULL;
507 $relType = array();
a7488080 508 if (!empty($this->_params['relationship_type_id_value'])) {
6a488035
TO
509 $relType = explode('_', $this->_params['relationship_type_id_value']);
510
511 $this->relationType = $relType[1] . '_' . $relType[2];
512 $this->_params['relationship_type_id_value'] = intval($relType[0]);
513 }
514
515 $this->buildACLClause(array($this->_aliases['civicrm_contact'], $this->_aliases['civicrm_contact_b']));
516 $sql = $this->buildQuery();
517 $this->buildRows($sql, $rows);
518
519 $this->formatDisplay($rows);
520 $this->doTemplateAssignment($rows);
521
522 if (!empty($relType)) {
523 // store its old value, CRM-5837
524 $this->_params['relationship_type_id_value'] = implode('_', $relType);
525 }
526 $this->endPostProcess($rows);
527 }
528
529 function alterDisplay(&$rows) {
530 // custom code to alter rows
531 $entryFound = FALSE;
532
533 foreach ($rows as $rowNum => $row) {
534
535 // handle country
536 if (array_key_exists('civicrm_address_country_id', $row)) {
537 if ($value = $row['civicrm_address_country_id']) {
538 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country($value, FALSE);
539 }
540 $entryFound = TRUE;
541 }
542
543 if (array_key_exists('civicrm_address_state_province_id', $row)) {
544 if ($value = $row['civicrm_address_state_province_id']) {
545 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvince($value, FALSE);
546 }
547 $entryFound = TRUE;
548 }
549
550 if (array_key_exists('civicrm_contact_sort_name_a', $row) &&
551 array_key_exists('civicrm_contact_id', $row)
552 ) {
553 $url = CRM_Report_Utils_Report::getNextUrl('contact/detail',
554 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
555 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
556 );
557 $rows[$rowNum]['civicrm_contact_sort_name_a_link'] = $url;
558 $rows[$rowNum]['civicrm_contact_sort_name_a_hover'] = ts("View Contact details for this contact.");
559 $entryFound = TRUE;
560 }
561
562 if (array_key_exists('civicrm_contact_b_sort_name_b', $row) &&
563 array_key_exists('civicrm_contact_b_id', $row)
564 ) {
565 $url = CRM_Report_Utils_Report::getNextUrl('contact/detail',
566 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_b_id'],
567 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
568 );
569 $rows[$rowNum]['civicrm_contact_b_sort_name_b_link'] = $url;
570 $rows[$rowNum]['civicrm_contact_b_sort_name_b_hover'] = ts("View Contact details for this contact.");
571 $entryFound = TRUE;
572 }
573
574 // skip looking further in rows, if first row itself doesn't
575 // have the column we need
576 if (!$entryFound) {
577 break;
578 }
579 }
580 }
581}
582