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