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