Merge pull request #15964 from mlutfy/financial109
[civicrm-core.git] / CRM / Report / Form.php
CommitLineData
6a488035 1<?php
6a488035 2/*
bc77d7c0
TO
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
e70a7fc0 10 */
6a488035
TO
11
12/**
7d7c50f9 13 * Class CRM_Report_Form
6a488035
TO
14 */
15class CRM_Report_Form extends CRM_Core_Form {
7da04cde 16 const ROW_COUNT_LIMIT = 50;
6a488035
TO
17
18 /**
19 * Operator types - used for displaying filter elements
20 */
7da04cde 21 const
9d72cede 22 OP_INT = 1,
6a488035 23 OP_STRING = 2,
9d72cede 24 OP_DATE = 4,
6a488035 25 OP_DATETIME = 5,
9d72cede 26 OP_FLOAT = 8,
6a488035
TO
27 OP_SELECT = 64,
28 OP_MULTISELECT = 65,
29 OP_MULTISELECT_SEPARATOR = 66,
2107cde9
CW
30 OP_MONTH = 128,
31 OP_ENTITYREF = 256;
6a488035
TO
32
33 /**
34 * The id of the report instance
35 *
9f266042 36 * @var int
6a488035
TO
37 */
38 protected $_id;
39
40 /**
41 * The id of the report template
42 *
9f266042 43 * @var int
6a488035
TO
44 */
45 protected $_templateID;
46
47 /**
48 * The report title
49 *
50 * @var string
51 */
52 protected $_title;
53 protected $_noFields = FALSE;
54
55 /**
56 * The set of all columns in the report. An associative array
537c70b8 57 * with column name as the key and attributes as the value
6a488035
TO
58 *
59 * @var array
60 */
be2fb01f 61 protected $_columns = [];
6a488035
TO
62
63 /**
64 * The set of filters in the report
65 *
66 * @var array
67 */
be2fb01f 68 protected $_filters = [];
6a488035
TO
69
70 /**
71 * The set of optional columns in the report
72 *
73 * @var array
74 */
be2fb01f 75 public $_options = [];
6a488035 76
688d37c6 77 /**
6a488035
TO
78 * By default most reports hide contact id.
79 * Setting this to true makes it available
c86d4e7c 80 * @var bool
6a488035
TO
81 */
82 protected $_exposeContactID = TRUE;
83
84 /**
85 * Set of statistic fields
86 *
87 * @var array
88 */
be2fb01f 89 protected $_statFields = [];
6a488035
TO
90
91 /**
92 * Set of statistics data
93 *
94 * @var array
95 */
be2fb01f 96 protected $_statistics = [];
6a488035
TO
97
98 /**
99 * List of fields not to be repeated during display
100 *
101 * @var array
102 */
be2fb01f 103 protected $_noRepeats = [];
6a488035
TO
104
105 /**
106 * List of fields not to be displayed
107 *
108 * @var array
109 */
be2fb01f 110 protected $_noDisplay = [];
6a488035
TO
111
112 /**
113 * Object type that a custom group extends
114 *
115 * @var null
116 */
117 protected $_customGroupExtends = NULL;
be2fb01f 118 protected $_customGroupExtendsJoin = [];
6a488035
TO
119 protected $_customGroupFilters = TRUE;
120 protected $_customGroupGroupBy = FALSE;
9d72cede 121 protected $_customGroupJoin = 'LEFT JOIN';
6a488035
TO
122
123 /**
100fef9d 124 * Build tags filter
c86d4e7c 125 * @var bool
6a488035
TO
126 */
127 protected $_tagFilter = FALSE;
128
129 /**
ed795723 130 * specify entity table for tags filter
c86d4e7c 131 * @var string
ed795723
JM
132 */
133 protected $_tagFilterTable = 'civicrm_contact';
134
6a488035 135 /**
0b62c1ab 136 * Build groups filter.
6a488035 137 *
f587aa17 138 * @var bool
6a488035
TO
139 */
140 protected $_groupFilter = FALSE;
141
43c1fa19 142 /**
143 * Has the report been optimised for group filtering.
144 *
145 * The functionality for group filtering has been improved but not
146 * all reports have been adjusted to take care of it.
147 *
148 * This property exists to highlight the reports which are still using the
149 * slow method & allow group filtering to still work for them until they
150 * can be migrated.
151 *
152 * In order to protect extensions we have to default to TRUE - but I have
153 * separately marked every class with a groupFilter in the hope that will trigger
154 * people to fix them as they touch them.
155 *
156 * CRM-19170
157 *
158 * @var bool
159 */
160 protected $groupFilterNotOptimised = TRUE;
161
6a488035
TO
162 /**
163 * Navigation fields
164 *
165 * @var array
166 */
be2fb01f 167 public $_navigation = [];
6a488035 168
be2fb01f 169 public $_drilldownReport = [];
6a488035 170
50951061 171 /**
0b62c1ab
EM
172 * Array of tabs to display on report.
173 *
174 * E.g we define the tab title, the tpl and the tab-specific part of the css or html link.
175 *
176 * $this->tabs['OrderBy'] = array(
177 * 'title' => ts('Sorting'),
178 * 'tpl' => 'OrderBy',
179 * 'div_label' => 'order-by',
180 * );
50951061
EM
181 *
182 * @var array
183 */
be2fb01f 184 protected $tabs = [];
50951061 185
182f5081 186 /**
187 * Should we add paging.
188 *
189 * @var bool
190 */
191 protected $addPaging = TRUE;
192
81a22d3d 193 protected $isForceGroupBy = FALSE;
194
195 protected $groupConcatTested = FALSE;
196
6a488035
TO
197 /**
198 * An attribute for checkbox/radio form field layout
199 *
200 * @var array
201 */
be2fb01f 202 protected $_fourColumnAttribute = [
9d72cede
EM
203 '</td><td width="25%">',
204 '</td><td width="25%">',
205 '</td><td width="25%">',
206 '</tr><tr><td>',
be2fb01f 207 ];
6a488035
TO
208
209 protected $_force = 1;
210
211 protected $_params = NULL;
212 protected $_formValues = NULL;
213 protected $_instanceValues = NULL;
214
215 protected $_instanceForm = FALSE;
216 protected $_criteriaForm = FALSE;
217
218 protected $_instanceButtonName = NULL;
219 protected $_createNewButtonName = NULL;
220 protected $_printButtonName = NULL;
221 protected $_pdfButtonName = NULL;
222 protected $_csvButtonName = NULL;
223 protected $_groupButtonName = NULL;
224 protected $_chartButtonName = NULL;
225 protected $_csvSupported = TRUE;
226 protected $_add2groupSupported = TRUE;
227 protected $_groups = NULL;
9b0380d9 228 protected $_grandFlag = FALSE;
6a488035 229 protected $_rowsFound = NULL;
be2fb01f 230 protected $_selectAliases = [];
6a488035 231 protected $_rollup = NULL;
6f900755 232
43c1fa19 233 /**
234 * Table containing list of contact IDs within the group filter.
235 *
236 * @var string
237 */
238 protected $groupTempTable = '';
239
f587aa17
EM
240 /**
241 * @var array
242 */
be2fb01f 243 protected $_aliases = [];
f587aa17
EM
244
245 /**
246 * @var string
247 */
248 protected $_where;
249
250 /**
251 * @var string
252 */
253 protected $_from;
254
6f900755
E
255 /**
256 * SQL Limit clause
257 * @var string
258 */
6a488035 259 protected $_limit = NULL;
6f900755
E
260
261 /**
262 * This can be set to specify a limit to the number of rows
263 * Since it is currently envisaged as part of the api usage it is only being applied
264 * when $_output mode is not 'html' or 'group' so as not to have to interpret / mess with that part
182f5081 265 * of the code (see limit() fn.
266 *
9f266042 267 * @var int
6f900755
E
268 */
269 protected $_limitValue = NULL;
270
271 /**
272 * This can be set to specify row offset
273 * See notes on _limitValue
9f266042 274 *
275 * @var int
6f900755
E
276 */
277 protected $_offsetValue = NULL;
cdb44cba
EM
278 /**
279 * @var null
280 */
44d59d04 281 protected $_sections = NULL;
6a488035
TO
282 protected $_autoIncludeIndexedFieldsAsOrderBys = 0;
283 protected $_absoluteUrl = FALSE;
284
ae555e90
DS
285 /**
286 * Flag to indicate if result-set is to be stored in a class variable which could be retrieved using getResultSet() method.
287 *
b67daa72 288 * @var bool
ae555e90
DS
289 */
290 protected $_storeResultSet = FALSE;
291
292 /**
293 * When _storeResultSet Flag is set use this var to store result set in form of array
294 *
b67daa72 295 * @var bool
ae555e90 296 */
be2fb01f 297 protected $_resultSet = [];
ae555e90 298
6a488035
TO
299 /**
300 * To what frequency group-by a date column
301 *
302 * @var array
303 */
be2fb01f 304 protected $_groupByDateFreq = [
6a488035
TO
305 'MONTH' => 'Month',
306 'YEARWEEK' => 'Week',
307 'QUARTER' => 'Quarter',
308 'YEAR' => 'Year',
be2fb01f 309 ];
6a488035
TO
310
311 /**
312 * Variables to hold the acl inner join and where clause
cc101011 313 * @var string|null
6a488035
TO
314 */
315 protected $_aclFrom = NULL;
316 protected $_aclWhere = NULL;
317
318 /**
568a0946 319 * Array of DAO tables having columns included in SELECT or ORDER BY clause.
320 *
321 * Where has also been added to this although perhaps the 'includes both' array should have a different name.
6a488035
TO
322 *
323 * @var array
324 */
be2fb01f 325 protected $_selectedTables = [];
6a488035 326
568a0946 327 /**
328 * Array of DAO tables having columns included in WHERE or HAVING clause
329 *
330 * @var array
331 */
332 protected $filteredTables;
333
c58f66e0 334 /**
dc0c71cf 335 * Output mode e.g 'print', 'csv', 'pdf'.
336 *
c58f66e0
E
337 * @var string
338 */
f63fae91 339 protected $_outputMode;
c58f66e0 340
dc0c71cf 341 /**
342 * Format of any chart in use.
343 *
344 * (it's unclear if this could be merged with outputMode at this stage)
345 *
cc101011 346 * @var string|null
dc0c71cf 347 */
348 protected $_format;
349
6a488035
TO
350 public $_having = NULL;
351 public $_select = NULL;
be2fb01f
CW
352 public $_selectClauses = [];
353 public $_columnHeaders = [];
6a488035 354 public $_orderBy = NULL;
be2fb01f
CW
355 public $_orderByFields = [];
356 public $_orderByArray = [];
70e504f2 357 /**
358 * Array of clauses to group by.
359 *
360 * @var array
361 */
be2fb01f 362 protected $_groupByArray = [];
6a488035 363 public $_groupBy = NULL;
be2fb01f
CW
364 public $_whereClauses = [];
365 public $_havingClauses = [];
1c4d8c3e 366
dbb4a0f9 367 /**
cc101011 368 * DashBoardRowCount Dashboard row count.
369 *
370 * @var int
dbb4a0f9
PN
371 */
372 public $_dashBoardRowCount;
373
c58f66e0
E
374 /**
375 * Is this being called without a form controller (ie. the report is being render outside the normal form
cc101011 376 * - e.g the api is retrieving the rows.
377 *
b67daa72 378 * @var bool
c58f66e0
E
379 */
380 public $noController = FALSE;
381
7a961f19 382 /**
cc101011 383 * Variable to hold the currency alias.
384 *
385 * @var string|null
7a961f19 386 */
387 protected $_currencyColumn = NULL;
6a488035 388
2fe3c48d
EM
389 /**
390 * @var string
391 */
392 protected $_interval;
393
394 /**
395 * @var bool
396 */
397 protected $_sendmail;
398
399 /**
400 * @var int
401 */
402 protected $_chartId;
403
9907633b
EM
404 /**
405 * @var int
406 */
44d59d04 407 public $_section;
9907633b
EM
408
409 /**
9f266042 410 * Report description.
411 *
412 * @var string
9907633b 413 */
15ba35a3 414 public $_description;
9907633b
EM
415
416 /**
9f266042 417 * Is an address field selected.
418 *
419 * @var bool
9907633b
EM
420 * This was intended to determine if the address table should be joined in
421 * The isTableSelected function is now preferred for this purpose
422 */
423 protected $_addressField;
424
425 /**
9f266042 426 * Is an email field selected.
427 *
428 * @var bool
9907633b
EM
429 * This was intended to determine if the email table should be joined in
430 * The isTableSelected function is now preferred for this purpose
431 */
432 protected $_emailField;
433
434 /**
9f266042 435 * Is a phone field selected.
436 *
437 * @var bool
9907633b
EM
438 * This was intended to determine if the phone table should be joined in
439 * The isTableSelected function is now preferred for this purpose
440 */
441 protected $_phoneField;
442
443 /**
9f266042 444 * Create new report instance? (or update existing) on save.
445 *
446 * @var bool
9907633b
EM
447 */
448 protected $_createNew;
449
8a2bee47 450 /**
451 * When a grand total row has calculated the status we pop it off to here.
452 *
453 * This allows us to access it from the stats function and avoid recalculating.
9f266042 454 *
c86d4e7c 455 * @var array
8a2bee47 456 */
be2fb01f 457 protected $rollupRow = [];
8a2bee47 458
9099266e 459 /**
9f266042 460 * Database attributes - character set and collation.
461 *
462 * @var string
9099266e 463 */
d09afb3e 464 protected $_databaseAttributes = ' DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci';
9099266e 465
02d451ab
EM
466 /**
467 * SQL being run in this report.
468 *
469 * The sql in the report is stored in this variable in order to be displayed on the developer tab.
470 *
471 * @var string
472 */
473
474 protected $sql;
55f71fa7 475
81a22d3d 476 /**
477 * An instruction not to add a Group By.
478 *
479 * This is relevant where the group by might be otherwise added after the code that determines the group by array.
480 *
481 * e.g. where stat fields are being added but other settings cause it to not be desirable to add a group by
482 * such as in pivot charts when no row header is set
483 *
484 * @var bool
485 */
486 protected $noGroupBy = FALSE;
487
55f71fa7 488 /**
489 * SQL being run in this report as an array.
490 *
491 * The sql in the report is stored in this variable in order to be returned to api & test calls.
492 *
493 * @var string
494 */
495
496 protected $sqlArray;
66ddb273 497
e6bab5ea 498 /**
499 * Tables created for the report that need removal afterwards.
500 *
501 * ['civicrm_temp_report_x' => ['temporary' => TRUE, 'name' => 'civicrm_temp_report_x']
502 * @var array
503 */
504 protected $temporaryTables = [];
505
66ddb273
SL
506 /**
507 * Can this report use the sql mode ONLY_FULL_GROUP_BY.
508 * @var bool
509 */
510 public $optimisedForOnlyFullGroupBy = TRUE;
c86d4e7c 511
6a488035 512 /**
7d7c50f9 513 * Class constructor.
6a488035 514 */
00be9182 515 public function __construct() {
6a488035
TO
516 parent::__construct();
517
22b67281
CW
518 $this->addClass('crm-report-form');
519
6a488035
TO
520 if ($this->_tagFilter) {
521 $this->buildTagFilter();
522 }
523 if ($this->_exposeContactID) {
524 if (array_key_exists('civicrm_contact', $this->_columns)) {
be2fb01f 525 $this->_columns['civicrm_contact']['fields']['exposed_id'] = [
6a488035 526 'name' => 'id',
ccc29f8e 527 'title' => ts('Contact ID'),
6a488035 528 'no_repeat' => TRUE,
be2fb01f 529 ];
6a488035
TO
530 }
531 }
532
533 if ($this->_groupFilter) {
534 $this->buildGroupFilter();
535 }
536
537 // Get all custom groups
cd43c5e3 538 $allGroups = CRM_Core_PseudoConstant::get('CRM_Core_DAO_CustomField', 'custom_group_id');
6a488035 539
4f8ec8be
DS
540 // Get the custom groupIds for which the user has VIEW permission
541 // If the user has 'access all custom data' permission, we'll leave $permCustomGroupIds empty
542 // and addCustomDataToColumns() will allow access to all custom groups.
be2fb01f 543 $permCustomGroupIds = [];
4f8ec8be
DS
544 if (!CRM_Core_Permission::check('access all custom data')) {
545 $permCustomGroupIds = CRM_ACL_API::group(CRM_Core_Permission::VIEW, NULL, 'civicrm_custom_group', $allGroups, NULL);
546 // do not allow custom data for reports if user doesn't have
547 // permission to access custom data.
548 if (!empty($this->_customGroupExtends) && empty($permCustomGroupIds)) {
be2fb01f 549 $this->_customGroupExtends = [];
4f8ec8be 550 }
6a488035
TO
551 }
552
553 // merge custom data columns to _columns list, if any
554 $this->addCustomDataToColumns(TRUE, $permCustomGroupIds);
555
556 // add / modify display columns, filters ..etc
557 CRM_Utils_Hook::alterReportVar('columns', $this->_columns, $this);
7a961f19 558
559 //assign currencyColumn variable to tpl
560 $this->assign('currencyColumn', $this->_currencyColumn);
6a488035
TO
561 }
562
0b62c1ab
EM
563 /**
564 * Shared pre-process function.
565 *
566 * If overriding preProcess function this should still be called.
567 *
568 * @throws \Exception
569 */
00be9182 570 public function preProcessCommon() {
1273d77c 571 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean');
77b97be7 572
1273d77c 573 $this->_dashBoardRowCount = CRM_Utils_Request::retrieve('rowCount', 'Integer');
6a488035 574
a3d827a7 575 $this->_section = CRM_Utils_Request::retrieve('section', 'Integer');
6a488035
TO
576
577 $this->assign('section', $this->_section);
be2fb01f 578 CRM_Core_Region::instance('page-header')->add([
6a488035 579 'markup' => sprintf('<!-- Report class: [%s] -->', htmlentities(get_class($this))),
be2fb01f 580 ]);
9d72cede 581 if (!$this->noController) {
c58f66e0 582 $this->setID($this->get('instanceId'));
6a488035 583
6a488035 584 if (!$this->_id) {
c58f66e0
E
585 $this->setID(CRM_Report_Utils_Report::getInstanceID());
586 if (!$this->_id) {
9d72cede 587 $this->setID(CRM_Report_Utils_Report::getInstanceIDForPath());
c58f66e0 588 }
6a488035 589 }
6a488035 590
c58f66e0
E
591 // set qfkey so that pager picks it up and use it in the "Next > Last >>" links.
592 // FIXME: Note setting it in $_GET doesn't work, since pager generates link based on QUERY_STRING
593 $_SERVER['QUERY_STRING'] .= "&qfKey={$this->controller->_key}";
594 }
6a488035
TO
595
596 if ($this->_id) {
597 $this->assign('instanceId', $this->_id);
be2fb01f
CW
598 $params = ['id' => $this->_id];
599 $this->_instanceValues = [];
0b25329b 600 CRM_Core_DAO::commonRetrieve('CRM_Report_DAO_ReportInstance',
6a488035
TO
601 $params,
602 $this->_instanceValues
603 );
604 if (empty($this->_instanceValues)) {
605 CRM_Core_Error::fatal("Report could not be loaded.");
606 }
7cab1323 607 $this->_title = $this->_instanceValues['title'];
6a488035
TO
608 if (!empty($this->_instanceValues['permission']) &&
609 (!(CRM_Core_Permission::check($this->_instanceValues['permission']) ||
610 CRM_Core_Permission::check('administer Reports')
611 ))
612 ) {
613 CRM_Utils_System::permissionDenied();
614 CRM_Utils_System::civiExit();
615 }
616
617 $formValues = CRM_Utils_Array::value('form_values', $this->_instanceValues);
618 if ($formValues) {
f24846d5 619 $this->_formValues = CRM_Utils_String::unserialize($formValues);
6a488035
TO
620 }
621 else {
622 $this->_formValues = NULL;
623 }
624
182f5081 625 $this->setOutputMode();
626
627 if ($this->_outputMode == 'copy') {
14cdc4cb 628 $this->_createNew = TRUE;
182f5081 629 $this->_params = $this->_formValues;
630 $this->_params['view_mode'] = 'criteria';
be2fb01f 631 $this->_params['title'] = $this->getTitle() . ts(' (copy created by %1 on %2)', [
8f864776 632 CRM_Core_Session::singleton()->getLoggedInContactDisplayName(),
633 CRM_Utils_Date::customFormat(date('Y-m-d H:i')),
be2fb01f 634 ]);
182f5081 635 // Do not pass go. Do not collect another chance to re-run the same query.
636 CRM_Report_Form_Instance::postProcess($this);
637 }
638
6a488035 639 // lets always do a force if reset is found in the url.
e6e7e540 640 // Hey why not? see CRM-17225 for more about this. The use of reset to be force is historical for reasons stated
641 // in the comment line above these 2.
182f5081 642 if (!empty($_REQUEST['reset'])
be2fb01f 643 && !in_array(CRM_Utils_Request::retrieve('output', 'String'), ['save', 'criteria'])) {
6a488035
TO
644 $this->_force = 1;
645 }
646
647 // set the mode
648 $this->assign('mode', 'instance');
649 }
c58f66e0 650 elseif (!$this->noController) {
6a488035
TO
651 list($optionValueID, $optionValue) = CRM_Report_Utils_Report::getValueIDFromUrl();
652 $instanceCount = CRM_Report_Utils_Report::getInstanceCount($optionValue);
653 if (($instanceCount > 0) && $optionValueID) {
654 $this->assign('instanceUrl',
655 CRM_Utils_System::url('civicrm/report/list',
656 "reset=1&ovid=$optionValueID"
657 )
658 );
659 }
660 if ($optionValueID) {
661 $this->_description = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $optionValueID, 'description');
662 }
663
664 // set the mode
665 $this->assign('mode', 'template');
666 }
667
668 // lets display the Report Settings section
669 $this->_instanceForm = $this->_force || $this->_id || (!empty($_POST));
670
671 // Do not display Report Settings section if administer Reports permission is absent OR
672 // if report instance is reserved and administer reserved reports absent
673 if (!CRM_Core_Permission::check('administer Reports') ||
9d72cede
EM
674 ($this->_instanceValues['is_reserved'] &&
675 !CRM_Core_Permission::check('administer reserved reports'))
676 ) {
6a488035
TO
677 $this->_instanceForm = FALSE;
678 }
679
680 $this->assign('criteriaForm', FALSE);
681 // Display Report Criteria section if user has access Report Criteria OR administer Reports AND report instance is not reserved
9d72cede
EM
682 if (CRM_Core_Permission::check('administer Reports') ||
683 CRM_Core_Permission::check('access Report Criteria')
684 ) {
685 if (!$this->_instanceValues['is_reserved'] ||
686 CRM_Core_Permission::check('administer reserved reports')
687 ) {
6a488035
TO
688 $this->assign('criteriaForm', TRUE);
689 $this->_criteriaForm = TRUE;
690 }
691 }
692
92991caf 693 // Special permissions check for private instance if it's not the current contact instance
766eb4ba 694 if ($this->_id &&
2d9a4a4c 695 (CRM_Report_BAO_ReportInstance::reportIsPrivate($this->_id) &&
696 !CRM_Report_BAO_ReportInstance::contactIsOwner($this->_id))) {
92991caf
SV
697 if (!CRM_Core_Permission::check('access all private reports')) {
698 $this->_instanceForm = FALSE;
699 $this->assign('criteriaForm', FALSE);
700 }
701 }
702
6a488035
TO
703 $this->_instanceButtonName = $this->getButtonName('submit', 'save');
704 $this->_createNewButtonName = $this->getButtonName('submit', 'next');
6a488035
TO
705 $this->_groupButtonName = $this->getButtonName('submit', 'group');
706 $this->_chartButtonName = $this->getButtonName('submit', 'chart');
707 }
708
0b62c1ab
EM
709 /**
710 * Add bread crumb.
711 */
00be9182 712 public function addBreadCrumb() {
971d41b1 713 $breadCrumbs
be2fb01f
CW
714 = [
715 [
8f1445ea
DL
716 'title' => ts('Report Templates'),
717 'url' => CRM_Utils_System::url('civicrm/admin/report/template/list', 'reset=1'),
be2fb01f
CW
718 ],
719 ];
6a488035
TO
720
721 CRM_Utils_System::appendBreadCrumb($breadCrumbs);
722 }
723
0b62c1ab
EM
724 /**
725 * Pre process function.
726 *
727 * Called prior to build form.
728 */
00be9182 729 public function preProcess() {
8f1445ea 730 $this->preProcessCommon();
6a488035 731
6a488035 732 if (!$this->_id) {
29fc2b79 733 $this->addBreadCrumb();
6a488035
TO
734 }
735
736 foreach ($this->_columns as $tableName => $table) {
8bb36676 737 $this->setTableAlias($table, $tableName);
6a488035 738
be2fb01f 739 $expFields = [];
6a488035 740 // higher preference to bao object
f63c531a 741 $daoOrBaoName = CRM_Utils_Array::value('bao', $table, CRM_Utils_Array::value('dao', $table));
742
743 if ($daoOrBaoName) {
744 if (method_exists($daoOrBaoName, 'exportableFields')) {
745 $expFields = $daoOrBaoName::exportableFields();
746 }
747 else {
748 $expFields = $daoOrBaoName::export();
749 }
8edb9849 750 }
6a488035 751
be2fb01f 752 $doNotCopy = ['required', 'default'];
6a488035 753
be2fb01f 754 $fieldGroups = ['fields', 'filters', 'group_bys', 'order_bys'];
6a488035 755 foreach ($fieldGroups as $fieldGrp) {
a7488080 756 if (!empty($table[$fieldGrp]) && is_array($table[$fieldGrp])) {
6a488035
TO
757 foreach ($table[$fieldGrp] as $fieldName => $field) {
758 // $name is the field name used to reference the BAO/DAO export fields array
759 $name = isset($field['name']) ? $field['name'] : $fieldName;
760
761 // Sometimes the field name key in the BAO/DAO export fields array is
762 // different from the actual database field name.
763 // Unset $field['name'] so that actual database field name can be obtained
764 // from the BAO/DAO export fields array.
765 unset($field['name']);
766
767 if (array_key_exists($name, $expFields)) {
768 foreach ($doNotCopy as $dnc) {
769 // unset the values we don't want to be copied.
770 unset($expFields[$name][$dnc]);
771 }
772 if (empty($field)) {
773 $this->_columns[$tableName][$fieldGrp][$fieldName] = $expFields[$name];
774 }
775 else {
776 foreach ($expFields[$name] as $property => $val) {
777 if (!array_key_exists($property, $field)) {
778 $this->_columns[$tableName][$fieldGrp][$fieldName][$property] = $val;
779 }
780 }
781 }
782 }
783
784 // fill other vars
a7488080 785 if (!empty($field['no_repeat'])) {
6a488035
TO
786 $this->_noRepeats[] = "{$tableName}_{$fieldName}";
787 }
a7488080 788 if (!empty($field['no_display'])) {
6a488035
TO
789 $this->_noDisplay[] = "{$tableName}_{$fieldName}";
790 }
791
792 // set alias = table-name, unless already set
971d41b1
CW
793 $alias = isset($field['alias']) ? $field['alias'] : (
794 isset($this->_columns[$tableName]['alias']) ? $this->_columns[$tableName]['alias'] : $tableName
6a488035
TO
795 );
796 $this->_columns[$tableName][$fieldGrp][$fieldName]['alias'] = $alias;
797
798 // set name = fieldName, unless already set
799 if (!isset($this->_columns[$tableName][$fieldGrp][$fieldName]['name'])) {
800 $this->_columns[$tableName][$fieldGrp][$fieldName]['name'] = $name;
801 }
802
81a22d3d 803 if (!isset($this->_columns[$tableName][$fieldGrp][$fieldName]['table_name'])) {
804 $this->_columns[$tableName][$fieldGrp][$fieldName]['table_name'] = $tableName;
805 }
806
6a488035
TO
807 // set dbAlias = alias.name, unless already set
808 if (!isset($this->_columns[$tableName][$fieldGrp][$fieldName]['dbAlias'])) {
971d41b1
CW
809 $this->_columns[$tableName][$fieldGrp][$fieldName]['dbAlias']
810 = $alias . '.' .
9d72cede 811 $this->_columns[$tableName][$fieldGrp][$fieldName]['name'];
6a488035
TO
812 }
813
c75e90fa 814 // a few auto fills for filters
c93f6d83 815 if ($fieldGrp == 'filters') {
c75e90fa
DS
816 // fill operator types
817 if (!array_key_exists('operatorType', $this->_columns[$tableName][$fieldGrp][$fieldName])) {
818 switch (CRM_Utils_Array::value('type', $this->_columns[$tableName][$fieldGrp][$fieldName])) {
819 case CRM_Utils_Type::T_MONEY:
820 case CRM_Utils_Type::T_FLOAT:
821 $this->_columns[$tableName][$fieldGrp][$fieldName]['operatorType'] = CRM_Report_Form::OP_FLOAT;
822 break;
ea100cb5 823
c75e90fa
DS
824 case CRM_Utils_Type::T_INT:
825 $this->_columns[$tableName][$fieldGrp][$fieldName]['operatorType'] = CRM_Report_Form::OP_INT;
826 break;
ea100cb5 827
c75e90fa 828 case CRM_Utils_Type::T_DATE:
c93f6d83 829 $this->_columns[$tableName][$fieldGrp][$fieldName]['operatorType'] = CRM_Report_Form::OP_DATE;
c75e90fa 830 break;
ea100cb5 831
c75e90fa
DS
832 case CRM_Utils_Type::T_BOOLEAN:
833 $this->_columns[$tableName][$fieldGrp][$fieldName]['operatorType'] = CRM_Report_Form::OP_SELECT;
834 if (!array_key_exists('options', $this->_columns[$tableName][$fieldGrp][$fieldName])) {
971d41b1 835 $this->_columns[$tableName][$fieldGrp][$fieldName]['options']
be2fb01f 836 = [
9d72cede
EM
837 '' => ts('Any'),
838 '0' => ts('No'),
21dfd5f5 839 '1' => ts('Yes'),
be2fb01f 840 ];
c75e90fa
DS
841 }
842 break;
ea100cb5 843
c75e90fa 844 default:
c93f6d83 845 if ($daoOrBaoName &&
9d72cede
EM
846 array_key_exists('pseudoconstant', $this->_columns[$tableName][$fieldGrp][$fieldName])
847 ) {
c75e90fa
DS
848 // with multiple options operator-type is generally multi-select
849 $this->_columns[$tableName][$fieldGrp][$fieldName]['operatorType'] = CRM_Report_Form::OP_MULTISELECT;
850 if (!array_key_exists('options', $this->_columns[$tableName][$fieldGrp][$fieldName])) {
851 // fill options
852 $this->_columns[$tableName][$fieldGrp][$fieldName]['options'] = CRM_Core_PseudoConstant::get($daoOrBaoName, $fieldName);
853 }
854 }
855 break;
856 }
6a488035
TO
857 }
858 }
534c4d20 859 if (!isset($this->_columns[$tableName]['metadata'][$fieldName])) {
860 $this->_columns[$tableName]['metadata'][$fieldName] = $this->_columns[$tableName][$fieldGrp][$fieldName];
861 }
59b19369
JJ
862 else {
863 $this->_columns[$tableName]['metadata'][$fieldName] = array_merge($this->_columns[$tableName][$fieldGrp][$fieldName], $this->_columns[$tableName]['metadata'][$fieldName]);
864 }
6a488035
TO
865 }
866 }
867 }
868
869 // copy filters to a separate handy variable
870 if (array_key_exists('filters', $table)) {
871 $this->_filters[$tableName] = $this->_columns[$tableName]['filters'];
872 }
873
874 if (array_key_exists('group_bys', $table)) {
875 $groupBys[$tableName] = $this->_columns[$tableName]['group_bys'];
876 }
877
878 if (array_key_exists('fields', $table)) {
879 $reportFields[$tableName] = $this->_columns[$tableName]['fields'];
880 }
881 }
882
883 if ($this->_force) {
884 $this->setDefaultValues(FALSE);
885 }
886
8f1445ea
DL
887 CRM_Report_Utils_Get::processFilter($this->_filters, $this->_defaults);
888 CRM_Report_Utils_Get::processGroupBy($groupBys, $this->_defaults);
889 CRM_Report_Utils_Get::processFields($reportFields, $this->_defaults);
6a488035
TO
890 CRM_Report_Utils_Get::processChart($this->_defaults);
891
892 if ($this->_force) {
893 $this->_formValues = $this->_defaults;
894 $this->postProcess();
895 }
896 }
897
74cf4551 898 /**
7d7c50f9
EM
899 * Set default values.
900 *
74cf4551
EM
901 * @param bool $freeze
902 *
903 * @return array
904 */
00be9182 905 public function setDefaultValues($freeze = TRUE) {
be2fb01f 906 $freezeGroup = [];
6a488035
TO
907
908 // FIXME: generalizing form field naming conventions would reduce
7d7c50f9 909 // Lots of lines below.
6a488035
TO
910 foreach ($this->_columns as $tableName => $table) {
911 if (array_key_exists('fields', $table)) {
912 foreach ($table['fields'] as $fieldName => $field) {
a7488080 913 if (empty($field['no_display'])) {
094ab048 914 if (!empty($field['required'])) {
6a488035
TO
915 // set default
916 $this->_defaults['fields'][$fieldName] = 1;
917
918 if ($freeze) {
919 // find element object, so that we could use quickform's freeze method
920 // for required elements
8f1445ea 921 $obj = $this->getElementFromGroup("fields", $fieldName);
6a488035
TO
922 if ($obj) {
923 $freezeGroup[] = $obj;
924 }
925 }
926 }
927 elseif (isset($field['default'])) {
928 $this->_defaults['fields'][$fieldName] = $field['default'];
929 }
930 }
931 }
932 }
933
934 if (array_key_exists('group_bys', $table)) {
935 foreach ($table['group_bys'] as $fieldName => $field) {
936 if (isset($field['default'])) {
a7488080 937 if (!empty($field['frequency'])) {
6a488035
TO
938 $this->_defaults['group_bys_freq'][$fieldName] = 'MONTH';
939 }
940 $this->_defaults['group_bys'][$fieldName] = $field['default'];
941 }
942 }
943 }
944 if (array_key_exists('filters', $table)) {
945 foreach ($table['filters'] as $fieldName => $field) {
946 if (isset($field['default'])) {
9d72cede
EM
947 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE
948 ) {
949 if (is_array($field['default'])) {
b0e34e7c 950 $this->_defaults["{$fieldName}_from"] = CRM_Utils_Array::value('from', $field['default']);
951 $this->_defaults["{$fieldName}_to"] = CRM_Utils_Array::value('to', $field['default']);
952 $this->_defaults["{$fieldName}_relative"] = 0;
953 }
9d72cede 954 else {
b0e34e7c 955 $this->_defaults["{$fieldName}_relative"] = $field['default'];
956 }
6a488035
TO
957 }
958 else {
2edb4e81 959 if ((CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_INT) && is_array($field['default'])) {
960 $this->_defaults["{$fieldName}_min"] = CRM_Utils_Array::value('min', $field['default']);
961 $this->_defaults["{$fieldName}_max"] = CRM_Utils_Array::value('max', $field['default']);
962 }
6a488035
TO
963 $this->_defaults["{$fieldName}_value"] = $field['default'];
964 }
965 }
966 //assign default value as "in" for multiselect
967 //operator, To freeze the select element
9d72cede
EM
968 if (CRM_Utils_Array::value('operatorType', $field) ==
969 CRM_Report_Form::OP_MULTISELECT
970 ) {
6a488035
TO
971 $this->_defaults["{$fieldName}_op"] = 'in';
972 }
9d72cede
EM
973 if (CRM_Utils_Array::value('operatorType', $field) ==
974 CRM_Report_Form::OP_ENTITYREF
975 ) {
2107cde9
CW
976 $this->_defaults["{$fieldName}_op"] = 'in';
977 }
9d72cede
EM
978 elseif (CRM_Utils_Array::value('operatorType', $field) ==
979 CRM_Report_Form::OP_MULTISELECT_SEPARATOR
980 ) {
6a488035
TO
981 $this->_defaults["{$fieldName}_op"] = 'mhas';
982 }
983 elseif ($op = CRM_Utils_Array::value('default_op', $field)) {
984 $this->_defaults["{$fieldName}_op"] = $op;
985 }
986 }
987 }
988
8f1445ea 989 if (
e76ce93e 990 empty($this->_formValues['order_bys']) &&
991 (array_key_exists('order_bys', $table) &&
992 is_array($table['order_bys']))
6a488035 993 ) {
6a488035 994 if (!array_key_exists('order_bys', $this->_defaults)) {
be2fb01f 995 $this->_defaults['order_bys'] = [];
6a488035
TO
996 }
997 foreach ($table['order_bys'] as $fieldName => $field) {
8cc574cf 998 if (!empty($field['default']) || !empty($field['default_order']) ||
9d72cede
EM
999 CRM_Utils_Array::value('default_is_section', $field) ||
1000 !empty($field['default_weight'])
1001 ) {
be2fb01f 1002 $order_by = [
6a488035
TO
1003 'column' => $fieldName,
1004 'order' => CRM_Utils_Array::value('default_order', $field, 'ASC'),
1005 'section' => CRM_Utils_Array::value('default_is_section', $field, 0),
be2fb01f 1006 ];
6a488035 1007
a7488080 1008 if (!empty($field['default_weight'])) {
6a488035
TO
1009 $this->_defaults['order_bys'][(int) $field['default_weight']] = $order_by;
1010 }
1011 else {
1012 array_unshift($this->_defaults['order_bys'], $order_by);
1013 }
1014 }
1015 }
1016 }
1017
1018 foreach ($this->_options as $fieldName => $field) {
1019 if (isset($field['default'])) {
1020 $this->_defaults['options'][$fieldName] = $field['default'];
1021 }
1022 }
1023 }
1024
1025 if (!empty($this->_submitValues)) {
1026 $this->preProcessOrderBy($this->_submitValues);
1027 }
1028 else {
1029 $this->preProcessOrderBy($this->_defaults);
1030 }
1031
1032 // lets finish freezing task here itself
1033 if (!empty($freezeGroup)) {
1034 foreach ($freezeGroup as $elem) {
1035 $elem->freeze();
1036 }
1037 }
1038
1039 if ($this->_formValues) {
1040 $this->_defaults = array_merge($this->_defaults, $this->_formValues);
1041 }
1042
1043 if ($this->_instanceValues) {
1044 $this->_defaults = array_merge($this->_defaults, $this->_instanceValues);
1045 }
1046
1047 CRM_Report_Form_Instance::setDefaultValues($this, $this->_defaults);
1048
1049 return $this->_defaults;
1050 }
1051
74cf4551 1052 /**
7d7c50f9
EM
1053 * Get element from group.
1054 *
8a925f33 1055 * @param string $group
100fef9d 1056 * @param string $grpFieldName
74cf4551
EM
1057 *
1058 * @return bool
1059 */
00be9182 1060 public function getElementFromGroup($group, $grpFieldName) {
6a488035
TO
1061 $eleObj = $this->getElement($group);
1062 foreach ($eleObj->_elements as $index => $obj) {
1063 if ($grpFieldName == $obj->_attributes['name']) {
1064 return $obj;
1065 }
1066 }
1067 return FALSE;
1068 }
1069
c58f66e0 1070 /**
7d7c50f9 1071 * Setter for $_params.
9d72cede 1072 *
c58f66e0
E
1073 * @param array $params
1074 */
00be9182 1075 public function setParams($params) {
c58f66e0
E
1076 $this->_params = $params;
1077 }
1078
8fc33241 1079 /**
1080 * Getter for $_params.
1081 *
1082 * @return void|array $params
1083 */
1084 public function getParams() {
1085 return $this->_params;
1086 }
1087
c58f66e0 1088 /**
0b62c1ab 1089 * Setter for $_id.
2a6da8d7 1090 *
537c70b8 1091 * @param int $instanceID
c58f66e0 1092 */
537c70b8
EM
1093 public function setID($instanceID) {
1094 $this->_id = $instanceID;
c58f66e0
E
1095 }
1096
1097 /**
07f44165 1098 * Setter for $_force.
9d72cede 1099 *
317a8023 1100 * @param bool $isForce
9d72cede 1101 */
00be9182 1102 public function setForce($isForce) {
c58f66e0
E
1103 $this->_force = $isForce;
1104 }
6f900755
E
1105
1106 /**
7d7c50f9 1107 * Setter for $_limitValue.
9d72cede 1108 *
d3e86119 1109 * @param int $_limitValue
6f900755 1110 */
00be9182 1111 public function setLimitValue($_limitValue) {
6f900755
E
1112 $this->_limitValue = $_limitValue;
1113 }
1114
1115 /**
7d7c50f9 1116 * Setter for $_offsetValue.
9d72cede 1117 *
d3e86119 1118 * @param int $_offsetValue
6f900755 1119 */
00be9182 1120 public function setOffsetValue($_offsetValue) {
6f900755
E
1121 $this->_offsetValue = $_offsetValue;
1122 }
1123
182f5081 1124 /**
1125 * Setter for $addPaging.
1126 *
1127 * @param bool $value
1128 */
1129 public function setAddPaging($value) {
1130 $this->addPaging = $value;
1131 }
1132
c58f66e0 1133 /**
7d7c50f9
EM
1134 * Getter for $_defaultValues.
1135 *
a6c01b45 1136 * @return array
c58f66e0 1137 */
00be9182 1138 public function getDefaultValues() {
c58f66e0
E
1139 return $this->_defaults;
1140 }
1141
e6bab5ea 1142 /**
1143 * Remove any temporary tables.
1144 */
1145 public function cleanUpTemporaryTables() {
1146 foreach ($this->temporaryTables as $temporaryTable) {
1147 CRM_Core_DAO::executeQuery('DROP ' . ($temporaryTable['temporary'] ? 'TEMPORARY' : '') . ' TABLE IF EXISTS ' . $temporaryTable['name']);
1148 }
1149 }
1150
5a14305b 1151 /**
1152 * Create a temporary table.
1153 *
1154 * This function creates a table AND adds the details to the developer tab & $this->>temporary tables.
1155 *
5a14305b 1156 * @param string $identifier
03843223 1157 * This is the key that will be used for the table in the temporaryTables property.
1158 * @param string $sql
1159 * Sql select statement or column description (the latter requires the columns flag)
1160 * @param bool $isColumns
1161 * Is the sql describing columns to create (rather than using a select query).
1162 * @param bool $isMemory
1163 * Create a memory table rather than a normal INNODB table.
5a14305b 1164 *
1165 * @return string
1166 */
03843223 1167 public function createTemporaryTable($identifier, $sql, $isColumns = FALSE, $isMemory = FALSE) {
0033aaaf 1168 $tempTable = CRM_Utils_SQL_TempTable::build();
03843223 1169 if ($isMemory) {
1170 $tempTable->setMemory();
1171 }
1172 if ($isColumns) {
1173 $tempTable->createWithColumns($sql);
1174 }
1175 else {
1176 $tempTable->createWithQuery($sql);
1177 }
f0197a3d 1178 $name = $tempTable->getName();
1179 // Developers may force tables to be durable to assist in debugging so lets check.
1180 $isNotTrueTemporary = $tempTable->isDurable();
03843223 1181 $this->addToDeveloperTab($tempTable->getCreateSql());
f0197a3d 1182 $this->temporaryTables[$identifier] = ['temporary' => !$isNotTrueTemporary, 'name' => $name];
5a14305b 1183 return $name;
1184 }
1185
7d7c50f9
EM
1186 /**
1187 * Add columns to report.
1188 */
00be9182 1189 public function addColumns() {
be2fb01f 1190 $options = [];
6a488035
TO
1191 $colGroups = NULL;
1192 foreach ($this->_columns as $tableName => $table) {
1193 if (array_key_exists('fields', $table)) {
1194 foreach ($table['fields'] as $fieldName => $field) {
8bdc861c 1195 $groupTitle = '';
a7488080 1196 if (empty($field['no_display'])) {
be2fb01f 1197 foreach (['table', 'field'] as $var) {
8bdc861c
DS
1198 if (!empty(${$var}['grouping'])) {
1199 if (!is_array(${$var}['grouping'])) {
1200 $tableName = ${$var}['grouping'];
9d72cede
EM
1201 }
1202 else {
b80138e0
DS
1203 $tableName = array_keys(${$var}['grouping']);
1204 $tableName = $tableName[0];
1205 $groupTitle = array_values(${$var}['grouping']);
1206 $groupTitle = $groupTitle[0];
8bdc861c
DS
1207 }
1208 }
6a488035 1209 }
8bdc861c
DS
1210
1211 if (!$groupTitle && isset($table['group_title'])) {
1212 $groupTitle = $table['group_title'];
6df2d404 1213 // Having a group_title is secret code for being a custom group
b44e3f84 1214 // which cryptically translates to needing an accordion.
6df2d404
EM
1215 // here we make that explicit.
1216 $colGroups[$tableName]['use_accordian_for_field_selection'] = TRUE;
6a488035 1217 }
6a488035 1218
8bdc861c 1219 $colGroups[$tableName]['fields'][$fieldName] = CRM_Utils_Array::value('title', $field);
8cc574cf 1220 if ($groupTitle && empty($colGroups[$tableName]['group_title'])) {
8bdc861c 1221 $colGroups[$tableName]['group_title'] = $groupTitle;
6a488035 1222 }
6a488035
TO
1223 $options[$fieldName] = CRM_Utils_Array::value('title', $field);
1224 }
1225 }
1226 }
1227 }
1228
1229 $this->addCheckBox("fields", ts('Select Columns'), $options, NULL,
1230 NULL, NULL, NULL, $this->_fourColumnAttribute, TRUE
1231 );
0b62c1ab 1232 if (!empty($colGroups)) {
be2fb01f 1233 $this->tabs['FieldSelection'] = [
0b62c1ab
EM
1234 'title' => ts('Columns'),
1235 'tpl' => 'FieldSelection',
1236 'div_label' => 'col-groups',
be2fb01f 1237 ];
0b62c1ab
EM
1238
1239 // Note this assignment is only really required in buildForm. It is being 'over-called'
1240 // to reduce risk of being missed due to overridden functions.
1241 $this->assign('tabs', $this->tabs);
1242 }
1243
6a488035
TO
1244 $this->assign('colGroups', $colGroups);
1245 }
1246
50951061
EM
1247 /**
1248 * Add filters to report.
1249 */
00be9182 1250 public function addFilters() {
be2fb01f 1251 $filters = $filterGroups = [];
6a488035 1252 $count = 1;
69719a15 1253
6a488035 1254 foreach ($this->_filters as $table => $attributes) {
69719a15
EM
1255 if (isset($this->_columns[$table]['group_title'])) {
1256 // The presence of 'group_title' is secret code for 'is_a_custom_table'
1257 // which magically means to 'display in an accordian'
1258 // here we make this explicit.
be2fb01f 1259 $filterGroups[$table] = [
69719a15
EM
1260 'group_title' => $this->_columns[$table]['group_title'],
1261 'use_accordian_for_field_selection' => TRUE,
1262
be2fb01f 1263 ];
69719a15 1264 }
6a488035
TO
1265 foreach ($attributes as $fieldName => $field) {
1266 // get ready with option value pair
1b36206c 1267 // @ todo being able to specific options for a field (e.g a date field) in the field spec as an array rather than an override
1268 // would be useful
1269 $operations = $this->getOperationPair(
8f1445ea 1270 CRM_Utils_Array::value('operatorType', $field),
1b36206c 1271 $fieldName);
6a488035
TO
1272
1273 $filters[$table][$fieldName] = $field;
1274
1275 switch (CRM_Utils_Array::value('operatorType', $field)) {
1276 case CRM_Report_Form::OP_MONTH:
9d72cede
EM
1277 if (!array_key_exists('options', $field) ||
1278 !is_array($field['options']) || empty($field['options'])
1279 ) {
6a488035 1280 // If there's no option list for this filter, define one.
be2fb01f 1281 $field['options'] = [
6a488035
TO
1282 1 => ts('January'),
1283 2 => ts('February'),
1284 3 => ts('March'),
1285 4 => ts('April'),
1286 5 => ts('May'),
1287 6 => ts('June'),
1288 7 => ts('July'),
1289 8 => ts('August'),
1290 9 => ts('September'),
1291 10 => ts('October'),
1292 11 => ts('November'),
1293 12 => ts('December'),
be2fb01f 1294 ];
6a488035
TO
1295 // Add this option list to this column _columns. This is
1296 // required so that filter statistics show properly.
1297 $this->_columns[$table]['filters'][$fieldName]['options'] = $field['options'];
1298 }
160d32e1
E
1299 case CRM_Report_Form::OP_MULTISELECT:
1300 case CRM_Report_Form::OP_MULTISELECT_SEPARATOR:
6a488035 1301 // assume a multi-select field
9d72cede
EM
1302 if (!empty($field['options']) ||
1303 $fieldName == 'state_province_id' || $fieldName == 'county_id'
1304 ) {
d59b14a6 1305 $element = $this->addElement('select', "{$fieldName}_op", ts('Operator:'), $operations,
a4818149 1306 array('onchange' => "return showHideMaxMinVal( '$fieldName', this.value );")
1307 );
1308
6a488035
TO
1309 if (count($operations) <= 1) {
1310 $element->freeze();
1311 }
9d72cede
EM
1312 if ($fieldName == 'state_province_id' ||
1313 $fieldName == 'county_id'
1314 ) {
be2fb01f 1315 $this->addChainSelect($fieldName . '_value', [
86bd39be
TO
1316 'multiple' => TRUE,
1317 'label' => NULL,
21dfd5f5 1318 'class' => 'huge',
be2fb01f 1319 ]);
c927c151
CW
1320 }
1321 else {
be2fb01f 1322 $this->addElement('select', "{$fieldName}_value", NULL, $field['options'], [
c927c151 1323 'style' => 'min-width:250px',
2107cde9 1324 'class' => 'crm-select2 huge',
c927c151
CW
1325 'multiple' => TRUE,
1326 'placeholder' => ts('- select -'),
be2fb01f 1327 ]);
c927c151 1328 }
6a488035
TO
1329 }
1330 break;
1331
160d32e1 1332 case CRM_Report_Form::OP_SELECT:
6a488035
TO
1333 // assume a select field
1334 $this->addElement('select', "{$fieldName}_op", ts('Operator:'), $operations);
9d72cede 1335 if (!empty($field['options'])) {
5a9a44d9 1336 $this->addElement('select', "{$fieldName}_value", NULL, $field['options']);
9d72cede 1337 }
6a488035
TO
1338 break;
1339
2107cde9
CW
1340 case CRM_Report_Form::OP_ENTITYREF:
1341 $this->addElement('select', "{$fieldName}_op", ts('Operator:'), $operations);
1342 $this->setEntityRefDefaults($field, $table);
1343 $this->addEntityRef("{$fieldName}_value", NULL, $field['attributes']);
1344 break;
1345
160d32e1 1346 case CRM_Report_Form::OP_DATE:
6a488035 1347 // build datetime fields
ccc29f8e 1348 CRM_Core_Form_Date::buildDateRange($this, $fieldName, $count, '_from', '_to', ts('From:'), FALSE, $operations);
6a488035
TO
1349 $count++;
1350 break;
1351
160d32e1 1352 case CRM_Report_Form::OP_DATETIME:
6a488035 1353 // build datetime fields
ccc29f8e 1354 CRM_Core_Form_Date::buildDateRange($this, $fieldName, $count, '_from', '_to', ts('From:'), FALSE, $operations, 'searchDate', TRUE);
6a488035
TO
1355 $count++;
1356 break;
1357
160d32e1
E
1358 case CRM_Report_Form::OP_INT:
1359 case CRM_Report_Form::OP_FLOAT:
6a488035
TO
1360 // and a min value input box
1361 $this->add('text', "{$fieldName}_min", ts('Min'));
1362 // and a max value input box
1363 $this->add('text', "{$fieldName}_max", ts('Max'));
1364 default:
1365 // default type is string
1366 $this->addElement('select', "{$fieldName}_op", ts('Operator:'), $operations,
be2fb01f 1367 ['onchange' => "return showHideMaxMinVal( '$fieldName', this.value );"]
6a488035
TO
1368 );
1369 // we need text box for value input
be2fb01f 1370 $this->add('text', "{$fieldName}_value", NULL, ['class' => 'huge']);
6a488035
TO
1371 break;
1372 }
1373 }
1374 }
0b62c1ab 1375 if (!empty($filters)) {
be2fb01f 1376 $this->tabs['Filters'] = [
0b62c1ab
EM
1377 'title' => ts('Filters'),
1378 'tpl' => 'Filters',
1379 'div_label' => 'set-filters',
be2fb01f 1380 ];
0b62c1ab 1381 }
6a488035 1382 $this->assign('filters', $filters);
69719a15 1383 $this->assign('filterGroups', $filterGroups);
6a488035
TO
1384 }
1385
0b62c1ab
EM
1386 /**
1387 * Function to assign the tabs to the template in the correct order.
1388 *
1389 * We want the tabs to wind up in this order (if not overridden).
1390 *
1391 * - Field Selection
1392 * - Group Bys
1393 * - Order Bys
1394 * - Other Options
1395 * - Filters
1396 */
1397 protected function assignTabs() {
be2fb01f 1398 $order = [
0b62c1ab
EM
1399 'FieldSelection',
1400 'GroupBy',
1401 'OrderBy',
1402 'ReportOptions',
1403 'Filters',
be2fb01f 1404 ];
0b62c1ab
EM
1405 $order = array_intersect_key(array_fill_keys($order, 1), $this->tabs);
1406 $order = array_merge($order, $this->tabs);
1407 $this->assign('tabs', $order);
1408 }
1409
02d451ab
EM
1410 /**
1411 * The intent is to add a tab for developers to view the sql.
1412 *
1413 * Currently using dpm.
1414 *
1415 * @param string $sql
1416 */
15d9e604 1417 public function addToDeveloperTab($sql) {
02d451ab
EM
1418 if (!CRM_Core_Permission::check('view report sql')) {
1419 return;
1420 }
be2fb01f 1421 $ignored_output_modes = ['pdf', 'csv', 'print'];
e7c9c0ff
JM
1422 if (in_array($this->_outputMode, $ignored_output_modes)) {
1423 return;
1424 }
be2fb01f 1425 $this->tabs['Developer'] = [
02d451ab
EM
1426 'title' => ts('Developer'),
1427 'tpl' => 'Developer',
1428 'div_label' => 'set-developer',
be2fb01f 1429 ];
02d451ab
EM
1430
1431 $this->assignTabs();
55f71fa7 1432 $this->sqlArray[] = $sql;
43c1fa19 1433 foreach ($this->sqlArray as $sql) {
be2fb01f 1434 foreach (['LEFT JOIN'] as $term) {
fe77ced7 1435 $sql = str_replace($term, '<br> ' . $term, $sql);
43c1fa19 1436 }
be2fb01f 1437 foreach (['FROM', 'WHERE', 'GROUP BY', 'ORDER BY', 'LIMIT', ';'] as $term) {
43c1fa19 1438 $sql = str_replace($term, '<br><br>' . $term, $sql);
1439 }
1440 $this->sqlFormattedArray[] = $sql;
1441 $this->assign('sql', implode(';<br><br><br><br>', $this->sqlFormattedArray));
02d451ab 1442 }
911eb44f 1443 $this->assign('sqlModes', $sqlModes = CRM_Utils_SQL::getSqlModes());
1444
02d451ab
EM
1445 }
1446
0b62c1ab 1447 /**
50951061
EM
1448 * Add options defined in $this->_options to the report.
1449 */
00be9182 1450 public function addOptions() {
6a488035
TO
1451 if (!empty($this->_options)) {
1452 // FIXME: For now lets build all elements as checkboxes.
1453 // Once we clear with the format we can build elements based on type
1454
6a488035 1455 foreach ($this->_options as $fieldName => $field) {
be2fb01f 1456 $options = [];
52b41f2f 1457
6a488035
TO
1458 if ($field['type'] == 'select') {
1459 $this->addElement('select', "{$fieldName}", $field['title'], $field['options']);
1460 }
4c9b6178 1461 elseif ($field['type'] == 'checkbox') {
6a488035 1462 $options[$field['title']] = $fieldName;
52634dad
DS
1463 $this->addCheckBox($fieldName, NULL,
1464 $options, NULL,
1465 NULL, NULL, NULL, $this->_fourColumnAttribute
1466 );
6a488035
TO
1467 }
1468 }
6a488035 1469 }
0f8c6e58 1470 if (!empty($this->_options) &&
1471 (!$this->_id
1472 || ($this->_id && CRM_Report_BAO_ReportInstance::contactCanAdministerReport($this->_id)))
1473 ) {
be2fb01f 1474 $this->tabs['ReportOptions'] = [
0b62c1ab
EM
1475 'title' => ts('Display Options'),
1476 'tpl' => 'ReportOptions',
1477 'div_label' => 'other-options',
be2fb01f 1478 ];
0b62c1ab 1479 }
52634dad 1480 $this->assign('otherOptions', $this->_options);
6a488035
TO
1481 }
1482
50951061
EM
1483 /**
1484 * Add chart options to the report.
1485 */
00be9182 1486 public function addChartOptions() {
6a488035 1487 if (!empty($this->_charts)) {
22b67281 1488 $this->addElement('select', "charts", ts('Chart'), $this->_charts);
6a488035
TO
1489 $this->assign('charts', $this->_charts);
1490 $this->addElement('submit', $this->_chartButtonName, ts('View'));
1491 }
1492 }
1493
50951061
EM
1494 /**
1495 * Add group by options to the report.
1496 */
00be9182 1497 public function addGroupBys() {
be2fb01f 1498 $options = $freqElements = [];
6a488035
TO
1499
1500 foreach ($this->_columns as $tableName => $table) {
1501 if (array_key_exists('group_bys', $table)) {
1502 foreach ($table['group_bys'] as $fieldName => $field) {
c3fdd2b7 1503 if (!empty($field) && empty($field['no_display'])) {
6a488035 1504 $options[$field['title']] = $fieldName;
a7488080 1505 if (!empty($field['frequency'])) {
6a488035
TO
1506 $freqElements[$field['title']] = $fieldName;
1507 }
1508 }
1509 }
1510 }
1511 }
1512 $this->addCheckBox("group_bys", ts('Group by columns'), $options, NULL,
1513 NULL, NULL, NULL, $this->_fourColumnAttribute
1514 );
1515 $this->assign('groupByElements', $options);
0b62c1ab 1516 if (!empty($options)) {
be2fb01f 1517 $this->tabs['GroupBy'] = [
0b62c1ab
EM
1518 'title' => ts('Grouping'),
1519 'tpl' => 'GroupBy',
1520 'div_label' => 'group-by-elements',
be2fb01f 1521 ];
0b62c1ab 1522 }
6a488035
TO
1523
1524 foreach ($freqElements as $name) {
1525 $this->addElement('select', "group_bys_freq[$name]",
1526 ts('Frequency'), $this->_groupByDateFreq
1527 );
1528 }
1529 }
1530
0b62c1ab
EM
1531 /**
1532 * Add data for order by tab.
1533 */
00be9182 1534 public function addOrderBys() {
be2fb01f 1535 $options = [];
6a488035
TO
1536 foreach ($this->_columns as $tableName => $table) {
1537
0b62c1ab 1538 // Report developer may define any column to order by; include these as order-by options.
6a488035
TO
1539 if (array_key_exists('order_bys', $table)) {
1540 foreach ($table['order_bys'] as $fieldName => $field) {
1541 if (!empty($field)) {
1542 $options[$fieldName] = $field['title'];
1543 }
1544 }
1545 }
1546
971d41b1
CW
1547 // Add searchable custom fields as order-by options, if so requested
1548 // (These are already indexed, so allowing to order on them is cheap.)
6a488035 1549
9d72cede
EM
1550 if ($this->_autoIncludeIndexedFieldsAsOrderBys &&
1551 array_key_exists('extends', $table) && !empty($table['extends'])
1552 ) {
6a488035 1553 foreach ($table['fields'] as $fieldName => $field) {
a7488080 1554 if (empty($field['no_display'])) {
6a488035
TO
1555 $options[$fieldName] = $field['title'];
1556 }
1557 }
1558 }
1559 }
1560
1561 asort($options);
1562
1563 $this->assign('orderByOptions', $options);
0b62c1ab 1564 if (!empty($options)) {
be2fb01f 1565 $this->tabs['OrderBy'] = [
0b62c1ab
EM
1566 'title' => ts('Sorting'),
1567 'tpl' => 'OrderBy',
cd732070 1568 'div_label' => 'order-by-elements',
be2fb01f 1569 ];
0b62c1ab 1570 }
6a488035
TO
1571
1572 if (!empty($options)) {
be2fb01f 1573 $options = [
971d41b1 1574 '-' => ' - none - ',
be2fb01f 1575 ] + $options;
6a488035
TO
1576 for ($i = 1; $i <= 5; $i++) {
1577 $this->addElement('select', "order_bys[{$i}][column]", ts('Order by Column'), $options);
be2fb01f 1578 $this->addElement('select', "order_bys[{$i}][order]", ts('Order by Order'), [
ccc29f8e 1579 'ASC' => ts('Ascending'),
1580 'DESC' => ts('Descending'),
be2fb01f
CW
1581 ]);
1582 $this->addElement('checkbox', "order_bys[{$i}][section]", ts('Order by Section'), FALSE, ['id' => "order_by_section_$i"]);
1583 $this->addElement('checkbox', "order_bys[{$i}][pageBreak]", ts('Page Break'), FALSE, ['id' => "order_by_pagebreak_$i"]);
6a488035
TO
1584 }
1585 }
1586 }
1587
0b62c1ab 1588 /**
07f44165 1589 * This adds the tab referred to as Title and Format, rendered through Instance.tpl.
0b62c1ab 1590 *
07f44165
EM
1591 * @todo call this tab into the report template in the same way as OrderBy etc, ie
1592 * by adding a description of the tab to $this->tabs, causing the tab to be added in
1593 * Criteria.tpl.
0b62c1ab 1594 */
00be9182 1595 public function buildInstanceAndButtons() {
6a488035 1596 CRM_Report_Form_Instance::buildForm($this);
87ecd5b7 1597 $this->_actionButtonName = $this->getButtonName('submit');
1598 $this->addTaskMenu($this->getActions($this->_id));
6a488035 1599
87ecd5b7 1600 $this->assign('instanceForm', $this->_instanceForm);
6a488035 1601
8a5c4609
DG
1602 // CRM-16274 Determine if user has 'edit all contacts' or equivalent
1603 $permission = CRM_Core_Permission::getPermission();
db1a2573 1604 if ($this->_instanceForm && $permission == CRM_Core_Permission::EDIT &&
9d72cede
EM
1605 $this->_add2groupSupported
1606 ) {
6a488035 1607 $this->addElement('select', 'groups', ts('Group'),
be2fb01f 1608 ['' => ts('Add Contacts to Group')] +
9d72cede 1609 CRM_Core_PseudoConstant::nestedGroup(),
be2fb01f 1610 ['class' => 'crm-select2 crm-action-menu fa-plus huge']
6a488035
TO
1611 );
1612 $this->assign('group', TRUE);
1613 }
1614
be2fb01f 1615 $this->addElement('submit', $this->_groupButtonName, '', ['style' => 'display: none;']);
6a488035
TO
1616
1617 $this->addChartOptions();
87ecd5b7 1618 $showResultsLabel = $this->getResultsLabel();
be2fb01f
CW
1619 $this->addButtons([
1620 [
6a488035 1621 'type' => 'submit',
87ecd5b7 1622 'name' => $showResultsLabel,
6a488035 1623 'isDefault' => TRUE,
be2fb01f 1624 ],
c86d4e7c 1625 ]);
6a488035
TO
1626 }
1627
87ecd5b7 1628 /**
1629 * Has this form been submitted already?
1630 *
1631 * @return bool
1632 */
1633 public function resultsDisplayed() {
1634 $buttonName = $this->controller->getButtonName();
1635 return ($buttonName || $this->_outputMode);
1636 }
1637
1638 /**
1639 * Get the actions for this report instance.
1640 *
1641 * @param int $instanceId
1642 *
1643 * @return array
1644 */
1645 protected function getActions($instanceId) {
44543184 1646 $actions = CRM_Report_BAO_ReportInstance::getActionMetadata();
87ecd5b7 1647 if (empty($instanceId)) {
be2fb01f 1648 $actions['report_instance.save'] = [
d0c1b07a 1649 'title' => ts('Create Report'),
be2fb01f 1650 'data' => [
d0c1b07a 1651 'is_confirm' => TRUE,
1652 'confirm_title' => ts('Create Report'),
be2fb01f
CW
1653 'confirm_refresh_fields' => json_encode([
1654 'title' => ['selector' => '.crm-report-instanceForm-form-block-title', 'prepend' => ''],
1655 'description' => ['selector' => '.crm-report-instanceForm-form-block-description', 'prepend' => ''],
1656 ]),
1657 ],
1658 ];
3f501e5e 1659 unset($actions['report_instance.delete']);
87ecd5b7 1660 }
1661
44543184 1662 if (!$this->_csvSupported) {
1663 unset($actions['report_instance.csv']);
87ecd5b7 1664 }
1665
1666 return $actions;
1667 }
1668
0b62c1ab
EM
1669 /**
1670 * Main build form function.
1671 */
00be9182 1672 public function buildQuickForm() {
6a488035
TO
1673 $this->addColumns();
1674
1675 $this->addFilters();
1676
1677 $this->addOptions();
1678
1679 $this->addGroupBys();
1680
1681 $this->addOrderBys();
1682
1683 $this->buildInstanceAndButtons();
1684
0b62c1ab 1685 // Add form rule for report.
be2fb01f 1686 if (is_callable([
9d72cede 1687 $this,
21dfd5f5 1688 'formRule',
be2fb01f
CW
1689 ])) {
1690 $this->addFormRule([get_class($this), 'formRule'], $this);
6a488035 1691 }
0b62c1ab 1692 $this->assignTabs();
6a488035
TO
1693 }
1694
74cf4551 1695 /**
317a8023 1696 * A form rule function for custom data.
1697 *
1698 * The rule ensures that fields selected in group_by if any) should only be the ones
1699 * present in display/select fields criteria;
4f1f1f2a 1700 * note: works if and only if any custom field selected in group_by.
0b62c1ab 1701 *
537c70b8 1702 * @param array $fields
74cf4551
EM
1703 * @param array $ignoreFields
1704 *
1705 * @return array
1706 */
be2fb01f
CW
1707 public function customDataFormRule($fields, $ignoreFields = []) {
1708 $errors = [];
9d72cede
EM
1709 if (!empty($this->_customGroupExtends) && $this->_customGroupGroupBy &&
1710 !empty($fields['group_bys'])
1711 ) {
6a488035 1712 foreach ($this->_columns as $tableName => $table) {
9d72cede
EM
1713 if ((substr($tableName, 0, 13) == 'civicrm_value' ||
1714 substr($tableName, 0, 12) == 'custom_value') &&
1715 !empty($this->_columns[$tableName]['fields'])
1716 ) {
6a488035
TO
1717 foreach ($this->_columns[$tableName]['fields'] as $fieldName => $field) {
1718 if (array_key_exists($fieldName, $fields['group_bys']) &&
1719 !array_key_exists($fieldName, $fields['fields'])
1720 ) {
1721 $errors['fields'] = "Please make sure fields selected in 'Group by Columns' section are also selected in 'Display Columns' section.";
1722 }
1723 elseif (array_key_exists($fieldName, $fields['group_bys'])) {
1724 foreach ($fields['fields'] as $fld => $val) {
9d72cede
EM
1725 if (!array_key_exists($fld, $fields['group_bys']) &&
1726 !in_array($fld, $ignoreFields)
1727 ) {
6a488035
TO
1728 $errors['fields'] = "Please ensure that fields selected in 'Display Columns' are also selected in 'Group by Columns' section.";
1729 }
1730 }
1731 }
1732 }
1733 }
1734 }
1735 }
1736 return $errors;
1737 }
1738
74cf4551 1739 /**
0b62c1ab
EM
1740 * Get operators to display on form.
1741 *
1742 * Note: $fieldName param allows inheriting class to build operationPairs specific to a field.
1743 *
74cf4551 1744 * @param string $type
8a925f33 1745 * @param string $fieldName
74cf4551
EM
1746 *
1747 * @return array
1748 */
00be9182 1749 public function getOperationPair($type = "string", $fieldName = NULL) {
6a488035
TO
1750 // FIXME: At some point we should move these key-val pairs
1751 // to option_group and option_value table.
6a488035 1752 switch ($type) {
160d32e1
E
1753 case CRM_Report_Form::OP_INT:
1754 case CRM_Report_Form::OP_FLOAT:
bed98343 1755
be2fb01f 1756 $result = [
bc3f7f04 1757 'lte' => ts('Is less than or equal to'),
6a488035
TO
1758 'gte' => ts('Is greater than or equal to'),
1759 'bw' => ts('Is between'),
1760 'eq' => ts('Is equal to'),
1761 'lt' => ts('Is less than'),
1762 'gt' => ts('Is greater than'),
1763 'neq' => ts('Is not equal to'),
1764 'nbw' => ts('Is not between'),
1765 'nll' => ts('Is empty (Null)'),
1766 'nnll' => ts('Is not empty (Null)'),
be2fb01f 1767 ];
6c552737 1768 return $result;
6a488035 1769
160d32e1 1770 case CRM_Report_Form::OP_SELECT:
be2fb01f 1771 $result = [
bc3f7f04 1772 'eq' => ts('Is equal to'),
be2fb01f 1773 ];
6c552737 1774 return $result;
6a488035 1775
160d32e1
E
1776 case CRM_Report_Form::OP_MONTH:
1777 case CRM_Report_Form::OP_MULTISELECT:
2107cde9 1778 case CRM_Report_Form::OP_ENTITYREF:
bed98343 1779
be2fb01f 1780 $result = [
bc3f7f04 1781 'in' => ts('Is one of'),
6a488035 1782 'notin' => ts('Is not one of'),
a4818149 1783 'nll' => ts('Is empty (Null)'),
1784 'nnll' => ts('Is not empty (Null)'),
be2fb01f 1785 ];
6c552737 1786 return $result;
6a488035 1787
160d32e1 1788 case CRM_Report_Form::OP_DATE:
bed98343 1789
be2fb01f 1790 $result = [
bc3f7f04 1791 'nll' => ts('Is empty (Null)'),
6a488035 1792 'nnll' => ts('Is not empty (Null)'),
be2fb01f 1793 ];
6c552737 1794 return $result;
6a488035 1795
160d32e1 1796 case CRM_Report_Form::OP_MULTISELECT_SEPARATOR:
6a488035
TO
1797 // use this operator for the values, concatenated with separator. For e.g if
1798 // multiple options for a column is stored as ^A{val1}^A{val2}^A
be2fb01f 1799 $result = [
bc3f7f04 1800 'mhas' => ts('Is one of'),
67788963 1801 'mnot' => ts('Is not one of'),
be2fb01f 1802 ];
6c552737 1803 return $result;
6a488035
TO
1804
1805 default:
1806 // type is string
be2fb01f 1807 $result = [
bc3f7f04 1808 'has' => ts('Contains'),
6a488035
TO
1809 'sw' => ts('Starts with'),
1810 'ew' => ts('Ends with'),
1811 'nhas' => ts('Does not contain'),
1812 'eq' => ts('Is equal to'),
1813 'neq' => ts('Is not equal to'),
1814 'nll' => ts('Is empty (Null)'),
1815 'nnll' => ts('Is not empty (Null)'),
be2fb01f 1816 ];
6c552737 1817 return $result;
6a488035
TO
1818 }
1819 }
1820
07f44165
EM
1821 /**
1822 * Build the tag filter field to display on the filters tab.
1823 */
00be9182 1824 public function buildTagFilter() {
ed795723 1825 $contactTags = CRM_Core_BAO_Tag::getTags($this->_tagFilterTable);
6a488035 1826 if (!empty($contactTags)) {
be2fb01f 1827 $this->_columns['civicrm_tag'] = [
6a488035 1828 'dao' => 'CRM_Core_DAO_Tag',
be2fb01f
CW
1829 'filters' => [
1830 'tagid' => [
6a488035
TO
1831 'name' => 'tag_id',
1832 'title' => ts('Tag'),
8ee006e7 1833 'type' => CRM_Utils_Type::T_INT,
6a488035
TO
1834 'tag' => TRUE,
1835 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
1836 'options' => $contactTags,
be2fb01f
CW
1837 ],
1838 ],
1839 ];
6a488035
TO
1840 }
1841 }
1842
688d37c6 1843 /**
0b62c1ab 1844 * Adds group filters to _columns (called from _Construct).
6a488035 1845 */
00be9182 1846 public function buildGroupFilter() {
be2fb01f
CW
1847 $this->_columns['civicrm_group']['filters'] = [
1848 'gid' => [
6a488035
TO
1849 'name' => 'group_id',
1850 'title' => ts('Group'),
8ee006e7 1851 'type' => CRM_Utils_Type::T_INT,
6a488035
TO
1852 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
1853 'group' => TRUE,
16e2e80c 1854 'options' => CRM_Core_PseudoConstant::nestedGroup(),
be2fb01f
CW
1855 ],
1856 ];
6a488035
TO
1857 if (empty($this->_columns['civicrm_group']['dao'])) {
1858 $this->_columns['civicrm_group']['dao'] = 'CRM_Contact_DAO_GroupContact';
1859 }
1860 if (empty($this->_columns['civicrm_group']['alias'])) {
1861 $this->_columns['civicrm_group']['alias'] = 'cgroup';
1862 }
1863 }
1864
74cf4551 1865 /**
07f44165
EM
1866 * Get SQL operator from form text version.
1867 *
74cf4551
EM
1868 * @param string $operator
1869 *
1870 * @return string
1871 */
00be9182 1872 public function getSQLOperator($operator = "like") {
6a488035
TO
1873 switch ($operator) {
1874 case 'eq':
1875 return '=';
1876
1877 case 'lt':
1878 return '<';
1879
1880 case 'lte':
1881 return '<=';
1882
1883 case 'gt':
1884 return '>';
1885
1886 case 'gte':
1887 return '>=';
1888
1889 case 'ne':
1890 case 'neq':
1891 return '!=';
1892
1893 case 'nhas':
1894 return 'NOT LIKE';
1895
1896 case 'in':
1897 return 'IN';
1898
1899 case 'notin':
1900 return 'NOT IN';
1901
1902 case 'nll':
1903 return 'IS NULL';
1904
1905 case 'nnll':
1906 return 'IS NOT NULL';
1907
1908 default:
1909 // type is string
1910 return 'LIKE';
1911 }
1912 }
1913
74cf4551 1914 /**
f587aa17 1915 * Generate where clause.
07f44165 1916 *
8a925f33 1917 * This can be overridden in reports for special treatment of a field
f587aa17 1918 *
537c70b8 1919 * @param array $field Field specifications
8a925f33 1920 * @param string $op Query operator (not an exact match to sql)
f587aa17
EM
1921 * @param mixed $value
1922 * @param float $min
1923 * @param float $max
74cf4551
EM
1924 *
1925 * @return null|string
1926 */
8a925f33 1927 public function whereClause(&$field, $op, $value, $min, $max) {
6a488035
TO
1928
1929 $type = CRM_Utils_Type::typeToString(CRM_Utils_Array::value('type', $field));
8ee006e7
WA
1930
1931 // CRM-18010: Ensure type of each report filters
1932 if (!$type) {
1933 trigger_error('Type is not defined for field ' . $field['name'], E_USER_WARNING);
1934 }
6a488035
TO
1935 $clause = NULL;
1936
1937 switch ($op) {
1938 case 'bw':
1939 case 'nbw':
1940 if (($min !== NULL && strlen($min) > 0) ||
1941 ($max !== NULL && strlen($max) > 0)
1942 ) {
be2fb01f 1943 $clauses = [];
6a488035 1944 if ($min) {
6621df60 1945 $min = CRM_Utils_Type::escape($min, $type);
6a488035
TO
1946 if ($op == 'bw') {
1947 $clauses[] = "( {$field['dbAlias']} >= $min )";
1948 }
1949 else {
cd87f963 1950 $clauses[] = "( {$field['dbAlias']} < $min OR {$field['dbAlias']} IS NULL )";
6a488035
TO
1951 }
1952 }
1953 if ($max) {
6621df60 1954 $max = CRM_Utils_Type::escape($max, $type);
6a488035
TO
1955 if ($op == 'bw') {
1956 $clauses[] = "( {$field['dbAlias']} <= $max )";
1957 }
1958 else {
1959 $clauses[] = "( {$field['dbAlias']} > $max )";
1960 }
1961 }
1962
1963 if (!empty($clauses)) {
1964 if ($op == 'bw') {
1965 $clause = implode(' AND ', $clauses);
1966 }
1967 else {
cd87f963 1968 $clause = '(' . implode('OR', $clauses) . ')';
6a488035
TO
1969 }
1970 }
1971 }
1972 break;
1973
1974 case 'has':
1975 case 'nhas':
1976 if ($value !== NULL && strlen($value) > 0) {
1977 $value = CRM_Utils_Type::escape($value, $type);
1978 if (strpos($value, '%') === FALSE) {
1979 $value = "'%{$value}%'";
1980 }
1981 else {
1982 $value = "'{$value}'";
1983 }
29fc2b79 1984 $sqlOP = $this->getSQLOperator($op);
6a488035
TO
1985 $clause = "( {$field['dbAlias']} $sqlOP $value )";
1986 }
1987 break;
1988
1989 case 'in':
1990 case 'notin':
43c1fa19 1991 if ((is_string($value) || is_numeric($value)) && strlen($value)) {
2107cde9
CW
1992 $value = explode(',', $value);
1993 }
6a488035 1994 if ($value !== NULL && is_array($value) && count($value) > 0) {
29fc2b79 1995 $sqlOP = $this->getSQLOperator($op);
9d72cede
EM
1996 if (CRM_Utils_Array::value('type', $field) ==
1997 CRM_Utils_Type::T_STRING
1998 ) {
f587aa17 1999 //cycle through selections and escape values
6a488035
TO
2000 foreach ($value as $key => $selection) {
2001 $value[$key] = CRM_Utils_Type::escape($selection, $type);
2002 }
971d41b1
CW
2003 $clause
2004 = "( {$field['dbAlias']} $sqlOP ( '" . implode("' , '", $value) .
9d72cede 2005 "') )";
6a488035
TO
2006 }
2007 else {
2008 // for numerical values
9d72cede
EM
2009 $clause = "{$field['dbAlias']} $sqlOP (" . implode(', ', $value) .
2010 ")";
6a488035
TO
2011 }
2012 if ($op == 'notin') {
2013 $clause = "( " . $clause . " OR {$field['dbAlias']} IS NULL )";
2014 }
2015 else {
2016 $clause = "( " . $clause . " )";
2017 }
2018 }
2019 break;
2020
2021 case 'mhas':
67788963 2022 case 'mnot':
05839ccc 2023 // multiple has or multiple not
67788963 2024 if ($value !== NULL && count($value) > 0) {
f4fd6082 2025 $value = CRM_Utils_Type::escapeAll($value, $type);
2026 $operator = $op == 'mnot' ? 'NOT' : '';
228753d3 2027 $regexp = "([[:cntrl:]]|^)" . implode('([[:cntrl:]]|$)|([[:cntrl:]]|^)', (array) $value) . "([[:cntrl:]]|$)";
f4fd6082 2028 $clause = "{$field['dbAlias']} {$operator} REGEXP '{$regexp}'";
67788963
J
2029 }
2030 break;
6a488035
TO
2031
2032 case 'sw':
2033 case 'ew':
2034 if ($value !== NULL && strlen($value) > 0) {
2035 $value = CRM_Utils_Type::escape($value, $type);
2036 if (strpos($value, '%') === FALSE) {
2037 if ($op == 'sw') {
2038 $value = "'{$value}%'";
2039 }
2040 else {
2041 $value = "'%{$value}'";
2042 }
2043 }
2044 else {
2045 $value = "'{$value}'";
2046 }
29fc2b79 2047 $sqlOP = $this->getSQLOperator($op);
6a488035
TO
2048 $clause = "( {$field['dbAlias']} $sqlOP $value )";
2049 }
2050 break;
2051
2052 case 'nll':
2053 case 'nnll':
29fc2b79 2054 $sqlOP = $this->getSQLOperator($op);
6a488035
TO
2055 $clause = "( {$field['dbAlias']} $sqlOP )";
2056 break;
2057
55a5250b 2058 case 'eq':
2059 case 'neq':
2060 case 'ne':
2061 //CRM-18457: some custom field passes value in array format against binary operator
2062 if (is_array($value) && count($value)) {
2063 $value = $value[0];
2064 }
2065
6a488035 2066 default:
55a5250b 2067 if ($value !== NULL && $value !== '') {
6a488035
TO
2068 if (isset($field['clause'])) {
2069 // FIXME: we not doing escape here. Better solution is to use two
2070 // different types - data-type and filter-type
0e6e8724 2071 $clause = $field['clause'];
6a488035 2072 }
55a5250b 2073 elseif (!is_array($value)) {
6a488035 2074 $value = CRM_Utils_Type::escape($value, $type);
29fc2b79 2075 $sqlOP = $this->getSQLOperator($op);
6a488035
TO
2076 if ($field['type'] == CRM_Utils_Type::T_STRING) {
2077 $value = "'{$value}'";
2078 }
2079 $clause = "( {$field['dbAlias']} $sqlOP $value )";
2080 }
2081 }
2082 break;
2083 }
2084
0816f78c
EL
2085 //dev/core/544 Add report support for multiple contact subTypes
2086 if ($field['name'] == 'contact_sub_type' && $clause) {
2087 $clause = $this->whereSubtypeClause($field, $value, $op);
2088 }
a7488080 2089 if (!empty($field['group']) && $clause) {
6a488035
TO
2090 $clause = $this->whereGroupClause($field, $value, $op);
2091 }
a7488080 2092 elseif (!empty($field['tag']) && $clause) {
6a488035
TO
2093 // not using left join in query because if any contact
2094 // belongs to more than one tag, results duplicate
2095 // entries.
2096 $clause = $this->whereTagClause($field, $value, $op);
2097 }
114a2c85 2098 elseif (!empty($field['membership_org']) && $clause) {
f587aa17 2099 $clause = $this->whereMembershipOrgClause($value, $op);
114a2c85
DG
2100 }
2101 elseif (!empty($field['membership_type']) && $clause) {
f587aa17 2102 $clause = $this->whereMembershipTypeClause($value, $op);
114a2c85 2103 }
6a488035
TO
2104 return $clause;
2105 }
2106
0816f78c
EL
2107 /**
2108 * Get SQL where clause for contact subtypes
2109 * @param string $field
2110 * @param mixed $value
2111 * @param string $op SQL Operator
2112 *
2113 * @return string
2114 */
2115 public function whereSubtypeClause($field, $value, $op) {
1d6f9f23 2116 // Get the correct SQL operator.
f8dcf338 2117 $orNull = FALSE;
1d6f9f23
JG
2118 switch ($op) {
2119 case 'notin':
2120 $op = 'nhas';
f8dcf338
AS
2121 $clauseSeparator = ' AND ';
2122 $orNull = TRUE;
1d6f9f23
JG
2123 break;
2124
2125 case 'in':
2126 $op = 'has';
f8dcf338 2127 $clauseSeparator = ' OR ';
1d6f9f23
JG
2128 break;
2129 }
2130 $sqlOp = $this->getSQLOperator($op);
1d6f9f23 2131 if ($sqlOp == 'IS NULL' || $sqlOp == 'IS NOT NULL') {
f8dcf338 2132 $clause = "{$field['dbAlias']} $sqlOp";
1d6f9f23
JG
2133 }
2134 else {
f8dcf338
AS
2135 $subclauses = [];
2136 foreach ($value as $item) {
2137 $subclauses[] = "( {$field['dbAlias']} $sqlOp '%" . CRM_Core_DAO::VALUE_SEPARATOR . $item . CRM_Core_DAO::VALUE_SEPARATOR . "%' )";
0816f78c 2138 }
f8dcf338
AS
2139 $clause = implode($clauseSeparator, $subclauses);
2140 }
2141 $clause = "( $clause )";
2142 if ($orNull) {
2143 $clause = "( ( {$field['dbAlias']} IS NULL ) OR $clause )";
0816f78c 2144 }
0816f78c
EL
2145 return $clause;
2146 }
2147
74cf4551 2148 /**
07f44165
EM
2149 * Get SQL where clause for a date field.
2150 *
100fef9d 2151 * @param string $fieldName
317a8023 2152 * @param string $relative
f587aa17
EM
2153 * @param string $from
2154 * @param string $to
07f44165
EM
2155 * @param string $type
2156 * @param string $fromTime
2157 * @param string $toTime
74cf4551
EM
2158 *
2159 * @return null|string
2160 */
971d41b1 2161 public function dateClause(
7d8c1168
TO
2162 $fieldName,
2163 $relative, $from, $to, $type = NULL, $fromTime = NULL, $toTime = NULL
6a488035 2164 ) {
be2fb01f 2165 $clauses = [];
160d32e1 2166 if (in_array($relative, array_keys($this->getOperationPair(CRM_Report_Form::OP_DATE)))) {
29fc2b79 2167 $sqlOP = $this->getSQLOperator($relative);
6a488035
TO
2168 return "( {$fieldName} {$sqlOP} )";
2169 }
2170
29fc2b79 2171 list($from, $to) = $this->getFromTo($relative, $from, $to, $fromTime, $toTime);
6a488035
TO
2172
2173 if ($from) {
6a488035
TO
2174 $clauses[] = "( {$fieldName} >= $from )";
2175 }
2176
2177 if ($to) {
6a488035
TO
2178 $clauses[] = "( {$fieldName} <= {$to} )";
2179 }
2180
2181 if (!empty($clauses)) {
2182 return implode(' AND ', $clauses);
2183 }
2184
2185 return NULL;
2186 }
9d72cede 2187
74cf4551 2188 /**
42b5c549
EM
2189 * Get values for from and to for date ranges.
2190 *
5e9221b7 2191 * @deprecated
2192 *
f587aa17
EM
2193 * @param bool $relative
2194 * @param string $from
2195 * @param string $to
9907633b
EM
2196 * @param string $fromTime
2197 * @param string $toTime
74cf4551
EM
2198 *
2199 * @return array
2200 */
5e9221b7 2201 public function getFromTo($relative, $from, $to, $fromTime = NULL, $toTime = NULL) {
2202 if (empty($toTime)) {
2203 // odd legacy behaviour to treat NULL as 'end of the day'
2204 // recommend updating reports to call CRM_Utils_Date::getFromTo
2205 //directly (default on the function is the actual default there).
2206 $toTime = '235959';
2207 }
7ec58d2b 2208 return CRM_Utils_Date::getFromTo($relative, $from, $to, $fromTime, $toTime);
6a488035
TO
2209 }
2210
74cf4551 2211 /**
4b62bc4f
EM
2212 * Alter display of rows.
2213 *
2214 * Iterate through the rows retrieved via SQL and make changes for display purposes,
2215 * such as rendering contacts as links.
2216 *
2217 * @param array $rows
2218 * Rows generated by SQL, with an array for each row.
74cf4551 2219 */
00be9182 2220 public function alterDisplay(&$rows) {
6a488035
TO
2221 }
2222
74cf4551 2223 /**
42b5c549
EM
2224 * Alter the way in which custom data fields are displayed.
2225 *
2226 * @param array $rows
74cf4551 2227 */
00be9182 2228 public function alterCustomDataDisplay(&$rows) {
6a488035
TO
2229 // custom code to alter rows having custom values
2230 if (empty($this->_customGroupExtends)) {
2231 return;
2232 }
2233
be2fb01f
CW
2234 $customFields = [];
2235 $customFieldIds = [];
6a488035
TO
2236 foreach ($this->_params['fields'] as $fieldAlias => $value) {
2237 if ($fieldId = CRM_Core_BAO_CustomField::getKeyID($fieldAlias)) {
2238 $customFieldIds[$fieldAlias] = $fieldId;
2239 }
2240 }
2241 if (empty($customFieldIds)) {
2242 return;
2243 }
2244
6a488035
TO
2245 // skip for type date and ContactReference since date format is already handled
2246 $query = "
28241a61 2247SELECT cg.table_name, cf.id
6a488035
TO
2248FROM civicrm_custom_field cf
2249INNER JOIN civicrm_custom_group cg ON cg.id = cf.custom_group_id
6a488035
TO
2250WHERE cg.extends IN ('" . implode("','", $this->_customGroupExtends) . "') AND
2251 cg.is_active = 1 AND
2252 cf.is_active = 1 AND
2253 cf.is_searchable = 1 AND
2254 cf.data_type NOT IN ('ContactReference', 'Date') AND
2255 cf.id IN (" . implode(",", $customFieldIds) . ")";
2256
2257 $dao = CRM_Core_DAO::executeQuery($query);
2258 while ($dao->fetch()) {
28241a61 2259 $customFields[$dao->table_name . '_custom_' . $dao->id] = $dao->id;
6a488035 2260 }
6a488035
TO
2261
2262 $entryFound = FALSE;
2263 foreach ($rows as $rowNum => $row) {
2264 foreach ($row as $tableCol => $val) {
2265 if (array_key_exists($tableCol, $customFields)) {
28241a61 2266 $rows[$rowNum][$tableCol] = CRM_Core_BAO_CustomField::displayValue($val, $customFields[$tableCol]);
6a488035
TO
2267 $entryFound = TRUE;
2268 }
2269 }
2270
2271 // skip looking further in rows, if first row itself doesn't
2272 // have the column we need
2273 if (!$entryFound) {
2274 break;
2275 }
2276 }
2277 }
2278
74cf4551 2279 /**
317a8023 2280 * Remove duplicate rows.
2281 *
2282 * @param array $rows
74cf4551 2283 */
00be9182 2284 public function removeDuplicates(&$rows) {
6a488035
TO
2285 if (empty($this->_noRepeats)) {
2286 return;
2287 }
be2fb01f 2288 $checkList = [];
6a488035
TO
2289
2290 foreach ($rows as $key => $list) {
2291 foreach ($list as $colName => $colVal) {
8f1445ea 2292 if (array_key_exists($colName, $checkList) &&
9d72cede
EM
2293 $checkList[$colName] == $colVal
2294 ) {
6a488035
TO
2295 $rows[$key][$colName] = "";
2296 }
2297 if (in_array($colName, $this->_noRepeats)) {
2298 $checkList[$colName] = $colVal;
2299 }
2300 }
2301 }
2302 }
2303
74cf4551 2304 /**
317a8023 2305 * Fix subtotal display.
2306 *
2307 * @param array $row
2308 * @param array $fields
74cf4551
EM
2309 * @param bool $subtotal
2310 */
00be9182 2311 public function fixSubTotalDisplay(&$row, $fields, $subtotal = TRUE) {
6a488035
TO
2312 foreach ($row as $colName => $colVal) {
2313 if (in_array($colName, $fields)) {
6a488035
TO
2314 }
2315 elseif (isset($this->_columnHeaders[$colName])) {
2316 if ($subtotal) {
8c2959c8 2317 $row[$colName] = 'Subtotal';
6a488035
TO
2318 $subtotal = FALSE;
2319 }
2320 else {
2321 unset($row[$colName]);
2322 }
2323 }
2324 }
2325 }
2326
74cf4551 2327 /**
317a8023 2328 * Calculate grant total.
2329 *
2330 * @param array $rows
74cf4551
EM
2331 *
2332 * @return bool
2333 */
00be9182 2334 public function grandTotal(&$rows) {
c160fde8 2335 if (!$this->_rollup || count($rows) == 1) {
2336 return FALSE;
2337 }
2338
2339 $this->moveSummaryColumnsToTheRightHandSide();
2340
2341 if ($this->_limit && count($rows) >= self::ROW_COUNT_LIMIT) {
6a488035
TO
2342 return FALSE;
2343 }
8a2bee47 2344
2345 $this->rollupRow = array_pop($rows);
6a488035 2346
6a488035
TO
2347 foreach ($this->_columnHeaders as $fld => $val) {
2348 if (!in_array($fld, $this->_statFields)) {
2349 if (!$this->_grandFlag) {
658682cd 2350 $this->rollupRow[$fld] = ts('Grand Total');
6a488035
TO
2351 $this->_grandFlag = TRUE;
2352 }
2353 else {
8a2bee47 2354 $this->rollupRow[$fld] = "";
6a488035
TO
2355 }
2356 }
2357 }
2358
8a2bee47 2359 $this->assign('grandStat', $this->rollupRow);
6a488035
TO
2360 return TRUE;
2361 }
2362
74cf4551 2363 /**
317a8023 2364 * Format display output.
2365 *
2366 * @param array $rows
74cf4551
EM
2367 * @param bool $pager
2368 */
00be9182 2369 public function formatDisplay(&$rows, $pager = TRUE) {
6a488035
TO
2370 // set pager based on if any limit was applied in the query.
2371 if ($pager) {
2372 $this->setPager();
2373 }
2374
2375 // allow building charts if any
2376 if (!empty($this->_params['charts']) && !empty($rows)) {
2377 $this->buildChart($rows);
2378 $this->assign('chartEnabled', TRUE);
9d72cede
EM
2379 $this->_chartId = "{$this->_params['charts']}_" .
2380 ($this->_id ? $this->_id : substr(get_class($this), 16)) . '_' .
2381 session_id();
6a488035
TO
2382 $this->assign('chartId', $this->_chartId);
2383 }
2384
2385 // unset columns not to be displayed.
2386 foreach ($this->_columnHeaders as $key => $value) {
a7488080 2387 if (!empty($value['no_display'])) {
6a488035
TO
2388 unset($this->_columnHeaders[$key]);
2389 }
2390 }
2391
2392 // unset columns not to be displayed.
2393 if (!empty($rows)) {
2394 foreach ($this->_noDisplay as $noDisplayField) {
2395 foreach ($rows as $rowNum => $row) {
2396 unset($this->_columnHeaders[$noDisplayField]);
2397 }
2398 }
2399 }
2400
59b19369
JJ
2401 // Find alter display functions.
2402 $firstRow = reset($rows);
2403 if ($firstRow) {
2404 $selectedFields = array_keys($firstRow);
be2fb01f 2405 $alterFunctions = $alterMap = $alterSpecs = [];
59b19369
JJ
2406 foreach ($this->_columns as $tableName => $table) {
2407 if (array_key_exists('metadata', $table)) {
2408 foreach ($table['metadata'] as $field => $specs) {
2409 if (in_array($tableName . '_' . $field, $selectedFields)) {
2410 if (array_key_exists('alter_display', $specs)) {
2411 $alterFunctions[$tableName . '_' . $field] = $specs['alter_display'];
2412 $alterMap[$tableName . '_' . $field] = $field;
2413 $alterSpecs[$tableName . '_' . $field] = NULL;
2414 }
2415 // Add any alters that can be intuited from the field specs.
2416 // So far only boolean but a lot more could be.
2417 if (empty($alterSpecs[$tableName . '_' . $field]) && isset($specs['type']) && $specs['type'] == CRM_Utils_Type::T_BOOLEAN) {
2418 $alterFunctions[$tableName . '_' . $field] = 'alterBoolean';
2419 $alterMap[$tableName . '_' . $field] = $field;
2420 $alterSpecs[$tableName . '_' . $field] = NULL;
2421 }
2422 }
2423 }
2424 }
2425 }
2426
2427 // Run the alter display functions
2428 foreach ($rows as $index => & $row) {
2429 foreach ($row as $selectedField => $value) {
cec0bbb8 2430 if (array_key_exists($selectedField, $alterFunctions) && isset($value)) {
59b19369
JJ
2431 $rows[$index][$selectedField] = $this->{$alterFunctions[$selectedField]}($value, $row, $selectedField, $alterMap[$selectedField], $alterSpecs[$selectedField]);
2432 }
2433 }
2434 }
2435 }
2436
6a488035
TO
2437 // use this method for formatting rows for display purpose.
2438 $this->alterDisplay($rows);
2439 CRM_Utils_Hook::alterReportVar('rows', $rows, $this);
2440
9e40c416 2441 // build array of section totals
2442 $this->sectionTotals();
2443
2444 // process grand-total row
2445 $this->grandTotal($rows);
2446
6a488035
TO
2447 // use this method for formatting custom rows for display purpose.
2448 $this->alterCustomDataDisplay($rows);
2449 }
2450
59b19369
JJ
2451 /**
2452 * @param $value
2453 * @param $row
2454 * @param $selectedfield
2455 * @param $criteriaFieldName
2456 *
2457 * @return array
2458 */
2459 protected function alterStateProvinceID($value, &$row, $selectedfield, $criteriaFieldName) {
2460 $url = CRM_Utils_System::url(CRM_Utils_System::currentPath(), "reset=1&force=1&{$criteriaFieldName}_op=in&{$criteriaFieldName}_value={$value}", $this->_absoluteUrl);
2461 $row[$selectedfield . '_link'] = $url;
be2fb01f 2462 $row[$selectedfield . '_hover'] = ts("%1 for this state.", [
59b19369 2463 1 => $value,
be2fb01f 2464 ]);
59b19369
JJ
2465
2466 $states = CRM_Core_PseudoConstant::stateProvince($value, FALSE);
2467 if (!is_array($states)) {
2468 return $states;
2469 }
2470 }
2471
2472 /**
2473 * @param $value
2474 * @param $row
2475 * @param $selectedField
2476 * @param $criteriaFieldName
2477 *
2478 * @return array
2479 */
2480 protected function alterCountryID($value, &$row, $selectedField, $criteriaFieldName) {
2481 $url = CRM_Utils_System::url(CRM_Utils_System::currentPath(), "reset=1&force=1&{$criteriaFieldName}_op=in&{$criteriaFieldName}_value={$value}", $this->_absoluteUrl);
2482 $row[$selectedField . '_link'] = $url;
be2fb01f 2483 $row[$selectedField . '_hover'] = ts("%1 for this country.", [
59b19369 2484 1 => $value,
be2fb01f 2485 ]);
59b19369
JJ
2486 $countries = CRM_Core_PseudoConstant::country($value, FALSE);
2487 if (!is_array($countries)) {
2488 return $countries;
2489 }
2490 }
2491
2492 /**
2493 * @param $value
2494 * @param $row
2495 * @param $selectedfield
2496 * @param $criteriaFieldName
2497 *
2498 * @return array
2499 */
2500 protected function alterCountyID($value, &$row, $selectedfield, $criteriaFieldName) {
2501 $url = CRM_Utils_System::url(CRM_Utils_System::currentPath(), "reset=1&force=1&{$criteriaFieldName}_op=in&{$criteriaFieldName}_value={$value}", $this->_absoluteUrl);
2502 $row[$selectedfield . '_link'] = $url;
be2fb01f 2503 $row[$selectedfield . '_hover'] = ts("%1 for this county.", [
59b19369 2504 1 => $value,
be2fb01f 2505 ]);
59b19369
JJ
2506 $counties = CRM_Core_PseudoConstant::county($value, FALSE);
2507 if (!is_array($counties)) {
2508 return $counties;
2509 }
2510 }
2511
2512 /**
2513 * @param $value
2514 * @param $row
2515 * @param $selectedfield
2516 * @param $criteriaFieldName
2517 *
2518 * @return mixed
2519 */
2520 protected function alterLocationTypeID($value, &$row, $selectedfield, $criteriaFieldName) {
d7786eaa 2521 return CRM_Core_PseudoConstant::getLabel('CRM_Core_DAO_Address', 'location_type_id', $value);
59b19369
JJ
2522 }
2523
2524 /**
2525 * @param $value
2526 * @param $row
2527 * @param $fieldname
2528 *
2529 * @return mixed
2530 */
2531 protected function alterContactID($value, &$row, $fieldname) {
2532 $nameField = substr($fieldname, 0, -2) . 'name';
2533 static $first = TRUE;
2534 static $viewContactList = FALSE;
2535 if ($first) {
2536 $viewContactList = CRM_Core_Permission::check('access CiviCRM');
2537 $first = FALSE;
2538 }
2539 if (!$viewContactList) {
2540 return $value;
2541 }
2542 if (array_key_exists($nameField, $row)) {
2543 $row[$nameField . '_link'] = CRM_Utils_System::url("civicrm/contact/view", 'reset=1&cid=' . $value, $this->_absoluteUrl);
2544 }
2545 else {
2546 $row[$fieldname . '_link'] = CRM_Utils_System::url("civicrm/contact/view", 'reset=1&cid=' . $value, $this->_absoluteUrl);
2547 }
2548 return $value;
2549 }
2550
1754f22f
AS
2551 /**
2552 * @param $value
2553 * @param $row
2554 * @param $fieldname
2555 *
2556 * @return mixed
2557 */
2558 protected function alterCommunicationtMethod($value, &$row, $fieldname) {
2559 $communicationMethods = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method');
2560
2561 // Explode padded values.
2562 $values = CRM_utils_array::explodePadded($value);
2563 // Flip values, compute intersection with $communicationMethods, and implode with commas.
2564 $value = implode(', ', array_intersect_key($communicationMethods, array_flip($values)));
2565 return $value;
2566 }
2567
59b19369
JJ
2568 /**
2569 * @param $value
2570 *
2571 * @return mixed
2572 */
2573 protected function alterBoolean($value) {
be2fb01f 2574 $options = [0 => '', 1 => ts('Yes')];
59b19369
JJ
2575 if (isset($options[$value])) {
2576 return $options[$value];
2577 }
2578 return $value;
2579 }
2580
74cf4551 2581 /**
317a8023 2582 * Build chart.
2583 *
2584 * @param array $rows
74cf4551 2585 */
00be9182 2586 public function buildChart(&$rows) {
6a488035
TO
2587 // override this method for building charts.
2588 }
2589
2590 // select() method below has been added recently (v3.3), and many of the report templates might
2591 // still be having their own select() method. We should fix them as and when encountered and move
2592 // towards generalizing the select() method below.
971d41b1
CW
2593
2594 /**
317a8023 2595 * Generate the SELECT clause and set class variable $_select.
971d41b1 2596 */
00be9182 2597 public function select() {
be2fb01f 2598 $select = $this->_selectAliases = [];
534c4d20 2599 $this->storeGroupByArray();
6a488035
TO
2600
2601 foreach ($this->_columns as $tableName => $table) {
2602 if (array_key_exists('fields', $table)) {
2603 foreach ($table['fields'] as $fieldName => $field) {
2604 if ($tableName == 'civicrm_address') {
f38e3c19 2605 // deprecated, use $this->isTableSelected.
6a488035
TO
2606 $this->_addressField = TRUE;
2607 }
2608 if ($tableName == 'civicrm_email') {
2609 $this->_emailField = TRUE;
2610 }
2611 if ($tableName == 'civicrm_phone') {
2612 $this->_phoneField = TRUE;
2613 }
2614
9d72cede
EM
2615 if (!empty($field['required']) ||
2616 !empty($this->_params['fields'][$fieldName])
2617 ) {
6a488035
TO
2618
2619 // 1. In many cases we want select clause to be built in slightly different way
317a8023 2620 // for a particular field of a particular type.
6a488035 2621 // 2. This method when used should receive params by reference and modify $this->_columnHeaders
317a8023 2622 // as needed.
6a488035
TO
2623 $selectClause = $this->selectClause($tableName, 'fields', $fieldName, $field);
2624 if ($selectClause) {
2625 $select[] = $selectClause;
2626 continue;
2627 }
2628
2629 // include statistics columns only if set
4b885f84 2630 if (!empty($field['statistics']) && !empty($this->_groupByArray)) {
0a01dff9 2631 $select = $this->addStatisticsToSelect($field, $tableName, $fieldName, $select);
6a488035
TO
2632 }
2633 else {
81a22d3d 2634
2635 $selectClause = $this->getSelectClauseWithGroupConcatIfNotGroupedBy($tableName, $fieldName, $field);
2636 if ($selectClause) {
2637 $select[] = $selectClause;
2638 }
2639 else {
2640 $select = $this->addBasicFieldToSelect($tableName, $fieldName, $field, $select);
2641 }
6a488035
TO
2642 }
2643 }
2644 }
2645 }
2646
2647 // select for group bys
2648 if (array_key_exists('group_bys', $table)) {
2649 foreach ($table['group_bys'] as $fieldName => $field) {
2650
2651 if ($tableName == 'civicrm_address') {
2652 $this->_addressField = TRUE;
2653 }
2654 if ($tableName == 'civicrm_email') {
2655 $this->_emailField = TRUE;
2656 }
2657 if ($tableName == 'civicrm_phone') {
2658 $this->_phoneField = TRUE;
2659 }
2660 // 1. In many cases we want select clause to be built in slightly different way
317a8023 2661 // for a particular field of a particular type.
6a488035 2662 // 2. This method when used should receive params by reference and modify $this->_columnHeaders
317a8023 2663 // as needed.
6a488035
TO
2664 $selectClause = $this->selectClause($tableName, 'group_bys', $fieldName, $field);
2665 if ($selectClause) {
2666 $select[] = $selectClause;
2667 continue;
2668 }
2669
9d72cede
EM
2670 if (!empty($this->_params['group_bys']) &&
2671 !empty($this->_params['group_bys'][$fieldName]) &&
2672 !empty($this->_params['group_bys_freq'])
2673 ) {
6a488035
TO
2674 switch (CRM_Utils_Array::value($fieldName, $this->_params['group_bys_freq'])) {
2675 case 'YEARWEEK':
9d72cede
EM
2676 $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL WEEKDAY({$field['dbAlias']}) DAY) AS {$tableName}_{$fieldName}_start";
2677 $select[] = "YEARWEEK({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
2678 $select[] = "WEEKOFYEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
6a488035
TO
2679 $field['title'] = 'Week';
2680 break;
2681
2682 case 'YEAR':
9d72cede
EM
2683 $select[] = "MAKEDATE(YEAR({$field['dbAlias']}), 1) AS {$tableName}_{$fieldName}_start";
2684 $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
2685 $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
6a488035
TO
2686 $field['title'] = 'Year';
2687 break;
2688
2689 case 'MONTH':
9d72cede
EM
2690 $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL (DAYOFMONTH({$field['dbAlias']})-1) DAY) as {$tableName}_{$fieldName}_start";
2691 $select[] = "MONTH({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
2692 $select[] = "MONTHNAME({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
6a488035
TO
2693 $field['title'] = 'Month';
2694 break;
2695
2696 case 'QUARTER':
9d72cede
EM
2697 $select[] = "STR_TO_DATE(CONCAT( 3 * QUARTER( {$field['dbAlias']} ) -2 , '/', '1', '/', YEAR( {$field['dbAlias']} ) ), '%m/%d/%Y') AS {$tableName}_{$fieldName}_start";
2698 $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
2699 $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
6a488035
TO
2700 $field['title'] = 'Quarter';
2701 break;
2702 }
2703 // for graphs and charts -
a7488080 2704 if (!empty($this->_params['group_bys_freq'][$fieldName])) {
6a488035 2705 $this->_interval = $field['title'];
971d41b1
CW
2706 $this->_columnHeaders["{$tableName}_{$fieldName}_start"]['title']
2707 = $field['title'] . ' Beginning';
6a488035
TO
2708 $this->_columnHeaders["{$tableName}_{$fieldName}_start"]['type'] = $field['type'];
2709 $this->_columnHeaders["{$tableName}_{$fieldName}_start"]['group_by'] = $this->_params['group_bys_freq'][$fieldName];
2710
7d7c50f9 2711 // just to make sure these values are transferred to rows.
6a488035
TO
2712 // since we 'll need them for calculation purpose,
2713 // e.g making subtotals look nicer or graphs
be2fb01f
CW
2714 $this->_columnHeaders["{$tableName}_{$fieldName}_interval"] = ['no_display' => TRUE];
2715 $this->_columnHeaders["{$tableName}_{$fieldName}_subtotal"] = ['no_display' => TRUE];
6a488035
TO
2716 }
2717 }
2718 }
2719 }
2720 }
2721
048387ef 2722 if (empty($select)) {
2723 // CRM-21412 Do not give fatal error on report when no fields selected
be2fb01f 2724 $select = [1];
048387ef 2725 }
2726
1f220d30 2727 $this->_selectClauses = $select;
6a488035
TO
2728 $this->_select = "SELECT " . implode(', ', $select) . " ";
2729 }
2730
74cf4551 2731 /**
317a8023 2732 * Build select clause for a single field.
2733 *
100fef9d 2734 * @param string $tableName
317a8023 2735 * @param string $tableKey
100fef9d 2736 * @param string $fieldName
317a8023 2737 * @param string $field
74cf4551
EM
2738 *
2739 * @return bool
2740 */
00be9182 2741 public function selectClause(&$tableName, $tableKey, &$fieldName, &$field) {
81a22d3d 2742 if (!empty($field['pseudofield'])) {
2743 $alias = "{$tableName}_{$fieldName}";
2744 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
2745 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
2746 $this->_columnHeaders["{$tableName}_{$fieldName}"]['dbAlias'] = CRM_Utils_Array::value('dbAlias', $field);
2747 $this->_selectAliases[] = $alias;
2748 return ' 1 as ' . $alias;
2749 }
6a488035
TO
2750 return FALSE;
2751 }
2752
317a8023 2753 /**
2754 * Build where clause.
2755 */
00be9182 2756 public function where() {
adfe2750 2757 $this->storeWhereHavingClauseArray();
2758
2759 if (empty($this->_whereClauses)) {
2760 $this->_where = "WHERE ( 1 ) ";
2761 $this->_having = "";
2762 }
2763 else {
2764 $this->_where = "WHERE " . implode(' AND ', $this->_whereClauses);
2765 }
2766
2767 if ($this->_aclWhere) {
2768 $this->_where .= " AND {$this->_aclWhere} ";
2769 }
2770
2771 if (!empty($this->_havingClauses)) {
2772 // use this clause to construct group by clause.
2773 $this->_having = "HAVING " . implode(' AND ', $this->_havingClauses);
2774 }
2775 }
2776
2777 /**
317a8023 2778 * Store Where clauses into an array.
2779 *
2780 * Breaking out this step makes over-riding more flexible as the clauses can be used in constructing a
adfe2750 2781 * temp table that may not be part of the final where clause or added
2782 * in other functions
2783 */
00be9182 2784 public function storeWhereHavingClauseArray() {
6a488035
TO
2785 foreach ($this->_columns as $tableName => $table) {
2786 if (array_key_exists('filters', $table)) {
2787 foreach ($table['filters'] as $fieldName => $field) {
d12de91c 2788 // respect pseudofield to filter spec so fields can be marked as
2789 // not to be handled here
9d72cede 2790 if (!empty($field['pseudofield'])) {
d12de91c 2791 continue;
2792 }
62491db8 2793 $clause = $this->generateFilterClause($field, $fieldName);
6a488035
TO
2794
2795 if (!empty($clause)) {
a7488080 2796 if (!empty($field['having'])) {
adfe2750 2797 $this->_havingClauses[] = $clause;
6a488035
TO
2798 }
2799 else {
adfe2750 2800 $this->_whereClauses[] = $clause;
6a488035
TO
2801 }
2802 }
2803 }
2804 }
2805 }
2806
6a488035 2807 }
9d72cede 2808
317a8023 2809 /**
2810 * Set output mode.
2811 */
00be9182 2812 public function processReportMode() {
182f5081 2813 $this->setOutputMode();
6a488035 2814
971d41b1
CW
2815 $this->_sendmail
2816 = CRM_Utils_Request::retrieve(
884605ca
DL
2817 'sendmail',
2818 'Boolean',
2819 CRM_Core_DAO::$_nullObject
2820 );
6a488035
TO
2821
2822 $this->_absoluteUrl = FALSE;
2823 $printOnly = FALSE;
2824 $this->assign('printOnly', FALSE);
2825
182f5081 2826 if ($this->_outputMode == 'print' ||
2827 ($this->_sendmail && !$this->_outputMode)
9d72cede 2828 ) {
6a488035
TO
2829 $this->assign('printOnly', TRUE);
2830 $printOnly = TRUE;
182f5081 2831 $this->addPaging = FALSE;
6a488035
TO
2832 $this->assign('outputMode', 'print');
2833 $this->_outputMode = 'print';
2834 if ($this->_sendmail) {
2835 $this->_absoluteUrl = TRUE;
9d72cede 2836 }
6a488035 2837 }
182f5081 2838 elseif ($this->_outputMode == 'pdf') {
6a488035 2839 $printOnly = TRUE;
182f5081 2840 $this->addPaging = FALSE;
6a488035
TO
2841 $this->_absoluteUrl = TRUE;
2842 }
182f5081 2843 elseif ($this->_outputMode == 'csv') {
6a488035 2844 $printOnly = TRUE;
6a488035 2845 $this->_absoluteUrl = TRUE;
182f5081 2846 $this->addPaging = FALSE;
6a488035 2847 }
182f5081 2848 elseif ($this->_outputMode == 'group') {
6a488035 2849 $this->assign('outputMode', 'group');
6a488035 2850 }
182f5081 2851 elseif ($this->_outputMode == 'create_report' && $this->_criteriaForm) {
6a488035 2852 $this->assign('outputMode', 'create_report');
6a488035 2853 }
182f5081 2854 elseif ($this->_outputMode == 'copy' && $this->_criteriaForm) {
2855 $this->_createNew = TRUE;
6a488035
TO
2856 }
2857
182f5081 2858 $this->assign('outputMode', $this->_outputMode);
2859 $this->assign('printOnly', $printOnly);
6a488035
TO
2860 // Get today's date to include in printed reports
2861 if ($printOnly) {
2862 $reportDate = CRM_Utils_Date::customFormat(date('Y-m-d H:i'));
2863 $this->assign('reportDate', $reportDate);
2864 }
2865 }
2866
9657ccf2 2867 /**
317a8023 2868 * Post Processing function for Form.
2869 *
2870 * postProcessCommon should be used to set other variables from input as the api accesses that function.
182f5081 2871 * This function is not accessed when the api calls the report.
9657ccf2 2872 */
00be9182 2873 public function beginPostProcess() {
c58f66e0 2874 $this->setParams($this->controller->exportValues($this->_name));
6a488035
TO
2875 if (empty($this->_params) &&
2876 $this->_force
2877 ) {
c58f66e0 2878 $this->setParams($this->_formValues);
6a488035
TO
2879 }
2880
2881 // hack to fix params when submitted from dashboard, CRM-8532
2882 // fields array is missing because form building etc is skipped
2883 // in dashboard mode for report
c58f66e0 2884 //@todo - this could be done in the dashboard no we have a setter
a7488080 2885 if (empty($this->_params['fields']) && !$this->_noFields) {
c58f66e0 2886 $this->setParams($this->_formValues);
6a488035
TO
2887 }
2888
6a488035 2889 $this->processReportMode();
182f5081 2890
2891 if ($this->_outputMode == 'save' || $this->_outputMode == 'copy') {
2892 $this->_createNew = ($this->_outputMode == 'copy');
182f5081 2893 CRM_Report_Form_Instance::postProcess($this);
2894 }
e3c612db 2895 if ($this->_outputMode == 'delete') {
2896 CRM_Report_BAO_ReportInstance::doFormDelete($this->_id, 'civicrm/report/list?reset=1', 'civicrm/report/list?reset=1');
2897 }
2898
a231baad 2899 $this->_formValues = $this->_params;
2900
c58f66e0
E
2901 $this->beginPostProcessCommon();
2902 }
2903
2904 /**
317a8023 2905 * BeginPostProcess function run in both report mode and non-report mode (api).
c58f66e0 2906 */
ccc29f8e 2907 public function beginPostProcessCommon() {
2908 }
6a488035 2909
74cf4551 2910 /**
317a8023 2911 * Build the report query.
2912 *
74cf4551
EM
2913 * @param bool $applyLimit
2914 *
2915 * @return string
2916 */
00be9182 2917 public function buildQuery($applyLimit = TRUE) {
43c1fa19 2918 $this->buildGroupTempTable();
6a488035
TO
2919 $this->select();
2920 $this->from();
2921 $this->customDataFrom();
f0384ec0 2922 $this->buildPermissionClause();
6a488035
TO
2923 $this->where();
2924 $this->groupBy();
2925 $this->orderBy();
2926
81a22d3d 2927 foreach ($this->unselectedOrderByColumns() as $alias => $field) {
2928 $clause = $this->getSelectClauseWithGroupConcatIfNotGroupedBy($field['table_name'], $field['name'], $field);
2929 if (!$clause) {
2930 $clause = "{$field['dbAlias']} as {$alias}";
2931 }
2932 $this->_select .= ", $clause ";
6a488035
TO
2933 }
2934
8cc574cf 2935 if ($applyLimit && empty($this->_params['charts'])) {
6a488035
TO
2936 $this->limit();
2937 }
2938 CRM_Utils_Hook::alterReportVar('sql', $this, $this);
2939
2940 $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy} {$this->_limit}";
02d451ab 2941 $this->addToDeveloperTab($sql);
6a488035
TO
2942 return $sql;
2943 }
2944
317a8023 2945 /**
2946 * Build group by clause.
2947 */
00be9182 2948 public function groupBy() {
4b885f84 2949 $this->storeGroupByArray();
6a488035 2950
70e504f2 2951 if (!empty($this->_groupByArray)) {
e40ce31e
JM
2952 if ($this->optimisedForOnlyFullGroupBy) {
2953 // We should probably deprecate this code path. What happens here is that
2954 // the group by is amended to reflect the select columns. This often breaks the
2955 // results. Retrofitting group strict group by onto existing report classes
2956 // went badly.
2957 $this->_groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, $this->_groupByArray);
2958 }
2959 else {
2960 $this->_groupBy = ' GROUP BY ' . implode($this->_groupByArray);
2961 }
6a488035
TO
2962 }
2963 }
2964
317a8023 2965 /**
2966 * Build order by clause.
2967 */
00be9182 2968 public function orderBy() {
9d72cede 2969 $this->_orderBy = "";
be2fb01f 2970 $this->_sections = [];
6a488035 2971 $this->storeOrderByArray();
9d72cede 2972 if (!empty($this->_orderByArray) && !$this->_rollup == 'WITH ROLLUP') {
6a488035
TO
2973 $this->_orderBy = "ORDER BY " . implode(', ', $this->_orderByArray);
2974 }
2975 $this->assign('sections', $this->_sections);
2976 }
f2947aea 2977
688d37c6 2978 /**
317a8023 2979 * Extract order by fields and store as an array.
2980 *
6a488035
TO
2981 * In some cases other functions want to know which fields are selected for ordering by
2982 * Separating this into a separate function allows it to be called separately from constructing
2983 * the order by clause
2984 */
00be9182 2985 public function storeOrderByArray() {
be2fb01f 2986 $orderBys = [];
6a488035 2987
a7488080 2988 if (!empty($this->_params['order_bys']) &&
6a488035
TO
2989 is_array($this->_params['order_bys']) &&
2990 !empty($this->_params['order_bys'])
2991 ) {
2992
9907633b 2993 // Process order_bys in user-specified order
6a488035 2994 foreach ($this->_params['order_bys'] as $orderBy) {
be2fb01f 2995 $orderByField = [];
6a488035
TO
2996 foreach ($this->_columns as $tableName => $table) {
2997 if (array_key_exists('order_bys', $table)) {
2998 // For DAO columns defined in $this->_columns
2999 $fields = $table['order_bys'];
3000 }
3001 elseif (array_key_exists('extends', $table)) {
3002 // For custom fields referenced in $this->_customGroupExtends
be2fb01f 3003 $fields = CRM_Utils_Array::value('fields', $table, []);
6a488035 3004 }
6f993086
AH
3005 else {
3006 continue;
3007 }
6a488035
TO
3008 if (!empty($fields) && is_array($fields)) {
3009 foreach ($fields as $fieldName => $field) {
3010 if ($fieldName == $orderBy['column']) {
f2947aea 3011 $orderByField = array_merge($field, $orderBy);
6a488035
TO
3012 $orderByField['tplField'] = "{$tableName}_{$fieldName}";
3013 break 2;
3014 }
3015 }
3016 }
3017 }
3018
3019 if (!empty($orderByField)) {
55f71fa7 3020 $this->_orderByFields[$orderByField['tplField']] = $orderByField;
81a22d3d 3021 if ($this->groupConcatTested) {
3022 $orderBys[$orderByField['tplField']] = "{$orderByField['tplField']} {$orderBy['order']}";
3023 }
3024 else {
3025 // Not sure when this is preferable to using tplField (which has
3026 // definitely been tested to work in cases then this does not.
3027 // in caution not switching unless report has been tested for
3028 // group concat functionality.
3029 $orderBys[$orderByField['tplField']] = "{$orderByField['dbAlias']} {$orderBy['order']}";
3030 }
6a488035
TO
3031
3032 // Record any section headers for assignment to the template
a7488080 3033 if (!empty($orderBy['section'])) {
b5801e1d 3034 $orderByField['pageBreak'] = CRM_Utils_Array::value('pageBreak', $orderBy);
6a488035
TO
3035 $this->_sections[$orderByField['tplField']] = $orderByField;
3036 }
3037 }
3038 }
3039 }
3040
3041 $this->_orderByArray = $orderBys;
3042
3043 $this->assign('sections', $this->_sections);
3044 }
3045
81a22d3d 3046 /**
3047 * Determine unselected columns.
3048 *
3049 * @return array
3050 */
3051 public function unselectedOrderByColumns() {
3052 return array_diff_key($this->_orderByFields, $this->getSelectColumns());
3053 }
3054
74cf4551 3055 /**
317a8023 3056 * Determine unselected columns.
3057 *
74cf4551
EM
3058 * @return array
3059 */
00be9182 3060 public function unselectedSectionColumns() {
6a488035 3061 if (is_array($this->_sections)) {
55f71fa7 3062 return array_diff_key($this->_sections, $this->getSelectColumns());
6a488035
TO
3063 }
3064 else {
be2fb01f 3065 return [];
6a488035
TO
3066 }
3067 }
3068
74cf4551 3069 /**
317a8023 3070 * Build output rows.
3071 *
3072 * @param string $sql
3073 * @param array $rows
74cf4551 3074 */
00be9182 3075 public function buildRows($sql, &$rows) {
66ddb273
SL
3076 if (!$this->optimisedForOnlyFullGroupBy) {
3077 CRM_Core_DAO::disableFullGroupByMode();
3078 }
6a488035 3079 $dao = CRM_Core_DAO::executeQuery($sql);
b040d4b6 3080 if (stristr($this->_select, 'SQL_CALC_FOUND_ROWS')) {
3081 $this->_rowsFound = CRM_Core_DAO::singleValueQuery('SELECT FOUND_ROWS()');
3082 }
2f68ef20 3083 CRM_Core_DAO::reenableFullGroupByMode();
6a488035 3084 if (!is_array($rows)) {
be2fb01f 3085 $rows = [];
6a488035
TO
3086 }
3087
3088 // use this method to modify $this->_columnHeaders
3089 $this->modifyColumnHeaders();
3090
3091 $unselectedSectionColumns = $this->unselectedSectionColumns();
3092
3093 while ($dao->fetch()) {
be2fb01f 3094 $row = [];
6a488035
TO
3095 foreach ($this->_columnHeaders as $key => $value) {
3096 if (property_exists($dao, $key)) {
3097 $row[$key] = $dao->$key;
3098 }
3099 }
3100
3101 // section headers not selected for display need to be added to row
3102 foreach ($unselectedSectionColumns as $key => $values) {
3103 if (property_exists($dao, $key)) {
3104 $row[$key] = $dao->$key;
3105 }
3106 }
3107
3108 $rows[] = $row;
3109 }
3110 }
3111
3112 /**
317a8023 3113 * Calculate section totals.
3114 *
6a488035
TO
3115 * When "order by" fields are marked as sections, this assigns to the template
3116 * an array of total counts for each section. This data is used by the Smarty
317a8023 3117 * plugin {sectionTotal}.
6a488035 3118 */
00be9182 3119 public function sectionTotals() {
6a488035
TO
3120
3121 // Reports using order_bys with sections must populate $this->_selectAliases in select() method.
3122 if (empty($this->_selectAliases)) {
3123 return;
3124 }
3125
3126 if (!empty($this->_sections)) {
3127 // build the query with no LIMIT clause
3128 $select = str_ireplace('SELECT SQL_CALC_FOUND_ROWS ', 'SELECT ', $this->_select);
3129 $sql = "{$select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy}";
3130
3131 // pull section aliases out of $this->_sections
3132 $sectionAliases = array_keys($this->_sections);
3133
be2fb01f 3134 $ifnulls = [];
6a488035
TO
3135 foreach (array_merge($sectionAliases, $this->_selectAliases) as $alias) {
3136 $ifnulls[] = "ifnull($alias, '') as $alias";
3137 }
b708c08d 3138 $this->_select = "SELECT " . implode(", ", $ifnulls);
36d2f4d5 3139 $this->_select = CRM_Contact_BAO_Query::appendAnyValueToSelect($ifnulls, $sectionAliases);
6a488035 3140
971d41b1
CW
3141 // Group (un-limited) report by all aliases and get counts. This might
3142 // be done more efficiently when the contents of $sql are known, ie. by
3143 // overriding this method in the report class.
6a488035 3144
b708c08d 3145 $query = $this->_select .
9d72cede
EM
3146 ", count(*) as ct from ($sql) as subquery group by " .
3147 implode(", ", $sectionAliases);
6a488035
TO
3148
3149 // initialize array of total counts
be2fb01f 3150 $totals = [];
6a488035
TO
3151 $dao = CRM_Core_DAO::executeQuery($query);
3152 while ($dao->fetch()) {
3153
3154 // let $this->_alterDisplay translate any integer ids to human-readable values.
3155 $rows[0] = $dao->toArray();
3156 $this->alterDisplay($rows);
3157 $row = $rows[0];
3158
3159 // add totals for all permutations of section values
be2fb01f 3160 $values = [];
9d72cede 3161 $i = 1;
6a488035
TO
3162 $aliasCount = count($sectionAliases);
3163 foreach ($sectionAliases as $alias) {
3164 $values[] = $row[$alias];
3165 $key = implode(CRM_Core_DAO::VALUE_SEPARATOR, $values);
3166 if ($i == $aliasCount) {
3167 // the last alias is the lowest-level section header; use count as-is
3168 $totals[$key] = $dao->ct;
3169 }
3170 else {
3171 // other aliases are higher level; roll count into their total
3172 $totals[$key] += $dao->ct;
3173 }
3174 }
3175 }
3176 $this->assign('sectionTotals', $totals);
3177 }
3178 }
3179
317a8023 3180 /**
3181 * Modify column headers.
3182 */
00be9182 3183 public function modifyColumnHeaders() {
6a488035
TO
3184 // use this method to modify $this->_columnHeaders
3185 }
3186
06983500 3187 /**
3188 * Move totals columns to the right edge of the table.
3189 *
3190 * It seems like a more logical layout to have any totals columns on the far right regardless of
3191 * the location of the rest of their table.
3192 */
3193 public function moveSummaryColumnsToTheRightHandSide() {
3194 $statHeaders = (array_intersect_key($this->_columnHeaders, array_flip($this->_statFields)));
3195 $this->_columnHeaders = array_merge(array_diff_key($this->_columnHeaders, $statHeaders), $this->_columnHeaders, $statHeaders);
3196 }
3197
74cf4551 3198 /**
317a8023 3199 * Assign rows to the template.
3200 *
3201 * @param array $rows
74cf4551 3202 */
00be9182 3203 public function doTemplateAssignment(&$rows) {
6a488035
TO
3204 $this->assign_by_ref('columnHeaders', $this->_columnHeaders);
3205 $this->assign_by_ref('rows', $rows);
3206 $this->assign('statistics', $this->statistics($rows));
3207 }
3208
74cf4551 3209 /**
317a8023 3210 * Build report statistics.
3211 *
3212 * Override this method to build your own statistics.
3213 *
3214 * @param array $rows
74cf4551
EM
3215 *
3216 * @return array
3217 */
00be9182 3218 public function statistics(&$rows) {
be2fb01f 3219 $statistics = [];
6a488035
TO
3220
3221 $count = count($rows);
8a2bee47 3222 // Why do we increment the count for rollup seems to artificially inflate the count.
c160fde8 3223 // It seems perhaps intentional to include the summary row in the count of results - although
3224 // this just seems odd.
6a488035
TO
3225 if ($this->_rollup && ($this->_rollup != '') && $this->_grandFlag) {
3226 $count++;
3227 }
3228
3229 $this->countStat($statistics, $count);
3230
3231 $this->groupByStat($statistics);
3232
3233 $this->filterStat($statistics);
3234
3235 return $statistics;
3236 }
3237
74cf4551 3238 /**
317a8023 3239 * Add count statistics.
3240 *
3241 * @param array $statistics
3242 * @param int $count
74cf4551 3243 */
00be9182 3244 public function countStat(&$statistics, $count) {
be2fb01f 3245 $statistics['counts']['rowCount'] = [
9d72cede
EM
3246 'title' => ts('Row(s) Listed'),
3247 'value' => $count,
be2fb01f 3248 ];
6a488035
TO
3249
3250 if ($this->_rowsFound && ($this->_rowsFound > $count)) {
be2fb01f 3251 $statistics['counts']['rowsFound'] = [
9d72cede
EM
3252 'title' => ts('Total Row(s)'),
3253 'value' => $this->_rowsFound,
be2fb01f 3254 ];
6a488035
TO
3255 }
3256 }
3257
74cf4551 3258 /**
317a8023 3259 * Add group by statistics.
3260 *
3261 * @param array $statistics
74cf4551 3262 */
00be9182 3263 public function groupByStat(&$statistics) {
a7488080 3264 if (!empty($this->_params['group_bys']) &&
6a488035
TO
3265 is_array($this->_params['group_bys']) &&
3266 !empty($this->_params['group_bys'])
3267 ) {
3268 foreach ($this->_columns as $tableName => $table) {
3269 if (array_key_exists('group_bys', $table)) {
3270 foreach ($table['group_bys'] as $fieldName => $field) {
a7488080 3271 if (!empty($this->_params['group_bys'][$fieldName])) {
6a488035
TO
3272 $combinations[] = $field['title'];
3273 }
3274 }
3275 }
3276 }
be2fb01f 3277 $statistics['groups'][] = [
9d72cede
EM
3278 'title' => ts('Grouping(s)'),
3279 'value' => implode(' & ', $combinations),
be2fb01f 3280 ];
6a488035
TO
3281 }
3282 }
3283
74cf4551 3284 /**
317a8023 3285 * Filter statistics.
3286 *
3287 * @param array $statistics
74cf4551 3288 */
00be9182 3289 public function filterStat(&$statistics) {
6a488035
TO
3290 foreach ($this->_columns as $tableName => $table) {
3291 if (array_key_exists('filters', $table)) {
3292 foreach ($table['filters'] as $fieldName => $field) {
5aae2a90 3293 if ((CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE ||
3294 CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_TIME) &&
9d72cede
EM
3295 CRM_Utils_Array::value('operatorType', $field) !=
3296 CRM_Report_Form::OP_MONTH
3297 ) {
971d41b1
CW
3298 list($from, $to)
3299 = $this->getFromTo(
8f1445ea
DL
3300 CRM_Utils_Array::value("{$fieldName}_relative", $this->_params),
3301 CRM_Utils_Array::value("{$fieldName}_from", $this->_params),
3302 CRM_Utils_Array::value("{$fieldName}_to", $this->_params),
3303 CRM_Utils_Array::value("{$fieldName}_from_time", $this->_params),
3304 CRM_Utils_Array::value("{$fieldName}_to_time", $this->_params)
3305 );
0d8afee2 3306 $from_time_format = !empty($this->_params["{$fieldName}_from_time"]) ? 'h' : 'd';
be2fb01f 3307 $from = CRM_Utils_Date::customFormat($from, NULL, [$from_time_format]);
6a488035 3308
0d8afee2 3309 $to_time_format = !empty($this->_params["{$fieldName}_to_time"]) ? 'h' : 'd';
be2fb01f 3310 $to = CRM_Utils_Date::customFormat($to, NULL, [$to_time_format]);
6a488035
TO
3311
3312 if ($from || $to) {
be2fb01f 3313 $statistics['filters'][] = [
6a488035 3314 'title' => $field['title'],
be2fb01f
CW
3315 'value' => ts("Between %1 and %2", [1 => $from, 2 => $to]),
3316 ];
6a488035
TO
3317 }
3318 elseif (in_array($rel = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params),
9d72cede
EM
3319 array_keys($this->getOperationPair(CRM_Report_Form::OP_DATE))
3320 )) {
160d32e1 3321 $pair = $this->getOperationPair(CRM_Report_Form::OP_DATE);
be2fb01f 3322 $statistics['filters'][] = [
6a488035
TO
3323 'title' => $field['title'],
3324 'value' => $pair[$rel],
be2fb01f 3325 ];
6a488035
TO
3326 }
3327 }
3328 else {
3329 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
3330 $value = NULL;
3331 if ($op) {
1b36206c 3332 $pair = $this->getOperationPair(
8f1445ea
DL
3333 CRM_Utils_Array::value('operatorType', $field),
3334 $fieldName
6a488035
TO
3335 );
3336 $min = CRM_Utils_Array::value("{$fieldName}_min", $this->_params);
3337 $max = CRM_Utils_Array::value("{$fieldName}_max", $this->_params);
3338 $val = CRM_Utils_Array::value("{$fieldName}_value", $this->_params);
be2fb01f 3339 if (in_array($op, ['bw', 'nbw']) && ($min || $max)) {
2107cde9
CW
3340 $value = "{$pair[$op]} $min " . ts('and') . " $max";
3341 }
d3e86119 3342 elseif ($val && CRM_Utils_Array::value('operatorType', $field) & self::OP_ENTITYREF) {
2107cde9 3343 $this->setEntityRefDefaults($field, $tableName);
9d72cede 3344 $result = civicrm_api3($field['attributes']['entity'], 'getlist',
be2fb01f
CW
3345 ['id' => $val] +
3346 CRM_Utils_Array::value('api', $field['attributes'], []));
3347 $values = [];
2107cde9
CW
3348 foreach ($result['values'] as $v) {
3349 $values[] = $v['label'];
3350 }
3351 $value = "{$pair[$op]} " . implode(', ', $values);
6a488035
TO
3352 }
3353 elseif ($op == 'nll' || $op == 'nnll') {
3354 $value = $pair[$op];
3355 }
3356 elseif (is_array($val) && (!empty($val))) {
be2fb01f 3357 $options = CRM_Utils_Array::value('options', $field, []);
6a488035
TO
3358 foreach ($val as $key => $valIds) {
3359 if (isset($options[$valIds])) {
3360 $val[$key] = $options[$valIds];
3361 }
3362 }
9d72cede
EM
3363 $pair[$op] = (count($val) == 1) ? (($op == 'notin' || $op ==
3364 'mnot') ? ts('Is Not') : ts('Is')) : CRM_Utils_Array::value($op, $pair);
3365 $val = implode(', ', $val);
3366 $value = "{$pair[$op]} " . $val;
6a488035 3367 }
9d72cede
EM
3368 elseif (!is_array($val) && (!empty($val) || $val == '0') &&
3369 isset($field['options']) &&
6a488035
TO
3370 is_array($field['options']) && !empty($field['options'])
3371 ) {
9d72cede
EM
3372 $value = CRM_Utils_Array::value($op, $pair) . " " .
3373 CRM_Utils_Array::value($val, $field['options'], $val);
6a488035
TO
3374 }
3375 elseif ($val) {
3376 $value = CRM_Utils_Array::value($op, $pair) . " " . $val;
3377 }
3378 }
da7ac680 3379 if ($value && empty($field['no_display'])) {
be2fb01f 3380 $statistics['filters'][] = [
9d72cede 3381 'title' => CRM_Utils_Array::value('title', $field),
db371f3d 3382 'value' => CRM_Utils_String::htmlToText($value),
be2fb01f 3383 ];
6a488035
TO
3384 }
3385 }
3386 }
3387 }
3388 }
3389 }
3390
74cf4551 3391 /**
7d7c50f9
EM
3392 * End post processing.
3393 *
3394 * @param array|null $rows
74cf4551 3395 */
00be9182 3396 public function endPostProcess(&$rows = NULL) {
fae21877 3397 $this->assign('report_class', get_class($this));
9d72cede 3398 if ($this->_storeResultSet) {
ae555e90
DS
3399 $this->_resultSet = $rows;
3400 }
3401
6a488035
TO
3402 if ($this->_outputMode == 'print' ||
3403 $this->_outputMode == 'pdf' ||
3404 $this->_sendmail
3405 ) {
3406
3407 $content = $this->compileContent();
3408 $url = CRM_Utils_System::url("civicrm/report/instance/{$this->_id}",
9d72cede 3409 "reset=1", TRUE
6a488035
TO
3410 );
3411
3412 if ($this->_sendmail) {
3413 $config = CRM_Core_Config::singleton();
be2fb01f 3414 $attachments = [];
6a488035
TO
3415
3416 if ($this->_outputMode == 'csv') {
971d41b1
CW
3417 $content
3418 = $this->_formValues['report_header'] . '<p>' . ts('Report URL') .
9d72cede
EM
3419 ": {$url}</p>" . '<p>' .
3420 ts('The report is attached as a CSV file.') . '</p>' .
3421 $this->_formValues['report_footer'];
3422
3423 $csvFullFilename = $config->templateCompileDir .
3424 CRM_Utils_File::makeFileName('CiviReport.csv');
6a488035
TO
3425 $csvContent = CRM_Report_Utils_Report::makeCsv($this, $rows);
3426 file_put_contents($csvFullFilename, $csvContent);
be2fb01f 3427 $attachments[] = [
6a488035
TO
3428 'fullPath' => $csvFullFilename,
3429 'mime_type' => 'text/csv',
3430 'cleanName' => 'CiviReport.csv',
be2fb01f 3431 ];
6a488035
TO
3432 }
3433 if ($this->_outputMode == 'pdf') {
3434 // generate PDF content
9d72cede
EM
3435 $pdfFullFilename = $config->templateCompileDir .
3436 CRM_Utils_File::makeFileName('CiviReport.pdf');
6a488035
TO
3437 file_put_contents($pdfFullFilename,
3438 CRM_Utils_PDF_Utils::html2pdf($content, "CiviReport.pdf",
be2fb01f 3439 TRUE, ['orientation' => 'landscape']
6a488035
TO
3440 )
3441 );
3442 // generate Email Content
971d41b1
CW
3443 $content
3444 = $this->_formValues['report_header'] . '<p>' . ts('Report URL') .
9d72cede
EM
3445 ": {$url}</p>" . '<p>' .
3446 ts('The report is attached as a PDF file.') . '</p>' .
3447 $this->_formValues['report_footer'];
6a488035 3448
be2fb01f 3449 $attachments[] = [
6a488035
TO
3450 'fullPath' => $pdfFullFilename,
3451 'mime_type' => 'application/pdf',
3452 'cleanName' => 'CiviReport.pdf',
be2fb01f 3453 ];
6a488035
TO
3454 }
3455
3456 if (CRM_Report_Utils_Report::mailReport($content, $this->_id,
9d72cede
EM
3457 $this->_outputMode, $attachments
3458 )
3459 ) {
6a488035
TO
3460 CRM_Core_Session::setStatus(ts("Report mail has been sent."), ts('Sent'), 'success');
3461 }
3462 else {
3463 CRM_Core_Session::setStatus(ts("Report mail could not be sent."), ts('Mail Error'), 'error');
3464 }
73b448bf 3465 return;
6a488035
TO
3466 }
3467 elseif ($this->_outputMode == 'print') {
3468 echo $content;
3469 }
3470 else {
003ae4f2
RLAR
3471 // Nb. Once upon a time we used a package called Open Flash Charts to
3472 // draw charts, and we had a feature whereby a browser could send the
3473 // server a PNG version of the chart, which could then be included in a
3474 // PDF by including <img> tags in the HTML for the conversion below.
3475 //
3476 // This feature stopped working when browsers stopped supporting Flash,
3477 // and although we have a different client-side charting library in
3478 // place, we decided not to reimplement the (rather convoluted)
3479 // browser-sending-rendered-chart-to-server process.
3480 //
3481 // If this feature is required in future we should find a better way to
3482 // render charts on the server side, e.g. server-created SVG.
be2fb01f 3483 CRM_Utils_PDF_Utils::html2pdf($content, "CiviReport.pdf", FALSE, ['orientation' => 'landscape']);
6a488035
TO
3484 }
3485 CRM_Utils_System::civiExit();
3486 }
3487 elseif ($this->_outputMode == 'csv') {
3488 CRM_Report_Utils_Report::export2csv($this, $rows);
3489 }
3490 elseif ($this->_outputMode == 'group') {
3491 $group = $this->_params['groups'];
3492 $this->add2group($group);
3493 }
6a488035 3494 }
8f1445ea 3495
8a925f33 3496 /**
7d7c50f9
EM
3497 * Set store result set indicator to TRUE.
3498 *
8a925f33
EM
3499 * @todo explain what this does
3500 */
00be9182 3501 public function storeResultSet() {
ae555e90
DS
3502 $this->_storeResultSet = TRUE;
3503 }
3504
74cf4551 3505 /**
7d7c50f9
EM
3506 * Get result set.
3507 *
74cf4551
EM
3508 * @return bool
3509 */
00be9182 3510 public function getResultSet() {
ae555e90
DS
3511 return $this->_resultSet;
3512 }
3513
55f71fa7 3514 /**
3515 * Get the sql used to generate the report.
3516 *
3517 * @return string
3518 */
3519 public function getReportSql() {
3520 return $this->sqlArray;
3521 }
3522
74cf4551 3523 /**
fe482240 3524 * Use the form name to create the tpl file name.
74cf4551
EM
3525 *
3526 * @return string
74cf4551 3527 */
00be9182 3528 public function getTemplateFileName() {
6a488035 3529 $defaultTpl = parent::getTemplateFileName();
9d72cede 3530 $template = CRM_Core_Smarty::singleton();
6a488035
TO
3531 if (!$template->template_exists($defaultTpl)) {
3532 $defaultTpl = 'CRM/Report/Form.tpl';
3533 }
3534 return $defaultTpl;
3535 }
3536
688d37c6 3537 /**
fe482240 3538 * Compile the report content.
317a8023 3539 *
688d37c6 3540 * Although this function is super-short it is useful to keep separate so it can be over-ridden by report classes.
6a488035 3541 *
74cf4551
EM
3542 * @return string
3543 */
00be9182 3544 public function compileContent() {
8aac22c8 3545 $templateFile = $this->getHookedTemplateFileName();
1a7356e7 3546 return CRM_Utils_Array::value('report_header', $this->_formValues) .
9d72cede 3547 CRM_Core_Form::$_template->fetch($templateFile) .
1a7356e7 3548 CRM_Utils_Array::value('report_footer', $this->_formValues);
6a488035
TO
3549 }
3550
317a8023 3551 /**
3552 * Post process function.
3553 */
00be9182 3554 public function postProcess() {
6a488035
TO
3555 // get ready with post process params
3556 $this->beginPostProcess();
3557
3558 // build query
3559 $sql = $this->buildQuery();
3560
3561 // build array of result based on column headers. This method also allows
3562 // modifying column headers before using it to build result set i.e $rows.
be2fb01f 3563 $rows = [];
6a488035
TO
3564 $this->buildRows($sql, $rows);
3565
3566 // format result set.
3567 $this->formatDisplay($rows);
3568
3569 // assign variables to templates
3570 $this->doTemplateAssignment($rows);
3571
3572 // do print / pdf / instance stuff if needed
3573 $this->endPostProcess($rows);
3574 }
3575
74cf4551 3576 /**
317a8023 3577 * Set limit.
3578 *
74cf4551 3579 * @param int $rowCount
317a8023 3580 *
688d37c6 3581 * @return array
74cf4551 3582 */
00be9182 3583 public function limit($rowCount = self::ROW_COUNT_LIMIT) {
6a488035
TO
3584 // lets do the pager if in html mode
3585 $this->_limit = NULL;
77b97be7 3586
dbb4a0f9
PN
3587 // CRM-14115, over-ride row count if rowCount is specified in URL
3588 if ($this->_dashBoardRowCount) {
3589 $rowCount = $this->_dashBoardRowCount;
3590 }
182f5081 3591 if ($this->addPaging) {
f0b5b73e 3592 $this->_select = preg_replace('/SELECT(\s+SQL_CALC_FOUND_ROWS)?\s+/i', 'SELECT SQL_CALC_FOUND_ROWS ', $this->_select);
6a488035 3593
a3d827a7 3594 $pageId = CRM_Utils_Request::retrieve('crmPID', 'Integer');
6a488035 3595
8a925f33
EM
3596 // @todo all http vars should be extracted in the preProcess
3597 // - not randomly in the class
6a488035
TO
3598 if (!$pageId && !empty($_POST)) {
3599 if (isset($_POST['PagerBottomButton']) && isset($_POST['crmPID_B'])) {
8a925f33 3600 $pageId = max((int) $_POST['crmPID_B'], 1);
6a488035
TO
3601 }
3602 elseif (isset($_POST['PagerTopButton']) && isset($_POST['crmPID'])) {
8a925f33 3603 $pageId = max((int) $_POST['crmPID'], 1);
6a488035
TO
3604 }
3605 unset($_POST['crmPID_B'], $_POST['crmPID']);
3606 }
3607
3608 $pageId = $pageId ? $pageId : 1;
3609 $this->set(CRM_Utils_Pager::PAGE_ID, $pageId);
3610 $offset = ($pageId - 1) * $rowCount;
3611
bf00d1b6
DL
3612 $offset = CRM_Utils_Type::escape($offset, 'Int');
3613 $rowCount = CRM_Utils_Type::escape($rowCount, 'Int');
3614
dd3a4117 3615 $this->_limit = " LIMIT $offset, $rowCount";
be2fb01f 3616 return [$offset, $rowCount];
6a488035 3617 }
9d72cede
EM
3618 if ($this->_limitValue) {
3619 if ($this->_offsetValue) {
6f900755
E
3620 $this->_limit = " LIMIT {$this->_offsetValue}, {$this->_limitValue} ";
3621 }
3622 else {
3623 $this->_limit = " LIMIT " . $this->_limitValue;
3624 }
3625 }
6a488035
TO
3626 }
3627
74cf4551 3628 /**
317a8023 3629 * Set pager.
3630 *
74cf4551
EM
3631 * @param int $rowCount
3632 */
00be9182 3633 public function setPager($rowCount = self::ROW_COUNT_LIMIT) {
dbb4a0f9
PN
3634 // CRM-14115, over-ride row count if rowCount is specified in URL
3635 if ($this->_dashBoardRowCount) {
3636 $rowCount = $this->_dashBoardRowCount;
3637 }
3638
6a488035 3639 if ($this->_limit && ($this->_limit != '')) {
c160fde8 3640 if (!$this->_rowsFound) {
3641 $sql = "SELECT FOUND_ROWS();";
3642 $this->_rowsFound = CRM_Core_DAO::singleValueQuery($sql);
3643 }
be2fb01f 3644 $params = [
6a488035
TO
3645 'total' => $this->_rowsFound,
3646 'rowCount' => $rowCount,
3647 'status' => ts('Records') . ' %%StatusMessage%%',
3648 'buttonBottom' => 'PagerBottomButton',
3649 'buttonTop' => 'PagerTopButton',
be2fb01f 3650 ];
f45db615 3651 if (!empty($this->controller)) {
3652 // This happens when being called from the api Really we want the api to be able to
3653 // pass paging parameters, but at this stage just preventing test crashes.
3654 $params['pageID'] = $this->get(CRM_Utils_Pager::PAGE_ID);
3655 }
6a488035
TO
3656
3657 $pager = new CRM_Utils_Pager($params);
3658 $this->assign_by_ref('pager', $pager);
ecc20f0e 3659 $this->ajaxResponse['totalRows'] = $this->_rowsFound;
6a488035
TO
3660 }
3661 }
3662
74cf4551 3663 /**
43c1fa19 3664 * Build a group filter with contempt for large data sets.
3665 *
3666 * This function has been retained as it takes time to migrate the reports over
3667 * to the new method which will not crash on large datasets.
3668 *
3669 * @deprecated
317a8023 3670 *
3671 * @param string $field
3672 * @param mixed $value
3673 * @param string $op
74cf4551
EM
3674 *
3675 * @return string
3676 */
43c1fa19 3677 public function legacySlowGroupFilterClause($field, $value, $op) {
6a488035
TO
3678 $smartGroupQuery = "";
3679
3680 $group = new CRM_Contact_DAO_Group();
3681 $group->is_active = 1;
3682 $group->find();
be2fb01f 3683 $smartGroups = [];
6a488035 3684 while ($group->fetch()) {
eae0f0d9 3685 if (in_array($group->id, (array) $this->_params['gid_value']) &&
9d72cede
EM
3686 $group->saved_search_id
3687 ) {
6a488035
TO
3688 $smartGroups[] = $group->id;
3689 }
3690 }
3691
3692 CRM_Contact_BAO_GroupContactCache::check($smartGroups);
3693
3694 $smartGroupQuery = '';
3695 if (!empty($smartGroups)) {
3696 $smartGroups = implode(',', $smartGroups);
3697 $smartGroupQuery = " UNION DISTINCT
3698 SELECT DISTINCT smartgroup_contact.contact_id
3699 FROM civicrm_group_contact_cache smartgroup_contact
3700 WHERE smartgroup_contact.group_id IN ({$smartGroups}) ";
3701 }
3702
29fc2b79 3703 $sqlOp = $this->getSQLOperator($op);
6a488035 3704 if (!is_array($value)) {
be2fb01f 3705 $value = [$value];
6a488035 3706 }
8e4785fa 3707 //include child groups if any
3708 $value = array_merge($value, CRM_Contact_BAO_Group::getChildGroupIds($value));
3709
6a488035
TO
3710 $clause = "{$field['dbAlias']} IN (" . implode(', ', $value) . ")";
3711
f3e8e853 3712 $contactAlias = $this->_aliases['civicrm_contact'];
3713 if (!empty($this->relationType) && $this->relationType == 'b_a') {
3714 $contactAlias = $this->_aliases['civicrm_contact_b'];
3715 }
3716 return " {$contactAlias}.id {$sqlOp} (
6a488035
TO
3717 SELECT DISTINCT {$this->_aliases['civicrm_group']}.contact_id
3718 FROM civicrm_group_contact {$this->_aliases['civicrm_group']}
3719 WHERE {$clause} AND {$this->_aliases['civicrm_group']}.status = 'Added'
3720 {$smartGroupQuery} ) ";
3721 }
3722
43c1fa19 3723 /**
3724 * Build where clause for groups.
3725 *
3726 * @param string $field
3727 * @param mixed $value
3728 * @param string $op
3729 *
3730 * @return string
3731 */
3732 public function whereGroupClause($field, $value, $op) {
3733 if ($this->groupFilterNotOptimised) {
3734 return $this->legacySlowGroupFilterClause($field, $value, $op);
3735 }
3736 if ($op === 'notin') {
3737 return " group_temp_table.id IS NULL ";
3738 }
3739 // We will have used an inner join instead.
3740 return "1";
3741 }
3742
43c1fa19 3743 /**
3744 * Create a table of the contact ids included by the group filter.
3745 *
3746 * This function is called by both the api (tests) and the UI.
3747 */
3748 public function buildGroupTempTable() {
a2e4e741 3749 if (!empty($this->groupTempTable) || empty($this->_params['gid_value']) || $this->groupFilterNotOptimised) {
43c1fa19 3750 return;
3751 }
3752 $filteredGroups = (array) $this->_params['gid_value'];
3753
be2fb01f 3754 $groups = civicrm_api3('Group', 'get', [
43c1fa19 3755 'is_active' => 1,
be2fb01f
CW
3756 'id' => ['IN' => $filteredGroups],
3757 'saved_search_id' => ['>' => 0],
43c1fa19 3758 'return' => 'id',
be2fb01f 3759 ]);
43c1fa19 3760 $smartGroups = array_keys($groups['values']);
3761
3762 $query = "
b6963180 3763 SELECT DISTINCT group_contact.contact_id as id
43c1fa19 3764 FROM civicrm_group_contact group_contact
3765 WHERE group_contact.group_id IN (" . implode(', ', $filteredGroups) . ")
3766 AND group_contact.status = 'Added' ";
3767
3768 if (!empty($smartGroups)) {
3769 CRM_Contact_BAO_GroupContactCache::check($smartGroups);
3770 $smartGroups = implode(',', $smartGroups);
3771 $query .= "
3772 UNION DISTINCT
3773 SELECT smartgroup_contact.contact_id as id
3774 FROM civicrm_group_contact_cache smartgroup_contact
3775 WHERE smartgroup_contact.group_id IN ({$smartGroups}) ";
3776 }
3777
f0197a3d 3778 $this->groupTempTable = $this->createTemporaryTable('rptgrp', $query);
43c1fa19 3779 CRM_Core_DAO::executeQuery("ALTER TABLE $this->groupTempTable ADD INDEX i_id(id)");
3780 }
3781
3782 /**
3783 * Execute query and add it to the developer tab.
3784 *
3785 * @param string $query
3786 * @param array $params
3787 *
3788 * @return \CRM_Core_DAO|object
3789 */
be2fb01f 3790 protected function executeReportQuery($query, $params = []) {
43c1fa19 3791 $this->addToDeveloperTab($query);
3792 return CRM_Core_DAO::executeQuery($query, $params);
3793 }
3794
74cf4551 3795 /**
317a8023 3796 * Build where clause for tags.
3797 *
3798 * @param string $field
3799 * @param mixed $value
3800 * @param string $op
74cf4551
EM
3801 *
3802 * @return string
3803 */
00be9182 3804 public function whereTagClause($field, $value, $op) {
6a488035
TO
3805 // not using left join in query because if any contact
3806 // belongs to more than one tag, results duplicate
3807 // entries.
29fc2b79 3808 $sqlOp = $this->getSQLOperator($op);
6a488035 3809 if (!is_array($value)) {
be2fb01f 3810 $value = [$value];
6a488035
TO
3811 }
3812 $clause = "{$field['dbAlias']} IN (" . implode(', ', $value) . ")";
ed795723
JM
3813 $entity_table = $this->_tagFilterTable;
3814 return " {$this->_aliases[$entity_table]}.id {$sqlOp} (
6a488035
TO
3815 SELECT DISTINCT {$this->_aliases['civicrm_tag']}.entity_id
3816 FROM civicrm_entity_tag {$this->_aliases['civicrm_tag']}
ed795723 3817 WHERE entity_table = '$entity_table' AND {$clause} ) ";
6a488035
TO
3818 }
3819
f587aa17
EM
3820 /**
3821 * Generate membership organization clause.
3822 *
3823 * @param mixed $value
3824 * @param string $op SQL Operator
3825 *
3826 * @return string
3827 */
3828 public function whereMembershipOrgClause($value, $op) {
114a2c85
DG
3829 $sqlOp = $this->getSQLOperator($op);
3830 if (!is_array($value)) {
be2fb01f 3831 $value = [$value];
114a2c85 3832 }
258e2add 3833
114a2c85
DG
3834 $tmp_membership_org_sql_list = implode(', ', $value);
3835 return " {$this->_aliases['civicrm_contact']}.id {$sqlOp} (
97709b72 3836 SELECT DISTINCT mem.contact_id
258e2add 3837 FROM civicrm_membership mem
97709b72 3838 LEFT JOIN civicrm_membership_status mem_status ON mem.status_id = mem_status.id
258e2add 3839 LEFT JOIN civicrm_membership_type mt ON mem.membership_type_id = mt.id
9d72cede
EM
3840 WHERE mt.member_of_contact_id IN (" .
3841 $tmp_membership_org_sql_list . ")
97709b72
DG
3842 AND mt.is_active = '1'
3843 AND mem_status.is_current_member = '1'
3844 AND mem_status.is_active = '1' ) ";
9d72cede 3845 }
114a2c85 3846
f587aa17 3847 /**
fe482240 3848 * Generate Membership Type SQL Clause.
317a8023 3849 *
f587aa17
EM
3850 * @param mixed $value
3851 * @param string $op
3852 *
3853 * @return string
3854 * SQL query string
3855 */
3856 public function whereMembershipTypeClause($value, $op) {
114a2c85
DG
3857 $sqlOp = $this->getSQLOperator($op);
3858 if (!is_array($value)) {
be2fb01f 3859 $value = [$value];
114a2c85 3860 }
258e2add 3861
9d72cede 3862 $tmp_membership_sql_list = implode(', ', $value);
114a2c85 3863 return " {$this->_aliases['civicrm_contact']}.id {$sqlOp} (
97709b72 3864 SELECT DISTINCT mem.contact_id
258e2add 3865 FROM civicrm_membership mem
3866 LEFT JOIN civicrm_membership_status mem_status ON mem.status_id = mem_status.id
3867 LEFT JOIN civicrm_membership_type mt ON mem.membership_type_id = mt.id
9d72cede
EM
3868 WHERE mem.membership_type_id IN (" .
3869 $tmp_membership_sql_list . ")
97709b72
DG
3870 AND mt.is_active = '1'
3871 AND mem_status.is_current_member = '1'
3872 AND mem_status.is_active = '1' ) ";
114a2c85 3873 }
258e2add 3874
74cf4551 3875 /**
f0384ec0
CW
3876 * Buld contact acl clause
3877 * @deprecated in favor of buildPermissionClause
317a8023 3878 *
74cf4551
EM
3879 * @param string $tableAlias
3880 */
00be9182 3881 public function buildACLClause($tableAlias = 'contact_a') {
6a488035
TO
3882 list($this->_aclFrom, $this->_aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause($tableAlias);
3883 }
3884
f0384ec0
CW
3885 /**
3886 * Build the permision clause for all entities in this report
3887 */
3888 public function buildPermissionClause() {
be2fb01f 3889 $ret = [];
f0384ec0
CW
3890 foreach ($this->selectedTables() as $tableName) {
3891 $baoName = str_replace('_DAO_', '_BAO_', CRM_Core_DAO_AllCoreTables::getClassForTable($tableName));
3892 if ($baoName && class_exists($baoName) && !empty($this->_columns[$tableName]['alias'])) {
3893 $tableAlias = $this->_columns[$tableName]['alias'];
3894 $clauses = array_filter($baoName::getSelectWhereClause($tableAlias));
3895 foreach ($clauses as $field => $clause) {
3896 // Skip contact_id field if redundant
3897 if ($field != 'contact_id' || !in_array('civicrm_contact', $this->selectedTables())) {
3898 $ret["$tableName.$field"] = $clause;
3899 }
3900 }
3901 }
3902 }
3903 // Override output from buildACLClause
3904 $this->_aclFrom = NULL;
3905 $this->_aclWhere = implode(' AND ', $ret);
3906 }
3907
74cf4551 3908 /**
317a8023 3909 * Add custom data to the columns.
3910 *
74cf4551
EM
3911 * @param bool $addFields
3912 * @param array $permCustomGroupIds
3913 */
be2fb01f 3914 public function addCustomDataToColumns($addFields = TRUE, $permCustomGroupIds = []) {
6a488035
TO
3915 if (empty($this->_customGroupExtends)) {
3916 return;
3917 }
3918 if (!is_array($this->_customGroupExtends)) {
be2fb01f 3919 $this->_customGroupExtends = [$this->_customGroupExtends];
6a488035
TO
3920 }
3921 $customGroupWhere = '';
3922 if (!empty($permCustomGroupIds)) {
9d72cede
EM
3923 $customGroupWhere = "cg.id IN (" . implode(',', $permCustomGroupIds) .
3924 ") AND";
6a488035
TO
3925 }
3926 $sql = "
3927SELECT cg.table_name, cg.title, cg.extends, cf.id as cf_id, cf.label,
3928 cf.column_name, cf.data_type, cf.html_type, cf.option_group_id, cf.time_format
3929FROM civicrm_custom_group cg
3930INNER JOIN civicrm_custom_field cf ON cg.id = cf.custom_group_id
3931WHERE cg.extends IN ('" . implode("','", $this->_customGroupExtends) . "') AND
3932 {$customGroupWhere}
3933 cg.is_active = 1 AND
3934 cf.is_active = 1 AND
3935 cf.is_searchable = 1
3936ORDER BY cg.weight, cf.weight";
3937 $customDAO = CRM_Core_DAO::executeQuery($sql);
3938
3939 $curTable = NULL;
3940 while ($customDAO->fetch()) {
3941 if ($customDAO->table_name != $curTable) {
3942 $curTable = $customDAO->table_name;
be2fb01f 3943 $curFields = $curFilters = [];
6a488035
TO
3944
3945 // dummy dao object
3946 $this->_columns[$curTable]['dao'] = 'CRM_Contact_DAO_Contact';
3947 $this->_columns[$curTable]['extends'] = $customDAO->extends;
3948 $this->_columns[$curTable]['grouping'] = $customDAO->table_name;
3949 $this->_columns[$curTable]['group_title'] = $customDAO->title;
3950
be2fb01f 3951 foreach (['fields', 'filters', 'group_bys'] as $colKey) {
6a488035 3952 if (!array_key_exists($colKey, $this->_columns[$curTable])) {
be2fb01f 3953 $this->_columns[$curTable][$colKey] = [];
6a488035
TO
3954 }
3955 }
3956 }
3957 $fieldName = 'custom_' . $customDAO->cf_id;
3958
3959 if ($addFields) {
3960 // this makes aliasing work in favor
be2fb01f 3961 $curFields[$fieldName] = [
6a488035
TO
3962 'name' => $customDAO->column_name,
3963 'title' => $customDAO->label,
3964 'dataType' => $customDAO->data_type,
3965 'htmlType' => $customDAO->html_type,
be2fb01f 3966 ];
6a488035
TO
3967 }
3968 if ($this->_customGroupFilters) {
3969 // this makes aliasing work in favor
be2fb01f 3970 $curFilters[$fieldName] = [
6a488035
TO
3971 'name' => $customDAO->column_name,
3972 'title' => $customDAO->label,
3973 'dataType' => $customDAO->data_type,
3974 'htmlType' => $customDAO->html_type,
be2fb01f 3975 ];
6a488035
TO
3976 }
3977
3978 switch ($customDAO->data_type) {
3979 case 'Date':
3980 // filters
3981 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form::OP_DATE;
3982 $curFilters[$fieldName]['type'] = CRM_Utils_Type::T_DATE;
3983 // CRM-6946, show time part for datetime date fields
3984 if ($customDAO->time_format) {
3985 $curFields[$fieldName]['type'] = CRM_Utils_Type::T_TIMESTAMP;
3986 }
3987 break;
3988
3989 case 'Boolean':
3990 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form::OP_SELECT;
be2fb01f 3991 $curFilters[$fieldName]['options'] = ['' => ts('- select -')] + CRM_Core_PseudoConstant::get('CRM_Core_BAO_CustomField', 'custom_' . $customDAO->cf_id, [], 'search');
6a488035
TO
3992 $curFilters[$fieldName]['type'] = CRM_Utils_Type::T_INT;
3993 break;
3994
3995 case 'Int':
3996 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form::OP_INT;
3997 $curFilters[$fieldName]['type'] = CRM_Utils_Type::T_INT;
3998 break;
3999
4000 case 'Money':
4001 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form::OP_FLOAT;
4002 $curFilters[$fieldName]['type'] = CRM_Utils_Type::T_MONEY;
c160fde8 4003 $curFields[$fieldName]['type'] = CRM_Utils_Type::T_MONEY;
6a488035
TO
4004 break;
4005
4006 case 'Float':
4007 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form::OP_FLOAT;
4008 $curFilters[$fieldName]['type'] = CRM_Utils_Type::T_FLOAT;
4009 break;
4010
4011 case 'String':
6a488035 4012 case 'StateProvince':
6a488035 4013 case 'Country':
106e51b4
CW
4014 $curFilters[$fieldName]['type'] = CRM_Utils_Type::T_STRING;
4015
be2fb01f 4016 $options = CRM_Core_PseudoConstant::get('CRM_Core_BAO_CustomField', 'custom_' . $customDAO->cf_id, [], 'search');
03b5a4aa 4017 if ((is_array($options) && count($options) != 0) || (!is_array($options) && $options !== FALSE)) {
106e51b4
CW
4018 $curFilters[$fieldName]['operatorType'] = CRM_Core_BAO_CustomField::isSerialized($customDAO) ? CRM_Report_Form::OP_MULTISELECT_SEPARATOR : CRM_Report_Form::OP_MULTISELECT;
4019 $curFilters[$fieldName]['options'] = $options;
6a488035 4020 }
6a488035
TO
4021 break;
4022
4023 case 'ContactReference':
4024 $curFilters[$fieldName]['type'] = CRM_Utils_Type::T_STRING;
4025 $curFilters[$fieldName]['name'] = 'display_name';
4026 $curFilters[$fieldName]['alias'] = "contact_{$fieldName}_civireport";
4027
4028 $curFields[$fieldName]['type'] = CRM_Utils_Type::T_STRING;
4029 $curFields[$fieldName]['name'] = 'display_name';
4030 $curFields[$fieldName]['alias'] = "contact_{$fieldName}_civireport";
4031 break;
4032
4033 default:
4034 $curFields[$fieldName]['type'] = CRM_Utils_Type::T_STRING;
4035 $curFilters[$fieldName]['type'] = CRM_Utils_Type::T_STRING;
4036 }
4037
d79e67a6 4038 // CRM-19401 fix
4039 if ($customDAO->html_type == 'Select' && !array_key_exists('options', $curFilters[$fieldName])) {
be2fb01f 4040 $options = CRM_Core_PseudoConstant::get('CRM_Core_BAO_CustomField', 'custom_' . $customDAO->cf_id, [], 'search');
d79e67a6 4041 if ($options !== FALSE) {
4042 $curFilters[$fieldName]['operatorType'] = CRM_Core_BAO_CustomField::isSerialized($customDAO) ? CRM_Report_Form::OP_MULTISELECT_SEPARATOR : CRM_Report_Form::OP_MULTISELECT;
4043 $curFilters[$fieldName]['options'] = $options;
4044 }
4045 }
4046
6a488035 4047 if (!array_key_exists('type', $curFields[$fieldName])) {
be2fb01f 4048 $curFields[$fieldName]['type'] = CRM_Utils_Array::value('type', $curFilters[$fieldName], []);
6a488035
TO
4049 }
4050
4051 if ($addFields) {
4052 $this->_columns[$curTable]['fields'] = array_merge($this->_columns[$curTable]['fields'], $curFields);
4053 }
4054 if ($this->_customGroupFilters) {
4055 $this->_columns[$curTable]['filters'] = array_merge($this->_columns[$curTable]['filters'], $curFilters);
4056 }
4057 if ($this->_customGroupGroupBy) {
4058 $this->_columns[$curTable]['group_bys'] = array_merge($this->_columns[$curTable]['group_bys'], $curFields);
4059 }
4060 }
4061 }
4062
317a8023 4063 /**
4064 * Build custom data from clause.
568a0946 4065 *
4066 * @param bool $joinsForFiltersOnly
4067 * Only include joins to support filters. This would be used if creating a table of contacts to include first.
317a8023 4068 */
568a0946 4069 public function customDataFrom($joinsForFiltersOnly = FALSE) {
6a488035
TO
4070 if (empty($this->_customGroupExtends)) {
4071 return;
4072 }
4073 $mapper = CRM_Core_BAO_CustomQuery::$extendsMap;
fae6de7c
JL
4074 //CRM-18276 GROUP_CONCAT could be used with singleValueQuery and then exploded,
4075 //but by default that truncates to 1024 characters, which causes errors with installs with lots of custom field sets
be2fb01f 4076 $customTables = [];
33621c4f 4077 $customTablesDAO = CRM_Core_DAO::executeQuery("SELECT table_name FROM civicrm_custom_group");
fae6de7c
JL
4078 while ($customTablesDAO->fetch()) {
4079 $customTables[] = $customTablesDAO->table_name;
4080 }
6a488035
TO
4081
4082 foreach ($this->_columns as $table => $prop) {
55f71fa7 4083 if (in_array($table, $customTables)) {
6a488035 4084 $extendsTable = $mapper[$prop['extends']];
568a0946 4085 // Check field is required for rendering the report.
4086 if ((!$this->isFieldSelected($prop)) || ($joinsForFiltersOnly && !$this->isFieldFiltered($prop))) {
6a488035
TO
4087 continue;
4088 }
4c9d78ea 4089 $baseJoin = CRM_Utils_Array::value($prop['extends'], $this->_customGroupExtendsJoin, "{$this->_aliases[$extendsTable]}.id");
6a488035 4090
9d72cede 4091 $customJoin = is_array($this->_customGroupJoin) ? $this->_customGroupJoin[$table] : $this->_customGroupJoin;
6a488035 4092 $this->_from .= "
aa1aa08e 4093{$customJoin} {$table} {$this->_aliases[$table]} ON {$this->_aliases[$table]}.entity_id = {$baseJoin}";
6a488035
TO
4094 // handle for ContactReference
4095 if (array_key_exists('fields', $prop)) {
4096 foreach ($prop['fields'] as $fieldName => $field) {
9d72cede
EM
4097 if (CRM_Utils_Array::value('dataType', $field) ==
4098 'ContactReference'
4099 ) {
6a488035
TO
4100 $columnName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', CRM_Core_BAO_CustomField::getKeyID($fieldName), 'column_name');
4101 $this->_from .= "
4102LEFT JOIN civicrm_contact {$field['alias']} ON {$field['alias']}.id = {$this->_aliases[$table]}.{$columnName} ";
4103 }
4104 }
4105 }
4106 }
4107 }
4108 }
4109
74cf4551 4110 /**
317a8023 4111 * Check if the field is selected.
4112 *
4113 * @param string $prop
74cf4551
EM
4114 *
4115 * @return bool
4116 */
00be9182 4117 public function isFieldSelected($prop) {
6a488035
TO
4118 if (empty($prop)) {
4119 return FALSE;
4120 }
4121
4122 if (!empty($this->_params['fields'])) {
4123 foreach (array_keys($prop['fields']) as $fieldAlias) {
4124 $customFieldId = CRM_Core_BAO_CustomField::getKeyID($fieldAlias);
4125 if ($customFieldId) {
4126 if (array_key_exists($fieldAlias, $this->_params['fields'])) {
4127 return TRUE;
4128 }
4129
4130 //might be survey response field.
9d72cede
EM
4131 if (!empty($this->_params['fields']['survey_response']) &&
4132 !empty($prop['fields'][$fieldAlias]['isSurveyResponseField'])
4133 ) {
6a488035
TO
4134 return TRUE;
4135 }
4136 }
4137 }
4138 }
4139
4140 if (!empty($this->_params['group_bys']) && $this->_customGroupGroupBy) {
4141 foreach (array_keys($prop['group_bys']) as $fieldAlias) {
9d72cede
EM
4142 if (array_key_exists($fieldAlias, $this->_params['group_bys']) &&
4143 CRM_Core_BAO_CustomField::getKeyID($fieldAlias)
4144 ) {
6a488035
TO
4145 return TRUE;
4146 }
4147 }
4148 }
4149
4150 if (!empty($this->_params['order_bys'])) {
4151 foreach (array_keys($prop['fields']) as $fieldAlias) {
4152 foreach ($this->_params['order_bys'] as $orderBy) {
9d72cede
EM
4153 if ($fieldAlias == $orderBy['column'] &&
4154 CRM_Core_BAO_CustomField::getKeyID($fieldAlias)
4155 ) {
6a488035
TO
4156 return TRUE;
4157 }
4158 }
4159 }
4160 }
568a0946 4161 return $this->isFieldFiltered($prop);
4162
4163 }
6a488035 4164
568a0946 4165 /**
4166 * Check if the field is used as a filter.
4167 *
4168 * @param string $prop
4169 *
4170 * @return bool
4171 */
4172 protected function isFieldFiltered($prop) {
6a488035
TO
4173 if (!empty($prop['filters']) && $this->_customGroupFilters) {
4174 foreach ($prop['filters'] as $fieldAlias => $val) {
be2fb01f 4175 foreach ([
c86d4e7c
SL
4176 'value',
4177 'min',
4178 'max',
4179 'relative',
4180 'from',
4181 'to',
4182 ] as $attach) {
6a488035 4183 if (isset($this->_params[$fieldAlias . '_' . $attach]) &&
dfe4b2f5 4184 (!empty($this->_params[$fieldAlias . '_' . $attach])
9d72cede
EM
4185 || ($attach != 'relative' &&
4186 $this->_params[$fieldAlias . '_' . $attach] == '0')
dfe4b2f5 4187 )
9d72cede 4188 ) {
6a488035
TO
4189 return TRUE;
4190 }
4191 }
a7488080 4192 if (!empty($this->_params[$fieldAlias . '_op']) &&
be2fb01f 4193 in_array($this->_params[$fieldAlias . '_op'], ['nll', 'nnll'])
6a488035
TO
4194 ) {
4195 return TRUE;
4196 }
4197 }
4198 }
4199
4200 return FALSE;
4201 }
4202
4203 /**
31a8b5f0 4204 * Check for empty order_by configurations and remove them.
4205 *
4206 * Also set template to hide them.
537c70b8
EM
4207 *
4208 * @param array $formValues
6a488035 4209 */
00be9182 4210 public function preProcessOrderBy(&$formValues) {
6a488035 4211 // Object to show/hide form elements
ae555e90 4212 $_showHide = new CRM_Core_ShowHideBlocks('', '');
6a488035
TO
4213
4214 $_showHide->addShow('optionField_1');
4215
4216 // Cycle through order_by options; skip any empty ones, and hide them as well
4217 $n = 1;
4218
4219 if (!empty($formValues['order_bys'])) {
4220 foreach ($formValues['order_bys'] as $order_by) {
4221 if ($order_by['column'] && $order_by['column'] != '-') {
4222 $_showHide->addShow('optionField_' . $n);
4223 $orderBys[$n] = $order_by;
4224 $n++;
4225 }
4226 }
4227 }
4228 for ($i = $n; $i <= 5; $i++) {
4229 if ($i > 1) {
4230 $_showHide->addHide('optionField_' . $i);
4231 }
4232 }
4233
4234 // overwrite order_by options with modified values
4235 if (!empty($orderBys)) {
4236 $formValues['order_bys'] = $orderBys;
4237 }
4238 else {
be2fb01f 4239 $formValues['order_bys'] = [1 => ['column' => '-']];
6a488035
TO
4240 }
4241
4242 // assign show/hide data to template
4243 $_showHide->addToTemplate();
4244 }
4245
4246 /**
317a8023 4247 * Check if table name has columns in SELECT clause.
6a488035 4248 *
7e06c9f5
TO
4249 * @param string $tableName
4250 * Name of table (index of $this->_columns array).
6a488035
TO
4251 *
4252 * @return bool
4253 */
00be9182 4254 public function isTableSelected($tableName) {
6a488035
TO
4255 return in_array($tableName, $this->selectedTables());
4256 }
4257
568a0946 4258 /**
4259 * Check if table name has columns in WHERE or HAVING clause.
4260 *
4261 * @param string $tableName
4262 * Name of table (index of $this->_columns array).
4263 *
4264 * @return bool
4265 */
4266 public function isTableFiltered($tableName) {
4267 // Cause the array to be generated if not previously done.
4268 if (!$this->_selectedTables && !$this->filteredTables) {
4269 $this->selectedTables();
4270 }
4271 return in_array($tableName, $this->filteredTables);
4272 }
4273
6a488035 4274 /**
fe482240 4275 * Fetch array of DAO tables having columns included in SELECT or ORDER BY clause.
317a8023 4276 *
4277 * If the array is unset it will be built.
6a488035 4278 *
971d41b1
CW
4279 * @return array
4280 * selectedTables
6a488035 4281 */
00be9182 4282 public function selectedTables() {
6a488035 4283 if (!$this->_selectedTables) {
be2fb01f 4284 $orderByColumns = [];
9d72cede
EM
4285 if (array_key_exists('order_bys', $this->_params) &&
4286 is_array($this->_params['order_bys'])
4287 ) {
6a488035
TO
4288 foreach ($this->_params['order_bys'] as $orderBy) {
4289 $orderByColumns[] = $orderBy['column'];
4290 }
4291 }
4292
4293 foreach ($this->_columns as $tableName => $table) {
4294 if (array_key_exists('fields', $table)) {
4295 foreach ($table['fields'] as $fieldName => $field) {
9d72cede
EM
4296 if (!empty($field['required']) ||
4297 !empty($this->_params['fields'][$fieldName])
4298 ) {
6a488035
TO
4299 $this->_selectedTables[] = $tableName;
4300 break;
4301 }
4302 }
4303 }
4304 if (array_key_exists('order_bys', $table)) {
4305 foreach ($table['order_bys'] as $orderByName => $orderBy) {
4306 if (in_array($orderByName, $orderByColumns)) {
4307 $this->_selectedTables[] = $tableName;
4308 break;
4309 }
4310 }
4311 }
4312 if (array_key_exists('filters', $table)) {
4313 foreach ($table['filters'] as $filterName => $filter) {
067705c3
PN
4314 if ((isset($this->_params["{$filterName}_value"])
4315 && !CRM_Utils_System::isNull($this->_params["{$filterName}_value"]))
3d8f552e 4316 || !empty($this->_params["{$filterName}_relative"])
4317 || CRM_Utils_Array::value("{$filterName}_op", $this->_params) ==
4318 'nll'
4319 || CRM_Utils_Array::value("{$filterName}_op", $this->_params) ==
9d72cede 4320 'nnll'
6a488035
TO
4321 ) {
4322 $this->_selectedTables[] = $tableName;
568a0946 4323 $this->filteredTables[] = $tableName;
6a488035
TO
4324 break;
4325 }
4326 }
4327 }
4328 }
4329 }
4330 return $this->_selectedTables;
4331 }
4332
689c6bd2 4333 /**
4334 * Add campaign fields.
c86d4e7c 4335 * @param string $entityTable
689c6bd2 4336 * @param bool $groupBy
4337 * Add GroupBy? Not appropriate for detail report.
4338 * @param bool $orderBy
4339 * Add OrderBy? Not appropriate for detail report.
4340 * @param bool $filters
4341 *
4342 */
4343 public function addCampaignFields($entityTable = 'civicrm_contribution', $groupBy = FALSE, $orderBy = FALSE, $filters = TRUE) {
4344 // Check if CiviCampaign is a) enabled and b) has active campaigns
4345 $config = CRM_Core_Config::singleton();
4346 $campaignEnabled = in_array('CiviCampaign', $config->enableComponents);
4347 if ($campaignEnabled) {
4348 $getCampaigns = CRM_Campaign_BAO_Campaign::getPermissionedCampaigns(NULL, NULL, FALSE, FALSE, TRUE);
4349 // If we have a campaign, build out the relevant elements
4350 if (!empty($getCampaigns['campaigns'])) {
4238f84b 4351 $this->campaigns = $getCampaigns['campaigns'];
4352 asort($this->campaigns);
be2fb01f 4353 $this->_columns[$entityTable]['fields']['campaign_id'] = ['title' => ts('Campaign'), 'default' => 'false'];
689c6bd2 4354 if ($filters) {
be2fb01f 4355 $this->_columns[$entityTable]['filters']['campaign_id'] = [
689c6bd2 4356 'title' => ts('Campaign'),
4357 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
4238f84b 4358 'options' => $this->campaigns,
689c6bd2 4359 'type' => CRM_Utils_Type::T_INT,
be2fb01f 4360 ];
689c6bd2 4361 }
9db913d3 4362
689c6bd2 4363 if ($groupBy) {
be2fb01f 4364 $this->_columns[$entityTable]['group_bys']['campaign_id'] = ['title' => ts('Campaign')];
689c6bd2 4365 }
9db913d3 4366
4367 if ($orderBy) {
be2fb01f 4368 $this->_columns[$entityTable]['order_bys']['campaign_id'] = ['title' => ts('Campaign')];
9db913d3 4369 }
689c6bd2 4370 }
4371 }
4372 }
4373
850e4640 4374 /**
317a8023 4375 * Add address fields.
4376 *
850e4640
E
4377 * @deprecated - use getAddressColumns which is a more accurate description
4378 * and also accepts an array of options rather than a long list
4379 *
c490a46a 4380 * adding address fields to construct function in reports
77b97be7 4381 *
7e06c9f5
TO
4382 * @param bool $groupBy
4383 * Add GroupBy? Not appropriate for detail report.
4384 * @param bool $orderBy
4385 * Add GroupBy? Not appropriate for detail report.
77b97be7
EM
4386 * @param bool $filters
4387 * @param array $defaults
4388 *
a6c01b45
CW
4389 * @return array
4390 * address fields for construct clause
6a488035 4391 */
be2fb01f
CW
4392 public function addAddressFields($groupBy = TRUE, $orderBy = FALSE, $filters = TRUE, $defaults = ['country_id' => TRUE]) {
4393 $defaultAddressFields = [
8b2dc0b1
PN
4394 'street_address' => ts('Street Address'),
4395 'supplemental_address_1' => ts('Supplementary Address Field 1'),
4396 'supplemental_address_2' => ts('Supplementary Address Field 2'),
207f62c6 4397 'supplemental_address_3' => ts('Supplementary Address Field 3'),
8b2dc0b1
PN
4398 'street_number' => ts('Street Number'),
4399 'street_name' => ts('Street Name'),
4400 'street_unit' => ts('Street Unit'),
4401 'city' => ts('City'),
4402 'postal_code' => ts('Postal Code'),
4403 'postal_code_suffix' => ts('Postal Code Suffix'),
4404 'country_id' => ts('Country'),
4405 'state_province_id' => ts('State/Province'),
4406 'county_id' => ts('County'),
be2fb01f
CW
4407 ];
4408 $addressFields = [
4409 'civicrm_address' => [
6a488035 4410 'dao' => 'CRM_Core_DAO_Address',
be2fb01f
CW
4411 'fields' => [
4412 'address_name' => [
9d72cede 4413 'title' => ts('Address Name'),
6a488035 4414 'default' => CRM_Utils_Array::value('name', $defaults, FALSE),
e5575773 4415 'name' => 'name',
be2fb01f
CW
4416 ],
4417 ],
6a488035 4418 'grouping' => 'location-fields',
be2fb01f
CW
4419 ],
4420 ];
8b2dc0b1 4421 foreach ($defaultAddressFields as $fieldName => $fieldLabel) {
be2fb01f 4422 $addressFields['civicrm_address']['fields'][$fieldName] = [
8b2dc0b1
PN
4423 'title' => $fieldLabel,
4424 'default' => CRM_Utils_Array::value($fieldName, $defaults, FALSE),
be2fb01f 4425 ];
8b2dc0b1 4426 }
6a488035 4427
be2fb01f 4428 $street_address_filters = $general_address_filters = [];
6a488035 4429 if ($filters) {
21c21057
JV
4430 // Address filter depends on whether street address parsing is enabled.
4431 // (CRM-18696)
4432 $addressOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
4433 'address_options'
4434 );
4435 if ($addressOptions['street_address_parsing']) {
be2fb01f
CW
4436 $street_address_filters = [
4437 'street_number' => [
21c21057 4438 'title' => ts('Street Number'),
8b2dc0b1 4439 'type' => CRM_Utils_Type::T_INT,
21c21057 4440 'name' => 'street_number',
be2fb01f
CW
4441 ],
4442 'street_name' => [
21c21057
JV
4443 'title' => ts('Street Name'),
4444 'name' => 'street_name',
8b2dc0b1 4445 'type' => CRM_Utils_Type::T_STRING,
be2fb01f
CW
4446 ],
4447 ];
21c21057
JV
4448 }
4449 else {
be2fb01f
CW
4450 $street_address_filters = [
4451 'street_address' => [
21c21057 4452 'title' => ts('Street Address'),
8b2dc0b1 4453 'type' => CRM_Utils_Type::T_STRING,
21c21057 4454 'name' => 'street_address',
be2fb01f
CW
4455 ],
4456 ];
21c21057 4457 }
be2fb01f
CW
4458 $general_address_filters = [
4459 'postal_code' => [
9d72cede 4460 'title' => ts('Postal Code'),
8b2dc0b1 4461 'type' => CRM_Utils_Type::T_STRING,
9d72cede 4462 'name' => 'postal_code',
be2fb01f
CW
4463 ],
4464 'city' => [
9d72cede 4465 'title' => ts('City'),
8b2dc0b1 4466 'type' => CRM_Utils_Type::T_STRING,
9d72cede 4467 'name' => 'city',
be2fb01f
CW
4468 ],
4469 'country_id' => [
863581d1
CW
4470 'name' => 'country_id',
4471 'title' => ts('Country'),
6a488035 4472 'type' => CRM_Utils_Type::T_INT,
9d72cede 4473 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
c927c151 4474 'options' => CRM_Core_PseudoConstant::country(),
be2fb01f
CW
4475 ],
4476 'state_province_id' => [
6a488035
TO
4477 'name' => 'state_province_id',
4478 'title' => ts('State/Province'),
4479 'type' => CRM_Utils_Type::T_INT,
c927c151 4480 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
be2fb01f
CW
4481 'options' => [],
4482 ],
4483 'county_id' => [
863581d1
CW
4484 'name' => 'county_id',
4485 'title' => ts('County'),
6a488035 4486 'type' => CRM_Utils_Type::T_INT,
c927c151 4487 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
be2fb01f
CW
4488 'options' => [],
4489 ],
4490 ];
6a488035 4491 }
21c21057
JV
4492 $addressFields['civicrm_address']['filters'] = array_merge(
4493 $street_address_filters,
4494 $general_address_filters);
6a488035
TO
4495
4496 if ($orderBy) {
be2fb01f
CW
4497 $addressFields['civicrm_address']['order_bys'] = [
4498 'street_name' => ['title' => ts('Street Name')],
4499 'street_number' => ['title' => ts('Odd / Even Street Number')],
9d72cede
EM
4500 'street_address' => NULL,
4501 'city' => NULL,
4502 'postal_code' => NULL,
be2fb01f 4503 ];
6a488035
TO
4504 }
4505
4506 if ($groupBy) {
be2fb01f 4507 $addressFields['civicrm_address']['group_bys'] = [
6a488035
TO
4508 'street_address' => NULL,
4509 'city' => NULL,
4510 'postal_code' => NULL,
be2fb01f 4511 'state_province_id' => [
9d72cede 4512 'title' => ts('State/Province'),
be2fb01f
CW
4513 ],
4514 'country_id' => [
9d72cede 4515 'title' => ts('Country'),
be2fb01f
CW
4516 ],
4517 'county_id' => [
9d72cede 4518 'title' => ts('County'),
be2fb01f
CW
4519 ],
4520 ];
6a488035
TO
4521 }
4522 return $addressFields;
4523 }
4524
74cf4551 4525 /**
fe482240 4526 * Do AlterDisplay processing on Address Fields.
66ad82d6 4527 * If there are multiple address field values then
4528 * on basis of provided separator the code values are translated into respective labels
688d37c6 4529 *
317a8023 4530 * @param array $row
4531 * @param array $rows
4532 * @param int $rowNum
4533 * @param string $baseUrl
e4e2ff09 4534 * @param string $linkText
66ad82d6 4535 * @param string $separator
74cf4551
EM
4536 *
4537 * @return bool
4538 */
66ad82d6 4539 public function alterDisplayAddressFields(&$row, &$rows, &$rowNum, $baseUrl, $linkText, $separator = ',') {
6a488035
TO
4540 $criteriaQueryParams = CRM_Report_Utils_Report::getPreviewCriteriaQueryParams($this->_defaults, $this->_params);
4541 $entryFound = FALSE;
be2fb01f 4542 $columnMap = [
66ad82d6 4543 'civicrm_address_country_id' => 'country',
4544 'civicrm_address_county_id' => 'county',
4545 'civicrm_address_state_province_id' => 'stateProvince',
be2fb01f 4546 ];
66ad82d6 4547 foreach ($columnMap as $fieldName => $fnName) {
4548 if (array_key_exists($fieldName, $row)) {
4549 if ($values = $row[$fieldName]) {
4550 $values = (array) explode($separator, $values);
4551 $rows[$rowNum][$fieldName] = [];
4552 $addressField = $fnName == 'stateProvince' ? 'state' : $fnName;
4553 foreach ($values as $value) {
4554 $rows[$rowNum][$fieldName][] = CRM_Core_PseudoConstant::$fnName($value);
4555 }
4556 $rows[$rowNum][$fieldName] = implode($separator, $rows[$rowNum][$fieldName]);
4557 if ($baseUrl) {
4558 $url = CRM_Report_Utils_Report::getNextUrl($baseUrl,
4559 sprintf("reset=1&force=1&%s&%s_op=in&%s_value=%s",
4560 $criteriaQueryParams,
4561 str_replace('civicrm_address_', '', $fieldName),
4562 str_replace('civicrm_address_', '', $fieldName),
4563 implode(',', $values)
4564 ), $this->_absoluteUrl, $this->_id
4565 );
4566 $rows[$rowNum]["{$fieldName}_link"] = $url;
be2fb01f 4567 $rows[$rowNum]["{$fieldName}_hover"] = ts("%1 for this %2.", [1 => $linkText, 2 => $addressField]);
66ad82d6 4568 }
cec737d8 4569 }
089492f7 4570 $entryFound = TRUE;
6a488035 4571 }
6a488035
TO
4572 }
4573
4574 return $entryFound;
4575 }
4576
e5575773 4577 /**
4578 * Do AlterDisplay processing on Address Fields.
4579 *
4580 * @param array $row
4581 * @param array $rows
4582 * @param int $rowNum
e4e2ff09 4583 * @param string $baseUrl
4584 * @param string $linkText
e5575773 4585 *
4586 * @return bool
4587 */
e4e2ff09 4588 public function alterDisplayContactFields(&$row, &$rows, &$rowNum, $baseUrl, $linkText) {
e5575773 4589 $entryFound = FALSE;
d9bf3c53 4590 // There is no reason not to add links for all fields but it seems a bit odd to be able to click on
4591 // 'Mrs'. Also, we don't have metadata about the title. So, add selectively to addLinks.
be2fb01f
CW
4592 $addLinks = ['gender_id' => 'Gender'];
4593 foreach (['prefix_id', 'suffix_id', 'gender_id', 'contact_sub_type', 'preferred_language'] as $fieldName) {
50172d25 4594 if (array_key_exists('civicrm_contact_' . $fieldName, $row)) {
4595 if (($value = $row['civicrm_contact_' . $fieldName]) != FALSE) {
b706a634 4596 $rowValues = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
be2fb01f 4597 $rowLabels = [];
b706a634 4598 foreach ($rowValues as $rowValue) {
4599 if ($rowValue) {
074dd766 4600 $rowLabels[] = CRM_Core_PseudoConstant::getLabel('CRM_Contact_BAO_Contact', $fieldName, $rowValue);
b706a634 4601 }
4602 }
4603 $rows[$rowNum]['civicrm_contact_' . $fieldName] = implode(', ', $rowLabels);
cec737d8 4604 if ($baseUrl && ($title = CRM_Utils_Array::value($fieldName, $addLinks)) != FALSE) {
d9bf3c53 4605 $this->addLinkToRow($rows[$rowNum], $baseUrl, $linkText, $value, $fieldName, 'civicrm_contact', $title);
4606 }
50172d25 4607 }
4608 $entryFound = TRUE;
e5575773 4609 }
e5575773 4610 }
be2fb01f 4611 $yesNoFields = [
ea477981 4612 'do_not_email', 'is_deceased', 'do_not_phone', 'do_not_sms', 'do_not_mail', 'is_opt_out',
be2fb01f 4613 ];
ea477981 4614 foreach ($yesNoFields as $fieldName) {
4615 if (array_key_exists('civicrm_contact_' . $fieldName, $row)) {
4616 // Since these are essentially 'negative fields' it feels like it
4617 // makes sense to only highlight the exceptions hence no 'No'.
4618 $rows[$rowNum]['civicrm_contact_' . $fieldName] = !empty($rows[$rowNum]['civicrm_contact_' . $fieldName]) ? ts('Yes') : '';
4619 $entryFound = TRUE;
4620 }
4621 }
316bcb72 4622
4623 // Handle employer id
4624 if (array_key_exists('civicrm_contact_employer_id', $row)) {
4625 $employerId = $row['civicrm_contact_employer_id'];
4626 if ($employerId) {
4627 $rows[$rowNum]['civicrm_contact_employer_id'] = CRM_Contact_BAO_Contact::displayName($employerId);
4628 $rows[$rowNum]['civicrm_contact_employer_id_link'] = CRM_Utils_System::url('civicrm/contact/view', 'reset=1&cid=' . $employerId, $this->_absoluteUrl);
4629 $rows[$rowNum]['civicrm_contact_employer_id_hover'] = ts('View Contact Summary for Employer.');
4630 $entryFound = TRUE;
4631 }
4632 }
4633
e5575773 4634 return $entryFound;
4635 }
4636
74cf4551 4637 /**
688d37c6
CW
4638 * Adjusts dates passed in to YEAR() for fiscal year.
4639 *
100fef9d 4640 * @param string $fieldName
74cf4551
EM
4641 *
4642 * @return string
4643 */
00be9182 4644 public function fiscalYearOffset($fieldName) {
6a488035
TO
4645 $config = CRM_Core_Config::singleton();
4646 $fy = $config->fiscalYearStart;
9d72cede
EM
4647 if (CRM_Utils_Array::value('yid_op', $this->_params) == 'calendar' ||
4648 ($fy['d'] == 1 && $fy['M'] == 1)
4649 ) {
6a488035
TO
4650 return "YEAR( $fieldName )";
4651 }
9d72cede
EM
4652 return "YEAR( $fieldName - INTERVAL " . ($fy['M'] - 1) . " MONTH" .
4653 ($fy['d'] > 1 ? (" - INTERVAL " . ($fy['d'] - 1) . " DAY") : '') . " )";
6a488035
TO
4654 }
4655
688d37c6 4656 /**
fe482240 4657 * Add Address into From Table if required.
18f511e2 4658 *
4659 * @deprecated use joinAddressFromContact
4660 * (left here in case extensions use it).
6a488035 4661 */
00be9182 4662 public function addAddressFromClause() {
496320c3 4663 CRM_Core_Error::deprecatedFunctionWarning('CRM_Report_Form::joinAddressFromContact');
6a488035
TO
4664 // include address field if address column is to be included
4665 if ((isset($this->_addressField) &&
4666 $this->_addressField
4667 ) ||
4668 $this->isTableSelected('civicrm_address')
4669 ) {
4670 $this->_from .= "
4671 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
4672 ON ({$this->_aliases['civicrm_contact']}.id =
4673 {$this->_aliases['civicrm_address']}.contact_id) AND
4674 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
4675 }
4676 }
4677
850e4640 4678 /**
fe482240 4679 * Add Phone into From Table if required.
18f511e2 4680 *
4681 * @deprecated use joinPhoneFromContact
4682 * (left here in case extensions use it).
850e4640 4683 */
00be9182 4684 public function addPhoneFromClause() {
496320c3 4685 CRM_Core_Error::deprecatedFunctionWarning('CRM_Report_Form::joinPhoneFromContact');
850e4640 4686 // include address field if address column is to be included
d2a1da52 4687 if ($this->isTableSelected('civicrm_phone')) {
850e4640
E
4688 $this->_from .= "
4689 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
4690 ON ({$this->_aliases['civicrm_contact']}.id =
4691 {$this->_aliases['civicrm_phone']}.contact_id) AND
4692 {$this->_aliases['civicrm_phone']}.is_primary = 1\n";
4693 }
4694 }
4695
e3c74b61 4696 /**
4697 * Add Address into From Table if required.
4698 *
4699 * Prefix will be added to both tables as
4700 * it is assumed you are using it to get address of a secondary contact.
4701 *
4702 * @param string $prefix
4703 * @param array $extra Additional options.
4704 * Not currently used in core but may be used in override extensions.
4705 */
be2fb01f 4706 protected function joinAddressFromContact($prefix = '', $extra = []) {
ea39e229
SL
4707 $defaults = ['primary_only' => TRUE];
4708 $params = array_merge($defaults, $extra);
e3c74b61 4709 $addressTables = ['civicrm_address', 'civicrm_country', 'civicrm_worldregion', 'civicrm_state_province'];
4710 $isJoinRequired = $this->_addressField;
4711 foreach ($addressTables as $addressTable) {
4712 if ($this->isTableSelected($prefix . $addressTable)) {
4713 $isJoinRequired = TRUE;
4714 }
4715 }
4716 if ($isJoinRequired) {
ea39e229 4717 $fromJoin = "
e3c74b61 4718 LEFT JOIN civicrm_address {$this->_aliases[$prefix . 'civicrm_address']}
4719 ON ({$this->_aliases[$prefix . 'civicrm_contact']}.id =
ea39e229
SL
4720 {$this->_aliases[$prefix . 'civicrm_address']}.contact_id)";
4721 if ($params['primary_only']) {
4722 $fromJoin .= " AND
e3c74b61 4723 {$this->_aliases[$prefix . 'civicrm_address']}.is_primary = 1\n";
ea39e229
SL
4724 }
4725 $this->_from .= $fromJoin;
e3c74b61 4726 }
4727 }
4728
4729 /**
4730 * Add Country into From Table if required.
4731 *
4732 * Prefix will be added to both tables as
4733 * it is assumed you are using it to get address of a secondary contact.
4734 *
4735 * @param string $prefix
4736 * @param array $extra Additional options.
4737 * Not currently used in core but may be used in override extensions.
4738 */
be2fb01f 4739 protected function joinCountryFromAddress($prefix = '', $extra = []) {
ea39e229
SL
4740 $defaults = ['primary_only' => TRUE];
4741 $params = array_merge($defaults, $extra);
e3c74b61 4742 // include country field if country column is to be included
6d81d64a 4743 if ($this->isTableSelected($prefix . 'civicrm_country') || $this->isTableSelected($prefix . 'civicrm_worldregion')) {
4744 if (empty($this->_aliases[$prefix . 'civicrm_country'])) {
4745 $this->_aliases[$prefix . 'civicrm_country'] = $prefix . '_report_country';
4746 }
ea39e229 4747 $fromJoin = "
e3c74b61 4748 LEFT JOIN civicrm_country {$this->_aliases[$prefix . 'civicrm_country']}
ea39e229
SL
4749 ON {$this->_aliases[$prefix . 'civicrm_address']}.country_id = {$this->_aliases[$prefix . 'civicrm_country']}.id";
4750 if ($params['primary_only']) {
4751 $fromJoin .= " AND
e3c74b61 4752 {$this->_aliases[$prefix . 'civicrm_address']}.is_primary = 1 ";
ea39e229
SL
4753 }
4754 $this->_from .= $fromJoin;
e3c74b61 4755 }
4756 }
4757
4758 /**
4759 * Add Phone into From Table if required.
4760 *
4761 * Prefix will be added to both tables as
4762 * it is assumed you are using it to get address of a secondary contact.
4763 *
4764 * @param string $prefix
4765 * @param array $extra Additional options.
4766 * Not currently used in core but may be used in override extensions.
4767 */
be2fb01f 4768 protected function joinPhoneFromContact($prefix = '', $extra = []) {
ea39e229
SL
4769 $defaults = ['primary_only' => TRUE];
4770 $params = array_merge($defaults, $extra);
e3c74b61 4771 // include phone field if phone column is to be included
4772 if ($this->isTableSelected($prefix . 'civicrm_phone')) {
ea39e229 4773 $fromJoin = "
e3c74b61 4774 LEFT JOIN civicrm_phone {$this->_aliases[$prefix . 'civicrm_phone']}
ea39e229
SL
4775 ON {$this->_aliases[$prefix . 'civicrm_contact']}.id = {$this->_aliases[$prefix . 'civicrm_phone']}.contact_id";
4776 if ($params['primary_only']) {
4777 $fromJoin .= " AND
e3c74b61 4778 {$this->_aliases[$prefix . 'civicrm_phone']}.is_primary = 1\n";
ea39e229
SL
4779 }
4780 $this->_from .= $fromJoin;
e3c74b61 4781 }
4782 }
4783
4784 /**
4785 * Add Email into From Table if required.
4786 *
4787 * Prefix will be added to both tables as
4788 * it is assumed you are using it to get address of a secondary contact.
4789 *
4790 * @param string $prefix
4791 * @param array $extra Additional options.
4792 * Not currently used in core but may be used in override extensions.
4793 */
be2fb01f 4794 protected function joinEmailFromContact($prefix = '', $extra = []) {
ea39e229
SL
4795 $defaults = ['primary_only' => TRUE];
4796 $params = array_merge($defaults, $extra);
e3c74b61 4797 // include email field if email column is to be included
4798 if ($this->isTableSelected($prefix . 'civicrm_email')) {
ea39e229 4799 $fromJoin = "
e3c74b61 4800 LEFT JOIN civicrm_email {$this->_aliases[$prefix . 'civicrm_email']}
ea39e229
SL
4801 ON {$this->_aliases[$prefix . 'civicrm_contact']}.id = {$this->_aliases[$prefix . 'civicrm_email']}.contact_id";
4802 if ($params['primary_only']) {
4803 $fromJoin .= " AND
4804 {$this->_aliases[$prefix . 'civicrm_email']}.is_primary = 1 ";
4805 }
4806 $this->_from .= $fromJoin;
e3c74b61 4807 }
4808 }
4809
d2a1da52
ERL
4810 /**
4811 * Add Financial Transaction into From Table if required.
4812 */
4813 public function addFinancialTrxnFromClause() {
4814 if ($this->isTableSelected('civicrm_financial_trxn')) {
4815 $this->_from .= "
4816 LEFT JOIN civicrm_entity_financial_trxn eftcc
4817 ON ({$this->_aliases['civicrm_contribution']}.id = eftcc.entity_id AND
4818 eftcc.entity_table = 'civicrm_contribution')
4819 LEFT JOIN civicrm_financial_trxn {$this->_aliases['civicrm_financial_trxn']}
4820 ON {$this->_aliases['civicrm_financial_trxn']}.id = eftcc.financial_trxn_id \n";
4821 }
4822 }
4823
850e4640 4824 /**
fe482240 4825 * Get phone columns to add to array.
9d72cede 4826 *
850e4640 4827 * @param array $options
16b10e64
CW
4828 * - prefix Prefix to add to table (in case of more than one instance of the table)
4829 * - prefix_label Label to give columns from this phone table instance
9d72cede 4830 *
a6c01b45
CW
4831 * @return array
4832 * phone columns definition
850e4640 4833 */
be2fb01f
CW
4834 public function getPhoneColumns($options = []) {
4835 $defaultOptions = [
850e4640
E
4836 'prefix' => '',
4837 'prefix_label' => '',
be2fb01f 4838 ];
850e4640 4839
9d72cede 4840 $options = array_merge($defaultOptions, $options);
850e4640 4841
be2fb01f
CW
4842 $fields = [
4843 $options['prefix'] . 'civicrm_phone' => [
9d72cede 4844 'dao' => 'CRM_Core_DAO_Phone',
be2fb01f
CW
4845 'fields' => [
4846 $options['prefix'] . 'phone' => [
c576cb65 4847 'title' => $options['prefix_label'] . ts('Phone'),
21dfd5f5 4848 'name' => 'phone',
be2fb01f
CW
4849 ],
4850 ],
4851 ],
4852 ];
850e4640
E
4853 return $fields;
4854 }
4855
e5575773 4856 /**
4857 * Get a standard set of contact fields.
7777f433 4858 * @deprecated - use getColumns('Contact') instead
e5575773 4859 * @return array
4860 */
4861 public function getBasicContactFields() {
be2fb01f
CW
4862 return [
4863 'sort_name' => [
e5575773 4864 'title' => ts('Contact Name'),
4865 'required' => TRUE,
4866 'default' => TRUE,
be2fb01f
CW
4867 ],
4868 'id' => [
e5575773 4869 'no_display' => TRUE,
4870 'required' => TRUE,
be2fb01f
CW
4871 ],
4872 'prefix_id' => [
e5575773 4873 'title' => ts('Contact Prefix'),
be2fb01f
CW
4874 ],
4875 'first_name' => [
e5575773 4876 'title' => ts('First Name'),
be2fb01f
CW
4877 ],
4878 'nick_name' => [
4120c775 4879 'title' => ts('Nick Name'),
be2fb01f
CW
4880 ],
4881 'middle_name' => [
e5575773 4882 'title' => ts('Middle Name'),
be2fb01f
CW
4883 ],
4884 'last_name' => [
e5575773 4885 'title' => ts('Last Name'),
be2fb01f
CW
4886 ],
4887 'suffix_id' => [
e5575773 4888 'title' => ts('Contact Suffix'),
be2fb01f
CW
4889 ],
4890 'postal_greeting_display' => ['title' => ts('Postal Greeting')],
4891 'email_greeting_display' => ['title' => ts('Email Greeting')],
4892 'addressee_display' => ['title' => ts('Addressee')],
4893 'contact_type' => [
e5575773 4894 'title' => ts('Contact Type'),
be2fb01f
CW
4895 ],
4896 'contact_sub_type' => [
e5575773 4897 'title' => ts('Contact Subtype'),
be2fb01f
CW
4898 ],
4899 'gender_id' => [
e5575773 4900 'title' => ts('Gender'),
be2fb01f
CW
4901 ],
4902 'birth_date' => [
e5575773 4903 'title' => ts('Birth Date'),
be2fb01f
CW
4904 ],
4905 'age' => [
e5575773 4906 'title' => ts('Age'),
4907 'dbAlias' => 'TIMESTAMPDIFF(YEAR, contact_civireport.birth_date, CURDATE())',
be2fb01f
CW
4908 ],
4909 'job_title' => [
e5575773 4910 'title' => ts('Contact Job title'),
be2fb01f
CW
4911 ],
4912 'organization_name' => [
e5575773 4913 'title' => ts('Organization Name'),
be2fb01f
CW
4914 ],
4915 'external_identifier' => [
b4d1eebe 4916 'title' => ts('Contact identifier from external system'),
be2fb01f
CW
4917 ],
4918 'do_not_email' => [],
4919 'do_not_phone' => [],
4920 'do_not_mail' => [],
4921 'do_not_sms' => [],
4922 'is_opt_out' => [],
4923 'is_deceased' => [],
4924 'preferred_language' => [],
4925 'employer_id' => [
316bcb72 4926 'title' => ts('Current Employer'),
be2fb01f
CW
4927 ],
4928 ];
e5575773 4929 }
4930
aceb083a 4931 /**
4932 * Get a standard set of contact filters.
4933 *
1ebe1a57
FBB
4934 * @param array $defaults
4935 *
aceb083a 4936 * @return array
4937 */
be2fb01f
CW
4938 public function getBasicContactFilters($defaults = []) {
4939 return [
4940 'sort_name' => [
aceb083a 4941 'title' => ts('Contact Name'),
be2fb01f
CW
4942 ],
4943 'source' => [
aceb083a 4944 'title' => ts('Contact Source'),
4945 'type' => CRM_Utils_Type::T_STRING,
be2fb01f
CW
4946 ],
4947 'id' => [
aceb083a 4948 'title' => ts('Contact ID'),
4949 'no_display' => TRUE,
be2fb01f
CW
4950 ],
4951 'gender_id' => [
aceb083a 4952 'title' => ts('Gender'),
4953 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
4954 'options' => CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'),
be2fb01f
CW
4955 ],
4956 'birth_date' => [
aceb083a 4957 'title' => ts('Birth Date'),
4958 'operatorType' => CRM_Report_Form::OP_DATE,
be2fb01f
CW
4959 ],
4960 'contact_type' => [
aceb083a 4961 'title' => ts('Contact Type'),
be2fb01f
CW
4962 ],
4963 'contact_sub_type' => [
aceb083a 4964 'title' => ts('Contact Subtype'),
be2fb01f
CW
4965 ],
4966 'modified_date' => [
aceb083a 4967 'title' => ts('Contact Modified'),
4968 'operatorType' => CRM_Report_Form::OP_DATE,
4969 'type' => CRM_Utils_Type::T_DATE,
be2fb01f
CW
4970 ],
4971 'is_deceased' => [
aceb083a 4972 'title' => ts('Deceased'),
4973 'type' => CRM_Utils_Type::T_BOOLEAN,
1ebe1a57 4974 'default' => CRM_Utils_Array::value('deceased', $defaults, 0),
be2fb01f
CW
4975 ],
4976 'do_not_email' => [
b3f2f5e9 4977 'title' => ts('Do not email'),
4978 'type' => CRM_Utils_Type::T_BOOLEAN,
be2fb01f
CW
4979 ],
4980 'do_not_phone' => [
b3f2f5e9 4981 'title' => ts('Do not phone'),
4982 'type' => CRM_Utils_Type::T_BOOLEAN,
be2fb01f
CW
4983 ],
4984 'do_not_mail' => [
b3f2f5e9 4985 'title' => ts('Do not mail'),
4986 'type' => CRM_Utils_Type::T_BOOLEAN,
be2fb01f
CW
4987 ],
4988 'do_not_sms' => [
b3f2f5e9 4989 'title' => ts('Do not SMS'),
4990 'type' => CRM_Utils_Type::T_BOOLEAN,
be2fb01f
CW
4991 ],
4992 'is_opt_out' => [
b3f2f5e9 4993 'title' => ts('Do not bulk email'),
4994 'type' => CRM_Utils_Type::T_BOOLEAN,
be2fb01f
CW
4995 ],
4996 'preferred_language' => [
b706a634 4997 'title' => ts('Preferred Language'),
be2fb01f
CW
4998 ],
4999 'is_deleted' => [
da7ac680
CW
5000 'no_display' => TRUE,
5001 'default' => 0,
5002 'type' => CRM_Utils_Type::T_BOOLEAN,
be2fb01f
CW
5003 ],
5004 ];
aceb083a 5005 }
5006
74cf4551 5007 /**
317a8023 5008 * Add contact to group.
5009 *
100fef9d 5010 * @param int $groupID
74cf4551 5011 */
00be9182 5012 public function add2group($groupID) {
6a488035
TO
5013 if (is_numeric($groupID) && isset($this->_aliases['civicrm_contact'])) {
5014 $select = "SELECT DISTINCT {$this->_aliases['civicrm_contact']}.id AS addtogroup_contact_id, ";
f0b5b73e 5015 $select = preg_replace('/SELECT(\s+SQL_CALC_FOUND_ROWS)?\s+/i', $select, $this->_select);
6a488035 5016 $sql = "{$select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy}";
e05e0dba 5017 $sql = str_replace('WITH ROLLUP', '', $sql);
6a488035
TO
5018 $dao = CRM_Core_DAO::executeQuery($sql);
5019
be2fb01f 5020 $contact_ids = [];
6a488035
TO
5021 // Add resulting contacts to group
5022 while ($dao->fetch()) {
5023 if ($dao->addtogroup_contact_id) {
5024 $contact_ids[$dao->addtogroup_contact_id] = $dao->addtogroup_contact_id;
5025 }
5026 }
5027
9d72cede 5028 if (!empty($contact_ids)) {
6a488035
TO
5029 CRM_Contact_BAO_GroupContact::addContactsToGroup($contact_ids, $groupID);
5030 CRM_Core_Session::setStatus(ts("Listed contact(s) have been added to the selected group."), ts('Contacts Added'), 'success');
5031 }
5032 else {
5033 CRM_Core_Session::setStatus(ts("The listed records(s) cannot be added to the group."));
5034 }
5035 }
5036 }
46065582 5037
2107cde9 5038 /**
fe482240 5039 * Apply common settings to entityRef fields.
9d72cede 5040 *
2107cde9
CW
5041 * @param array $field
5042 * @param string $table
5043 */
b2c5abe3 5044 public function setEntityRefDefaults(&$field, $table) {
be2fb01f
CW
5045 $field['attributes'] = $field['attributes'] ? $field['attributes'] : [];
5046 $field['attributes'] += [
2107cde9
CW
5047 'entity' => CRM_Core_DAO_AllCoreTables::getBriefName(CRM_Core_DAO_AllCoreTables::getClassForTable($table)),
5048 'multiple' => TRUE,
5049 'placeholder' => ts('- select -'),
be2fb01f 5050 ];
2107cde9 5051 }
96025800 5052
e4e2ff09 5053 /**
5054 * Add link fields to the row.
5055 *
5056 * Function adds the _link & _hover fields to the row.
5057 *
5058 * @param array $row
5059 * @param string $baseUrl
5060 * @param string $linkText
5061 * @param string $value
5062 * @param string $fieldName
5063 * @param string $tablePrefix
5064 * @param string $fieldLabel
5065 *
5066 * @return mixed
5067 */
5068 protected function addLinkToRow(&$row, $baseUrl, $linkText, $value, $fieldName, $tablePrefix, $fieldLabel) {
5069 $criteriaQueryParams = CRM_Report_Utils_Report::getPreviewCriteriaQueryParams($this->_defaults, $this->_params);
5070 $url = CRM_Report_Utils_Report::getNextUrl($baseUrl,
5071 "reset=1&force=1&{$criteriaQueryParams}&" .
5072 $fieldName . "_op=in&{$fieldName}_value={$value}",
5073 $this->_absoluteUrl, $this->_id
5074 );
5075 $row["{$tablePrefix}_{$fieldName}_link"] = $url;
5076 $row["{$tablePrefix}_{$fieldName}_hover"] = ts("%1 for this %2.",
be2fb01f 5077 [1 => $linkText, 2 => $fieldLabel]
e4e2ff09 5078 );
5079 }
5080
182f5081 5081 /**
5082 * Get label for show results buttons.
5083 *
5084 * @return string
5085 */
5086 public function getResultsLabel() {
5087 $showResultsLabel = $this->resultsDisplayed() ? ts('Refresh results') : ts('View results');
5088 return $showResultsLabel;
5089 }
5090
5091 /**
5092 * Determine the output mode from the url or input.
5093 *
5094 * Output could be
5095 * - pdf : Render as pdf
5096 * - csv : Render as csv
5097 * - print : Render in print format
5098 * - save : save the report and display the new report
5099 * - copy : save the report as a new instance and display that.
5100 * - group : go to the add to group screen.
5101 *
5102 * Potentially chart variations could also be included but the complexity
5103 * is that we might print a bar chart as a pdf.
5104 */
5105 protected function setOutputMode() {
1a7356e7 5106 $this->_outputMode = str_replace('report_instance.', '', CRM_Utils_Request::retrieve(
182f5081 5107 'output',
5108 'String',
5109 CRM_Core_DAO::$_nullObject,
5110 FALSE,
5111 CRM_Utils_Array::value('task', $this->_params)
44543184 5112 ));
7343d8a7 5113 // if contacts are added to group
5114 if (!empty($this->_params['groups']) && empty($this->_outputMode)) {
5115 $this->_outputMode = 'group';
5116 }
88fefc2f 5117 if (isset($this->_params['task'])) {
5118 unset($this->_params['task']);
5119 }
182f5081 5120 }
5121
0fefbf7f 5122 /**
ecf1e543 5123 * CRM-17793 - Alter DateTime section header to group by date from the datetime field.
3b6d61f2 5124 *
ecf1e543 5125 * @param $tempTable
5126 * @param $columnName
5127 */
5128 public function alterSectionHeaderForDateTime($tempTable, $columnName) {
0fefbf7f 5129 // add new column with date value for the datetime field
ecf1e543 5130 $tempQuery = "ALTER TABLE {$tempTable} ADD COLUMN {$columnName}_date VARCHAR(128)";
5131 CRM_Core_DAO::executeQuery($tempQuery);
5132 $updateQuery = "UPDATE {$tempTable} SET {$columnName}_date = date({$columnName})";
5133 CRM_Core_DAO::executeQuery($updateQuery);
b708c08d 5134 $this->_selectClauses[] = "{$columnName}_date";
ecf1e543 5135 $this->_select .= ", {$columnName}_date";
5136 $this->_sections["{$columnName}_date"] = $this->_sections["{$columnName}"];
5137 unset($this->_sections["{$columnName}"]);
5138 $this->assign('sections', $this->_sections);
5139 }
5140
55f71fa7 5141 /**
5142 * Get an array of the columns that have been selected for display.
5143 *
5144 * @return array
5145 */
5146 public function getSelectColumns() {
be2fb01f 5147 $selectColumns = [];
55f71fa7 5148 foreach ($this->_columns as $tableName => $table) {
5149 if (array_key_exists('fields', $table)) {
5150 foreach ($table['fields'] as $fieldName => $field) {
5151 if (!empty($field['required']) ||
5152 !empty($this->_params['fields'][$fieldName])
5153 ) {
5154
5155 $selectColumns["{$tableName}_{$fieldName}"] = 1;
5156 }
5157 }
5158 }
5159 }
5160 return $selectColumns;
5161 }
5162
c160fde8 5163 /**
5164 * Add location tables to the query if they are used for filtering.
5165 *
5166 * This is for when we are running the query separately for filtering and retrieving display fields.
5167 */
5168 public function selectivelyAddLocationTablesJoinsToFilterQuery() {
5169 if ($this->isTableFiltered('civicrm_email')) {
5170 $this->_from .= "
5171 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
5172 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id
5173 AND {$this->_aliases['civicrm_email']}.is_primary = 1";
5174 }
5175 if ($this->isTableFiltered('civicrm_phone')) {
5176 $this->_from .= "
5177 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
5178 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id
5179 AND {$this->_aliases['civicrm_phone']}.is_primary = 1";
5180 }
5181 if ($this->isTableFiltered('civicrm_address')) {
5182 $this->_from .= "
5183 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
5184 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id)
5185 AND {$this->_aliases['civicrm_address']}.is_primary = 1\n";
5186 }
5187 }
5188
43c1fa19 5189 /**
5190 * Set the base table for the FROM clause.
5191 *
5192 * Sets up the from clause, allowing for the possibility it might be a
5193 * temp table pre-filtered by groups if a group filter is in use.
5194 *
5195 * @param string $baseTable
5196 * @param string $field
5197 * @param null $tableAlias
5198 */
5199 public function setFromBase($baseTable, $field = 'id', $tableAlias = NULL) {
5200 if (!$tableAlias) {
5201 $tableAlias = $this->_aliases[$baseTable];
5202 }
5203 $this->_from = $this->_from = " FROM $baseTable $tableAlias ";
5204 $this->joinGroupTempTable($baseTable, $field, $tableAlias);
5205 $this->_from .= " {$this->_aclFrom} ";
5206 }
5207
5208 /**
5209 * Join the temp table contacting contacts who are members of the filtered groups.
5210 *
5211 * If we are using an IN filter we use an inner join, otherwise a left join.
5212 *
5213 * @param string $baseTable
5214 * @param string $field
5215 * @param string $tableAlias
5216 */
5217 public function joinGroupTempTable($baseTable, $field, $tableAlias) {
5218 if ($this->groupTempTable) {
5219 if ($this->_params['gid_op'] == 'in') {
5220 $this->_from = " FROM $this->groupTempTable group_temp_table INNER JOIN $baseTable $tableAlias
5221 ON group_temp_table.id = $tableAlias.{$field} ";
5222 }
5223 else {
5224 $this->_from .= "
5225 LEFT JOIN $this->groupTempTable group_temp_table
5226 ON $tableAlias.{$field} = group_temp_table.id ";
5227 }
5228 }
5229 }
5230
fa5fb88c 5231 /**
a51858a1 5232 * Get all labels for fields that are used in a group concat.
fa5fb88c 5233 *
a51858a1
E
5234 * @param string $options
5235 * comma separated option values.
5236 * @param string $baoName
5237 * The BAO name for the field.
5238 * @param string $fieldName
5239 * The name of the field for which labels should be retrieved.
5240 *
5241 * return string
fa5fb88c 5242 */
a51858a1
E
5243 public function getLabels($options, $baoName, $fieldName) {
5244 $types = explode(',', $options);
be2fb01f 5245 $labels = [];
fa5fb88c 5246 foreach ($types as $value) {
a51858a1 5247 $labels[$value] = CRM_Core_PseudoConstant::getLabel($baoName, $fieldName, $value);
fa5fb88c
E
5248 }
5249 return implode(', ', array_filter($labels));
5250 }
5251
074dd766 5252 /**
0a01dff9 5253 * Add statistics columns.
5254 *
5255 * If a group by is in play then add columns for the statistics fields.
5256 *
5257 * This would lead to a new field in the $row such as $fieldName_sum and a new, matching
5258 * column header field.
5259 *
5260 * @param array $field
5261 * @param string $tableName
5262 * @param string $fieldName
5263 * @param array $select
5264 *
5265 * @return array
5266 */
5267 protected function addStatisticsToSelect($field, $tableName, $fieldName, $select) {
5268 foreach ($field['statistics'] as $stat => $label) {
5269 $alias = "{$tableName}_{$fieldName}_{$stat}";
5270 switch (strtolower($stat)) {
5271 case 'max':
5272 case 'sum':
5273 $select[] = "$stat({$field['dbAlias']}) as $alias";
5274 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
5275 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
5276 $this->_statFields[$label] = $alias;
5277 $this->_selectAliases[] = $alias;
5278 break;
5279
5280 case 'count':
5281 $select[] = "COUNT({$field['dbAlias']}) as $alias";
5282 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
5283 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type::T_INT;
5284 $this->_statFields[$label] = $alias;
5285 $this->_selectAliases[] = $alias;
5286 break;
5287
5288 case 'count_distinct':
5289 $select[] = "COUNT(DISTINCT {$field['dbAlias']}) as $alias";
5290 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
5291 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type::T_INT;
5292 $this->_statFields[$label] = $alias;
5293 $this->_selectAliases[] = $alias;
5294 break;
5295
5296 case 'avg':
5297 $select[] = "ROUND(AVG({$field['dbAlias']}),2) as $alias";
5298 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
5299 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
5300 $this->_statFields[$label] = $alias;
5301 $this->_selectAliases[] = $alias;
5302 break;
5303 }
5304 }
5305 return $select;
5306 }
5307
5308 /**
5309 * Add a basic field to the select clause.
5310 *
5311 * @param string $tableName
5312 * @param string $fieldName
5313 * @param array $field
5314 * @param string $select
5315 * @return array
5316 */
5317 protected function addBasicFieldToSelect($tableName, $fieldName, $field, $select) {
5318 $alias = "{$tableName}_{$fieldName}";
5319 $select[] = "{$field['dbAlias']} as $alias";
5320 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
5321 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
5322 $this->_selectAliases[] = $alias;
5323 return $select;
5324 }
5325
8bb36676 5326 /**
5327 * Set table alias.
5328 *
5329 * @param array $table
5330 * @param string $tableName
5331 *
5332 * @return string
5333 * Alias for table.
5334 */
5335 protected function setTableAlias($table, $tableName) {
5336 if (!isset($table['alias'])) {
5337 $this->_columns[$tableName]['alias'] = substr($tableName, 8) .
5338 '_civireport';
5339 }
5340 else {
5341 $this->_columns[$tableName]['alias'] = $table['alias'] . '_civireport';
5342 }
5343
5344 $this->_aliases[$tableName] = $this->_columns[$tableName]['alias'];
5345 return $this->_aliases[$tableName];
5346 }
5347
d7e34fc6 5348 /**
5349 * Function to add columns to reports.
5350 *
5351 * This is ported from extended reports, which also adds join filters to the options.
5352 *
5353 * @param string $type
5354 * @param array $options
5355 * - prefix - A string to prepend to the table name
5356 * - prefix_label A string to prepend to the fields
5357 * - fields (bool) - should the fields for this table be made available
5358 * - group_by (bool) - should the group bys for this table be made available.
5359 * - order_by (bool) - should the group bys for this table be made available.
5360 * - filters (bool) - should the filters for this table by made available.
5361 * - fields_defaults (array) array of fields that should be displayed by default.
5362 * - filters_defaults (array) array of fields that should be filtered by default.
5363 * - join_filters (array) fields available for filtering joins (requires additional custom code).
5364 * - join_fields (array) fields available from join (requires additional custom code).
5365 * - group_by_defaults (array) array of group bys that should be applied by default.
5366 * - order_by_defaults (array) array of order bys that should be applied by default.
5367 * - custom_fields (array) array of entity types for custom fields (not usually required).
5368 * - contact_type (string) optional restriction on contact type for some tables.
5369 * - fields_excluded (array) fields that are in the generic set for the table but not in the report.
5370 *
5371 * @return array
5372 */
be2fb01f
CW
5373 protected function getColumns($type, $options = []) {
5374 $defaultOptions = [
d7e34fc6 5375 'prefix' => '',
5376 'prefix_label' => '',
5377 'fields' => TRUE,
5378 'group_bys' => FALSE,
5379 'order_bys' => TRUE,
5380 'filters' => TRUE,
5381 'join_filters' => FALSE,
be2fb01f
CW
5382 'fields_defaults' => [],
5383 'filters_defaults' => [],
5384 'group_bys_defaults' => [],
5385 'order_bys_defaults' => [],
5386 ];
d7e34fc6 5387 $options = array_merge($defaultOptions, $options);
5388
5389 $fn = 'get' . $type . 'Columns';
5390 return $this->$fn($options);
5391 }
5392
5393 /**
5394 * Get columns for contact table.
5395 *
5396 * @param array $options
5397 *
5398 * @return array
5399 */
be2fb01f
CW
5400 protected function getContactColumns($options = []) {
5401 $defaultOptions = [
5402 'custom_fields' => ['Individual', 'Contact', 'Organization'],
5403 'fields_defaults' => ['display_name', 'id'],
5404 'order_bys_defaults' => ['sort_name ASC'],
d7e34fc6 5405 'contact_type' => NULL,
be2fb01f 5406 ];
d7e34fc6 5407
5408 $options = array_merge($defaultOptions, $options);
5409
5410 $tableAlias = $options['prefix'] . 'contact';
5411
be2fb01f
CW
5412 $spec = [
5413 $options['prefix'] . 'display_name' => [
d7e34fc6 5414 'name' => 'display_name',
c576cb65 5415 'title' => $options['prefix_label'] . ts('Contact Name'),
d7e34fc6 5416 'is_fields' => TRUE,
be2fb01f
CW
5417 ],
5418 $options['prefix'] . 'sort_name' => [
d7e34fc6 5419 'name' => 'sort_name',
c576cb65 5420 'title' => $options['prefix_label'] . ts('Contact Name (in sort format)'),
d7e34fc6 5421 'is_fields' => TRUE,
5422 'is_filters' => TRUE,
5423 'is_order_bys' => TRUE,
be2fb01f
CW
5424 ],
5425 $options['prefix'] . 'id' => [
d7e34fc6 5426 'name' => 'id',
c576cb65 5427 'title' => $options['prefix_label'] . ts('Contact ID'),
d7e34fc6 5428 'alter_display' => 'alterContactID',
5429 'type' => CRM_Utils_Type::T_INT,
5430 'is_order_bys' => TRUE,
5431 'is_group_bys' => TRUE,
5432 'is_fields' => TRUE,
bad366ab 5433 'is_filters' => TRUE,
be2fb01f
CW
5434 ],
5435 $options['prefix'] . 'external_identifier' => [
d7e34fc6 5436 'name' => 'external_identifier',
c576cb65 5437 'title' => $options['prefix_label'] . ts('External ID'),
d7e34fc6 5438 'type' => CRM_Utils_Type::T_INT,
5439 'is_fields' => TRUE,
be2fb01f
CW
5440 ],
5441 $options['prefix'] . 'contact_type' => [
c576cb65 5442 'title' => $options['prefix_label'] . ts('Contact Type'),
d7e34fc6 5443 'name' => 'contact_type',
5444 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
5445 'options' => CRM_Contact_BAO_Contact::buildOptions('contact_type'),
5446 'is_fields' => TRUE,
5447 'is_filters' => TRUE,
5448 'is_group_bys' => TRUE,
be2fb01f
CW
5449 ],
5450 $options['prefix'] . 'contact_sub_type' => [
c576cb65 5451 'title' => $options['prefix_label'] . ts('Contact Sub Type'),
d7e34fc6 5452 'name' => 'contact_sub_type',
5453 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
5454 'options' => CRM_Contact_BAO_Contact::buildOptions('contact_sub_type'),
5455 'is_fields' => TRUE,
5456 'is_filters' => TRUE,
5457 'is_group_bys' => TRUE,
be2fb01f
CW
5458 ],
5459 $options['prefix'] . 'is_deleted' => [
c576cb65 5460 'title' => $options['prefix_label'] . ts('Is deleted'),
d7e34fc6 5461 'name' => 'is_deleted',
5462 'type' => CRM_Utils_Type::T_BOOLEAN,
5463 'is_fields' => FALSE,
5464 'is_filters' => TRUE,
5465 'is_group_bys' => FALSE,
be2fb01f
CW
5466 ],
5467 $options['prefix'] . 'external_identifier' => [
7777f433
JG
5468 'title' => $options['prefix_label'] . ts('Contact identifier from external system'),
5469 'name' => 'external_identifier',
5470 'is_fields' => TRUE,
5471 'is_filters' => FALSE,
5472 'is_group_bys' => FALSE,
5473 'is_order_bys' => TRUE,
be2fb01f
CW
5474 ],
5475 $options['prefix'] . 'preferred_language' => [
7777f433
JG
5476 'title' => $options['prefix_label'] . ts('Preferred Language'),
5477 'name' => 'preferred_language',
5478 'is_fields' => TRUE,
5479 'is_filters' => TRUE,
5480 'is_group_bys' => TRUE,
5481 'is_order_bys' => TRUE,
be2fb01f 5482 ],
1754f22f
AS
5483 $options['prefix'] . 'preferred_communication_method' => [
5484 'title' => $options['prefix_label'] . ts('Preferred Communication Method'),
5485 'alter_display' => 'alterCommunicationtMethod',
5486 'name' => 'preferred_communication_method',
5487 'is_fields' => TRUE,
5488 'is_filters' => FALSE,
5489 'is_group_bys' => FALSE,
5490 'is_order_bys' => FALSE,
5491 ],
be2fb01f 5492 ];
7777f433
JG
5493 foreach ([
5494 'postal_greeting_display' => 'Postal Greeting',
5495 'email_greeting_display' => 'Email Greeting',
5496 'addressee_display' => 'Addressee',
5497 ] as $field => $title) {
be2fb01f 5498 $spec[$options['prefix'] . $field] = [
7777f433
JG
5499 'title' => $options['prefix_label'] . ts($title),
5500 'name' => $field,
5501 'is_fields' => TRUE,
5502 'is_filters' => FALSE,
5503 'is_group_bys' => FALSE,
be2fb01f 5504 ];
7777f433
JG
5505 }
5506 foreach (['do_not_email', 'do_not_phone', 'do_not_mail', 'do_not_sms', 'is_opt_out'] as $field) {
5507 $spec[$options['prefix'] . $field] = [
5508 'name' => $field,
5509 'type' => CRM_Utils_Type::T_BOOLEAN,
5510 'is_fields' => TRUE,
5511 'is_filters' => TRUE,
5512 'is_group_bys' => FALSE,
5513 ];
5514 }
be2fb01f
CW
5515 $individualFields = [
5516 $options['prefix'] . 'first_name' => [
d7e34fc6 5517 'name' => 'first_name',
c576cb65 5518 'title' => $options['prefix_label'] . ts('First Name'),
d7e34fc6 5519 'is_fields' => TRUE,
5520 'is_filters' => TRUE,
5521 'is_order_bys' => TRUE,
be2fb01f
CW
5522 ],
5523 $options['prefix'] . 'middle_name' => [
d7e34fc6 5524 'name' => 'middle_name',
c576cb65 5525 'title' => $options['prefix_label'] . ts('Middle Name'),
d7e34fc6 5526 'is_fields' => TRUE,
be2fb01f
CW
5527 ],
5528 $options['prefix'] . 'last_name' => [
d7e34fc6 5529 'name' => 'last_name',
c576cb65 5530 'title' => $options['prefix_label'] . ts('Last Name'),
d7e34fc6 5531 'default_order' => 'ASC',
5532 'is_fields' => TRUE,
be2fb01f
CW
5533 ],
5534 $options['prefix'] . 'nick_name' => [
d7e34fc6 5535 'name' => 'nick_name',
c576cb65 5536 'title' => $options['prefix_label'] . ts('Nick Name'),
d7e34fc6 5537 'is_fields' => TRUE,
be2fb01f
CW
5538 ],
5539 $options['prefix'] . 'prefix_id' => [
c5185dd8 5540 'name' => 'prefix_id',
5541 'title' => $options['prefix_label'] . ts('Prefix'),
5542 'options' => CRM_Contact_BAO_Contact::buildOptions('prefix_id'),
5543 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
5544 'is_fields' => TRUE,
5545 'is_filters' => TRUE,
be2fb01f
CW
5546 ],
5547 $options['prefix'] . 'suffix_id' => [
c5185dd8 5548 'name' => 'suffix_id',
5549 'title' => $options['prefix_label'] . ts('Suffix'),
5550 'options' => CRM_Contact_BAO_Contact::buildOptions('suffix_id'),
5551 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
5552 'is_fields' => TRUE,
5553 'is_filters' => TRUE,
be2fb01f
CW
5554 ],
5555 $options['prefix'] . 'gender_id' => [
d7e34fc6 5556 'name' => 'gender_id',
c576cb65 5557 'title' => $options['prefix_label'] . ts('Gender'),
d7e34fc6 5558 'options' => CRM_Contact_BAO_Contact::buildOptions('gender_id'),
5559 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
d7e34fc6 5560 'is_fields' => TRUE,
5561 'is_filters' => TRUE,
be2fb01f
CW
5562 ],
5563 'birth_date' => [
c576cb65 5564 'title' => $options['prefix_label'] . ts('Birth Date'),
d7e34fc6 5565 'operatorType' => CRM_Report_Form::OP_DATE,
5566 'type' => CRM_Utils_Type::T_DATE,
5567 'is_fields' => TRUE,
5568 'is_filters' => TRUE,
be2fb01f
CW
5569 ],
5570 'age' => [
c576cb65 5571 'title' => $options['prefix_label'] . ts('Age'),
75344f5f 5572 'dbAlias' => 'TIMESTAMPDIFF(YEAR, ' . $tableAlias . '_civireport.birth_date, CURDATE())',
d7e34fc6 5573 'type' => CRM_Utils_Type::T_INT,
5574 'is_fields' => TRUE,
be2fb01f
CW
5575 ],
5576 $options['prefix'] . 'is_deceased' => [
c576cb65 5577 'title' => $options['prefix_label'] . ts('Is deceased'),
d7e34fc6 5578 'name' => 'is_deceased',
5579 'type' => CRM_Utils_Type::T_BOOLEAN,
5580 'is_fields' => FALSE,
5581 'is_filters' => TRUE,
5582 'is_group_bys' => FALSE,
be2fb01f
CW
5583 ],
5584 $options['prefix'] . 'job_title' => [
7777f433
JG
5585 'name' => 'job_title',
5586 'is_fields' => TRUE,
5587 'is_filters' => FALSE,
5588 'is_group_bys' => FALSE,
be2fb01f
CW
5589 ],
5590 $options['prefix'] . 'employer_id' => [
7777f433
JG
5591 'title' => $options['prefix_label'] . ts('Current Employer'),
5592 'type' => CRM_Utils_Type::T_INT,
5593 'name' => 'employer_id',
5594 'is_fields' => TRUE,
5595 'is_filters' => FALSE,
5596 'is_group_bys' => TRUE,
be2fb01f
CW
5597 ],
5598 ];
d7e34fc6 5599 if (!$options['contact_type'] || $options['contact_type'] === 'Individual') {
5600 $spec = array_merge($spec, $individualFields);
5601 }
5602
5603 if (!empty($options['custom_fields'])) {
be2fb01f 5604 $this->_customGroupExtended[$options['prefix'] . 'civicrm_contact'] = [
d7e34fc6 5605 'extends' => $options['custom_fields'],
5606 'title' => $options['prefix_label'],
5607 'filters' => $options['filters'],
5608 'prefix' => $options['prefix'],
5609 'prefix_label' => $options['prefix_label'],
be2fb01f 5610 ];
d7e34fc6 5611 }
5612
5613 return $this->buildColumns($spec, $options['prefix'] . 'civicrm_contact', 'CRM_Contact_DAO_Contact', $tableAlias, $this->getDefaultsFromOptions($options), $options);
5614 }
5615
59b19369
JJ
5616 /**
5617 * Get address columns to add to array.
5618 *
5619 * @param array $options
5620 * - prefix Prefix to add to table (in case of more than one instance of the table)
5621 * - prefix_label Label to give columns from this address table instance
5622 * - group_bys enable these fields for group by - default false
5623 * - order_bys enable these fields for order by
5624 * - filters enable these fields for filtering
5625 *
5626 * @return array address columns definition
5627 */
be2fb01f
CW
5628 protected function getAddressColumns($options = []) {
5629 $defaultOptions = [
59b19369
JJ
5630 'prefix' => '',
5631 'prefix_label' => '',
5632 'fields' => TRUE,
5633 'group_bys' => FALSE,
5634 'order_bys' => TRUE,
5635 'filters' => TRUE,
5636 'join_filters' => FALSE,
be2fb01f
CW
5637 'fields_defaults' => [],
5638 'filters_defaults' => [],
5639 'group_bys_defaults' => [],
5640 'order_bys_defaults' => [],
5641 ];
59b19369
JJ
5642
5643 $options = array_merge($defaultOptions, $options);
5644 $defaults = $this->getDefaultsFromOptions($options);
5645 $tableAlias = $options['prefix'] . 'address';
5646
be2fb01f
CW
5647 $spec = [
5648 $options['prefix'] . 'name' => [
1836ab9e 5649 'title' => $options['prefix_label'] . ts('Address Name'),
59b19369
JJ
5650 'name' => 'name',
5651 'is_fields' => TRUE,
be2fb01f
CW
5652 ],
5653 $options['prefix'] . 'street_number' => [
59b19369 5654 'name' => 'street_number',
1836ab9e 5655 'title' => $options['prefix_label'] . ts('Street Number'),
59b19369
JJ
5656 'type' => 1,
5657 'is_fields' => TRUE,
be2fb01f
CW
5658 ],
5659 $options['prefix'] . 'odd_street_number' => [
8fc33241 5660 'title' => ts('Odd / Even Street Number'),
5661 'name' => 'odd_street_number',
5662 'type' => CRM_Utils_Type::T_INT,
5663 'no_display' => TRUE,
5664 'required' => TRUE,
38a85cdd 5665 'dbAlias' => "({$tableAlias}_civireport.street_number % 2)",
8fc33241 5666 'is_fields' => TRUE,
5667 'is_order_bys' => TRUE,
be2fb01f
CW
5668 ],
5669 $options['prefix'] . 'street_name' => [
59b19369 5670 'name' => 'street_name',
1836ab9e 5671 'title' => $options['prefix_label'] . ts('Street Name'),
59b19369
JJ
5672 'type' => 1,
5673 'is_fields' => TRUE,
5674 'is_filters' => TRUE,
5675 'operator' => 'like',
5676 'is_order_bys' => TRUE,
be2fb01f
CW
5677 ],
5678 $options['prefix'] . 'street_address' => [
1836ab9e 5679 'title' => $options['prefix_label'] . ts('Street Address'),
59b19369
JJ
5680 'name' => 'street_address',
5681 'is_fields' => TRUE,
5682 'is_filters' => TRUE,
5683 'is_group_bys' => TRUE,
be2fb01f
CW
5684 ],
5685 $options['prefix'] . 'supplemental_address_1' => [
1836ab9e 5686 'title' => $options['prefix_label'] . ts('Supplementary Address Field 1'),
59b19369
JJ
5687 'name' => 'supplemental_address_1',
5688 'is_fields' => TRUE,
be2fb01f
CW
5689 ],
5690 $options['prefix'] . 'supplemental_address_2' => [
1836ab9e 5691 'title' => $options['prefix_label'] . ts('Supplementary Address Field 2'),
59b19369
JJ
5692 'name' => 'supplemental_address_2',
5693 'is_fields' => TRUE,
be2fb01f
CW
5694 ],
5695 $options['prefix'] . 'supplemental_address_3' => [
1836ab9e 5696 'title' => $options['prefix_label'] . ts('Supplementary Address Field 3'),
59b19369
JJ
5697 'name' => 'supplemental_address_3',
5698 'is_fields' => TRUE,
be2fb01f
CW
5699 ],
5700 $options['prefix'] . 'street_number' => [
59b19369 5701 'name' => 'street_number',
1836ab9e 5702 'title' => $options['prefix_label'] . ts('Street Number'),
59b19369
JJ
5703 'type' => 1,
5704 'is_order_bys' => TRUE,
5705 'is_filters' => TRUE,
5706 'is_fields' => TRUE,
be2fb01f
CW
5707 ],
5708 $options['prefix'] . 'street_unit' => [
59b19369 5709 'name' => 'street_unit',
1836ab9e 5710 'title' => $options['prefix_label'] . ts('Street Unit'),
59b19369
JJ
5711 'type' => 1,
5712 'is_fields' => TRUE,
be2fb01f
CW
5713 ],
5714 $options['prefix'] . 'city' => [
1836ab9e 5715 'title' => $options['prefix_label'] . ts('City'),
59b19369
JJ
5716 'name' => 'city',
5717 'operator' => 'like',
5718 'is_fields' => TRUE,
5719 'is_filters' => TRUE,
5720 'is_group_bys' => TRUE,
5721 'is_order_bys' => TRUE,
be2fb01f
CW
5722 ],
5723 $options['prefix'] . 'postal_code' => [
1836ab9e 5724 'title' => $options['prefix_label'] . ts('Postal Code'),
59b19369 5725 'name' => 'postal_code',
1754f22f 5726 'type' => 2,
59b19369
JJ
5727 'is_fields' => TRUE,
5728 'is_filters' => TRUE,
5729 'is_group_bys' => TRUE,
5730 'is_order_bys' => TRUE,
be2fb01f
CW
5731 ],
5732 $options['prefix'] . 'postal_code_suffix' => [
1836ab9e 5733 'title' => $options['prefix_label'] . ts('Postal Code Suffix'),
e088ce02 5734 'name' => 'postal_code_suffix',
1754f22f 5735 'type' => 2,
59b19369
JJ
5736 'is_fields' => TRUE,
5737 'is_filters' => TRUE,
5738 'is_group_bys' => TRUE,
5739 'is_order_bys' => TRUE,
be2fb01f
CW
5740 ],
5741 $options['prefix'] . 'county_id' => [
1836ab9e 5742 'title' => $options['prefix_label'] . ts('County'),
59b19369
JJ
5743 'alter_display' => 'alterCountyID',
5744 'name' => 'county_id',
5745 'type' => CRM_Utils_Type::T_INT,
5746 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
5747 'options' => CRM_Core_PseudoConstant::county(),
5748 'is_fields' => TRUE,
5749 'is_filters' => TRUE,
5750 'is_group_bys' => TRUE,
be2fb01f
CW
5751 ],
5752 $options['prefix'] . 'state_province_id' => [
1836ab9e 5753 'title' => $options['prefix_label'] . ts('State/Province'),
59b19369
JJ
5754 'alter_display' => 'alterStateProvinceID',
5755 'name' => 'state_province_id',
5756 'type' => CRM_Utils_Type::T_INT,
5757 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
5758 'options' => CRM_Core_PseudoConstant::stateProvince(),
5759 'is_fields' => TRUE,
5760 'is_filters' => TRUE,
5761 'is_group_bys' => TRUE,
be2fb01f
CW
5762 ],
5763 $options['prefix'] . 'country_id' => [
1836ab9e 5764 'title' => $options['prefix_label'] . ts('Country'),
59b19369
JJ
5765 'alter_display' => 'alterCountryID',
5766 'name' => 'country_id',
5767 'is_fields' => TRUE,
5768 'is_filters' => TRUE,
5769 'is_group_bys' => TRUE,
5770 'type' => CRM_Utils_Type::T_INT,
5771 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
5772 'options' => CRM_Core_PseudoConstant::country(),
be2fb01f
CW
5773 ],
5774 $options['prefix'] . 'location_type_id' => [
cab1863a 5775 'name' => 'location_type_id',
1836ab9e 5776 'title' => $options['prefix_label'] . ts('Location Type'),
59b19369
JJ
5777 'type' => CRM_Utils_Type::T_INT,
5778 'is_fields' => TRUE,
5779 'alter_display' => 'alterLocationTypeID',
be2fb01f
CW
5780 ],
5781 $options['prefix'] . 'id' => [
1836ab9e 5782 'title' => $options['prefix_label'] . ts('ID'),
59b19369
JJ
5783 'name' => 'id',
5784 'is_fields' => TRUE,
be2fb01f
CW
5785 ],
5786 $options['prefix'] . 'is_primary' => [
59b19369 5787 'name' => 'is_primary',
1836ab9e 5788 'title' => $options['prefix_label'] . ts('Primary Address?'),
59b19369
JJ
5789 'type' => CRM_Utils_Type::T_BOOLEAN,
5790 'is_fields' => TRUE,
be2fb01f
CW
5791 ],
5792 ];
59b19369
JJ
5793 return $this->buildColumns($spec, $options['prefix'] . 'civicrm_address', 'CRM_Core_DAO_Address', $tableAlias, $defaults, $options);
5794 }
5795
d7e34fc6 5796 /**
5797 * Build the columns.
5798 *
5799 * The normal report class needs you to remember to do a few things that are often erratic
5800 *
5801 * 1) use a unique key for any field that might not be unique (e.g. start date, label)
5802 * - this class will prepend an alias to the key & set the 'name' if you don't set it yourself.
5803 * You can suppress the alias with 'no_field_disambiguation' if transitioning existing reports. This
5804 * means any saved filters / fields on saved report instances. This will mean that matching names from
5805 * different tables may be ambigious, but it will smooth any code transition.
5806 * - note that it assumes the value being passed in is the actual table field name
5807 *
5808 * 2) set the field & set it to no display if you don't want the field but you might want to use the field in other
5809 * contexts - the code looks up the fields array for data - so it both defines the field spec & the fields you want to show
5810 *
5811 * 3) this function also sets the 'metadata' array - the extended report class now uses this in place
5812 * of the fields array to reduce the issues caused when metadata is needed but 'fields' are not defined. Code in
5813 * the core classes can start to move towards that.
5814 *
5815 * @param array $specs
5816 * @param string $tableName
5817 * @param string $daoName
5818 * @param string $tableAlias
5819 * @param array $defaults
5820 * @param array $options
5821 *
5822 * @return array
5823 */
be2fb01f 5824 protected function buildColumns($specs, $tableName, $daoName = NULL, $tableAlias = NULL, $defaults = [], $options = []) {
d7e34fc6 5825 if (!$tableAlias) {
5826 $tableAlias = str_replace('civicrm_', '', $tableName);
5827 }
be2fb01f
CW
5828 $types = ['filters', 'group_bys', 'order_bys', 'join_filters'];
5829 $columns = [$tableName => array_fill_keys($types, [])];
d7e34fc6 5830 // The code that uses this no longer cares if it is a DAO or BAO so just call it a DAO.
5831 $columns[$tableName]['dao'] = $daoName;
5832 $columns[$tableName]['alias'] = $tableAlias;
5833
5834 foreach ($specs as $specName => $spec) {
5835 if (empty($spec['name'])) {
5836 $spec['name'] = $specName;
5837 }
5838
5839 $fieldAlias = (empty($options['no_field_disambiguation']) ? $tableAlias . '_' : '') . $specName;
5840 $columns[$tableName]['metadata'][$fieldAlias] = $spec;
5841 $columns[$tableName]['fields'][$fieldAlias] = $spec;
5842 if (isset($defaults['fields_defaults']) && in_array($spec['name'], $defaults['fields_defaults'])) {
5843 $columns[$tableName]['fields'][$fieldAlias]['default'] = TRUE;
5844 }
5845
5846 if (!$spec['is_fields'] || (isset($options['fields_excluded']) && in_array($specName, $options['fields_excluded']))) {
5847 $columns[$tableName]['fields'][$fieldAlias]['no_display'] = TRUE;
5848 }
5849
5850 if (isset($options['fields_required']) && in_array($specName, $options['fields_required'])) {
5851 $columns[$tableName]['fields'][$fieldAlias]['required'] = TRUE;
5852 }
5853
5854 foreach ($types as $type) {
5855 if ($options[$type] && !empty($spec['is_' . $type])) {
5856 $columns[$tableName][$type][$fieldAlias] = $spec;
5857 if (isset($defaults[$type . '_defaults']) && isset($defaults[$type . '_defaults'][$spec['name']])) {
5858 $columns[$tableName][$type][$fieldAlias]['default'] = $defaults[$type . '_defaults'][$spec['name']];
5859 }
5860 }
5861 }
5862 }
5863 return $columns;
5864 }
5865
534c4d20 5866 /**
5867 * Store group bys into array - so we can check elsewhere what is grouped.
5868 */
5869 protected function storeGroupByArray() {
5870
c3fdd2b7 5871 if (!CRM_Utils_Array::value('group_bys', $this->_params)
5872 || !is_array($this->_params['group_bys'])) {
5873 $this->_params['group_bys'] = [];
5874 }
534c4d20 5875
c3fdd2b7 5876 foreach ($this->_columns as $tableName => $table) {
5877 $table = $this->_columns[$tableName];
5878 if (array_key_exists('group_bys', $table)) {
5879 foreach ($table['group_bys'] as $fieldName => $fieldData) {
5880 $field = $this->_columns[$tableName]['metadata'][$fieldName];
5881 if (!empty($this->_params['group_bys'][$fieldName]) || !empty($fieldData['required'])) {
5882 if (!empty($field['chart'])) {
5883 $this->assign('chartSupported', TRUE);
5884 }
534c4d20 5885
c3fdd2b7 5886 if (!empty($table['group_bys'][$fieldName]['frequency']) &&
5887 !empty($this->_params['group_bys_freq'][$fieldName])
5888 ) {
534c4d20 5889
c3fdd2b7 5890 switch ($this->_params['group_bys_freq'][$fieldName]) {
5891 case 'FISCALYEAR':
5892 $this->_groupByArray[$tableName . '_' . $fieldName . '_start'] = self::fiscalYearOffset($field['dbAlias']);
bd2913d3 5893 break;
534c4d20 5894
c3fdd2b7 5895 case 'YEAR':
bd2913d3 5896 $this->_groupByArray[$tableName . '_' . $fieldName . '_start'] = " YEAR({$field['dbAlias']})";
5897 break;
c3fdd2b7 5898
bd2913d3 5899 case 'QUARTER':
5900 $this->_groupByArray[$tableName . '_' . $fieldName . '_start'] = "YEAR({$field['dbAlias']}), QUARTER({$field['dbAlias']})";
5901 break;
534c4d20 5902
bd2913d3 5903 case 'YEARWEEK':
5904 $this->_groupByArray[$tableName . '_' . $fieldName . '_start'] = "YEARWEEK({$field['dbAlias']})";
5905 break;
5906
5907 case 'MONTH':
5908 $this->_groupByArray[$tableName . '_' . $fieldName . '_start'] = "EXTRACT(YEAR_MONTH FROM {$field['dbAlias']})";
5909 break;
5910
5911 case 'DATE':
5912 $this->_groupByArray[$tableName . '_' . $fieldName . '_start'] = "DATE({$field['dbAlias']})";
5913 break;
534c4d20 5914 }
c3fdd2b7 5915 }
5916 else {
5917 if (!in_array($field['dbAlias'], $this->_groupByArray)) {
5918 $this->_groupByArray[$tableName . '_' . $fieldName] = $field['dbAlias'];
534c4d20 5919 }
5920 }
5921 }
534c4d20 5922 }
c3fdd2b7 5923
534c4d20 5924 }
5925 }
5926 }
5927
d7e34fc6 5928 /**
5929 * @param $options
5930 *
5931 * @return array
5932 */
5933 protected function getDefaultsFromOptions($options) {
be2fb01f 5934 $defaults = [
d7e34fc6 5935 'fields_defaults' => $options['fields_defaults'],
5936 'filters_defaults' => $options['filters_defaults'],
5937 'group_bys_defaults' => $options['group_bys_defaults'],
5938 'order_bys_defaults' => $options['order_bys_defaults'],
be2fb01f 5939 ];
d7e34fc6 5940 return $defaults;
5941 }
5942
81a22d3d 5943 /**
5944 * Get the select clause for a field, wrapping in GROUP_CONCAT if appropriate.
5945 *
5946 * Full group by mode dictates that a field must either be in the group by function or
5947 * wrapped in a aggregate function. Here we wrap the field in GROUP_CONCAT if it is not in the
5948 * group concat.
5949 *
5950 * @param string $tableName
5951 * @param string $fieldName
5952 * @param string $field
5953 * @return string
5954 */
5955 protected function getSelectClauseWithGroupConcatIfNotGroupedBy($tableName, &$fieldName, &$field) {
5956 if ($this->groupConcatTested && (!empty($this->_groupByArray) || $this->isForceGroupBy)) {
5957 if ((empty($field['statistics']) || in_array('GROUP_CONCAT', $field['statistics']))) {
5958 $label = CRM_Utils_Array::value('title', $field);
d7a896b4 5959 $alias = $field['tplField'] ?? "{$tableName}_{$fieldName}";
81a22d3d 5960 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $label;
5961 $this->_selectAliases[] = $alias;
5962 if (empty($this->_groupByArray[$tableName . '_' . $fieldName])) {
5963 return "GROUP_CONCAT(DISTINCT {$field['dbAlias']}) as $alias";
5964 }
5965 return "({$field['dbAlias']}) as $alias";
5966 }
5967 }
5968 }
5969
62491db8 5970 /**
5971 * Generate clause for the selected filter.
5972 *
5973 * @param array $field
5974 * Field specification
5975 * @param string $fieldName
5976 * Field name.
5977 *
5978 * @return string
5979 * Relevant where clause.
5980 */
5981 protected function generateFilterClause($field, $fieldName) {
5982 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
5983 if (CRM_Utils_Array::value('operatorType', $field) ==
5984 CRM_Report_Form::OP_MONTH
5985 ) {
5986 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
5987 $value = CRM_Utils_Array::value("{$fieldName}_value", $this->_params);
5988 if (is_array($value) && !empty($value)) {
5989 return "(month({$field['dbAlias']}) $op (" . implode(', ', $value) .
5990 '))';
5991 }
5992 }
5993 else {
5994 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
5995 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
5996 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
5997 $fromTime = CRM_Utils_Array::value("{$fieldName}_from_time", $this->_params);
5998 $toTime = CRM_Utils_Array::value("{$fieldName}_to_time", $this->_params);
5999 return $this->dateClause($field['dbAlias'], $relative, $from, $to, $field['type'], $fromTime, $toTime);
6000 }
6001 }
6002 else {
6003 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
6004 if ($op) {
6005 return $this->whereClause($field,
6006 $op,
6007 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
6008 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
6009 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
6010 );
6011 }
6012 }
6013 return '';
6014 }
6015
232624b1 6016}