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');
47 function __construct() {
49 $contact_type = CRM_Contact_BAO_ContactType
::getSelectElements(FALSE, TRUE, '_');
51 $this->_columns
= array(
54 'dao' => 'CRM_Contact_DAO_Contact',
58 array('title' => ts('Contact A'),
59 'name' => 'sort_name',
69 'title' => ts('Contact Type (Contact A)'),
70 'name' => 'contact_type',
72 'contact_sub_type_a' =>
74 'title' => ts('Contact SubType (Contact A)'),
75 'name' => 'contact_sub_type',
81 array('title' => ts('Contact A'),
82 'name' => 'sort_name',
84 'type' => CRM_Report_Form
::OP_STRING
,
87 'grouping' => 'conact_a_fields',
89 'civicrm_contact_b' =>
91 'dao' => 'CRM_Contact_DAO_Contact',
92 'alias' => 'contact_b',
96 array('title' => ts('Contact B'),
97 'name' => 'sort_name',
102 'no_display' => TRUE,
107 'title' => ts('Contact Type (Contact B)'),
108 'name' => 'contact_type',
110 'contact_sub_type_b' =>
112 'title' => ts('Contact SubType (Contact B)'),
113 'name' => 'contact_sub_type',
119 array('title' => ts('Contact B'),
120 'name' => 'sort_name',
121 'operator' => 'like',
122 'type' => CRM_Report_Form
::OP_STRING
,
125 'grouping' => 'conact_b_fields',
129 'dao' => 'CRM_Core_DAO_Email',
133 array('title' => ts('Email of Contact A'),
137 'grouping' => 'conact_a_fields',
141 'dao' => 'CRM_Core_DAO_Email',
142 'alias' => 'email_b',
146 array('title' => ts('Email of Contact B'),
150 'grouping' => 'conact_b_fields',
154 'dao' => 'CRM_Core_DAO_Phone',
155 'alias' => 'phone_a',
160 'title' => ts('Phone of Contact A'),
165 'title' => ts('Phone Extension of Contact A'),
166 'name' => 'phone_ext',
169 'grouping' => 'conact_a_fields',
173 'dao' => 'CRM_Core_DAO_Phone',
174 'alias' => 'phone_b',
179 'title' => ts('Phone of Contact B'),
184 'title' => ts('Phone Extension of Contact B'),
185 'name' => 'phone_ext'
188 'grouping' => 'conact_b_fields',
190 'civicrm_relationship_type' =>
192 'dao' => 'CRM_Contact_DAO_RelationshipType',
196 array('title' => ts('Relationship A-B '),
200 array('title' => ts('Relationship B-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
,
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
,
219 'grouping' => 'relation-fields',
221 'civicrm_relationship' =>
223 'dao' => 'CRM_Contact_DAO_Relationship',
227 array('title' => ts('Relationship Start Date'),
230 array('title' => ts('Relationship End Date'),
233 array('title' => ts('Description'),
239 array('title' => ts('Relationship Status'),
240 'operatorType' => CRM_Report_Form
::OP_SELECT
,
247 'type' => CRM_Utils_Type
::T_INT
,
249 'relationship_type_id' =>
250 array('title' => ts('Relationship'),
251 'operatorType' => CRM_Report_Form
::OP_SELECT
,
254 '' => '- any relationship type -') +
255 CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, 'null', NULL, NULL, TRUE),
256 'type' => CRM_Utils_Type
::T_INT
,
259 'grouping' => 'relation-fields',
263 'dao' => 'CRM_Core_DAO_Address',
267 array('title' => ts('Country'),
268 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
269 'options' => CRM_Core_PseudoConstant
::country(),
271 'state_province_id' =>
272 array('title' => ts('State/Province'),
273 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
274 'options' => CRM_Core_PseudoConstant
::stateProvince(),
277 'grouping' => 'contact-fields',
281 'dao' => 'CRM_Contact_DAO_Group',
287 'name' => 'group_id',
288 'title' => ts('Group'),
289 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
291 'options' => CRM_Core_PseudoConstant
::group(),
297 $this->_tagFilter
= TRUE;
298 parent
::__construct();
301 function preProcess() {
302 parent
::preProcess();
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) {
310 if (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
312 if ($fieldName == 'email_a') {
313 $this->_emailField_a
= TRUE;
315 if ($fieldName == 'email_b') {
316 $this->_emailField_b
= TRUE;
318 if ($fieldName == 'phone_a'){
319 $this->_phoneField_a
= TRUE;
321 if ($fieldName == 'phone_b'){
322 $this->_phoneField_b
= TRUE;
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);
332 $this->_select
= "SELECT " . implode(', ', $select) . " ";
337 FROM civicrm_relationship {$this->_aliases['civicrm_relationship']}
339 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
340 ON ( {$this->_aliases['civicrm_relationship']}.contact_id_a =
341 {$this->_aliases['civicrm_contact']}.id )
343 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact_b']}
344 ON ( {$this->_aliases['civicrm_relationship']}.contact_id_b =
345 {$this->_aliases['civicrm_contact_b']}.id )
349 if (!empty($this->_params
['country_id_value']) ||
350 !empty($this->_params
['state_province_id_value'])
353 INNER JOIN civicrm_address {$this->_aliases['civicrm_address']}
354 ON (( {$this->_aliases['civicrm_address']}.contact_id =
355 {$this->_aliases['civicrm_contact']}.id OR
356 {$this->_aliases['civicrm_address']}.contact_id =
357 {$this->_aliases['civicrm_contact_b']}.id ) AND
358 {$this->_aliases['civicrm_address']}.is_primary = 1 ) ";
362 INNER JOIN civicrm_relationship_type {$this->_aliases['civicrm_relationship_type']}
363 ON ( {$this->_aliases['civicrm_relationship']}.relationship_type_id =
364 {$this->_aliases['civicrm_relationship_type']}.id ) ";
366 // include Email Field
367 if ($this->_emailField_a
) {
369 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
370 ON ( {$this->_aliases['civicrm_contact']}.id =
371 {$this->_aliases['civicrm_email']}.contact_id AND
372 {$this->_aliases['civicrm_email']}.is_primary = 1 )";
374 if ($this->_emailField_b
) {
376 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email_b']}
377 ON ( {$this->_aliases['civicrm_contact_b']}.id =
378 {$this->_aliases['civicrm_email_b']}.contact_id AND
379 {$this->_aliases['civicrm_email_b']}.is_primary = 1 )";
381 // include Phone Field
382 if ($this->_phoneField_a
) {
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 )";
389 if ($this->_phoneField_b
) {
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 )";
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) {
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
);
410 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
413 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
416 if ($tableName == 'civicrm_relationship_type' &&
417 ($fieldName == 'contact_type_a' ||
$fieldName == 'contact_type_b')
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];
427 elseif ($getTypes[0] && !in_array($getTypes[0], $contactTypes)) {
428 $contactTypes[] = $getTypes[0];
433 if (!empty($contactTypes)) {
434 $clause = $this->whereClause($field,
437 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
438 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
442 if (!empty($contactSubTypes)) {
443 if ($fieldName == 'contact_type_a') {
444 $field['name'] = 'contact_sub_type_a';
447 $field['name'] = 'contact_sub_type_b';
449 $field['dbAlias'] = $field['alias'] . '.' . $field['name'];
450 $subTypeClause = $this->whereClause($field,
453 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
454 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
457 $clause = '(' . $clause . ' OR ' . $subTypeClause . ')';
460 $clause = $subTypeClause;
466 $clause = $this->whereClause($field,
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
)
476 if (!empty($clause)) {
477 if (!empty($field['having'])) {
478 $havingClauses[] = $clause;
481 $whereClauses[] = $clause;
488 if (empty($whereClauses)) {
489 $this->_where
= 'WHERE ( 1 ) ';
493 $this->_where
= 'WHERE ' . implode(' AND ', $whereClauses);
496 if ($this->_aclWhere
) {
497 $this->_where
.= " AND {$this->_aclWhere} ";
500 if (!empty($havingClauses)) {
501 // use this clause to construct group by clause.
502 $this->_having
= 'HAVING ' . implode(' AND ', $havingClauses);
506 function statistics(&$rows) {
507 $statistics = parent
::statistics($rows);
509 $isStatusFilter = FALSE;
511 if (CRM_Utils_Array
::value('is_active_value', $this->_params
) == '1') {
512 $relStatus = 'Is equal to Active';
514 elseif (CRM_Utils_Array
::value('is_active_value', $this->_params
) == '0') {
515 $relStatus = 'Is equal to Inactive';
517 if (!empty($statistics['filters'])) {
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
];
525 //for displaying relationship status
526 if ($value['title'] == 'Relationship Status') {
527 $isStatusFilter = TRUE;
528 $statistics['filters'][$id]['value'] = $relStatus;
532 //for displaying relationship status
533 if (!$isStatusFilter && $relStatus) {
534 $statistics['filters'][] = array(
535 'title' => 'Relationship Status',
536 'value' => $relStatus,
543 $this->_groupBy
= " ";
545 if ($this->relationType
== 'a_b') {
546 $groupBy[] = " {$this->_aliases['civicrm_contact']}.id";
548 elseif ($this->relationType
== 'b_a') {
549 $groupBy[] = " {$this->_aliases['civicrm_contact_b']}.id";
552 if (!empty($groupBy)) {
553 $this->_groupBy
= " GROUP BY " . implode(', ', $groupBy) . " , {$this->_aliases['civicrm_relationship']}.id ";
556 $this->_groupBy
= " GROUP BY {$this->_aliases['civicrm_relationship']}.id ";
561 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact_b']}.sort_name ";
564 function postProcess() {
565 $this->beginPostProcess();
567 $this->relationType
= NULL;
569 if (!empty($this->_params
['relationship_type_id_value'])) {
570 $relType = explode('_', $this->_params
['relationship_type_id_value']);
572 $this->relationType
= $relType[1] . '_' . $relType[2];
573 $this->_params
['relationship_type_id_value'] = intval($relType[0]);
576 $this->buildACLClause(array($this->_aliases
['civicrm_contact'], $this->_aliases
['civicrm_contact_b']));
577 $sql = $this->buildQuery();
578 $this->buildRows($sql, $rows);
580 $this->formatDisplay($rows);
581 $this->doTemplateAssignment($rows);
583 if (!empty($relType)) {
584 // store its old value, CRM-5837
585 $this->_params
['relationship_type_id_value'] = implode('_', $relType);
587 $this->endPostProcess($rows);
590 function alterDisplay(&$rows) {
591 // custom code to alter rows
594 foreach ($rows as $rowNum => $row) {
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);
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);
611 if (array_key_exists('civicrm_contact_sort_name_a', $row) &&
612 array_key_exists('civicrm_contact_id', $row)
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
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.");
623 if (array_key_exists('civicrm_contact_b_sort_name_b', $row) &&
624 array_key_exists('civicrm_contact_b_id', $row)
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
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.");
635 // skip looking further in rows, if first row itself doesn't
636 // have the column we need