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