Merge pull request #10466 from kryptothesuperdog/master
[civicrm-core.git] / CRM / Report / Form / Contact / Relationship.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
0f03f337 31 * @copyright CiviCRM LLC (c) 2004-2017
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Report_Form_Contact_Relationship extends CRM_Report_Form {
36
37 protected $_summary = NULL;
38 protected $_emailField_a = FALSE;
39 protected $_emailField_b = FALSE;
7393ba64
M
40 protected $_phoneField_a = FALSE;
41 protected $_phoneField_b = FALSE;
6a488035 42 protected $_customGroupExtends = array(
21dfd5f5 43 'Relationship',
9d72cede 44 );
6a488035 45 public $_drilldownReport = array('contact/detail' => 'Link to Detail Report');
2f4c2f5d 46
1728e9a0 47 /**
48 * This report has not been optimised for group filtering.
49 *
50 * The functionality for group filtering has been improved but not
51 * all reports have been adjusted to take care of it. This report has not
52 * and will run an inefficient query until fixed.
53 *
54 * CRM-19170
55 *
56 * @var bool
57 */
58 protected $groupFilterNotOptimised = TRUE;
59
55f71fa7 60 /**
61 * This will be a_b or b_a.
62 *
63 * @var string
64 */
65 protected $relationType;
66
70599df6 67 /**
68 * Class constructor.
69 */
00be9182 70 public function __construct() {
6a488035
TO
71
72 $contact_type = CRM_Contact_BAO_ContactType::getSelectElements(FALSE, TRUE, '_');
73
74 $this->_columns = array(
9d72cede 75 'civicrm_contact' => array(
6a488035 76 'dao' => 'CRM_Contact_DAO_Contact',
9d72cede
EM
77 'fields' => array(
78 'sort_name_a' => array(
79 'title' => ts('Contact A'),
6a488035
TO
80 'name' => 'sort_name',
81 'required' => TRUE,
82 ),
16e98ad0
SL
83 'display_name_a' => array(
84 'title' => ts('Contact A Full Name'),
85 'name' => 'display_name',
86 ),
9d72cede 87 'id' => array(
6a488035
TO
88 'no_display' => TRUE,
89 'required' => TRUE,
90 ),
9d72cede 91 'contact_type_a' => array(
30f85891
RN
92 'title' => ts('Contact Type (Contact A)'),
93 'name' => 'contact_type',
94 ),
9d72cede 95 'contact_sub_type_a' => array(
b8f96eb8 96 'title' => ts('Contact Subtype (Contact A)'),
30f85891
RN
97 'name' => 'contact_sub_type',
98 ),
6a488035 99 ),
9d72cede
EM
100 'filters' => array(
101 'sort_name_a' => array(
102 'title' => ts('Contact A'),
6a488035
TO
103 'name' => 'sort_name',
104 'operator' => 'like',
105 'type' => CRM_Report_Form::OP_STRING,
106 ),
9d72cede
EM
107 'contact_type_a' => array(
108 'title' => ts('Contact Type A'),
bc1f381d
BS
109 'name' => 'contact_type',
110 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
111 'options' => $contact_type,
112 'type' => CRM_Utils_Type::T_STRING,
113 ),
6a488035 114 ),
6f5155f0
BS
115 'order_bys' => array(
116 'sort_name_a' => array(
117 'title' => ts('Contact A'),
118 'name' => 'sort_name',
119 'default_weight' => '1',
120 ),
121 ),
70bea8e2 122 'grouping' => 'contact_a_fields',
6a488035 123 ),
9d72cede 124 'civicrm_contact_b' => array(
6a488035
TO
125 'dao' => 'CRM_Contact_DAO_Contact',
126 'alias' => 'contact_b',
9d72cede
EM
127 'fields' => array(
128 'sort_name_b' => array(
129 'title' => ts('Contact B'),
6a488035
TO
130 'name' => 'sort_name',
131 'required' => TRUE,
132 ),
16e98ad0
SL
133 'display_name_b' => array(
134 'title' => ts('Contact B Full Name'),
135 'name' => 'display_name',
136 ),
9d72cede 137 'id' => array(
6a488035
TO
138 'no_display' => TRUE,
139 'required' => TRUE,
140 ),
9d72cede 141 'contact_type_b' => array(
30f85891
RN
142 'title' => ts('Contact Type (Contact B)'),
143 'name' => 'contact_type',
144 ),
9d72cede 145 'contact_sub_type_b' => array(
b8f96eb8 146 'title' => ts('Contact Subtype (Contact B)'),
30f85891
RN
147 'name' => 'contact_sub_type',
148 ),
6a488035 149 ),
9d72cede
EM
150 'filters' => array(
151 'sort_name_b' => array(
152 'title' => ts('Contact B'),
6a488035
TO
153 'name' => 'sort_name',
154 'operator' => 'like',
155 'type' => CRM_Report_Form::OP_STRING,
156 ),
9d72cede
EM
157 'contact_type_b' => array(
158 'title' => ts('Contact Type B'),
bc1f381d
BS
159 'name' => 'contact_type',
160 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
161 'options' => $contact_type,
162 'type' => CRM_Utils_Type::T_STRING,
163 ),
6a488035 164 ),
6f5155f0
BS
165 'order_bys' => array(
166 'sort_name_b' => array(
167 'title' => ts('Contact B'),
168 'name' => 'sort_name',
169 'default_weight' => '2',
170 ),
171 ),
70bea8e2 172 'grouping' => 'contact_b_fields',
6a488035 173 ),
9d72cede 174 'civicrm_email' => array(
6a488035 175 'dao' => 'CRM_Core_DAO_Email',
9d72cede
EM
176 'fields' => array(
177 'email_a' => array(
178 'title' => ts('Email (Contact A)'),
6a488035
TO
179 'name' => 'email',
180 ),
181 ),
70bea8e2 182 'grouping' => 'contact_a_fields',
6a488035 183 ),
9d72cede 184 'civicrm_email_b' => array(
6a488035
TO
185 'dao' => 'CRM_Core_DAO_Email',
186 'alias' => 'email_b',
9d72cede
EM
187 'fields' => array(
188 'email_b' => array(
189 'title' => ts('Email (Contact B)'),
6a488035
TO
190 'name' => 'email',
191 ),
192 ),
70bea8e2 193 'grouping' => 'contact_b_fields',
6a488035 194 ),
9d72cede 195 'civicrm_phone' => array(
7393ba64
M
196 'dao' => 'CRM_Core_DAO_Phone',
197 'alias' => 'phone_a',
9d72cede
EM
198 'fields' => array(
199 'phone_a' => array(
a9315b5e 200 'title' => ts('Phone (Contact A)'),
7393ba64
M
201 'name' => 'phone',
202 ),
9d72cede 203 'phone_ext_a' => array(
a9315b5e 204 'title' => ts('Phone Ext (Contact A)'),
7393ba64
M
205 'name' => 'phone_ext',
206 ),
207 ),
70bea8e2 208 'grouping' => 'contact_a_fields',
7393ba64 209 ),
9d72cede 210 'civicrm_phone_b' => array(
7393ba64
M
211 'dao' => 'CRM_Core_DAO_Phone',
212 'alias' => 'phone_b',
9d72cede
EM
213 'fields' => array(
214 'phone_b' => array(
a9315b5e 215 'title' => ts('Phone (Contact B)'),
21dfd5f5 216 'name' => 'phone',
7393ba64 217 ),
9d72cede 218 'phone_ext_b' => array(
a9315b5e 219 'title' => ts('Phone Ext (Contact B)'),
21dfd5f5 220 'name' => 'phone_ext',
7393ba64
M
221 ),
222 ),
70bea8e2 223 'grouping' => 'contact_b_fields',
7393ba64 224 ),
9d72cede 225 'civicrm_relationship_type' => array(
6a488035 226 'dao' => 'CRM_Contact_DAO_RelationshipType',
9d72cede
EM
227 'fields' => array(
228 'label_a_b' => array(
229 'title' => ts('Relationship A-B '),
6a488035
TO
230 'default' => TRUE,
231 ),
9d72cede
EM
232 'label_b_a' => array(
233 'title' => ts('Relationship B-A '),
6a488035
TO
234 'default' => TRUE,
235 ),
236 ),
6f5155f0
BS
237 'order_bys' => array(
238 'label_a_b' => array(
239 'title' => ts('Relationship A-B'),
240 'name' => 'label_a_b',
241 ),
242 'label_b_A' => array(
243 'title' => ts('Relationship B-A'),
244 'name' => 'label_b_a',
245 ),
246 ),
6a488035
TO
247 'grouping' => 'relation-fields',
248 ),
9d72cede 249 'civicrm_relationship' => array(
6a488035 250 'dao' => 'CRM_Contact_DAO_Relationship',
9d72cede
EM
251 'fields' => array(
252 'start_date' => array(
253 'title' => ts('Relationship Start Date'),
6a488035 254 ),
9d72cede
EM
255 'end_date' => array(
256 'title' => ts('Relationship End Date'),
6a488035 257 ),
7d56e11e 258 'is_permission_a_b' => array(
259 'title' => ts('Is permission A over B?'),
260 ),
261 'is_permission_b_a' => array(
262 'title' => ts('Is permission B over A?'),
263 ),
9d72cede
EM
264 'description' => array(
265 'title' => ts('Description'),
6a488035 266 ),
9d72cede 267 'relationship_id' => array(
a9315b5e
DG
268 'title' => ts('Rel ID'),
269 'name' => 'id',
270 ),
6a488035 271 ),
9d72cede
EM
272 'filters' => array(
273 'is_active' => array(
274 'title' => ts('Relationship Status'),
6a488035 275 'operatorType' => CRM_Report_Form::OP_SELECT,
9d72cede 276 'options' => array(
ccc29f8e 277 '' => ts('- Any -'),
278 1 => ts('Active'),
279 0 => ts('Inactive'),
6a488035
TO
280 ),
281 'type' => CRM_Utils_Type::T_INT,
282 ),
7c41e675 283 'is_valid' => array(
284 'title' => ts('Relationship Dates Validity'),
285 'operatorType' => CRM_Report_Form::OP_SELECT,
286 'options' => array(
287 NULL => ts('- Any -'),
288 1 => ts('Not expired'),
289 0 => ts('Expired'),
290 ),
291 'type' => CRM_Utils_Type::T_INT,
292 ),
9d72cede
EM
293 'relationship_type_id' => array(
294 'title' => ts('Relationship'),
02cbed89 295 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
0ddfa7be 296 'options' => CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, NULL, TRUE),
6a488035
TO
297 'type' => CRM_Utils_Type::T_INT,
298 ),
7c41e675 299 'start_date' => array(
300 'title' => ts('Start Date'),
301 'type' => CRM_Utils_Type::T_DATE,
302 ),
303 'end_date' => array(
304 'title' => ts('End Date'),
305 'type' => CRM_Utils_Type::T_DATE,
306 ),
7d56e11e 307 'is_permission_a_b' => array(
308 'title' => ts('Does contact A have permission over contact B?'),
309 'operatorType' => CRM_Report_Form::OP_SELECT,
310 'options' => array(
311 '' => ts('- Any -'),
312 1 => ts('Yes'),
313 0 => ts('No'),
314 ),
315 'type' => CRM_Utils_Type::T_INT,
316 ),
317 'is_permission_b_a' => array(
318 'title' => ts('Does contact B have permission over contact A?'),
319 'operatorType' => CRM_Report_Form::OP_SELECT,
320 'options' => array(
321 '' => ts('- Any -'),
322 1 => ts('Yes'),
323 0 => ts('No'),
324 ),
325 'type' => CRM_Utils_Type::T_INT,
326 ),
327 ),
328
329 'order_bys' => array(
330 'start_date' => array(
331 'title' => ts('Start Date'),
332 'name' => 'start_date',
b3c6487a 333 ),
eea5db81 334 'active_period_date' => array(
335 'title' => ts('Active Period'),
336 'type' => CRM_Utils_Type::T_DATE,
337 ),
6a488035
TO
338 ),
339 'grouping' => 'relation-fields',
340 ),
9d72cede 341 'civicrm_address' => array(
6a488035 342 'dao' => 'CRM_Core_DAO_Address',
9d72cede
EM
343 'filters' => array(
344 'country_id' => array(
345 'title' => ts('Country'),
8ee006e7 346 'type' => CRM_Utils_Type::T_INT,
6a488035
TO
347 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
348 'options' => CRM_Core_PseudoConstant::country(),
349 ),
9d72cede
EM
350 'state_province_id' => array(
351 'title' => ts('State/Province'),
8ee006e7 352 'type' => CRM_Utils_Type::T_INT,
6a488035
TO
353 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
354 'options' => CRM_Core_PseudoConstant::stateProvince(),
355 ),
356 ),
357 'grouping' => 'contact-fields',
358 ),
6a488035
TO
359 );
360
16e2e80c 361 $this->_groupFilter = TRUE;
6a488035
TO
362 $this->_tagFilter = TRUE;
363 parent::__construct();
364 }
365
00be9182 366 public function preProcess() {
6a488035
TO
367 parent::preProcess();
368 }
369
00be9182 370 public function select() {
6a488035
TO
371 $select = $this->_columnHeaders = array();
372 foreach ($this->_columns as $tableName => $table) {
373 if (array_key_exists('fields', $table)) {
374 foreach ($table['fields'] as $fieldName => $field) {
9d72cede
EM
375 if (!empty($field['required']) ||
376 !empty($this->_params['fields'][$fieldName])
377 ) {
6a488035
TO
378
379 if ($fieldName == 'email_a') {
380 $this->_emailField_a = TRUE;
381 }
382 if ($fieldName == 'email_b') {
383 $this->_emailField_b = TRUE;
384 }
9d72cede 385 if ($fieldName == 'phone_a') {
7393ba64
M
386 $this->_phoneField_a = TRUE;
387 }
9d72cede 388 if ($fieldName == 'phone_b') {
7393ba64
M
389 $this->_phoneField_b = TRUE;
390 }
6a488035
TO
391 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
392 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
393 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
394 }
395 }
396 }
397 }
d1641c51 398 $this->_selectClauses = $select;
6a488035
TO
399
400 $this->_select = "SELECT " . implode(', ', $select) . " ";
401 }
402
00be9182 403 public function from() {
6a488035
TO
404 $this->_from = "
405 FROM civicrm_relationship {$this->_aliases['civicrm_relationship']}
2f4c2f5d 406
6a488035 407 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
2f4c2f5d 408 ON ( {$this->_aliases['civicrm_relationship']}.contact_id_a =
6a488035
TO
409 {$this->_aliases['civicrm_contact']}.id )
410
411 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact_b']}
2f4c2f5d 412 ON ( {$this->_aliases['civicrm_relationship']}.contact_id_b =
6a488035
TO
413 {$this->_aliases['civicrm_contact_b']}.id )
414
415 {$this->_aclFrom} ";
416
417 if (!empty($this->_params['country_id_value']) ||
418 !empty($this->_params['state_province_id_value'])
419 ) {
420 $this->_from .= "
2f4c2f5d 421 INNER JOIN civicrm_address {$this->_aliases['civicrm_address']}
6a488035
TO
422 ON (( {$this->_aliases['civicrm_address']}.contact_id =
423 {$this->_aliases['civicrm_contact']}.id OR
424 {$this->_aliases['civicrm_address']}.contact_id =
2f4c2f5d 425 {$this->_aliases['civicrm_contact_b']}.id ) AND
6a488035
TO
426 {$this->_aliases['civicrm_address']}.is_primary = 1 ) ";
427 }
428
429 $this->_from .= "
430 INNER JOIN civicrm_relationship_type {$this->_aliases['civicrm_relationship_type']}
2f4c2f5d 431 ON ( {$this->_aliases['civicrm_relationship']}.relationship_type_id =
6a488035
TO
432 {$this->_aliases['civicrm_relationship_type']}.id ) ";
433
eaafc14c 434 // Include Email Field.
6a488035 435 if ($this->_emailField_a) {
2f4c2f5d 436 $this->_from .= "
6a488035
TO
437 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
438 ON ( {$this->_aliases['civicrm_contact']}.id =
2f4c2f5d 439 {$this->_aliases['civicrm_email']}.contact_id AND
6a488035
TO
440 {$this->_aliases['civicrm_email']}.is_primary = 1 )";
441 }
442 if ($this->_emailField_b) {
2f4c2f5d 443 $this->_from .= "
444 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email_b']}
6a488035 445 ON ( {$this->_aliases['civicrm_contact_b']}.id =
2f4c2f5d 446 {$this->_aliases['civicrm_email_b']}.contact_id AND
6a488035
TO
447 {$this->_aliases['civicrm_email_b']}.is_primary = 1 )";
448 }
eaafc14c 449 // Include Phone Field.
7393ba64
M
450 if ($this->_phoneField_a) {
451 $this->_from .= "
452 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
453 ON ( {$this->_aliases['civicrm_contact']}.id =
454 {$this->_aliases['civicrm_phone']}.contact_id AND
455 {$this->_aliases['civicrm_phone']}.is_primary = 1 )";
456 }
457 if ($this->_phoneField_b) {
458 $this->_from .= "
459 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone_b']}
460 ON ( {$this->_aliases['civicrm_contact_b']}.id =
461 {$this->_aliases['civicrm_phone_b']}.contact_id AND
462 {$this->_aliases['civicrm_phone_b']}.is_primary = 1 )";
463 }
6a488035
TO
464 }
465
00be9182 466 public function where() {
6a488035
TO
467 $whereClauses = $havingClauses = array();
468 foreach ($this->_columns as $tableName => $table) {
469 if (array_key_exists('filters', $table)) {
470 foreach ($table['filters'] as $fieldName => $field) {
471
472 $clause = NULL;
473 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
474 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
9d72cede
EM
475 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
476 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
6a488035 477
eea5db81 478 if ($fieldName == 'active_period_date') {
479 $clause = $this->activeClause($field['name'], $relative, $from, $to, $field['type']);
480 }
481 else {
482 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
483 }
6a488035
TO
484 }
485 else {
486 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
487 if ($op) {
9d72cede
EM
488 if (($tableName == 'civicrm_contact' ||
489 $tableName == 'civicrm_contact_b') &&
490 ($fieldName == 'contact_type_a' ||
491 $fieldName == 'contact_type_b')
6a488035
TO
492 ) {
493 $cTypes = CRM_Utils_Array::value("{$fieldName}_value", $this->_params);
494 $contactTypes = $contactSubTypes = array();
495 if (!empty($cTypes)) {
496 foreach ($cTypes as $ctype) {
497 $getTypes = CRM_Utils_System::explode('_', $ctype, 2);
9d72cede
EM
498 if ($getTypes[1] &&
499 !in_array($getTypes[1], $contactSubTypes)
500 ) {
6a488035
TO
501 $contactSubTypes[] = $getTypes[1];
502 }
9d72cede
EM
503 elseif ($getTypes[0] &&
504 !in_array($getTypes[0], $contactTypes)
505 ) {
6a488035
TO
506 $contactTypes[] = $getTypes[0];
507 }
508 }
509 }
510
511 if (!empty($contactTypes)) {
512 $clause = $this->whereClause($field,
513 $op,
514 $contactTypes,
515 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
516 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
517 );
518 }
519
520 if (!empty($contactSubTypes)) {
bc1f381d 521 $field['name'] = 'contact_sub_type';
6a488035
TO
522 $field['dbAlias'] = $field['alias'] . '.' . $field['name'];
523 $subTypeClause = $this->whereClause($field,
524 $op,
525 $contactSubTypes,
526 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
527 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
528 );
529 if ($clause) {
530 $clause = '(' . $clause . ' OR ' . $subTypeClause . ')';
531 }
532 else {
533 $clause = $subTypeClause;
534 }
535 }
536 }
537 else {
e93563f1 538 if ($fieldName == 'is_valid') {
7c41e675 539 $clause = $this->buildValidityQuery(CRM_Utils_Array::value("{$fieldName}_value", $this->_params));
540 }
541 else {
542 $clause = $this->whereClause($field,
543 $op,
544 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
545 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
546 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
547 );
548 }
6a488035
TO
549 }
550 }
551 }
552
553 if (!empty($clause)) {
a7488080 554 if (!empty($field['having'])) {
6a488035
TO
555 $havingClauses[] = $clause;
556 }
557 else {
558 $whereClauses[] = $clause;
559 }
560 }
561 }
562 }
563 }
7d56e11e 564 $this->_where = "WHERE ( {$this->_aliases['civicrm_contact']}.is_deleted = 0 AND {$this->_aliases['civicrm_contact_b']}.is_deleted = 0 ) ";
565 if ($whereClauses) {
566 $this->_where .= ' AND ' . implode(' AND ', $whereClauses);
6a488035
TO
567 }
568 else {
7d56e11e 569 $this->_having = '';
6a488035
TO
570 }
571
572 if ($this->_aclWhere) {
573 $this->_where .= " AND {$this->_aclWhere} ";
574 }
575
576 if (!empty($havingClauses)) {
577 // use this clause to construct group by clause.
578 $this->_having = 'HAVING ' . implode(' AND ', $havingClauses);
579 }
580 }
581
74cf4551
EM
582 /**
583 * @param $rows
584 *
585 * @return array
586 */
00be9182 587 public function statistics(&$rows) {
6a488035
TO
588 $statistics = parent::statistics($rows);
589
590 $isStatusFilter = FALSE;
591 $relStatus = NULL;
592 if (CRM_Utils_Array::value('is_active_value', $this->_params) == '1') {
593 $relStatus = 'Is equal to Active';
594 }
595 elseif (CRM_Utils_Array::value('is_active_value', $this->_params) == '0') {
596 $relStatus = 'Is equal to Inactive';
597 }
a7488080 598 if (!empty($statistics['filters'])) {
6a488035 599 foreach ($statistics['filters'] as $id => $value) {
eaafc14c 600 // For displaying relationship type filter.
6a488035
TO
601 if ($value['title'] == 'Relationship') {
602 $relTypes = CRM_Core_PseudoConstant::relationshipType();
ccc29f8e 603 $op = CRM_Utils_Array::value('relationship_type_id_op', $this->_params) == 'in' ? ts('Is one of') . ' ' : ts('Is not one of') . ' ';
02cbed89
SL
604 $relationshipTypes = array();
605 foreach ($this->_params['relationship_type_id_value'] as $relationship) {
606 $relationshipTypes[] = $relTypes[$relationship]['label_' . $this->relationType];
607 }
608 $statistics['filters'][$id]['value'] = $op .
609 implode(', ', $relationshipTypes);
6a488035
TO
610 }
611
eaafc14c 612 // For displaying relationship status.
6a488035
TO
613 if ($value['title'] == 'Relationship Status') {
614 $isStatusFilter = TRUE;
615 $statistics['filters'][$id]['value'] = $relStatus;
616 }
617 }
618 }
eaafc14c 619 // For displaying relationship status.
6a488035
TO
620 if (!$isStatusFilter && $relStatus) {
621 $statistics['filters'][] = array(
e300cf31 622 'title' => ts('Relationship Status'),
6a488035
TO
623 'value' => $relStatus,
624 );
625 }
626 return $statistics;
627 }
628
00be9182 629 public function groupBy() {
6a488035
TO
630 $this->_groupBy = " ";
631 $groupBy = array();
632 if ($this->relationType == 'a_b') {
633 $groupBy[] = " {$this->_aliases['civicrm_contact']}.id";
634 }
635 elseif ($this->relationType == 'b_a') {
636 $groupBy[] = " {$this->_aliases['civicrm_contact_b']}.id";
637 }
638
639 if (!empty($groupBy)) {
d1641c51 640 $groupBy[] = "{$this->_aliases['civicrm_relationship']}.id";
6a488035
TO
641 }
642 else {
d1641c51 643 $groupBy = array("{$this->_aliases['civicrm_relationship']}.id");
6a488035 644 }
d1641c51 645
b708c08d 646 $this->_groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, $groupBy);
6a488035
TO
647 }
648
99a555a5 649 public function beginPostProcessCommon() {
13415144
CW
650 $originalRelationshipTypeIdValue = CRM_Utils_Array::value('relationship_type_id_value', $this->_params);
651 if ($originalRelationshipTypeIdValue) {
02cbed89
SL
652 $relationshipTypes = array();
653 $direction = array();
13415144 654 foreach ((array) $originalRelationshipTypeIdValue as $relationship_type) {
02cbed89
SL
655 $relType = explode('_', $relationship_type);
656 $direction[] = $relType[1] . '_' . $relType[2];
657 $relationshipTypes[] = intval($relType[0]);
658 }
eaafc14c 659 // Lets take the first relationship type to guide us in the relationship
660 // direction we should use.
02cbed89
SL
661 $this->relationType = $direction[0];
662 $this->_params['relationship_type_id_value'] = $relationshipTypes;
6a488035 663 }
99a555a5
ML
664 }
665
666 public function postProcess() {
667 $this->beginPostProcess();
6a488035 668
9d72cede 669 $this->buildACLClause(array(
353ffa53
TO
670 $this->_aliases['civicrm_contact'],
671 $this->_aliases['civicrm_contact_b'],
672 ));
99a555a5 673
6a488035
TO
674 $sql = $this->buildQuery();
675 $this->buildRows($sql, $rows);
676
677 $this->formatDisplay($rows);
678 $this->doTemplateAssignment($rows);
679
674b20c9 680 if (!empty($originalRelationshipTypeIdValue)) {
eaafc14c 681 // Store its old value, CRM-5837.
674b20c9 682 $this->_params['relationship_type_id_value'] = $originalRelationshipTypeIdValue;
6a488035
TO
683 }
684 $this->endPostProcess($rows);
685 }
686
74cf4551 687 /**
70bea8e2 688 * @param $rows
74cf4551 689 */
00be9182 690 public function alterDisplay(&$rows) {
eaafc14c 691 // Custom code to alter rows.
6a488035
TO
692 $entryFound = FALSE;
693
694 foreach ($rows as $rowNum => $row) {
695
37935ea7
WM
696 // Handle ID to label conversion for contact fields
697 $entryFound = $this->alterDisplayContactFields($row, $rows, $rowNum, 'contact/relationship', 'View Relationships') ? TRUE : $entryFound;
698
699 // Handle contact subtype A
7ed79ffa 700 // @todo refactor into separate function
37935ea7
WM
701 if (array_key_exists('civicrm_contact_contact_sub_type_a', $row)) {
702 if ($value = $row['civicrm_contact_contact_sub_type_a']) {
703 $rowValues = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
704 $rowLabels = array();
705 foreach ($rowValues as $rowValue) {
706 if ($rowValue) {
707 $rowLabels[] = CRM_Core_Pseudoconstant::getLabel('CRM_Contact_BAO_Contact', 'contact_sub_type', $rowValue);
708 }
709 }
710 $rows[$rowNum]['civicrm_contact_contact_sub_type_a'] = implode(', ', $rowLabels);
711 }
712 $entryFound = TRUE;
713 }
714
715 // Handle contact subtype B
7ed79ffa 716 // @todo refactor into separate function
37935ea7
WM
717 if (array_key_exists('civicrm_contact_b_contact_sub_type_b', $row)) {
718 if ($value = $row['civicrm_contact_b_contact_sub_type_b']) {
719 $rowValues = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
720 $rowLabels = array();
721 foreach ($rowValues as $rowValue) {
722 if ($rowValue) {
723 $rowLabels[] = CRM_Core_Pseudoconstant::getLabel('CRM_Contact_BAO_Contact', 'contact_sub_type', $rowValue);
724 }
725 }
726 $rows[$rowNum]['civicrm_contact_b_contact_sub_type_b'] = implode(', ', $rowLabels);
727 }
728 $entryFound = TRUE;
729 }
730
7ed79ffa
WM
731 // Handle country
732 // @todo use alterDisplayAddressFields function
6a488035
TO
733 if (array_key_exists('civicrm_address_country_id', $row)) {
734 if ($value = $row['civicrm_address_country_id']) {
735 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant::country($value, FALSE);
736 }
737 $entryFound = TRUE;
738 }
739
7ed79ffa
WM
740 // Handle state/province
741 // @todo use alterDisplayAddressFields function
6a488035
TO
742 if (array_key_exists('civicrm_address_state_province_id', $row)) {
743 if ($value = $row['civicrm_address_state_province_id']) {
744 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant::stateProvince($value, FALSE);
745 }
746 $entryFound = TRUE;
747 }
748
7ed79ffa
WM
749 // Handle contact name A
750 // @todo refactor into separate function
6a488035
TO
751 if (array_key_exists('civicrm_contact_sort_name_a', $row) &&
752 array_key_exists('civicrm_contact_id', $row)
753 ) {
754 $url = CRM_Report_Utils_Report::getNextUrl('contact/detail',
755 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_id'],
756 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
757 );
7c550ca0
WA
758 $rows[$rowNum]['civicrm_contact_sort_name_a']
759 = $rows[$rowNum]['civicrm_contact_sort_name_a'] . ' (' .
9d72cede 760 $rows[$rowNum]['civicrm_contact_id'] . ')';
6a488035 761 $rows[$rowNum]['civicrm_contact_sort_name_a_link'] = $url;
37935ea7 762 $rows[$rowNum]['civicrm_contact_sort_name_a_hover'] = ts('View Contact Detail Report for this contact');
6a488035
TO
763 $entryFound = TRUE;
764 }
765
7ed79ffa
WM
766 // Handle contact name B
767 // @todo refactor into separate function
6a488035
TO
768 if (array_key_exists('civicrm_contact_b_sort_name_b', $row) &&
769 array_key_exists('civicrm_contact_b_id', $row)
770 ) {
771 $url = CRM_Report_Utils_Report::getNextUrl('contact/detail',
772 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_contact_b_id'],
773 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
774 );
7c550ca0
WA
775 $rows[$rowNum]['civicrm_contact_b_sort_name_b']
776 = $rows[$rowNum]['civicrm_contact_b_sort_name_b'] . ' (' .
9d72cede 777 $rows[$rowNum]['civicrm_contact_b_id'] . ')';
6a488035 778 $rows[$rowNum]['civicrm_contact_b_sort_name_b_link'] = $url;
37935ea7 779 $rows[$rowNum]['civicrm_contact_b_sort_name_b_hover'] = ts('View Contact Detail Report for this contact');
a9315b5e
DG
780 $entryFound = TRUE;
781 }
782
7ed79ffa 783 // Handle relationship
bb0a814d
WM
784 if (array_key_exists('civicrm_relationship_relationship_id', $row) &&
785 array_key_exists('civicrm_contact_id', $row)
786 ) {
787 $url = "/civicrm/contact/view/rel?reset=1&action=update&rtype=a_b&cid=" .
788 $row['civicrm_contact_id'] . "&id=" .
789 $row['civicrm_relationship_relationship_id'];
790 $rows[$rowNum]['civicrm_relationship_relationship_id_link'] = $url;
791 $rows[$rowNum]['civicrm_relationship_relationship_id_hover'] = ts("Edit this relationship.");
792 $entryFound = TRUE;
793 }
794
6a488035
TO
795 // skip looking further in rows, if first row itself doesn't
796 // have the column we need
797 if (!$entryFound) {
798 break;
799 }
800 }
801 }
96025800 802
7c41e675 803 /**
81ec12f6 804 * @param $valid bool - set to 1 if we are looking for a valid relationship, 0 if not
7c41e675 805 *
806 * @return array
807 */
81ec12f6 808 public function buildValidityQuery($valid) {
7c41e675 809 $clause = NULL;
810 if ($valid == '1') {
eaafc14c 811 // Relationships dates are not expired.
7c41e675 812 $clause = "((start_date <= CURDATE() OR start_date is null) AND (end_date >= CURDATE() OR end_date is null))";
813 }
814 elseif ($valid == '0') {
eaafc14c 815 // Relationships dates are expired or has not started yet.
7c41e675 816 $clause = "(start_date >= CURDATE() OR end_date < CURDATE())";
817 }
818 return $clause;
819 }
81ec12f6 820
eea5db81 821 /**
822 * Get SQL where clause for a active period field.
823 *
824 * @param string $fieldName
825 * @param string $relative
826 * @param string $from
827 * @param string $to
828 * @param string $type
829 * @param string $fromTime
830 * @param string $toTime
831 *
832 * @return null|string
833 */
834 public function activeClause(
835 $fieldName,
836 $relative, $from, $to, $type = NULL, $fromTime = NULL, $toTime = NULL
837 ) {
838 $clauses = array();
839 if (in_array($relative, array_keys($this->getOperationPair(CRM_Report_Form::OP_DATE)))) {
840 return NULL;
841 }
842
843 list($from, $to) = $this->getFromTo($relative, $from, $to, $fromTime, $toTime);
844
845 if ($from) {
846 $from = ($type == CRM_Utils_Type::T_DATE) ? substr($from, 0, 8) : $from;
847 }
848
849 if ($to) {
850 $to = ($type == CRM_Utils_Type::T_DATE) ? substr($to, 0, 8) : $to;
851 }
852
02633eb0 853 if ($from || $to) {
eea5db81 854 return CRM_Contact_BAO_Query::getRelationshipActivePeriodClauses($from, $to, FALSE);
855 }
eea5db81 856 return NULL;
857 }
858
6a488035 859}