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