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