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 will be a_b or b_a.
52 protected $relationType;
57 public function __construct() {
59 $contact_type = CRM_Contact_BAO_ContactType
::getSelectElements(FALSE, TRUE, '_');
61 $this->_columns
= array(
62 'civicrm_contact' => array(
63 'dao' => 'CRM_Contact_DAO_Contact',
65 'sort_name_a' => array(
66 'title' => ts('Contact A'),
67 'name' => 'sort_name',
70 'display_name_a' => array(
71 'title' => ts('Contact A Full Name'),
72 'name' => 'display_name',
78 'contact_type_a' => array(
79 'title' => ts('Contact Type (Contact A)'),
80 'name' => 'contact_type',
82 'contact_sub_type_a' => array(
83 'title' => ts('Contact Subtype (Contact A)'),
84 'name' => 'contact_sub_type',
88 'sort_name_a' => array(
89 'title' => ts('Contact A'),
90 'name' => 'sort_name',
92 'type' => CRM_Report_Form
::OP_STRING
,
94 'contact_type_a' => array(
95 'title' => ts('Contact Type A'),
96 'name' => 'contact_type',
97 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
98 'options' => $contact_type,
99 'type' => CRM_Utils_Type
::T_STRING
,
102 'grouping' => 'contact_a_fields',
104 'civicrm_contact_b' => array(
105 'dao' => 'CRM_Contact_DAO_Contact',
106 'alias' => 'contact_b',
108 'sort_name_b' => array(
109 'title' => ts('Contact B'),
110 'name' => 'sort_name',
113 'display_name_b' => array(
114 'title' => ts('Contact B Full Name'),
115 'name' => 'display_name',
118 'no_display' => TRUE,
121 'contact_type_b' => array(
122 'title' => ts('Contact Type (Contact B)'),
123 'name' => 'contact_type',
125 'contact_sub_type_b' => array(
126 'title' => ts('Contact Subtype (Contact B)'),
127 'name' => 'contact_sub_type',
131 'sort_name_b' => array(
132 'title' => ts('Contact B'),
133 'name' => 'sort_name',
134 'operator' => 'like',
135 'type' => CRM_Report_Form
::OP_STRING
,
137 'contact_type_b' => array(
138 'title' => ts('Contact Type B'),
139 'name' => 'contact_type',
140 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
141 'options' => $contact_type,
142 'type' => CRM_Utils_Type
::T_STRING
,
145 'grouping' => 'contact_b_fields',
147 'civicrm_email' => array(
148 'dao' => 'CRM_Core_DAO_Email',
151 'title' => ts('Email (Contact A)'),
155 'grouping' => 'contact_a_fields',
157 'civicrm_email_b' => array(
158 'dao' => 'CRM_Core_DAO_Email',
159 'alias' => 'email_b',
162 'title' => ts('Email (Contact B)'),
166 'grouping' => 'contact_b_fields',
168 'civicrm_phone' => array(
169 'dao' => 'CRM_Core_DAO_Phone',
170 'alias' => 'phone_a',
173 'title' => ts('Phone (Contact A)'),
176 'phone_ext_a' => array(
177 'title' => ts('Phone Ext (Contact A)'),
178 'name' => 'phone_ext',
181 'grouping' => 'contact_a_fields',
183 'civicrm_phone_b' => array(
184 'dao' => 'CRM_Core_DAO_Phone',
185 'alias' => 'phone_b',
188 'title' => ts('Phone (Contact B)'),
191 'phone_ext_b' => array(
192 'title' => ts('Phone Ext (Contact B)'),
193 'name' => 'phone_ext',
196 'grouping' => 'contact_b_fields',
198 'civicrm_relationship_type' => array(
199 'dao' => 'CRM_Contact_DAO_RelationshipType',
201 'label_a_b' => array(
202 'title' => ts('Relationship A-B '),
205 'label_b_a' => array(
206 'title' => ts('Relationship B-A '),
210 'grouping' => 'relation-fields',
212 'civicrm_relationship' => array(
213 'dao' => 'CRM_Contact_DAO_Relationship',
215 'start_date' => array(
216 'title' => ts('Relationship Start Date'),
219 'title' => ts('Relationship End Date'),
221 'description' => array(
222 'title' => ts('Description'),
224 'relationship_id' => array(
225 'title' => ts('Rel ID'),
230 'is_active' => array(
231 'title' => ts('Relationship Status'),
232 'operatorType' => CRM_Report_Form
::OP_SELECT
,
238 'type' => CRM_Utils_Type
::T_INT
,
241 'title' => ts('Relationship Dates Validity'),
242 'operatorType' => CRM_Report_Form
::OP_SELECT
,
244 NULL => ts('- Any -'),
245 1 => ts('Not expired'),
248 'type' => CRM_Utils_Type
::T_INT
,
250 'relationship_type_id' => array(
251 'title' => ts('Relationship'),
252 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
253 'options' => CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, 'null', NULL, NULL, TRUE),
254 'type' => CRM_Utils_Type
::T_INT
,
256 'start_date' => array(
257 'title' => ts('Start Date'),
258 'type' => CRM_Utils_Type
::T_DATE
,
261 'title' => ts('End Date'),
262 'type' => CRM_Utils_Type
::T_DATE
,
265 'grouping' => 'relation-fields',
267 'civicrm_address' => array(
268 'dao' => 'CRM_Core_DAO_Address',
270 'country_id' => array(
271 'title' => ts('Country'),
272 'type' => CRM_Utils_Type
::T_INT
,
273 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
274 'options' => CRM_Core_PseudoConstant
::country(),
276 'state_province_id' => array(
277 'title' => ts('State/Province'),
278 'type' => CRM_Utils_Type
::T_INT
,
279 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
280 'options' => CRM_Core_PseudoConstant
::stateProvince(),
283 'grouping' => 'contact-fields',
287 $this->_groupFilter
= TRUE;
288 $this->_tagFilter
= TRUE;
289 parent
::__construct();
292 public function preProcess() {
293 parent
::preProcess();
296 public function select() {
297 $select = $this->_columnHeaders
= array();
298 foreach ($this->_columns
as $tableName => $table) {
299 if (array_key_exists('fields', $table)) {
300 foreach ($table['fields'] as $fieldName => $field) {
301 if (!empty($field['required']) ||
302 !empty($this->_params
['fields'][$fieldName])
305 if ($fieldName == 'email_a') {
306 $this->_emailField_a
= TRUE;
308 if ($fieldName == 'email_b') {
309 $this->_emailField_b
= TRUE;
311 if ($fieldName == 'phone_a') {
312 $this->_phoneField_a
= TRUE;
314 if ($fieldName == 'phone_b') {
315 $this->_phoneField_b
= TRUE;
317 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
318 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
319 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
324 $this->_selectClauses
= $select;
326 $this->_select
= "SELECT " . implode(', ', $select) . " ";
329 public function from() {
331 FROM civicrm_relationship {$this->_aliases['civicrm_relationship']}
333 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
334 ON ( {$this->_aliases['civicrm_relationship']}.contact_id_a =
335 {$this->_aliases['civicrm_contact']}.id )
337 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact_b']}
338 ON ( {$this->_aliases['civicrm_relationship']}.contact_id_b =
339 {$this->_aliases['civicrm_contact_b']}.id )
343 if (!empty($this->_params
['country_id_value']) ||
344 !empty($this->_params
['state_province_id_value'])
347 INNER JOIN civicrm_address {$this->_aliases['civicrm_address']}
348 ON (( {$this->_aliases['civicrm_address']}.contact_id =
349 {$this->_aliases['civicrm_contact']}.id OR
350 {$this->_aliases['civicrm_address']}.contact_id =
351 {$this->_aliases['civicrm_contact_b']}.id ) AND
352 {$this->_aliases['civicrm_address']}.is_primary = 1 ) ";
356 INNER JOIN civicrm_relationship_type {$this->_aliases['civicrm_relationship_type']}
357 ON ( {$this->_aliases['civicrm_relationship']}.relationship_type_id =
358 {$this->_aliases['civicrm_relationship_type']}.id ) ";
360 // include Email Field
361 if ($this->_emailField_a
) {
363 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
364 ON ( {$this->_aliases['civicrm_contact']}.id =
365 {$this->_aliases['civicrm_email']}.contact_id AND
366 {$this->_aliases['civicrm_email']}.is_primary = 1 )";
368 if ($this->_emailField_b
) {
370 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email_b']}
371 ON ( {$this->_aliases['civicrm_contact_b']}.id =
372 {$this->_aliases['civicrm_email_b']}.contact_id AND
373 {$this->_aliases['civicrm_email_b']}.is_primary = 1 )";
375 // include Phone Field
376 if ($this->_phoneField_a
) {
378 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
379 ON ( {$this->_aliases['civicrm_contact']}.id =
380 {$this->_aliases['civicrm_phone']}.contact_id AND
381 {$this->_aliases['civicrm_phone']}.is_primary = 1 )";
383 if ($this->_phoneField_b
) {
385 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone_b']}
386 ON ( {$this->_aliases['civicrm_contact_b']}.id =
387 {$this->_aliases['civicrm_phone_b']}.contact_id AND
388 {$this->_aliases['civicrm_phone_b']}.is_primary = 1 )";
392 public function where() {
393 $whereClauses = $havingClauses = array();
394 foreach ($this->_columns
as $tableName => $table) {
395 if (array_key_exists('filters', $table)) {
396 foreach ($table['filters'] as $fieldName => $field) {
399 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
400 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
401 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
402 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
404 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
407 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
409 if (($tableName == 'civicrm_contact' ||
410 $tableName == 'civicrm_contact_b') &&
411 ($fieldName == 'contact_type_a' ||
412 $fieldName == 'contact_type_b')
414 $cTypes = CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
);
415 $contactTypes = $contactSubTypes = array();
416 if (!empty($cTypes)) {
417 foreach ($cTypes as $ctype) {
418 $getTypes = CRM_Utils_System
::explode('_', $ctype, 2);
420 !in_array($getTypes[1], $contactSubTypes)
422 $contactSubTypes[] = $getTypes[1];
424 elseif ($getTypes[0] &&
425 !in_array($getTypes[0], $contactTypes)
427 $contactTypes[] = $getTypes[0];
432 if (!empty($contactTypes)) {
433 $clause = $this->whereClause($field,
436 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
437 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
441 if (!empty($contactSubTypes)) {
442 $field['name'] = 'contact_sub_type';
443 $field['dbAlias'] = $field['alias'] . '.' . $field['name'];
444 $subTypeClause = $this->whereClause($field,
447 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
448 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
451 $clause = '(' . $clause . ' OR ' . $subTypeClause . ')';
454 $clause = $subTypeClause;
459 if ($fieldName == 'is_valid') {
460 $clause = $this->buildValidityQuery(CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
));
463 $clause = $this->whereClause($field,
465 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
466 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
467 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
474 if (!empty($clause)) {
475 if (!empty($field['having'])) {
476 $havingClauses[] = $clause;
479 $whereClauses[] = $clause;
486 if (empty($whereClauses)) {
487 $this->_where
= 'WHERE ( 1 ) ';
491 $this->_where
= 'WHERE ' . implode(' AND ', $whereClauses);
494 if ($this->_aclWhere
) {
495 $this->_where
.= " AND {$this->_aclWhere} ";
498 if (!empty($havingClauses)) {
499 // use this clause to construct group by clause.
500 $this->_having
= 'HAVING ' . implode(' AND ', $havingClauses);
509 public function statistics(&$rows) {
510 $statistics = parent
::statistics($rows);
512 $isStatusFilter = FALSE;
514 if (CRM_Utils_Array
::value('is_active_value', $this->_params
) == '1') {
515 $relStatus = 'Is equal to Active';
517 elseif (CRM_Utils_Array
::value('is_active_value', $this->_params
) == '0') {
518 $relStatus = 'Is equal to Inactive';
520 if (!empty($statistics['filters'])) {
521 foreach ($statistics['filters'] as $id => $value) {
522 //for displaying relationship type filter
523 if ($value['title'] == 'Relationship') {
524 $relTypes = CRM_Core_PseudoConstant
::relationshipType();
525 $op = CRM_Utils_Array
::value('relationship_type_id_op', $this->_params
) == 'in' ?
526 ts('Is one of') . ' ' : ts('Is not one of') . ' ';
527 $relationshipTypes = array();
528 foreach ($this->_params
['relationship_type_id_value'] as $relationship) {
529 $relationshipTypes[] = $relTypes[$relationship]['label_' . $this->relationType
];
531 $statistics['filters'][$id]['value'] = $op .
532 implode(', ', $relationshipTypes);
535 //for displaying relationship status
536 if ($value['title'] == 'Relationship Status') {
537 $isStatusFilter = TRUE;
538 $statistics['filters'][$id]['value'] = $relStatus;
542 //for displaying relationship status
543 if (!$isStatusFilter && $relStatus) {
544 $statistics['filters'][] = array(
545 'title' => 'Relationship Status',
546 'value' => $relStatus,
552 public function groupBy() {
553 $this->_groupBy
= " ";
555 if ($this->relationType
== 'a_b') {
556 $groupBy[] = " {$this->_aliases['civicrm_contact']}.id";
558 elseif ($this->relationType
== 'b_a') {
559 $groupBy[] = " {$this->_aliases['civicrm_contact_b']}.id";
562 if (!empty($groupBy)) {
563 $groupBy[] = "{$this->_aliases['civicrm_relationship']}.id";
566 $groupBy = array("{$this->_aliases['civicrm_relationship']}.id");
569 $this->_groupBy
= CRM_Contact_BAO_Query
::getGroupByFromSelectColumns($this->_selectClauses
, $groupBy);
572 public function orderBy() {
573 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact_b']}.sort_name ";
576 public function postProcess() {
577 $this->beginPostProcess();
579 $originalRelationshipTypeIdValue = $this->_params
['relationship_type_id_value'];
580 if (!empty($this->_params
['relationship_type_id_value'])) {
581 $relationshipTypes = array();
582 $direction = array();
584 foreach ($this->_params
['relationship_type_id_value'] as $relationship_type) {
585 $relType = explode('_', $relationship_type);
586 $direction[] = $relType[1] . '_' . $relType[2];
587 $relationshipTypes[] = intval($relType[0]);
589 // Lets take the first relationship type to guide us in the relationship direction
591 $this->relationType
= $direction[0];
592 $this->_params
['relationship_type_id_value'] = $relationshipTypes;
595 $this->buildACLClause(array(
596 $this->_aliases
['civicrm_contact'],
597 $this->_aliases
['civicrm_contact_b'],
599 $sql = $this->buildQuery();
600 $this->buildRows($sql, $rows);
602 $this->formatDisplay($rows);
603 $this->doTemplateAssignment($rows);
605 if (!empty($originalRelationshipTypeIdValue)) {
606 // store its old value, CRM-5837
607 $this->_params
['relationship_type_id_value'] = $originalRelationshipTypeIdValue;
609 $this->endPostProcess($rows);
615 public function alterDisplay(&$rows) {
616 // custom code to alter rows
619 foreach ($rows as $rowNum => $row) {
622 if (array_key_exists('civicrm_address_country_id', $row)) {
623 if ($value = $row['civicrm_address_country_id']) {
624 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
629 if (array_key_exists('civicrm_address_state_province_id', $row)) {
630 if ($value = $row['civicrm_address_state_province_id']) {
631 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
636 if (array_key_exists('civicrm_contact_sort_name_a', $row) &&
637 array_key_exists('civicrm_contact_id', $row)
639 $url = CRM_Report_Utils_Report
::getNextUrl('contact/detail',
640 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
641 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
643 $rows[$rowNum]['civicrm_contact_sort_name_a']
644 = $rows[$rowNum]['civicrm_contact_sort_name_a'] . ' (' .
645 $rows[$rowNum]['civicrm_contact_id'] . ')';
646 $rows[$rowNum]['civicrm_contact_sort_name_a_link'] = $url;
647 $rows[$rowNum]['civicrm_contact_sort_name_a_hover'] = ts("View Contact details for this contact.");
651 if (array_key_exists('civicrm_contact_b_sort_name_b', $row) &&
652 array_key_exists('civicrm_contact_b_id', $row)
654 $url = CRM_Report_Utils_Report
::getNextUrl('contact/detail',
655 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_b_id'],
656 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
658 $rows[$rowNum]['civicrm_contact_b_sort_name_b']
659 = $rows[$rowNum]['civicrm_contact_b_sort_name_b'] . ' (' .
660 $rows[$rowNum]['civicrm_contact_b_id'] . ')';
661 $rows[$rowNum]['civicrm_contact_b_sort_name_b_link'] = $url;
662 $rows[$rowNum]['civicrm_contact_b_sort_name_b_hover'] = ts("View Contact details for this contact.");
666 if (array_key_exists('civicrm_relationship_relationship_id', $row) &&
667 array_key_exists('civicrm_contact_id', $row)
669 $url = "/civicrm/contact/view/rel?reset=1&action=update&rtype=a_b&cid=" .
670 $row['civicrm_contact_id'] . "&id=" .
671 $row['civicrm_relationship_relationship_id'];
672 $rows[$rowNum]['civicrm_relationship_relationship_id_link'] = $url;
673 $rows[$rowNum]['civicrm_relationship_relationship_id_hover'] = ts("Edit this relationship.");
677 // skip looking further in rows, if first row itself doesn't
678 // have the column we need
686 * @param $valid bool - set to 1 if we are looking for a valid relationship, 0 if not
690 public function buildValidityQuery($valid) {
693 // relationships dates are not expired
694 $clause = "((start_date <= CURDATE() OR start_date is null) AND (end_date >= CURDATE() OR end_date is null))";
696 elseif ($valid == '0') {
697 // relationships dates are expired or has not started yet
698 $clause = "(start_date >= CURDATE() OR end_date < CURDATE())";