4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.4 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 $_customGroupExtends = array(
43 public $_drilldownReport = array('contact/detail' => 'Link to Detail Report');
45 function __construct() {
47 $contact_type = CRM_Contact_BAO_ContactType
::getSelectElements(FALSE, TRUE, '_');
49 $this->_columns
= array(
52 'dao' => 'CRM_Contact_DAO_Contact',
56 array('title' => ts('Contact A'),
57 'name' => 'sort_name',
67 'title' => ts('Contact Type (Contact A)'),
68 'name' => 'contact_type',
70 'contact_sub_type_a' =>
72 'title' => ts('Contact SubType (Contact A)'),
73 'name' => 'contact_sub_type',
79 array('title' => ts('Contact A'),
80 'name' => 'sort_name',
82 'type' => CRM_Report_Form
::OP_STRING
,
85 'grouping' => 'conact_a_fields',
87 'civicrm_contact_b' =>
89 'dao' => 'CRM_Contact_DAO_Contact',
90 'alias' => 'contact_b',
94 array('title' => ts('Contact B'),
95 'name' => 'sort_name',
100 'no_display' => TRUE,
105 'title' => ts('Contact Type (Contact B)'),
106 'name' => 'contact_type',
108 'contact_sub_type_b' =>
110 'title' => ts('Contact SubType (Contact B)'),
111 'name' => 'contact_sub_type',
117 array('title' => ts('Contact B'),
118 'name' => 'sort_name',
119 'operator' => 'like',
120 'type' => CRM_Report_Form
::OP_STRING
,
123 'grouping' => 'conact_b_fields',
127 'dao' => 'CRM_Core_DAO_Email',
131 array('title' => ts('Email of Contact A'),
135 'grouping' => 'conact_a_fields',
139 'dao' => 'CRM_Core_DAO_Email',
140 'alias' => 'email_b',
144 array('title' => ts('Email of Contact B'),
148 'grouping' => 'conact_b_fields',
150 'civicrm_relationship_type' =>
152 'dao' => 'CRM_Contact_DAO_RelationshipType',
156 array('title' => ts('Relationship A-B '),
160 array('title' => ts('Relationship B-A '),
167 array('title' => ts('Contact Type A'),
168 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
169 'options' => $contact_type,
170 'type' => CRM_Utils_Type
::T_STRING
,
173 array('title' => ts('Contact Type B'),
174 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
175 'options' => $contact_type,
176 'type' => CRM_Utils_Type
::T_STRING
,
179 'grouping' => 'relation-fields',
181 'civicrm_relationship' =>
183 'dao' => 'CRM_Contact_DAO_Relationship',
187 array('title' => ts('Relationship Start Date'),
190 array('title' => ts('Relationship End Date'),
193 array('title' => ts('Description'),
199 array('title' => ts('Relationship Status'),
200 'operatorType' => CRM_Report_Form
::OP_SELECT
,
207 'type' => CRM_Utils_Type
::T_INT
,
209 'relationship_type_id' =>
210 array('title' => ts('Relationship'),
211 'operatorType' => CRM_Report_Form
::OP_SELECT
,
214 '' => '- any relationship type -') +
215 CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, 'null', NULL, NULL, TRUE),
216 'type' => CRM_Utils_Type
::T_INT
,
219 'grouping' => 'relation-fields',
223 'dao' => 'CRM_Core_DAO_Address',
227 array('title' => ts('Country'),
228 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
229 'options' => CRM_Core_PseudoConstant
::country(),
231 'state_province_id' =>
232 array('title' => ts('State/Province'),
233 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
234 'options' => CRM_Core_PseudoConstant
::stateProvince(),
237 'grouping' => 'contact-fields',
241 'dao' => 'CRM_Contact_DAO_Group',
247 'name' => 'group_id',
248 'title' => ts('Group'),
249 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
251 'options' => CRM_Core_PseudoConstant
::group(),
257 $this->_tagFilter
= TRUE;
258 parent
::__construct();
261 function preProcess() {
262 parent
::preProcess();
266 $select = $this->_columnHeaders
= array();
267 foreach ($this->_columns
as $tableName => $table) {
268 if (array_key_exists('fields', $table)) {
269 foreach ($table['fields'] as $fieldName => $field) {
270 if (CRM_Utils_Array
::value('required', $field) ||
271 CRM_Utils_Array
::value($fieldName, $this->_params
['fields'])
274 if ($fieldName == 'email_a') {
275 $this->_emailField_a
= TRUE;
277 if ($fieldName == 'email_b') {
278 $this->_emailField_b
= TRUE;
280 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
281 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
282 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
288 $this->_select
= "SELECT " . implode(', ', $select) . " ";
293 FROM civicrm_relationship {$this->_aliases['civicrm_relationship']}
295 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
296 ON ( {$this->_aliases['civicrm_relationship']}.contact_id_a =
297 {$this->_aliases['civicrm_contact']}.id )
299 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact_b']}
300 ON ( {$this->_aliases['civicrm_relationship']}.contact_id_b =
301 {$this->_aliases['civicrm_contact_b']}.id )
305 if (!empty($this->_params
['country_id_value']) ||
306 !empty($this->_params
['state_province_id_value'])
309 INNER JOIN civicrm_address {$this->_aliases['civicrm_address']}
310 ON (( {$this->_aliases['civicrm_address']}.contact_id =
311 {$this->_aliases['civicrm_contact']}.id OR
312 {$this->_aliases['civicrm_address']}.contact_id =
313 {$this->_aliases['civicrm_contact_b']}.id ) AND
314 {$this->_aliases['civicrm_address']}.is_primary = 1 ) ";
318 INNER JOIN civicrm_relationship_type {$this->_aliases['civicrm_relationship_type']}
319 ON ( {$this->_aliases['civicrm_relationship']}.relationship_type_id =
320 {$this->_aliases['civicrm_relationship_type']}.id ) ";
322 // include Email Field
323 if ($this->_emailField_a
) {
325 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
326 ON ( {$this->_aliases['civicrm_contact']}.id =
327 {$this->_aliases['civicrm_email']}.contact_id AND
328 {$this->_aliases['civicrm_email']}.is_primary = 1 )";
330 if ($this->_emailField_b
) {
332 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email_b']}
333 ON ( {$this->_aliases['civicrm_contact_b']}.id =
334 {$this->_aliases['civicrm_email_b']}.contact_id AND
335 {$this->_aliases['civicrm_email_b']}.is_primary = 1 )";
340 $whereClauses = $havingClauses = array();
341 foreach ($this->_columns
as $tableName => $table) {
342 if (array_key_exists('filters', $table)) {
343 foreach ($table['filters'] as $fieldName => $field) {
346 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
347 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
348 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
349 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
351 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
354 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
357 if ($tableName == 'civicrm_relationship_type' &&
358 ($fieldName == 'contact_type_a' ||
$fieldName == 'contact_type_b')
360 $cTypes = CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
);
361 $contactTypes = $contactSubTypes = array();
362 if (!empty($cTypes)) {
363 foreach ($cTypes as $ctype) {
364 $getTypes = CRM_Utils_System
::explode('_', $ctype, 2);
365 if ($getTypes[1] && !in_array($getTypes[1], $contactSubTypes)) {
366 $contactSubTypes[] = $getTypes[1];
368 elseif ($getTypes[0] && !in_array($getTypes[0], $contactTypes)) {
369 $contactTypes[] = $getTypes[0];
374 if (!empty($contactTypes)) {
375 $clause = $this->whereClause($field,
378 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
379 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
383 if (!empty($contactSubTypes)) {
384 if ($fieldName == 'contact_type_a') {
385 $field['name'] = 'contact_sub_type_a';
388 $field['name'] = 'contact_sub_type_b';
390 $field['dbAlias'] = $field['alias'] . '.' . $field['name'];
391 $subTypeClause = $this->whereClause($field,
394 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
395 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
398 $clause = '(' . $clause . ' OR ' . $subTypeClause . ')';
401 $clause = $subTypeClause;
407 $clause = $this->whereClause($field,
409 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
410 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
411 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
417 if (!empty($clause)) {
418 if (CRM_Utils_Array
::value('having', $field)) {
419 $havingClauses[] = $clause;
422 $whereClauses[] = $clause;
429 if (empty($whereClauses)) {
430 $this->_where
= 'WHERE ( 1 ) ';
434 $this->_where
= 'WHERE ' . implode(' AND ', $whereClauses);
437 if ($this->_aclWhere
) {
438 $this->_where
.= " AND {$this->_aclWhere} ";
441 if (!empty($havingClauses)) {
442 // use this clause to construct group by clause.
443 $this->_having
= 'HAVING ' . implode(' AND ', $havingClauses);
447 function statistics(&$rows) {
448 $statistics = parent
::statistics($rows);
450 $isStatusFilter = FALSE;
452 if (CRM_Utils_Array
::value('is_active_value', $this->_params
) == '1') {
453 $relStatus = 'Is equal to Active';
455 elseif (CRM_Utils_Array
::value('is_active_value', $this->_params
) == '0') {
456 $relStatus = 'Is equal to Inactive';
458 if (CRM_Utils_Array
::value('filters', $statistics)) {
459 foreach ($statistics['filters'] as $id => $value) {
460 //for displaying relationship type filter
461 if ($value['title'] == 'Relationship') {
462 $relTypes = CRM_Core_PseudoConstant
::relationshipType();
463 $statistics['filters'][$id]['value'] = 'Is equal to ' . $relTypes[$this->_params
['relationship_type_id_value']]['label_' . $this->relationType
];
466 //for displaying relationship status
467 if ($value['title'] == 'Relationship Status') {
468 $isStatusFilter = TRUE;
469 $statistics['filters'][$id]['value'] = $relStatus;
473 //for displaying relationship status
474 if (!$isStatusFilter && $relStatus) {
475 $statistics['filters'][] = array(
476 'title' => 'Relationship Status',
477 'value' => $relStatus,
484 $this->_groupBy
= " ";
486 if ($this->relationType
== 'a_b') {
487 $groupBy[] = " {$this->_aliases['civicrm_contact']}.id";
489 elseif ($this->relationType
== 'b_a') {
490 $groupBy[] = " {$this->_aliases['civicrm_contact_b']}.id";
493 if (!empty($groupBy)) {
494 $this->_groupBy
= " GROUP BY " . implode(', ', $groupBy) . " , {$this->_aliases['civicrm_relationship']}.id ";
497 $this->_groupBy
= " GROUP BY {$this->_aliases['civicrm_relationship']}.id ";
502 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact_b']}.sort_name ";
505 function postProcess() {
506 $this->beginPostProcess();
508 $this->relationType
= NULL;
510 if (CRM_Utils_Array
::value('relationship_type_id_value', $this->_params
)) {
511 $relType = explode('_', $this->_params
['relationship_type_id_value']);
513 $this->relationType
= $relType[1] . '_' . $relType[2];
514 $this->_params
['relationship_type_id_value'] = intval($relType[0]);
517 $this->buildACLClause(array($this->_aliases
['civicrm_contact'], $this->_aliases
['civicrm_contact_b']));
518 $sql = $this->buildQuery();
519 $this->buildRows($sql, $rows);
521 $this->formatDisplay($rows);
522 $this->doTemplateAssignment($rows);
524 if (!empty($relType)) {
525 // store its old value, CRM-5837
526 $this->_params
['relationship_type_id_value'] = implode('_', $relType);
528 $this->endPostProcess($rows);
531 function alterDisplay(&$rows) {
532 // custom code to alter rows
535 foreach ($rows as $rowNum => $row) {
538 if (array_key_exists('civicrm_address_country_id', $row)) {
539 if ($value = $row['civicrm_address_country_id']) {
540 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
545 if (array_key_exists('civicrm_address_state_province_id', $row)) {
546 if ($value = $row['civicrm_address_state_province_id']) {
547 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
552 if (array_key_exists('civicrm_contact_sort_name_a', $row) &&
553 array_key_exists('civicrm_contact_id', $row)
555 $url = CRM_Report_Utils_Report
::getNextUrl('contact/detail',
556 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
557 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
559 $rows[$rowNum]['civicrm_contact_sort_name_a_link'] = $url;
560 $rows[$rowNum]['civicrm_contact_sort_name_a_hover'] = ts("View Contact details for this contact.");
564 if (array_key_exists('civicrm_contact_b_sort_name_b', $row) &&
565 array_key_exists('civicrm_contact_b_id', $row)
567 $url = CRM_Report_Utils_Report
::getNextUrl('contact/detail',
568 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_b_id'],
569 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
571 $rows[$rowNum]['civicrm_contact_b_sort_name_b_link'] = $url;
572 $rows[$rowNum]['civicrm_contact_b_sort_name_b_hover'] = ts("View Contact details for this contact.");
576 // skip looking further in rows, if first row itself doesn't
577 // have the column we need