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