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