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