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