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