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