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