4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
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',
69 array('title' => ts('Contact A'),
70 'name' => 'sort_name',
72 'type' => CRM_Report_Form
::OP_STRING
,
75 'grouping' => 'conact_a_fields',
77 'civicrm_contact_b' =>
79 'dao' => 'CRM_Contact_DAO_Contact',
80 'alias' => 'contact_b',
84 array('title' => ts('Contact B'),
85 'name' => 'sort_name',
97 array('title' => ts('Contact B'),
98 'name' => 'sort_name',
100 'type' => CRM_Report_Form
::OP_STRING
,
103 'grouping' => 'conact_b_fields',
107 'dao' => 'CRM_Core_DAO_Email',
111 array('title' => ts('Email of Contact A'),
115 'grouping' => 'conact_a_fields',
119 'dao' => 'CRM_Core_DAO_Email',
120 'alias' => 'email_b',
124 array('title' => ts('Email of Contact B'),
128 'grouping' => 'conact_b_fields',
130 'civicrm_relationship_type' =>
132 'dao' => 'CRM_Contact_DAO_RelationshipType',
136 array('title' => ts('Relationship A-B '),
140 array('title' => ts('Relationship B-A '),
147 array('title' => ts('Contact Type A'),
148 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
149 'options' => $contact_type,
150 'type' => CRM_Utils_Type
::T_STRING
,
153 array('title' => ts('Contact Type B'),
154 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
155 'options' => $contact_type,
156 'type' => CRM_Utils_Type
::T_STRING
,
159 'grouping' => 'relation-fields',
161 'civicrm_relationship' =>
163 'dao' => 'CRM_Contact_DAO_Relationship',
167 array('title' => ts('Relationship Start Date'),
170 array('title' => ts('Relationship End Date'),
173 array('title' => ts('Description'),
179 array('title' => ts('Relationship Status'),
180 'operatorType' => CRM_Report_Form
::OP_SELECT
,
187 'type' => CRM_Utils_Type
::T_INT
,
189 'relationship_type_id' =>
190 array('title' => ts('Relationship'),
191 'operatorType' => CRM_Report_Form
::OP_SELECT
,
194 '' => '- any relationship type -') +
195 CRM_Contact_BAO_Relationship
::getContactRelationshipType(NULL, 'null', NULL, NULL, TRUE),
196 'type' => CRM_Utils_Type
::T_INT
,
199 'grouping' => 'relation-fields',
203 'dao' => 'CRM_Core_DAO_Address',
207 array('title' => ts('Country'),
208 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
209 'options' => CRM_Core_PseudoConstant
::country(),
211 'state_province_id' =>
212 array('title' => ts('State/Province'),
213 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
214 'options' => CRM_Core_PseudoConstant
::stateProvince(),
217 'grouping' => 'contact-fields',
221 'dao' => 'CRM_Contact_DAO_Group',
227 'name' => 'group_id',
228 'title' => ts('Group'),
229 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
231 'options' => CRM_Core_PseudoConstant
::group(),
237 $this->_tagFilter
= TRUE;
238 parent
::__construct();
241 function preProcess() {
242 parent
::preProcess();
246 $select = $this->_columnHeaders
= array();
247 foreach ($this->_columns
as $tableName => $table) {
248 if (array_key_exists('fields', $table)) {
249 foreach ($table['fields'] as $fieldName => $field) {
250 if (CRM_Utils_Array
::value('required', $field) ||
251 CRM_Utils_Array
::value($fieldName, $this->_params
['fields'])
254 if ($fieldName == 'email_a') {
255 $this->_emailField_a
= TRUE;
257 if ($fieldName == 'email_b') {
258 $this->_emailField_b
= TRUE;
260 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
261 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
262 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
268 $this->_select
= "SELECT " . implode(', ', $select) . " ";
273 FROM civicrm_relationship {$this->_aliases['civicrm_relationship']}
275 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
276 ON ( {$this->_aliases['civicrm_relationship']}.contact_id_a =
277 {$this->_aliases['civicrm_contact']}.id )
279 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact_b']}
280 ON ( {$this->_aliases['civicrm_relationship']}.contact_id_b =
281 {$this->_aliases['civicrm_contact_b']}.id )
285 if (!empty($this->_params
['country_id_value']) ||
286 !empty($this->_params
['state_province_id_value'])
289 INNER JOIN civicrm_address {$this->_aliases['civicrm_address']}
290 ON (( {$this->_aliases['civicrm_address']}.contact_id =
291 {$this->_aliases['civicrm_contact']}.id OR
292 {$this->_aliases['civicrm_address']}.contact_id =
293 {$this->_aliases['civicrm_contact_b']}.id ) AND
294 {$this->_aliases['civicrm_address']}.is_primary = 1 ) ";
298 INNER JOIN civicrm_relationship_type {$this->_aliases['civicrm_relationship_type']}
299 ON ( {$this->_aliases['civicrm_relationship']}.relationship_type_id =
300 {$this->_aliases['civicrm_relationship_type']}.id ) ";
302 // include Email Field
303 if ($this->_emailField_a
) {
305 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
306 ON ( {$this->_aliases['civicrm_contact']}.id =
307 {$this->_aliases['civicrm_email']}.contact_id AND
308 {$this->_aliases['civicrm_email']}.is_primary = 1 )";
310 if ($this->_emailField_b
) {
312 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email_b']}
313 ON ( {$this->_aliases['civicrm_contact_b']}.id =
314 {$this->_aliases['civicrm_email_b']}.contact_id AND
315 {$this->_aliases['civicrm_email_b']}.is_primary = 1 )";
320 $whereClauses = $havingClauses = array();
321 foreach ($this->_columns
as $tableName => $table) {
322 if (array_key_exists('filters', $table)) {
323 foreach ($table['filters'] as $fieldName => $field) {
326 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
327 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
328 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
329 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
331 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
334 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
337 if ($tableName == 'civicrm_relationship_type' &&
338 ($fieldName == 'contact_type_a' ||
$fieldName == 'contact_type_b')
340 $cTypes = CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
);
341 $contactTypes = $contactSubTypes = array();
342 if (!empty($cTypes)) {
343 foreach ($cTypes as $ctype) {
344 $getTypes = CRM_Utils_System
::explode('_', $ctype, 2);
345 if ($getTypes[1] && !in_array($getTypes[1], $contactSubTypes)) {
346 $contactSubTypes[] = $getTypes[1];
348 elseif ($getTypes[0] && !in_array($getTypes[0], $contactTypes)) {
349 $contactTypes[] = $getTypes[0];
354 if (!empty($contactTypes)) {
355 $clause = $this->whereClause($field,
358 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
359 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
363 if (!empty($contactSubTypes)) {
364 if ($fieldName == 'contact_type_a') {
365 $field['name'] = 'contact_sub_type_a';
368 $field['name'] = 'contact_sub_type_b';
370 $field['dbAlias'] = $field['alias'] . '.' . $field['name'];
371 $subTypeClause = $this->whereClause($field,
374 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
375 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
378 $clause = '(' . $clause . ' OR ' . $subTypeClause . ')';
381 $clause = $subTypeClause;
387 $clause = $this->whereClause($field,
389 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
390 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
391 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
397 if (!empty($clause)) {
398 if (CRM_Utils_Array
::value('having', $field)) {
399 $havingClauses[] = $clause;
402 $whereClauses[] = $clause;
409 if (empty($whereClauses)) {
410 $this->_where
= 'WHERE ( 1 ) ';
414 $this->_where
= 'WHERE ' . implode(' AND ', $whereClauses);
417 if ($this->_aclWhere
) {
418 $this->_where
.= " AND {$this->_aclWhere} ";
421 if (!empty($havingClauses)) {
422 // use this clause to construct group by clause.
423 $this->_having
= 'HAVING ' . implode(' AND ', $havingClauses);
427 function statistics(&$rows) {
428 $statistics = parent
::statistics($rows);
430 $isStatusFilter = FALSE;
432 if (CRM_Utils_Array
::value('is_active_value', $this->_params
) == '1') {
433 $relStatus = 'Is equal to Active';
435 elseif (CRM_Utils_Array
::value('is_active_value', $this->_params
) == '0') {
436 $relStatus = 'Is equal to Inactive';
438 if (CRM_Utils_Array
::value('filters', $statistics)) {
439 foreach ($statistics['filters'] as $id => $value) {
440 //for displaying relationship type filter
441 if ($value['title'] == 'Relationship') {
442 $relTypes = CRM_Core_PseudoConstant
::relationshipType();
443 $statistics['filters'][$id]['value'] = 'Is equal to ' . $relTypes[$this->_params
['relationship_type_id_value']]['label_' . $this->relationType
];
446 //for displaying relationship status
447 if ($value['title'] == 'Relationship Status') {
448 $isStatusFilter = TRUE;
449 $statistics['filters'][$id]['value'] = $relStatus;
453 //for displaying relationship status
454 if (!$isStatusFilter && $relStatus) {
455 $statistics['filters'][] = array(
456 'title' => 'Relationship Status',
457 'value' => $relStatus,
464 $this->_groupBy
= " ";
466 if ($this->relationType
== 'a_b') {
467 $groupBy[] = " {$this->_aliases['civicrm_contact']}.id";
469 elseif ($this->relationType
== 'b_a') {
470 $groupBy[] = " {$this->_aliases['civicrm_contact_b']}.id";
473 if (!empty($groupBy)) {
474 $this->_groupBy
= " GROUP BY " . implode(', ', $groupBy) . " , {$this->_aliases['civicrm_relationship']}.id ";
477 $this->_groupBy
= " GROUP BY {$this->_aliases['civicrm_relationship']}.id ";
482 $this->_orderBy
= " ORDER BY {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact_b']}.sort_name ";
485 function postProcess() {
486 $this->beginPostProcess();
488 $this->relationType
= NULL;
490 if (CRM_Utils_Array
::value('relationship_type_id_value', $this->_params
)) {
491 $relType = explode('_', $this->_params
['relationship_type_id_value']);
493 $this->relationType
= $relType[1] . '_' . $relType[2];
494 $this->_params
['relationship_type_id_value'] = intval($relType[0]);
497 $this->buildACLClause(array($this->_aliases
['civicrm_contact'], $this->_aliases
['civicrm_contact_b']));
498 $sql = $this->buildQuery();
499 $this->buildRows($sql, $rows);
501 $this->formatDisplay($rows);
502 $this->doTemplateAssignment($rows);
504 if (!empty($relType)) {
505 // store its old value, CRM-5837
506 $this->_params
['relationship_type_id_value'] = implode('_', $relType);
508 $this->endPostProcess($rows);
511 function alterDisplay(&$rows) {
512 // custom code to alter rows
515 foreach ($rows as $rowNum => $row) {
518 if (array_key_exists('civicrm_address_country_id', $row)) {
519 if ($value = $row['civicrm_address_country_id']) {
520 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
525 if (array_key_exists('civicrm_address_state_province_id', $row)) {
526 if ($value = $row['civicrm_address_state_province_id']) {
527 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
532 if (array_key_exists('civicrm_contact_sort_name_a', $row) &&
533 array_key_exists('civicrm_contact_id', $row)
535 $url = CRM_Report_Utils_Report
::getNextUrl('contact/detail',
536 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
537 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
539 $rows[$rowNum]['civicrm_contact_sort_name_a_link'] = $url;
540 $rows[$rowNum]['civicrm_contact_sort_name_a_hover'] = ts("View Contact details for this contact.");
544 if (array_key_exists('civicrm_contact_b_sort_name_b', $row) &&
545 array_key_exists('civicrm_contact_b_id', $row)
547 $url = CRM_Report_Utils_Report
::getNextUrl('contact/detail',
548 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_b_id'],
549 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
551 $rows[$rowNum]['civicrm_contact_b_sort_name_b_link'] = $url;
552 $rows[$rowNum]['civicrm_contact_b_sort_name_b_hover'] = ts("View Contact details for this contact.");
556 // skip looking further in rows, if first row itself doesn't
557 // have the column we need