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