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