4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2014
36 class CRM_Report_Form_Contact_Relationship
extends CRM_Report_Form
{
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(
45 public $_drilldownReport = array('contact/detail' => 'Link to Detail Report');
53 function __construct() {
55 $contact_type = CRM_Contact_BAO_ContactType
::getSelectElements(FALSE, TRUE, '_');
57 $this->_columns
= array(
60 'dao' => 'CRM_Contact_DAO_Contact',
64 array('title' => ts('Contact A'),
65 'name' => 'sort_name',
75 'title' => ts('Contact Type (Contact A)'),
76 'name' => 'contact_type',
78 'contact_sub_type_a' =>
80 'title' => ts('Contact SubType (Contact A)'),
81 'name' => 'contact_sub_type',
87 array('title' => ts('Contact A'),
88 'name' => 'sort_name',
90 'type' => CRM_Report_Form
::OP_STRING
,
93 'grouping' => 'conact_a_fields',
95 'civicrm_contact_b' =>
97 'dao' => 'CRM_Contact_DAO_Contact',
98 'alias' => 'contact_b',
102 array('title' => ts('Contact B'),
103 'name' => 'sort_name',
108 'no_display' => TRUE,
113 'title' => ts('Contact Type (Contact B)'),
114 'name' => 'contact_type',
116 'contact_sub_type_b' =>
118 'title' => ts('Contact SubType (Contact B)'),
119 'name' => 'contact_sub_type',
125 array('title' => ts('Contact B'),
126 'name' => 'sort_name',
127 'operator' => 'like',
128 'type' => CRM_Report_Form
::OP_STRING
,
131 'grouping' => 'conact_b_fields',
135 'dao' => 'CRM_Core_DAO_Email',
139 array('title' => ts('Email (Contact A)'),
143 'grouping' => 'conact_a_fields',
147 'dao' => 'CRM_Core_DAO_Email',
148 'alias' => 'email_b',
152 array('title' => ts('Email (Contact B)'),
156 'grouping' => 'conact_b_fields',
160 'dao' => 'CRM_Core_DAO_Phone',
161 'alias' => 'phone_a',
166 'title' => ts('Phone (Contact A)'),
171 'title' => ts('Phone Ext (Contact A)'),
172 'name' => 'phone_ext',
175 'grouping' => 'conact_a_fields',
179 'dao' => 'CRM_Core_DAO_Phone',
180 'alias' => 'phone_b',
185 'title' => ts('Phone (Contact B)'),
190 'title' => ts('Phone Ext (Contact B)'),
191 'name' => 'phone_ext'
194 'grouping' => 'conact_b_fields',
196 'civicrm_relationship_type' =>
198 'dao' => 'CRM_Contact_DAO_RelationshipType',
202 array('title' => ts('Relationship A-B '),
206 array('title' => ts('Relationship B-A '),
213 array('title' => ts('Contact Type A'),
214 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
215 'options' => $contact_type,
216 'type' => CRM_Utils_Type
::T_STRING
,
219 array('title' => ts('Contact Type B'),
220 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
221 'options' => $contact_type,
222 'type' => CRM_Utils_Type
::T_STRING
,
225 'grouping' => 'relation-fields',
227 'civicrm_relationship' =>
229 'dao' => 'CRM_Contact_DAO_Relationship',
233 array('title' => ts('Relationship Start Date'),
236 array('title' => ts('Relationship End Date'),
239 array('title' => ts('Description'),
243 'title' => ts('Rel ID'),
250 array('title' => ts('Relationship Status'),
251 'operatorType' => CRM_Report_Form
::OP_SELECT
,
258 'type' => CRM_Utils_Type
::T_INT
,
260 'relationship_type_id' =>
261 array('title' => ts('Relationship'),
262 'operatorType' => CRM_Report_Form
::OP_SELECT
,
265 '' => '- any relationship type -') +
266 CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, 'null', NULL, NULL, TRUE),
267 'type' => CRM_Utils_Type
::T_INT
,
270 'grouping' => 'relation-fields',
274 'dao' => 'CRM_Core_DAO_Address',
278 array('title' => ts('Country'),
279 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
280 'options' => CRM_Core_PseudoConstant
::country(),
282 'state_province_id' =>
283 array('title' => ts('State/Province'),
284 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
285 'options' => CRM_Core_PseudoConstant
::stateProvince(),
288 'grouping' => 'contact-fields',
292 'dao' => 'CRM_Contact_DAO_Group',
298 'name' => 'group_id',
299 'title' => ts('Group'),
300 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
302 'options' => CRM_Core_PseudoConstant
::group(),
308 $this->_tagFilter
= TRUE;
309 parent
::__construct();
312 function preProcess() {
313 parent
::preProcess();
317 $select = $this->_columnHeaders
= array();
318 foreach ($this->_columns
as $tableName => $table) {
319 if (array_key_exists('fields', $table)) {
320 foreach ($table['fields'] as $fieldName => $field) {
321 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
323 if ($fieldName == 'email_a') {
324 $this->_emailField_a
= TRUE;
326 if ($fieldName == 'email_b') {
327 $this->_emailField_b
= TRUE;
329 if ($fieldName == 'phone_a'){
330 $this->_phoneField_a
= TRUE;
332 if ($fieldName == 'phone_b'){
333 $this->_phoneField_b
= TRUE;
335 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
336 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
337 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
343 $this->_select
= "SELECT " . implode(', ', $select) . " ";
348 FROM civicrm_relationship {$this->_aliases['civicrm_relationship']}
350 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
351 ON ( {$this->_aliases['civicrm_relationship']}.contact_id_a =
352 {$this->_aliases['civicrm_contact']}.id )
354 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact_b']}
355 ON ( {$this->_aliases['civicrm_relationship']}.contact_id_b =
356 {$this->_aliases['civicrm_contact_b']}.id )
360 if (!empty($this->_params
['country_id_value']) ||
361 !empty($this->_params
['state_province_id_value'])
364 INNER JOIN civicrm_address {$this->_aliases['civicrm_address']}
365 ON (( {$this->_aliases['civicrm_address']}.contact_id =
366 {$this->_aliases['civicrm_contact']}.id OR
367 {$this->_aliases['civicrm_address']}.contact_id =
368 {$this->_aliases['civicrm_contact_b']}.id ) AND
369 {$this->_aliases['civicrm_address']}.is_primary = 1 ) ";
373 INNER JOIN civicrm_relationship_type {$this->_aliases['civicrm_relationship_type']}
374 ON ( {$this->_aliases['civicrm_relationship']}.relationship_type_id =
375 {$this->_aliases['civicrm_relationship_type']}.id ) ";
377 // include Email Field
378 if ($this->_emailField_a
) {
380 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
381 ON ( {$this->_aliases['civicrm_contact']}.id =
382 {$this->_aliases['civicrm_email']}.contact_id AND
383 {$this->_aliases['civicrm_email']}.is_primary = 1 )";
385 if ($this->_emailField_b
) {
387 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email_b']}
388 ON ( {$this->_aliases['civicrm_contact_b']}.id =
389 {$this->_aliases['civicrm_email_b']}.contact_id AND
390 {$this->_aliases['civicrm_email_b']}.is_primary = 1 )";
392 // include Phone Field
393 if ($this->_phoneField_a
) {
395 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
396 ON ( {$this->_aliases['civicrm_contact']}.id =
397 {$this->_aliases['civicrm_phone']}.contact_id AND
398 {$this->_aliases['civicrm_phone']}.is_primary = 1 )";
400 if ($this->_phoneField_b
) {
402 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone_b']}
403 ON ( {$this->_aliases['civicrm_contact_b']}.id =
404 {$this->_aliases['civicrm_phone_b']}.contact_id AND
405 {$this->_aliases['civicrm_phone_b']}.is_primary = 1 )";
410 $whereClauses = $havingClauses = array();
411 foreach ($this->_columns
as $tableName => $table) {
412 if (array_key_exists('filters', $table)) {
413 foreach ($table['filters'] as $fieldName => $field) {
416 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
417 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
418 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
419 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
421 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
424 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
427 if ($tableName == 'civicrm_relationship_type' &&
428 ($fieldName == 'contact_type_a' ||
$fieldName == 'contact_type_b')
430 $cTypes = CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
);
431 $contactTypes = $contactSubTypes = array();
432 if (!empty($cTypes)) {
433 foreach ($cTypes as $ctype) {
434 $getTypes = CRM_Utils_System
::explode('_', $ctype, 2);
435 if ($getTypes[1] && !in_array($getTypes[1], $contactSubTypes)) {
436 $contactSubTypes[] = $getTypes[1];
438 elseif ($getTypes[0] && !in_array($getTypes[0], $contactTypes)) {
439 $contactTypes[] = $getTypes[0];
444 if (!empty($contactTypes)) {
445 $clause = $this->whereClause($field,
448 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
449 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
453 if (!empty($contactSubTypes)) {
454 if ($fieldName == 'contact_type_a') {
455 $field['name'] = 'contact_sub_type_a';
458 $field['name'] = 'contact_sub_type_b';
460 $field['dbAlias'] = $field['alias'] . '.' . $field['name'];
461 $subTypeClause = $this->whereClause($field,
464 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
465 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
468 $clause = '(' . $clause . ' OR ' . $subTypeClause . ')';
471 $clause = $subTypeClause;
477 $clause = $this->whereClause($field,
479 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
480 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
481 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 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 $statistics['filters'][$id]['value'] = 'Is equal to ' . $relTypes[$this->_params
['relationship_type_id_value']]['label_' . $this->relationType
];
541 //for displaying relationship status
542 if ($value['title'] == 'Relationship Status') {
543 $isStatusFilter = TRUE;
544 $statistics['filters'][$id]['value'] = $relStatus;
548 //for displaying relationship status
549 if (!$isStatusFilter && $relStatus) {
550 $statistics['filters'][] = array(
551 'title' => 'Relationship Status',
552 'value' => $relStatus,
559 $this->_groupBy
= " ";
561 if ($this->relationType
== 'a_b') {
562 $groupBy[] = " {$this->_aliases['civicrm_contact']}.id";
564 elseif ($this->relationType
== 'b_a') {
565 $groupBy[] = " {$this->_aliases['civicrm_contact_b']}.id";
568 if (!empty($groupBy)) {
569 $this->_groupBy
= " GROUP BY " . implode(', ', $groupBy) . " , {$this->_aliases['civicrm_relationship']}.id ";
572 $this->_groupBy
= " GROUP BY {$this->_aliases['civicrm_relationship']}.id ";
577 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact_b']}.sort_name ";
580 function postProcess() {
581 $this->beginPostProcess();
583 $this->relationType
= NULL;
585 if (!empty($this->_params
['relationship_type_id_value'])) {
586 $relType = explode('_', $this->_params
['relationship_type_id_value']);
588 $this->relationType
= $relType[1] . '_' . $relType[2];
589 $this->_params
['relationship_type_id_value'] = intval($relType[0]);
592 $this->buildACLClause(array($this->_aliases
['civicrm_contact'], $this->_aliases
['civicrm_contact_b']));
593 $sql = $this->buildQuery();
594 $this->buildRows($sql, $rows);
596 $this->formatDisplay($rows);
597 $this->doTemplateAssignment($rows);
599 if (!empty($relType)) {
600 // store its old value, CRM-5837
601 $this->_params
['relationship_type_id_value'] = implode('_', $relType);
603 $this->endPostProcess($rows);
609 function alterDisplay(&$rows) {
610 // custom code to alter rows
613 foreach ($rows as $rowNum => $row) {
616 if (array_key_exists('civicrm_address_country_id', $row)) {
617 if ($value = $row['civicrm_address_country_id']) {
618 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
623 if (array_key_exists('civicrm_address_state_province_id', $row)) {
624 if ($value = $row['civicrm_address_state_province_id']) {
625 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
630 if (array_key_exists('civicrm_contact_sort_name_a', $row) &&
631 array_key_exists('civicrm_contact_id', $row)
633 $url = CRM_Report_Utils_Report
::getNextUrl('contact/detail',
634 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
635 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
637 $rows[$rowNum]['civicrm_contact_sort_name_a'] = $rows[$rowNum]['civicrm_contact_sort_name_a'] . ' (' . $rows[$rowNum]['civicrm_contact_id'] . ')';
638 $rows[$rowNum]['civicrm_contact_sort_name_a_link'] = $url;
639 $rows[$rowNum]['civicrm_contact_sort_name_a_hover'] = ts("View Contact details for this contact.");
643 if (array_key_exists('civicrm_contact_b_sort_name_b', $row) &&
644 array_key_exists('civicrm_contact_b_id', $row)
646 $url = CRM_Report_Utils_Report
::getNextUrl('contact/detail',
647 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_b_id'],
648 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
650 $rows[$rowNum]['civicrm_contact_b_sort_name_b'] = $rows[$rowNum]['civicrm_contact_b_sort_name_b'] . ' (' . $rows[$rowNum]['civicrm_contact_b_id'] . ')';
651 $rows[$rowNum]['civicrm_contact_b_sort_name_b_link'] = $url;
652 $rows[$rowNum]['civicrm_contact_b_sort_name_b_hover'] = ts("View Contact details for this contact.");
656 if (array_key_exists('civicrm_relationship_relationship_id', $row) && array_key_exists('civicrm_contact_id', $row)) {
657 $url = "/civicrm/contact/view/rel?reset=1&action=update&rtype=a_b&cid=" . $row['civicrm_contact_id'] . "&id=" . $row['civicrm_relationship_relationship_id'];
658 $rows[$rowNum]['civicrm_relationship_relationship_id_link'] = $url;
659 $rows[$rowNum]['civicrm_relationship_relationship_id_hover'] = ts("Edit this relationship.");
663 // skip looking further in rows, if first row itself doesn't
664 // have the column we need