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 (Contact A)'),
137 'grouping' => 'conact_a_fields',
141 'dao' => 'CRM_Core_DAO_Email',
142 'alias' => 'email_b',
146 array('title' => ts('Email (Contact B)'),
150 'grouping' => 'conact_b_fields',
154 'dao' => 'CRM_Core_DAO_Phone',
155 'alias' => 'phone_a',
160 'title' => ts('Phone (Contact A)'),
165 'title' => ts('Phone Ext (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 (Contact B)'),
184 'title' => ts('Phone Ext (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'),
237 'title' => ts('Rel ID'),
244 array('title' => ts('Relationship Status'),
245 'operatorType' => CRM_Report_Form
::OP_SELECT
,
252 'type' => CRM_Utils_Type
::T_INT
,
254 'relationship_type_id' =>
255 array('title' => ts('Relationship'),
256 'operatorType' => CRM_Report_Form
::OP_SELECT
,
259 '' => '- any relationship type -') +
260 CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, 'null', NULL, NULL, TRUE),
261 'type' => CRM_Utils_Type
::T_INT
,
264 'grouping' => 'relation-fields',
268 'dao' => 'CRM_Core_DAO_Address',
272 array('title' => ts('Country'),
273 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
274 'options' => CRM_Core_PseudoConstant
::country(),
276 'state_province_id' =>
277 array('title' => ts('State/Province'),
278 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
279 'options' => CRM_Core_PseudoConstant
::stateProvince(),
282 'grouping' => 'contact-fields',
286 'dao' => 'CRM_Contact_DAO_Group',
292 'name' => 'group_id',
293 'title' => ts('Group'),
294 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
296 'options' => CRM_Core_PseudoConstant
::group(),
302 $this->_tagFilter
= TRUE;
303 parent
::__construct();
306 function preProcess() {
307 parent
::preProcess();
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])) {
317 if ($fieldName == 'email_a') {
318 $this->_emailField_a
= TRUE;
320 if ($fieldName == 'email_b') {
321 $this->_emailField_b
= TRUE;
323 if ($fieldName == 'phone_a'){
324 $this->_phoneField_a
= TRUE;
326 if ($fieldName == 'phone_b'){
327 $this->_phoneField_b
= TRUE;
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);
337 $this->_select
= "SELECT " . implode(', ', $select) . " ";
342 FROM civicrm_relationship {$this->_aliases['civicrm_relationship']}
344 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
345 ON ( {$this->_aliases['civicrm_relationship']}.contact_id_a =
346 {$this->_aliases['civicrm_contact']}.id )
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 )
354 if (!empty($this->_params
['country_id_value']) ||
355 !empty($this->_params
['state_province_id_value'])
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 ) ";
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 ) ";
371 // include Email Field
372 if ($this->_emailField_a
) {
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 )";
379 if ($this->_emailField_b
) {
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 )";
386 // include Phone Field
387 if ($this->_phoneField_a
) {
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 )";
394 if ($this->_phoneField_b
) {
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 )";
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) {
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
);
415 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
418 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
421 if ($tableName == 'civicrm_relationship_type' &&
422 ($fieldName == 'contact_type_a' ||
$fieldName == 'contact_type_b')
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];
432 elseif ($getTypes[0] && !in_array($getTypes[0], $contactTypes)) {
433 $contactTypes[] = $getTypes[0];
438 if (!empty($contactTypes)) {
439 $clause = $this->whereClause($field,
442 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
443 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
447 if (!empty($contactSubTypes)) {
448 if ($fieldName == 'contact_type_a') {
449 $field['name'] = 'contact_sub_type_a';
452 $field['name'] = 'contact_sub_type_b';
454 $field['dbAlias'] = $field['alias'] . '.' . $field['name'];
455 $subTypeClause = $this->whereClause($field,
458 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
459 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
462 $clause = '(' . $clause . ' OR ' . $subTypeClause . ')';
465 $clause = $subTypeClause;
471 $clause = $this->whereClause($field,
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
)
481 if (!empty($clause)) {
482 if (!empty($field['having'])) {
483 $havingClauses[] = $clause;
486 $whereClauses[] = $clause;
493 if (empty($whereClauses)) {
494 $this->_where
= 'WHERE ( 1 ) ';
498 $this->_where
= 'WHERE ' . implode(' AND ', $whereClauses);
501 if ($this->_aclWhere
) {
502 $this->_where
.= " AND {$this->_aclWhere} ";
505 if (!empty($havingClauses)) {
506 // use this clause to construct group by clause.
507 $this->_having
= 'HAVING ' . implode(' AND ', $havingClauses);
511 function statistics(&$rows) {
512 $statistics = parent
::statistics($rows);
514 $isStatusFilter = FALSE;
516 if (CRM_Utils_Array
::value('is_active_value', $this->_params
) == '1') {
517 $relStatus = 'Is equal to Active';
519 elseif (CRM_Utils_Array
::value('is_active_value', $this->_params
) == '0') {
520 $relStatus = 'Is equal to Inactive';
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
];
530 //for displaying relationship status
531 if ($value['title'] == 'Relationship Status') {
532 $isStatusFilter = TRUE;
533 $statistics['filters'][$id]['value'] = $relStatus;
537 //for displaying relationship status
538 if (!$isStatusFilter && $relStatus) {
539 $statistics['filters'][] = array(
540 'title' => 'Relationship Status',
541 'value' => $relStatus,
548 $this->_groupBy
= " ";
550 if ($this->relationType
== 'a_b') {
551 $groupBy[] = " {$this->_aliases['civicrm_contact']}.id";
553 elseif ($this->relationType
== 'b_a') {
554 $groupBy[] = " {$this->_aliases['civicrm_contact_b']}.id";
557 if (!empty($groupBy)) {
558 $this->_groupBy
= " GROUP BY " . implode(', ', $groupBy) . " , {$this->_aliases['civicrm_relationship']}.id ";
561 $this->_groupBy
= " GROUP BY {$this->_aliases['civicrm_relationship']}.id ";
566 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact_b']}.sort_name ";
569 function postProcess() {
570 $this->beginPostProcess();
572 $this->relationType
= NULL;
574 if (!empty($this->_params
['relationship_type_id_value'])) {
575 $relType = explode('_', $this->_params
['relationship_type_id_value']);
577 $this->relationType
= $relType[1] . '_' . $relType[2];
578 $this->_params
['relationship_type_id_value'] = intval($relType[0]);
581 $this->buildACLClause(array($this->_aliases
['civicrm_contact'], $this->_aliases
['civicrm_contact_b']));
582 $sql = $this->buildQuery();
583 $this->buildRows($sql, $rows);
585 $this->formatDisplay($rows);
586 $this->doTemplateAssignment($rows);
588 if (!empty($relType)) {
589 // store its old value, CRM-5837
590 $this->_params
['relationship_type_id_value'] = implode('_', $relType);
592 $this->endPostProcess($rows);
595 function alterDisplay(&$rows) {
596 // custom code to alter rows
599 foreach ($rows as $rowNum => $row) {
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);
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);
616 if (array_key_exists('civicrm_contact_sort_name_a', $row) &&
617 array_key_exists('civicrm_contact_id', $row)
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
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.");
629 if (array_key_exists('civicrm_contact_b_sort_name_b', $row) &&
630 array_key_exists('civicrm_contact_b_id', $row)
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
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.");
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.");
649 // skip looking further in rows, if first row itself doesn't
650 // have the column we need