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