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 $_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 (!empty($field['required']) ||
!empty($this->_params
['fields'][$fieldName])) {
272 if ($fieldName == 'email_a') {
273 $this->_emailField_a
= TRUE;
275 if ($fieldName == 'email_b') {
276 $this->_emailField_b
= TRUE;
278 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
279 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
280 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
286 $this->_select
= "SELECT " . implode(', ', $select) . " ";
291 FROM civicrm_relationship {$this->_aliases['civicrm_relationship']}
293 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
294 ON ( {$this->_aliases['civicrm_relationship']}.contact_id_a =
295 {$this->_aliases['civicrm_contact']}.id )
297 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact_b']}
298 ON ( {$this->_aliases['civicrm_relationship']}.contact_id_b =
299 {$this->_aliases['civicrm_contact_b']}.id )
303 if (!empty($this->_params
['country_id_value']) ||
304 !empty($this->_params
['state_province_id_value'])
307 INNER JOIN civicrm_address {$this->_aliases['civicrm_address']}
308 ON (( {$this->_aliases['civicrm_address']}.contact_id =
309 {$this->_aliases['civicrm_contact']}.id OR
310 {$this->_aliases['civicrm_address']}.contact_id =
311 {$this->_aliases['civicrm_contact_b']}.id ) AND
312 {$this->_aliases['civicrm_address']}.is_primary = 1 ) ";
316 INNER JOIN civicrm_relationship_type {$this->_aliases['civicrm_relationship_type']}
317 ON ( {$this->_aliases['civicrm_relationship']}.relationship_type_id =
318 {$this->_aliases['civicrm_relationship_type']}.id ) ";
320 // include Email Field
321 if ($this->_emailField_a
) {
323 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
324 ON ( {$this->_aliases['civicrm_contact']}.id =
325 {$this->_aliases['civicrm_email']}.contact_id AND
326 {$this->_aliases['civicrm_email']}.is_primary = 1 )";
328 if ($this->_emailField_b
) {
330 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email_b']}
331 ON ( {$this->_aliases['civicrm_contact_b']}.id =
332 {$this->_aliases['civicrm_email_b']}.contact_id AND
333 {$this->_aliases['civicrm_email_b']}.is_primary = 1 )";
338 $whereClauses = $havingClauses = array();
339 foreach ($this->_columns
as $tableName => $table) {
340 if (array_key_exists('filters', $table)) {
341 foreach ($table['filters'] as $fieldName => $field) {
344 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
345 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
346 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
347 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
349 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
352 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
355 if ($tableName == 'civicrm_relationship_type' &&
356 ($fieldName == 'contact_type_a' ||
$fieldName == 'contact_type_b')
358 $cTypes = CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
);
359 $contactTypes = $contactSubTypes = array();
360 if (!empty($cTypes)) {
361 foreach ($cTypes as $ctype) {
362 $getTypes = CRM_Utils_System
::explode('_', $ctype, 2);
363 if ($getTypes[1] && !in_array($getTypes[1], $contactSubTypes)) {
364 $contactSubTypes[] = $getTypes[1];
366 elseif ($getTypes[0] && !in_array($getTypes[0], $contactTypes)) {
367 $contactTypes[] = $getTypes[0];
372 if (!empty($contactTypes)) {
373 $clause = $this->whereClause($field,
376 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
377 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
381 if (!empty($contactSubTypes)) {
382 if ($fieldName == 'contact_type_a') {
383 $field['name'] = 'contact_sub_type_a';
386 $field['name'] = 'contact_sub_type_b';
388 $field['dbAlias'] = $field['alias'] . '.' . $field['name'];
389 $subTypeClause = $this->whereClause($field,
392 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
393 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
396 $clause = '(' . $clause . ' OR ' . $subTypeClause . ')';
399 $clause = $subTypeClause;
405 $clause = $this->whereClause($field,
407 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
408 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
409 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
415 if (!empty($clause)) {
416 if (!empty($field['having'])) {
417 $havingClauses[] = $clause;
420 $whereClauses[] = $clause;
427 if (empty($whereClauses)) {
428 $this->_where
= 'WHERE ( 1 ) ';
432 $this->_where
= 'WHERE ' . implode(' AND ', $whereClauses);
435 if ($this->_aclWhere
) {
436 $this->_where
.= " AND {$this->_aclWhere} ";
439 if (!empty($havingClauses)) {
440 // use this clause to construct group by clause.
441 $this->_having
= 'HAVING ' . implode(' AND ', $havingClauses);
445 function statistics(&$rows) {
446 $statistics = parent
::statistics($rows);
448 $isStatusFilter = FALSE;
450 if (CRM_Utils_Array
::value('is_active_value', $this->_params
) == '1') {
451 $relStatus = 'Is equal to Active';
453 elseif (CRM_Utils_Array
::value('is_active_value', $this->_params
) == '0') {
454 $relStatus = 'Is equal to Inactive';
456 if (!empty($statistics['filters'])) {
457 foreach ($statistics['filters'] as $id => $value) {
458 //for displaying relationship type filter
459 if ($value['title'] == 'Relationship') {
460 $relTypes = CRM_Core_PseudoConstant
::relationshipType();
461 $statistics['filters'][$id]['value'] = 'Is equal to ' . $relTypes[$this->_params
['relationship_type_id_value']]['label_' . $this->relationType
];
464 //for displaying relationship status
465 if ($value['title'] == 'Relationship Status') {
466 $isStatusFilter = TRUE;
467 $statistics['filters'][$id]['value'] = $relStatus;
471 //for displaying relationship status
472 if (!$isStatusFilter && $relStatus) {
473 $statistics['filters'][] = array(
474 'title' => 'Relationship Status',
475 'value' => $relStatus,
482 $this->_groupBy
= " ";
484 if ($this->relationType
== 'a_b') {
485 $groupBy[] = " {$this->_aliases['civicrm_contact']}.id";
487 elseif ($this->relationType
== 'b_a') {
488 $groupBy[] = " {$this->_aliases['civicrm_contact_b']}.id";
491 if (!empty($groupBy)) {
492 $this->_groupBy
= " GROUP BY " . implode(', ', $groupBy) . " , {$this->_aliases['civicrm_relationship']}.id ";
495 $this->_groupBy
= " GROUP BY {$this->_aliases['civicrm_relationship']}.id ";
500 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact_b']}.sort_name ";
503 function postProcess() {
504 $this->beginPostProcess();
506 $this->relationType
= NULL;
508 if (!empty($this->_params
['relationship_type_id_value'])) {
509 $relType = explode('_', $this->_params
['relationship_type_id_value']);
511 $this->relationType
= $relType[1] . '_' . $relType[2];
512 $this->_params
['relationship_type_id_value'] = intval($relType[0]);
515 $this->buildACLClause(array($this->_aliases
['civicrm_contact'], $this->_aliases
['civicrm_contact_b']));
516 $sql = $this->buildQuery();
517 $this->buildRows($sql, $rows);
519 $this->formatDisplay($rows);
520 $this->doTemplateAssignment($rows);
522 if (!empty($relType)) {
523 // store its old value, CRM-5837
524 $this->_params
['relationship_type_id_value'] = implode('_', $relType);
526 $this->endPostProcess($rows);
529 function alterDisplay(&$rows) {
530 // custom code to alter rows
533 foreach ($rows as $rowNum => $row) {
536 if (array_key_exists('civicrm_address_country_id', $row)) {
537 if ($value = $row['civicrm_address_country_id']) {
538 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
543 if (array_key_exists('civicrm_address_state_province_id', $row)) {
544 if ($value = $row['civicrm_address_state_province_id']) {
545 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
550 if (array_key_exists('civicrm_contact_sort_name_a', $row) &&
551 array_key_exists('civicrm_contact_id', $row)
553 $url = CRM_Report_Utils_Report
::getNextUrl('contact/detail',
554 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
555 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
557 $rows[$rowNum]['civicrm_contact_sort_name_a_link'] = $url;
558 $rows[$rowNum]['civicrm_contact_sort_name_a_hover'] = ts("View Contact details for this contact.");
562 if (array_key_exists('civicrm_contact_b_sort_name_b', $row) &&
563 array_key_exists('civicrm_contact_b_id', $row)
565 $url = CRM_Report_Utils_Report
::getNextUrl('contact/detail',
566 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_b_id'],
567 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
569 $rows[$rowNum]['civicrm_contact_b_sort_name_b_link'] = $url;
570 $rows[$rowNum]['civicrm_contact_b_sort_name_b_hover'] = ts("View Contact details for this contact.");
574 // skip looking further in rows, if first row itself doesn't
575 // have the column we need