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