forked from civicrm/civicrm-core
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathForm.php
6118 lines (5585 loc) · 197 KB
/
Form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
use Civi\Report\OutputHandlerFactory;
/**
* Class CRM_Report_Form
*/
class CRM_Report_Form extends CRM_Core_Form {
/**
* Variables smarty expects to have set.
*
* We ensure these are assigned (value = NULL) when Smarty is instantiated in
* order to avoid e-notices / having to use empty or isset in the template layer.
*
* @var string[]
*/
public $expectedSmartyVariables = ['pager', 'skip', 'sections', 'grandStat', 'chartEnabled', 'uniqueId', 'rows', 'group_bys_freq'];
/**
* Deprecated constant, Reports should be updated to use the getRowCount function.
*/
const ROW_COUNT_LIMIT = 50;
/**
* Operator types - used for displaying filter elements
*/
const
OP_INT = 1,
OP_STRING = 2,
OP_DATE = 4,
OP_DATETIME = 5,
OP_FLOAT = 8,
OP_SELECT = 64,
OP_MULTISELECT = 65,
OP_MULTISELECT_SEPARATOR = 66,
OP_MONTH = 128,
OP_ENTITYREF = 256;
/**
* The id of the report instance
*
* @var int
*/
protected $_id;
/**
* The Number of rows to display on screen
* @var int
*/
protected $_rowCount;
/**
* The id of the report template
*
* @var int
*/
protected $_templateID;
/**
* The report title
*
* @var string
*/
protected $_title;
protected $_noFields = FALSE;
/**
* The set of all columns in the report. An associative array
* with column name as the key and attributes as the value
*
* @var array
*/
protected $_columns = [];
/**
* The set of filters in the report
*
* @var array
*/
protected $_filters = [];
/**
* The set of optional columns in the report
*
* @var array
*/
public $_options = [];
/**
* By default most reports hide contact id.
* Setting this to true makes it available
* @var bool
*/
protected $_exposeContactID = TRUE;
/**
* Set of statistic fields
*
* @var array
*/
protected $_statFields = [];
/**
* Set of statistics data
*
* @var array
*/
protected $_statistics = [];
/**
* List of fields not to be repeated during display
*
* @var array
*/
protected $_noRepeats = [];
/**
* List of fields not to be displayed
*
* @var array
*/
protected $_noDisplay = [];
/**
* Object type that a custom group extends
*
* @var null
*/
protected $_customGroupExtends = NULL;
protected $_customGroupExtendsJoin = [];
protected $_customGroupFilters = TRUE;
protected $_customGroupGroupBy = FALSE;
protected $_customGroupJoin = 'LEFT JOIN';
/**
* Build tags filter
* @var bool
*/
protected $_tagFilter = FALSE;
/**
* specify entity table for tags filter
* @var string
*/
protected $_tagFilterTable = 'civicrm_contact';
/**
* Build groups filter.
*
* @var bool
*/
protected $_groupFilter = FALSE;
/**
* Has the report been optimised for group filtering.
*
* The functionality for group filtering has been improved but not
* all reports have been adjusted to take care of it.
*
* This property exists to highlight the reports which are still using the
* slow method & allow group filtering to still work for them until they
* can be migrated.
*
* In order to protect extensions we have to default to TRUE - but I have
* separately marked every class with a groupFilter in the hope that will trigger
* people to fix them as they touch them.
*
* @var bool
* @see https://issues.civicrm.org/jira/browse/CRM-19170
*/
protected $groupFilterNotOptimised = TRUE;
/**
* Navigation fields
*
* @var array
*/
public $_navigation = [];
public $_drilldownReport = [];
/**
* Array of tabs to display on report.
*
* E.g we define the tab title, the tpl and the tab-specific part of the css or html link.
*
* $this->tabs['OrderBy'] = array(
* 'title' => ts('Sorting'),
* 'tpl' => 'OrderBy',
* 'div_label' => 'order-by',
* );
*
* @var array
*/
protected $tabs = [];
/**
* Should we add paging.
*
* @var bool
*/
protected $addPaging = TRUE;
protected $isForceGroupBy = FALSE;
protected $groupConcatTested = FALSE;
/**
* Are we in print mode. Can be set by another outputMode, ex: sendmail.
*
* @var bool
*/
public $printOnly = FALSE;
/**
* An attribute for checkbox/radio form field layout
*
* @var array
*/
protected $_fourColumnAttribute = [
'</td><td width="25%">',
'</td><td width="25%">',
'</td><td width="25%">',
'</tr><tr><td>',
];
protected $_force = 1;
protected $_params = NULL;
protected $_formValues = NULL;
protected $_instanceValues = NULL;
protected $_instanceForm = FALSE;
protected $_criteriaForm = FALSE;
protected $_instanceButtonName = NULL;
protected $_createNewButtonName = NULL;
protected $_groupButtonName = NULL;
protected $_chartButtonName = NULL;
protected $_csvSupported = TRUE;
protected $_add2groupSupported = TRUE;
protected $_groups = NULL;
protected $_grandFlag = FALSE;
protected $_rowsFound;
/**
* @var array
*/
protected $_selectAliases = [];
protected $_rollup;
/**
* Table containing list of contact IDs within the group filter.
*
* @var string
*/
protected $groupTempTable = '';
/**
* Table aliases. May be altered by hook_civicrm_alterReportVar.
* @var array
*/
public $_aliases = [];
/**
* SQL where clause. May be altered by hook_civicrm_alterReportVar.
* @var string
*/
public $_where;
/**
* SQL from clause. May be altered by hook_civicrm_alterReportVar.
* @var string
*/
public $_from;
/**
* SQL Limit clause
* @var string
*/
protected $_limit = NULL;
/**
* This can be set to specify a limit to the number of rows
* Since it is currently envisaged as part of the api usage it is only being applied
* when $_output mode is not 'html' or 'group' so as not to have to interpret / mess with that part
* of the code (see limit() fn.
*
* @var int
*/
protected $_limitValue = NULL;
/**
* This can be set to specify row offset
* See notes on _limitValue
*
* @var int
*/
protected $_offsetValue = NULL;
/**
* @var array
*/
protected $_sections = [];
protected $_autoIncludeIndexedFieldsAsOrderBys = 0;
/**
* Whether to generate absolute URLs (ex: in report emails).
*
* @var bool
*/
public $_absoluteUrl = FALSE;
/**
* Flag to indicate if result-set is to be stored in a class variable which could be retrieved using getResultSet() method.
*
* @var bool
*/
protected $_storeResultSet = FALSE;
/**
* When _storeResultSet Flag is set use this var to store result set in form of array
*
* @var bool
*/
protected $_resultSet = [];
/**
* To what frequency group-by a date column
*
* @var array
*/
protected $_groupByDateFreq = [
'MONTH' => 'Month',
'YEARWEEK' => 'Week',
'QUARTER' => 'Quarter',
'YEAR' => 'Year',
];
/**
* Variables to hold the acl inner join and where clause
* @var string|null
*/
protected $_aclFrom = NULL;
protected $_aclWhere = NULL;
/**
* Array of DAO tables having columns included in SELECT or ORDER BY clause.
*
* Where has also been added to this although perhaps the 'includes both' array should have a different name.
*
* @var array
*/
protected $_selectedTables = [];
/**
* Array of DAO tables having columns included in WHERE or HAVING clause
*
* @var array
*/
protected $filteredTables;
/**
* Output mode e.g 'print', 'csv', 'pdf'.
*
* @var string
*/
protected $_outputMode;
/**
* Format of any chart in use.
*
* (it's unclear if this could be merged with outputMode at this stage)
*
* @var string|null
*/
protected $_format;
public $_having = NULL;
public $_select = NULL;
public $_selectClauses = [];
public $_columnHeaders = [];
public $_orderBy = NULL;
public $_orderByFields = [];
public $_orderByArray = [];
/**
* Array of clauses to group by.
*
* @var array
*/
protected $_groupByArray = [];
public $_groupBy = NULL;
public $_whereClauses = [];
public $_havingClauses = [];
/**
* DashBoardRowCount Dashboard row count.
*
* @var int
*/
public $_dashBoardRowCount;
/**
* Is this being called without a form controller (ie. the report is being render outside the normal form
* - e.g the api is retrieving the rows.
*
* @var bool
*/
public $noController = FALSE;
/**
* Variable to hold the currency alias.
*
* @var string|null
*/
protected $_currencyColumn = NULL;
/**
* @var string
*/
protected $_interval;
/**
* @var bool
*/
protected $_sendmail;
/**
* @var int
*/
protected $_chartId;
/**
* @var int
*/
public $_section;
/**
* Report description.
*
* @var string
*/
public $_description;
/**
* Is an address field selected.
*
* @var bool
* This was intended to determine if the address table should be joined in
* The isTableSelected function is now preferred for this purpose
*/
protected $_addressField;
/**
* Is an email field selected.
*
* @var bool
* This was intended to determine if the email table should be joined in
* The isTableSelected function is now preferred for this purpose
*/
protected $_emailField;
/**
* Is a phone field selected.
*
* @var bool
* This was intended to determine if the phone table should be joined in
* The isTableSelected function is now preferred for this purpose
*/
protected $_phoneField;
/**
* Create new report instance? (or update existing) on save.
*
* @var bool
*/
protected $_createNew;
/**
* When a grand total row has calculated the status we pop it off to here.
*
* This allows us to access it from the stats function and avoid recalculating.
*
* @var array
*/
protected $rollupRow = [];
/**
* Database attributes - character set and collation.
*
* @var string
*/
protected $_databaseAttributes = ' DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci';
/**
* SQL being run in this report.
*
* The sql in the report is stored in this variable in order to be displayed on the developer tab.
*
* @var string
*/
protected $sql;
/**
* An instruction not to add a Group By.
*
* This is relevant where the group by might be otherwise added after the code that determines the group by array.
*
* e.g. where stat fields are being added but other settings cause it to not be desirable to add a group by
* such as in pivot charts when no row header is set
*
* @var bool
*/
protected $noGroupBy = FALSE;
/**
* SQL being run in this report as an array.
*
* The sql in the report is stored in this variable in order to be returned to api & test calls.
*
* @var array
*/
protected $sqlArray;
/**
* Tables created for the report that need removal afterwards.
*
* ['civicrm_temp_report_x' => ['temporary' => TRUE, 'name' => 'civicrm_temp_report_x']
* @var array
*/
protected $temporaryTables = [];
/**
* Can this report use the sql mode ONLY_FULL_GROUP_BY.
* @var bool
*/
public $optimisedForOnlyFullGroupBy = TRUE;
/**
* Determines which chart types are supported for this report
* @var string[]
*/
protected $_charts = [];
/**
* Array of campaign data,
* populated by calling `$this::addCampaignFields()`
*
* @var array
*/
protected $campaigns = [];
/**
* @var \Civi\Report\OutputHandlerInterface
*/
private $outputHandler;
/**
* Get the number of rows to show
* @return int
*/
public function getRowCount(): int {
return $this->_rowCount;
}
/**
* set the number of rows to show
* @param int $rowCount
*/
public function setRowCount($rowCount): void {
$this->_rowCount = $rowCount;
}
/**
* Class constructor.
*/
public function __construct() {
parent::__construct();
$this->setRowCount(\Civi::settings()->get('default_pager_size'));
$this->addClass('crm-report-form');
if ($this->_tagFilter) {
$this->buildTagFilter();
}
if ($this->_exposeContactID) {
if (array_key_exists('civicrm_contact', $this->_columns)) {
$this->_columns['civicrm_contact']['fields']['exposed_id'] = [
'name' => 'id',
'title' => ts('Contact ID'),
'no_repeat' => TRUE,
];
}
}
if ($this->_groupFilter) {
$this->buildGroupFilter();
}
$this->addCustomDataToColumns();
// add / modify display columns, filters ..etc
CRM_Utils_Hook::alterReportVar('columns', $this->_columns, $this);
//assign currencyColumn variable to tpl
$this->assign('currencyColumn', $this->_currencyColumn);
}
/**
* Shared pre-process function.
*
* If overriding preProcess function this should still be called.
*
* @throws \Exception
*/
public function preProcessCommon() {
$this->_force = CRM_Utils_Request::retrieve('force', 'Boolean');
// Ensure smarty variables are assigned here since this function is called from
// the report api and the main buildForm is not.
self::$_template->ensureVariablesAreAssigned($this->expectedSmartyVariables);
$this->_dashBoardRowCount = CRM_Utils_Request::retrieve('rowCount', 'Integer') ?? CRM_Utils_Request::retrieve('crmRowCount', 'Integer');
$this->_section = CRM_Utils_Request::retrieve('section', 'Integer');
$this->assign('section', $this->_section);
CRM_Core_Region::instance('page-header')->add([
'markup' => sprintf('<!-- Report class: [%s] -->', htmlentities(get_class($this))),
]);
if (!$this->noController) {
$this->setID($this->get('instanceId'));
if (!$this->_id) {
$this->setID(CRM_Report_Utils_Report::getInstanceID());
if (!$this->_id) {
$this->setID(CRM_Report_Utils_Report::getInstanceIDForPath());
}
}
// set qfkey so that pager picks it up and use it in the "Next > Last >>" links.
// FIXME: Note setting it in $_GET doesn't work, since pager generates link based on QUERY_STRING
if (!isset($_SERVER['QUERY_STRING'])) {
// in php 7.4 can do this with less lines with ??=
$_SERVER['QUERY_STRING'] = '';
}
$_SERVER['QUERY_STRING'] .= "&qfKey={$this->controller->_key}";
}
if ($this->_id) {
$this->assign('instanceId', $this->_id);
$params = ['id' => $this->_id];
$this->_instanceValues = [];
CRM_Core_DAO::commonRetrieve('CRM_Report_DAO_ReportInstance',
$params,
$this->_instanceValues
);
if (empty($this->_instanceValues)) {
CRM_Core_Error::statusBounce("Report could not be loaded.");
}
$this->_title = $this->_instanceValues['title'];
if (!empty($this->_instanceValues['permission']) &&
(!(CRM_Core_Permission::check($this->_instanceValues['permission']) ||
CRM_Core_Permission::check('administer Reports')
))
) {
CRM_Utils_System::permissionDenied();
CRM_Utils_System::civiExit();
}
$formValues = $this->_instanceValues['form_values'] ?? NULL;
if ($formValues) {
$this->_formValues = CRM_Utils_String::unserialize($formValues);
}
else {
$this->_formValues = NULL;
}
$this->setOutputMode();
if ($this->_outputMode === 'copy') {
$this->_createNew = TRUE;
$this->_params = $this->_formValues;
$this->_params['view_mode'] = 'criteria';
$this->_params['title'] = $this->getTitle() . ts(' (copy created by %1 on %2)', [
CRM_Core_Session::singleton()->getLoggedInContactDisplayName(),
CRM_Utils_Date::customFormat(date('Y-m-d H:i')),
]);
// Do not pass go. Do not collect another chance to re-run the same query.
CRM_Report_Form_Instance::postProcess($this);
}
// lets always do a force if reset is found in the url.
// Hey why not? see CRM-17225 for more about this. The use of reset to be force is historical for reasons stated
// in the comment line above these 2.
if (!empty($_REQUEST['reset'])
&& !in_array(CRM_Utils_Request::retrieve('output', 'String'), ['save', 'criteria'])) {
$this->_force = 1;
}
// set the mode
$this->assign('mode', 'instance');
}
elseif (!$this->noController) {
[$optionValueID, $optionValue] = CRM_Report_Utils_Report::getValueIDFromUrl();
$instanceCount = CRM_Report_Utils_Report::getInstanceCount($optionValue);
if (($instanceCount > 0) && $optionValueID) {
$this->assign('instanceUrl',
CRM_Utils_System::url('civicrm/report/list',
"reset=1&ovid=$optionValueID"
)
);
}
if ($optionValueID) {
$this->_description = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $optionValueID, 'description');
}
// set the mode
$this->assign('mode', 'template');
}
// lets display the Report Settings section
$this->_instanceForm = $this->_force || $this->_id || (!empty($_POST));
// Do not display Report Settings section if administer Reports permission is absent OR
// if report instance is reserved and administer reserved reports absent
if (!CRM_Core_Permission::check('administer Reports') ||
(!empty($this->_instanceValues['is_reserved']) &&
!CRM_Core_Permission::check('administer reserved reports'))
) {
$this->_instanceForm = FALSE;
}
$this->assign('criteriaForm', FALSE);
// Will be overwritten in postProcess if TRUE.
$this->assign('printOnly', FALSE);
// Display Report Criteria section if user has access Report Criteria OR administer Reports AND report instance is not reserved
if (CRM_Core_Permission::check('administer Reports') ||
CRM_Core_Permission::check('access Report Criteria')
) {
if (empty($this->_instanceValues['is_reserved']) ||
CRM_Core_Permission::check('administer reserved reports')
) {
$this->assign('criteriaForm', TRUE);
$this->_criteriaForm = TRUE;
}
}
// Special permissions check for private instance if it's not the current contact instance
if ($this->_id &&
(CRM_Report_BAO_ReportInstance::reportIsPrivate($this->_id) &&
!CRM_Report_BAO_ReportInstance::contactIsOwner($this->_id))) {
if (!CRM_Core_Permission::check('access all private reports')) {
$this->_instanceForm = FALSE;
$this->assign('criteriaForm', FALSE);
}
}
$this->_instanceButtonName = $this->getButtonName('submit', 'save');
$this->_createNewButtonName = $this->getButtonName('submit', 'next');
$this->_groupButtonName = $this->getButtonName('submit', 'group');
$this->_chartButtonName = $this->getButtonName('submit', 'chart');
// graphs require the visual bundle
\Civi::resources()->addBundle('visual');
}
/**
* Add bread crumb.
*/
public function addBreadCrumb() {
$breadCrumbs
= [
[
'title' => ts('Report Templates'),
'url' => CRM_Utils_System::url('civicrm/admin/report/template/list', 'reset=1'),
],
];
CRM_Utils_System::appendBreadCrumb($breadCrumbs);
}
/**
* Pre process function.
*
* Called prior to build form.
*/
public function preProcess() {
$this->preProcessCommon();
if (!$this->_id) {
$this->addBreadCrumb();
}
foreach ($this->_columns as $tableName => $table) {
$this->setTableAlias($table, $tableName);
$expFields = [];
// higher preference to bao object
$daoOrBaoName = $table['bao'] ?? $table['dao'] ?? NULL;
if ($daoOrBaoName) {
if (method_exists($daoOrBaoName, 'exportableFields')) {
$expFields = $daoOrBaoName::exportableFields();
}
else {
$expFields = $daoOrBaoName::export();
}
}
$doNotCopy = ['required', 'default'];
$fieldGroups = ['fields', 'filters', 'group_bys', 'order_bys'];
foreach ($fieldGroups as $fieldGrp) {
if (!empty($table[$fieldGrp]) && is_array($table[$fieldGrp])) {
foreach ($table[$fieldGrp] as $fieldName => $field) {
// $name is the field name used to reference the BAO/DAO export fields array
$name = $field['name'] ?? $fieldName;
// Sometimes the field name key in the BAO/DAO export fields array is
// different from the actual database field name.
// Unset $field['name'] so that actual database field name can be obtained
// from the BAO/DAO export fields array.
unset($field['name']);
if (array_key_exists($name, $expFields)) {
foreach ($doNotCopy as $dnc) {
// unset the values we don't want to be copied.
unset($expFields[$name][$dnc]);
}
if (empty($field)) {
$this->_columns[$tableName][$fieldGrp][$fieldName] = $expFields[$name];
}
else {
foreach ($expFields[$name] as $property => $val) {
if (!array_key_exists($property, $field)) {
$this->_columns[$tableName][$fieldGrp][$fieldName][$property] = $val;
}
}
}
}
// fill other vars
if (!empty($field['no_repeat'])) {
$this->_noRepeats[] = "{$tableName}_{$fieldName}";
}
if (!empty($field['no_display'])) {
$this->_noDisplay[] = "{$tableName}_{$fieldName}";
}
// set alias = table-name, unless already set
$alias = $field['alias'] ?? (
$this->_columns[$tableName]['alias'] ?? $tableName
);
$this->_columns[$tableName][$fieldGrp][$fieldName]['alias'] = $alias;
// set name = fieldName, unless already set
if (!isset($this->_columns[$tableName][$fieldGrp][$fieldName]['name'])) {
$this->_columns[$tableName][$fieldGrp][$fieldName]['name'] = $name;
}
if (!isset($this->_columns[$tableName][$fieldGrp][$fieldName]['table_name'])) {
$this->_columns[$tableName][$fieldGrp][$fieldName]['table_name'] = $tableName;
}
// set dbAlias = alias.name, unless already set
if (!isset($this->_columns[$tableName][$fieldGrp][$fieldName]['dbAlias'])) {
$this->_columns[$tableName][$fieldGrp][$fieldName]['dbAlias']
= $alias . '.' .
$this->_columns[$tableName][$fieldGrp][$fieldName]['name'];
}
// a few auto fills for filters
if ($fieldGrp == 'filters') {
// fill operator types
if (!array_key_exists('operatorType', $this->_columns[$tableName][$fieldGrp][$fieldName])) {
switch ($this->_columns[$tableName][$fieldGrp][$fieldName]['type'] ?? NULL) {
case CRM_Utils_Type::T_MONEY:
case CRM_Utils_Type::T_FLOAT:
$this->_columns[$tableName][$fieldGrp][$fieldName]['operatorType'] = CRM_Report_Form::OP_FLOAT;
break;
case CRM_Utils_Type::T_INT:
$this->_columns[$tableName][$fieldGrp][$fieldName]['operatorType'] = CRM_Report_Form::OP_INT;
break;
case CRM_Utils_Type::T_DATE:
$this->_columns[$tableName][$fieldGrp][$fieldName]['operatorType'] = CRM_Report_Form::OP_DATE;
break;
case CRM_Utils_Type::T_BOOLEAN:
$this->_columns[$tableName][$fieldGrp][$fieldName]['operatorType'] = CRM_Report_Form::OP_SELECT;
if (!array_key_exists('options', $this->_columns[$tableName][$fieldGrp][$fieldName])) {
$this->_columns[$tableName][$fieldGrp][$fieldName]['options']
= [
'' => ts('Any'),
'0' => ts('No'),
'1' => ts('Yes'),
];
}
break;
default:
if ($daoOrBaoName &&
array_key_exists('pseudoconstant', $this->_columns[$tableName][$fieldGrp][$fieldName])
) {
// with multiple options operator-type is generally multi-select
$this->_columns[$tableName][$fieldGrp][$fieldName]['operatorType'] = CRM_Report_Form::OP_MULTISELECT;
if (!array_key_exists('options', $this->_columns[$tableName][$fieldGrp][$fieldName])) {
// fill options
$this->_columns[$tableName][$fieldGrp][$fieldName]['options'] = CRM_Core_PseudoConstant::get($daoOrBaoName, $fieldName);
}
}
break;
}
}
}
if (!isset($this->_columns[$tableName]['metadata'][$fieldName])) {
$this->_columns[$tableName]['metadata'][$fieldName] = $this->_columns[$tableName][$fieldGrp][$fieldName];
}
else {
$this->_columns[$tableName]['metadata'][$fieldName] = array_merge($this->_columns[$tableName][$fieldGrp][$fieldName], $this->_columns[$tableName]['metadata'][$fieldName]);
}
}
}
}
// copy filters to a separate handy variable
if (array_key_exists('filters', $table)) {
$this->_filters[$tableName] = $this->_columns[$tableName]['filters'];
}
if (array_key_exists('group_bys', $table)) {
$groupBys[$tableName] = $this->_columns[$tableName]['group_bys'];
}
if (array_key_exists('fields', $table)) {
$reportFields[$tableName] = $this->_columns[$tableName]['fields'];
}
}
if ($this->_force) {
$this->setDefaultValues(FALSE);
}
CRM_Report_Utils_Get::processFilter($this->_filters, $this->_defaults);
CRM_Report_Utils_Get::processGroupBy($groupBys, $this->_defaults);
CRM_Report_Utils_Get::processFields($reportFields, $this->_defaults);
CRM_Report_Utils_Get::processChart($this->_defaults);
if ($this->_force) {
$this->_formValues = $this->_defaults;
$this->postProcess();
}
}
/**
* Set default values.
*
* @param bool $freeze
*
* @return array
*/
public function setDefaultValues($freeze = TRUE) {
$freezeGroup = [];
// FIXME: generalizing form field naming conventions would reduce
// Lots of lines below.
foreach ($this->_columns as $tableName => $table) {
if (array_key_exists('fields', $table)) {
foreach ($table['fields'] as $fieldName => $field) {
if (empty($field['no_display'])) {
if (!empty($field['required'])) {
// set default
$this->_defaults['fields'][$fieldName] = 1;
if ($freeze) {
// find element object, so that we could use quickform's freeze method
// for required elements
$obj = $this->getElementFromGroup("fields", $fieldName);
if ($obj) {
$freezeGroup[] = $obj;
}
}
}
elseif (isset($field['default'])) {
$this->_defaults['fields'][$fieldName] = $field['default'];
}
}
}