3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2016
35 class CRM_Report_Form_Contact_Relationship
extends CRM_Report_Form
{
37 protected $_summary = NULL;
38 protected $_emailField_a = FALSE;
39 protected $_emailField_b = FALSE;
40 protected $_phoneField_a = FALSE;
41 protected $_phoneField_b = FALSE;
42 protected $_customGroupExtends = array(
45 public $_drilldownReport = array('contact/detail' => 'Link to Detail Report');
48 * This report has not been optimised for group filtering.
50 * The functionality for group filtering has been improved but not
51 * all reports have been adjusted to take care of it. This report has not
52 * and will run an inefficient query until fixed.
58 protected $groupFilterNotOptimised = TRUE;
61 * This will be a_b or b_a.
65 protected $relationType;
70 public function __construct() {
72 $contact_type = CRM_Contact_BAO_ContactType
::getSelectElements(FALSE, TRUE, '_');
74 $this->_columns
= array(
75 'civicrm_contact' => array(
76 'dao' => 'CRM_Contact_DAO_Contact',
78 'sort_name_a' => array(
79 'title' => ts('Contact A'),
80 'name' => 'sort_name',
83 'display_name_a' => array(
84 'title' => ts('Contact A Full Name'),
85 'name' => 'display_name',
91 'contact_type_a' => array(
92 'title' => ts('Contact Type (Contact A)'),
93 'name' => 'contact_type',
95 'contact_sub_type_a' => array(
96 'title' => ts('Contact Subtype (Contact A)'),
97 'name' => 'contact_sub_type',
101 'sort_name_a' => array(
102 'title' => ts('Contact A'),
103 'name' => 'sort_name',
104 'operator' => 'like',
105 'type' => CRM_Report_Form
::OP_STRING
,
107 'contact_type_a' => array(
108 'title' => ts('Contact Type A'),
109 'name' => 'contact_type',
110 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
111 'options' => $contact_type,
112 'type' => CRM_Utils_Type
::T_STRING
,
115 'grouping' => 'contact_a_fields',
117 'civicrm_contact_b' => array(
118 'dao' => 'CRM_Contact_DAO_Contact',
119 'alias' => 'contact_b',
121 'sort_name_b' => array(
122 'title' => ts('Contact B'),
123 'name' => 'sort_name',
126 'display_name_b' => array(
127 'title' => ts('Contact B Full Name'),
128 'name' => 'display_name',
131 'no_display' => TRUE,
134 'contact_type_b' => array(
135 'title' => ts('Contact Type (Contact B)'),
136 'name' => 'contact_type',
138 'contact_sub_type_b' => array(
139 'title' => ts('Contact Subtype (Contact B)'),
140 'name' => 'contact_sub_type',
144 'sort_name_b' => array(
145 'title' => ts('Contact B'),
146 'name' => 'sort_name',
147 'operator' => 'like',
148 'type' => CRM_Report_Form
::OP_STRING
,
150 'contact_type_b' => array(
151 'title' => ts('Contact Type B'),
152 'name' => 'contact_type',
153 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
154 'options' => $contact_type,
155 'type' => CRM_Utils_Type
::T_STRING
,
158 'grouping' => 'contact_b_fields',
160 'civicrm_email' => array(
161 'dao' => 'CRM_Core_DAO_Email',
164 'title' => ts('Email (Contact A)'),
168 'grouping' => 'contact_a_fields',
170 'civicrm_email_b' => array(
171 'dao' => 'CRM_Core_DAO_Email',
172 'alias' => 'email_b',
175 'title' => ts('Email (Contact B)'),
179 'grouping' => 'contact_b_fields',
181 'civicrm_phone' => array(
182 'dao' => 'CRM_Core_DAO_Phone',
183 'alias' => 'phone_a',
186 'title' => ts('Phone (Contact A)'),
189 'phone_ext_a' => array(
190 'title' => ts('Phone Ext (Contact A)'),
191 'name' => 'phone_ext',
194 'grouping' => 'contact_a_fields',
196 'civicrm_phone_b' => array(
197 'dao' => 'CRM_Core_DAO_Phone',
198 'alias' => 'phone_b',
201 'title' => ts('Phone (Contact B)'),
204 'phone_ext_b' => array(
205 'title' => ts('Phone Ext (Contact B)'),
206 'name' => 'phone_ext',
209 'grouping' => 'contact_b_fields',
211 'civicrm_relationship_type' => array(
212 'dao' => 'CRM_Contact_DAO_RelationshipType',
214 'label_a_b' => array(
215 'title' => ts('Relationship A-B '),
218 'label_b_a' => array(
219 'title' => ts('Relationship B-A '),
223 'grouping' => 'relation-fields',
225 'civicrm_relationship' => array(
226 'dao' => 'CRM_Contact_DAO_Relationship',
228 'start_date' => array(
229 'title' => ts('Relationship Start Date'),
232 'title' => ts('Relationship End Date'),
234 'description' => array(
235 'title' => ts('Description'),
237 'relationship_id' => array(
238 'title' => ts('Rel ID'),
243 'is_active' => array(
244 'title' => ts('Relationship Status'),
245 'operatorType' => CRM_Report_Form
::OP_SELECT
,
251 'type' => CRM_Utils_Type
::T_INT
,
254 'title' => ts('Relationship Dates Validity'),
255 'operatorType' => CRM_Report_Form
::OP_SELECT
,
257 NULL => ts('- Any -'),
258 1 => ts('Not expired'),
261 'type' => CRM_Utils_Type
::T_INT
,
263 'relationship_type_id' => array(
264 'title' => ts('Relationship'),
265 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
266 'options' => CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, 'null', NULL, NULL, TRUE),
267 'type' => CRM_Utils_Type
::T_INT
,
269 'start_date' => array(
270 'title' => ts('Start Date'),
271 'type' => CRM_Utils_Type
::T_DATE
,
274 'title' => ts('End Date'),
275 'type' => CRM_Utils_Type
::T_DATE
,
278 'grouping' => 'relation-fields',
280 'civicrm_address' => array(
281 'dao' => 'CRM_Core_DAO_Address',
283 'country_id' => array(
284 'title' => ts('Country'),
285 'type' => CRM_Utils_Type
::T_INT
,
286 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
287 'options' => CRM_Core_PseudoConstant
::country(),
289 'state_province_id' => array(
290 'title' => ts('State/Province'),
291 'type' => CRM_Utils_Type
::T_INT
,
292 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
293 'options' => CRM_Core_PseudoConstant
::stateProvince(),
296 'grouping' => 'contact-fields',
300 $this->_groupFilter
= TRUE;
301 $this->_tagFilter
= TRUE;
302 parent
::__construct();
305 public function preProcess() {
306 parent
::preProcess();
309 public function select() {
310 $select = $this->_columnHeaders
= array();
311 foreach ($this->_columns
as $tableName => $table) {
312 if (array_key_exists('fields', $table)) {
313 foreach ($table['fields'] as $fieldName => $field) {
314 if (!empty($field['required']) ||
315 !empty($this->_params
['fields'][$fieldName])
318 if ($fieldName == 'email_a') {
319 $this->_emailField_a
= TRUE;
321 if ($fieldName == 'email_b') {
322 $this->_emailField_b
= TRUE;
324 if ($fieldName == 'phone_a') {
325 $this->_phoneField_a
= TRUE;
327 if ($fieldName == 'phone_b') {
328 $this->_phoneField_b
= TRUE;
330 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
331 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
332 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
337 $this->_selectClauses
= $select;
339 $this->_select
= "SELECT " . implode(', ', $select) . " ";
342 public function from() {
344 FROM civicrm_relationship {$this->_aliases['civicrm_relationship']}
346 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
347 ON ( {$this->_aliases['civicrm_relationship']}.contact_id_a =
348 {$this->_aliases['civicrm_contact']}.id )
350 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact_b']}
351 ON ( {$this->_aliases['civicrm_relationship']}.contact_id_b =
352 {$this->_aliases['civicrm_contact_b']}.id )
356 if (!empty($this->_params
['country_id_value']) ||
357 !empty($this->_params
['state_province_id_value'])
360 INNER JOIN civicrm_address {$this->_aliases['civicrm_address']}
361 ON (( {$this->_aliases['civicrm_address']}.contact_id =
362 {$this->_aliases['civicrm_contact']}.id OR
363 {$this->_aliases['civicrm_address']}.contact_id =
364 {$this->_aliases['civicrm_contact_b']}.id ) AND
365 {$this->_aliases['civicrm_address']}.is_primary = 1 ) ";
369 INNER JOIN civicrm_relationship_type {$this->_aliases['civicrm_relationship_type']}
370 ON ( {$this->_aliases['civicrm_relationship']}.relationship_type_id =
371 {$this->_aliases['civicrm_relationship_type']}.id ) ";
373 // Include Email Field.
374 if ($this->_emailField_a
) {
376 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
377 ON ( {$this->_aliases['civicrm_contact']}.id =
378 {$this->_aliases['civicrm_email']}.contact_id AND
379 {$this->_aliases['civicrm_email']}.is_primary = 1 )";
381 if ($this->_emailField_b
) {
383 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email_b']}
384 ON ( {$this->_aliases['civicrm_contact_b']}.id =
385 {$this->_aliases['civicrm_email_b']}.contact_id AND
386 {$this->_aliases['civicrm_email_b']}.is_primary = 1 )";
388 // Include Phone Field.
389 if ($this->_phoneField_a
) {
391 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
392 ON ( {$this->_aliases['civicrm_contact']}.id =
393 {$this->_aliases['civicrm_phone']}.contact_id AND
394 {$this->_aliases['civicrm_phone']}.is_primary = 1 )";
396 if ($this->_phoneField_b
) {
398 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone_b']}
399 ON ( {$this->_aliases['civicrm_contact_b']}.id =
400 {$this->_aliases['civicrm_phone_b']}.contact_id AND
401 {$this->_aliases['civicrm_phone_b']}.is_primary = 1 )";
405 public function where() {
406 $whereClauses = $havingClauses = array();
407 foreach ($this->_columns
as $tableName => $table) {
408 if (array_key_exists('filters', $table)) {
409 foreach ($table['filters'] as $fieldName => $field) {
412 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
413 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
414 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
415 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
417 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
420 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
422 if (($tableName == 'civicrm_contact' ||
423 $tableName == 'civicrm_contact_b') &&
424 ($fieldName == 'contact_type_a' ||
425 $fieldName == 'contact_type_b')
427 $cTypes = CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
);
428 $contactTypes = $contactSubTypes = array();
429 if (!empty($cTypes)) {
430 foreach ($cTypes as $ctype) {
431 $getTypes = CRM_Utils_System
::explode('_', $ctype, 2);
433 !in_array($getTypes[1], $contactSubTypes)
435 $contactSubTypes[] = $getTypes[1];
437 elseif ($getTypes[0] &&
438 !in_array($getTypes[0], $contactTypes)
440 $contactTypes[] = $getTypes[0];
445 if (!empty($contactTypes)) {
446 $clause = $this->whereClause($field,
449 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
450 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
454 if (!empty($contactSubTypes)) {
455 $field['name'] = 'contact_sub_type';
456 $field['dbAlias'] = $field['alias'] . '.' . $field['name'];
457 $subTypeClause = $this->whereClause($field,
460 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
461 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
464 $clause = '(' . $clause . ' OR ' . $subTypeClause . ')';
467 $clause = $subTypeClause;
472 if ($fieldName == 'is_valid') {
473 $clause = $this->buildValidityQuery(CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
));
476 $clause = $this->whereClause($field,
478 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
479 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
480 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
487 if (!empty($clause)) {
488 if (!empty($field['having'])) {
489 $havingClauses[] = $clause;
492 $whereClauses[] = $clause;
499 if (empty($whereClauses)) {
500 $this->_where
= 'WHERE ( 1 ) ';
504 $this->_where
= 'WHERE ' . implode(' AND ', $whereClauses);
507 if ($this->_aclWhere
) {
508 $this->_where
.= " AND {$this->_aclWhere} ";
511 if (!empty($havingClauses)) {
512 // use this clause to construct group by clause.
513 $this->_having
= 'HAVING ' . implode(' AND ', $havingClauses);
522 public function statistics(&$rows) {
523 $statistics = parent
::statistics($rows);
525 $isStatusFilter = FALSE;
527 if (CRM_Utils_Array
::value('is_active_value', $this->_params
) == '1') {
528 $relStatus = 'Is equal to Active';
530 elseif (CRM_Utils_Array
::value('is_active_value', $this->_params
) == '0') {
531 $relStatus = 'Is equal to Inactive';
533 if (!empty($statistics['filters'])) {
534 foreach ($statistics['filters'] as $id => $value) {
535 // For displaying relationship type filter.
536 if ($value['title'] == 'Relationship') {
537 $relTypes = CRM_Core_PseudoConstant
::relationshipType();
538 $op = CRM_Utils_Array
::value('relationship_type_id_op', $this->_params
) == 'in' ?
ts('Is one of') . ' ' : ts('Is not one of') . ' ';
539 $relationshipTypes = array();
540 foreach ($this->_params
['relationship_type_id_value'] as $relationship) {
541 $relationshipTypes[] = $relTypes[$relationship]['label_' . $this->relationType
];
543 $statistics['filters'][$id]['value'] = $op .
544 implode(', ', $relationshipTypes);
547 // For displaying relationship status.
548 if ($value['title'] == 'Relationship Status') {
549 $isStatusFilter = TRUE;
550 $statistics['filters'][$id]['value'] = $relStatus;
554 // For displaying relationship status.
555 if (!$isStatusFilter && $relStatus) {
556 $statistics['filters'][] = array(
557 'title' => 'Relationship Status',
558 'value' => $relStatus,
564 public function groupBy() {
565 $this->_groupBy
= " ";
567 if ($this->relationType
== 'a_b') {
568 $groupBy[] = " {$this->_aliases['civicrm_contact']}.id";
570 elseif ($this->relationType
== 'b_a') {
571 $groupBy[] = " {$this->_aliases['civicrm_contact_b']}.id";
574 if (!empty($groupBy)) {
575 $groupBy[] = "{$this->_aliases['civicrm_relationship']}.id";
578 $groupBy = array("{$this->_aliases['civicrm_relationship']}.id");
581 $this->_groupBy
= CRM_Contact_BAO_Query
::getGroupByFromSelectColumns($this->_selectClauses
, $groupBy);
584 public function orderBy() {
585 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact_b']}.sort_name ";
588 public function postProcess() {
589 $this->beginPostProcess();
591 $originalRelationshipTypeIdValue = $this->_params
['relationship_type_id_value'];
592 if (!empty($this->_params
['relationship_type_id_value'])) {
593 $relationshipTypes = array();
594 $direction = array();
596 foreach ($this->_params
['relationship_type_id_value'] as $relationship_type) {
597 $relType = explode('_', $relationship_type);
598 $direction[] = $relType[1] . '_' . $relType[2];
599 $relationshipTypes[] = intval($relType[0]);
601 // Lets take the first relationship type to guide us in the relationship
602 // direction we should use.
603 $this->relationType
= $direction[0];
604 $this->_params
['relationship_type_id_value'] = $relationshipTypes;
607 $this->buildACLClause(array(
608 $this->_aliases
['civicrm_contact'],
609 $this->_aliases
['civicrm_contact_b'],
611 $sql = $this->buildQuery();
612 $this->buildRows($sql, $rows);
614 $this->formatDisplay($rows);
615 $this->doTemplateAssignment($rows);
617 if (!empty($originalRelationshipTypeIdValue)) {
618 // Store its old value, CRM-5837.
619 $this->_params
['relationship_type_id_value'] = $originalRelationshipTypeIdValue;
621 $this->endPostProcess($rows);
627 public function alterDisplay(&$rows) {
628 // Custom code to alter rows.
631 foreach ($rows as $rowNum => $row) {
634 if (array_key_exists('civicrm_address_country_id', $row)) {
635 if ($value = $row['civicrm_address_country_id']) {
636 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
641 if (array_key_exists('civicrm_address_state_province_id', $row)) {
642 if ($value = $row['civicrm_address_state_province_id']) {
643 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
648 if (array_key_exists('civicrm_contact_sort_name_a', $row) &&
649 array_key_exists('civicrm_contact_id', $row)
651 $url = CRM_Report_Utils_Report
::getNextUrl('contact/detail',
652 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
653 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
655 $rows[$rowNum]['civicrm_contact_sort_name_a']
656 = $rows[$rowNum]['civicrm_contact_sort_name_a'] . ' (' .
657 $rows[$rowNum]['civicrm_contact_id'] . ')';
658 $rows[$rowNum]['civicrm_contact_sort_name_a_link'] = $url;
659 $rows[$rowNum]['civicrm_contact_sort_name_a_hover'] = ts("View Contact details for this contact.");
663 if (array_key_exists('civicrm_contact_b_sort_name_b', $row) &&
664 array_key_exists('civicrm_contact_b_id', $row)
666 $url = CRM_Report_Utils_Report
::getNextUrl('contact/detail',
667 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_b_id'],
668 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
670 $rows[$rowNum]['civicrm_contact_b_sort_name_b']
671 = $rows[$rowNum]['civicrm_contact_b_sort_name_b'] . ' (' .
672 $rows[$rowNum]['civicrm_contact_b_id'] . ')';
673 $rows[$rowNum]['civicrm_contact_b_sort_name_b_link'] = $url;
674 $rows[$rowNum]['civicrm_contact_b_sort_name_b_hover'] = ts("View Contact details for this contact.");
678 if (array_key_exists('civicrm_relationship_relationship_id', $row) &&
679 array_key_exists('civicrm_contact_id', $row)
681 $url = "/civicrm/contact/view/rel?reset=1&action=update&rtype=a_b&cid=" .
682 $row['civicrm_contact_id'] . "&id=" .
683 $row['civicrm_relationship_relationship_id'];
684 $rows[$rowNum]['civicrm_relationship_relationship_id_link'] = $url;
685 $rows[$rowNum]['civicrm_relationship_relationship_id_hover'] = ts("Edit this relationship.");
689 // skip looking further in rows, if first row itself doesn't
690 // have the column we need
698 * @param $valid bool - set to 1 if we are looking for a valid relationship, 0 if not
702 public function buildValidityQuery($valid) {
705 // Relationships dates are not expired.
706 $clause = "((start_date <= CURDATE() OR start_date is null) AND (end_date >= CURDATE() OR end_date is null))";
708 elseif ($valid == '0') {
709 // Relationships dates are expired or has not started yet.
710 $clause = "(start_date >= CURDATE() OR end_date < CURDATE())";