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