Merge pull request #3199 from davecivicrm/CRM-14656
[civicrm-core.git] / CRM / Report / Form / Contact / Relationship.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_Relationship extends CRM_Report_Form {
37
38 protected $_summary = NULL;
39 protected $_emailField_a = FALSE;
40 protected $_emailField_b = FALSE;
41 protected $_phoneField_a = FALSE;
42 protected $_phoneField_b = FALSE;
43 protected $_customGroupExtends = array(
44 'Relationship');
45 public $_drilldownReport = array('contact/detail' => 'Link to Detail Report');
46
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 ),
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 ),
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 ),
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 ),
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 (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 (Contact B)'),
147 'name' => 'email',
148 ),
149 ),
150 'grouping' => 'conact_b_fields',
151 ),
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 (Contact A)'),
161 'name' => 'phone',
162 ),
163 'phone_ext_a' =>
164 array(
165 'title' => ts('Phone Ext (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 (Contact B)'),
180 'name' => 'phone'
181 ),
182 'phone_ext_b' =>
183 array(
184 'title' => ts('Phone Ext (Contact B)'),
185 'name' => 'phone_ext'
186 ),
187 ),
188 'grouping' => 'conact_b_fields',
189 ),
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 'relationship_id' =>
236 array(
237 'title' => ts('Rel ID'),
238 'name' => 'id',
239 ),
240 ),
241 'filters' =>
242 array(
243 'is_active' =>
244 array('title' => ts('Relationship Status'),
245 'operatorType' => CRM_Report_Form::OP_SELECT,
246 'options' =>
247 array(
248 '' => '- Any -',
249 1 => 'Active',
250 0 => 'Inactive',
251 ),
252 'type' => CRM_Utils_Type::T_INT,
253 ),
254 'relationship_type_id' =>
255 array('title' => ts('Relationship'),
256 'operatorType' => CRM_Report_Form::OP_SELECT,
257 'options' =>
258 array(
259 '' => '- any relationship type -') +
260 CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, 'null', NULL, NULL, TRUE),
261 'type' => CRM_Utils_Type::T_INT,
262 ),
263 ),
264 'grouping' => 'relation-fields',
265 ),
266 'civicrm_address' =>
267 array(
268 'dao' => 'CRM_Core_DAO_Address',
269 'filters' =>
270 array(
271 'country_id' =>
272 array('title' => ts('Country'),
273 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
274 'options' => CRM_Core_PseudoConstant::country(),
275 ),
276 'state_province_id' =>
277 array('title' => ts('State/Province'),
278 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
279 'options' => CRM_Core_PseudoConstant::stateProvince(),
280 ),
281 ),
282 'grouping' => 'contact-fields',
283 ),
284 'civicrm_group' =>
285 array(
286 'dao' => 'CRM_Contact_DAO_Group',
287 'alias' => 'cgroup',
288 'filters' =>
289 array(
290 'gid' =>
291 array(
292 'name' => 'group_id',
293 'title' => ts('Group'),
294 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
295 'group' => TRUE,
296 'options' => CRM_Core_PseudoConstant::group(),
297 ),
298 ),
299 ),
300 );
301
302 $this->_tagFilter = TRUE;
303 parent::__construct();
304 }
305
306 function preProcess() {
307 parent::preProcess();
308 }
309
310 function select() {
311 $select = $this->_columnHeaders = array();
312 foreach ($this->_columns as $tableName => $table) {
313 if (array_key_exists('fields', $table)) {
314 foreach ($table['fields'] as $fieldName => $field) {
315 if (!empty($field['required']) || !empty($this->_params['fields'][$fieldName])) {
316
317 if ($fieldName == 'email_a') {
318 $this->_emailField_a = TRUE;
319 }
320 if ($fieldName == 'email_b') {
321 $this->_emailField_b = TRUE;
322 }
323 if ($fieldName == 'phone_a'){
324 $this->_phoneField_a = TRUE;
325 }
326 if ($fieldName == 'phone_b'){
327 $this->_phoneField_b = TRUE;
328 }
329 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
330 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
331 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
332 }
333 }
334 }
335 }
336
337 $this->_select = "SELECT " . implode(', ', $select) . " ";
338 }
339
340 function from() {
341 $this->_from = "
342 FROM civicrm_relationship {$this->_aliases['civicrm_relationship']}
343
344 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
345 ON ( {$this->_aliases['civicrm_relationship']}.contact_id_a =
346 {$this->_aliases['civicrm_contact']}.id )
347
348 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact_b']}
349 ON ( {$this->_aliases['civicrm_relationship']}.contact_id_b =
350 {$this->_aliases['civicrm_contact_b']}.id )
351
352 {$this->_aclFrom} ";
353
354 if (!empty($this->_params['country_id_value']) ||
355 !empty($this->_params['state_province_id_value'])
356 ) {
357 $this->_from .= "
358 INNER JOIN civicrm_address {$this->_aliases['civicrm_address']}
359 ON (( {$this->_aliases['civicrm_address']}.contact_id =
360 {$this->_aliases['civicrm_contact']}.id OR
361 {$this->_aliases['civicrm_address']}.contact_id =
362 {$this->_aliases['civicrm_contact_b']}.id ) AND
363 {$this->_aliases['civicrm_address']}.is_primary = 1 ) ";
364 }
365
366 $this->_from .= "
367 INNER JOIN civicrm_relationship_type {$this->_aliases['civicrm_relationship_type']}
368 ON ( {$this->_aliases['civicrm_relationship']}.relationship_type_id =
369 {$this->_aliases['civicrm_relationship_type']}.id ) ";
370
371 // include Email Field
372 if ($this->_emailField_a) {
373 $this->_from .= "
374 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
375 ON ( {$this->_aliases['civicrm_contact']}.id =
376 {$this->_aliases['civicrm_email']}.contact_id AND
377 {$this->_aliases['civicrm_email']}.is_primary = 1 )";
378 }
379 if ($this->_emailField_b) {
380 $this->_from .= "
381 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email_b']}
382 ON ( {$this->_aliases['civicrm_contact_b']}.id =
383 {$this->_aliases['civicrm_email_b']}.contact_id AND
384 {$this->_aliases['civicrm_email_b']}.is_primary = 1 )";
385 }
386 // include Phone Field
387 if ($this->_phoneField_a) {
388 $this->_from .= "
389 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
390 ON ( {$this->_aliases['civicrm_contact']}.id =
391 {$this->_aliases['civicrm_phone']}.contact_id AND
392 {$this->_aliases['civicrm_phone']}.is_primary = 1 )";
393 }
394 if ($this->_phoneField_b) {
395 $this->_from .= "
396 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone_b']}
397 ON ( {$this->_aliases['civicrm_contact_b']}.id =
398 {$this->_aliases['civicrm_phone_b']}.contact_id AND
399 {$this->_aliases['civicrm_phone_b']}.is_primary = 1 )";
400 }
401 }
402
403 function where() {
404 $whereClauses = $havingClauses = array();
405 foreach ($this->_columns as $tableName => $table) {
406 if (array_key_exists('filters', $table)) {
407 foreach ($table['filters'] as $fieldName => $field) {
408
409 $clause = NULL;
410 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
411 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
412 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
413 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
414
415 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
416 }
417 else {
418 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
419 if ($op) {
420
421 if ($tableName == 'civicrm_relationship_type' &&
422 ($fieldName == 'contact_type_a' || $fieldName == 'contact_type_b')
423 ) {
424 $cTypes = CRM_Utils_Array::value("{$fieldName}_value", $this->_params);
425 $contactTypes = $contactSubTypes = array();
426 if (!empty($cTypes)) {
427 foreach ($cTypes as $ctype) {
428 $getTypes = CRM_Utils_System::explode('_', $ctype, 2);
429 if ($getTypes[1] && !in_array($getTypes[1], $contactSubTypes)) {
430 $contactSubTypes[] = $getTypes[1];
431 }
432 elseif ($getTypes[0] && !in_array($getTypes[0], $contactTypes)) {
433 $contactTypes[] = $getTypes[0];
434 }
435 }
436 }
437
438 if (!empty($contactTypes)) {
439 $clause = $this->whereClause($field,
440 $op,
441 $contactTypes,
442 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
443 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
444 );
445 }
446
447 if (!empty($contactSubTypes)) {
448 if ($fieldName == 'contact_type_a') {
449 $field['name'] = 'contact_sub_type_a';
450 }
451 else {
452 $field['name'] = 'contact_sub_type_b';
453 }
454 $field['dbAlias'] = $field['alias'] . '.' . $field['name'];
455 $subTypeClause = $this->whereClause($field,
456 $op,
457 $contactSubTypes,
458 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
459 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
460 );
461 if ($clause) {
462 $clause = '(' . $clause . ' OR ' . $subTypeClause . ')';
463 }
464 else {
465 $clause = $subTypeClause;
466 }
467 }
468 }
469 else {
470
471 $clause = $this->whereClause($field,
472 $op,
473 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
474 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
475 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
476 );
477 }
478 }
479 }
480
481 if (!empty($clause)) {
482 if (!empty($field['having'])) {
483 $havingClauses[] = $clause;
484 }
485 else {
486 $whereClauses[] = $clause;
487 }
488 }
489 }
490 }
491 }
492
493 if (empty($whereClauses)) {
494 $this->_where = 'WHERE ( 1 ) ';
495 $this->_having = '';
496 }
497 else {
498 $this->_where = 'WHERE ' . implode(' AND ', $whereClauses);
499 }
500
501 if ($this->_aclWhere) {
502 $this->_where .= " AND {$this->_aclWhere} ";
503 }
504
505 if (!empty($havingClauses)) {
506 // use this clause to construct group by clause.
507 $this->_having = 'HAVING ' . implode(' AND ', $havingClauses);
508 }
509 }
510
511 function statistics(&$rows) {
512 $statistics = parent::statistics($rows);
513
514 $isStatusFilter = FALSE;
515 $relStatus = NULL;
516 if (CRM_Utils_Array::value('is_active_value', $this->_params) == '1') {
517 $relStatus = 'Is equal to Active';
518 }
519 elseif (CRM_Utils_Array::value('is_active_value', $this->_params) == '0') {
520 $relStatus = 'Is equal to Inactive';
521 }
522 if (!empty($statistics['filters'])) {
523 foreach ($statistics['filters'] as $id => $value) {
524 //for displaying relationship type filter
525 if ($value['title'] == 'Relationship') {
526 $relTypes = CRM_Core_PseudoConstant::relationshipType();
527 $statistics['filters'][$id]['value'] = 'Is equal to ' . $relTypes[$this->_params['relationship_type_id_value']]['label_' . $this->relationType];
528 }
529
530 //for displaying relationship status
531 if ($value['title'] == 'Relationship Status') {
532 $isStatusFilter = TRUE;
533 $statistics['filters'][$id]['value'] = $relStatus;
534 }
535 }
536 }
537 //for displaying relationship status
538 if (!$isStatusFilter && $relStatus) {
539 $statistics['filters'][] = array(
540 'title' => 'Relationship Status',
541 'value' => $relStatus,
542 );
543 }
544 return $statistics;
545 }
546
547 function groupBy() {
548 $this->_groupBy = " ";
549 $groupBy = array();
550 if ($this->relationType == 'a_b') {
551 $groupBy[] = " {$this->_aliases['civicrm_contact']}.id";
552 }
553 elseif ($this->relationType == 'b_a') {
554 $groupBy[] = " {$this->_aliases['civicrm_contact_b']}.id";
555 }
556
557 if (!empty($groupBy)) {
558 $this->_groupBy = " GROUP BY " . implode(', ', $groupBy) . " , {$this->_aliases['civicrm_relationship']}.id ";
559 }
560 else {
561 $this->_groupBy = " GROUP BY {$this->_aliases['civicrm_relationship']}.id ";
562 }
563 }
564
565 function orderBy() {
566 $this->_orderBy = " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact_b']}.sort_name ";
567 }
568
569 function postProcess() {
570 $this->beginPostProcess();
571
572 $this->relationType = NULL;
573 $relType = array();
574 if (!empty($this->_params['relationship_type_id_value'])) {
575 $relType = explode('_', $this->_params['relationship_type_id_value']);
576
577 $this->relationType = $relType[1] . '_' . $relType[2];
578 $this->_params['relationship_type_id_value'] = intval($relType[0]);
579 }
580
581 $this->buildACLClause(array($this->_aliases['civicrm_contact'], $this->_aliases['civicrm_contact_b']));
582 $sql = $this->buildQuery();
583 $this->buildRows($sql, $rows);
584
585 $this->formatDisplay($rows);
586 $this->doTemplateAssignment($rows);
587
588 if (!empty($relType)) {
589 // store its old value, CRM-5837
590 $this->_params['relationship_type_id_value'] = implode('_', $relType);
591 }
592 $this->endPostProcess($rows);
593 }
594
595 function alterDisplay(&$rows) {
596 // custom code to alter rows
597 $entryFound = FALSE;
598
599 foreach ($rows as $rowNum => $row) {
600
601 // handle country
602 if (array_key_exists('civicrm_address_country_id', $row)) {
603 if ($value = $row['civicrm_address_country_id']) {
604 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country($value, FALSE);
605 }
606 $entryFound = TRUE;
607 }
608
609 if (array_key_exists('civicrm_address_state_province_id', $row)) {
610 if ($value = $row['civicrm_address_state_province_id']) {
611 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvince($value, FALSE);
612 }
613 $entryFound = TRUE;
614 }
615
616 if (array_key_exists('civicrm_contact_sort_name_a', $row) &&
617 array_key_exists('civicrm_contact_id', $row)
618 ) {
619 $url = CRM_Report_Utils_Report::getNextUrl('contact/detail',
620 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
621 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
622 );
623 $rows[$rowNum]['civicrm_contact_sort_name_a'] = $rows[$rowNum]['civicrm_contact_sort_name_a'] . ' (' . $rows[$rowNum]['civicrm_contact_id'] . ')';
624 $rows[$rowNum]['civicrm_contact_sort_name_a_link'] = $url;
625 $rows[$rowNum]['civicrm_contact_sort_name_a_hover'] = ts("View Contact details for this contact.");
626 $entryFound = TRUE;
627 }
628
629 if (array_key_exists('civicrm_contact_b_sort_name_b', $row) &&
630 array_key_exists('civicrm_contact_b_id', $row)
631 ) {
632 $url = CRM_Report_Utils_Report::getNextUrl('contact/detail',
633 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_b_id'],
634 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
635 );
636 $rows[$rowNum]['civicrm_contact_b_sort_name_b'] = $rows[$rowNum]['civicrm_contact_b_sort_name_b'] . ' (' . $rows[$rowNum]['civicrm_contact_b_id'] . ')';
637 $rows[$rowNum]['civicrm_contact_b_sort_name_b_link'] = $url;
638 $rows[$rowNum]['civicrm_contact_b_sort_name_b_hover'] = ts("View Contact details for this contact.");
639 $entryFound = TRUE;
640 }
641
642 if (array_key_exists('civicrm_relationship_relationship_id', $row) && array_key_exists('civicrm_contact_id', $row)) {
643 $url = "/civicrm/contact/view/rel?reset=1&action=update&rtype=a_b&cid=" . $row['civicrm_contact_id'] . "&id=" . $row['civicrm_relationship_relationship_id'];
644 $rows[$rowNum]['civicrm_relationship_relationship_id_link'] = $url;
645 $rows[$rowNum]['civicrm_relationship_relationship_id_hover'] = ts("Edit this relationship.");
646 $entryFound = TRUE;
647 }
648
649 // skip looking further in rows, if first row itself doesn't
650 // have the column we need
651 if (!$entryFound) {
652 break;
653 }
654 }
655 }
656 }
657