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