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