Merge pull request #13964 from eileenmcnaughton/test
[civicrm-core.git] / CRM / Report / Form.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
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 |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Class CRM_Report_Form
30 */
31 class CRM_Report_Form extends CRM_Core_Form {
32 const ROW_COUNT_LIMIT = 50;
33
34 /**
35 * Operator types - used for displaying filter elements
36 */
37 const
38 OP_INT = 1,
39 OP_STRING = 2,
40 OP_DATE = 4,
41 OP_DATETIME = 5,
42 OP_FLOAT = 8,
43 OP_SELECT = 64,
44 OP_MULTISELECT = 65,
45 OP_MULTISELECT_SEPARATOR = 66,
46 OP_MONTH = 128,
47 OP_ENTITYREF = 256;
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
73 * with column name as the key and attributes as the value
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 public $_options = array();
92
93 /**
94 * By default most reports hide contact id.
95 * Setting this to true makes it available
96 */
97 protected $_exposeContactID = TRUE;
98
99 /**
100 * Set of statistic fields
101 *
102 * @var array
103 */
104 protected $_statFields = array();
105
106 /**
107 * Set of statistics data
108 *
109 * @var array
110 */
111 protected $_statistics = array();
112
113 /**
114 * List of fields not to be repeated during display
115 *
116 * @var array
117 */
118 protected $_noRepeats = array();
119
120 /**
121 * List of fields not to be displayed
122 *
123 * @var array
124 */
125 protected $_noDisplay = array();
126
127 /**
128 * Object type that a custom group extends
129 *
130 * @var null
131 */
132 protected $_customGroupExtends = NULL;
133 protected $_customGroupExtendsJoin = array();
134 protected $_customGroupFilters = TRUE;
135 protected $_customGroupGroupBy = FALSE;
136 protected $_customGroupJoin = 'LEFT JOIN';
137
138 /**
139 * Build tags filter
140 */
141 protected $_tagFilter = FALSE;
142
143 /**
144 * specify entity table for tags filter
145 */
146 protected $_tagFilterTable = 'civicrm_contact';
147
148 /**
149 * Build groups filter.
150 *
151 * @var bool
152 */
153 protected $_groupFilter = FALSE;
154
155 /**
156 * Has the report been optimised for group filtering.
157 *
158 * The functionality for group filtering has been improved but not
159 * all reports have been adjusted to take care of it.
160 *
161 * This property exists to highlight the reports which are still using the
162 * slow method & allow group filtering to still work for them until they
163 * can be migrated.
164 *
165 * In order to protect extensions we have to default to TRUE - but I have
166 * separately marked every class with a groupFilter in the hope that will trigger
167 * people to fix them as they touch them.
168 *
169 * CRM-19170
170 *
171 * @var bool
172 */
173 protected $groupFilterNotOptimised = TRUE;
174
175 /**
176 * Navigation fields
177 *
178 * @var array
179 */
180 public $_navigation = array();
181
182 public $_drilldownReport = array();
183
184 /**
185 * Array of tabs to display on report.
186 *
187 * E.g we define the tab title, the tpl and the tab-specific part of the css or html link.
188 *
189 * $this->tabs['OrderBy'] = array(
190 * 'title' => ts('Sorting'),
191 * 'tpl' => 'OrderBy',
192 * 'div_label' => 'order-by',
193 * );
194 *
195 * @var array
196 */
197 protected $tabs = array();
198
199 /**
200 * Should we add paging.
201 *
202 * @var bool
203 */
204 protected $addPaging = TRUE;
205
206 protected $isForceGroupBy = FALSE;
207
208 protected $groupConcatTested = FALSE;
209
210 /**
211 * An attribute for checkbox/radio form field layout
212 *
213 * @var array
214 */
215 protected $_fourColumnAttribute = array(
216 '</td><td width="25%">',
217 '</td><td width="25%">',
218 '</td><td width="25%">',
219 '</tr><tr><td>',
220 );
221
222 protected $_force = 1;
223
224 protected $_params = NULL;
225 protected $_formValues = NULL;
226 protected $_instanceValues = NULL;
227
228 protected $_instanceForm = FALSE;
229 protected $_criteriaForm = FALSE;
230
231 protected $_instanceButtonName = NULL;
232 protected $_createNewButtonName = NULL;
233 protected $_printButtonName = NULL;
234 protected $_pdfButtonName = NULL;
235 protected $_csvButtonName = NULL;
236 protected $_groupButtonName = NULL;
237 protected $_chartButtonName = NULL;
238 protected $_csvSupported = TRUE;
239 protected $_add2groupSupported = TRUE;
240 protected $_groups = NULL;
241 protected $_grandFlag = FALSE;
242 protected $_rowsFound = NULL;
243 protected $_selectAliases = array();
244 protected $_rollup = NULL;
245
246 /**
247 * Table containing list of contact IDs within the group filter.
248 *
249 * @var string
250 */
251 protected $groupTempTable = '';
252
253 /**
254 * @var array
255 */
256 protected $_aliases = array();
257
258 /**
259 * @var string
260 */
261 protected $_where;
262
263 /**
264 * @var string
265 */
266 protected $_from;
267
268 /**
269 * SQL Limit clause
270 * @var string
271 */
272 protected $_limit = NULL;
273
274 /**
275 * This can be set to specify a limit to the number of rows
276 * Since it is currently envisaged as part of the api usage it is only being applied
277 * when $_output mode is not 'html' or 'group' so as not to have to interpret / mess with that part
278 * of the code (see limit() fn.
279 *
280 * @var integer
281 */
282 protected $_limitValue = NULL;
283
284 /**
285 * This can be set to specify row offset
286 * See notes on _limitValue
287 * @var integer
288 */
289 protected $_offsetValue = NULL;
290 /**
291 * @var null
292 */
293 protected $_sections = NULL;
294 protected $_autoIncludeIndexedFieldsAsOrderBys = 0;
295 protected $_absoluteUrl = FALSE;
296
297 /**
298 * Flag to indicate if result-set is to be stored in a class variable which could be retrieved using getResultSet() method.
299 *
300 * @var boolean
301 */
302 protected $_storeResultSet = FALSE;
303
304 /**
305 * When _storeResultSet Flag is set use this var to store result set in form of array
306 *
307 * @var boolean
308 */
309 protected $_resultSet = array();
310
311 /**
312 * To what frequency group-by a date column
313 *
314 * @var array
315 */
316 protected $_groupByDateFreq = array(
317 'MONTH' => 'Month',
318 'YEARWEEK' => 'Week',
319 'QUARTER' => 'Quarter',
320 'YEAR' => 'Year',
321 );
322
323 /**
324 * Variables to hold the acl inner join and where clause
325 */
326 protected $_aclFrom = NULL;
327 protected $_aclWhere = NULL;
328
329 /**
330 * Array of DAO tables having columns included in SELECT or ORDER BY clause.
331 *
332 * Where has also been added to this although perhaps the 'includes both' array should have a different name.
333 *
334 * @var array
335 */
336 protected $_selectedTables = array();
337
338 /**
339 * Array of DAO tables having columns included in WHERE or HAVING clause
340 *
341 * @var array
342 */
343 protected $filteredTables;
344
345 /**
346 * Output mode e.g 'print', 'csv', 'pdf'.
347 *
348 * @var string
349 */
350 protected $_outputMode;
351
352 /**
353 * Format of any chart in use.
354 *
355 * (it's unclear if this could be merged with outputMode at this stage)
356 *
357 * @var
358 */
359 protected $_format;
360
361 public $_having = NULL;
362 public $_select = NULL;
363 public $_selectClauses = array();
364 public $_columnHeaders = array();
365 public $_orderBy = NULL;
366 public $_orderByFields = array();
367 public $_orderByArray = array();
368 /**
369 * Array of clauses to group by.
370 *
371 * @var array
372 */
373 protected $_groupByArray = array();
374 public $_groupBy = NULL;
375 public $_whereClauses = array();
376 public $_havingClauses = array();
377
378 /**
379 * DashBoardRowCount Dashboard row count
380 * @var Integer
381 */
382 public $_dashBoardRowCount;
383
384 /**
385 * Is this being called without a form controller (ie. the report is being render outside the normal form
386 * - e.g the api is retrieving the rows
387 * @var boolean
388 */
389 public $noController = FALSE;
390
391 /**
392 * Variable to hold the currency alias
393 */
394 protected $_currencyColumn = NULL;
395
396 /**
397 * @var string
398 */
399 protected $_interval;
400
401 /**
402 * @var bool
403 */
404 protected $_sendmail;
405
406 /**
407 * @var int
408 */
409 protected $_chartId;
410
411 /**
412 * @var int
413 */
414 public $_section;
415
416 /**
417 * @var string Report description.
418 */
419 public $_description;
420
421 /**
422 * @var bool Is an address field selected.
423 * This was intended to determine if the address table should be joined in
424 * The isTableSelected function is now preferred for this purpose
425 */
426 protected $_addressField;
427
428 /**
429 * @var bool Is an email field selected.
430 * This was intended to determine if the email table should be joined in
431 * The isTableSelected function is now preferred for this purpose
432 */
433 protected $_emailField;
434
435 /**
436 * @var bool Is a phone field selected.
437 * This was intended to determine if the phone table should be joined in
438 * The isTableSelected function is now preferred for this purpose
439 */
440 protected $_phoneField;
441
442 /**
443 * @var bool Create new report instance? (or update existing) on save.
444 */
445 protected $_createNew;
446
447 /**
448 * When a grand total row has calculated the status we pop it off to here.
449 *
450 * This allows us to access it from the stats function and avoid recalculating.
451 */
452 protected $rollupRow = array();
453
454 /**
455 * @var string Database attributes - character set and collation
456 */
457 protected $_databaseAttributes = ' DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci';
458
459 /**
460 * SQL being run in this report.
461 *
462 * The sql in the report is stored in this variable in order to be displayed on the developer tab.
463 *
464 * @var string
465 */
466
467 protected $sql;
468
469 /**
470 * An instruction not to add a Group By.
471 *
472 * This is relevant where the group by might be otherwise added after the code that determines the group by array.
473 *
474 * e.g. where stat fields are being added but other settings cause it to not be desirable to add a group by
475 * such as in pivot charts when no row header is set
476 *
477 * @var bool
478 */
479 protected $noGroupBy = FALSE;
480
481 /**
482 * SQL being run in this report as an array.
483 *
484 * The sql in the report is stored in this variable in order to be returned to api & test calls.
485 *
486 * @var string
487 */
488
489 protected $sqlArray;
490
491 /**
492 * Tables created for the report that need removal afterwards.
493 *
494 * ['civicrm_temp_report_x' => ['temporary' => TRUE, 'name' => 'civicrm_temp_report_x']
495 * @var array
496 */
497 protected $temporaryTables = [];
498
499 /**
500 * Can this report use the sql mode ONLY_FULL_GROUP_BY.
501 * @var bool
502 */
503 public $optimisedForOnlyFullGroupBy = TRUE;
504 /**
505 * Class constructor.
506 */
507 public function __construct() {
508 parent::__construct();
509
510 $this->addClass('crm-report-form');
511
512 if ($this->_tagFilter) {
513 $this->buildTagFilter();
514 }
515 if ($this->_exposeContactID) {
516 if (array_key_exists('civicrm_contact', $this->_columns)) {
517 $this->_columns['civicrm_contact']['fields']['exposed_id'] = array(
518 'name' => 'id',
519 'title' => ts('Contact ID'),
520 'no_repeat' => TRUE,
521 );
522 }
523 }
524
525 if ($this->_groupFilter) {
526 $this->buildGroupFilter();
527 }
528
529 // Get all custom groups
530 $allGroups = CRM_Core_PseudoConstant::get('CRM_Core_DAO_CustomField', 'custom_group_id');
531
532 // Get the custom groupIds for which the user has VIEW permission
533 // If the user has 'access all custom data' permission, we'll leave $permCustomGroupIds empty
534 // and addCustomDataToColumns() will allow access to all custom groups.
535 $permCustomGroupIds = array();
536 if (!CRM_Core_Permission::check('access all custom data')) {
537 $permCustomGroupIds = CRM_ACL_API::group(CRM_Core_Permission::VIEW, NULL, 'civicrm_custom_group', $allGroups, NULL);
538 // do not allow custom data for reports if user doesn't have
539 // permission to access custom data.
540 if (!empty($this->_customGroupExtends) && empty($permCustomGroupIds)) {
541 $this->_customGroupExtends = array();
542 }
543 }
544
545 // merge custom data columns to _columns list, if any
546 $this->addCustomDataToColumns(TRUE, $permCustomGroupIds);
547
548 // add / modify display columns, filters ..etc
549 CRM_Utils_Hook::alterReportVar('columns', $this->_columns, $this);
550
551 //assign currencyColumn variable to tpl
552 $this->assign('currencyColumn', $this->_currencyColumn);
553 }
554
555 /**
556 * Shared pre-process function.
557 *
558 * If overriding preProcess function this should still be called.
559 *
560 * @throws \Exception
561 */
562 public function preProcessCommon() {
563 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean');
564
565 $this->_dashBoardRowCount = CRM_Utils_Request::retrieve('rowCount', 'Integer');
566
567 $this->_section = CRM_Utils_Request::retrieve('section', 'Integer');
568
569 $this->assign('section', $this->_section);
570 CRM_Core_Region::instance('page-header')->add(array(
571 'markup' => sprintf('<!-- Report class: [%s] -->', htmlentities(get_class($this))),
572 ));
573 if (!$this->noController) {
574 $this->setID($this->get('instanceId'));
575
576 if (!$this->_id) {
577 $this->setID(CRM_Report_Utils_Report::getInstanceID());
578 if (!$this->_id) {
579 $this->setID(CRM_Report_Utils_Report::getInstanceIDForPath());
580 }
581 }
582
583 // set qfkey so that pager picks it up and use it in the "Next > Last >>" links.
584 // FIXME: Note setting it in $_GET doesn't work, since pager generates link based on QUERY_STRING
585 $_SERVER['QUERY_STRING'] .= "&qfKey={$this->controller->_key}";
586 }
587
588 if ($this->_id) {
589 $this->assign('instanceId', $this->_id);
590 $params = array('id' => $this->_id);
591 $this->_instanceValues = array();
592 CRM_Core_DAO::commonRetrieve('CRM_Report_DAO_ReportInstance',
593 $params,
594 $this->_instanceValues
595 );
596 if (empty($this->_instanceValues)) {
597 CRM_Core_Error::fatal("Report could not be loaded.");
598 }
599 $this->_title = $this->_instanceValues['title'];
600 if (!empty($this->_instanceValues['permission']) &&
601 (!(CRM_Core_Permission::check($this->_instanceValues['permission']) ||
602 CRM_Core_Permission::check('administer Reports')
603 ))
604 ) {
605 CRM_Utils_System::permissionDenied();
606 CRM_Utils_System::civiExit();
607 }
608
609 $formValues = CRM_Utils_Array::value('form_values', $this->_instanceValues);
610 if ($formValues) {
611 $this->_formValues = unserialize($formValues);
612 }
613 else {
614 $this->_formValues = NULL;
615 }
616
617 $this->setOutputMode();
618
619 if ($this->_outputMode == 'copy') {
620 $this->_createNew = TRUE;
621 $this->_params = $this->_formValues;
622 $this->_params['view_mode'] = 'criteria';
623 $this->_params['title'] = $this->getTitle() . ts(' (copy created by %1 on %2)', array(
624 CRM_Core_Session::singleton()->getLoggedInContactDisplayName(),
625 CRM_Utils_Date::customFormat(date('Y-m-d H:i')),
626 ));
627 // Do not pass go. Do not collect another chance to re-run the same query.
628 CRM_Report_Form_Instance::postProcess($this);
629 }
630
631 // lets always do a force if reset is found in the url.
632 // Hey why not? see CRM-17225 for more about this. The use of reset to be force is historical for reasons stated
633 // in the comment line above these 2.
634 if (!empty($_REQUEST['reset'])
635 && !in_array(CRM_Utils_Request::retrieve('output', 'String'), array('save', 'criteria'))) {
636 $this->_force = 1;
637 }
638
639 // set the mode
640 $this->assign('mode', 'instance');
641 }
642 elseif (!$this->noController) {
643 list($optionValueID, $optionValue) = CRM_Report_Utils_Report::getValueIDFromUrl();
644 $instanceCount = CRM_Report_Utils_Report::getInstanceCount($optionValue);
645 if (($instanceCount > 0) && $optionValueID) {
646 $this->assign('instanceUrl',
647 CRM_Utils_System::url('civicrm/report/list',
648 "reset=1&ovid=$optionValueID"
649 )
650 );
651 }
652 if ($optionValueID) {
653 $this->_description = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $optionValueID, 'description');
654 }
655
656 // set the mode
657 $this->assign('mode', 'template');
658 }
659
660 // lets display the Report Settings section
661 $this->_instanceForm = $this->_force || $this->_id || (!empty($_POST));
662
663 // Do not display Report Settings section if administer Reports permission is absent OR
664 // if report instance is reserved and administer reserved reports absent
665 if (!CRM_Core_Permission::check('administer Reports') ||
666 ($this->_instanceValues['is_reserved'] &&
667 !CRM_Core_Permission::check('administer reserved reports'))
668 ) {
669 $this->_instanceForm = FALSE;
670 }
671
672 $this->assign('criteriaForm', FALSE);
673 // Display Report Criteria section if user has access Report Criteria OR administer Reports AND report instance is not reserved
674 if (CRM_Core_Permission::check('administer Reports') ||
675 CRM_Core_Permission::check('access Report Criteria')
676 ) {
677 if (!$this->_instanceValues['is_reserved'] ||
678 CRM_Core_Permission::check('administer reserved reports')
679 ) {
680 $this->assign('criteriaForm', TRUE);
681 $this->_criteriaForm = TRUE;
682 }
683 }
684
685 // Special permissions check for private instance if it's not the current contact instance
686 if ($this->_id &&
687 (CRM_Report_BAO_ReportInstance::reportIsPrivate($this->_id) &&
688 !CRM_Report_BAO_ReportInstance::contactIsOwner($this->_id))) {
689 if (!CRM_Core_Permission::check('access all private reports')) {
690 $this->_instanceForm = FALSE;
691 $this->assign('criteriaForm', FALSE);
692 }
693 }
694
695 $this->_instanceButtonName = $this->getButtonName('submit', 'save');
696 $this->_createNewButtonName = $this->getButtonName('submit', 'next');
697 $this->_groupButtonName = $this->getButtonName('submit', 'group');
698 $this->_chartButtonName = $this->getButtonName('submit', 'chart');
699 }
700
701 /**
702 * Add bread crumb.
703 */
704 public function addBreadCrumb() {
705 $breadCrumbs
706 = array(
707 array(
708 'title' => ts('Report Templates'),
709 'url' => CRM_Utils_System::url('civicrm/admin/report/template/list', 'reset=1'),
710 ),
711 );
712
713 CRM_Utils_System::appendBreadCrumb($breadCrumbs);
714 }
715
716 /**
717 * Pre process function.
718 *
719 * Called prior to build form.
720 */
721 public function preProcess() {
722 $this->preProcessCommon();
723
724 if (!$this->_id) {
725 $this->addBreadCrumb();
726 }
727
728 foreach ($this->_columns as $tableName => $table) {
729 $this->setTableAlias($table, $tableName);
730
731 $expFields = array();
732 // higher preference to bao object
733 $daoOrBaoName = CRM_Utils_Array::value('bao', $table, CRM_Utils_Array::value('dao', $table));
734
735 if ($daoOrBaoName) {
736 if (method_exists($daoOrBaoName, 'exportableFields')) {
737 $expFields = $daoOrBaoName::exportableFields();
738 }
739 else {
740 $expFields = $daoOrBaoName::export();
741 }
742 }
743
744 $doNotCopy = array('required', 'default');
745
746 $fieldGroups = array('fields', 'filters', 'group_bys', 'order_bys');
747 foreach ($fieldGroups as $fieldGrp) {
748 if (!empty($table[$fieldGrp]) && is_array($table[$fieldGrp])) {
749 foreach ($table[$fieldGrp] as $fieldName => $field) {
750 // $name is the field name used to reference the BAO/DAO export fields array
751 $name = isset($field['name']) ? $field['name'] : $fieldName;
752
753 // Sometimes the field name key in the BAO/DAO export fields array is
754 // different from the actual database field name.
755 // Unset $field['name'] so that actual database field name can be obtained
756 // from the BAO/DAO export fields array.
757 unset($field['name']);
758
759 if (array_key_exists($name, $expFields)) {
760 foreach ($doNotCopy as $dnc) {
761 // unset the values we don't want to be copied.
762 unset($expFields[$name][$dnc]);
763 }
764 if (empty($field)) {
765 $this->_columns[$tableName][$fieldGrp][$fieldName] = $expFields[$name];
766 }
767 else {
768 foreach ($expFields[$name] as $property => $val) {
769 if (!array_key_exists($property, $field)) {
770 $this->_columns[$tableName][$fieldGrp][$fieldName][$property] = $val;
771 }
772 }
773 }
774 }
775
776 // fill other vars
777 if (!empty($field['no_repeat'])) {
778 $this->_noRepeats[] = "{$tableName}_{$fieldName}";
779 }
780 if (!empty($field['no_display'])) {
781 $this->_noDisplay[] = "{$tableName}_{$fieldName}";
782 }
783
784 // set alias = table-name, unless already set
785 $alias = isset($field['alias']) ? $field['alias'] : (
786 isset($this->_columns[$tableName]['alias']) ? $this->_columns[$tableName]['alias'] : $tableName
787 );
788 $this->_columns[$tableName][$fieldGrp][$fieldName]['alias'] = $alias;
789
790 // set name = fieldName, unless already set
791 if (!isset($this->_columns[$tableName][$fieldGrp][$fieldName]['name'])) {
792 $this->_columns[$tableName][$fieldGrp][$fieldName]['name'] = $name;
793 }
794
795 if (!isset($this->_columns[$tableName][$fieldGrp][$fieldName]['table_name'])) {
796 $this->_columns[$tableName][$fieldGrp][$fieldName]['table_name'] = $tableName;
797 }
798
799 // set dbAlias = alias.name, unless already set
800 if (!isset($this->_columns[$tableName][$fieldGrp][$fieldName]['dbAlias'])) {
801 $this->_columns[$tableName][$fieldGrp][$fieldName]['dbAlias']
802 = $alias . '.' .
803 $this->_columns[$tableName][$fieldGrp][$fieldName]['name'];
804 }
805
806 // a few auto fills for filters
807 if ($fieldGrp == 'filters') {
808 // fill operator types
809 if (!array_key_exists('operatorType', $this->_columns[$tableName][$fieldGrp][$fieldName])) {
810 switch (CRM_Utils_Array::value('type', $this->_columns[$tableName][$fieldGrp][$fieldName])) {
811 case CRM_Utils_Type::T_MONEY:
812 case CRM_Utils_Type::T_FLOAT:
813 $this->_columns[$tableName][$fieldGrp][$fieldName]['operatorType'] = CRM_Report_Form::OP_FLOAT;
814 break;
815
816 case CRM_Utils_Type::T_INT:
817 $this->_columns[$tableName][$fieldGrp][$fieldName]['operatorType'] = CRM_Report_Form::OP_INT;
818 break;
819
820 case CRM_Utils_Type::T_DATE:
821 $this->_columns[$tableName][$fieldGrp][$fieldName]['operatorType'] = CRM_Report_Form::OP_DATE;
822 break;
823
824 case CRM_Utils_Type::T_BOOLEAN:
825 $this->_columns[$tableName][$fieldGrp][$fieldName]['operatorType'] = CRM_Report_Form::OP_SELECT;
826 if (!array_key_exists('options', $this->_columns[$tableName][$fieldGrp][$fieldName])) {
827 $this->_columns[$tableName][$fieldGrp][$fieldName]['options']
828 = array(
829 '' => ts('Any'),
830 '0' => ts('No'),
831 '1' => ts('Yes'),
832 );
833 }
834 break;
835
836 default:
837 if ($daoOrBaoName &&
838 array_key_exists('pseudoconstant', $this->_columns[$tableName][$fieldGrp][$fieldName])
839 ) {
840 // with multiple options operator-type is generally multi-select
841 $this->_columns[$tableName][$fieldGrp][$fieldName]['operatorType'] = CRM_Report_Form::OP_MULTISELECT;
842 if (!array_key_exists('options', $this->_columns[$tableName][$fieldGrp][$fieldName])) {
843 // fill options
844 $this->_columns[$tableName][$fieldGrp][$fieldName]['options'] = CRM_Core_PseudoConstant::get($daoOrBaoName, $fieldName);
845 }
846 }
847 break;
848 }
849 }
850 }
851 if (!isset($this->_columns[$tableName]['metadata'][$fieldName])) {
852 $this->_columns[$tableName]['metadata'][$fieldName] = $this->_columns[$tableName][$fieldGrp][$fieldName];
853 }
854 else {
855 $this->_columns[$tableName]['metadata'][$fieldName] = array_merge($this->_columns[$tableName][$fieldGrp][$fieldName], $this->_columns[$tableName]['metadata'][$fieldName]);
856 }
857 }
858 }
859 }
860
861 // copy filters to a separate handy variable
862 if (array_key_exists('filters', $table)) {
863 $this->_filters[$tableName] = $this->_columns[$tableName]['filters'];
864 }
865
866 if (array_key_exists('group_bys', $table)) {
867 $groupBys[$tableName] = $this->_columns[$tableName]['group_bys'];
868 }
869
870 if (array_key_exists('fields', $table)) {
871 $reportFields[$tableName] = $this->_columns[$tableName]['fields'];
872 }
873 }
874
875 if ($this->_force) {
876 $this->setDefaultValues(FALSE);
877 }
878
879 CRM_Report_Utils_Get::processFilter($this->_filters, $this->_defaults);
880 CRM_Report_Utils_Get::processGroupBy($groupBys, $this->_defaults);
881 CRM_Report_Utils_Get::processFields($reportFields, $this->_defaults);
882 CRM_Report_Utils_Get::processChart($this->_defaults);
883
884 if ($this->_force) {
885 $this->_formValues = $this->_defaults;
886 $this->postProcess();
887 }
888 }
889
890 /**
891 * Set default values.
892 *
893 * @param bool $freeze
894 *
895 * @return array
896 */
897 public function setDefaultValues($freeze = TRUE) {
898 $freezeGroup = array();
899
900 // FIXME: generalizing form field naming conventions would reduce
901 // Lots of lines below.
902 foreach ($this->_columns as $tableName => $table) {
903 if (array_key_exists('fields', $table)) {
904 foreach ($table['fields'] as $fieldName => $field) {
905 if (empty($field['no_display'])) {
906 if (!empty($field['required'])) {
907 // set default
908 $this->_defaults['fields'][$fieldName] = 1;
909
910 if ($freeze) {
911 // find element object, so that we could use quickform's freeze method
912 // for required elements
913 $obj = $this->getElementFromGroup("fields", $fieldName);
914 if ($obj) {
915 $freezeGroup[] = $obj;
916 }
917 }
918 }
919 elseif (isset($field['default'])) {
920 $this->_defaults['fields'][$fieldName] = $field['default'];
921 }
922 }
923 }
924 }
925
926 if (array_key_exists('group_bys', $table)) {
927 foreach ($table['group_bys'] as $fieldName => $field) {
928 if (isset($field['default'])) {
929 if (!empty($field['frequency'])) {
930 $this->_defaults['group_bys_freq'][$fieldName] = 'MONTH';
931 }
932 $this->_defaults['group_bys'][$fieldName] = $field['default'];
933 }
934 }
935 }
936 if (array_key_exists('filters', $table)) {
937 foreach ($table['filters'] as $fieldName => $field) {
938 if (isset($field['default'])) {
939 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE
940 ) {
941 if (is_array($field['default'])) {
942 $this->_defaults["{$fieldName}_from"] = CRM_Utils_Array::value('from', $field['default']);
943 $this->_defaults["{$fieldName}_to"] = CRM_Utils_Array::value('to', $field['default']);
944 $this->_defaults["{$fieldName}_relative"] = 0;
945 }
946 else {
947 $this->_defaults["{$fieldName}_relative"] = $field['default'];
948 }
949 }
950 else {
951 if ((CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_INT) && is_array($field['default'])) {
952 $this->_defaults["{$fieldName}_min"] = CRM_Utils_Array::value('min', $field['default']);
953 $this->_defaults["{$fieldName}_max"] = CRM_Utils_Array::value('max', $field['default']);
954 }
955 $this->_defaults["{$fieldName}_value"] = $field['default'];
956 }
957 }
958 //assign default value as "in" for multiselect
959 //operator, To freeze the select element
960 if (CRM_Utils_Array::value('operatorType', $field) ==
961 CRM_Report_Form::OP_MULTISELECT
962 ) {
963 $this->_defaults["{$fieldName}_op"] = 'in';
964 }
965 if (CRM_Utils_Array::value('operatorType', $field) ==
966 CRM_Report_Form::OP_ENTITYREF
967 ) {
968 $this->_defaults["{$fieldName}_op"] = 'in';
969 }
970 elseif (CRM_Utils_Array::value('operatorType', $field) ==
971 CRM_Report_Form::OP_MULTISELECT_SEPARATOR
972 ) {
973 $this->_defaults["{$fieldName}_op"] = 'mhas';
974 }
975 elseif ($op = CRM_Utils_Array::value('default_op', $field)) {
976 $this->_defaults["{$fieldName}_op"] = $op;
977 }
978 }
979 }
980
981 if (
982 empty($this->_formValues['order_bys']) &&
983 (array_key_exists('order_bys', $table) &&
984 is_array($table['order_bys']))
985 ) {
986 if (!array_key_exists('order_bys', $this->_defaults)) {
987 $this->_defaults['order_bys'] = array();
988 }
989 foreach ($table['order_bys'] as $fieldName => $field) {
990 if (!empty($field['default']) || !empty($field['default_order']) ||
991 CRM_Utils_Array::value('default_is_section', $field) ||
992 !empty($field['default_weight'])
993 ) {
994 $order_by = array(
995 'column' => $fieldName,
996 'order' => CRM_Utils_Array::value('default_order', $field, 'ASC'),
997 'section' => CRM_Utils_Array::value('default_is_section', $field, 0),
998 );
999
1000 if (!empty($field['default_weight'])) {
1001 $this->_defaults['order_bys'][(int) $field['default_weight']] = $order_by;
1002 }
1003 else {
1004 array_unshift($this->_defaults['order_bys'], $order_by);
1005 }
1006 }
1007 }
1008 }
1009
1010 foreach ($this->_options as $fieldName => $field) {
1011 if (isset($field['default'])) {
1012 $this->_defaults['options'][$fieldName] = $field['default'];
1013 }
1014 }
1015 }
1016
1017 if (!empty($this->_submitValues)) {
1018 $this->preProcessOrderBy($this->_submitValues);
1019 }
1020 else {
1021 $this->preProcessOrderBy($this->_defaults);
1022 }
1023
1024 // lets finish freezing task here itself
1025 if (!empty($freezeGroup)) {
1026 foreach ($freezeGroup as $elem) {
1027 $elem->freeze();
1028 }
1029 }
1030
1031 if ($this->_formValues) {
1032 $this->_defaults = array_merge($this->_defaults, $this->_formValues);
1033 }
1034
1035 if ($this->_instanceValues) {
1036 $this->_defaults = array_merge($this->_defaults, $this->_instanceValues);
1037 }
1038
1039 CRM_Report_Form_Instance::setDefaultValues($this, $this->_defaults);
1040
1041 return $this->_defaults;
1042 }
1043
1044 /**
1045 * Get element from group.
1046 *
1047 * @param string $group
1048 * @param string $grpFieldName
1049 *
1050 * @return bool
1051 */
1052 public function getElementFromGroup($group, $grpFieldName) {
1053 $eleObj = $this->getElement($group);
1054 foreach ($eleObj->_elements as $index => $obj) {
1055 if ($grpFieldName == $obj->_attributes['name']) {
1056 return $obj;
1057 }
1058 }
1059 return FALSE;
1060 }
1061
1062 /**
1063 * Setter for $_params.
1064 *
1065 * @param array $params
1066 */
1067 public function setParams($params) {
1068 $this->_params = $params;
1069 }
1070
1071 /**
1072 * Getter for $_params.
1073 *
1074 * @return void|array $params
1075 */
1076 public function getParams() {
1077 return $this->_params;
1078 }
1079
1080 /**
1081 * Setter for $_id.
1082 *
1083 * @param int $instanceID
1084 */
1085 public function setID($instanceID) {
1086 $this->_id = $instanceID;
1087 }
1088
1089 /**
1090 * Setter for $_force.
1091 *
1092 * @param bool $isForce
1093 */
1094 public function setForce($isForce) {
1095 $this->_force = $isForce;
1096 }
1097
1098 /**
1099 * Setter for $_limitValue.
1100 *
1101 * @param int $_limitValue
1102 */
1103 public function setLimitValue($_limitValue) {
1104 $this->_limitValue = $_limitValue;
1105 }
1106
1107 /**
1108 * Setter for $_offsetValue.
1109 *
1110 * @param int $_offsetValue
1111 */
1112 public function setOffsetValue($_offsetValue) {
1113 $this->_offsetValue = $_offsetValue;
1114 }
1115
1116 /**
1117 * Setter for $addPaging.
1118 *
1119 * @param bool $value
1120 */
1121 public function setAddPaging($value) {
1122 $this->addPaging = $value;
1123 }
1124
1125 /**
1126 * Getter for $_defaultValues.
1127 *
1128 * @return array
1129 */
1130 public function getDefaultValues() {
1131 return $this->_defaults;
1132 }
1133
1134 /**
1135 * Remove any temporary tables.
1136 */
1137 public function cleanUpTemporaryTables() {
1138 foreach ($this->temporaryTables as $temporaryTable) {
1139 CRM_Core_DAO::executeQuery('DROP ' . ($temporaryTable['temporary'] ? 'TEMPORARY' : '') . ' TABLE IF EXISTS ' . $temporaryTable['name']);
1140 }
1141 }
1142
1143 /**
1144 * Create a temporary table.
1145 *
1146 * This function creates a table AND adds the details to the developer tab & $this->>temporary tables.
1147 *
1148 * @param string $identifier
1149 * This is the key that will be used for the table in the temporaryTables property.
1150 * @param string $sql
1151 * Sql select statement or column description (the latter requires the columns flag)
1152 * @param bool $isColumns
1153 * Is the sql describing columns to create (rather than using a select query).
1154 * @param bool $isMemory
1155 * Create a memory table rather than a normal INNODB table.
1156 *
1157 * @return string
1158 */
1159 public function createTemporaryTable($identifier, $sql, $isColumns = FALSE, $isMemory = FALSE) {
1160 $tempTable = CRM_Utils_SQL_TempTable::build()->setUtf8();
1161 if ($isMemory) {
1162 $tempTable->setMemory();
1163 }
1164 if ($isColumns) {
1165 $tempTable->createWithColumns($sql);
1166 }
1167 else {
1168 $tempTable->createWithQuery($sql);
1169 }
1170 $name = $tempTable->getName();
1171 // Developers may force tables to be durable to assist in debugging so lets check.
1172 $isNotTrueTemporary = $tempTable->isDurable();
1173 $this->addToDeveloperTab($tempTable->getCreateSql());
1174 $this->temporaryTables[$identifier] = ['temporary' => !$isNotTrueTemporary, 'name' => $name];
1175 return $name;
1176 }
1177
1178 /**
1179 * Add columns to report.
1180 */
1181 public function addColumns() {
1182 $options = array();
1183 $colGroups = NULL;
1184 foreach ($this->_columns as $tableName => $table) {
1185 if (array_key_exists('fields', $table)) {
1186 foreach ($table['fields'] as $fieldName => $field) {
1187 $groupTitle = '';
1188 if (empty($field['no_display'])) {
1189 foreach (array('table', 'field') as $var) {
1190 if (!empty(${$var}['grouping'])) {
1191 if (!is_array(${$var}['grouping'])) {
1192 $tableName = ${$var}['grouping'];
1193 }
1194 else {
1195 $tableName = array_keys(${$var}['grouping']);
1196 $tableName = $tableName[0];
1197 $groupTitle = array_values(${$var}['grouping']);
1198 $groupTitle = $groupTitle[0];
1199 }
1200 }
1201 }
1202
1203 if (!$groupTitle && isset($table['group_title'])) {
1204 $groupTitle = $table['group_title'];
1205 // Having a group_title is secret code for being a custom group
1206 // which cryptically translates to needing an accordion.
1207 // here we make that explicit.
1208 $colGroups[$tableName]['use_accordian_for_field_selection'] = TRUE;
1209 }
1210
1211 $colGroups[$tableName]['fields'][$fieldName] = CRM_Utils_Array::value('title', $field);
1212 if ($groupTitle && empty($colGroups[$tableName]['group_title'])) {
1213 $colGroups[$tableName]['group_title'] = $groupTitle;
1214 }
1215 $options[$fieldName] = CRM_Utils_Array::value('title', $field);
1216 }
1217 }
1218 }
1219 }
1220
1221 $this->addCheckBox("fields", ts('Select Columns'), $options, NULL,
1222 NULL, NULL, NULL, $this->_fourColumnAttribute, TRUE
1223 );
1224 if (!empty($colGroups)) {
1225 $this->tabs['FieldSelection'] = array(
1226 'title' => ts('Columns'),
1227 'tpl' => 'FieldSelection',
1228 'div_label' => 'col-groups',
1229 );
1230
1231 // Note this assignment is only really required in buildForm. It is being 'over-called'
1232 // to reduce risk of being missed due to overridden functions.
1233 $this->assign('tabs', $this->tabs);
1234 }
1235
1236 $this->assign('colGroups', $colGroups);
1237 }
1238
1239 /**
1240 * Add filters to report.
1241 */
1242 public function addFilters() {
1243 $filters = $filterGroups = array();
1244 $count = 1;
1245
1246 foreach ($this->_filters as $table => $attributes) {
1247 if (isset($this->_columns[$table]['group_title'])) {
1248 // The presence of 'group_title' is secret code for 'is_a_custom_table'
1249 // which magically means to 'display in an accordian'
1250 // here we make this explicit.
1251 $filterGroups[$table] = array(
1252 'group_title' => $this->_columns[$table]['group_title'],
1253 'use_accordian_for_field_selection' => TRUE,
1254
1255 );
1256 }
1257 foreach ($attributes as $fieldName => $field) {
1258 // get ready with option value pair
1259 // @ 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
1260 // would be useful
1261 $operations = $this->getOperationPair(
1262 CRM_Utils_Array::value('operatorType', $field),
1263 $fieldName);
1264
1265 $filters[$table][$fieldName] = $field;
1266
1267 switch (CRM_Utils_Array::value('operatorType', $field)) {
1268 case CRM_Report_Form::OP_MONTH:
1269 if (!array_key_exists('options', $field) ||
1270 !is_array($field['options']) || empty($field['options'])
1271 ) {
1272 // If there's no option list for this filter, define one.
1273 $field['options'] = array(
1274 1 => ts('January'),
1275 2 => ts('February'),
1276 3 => ts('March'),
1277 4 => ts('April'),
1278 5 => ts('May'),
1279 6 => ts('June'),
1280 7 => ts('July'),
1281 8 => ts('August'),
1282 9 => ts('September'),
1283 10 => ts('October'),
1284 11 => ts('November'),
1285 12 => ts('December'),
1286 );
1287 // Add this option list to this column _columns. This is
1288 // required so that filter statistics show properly.
1289 $this->_columns[$table]['filters'][$fieldName]['options'] = $field['options'];
1290 }
1291 case CRM_Report_Form::OP_MULTISELECT:
1292 case CRM_Report_Form::OP_MULTISELECT_SEPARATOR:
1293 // assume a multi-select field
1294 if (!empty($field['options']) ||
1295 $fieldName == 'state_province_id' || $fieldName == 'county_id'
1296 ) {
1297 $element = $this->addElement('select', "{$fieldName}_op", ts('Operator:'), $operations);
1298 if (count($operations) <= 1) {
1299 $element->freeze();
1300 }
1301 if ($fieldName == 'state_province_id' ||
1302 $fieldName == 'county_id'
1303 ) {
1304 $this->addChainSelect($fieldName . '_value', array(
1305 'multiple' => TRUE,
1306 'label' => NULL,
1307 'class' => 'huge',
1308 ));
1309 }
1310 else {
1311 $this->addElement('select', "{$fieldName}_value", NULL, $field['options'], array(
1312 'style' => 'min-width:250px',
1313 'class' => 'crm-select2 huge',
1314 'multiple' => TRUE,
1315 'placeholder' => ts('- select -'),
1316 ));
1317 }
1318 }
1319 break;
1320
1321 case CRM_Report_Form::OP_SELECT:
1322 // assume a select field
1323 $this->addElement('select', "{$fieldName}_op", ts('Operator:'), $operations);
1324 if (!empty($field['options'])) {
1325 $this->addElement('select', "{$fieldName}_value", NULL, $field['options']);
1326 }
1327 break;
1328
1329 case CRM_Report_Form::OP_ENTITYREF:
1330 $this->addElement('select', "{$fieldName}_op", ts('Operator:'), $operations);
1331 $this->setEntityRefDefaults($field, $table);
1332 $this->addEntityRef("{$fieldName}_value", NULL, $field['attributes']);
1333 break;
1334
1335 case CRM_Report_Form::OP_DATE:
1336 // build datetime fields
1337 CRM_Core_Form_Date::buildDateRange($this, $fieldName, $count, '_from', '_to', ts('From:'), FALSE, $operations);
1338 $count++;
1339 break;
1340
1341 case CRM_Report_Form::OP_DATETIME:
1342 // build datetime fields
1343 CRM_Core_Form_Date::buildDateRange($this, $fieldName, $count, '_from', '_to', ts('From:'), FALSE, $operations, 'searchDate', TRUE);
1344 $count++;
1345 break;
1346
1347 case CRM_Report_Form::OP_INT:
1348 case CRM_Report_Form::OP_FLOAT:
1349 // and a min value input box
1350 $this->add('text', "{$fieldName}_min", ts('Min'));
1351 // and a max value input box
1352 $this->add('text', "{$fieldName}_max", ts('Max'));
1353 default:
1354 // default type is string
1355 $this->addElement('select', "{$fieldName}_op", ts('Operator:'), $operations,
1356 array('onchange' => "return showHideMaxMinVal( '$fieldName', this.value );")
1357 );
1358 // we need text box for value input
1359 $this->add('text', "{$fieldName}_value", NULL, array('class' => 'huge'));
1360 break;
1361 }
1362 }
1363 }
1364 if (!empty($filters)) {
1365 $this->tabs['Filters'] = array(
1366 'title' => ts('Filters'),
1367 'tpl' => 'Filters',
1368 'div_label' => 'set-filters',
1369 );
1370 }
1371 $this->assign('filters', $filters);
1372 $this->assign('filterGroups', $filterGroups);
1373 }
1374
1375 /**
1376 * Function to assign the tabs to the template in the correct order.
1377 *
1378 * We want the tabs to wind up in this order (if not overridden).
1379 *
1380 * - Field Selection
1381 * - Group Bys
1382 * - Order Bys
1383 * - Other Options
1384 * - Filters
1385 */
1386 protected function assignTabs() {
1387 $order = array(
1388 'FieldSelection',
1389 'GroupBy',
1390 'OrderBy',
1391 'ReportOptions',
1392 'Filters',
1393 );
1394 $order = array_intersect_key(array_fill_keys($order, 1), $this->tabs);
1395 $order = array_merge($order, $this->tabs);
1396 $this->assign('tabs', $order);
1397 }
1398
1399 /**
1400 * The intent is to add a tab for developers to view the sql.
1401 *
1402 * Currently using dpm.
1403 *
1404 * @param string $sql
1405 */
1406 public function addToDeveloperTab($sql) {
1407 if (!CRM_Core_Permission::check('view report sql')) {
1408 return;
1409 }
1410 $ignored_output_modes = array('pdf', 'csv', 'print');
1411 if (in_array($this->_outputMode, $ignored_output_modes)) {
1412 return;
1413 }
1414 $this->tabs['Developer'] = array(
1415 'title' => ts('Developer'),
1416 'tpl' => 'Developer',
1417 'div_label' => 'set-developer',
1418 );
1419
1420 $this->assignTabs();
1421 $this->sqlArray[] = $sql;
1422 foreach ($this->sqlArray as $sql) {
1423 foreach (array('LEFT JOIN') as $term) {
1424 $sql = str_replace($term, '<br> ' . $term, $sql);
1425 }
1426 foreach (array('FROM', 'WHERE', 'GROUP BY', 'ORDER BY', 'LIMIT', ';') as $term) {
1427 $sql = str_replace($term, '<br><br>' . $term, $sql);
1428 }
1429 $this->sqlFormattedArray[] = $sql;
1430 $this->assign('sql', implode(';<br><br><br><br>', $this->sqlFormattedArray));
1431 }
1432 $this->assign('sqlModes', $sqlModes = CRM_Utils_SQL::getSqlModes());
1433
1434 }
1435
1436 /**
1437 * Add options defined in $this->_options to the report.
1438 */
1439 public function addOptions() {
1440 if (!empty($this->_options)) {
1441 // FIXME: For now lets build all elements as checkboxes.
1442 // Once we clear with the format we can build elements based on type
1443
1444 foreach ($this->_options as $fieldName => $field) {
1445 $options = array();
1446
1447 if ($field['type'] == 'select') {
1448 $this->addElement('select', "{$fieldName}", $field['title'], $field['options']);
1449 }
1450 elseif ($field['type'] == 'checkbox') {
1451 $options[$field['title']] = $fieldName;
1452 $this->addCheckBox($fieldName, NULL,
1453 $options, NULL,
1454 NULL, NULL, NULL, $this->_fourColumnAttribute
1455 );
1456 }
1457 }
1458 }
1459 if (!empty($this->_options) &&
1460 (!$this->_id
1461 || ($this->_id && CRM_Report_BAO_ReportInstance::contactCanAdministerReport($this->_id)))
1462 ) {
1463 $this->tabs['ReportOptions'] = array(
1464 'title' => ts('Display Options'),
1465 'tpl' => 'ReportOptions',
1466 'div_label' => 'other-options',
1467 );
1468 }
1469 $this->assign('otherOptions', $this->_options);
1470 }
1471
1472 /**
1473 * Add chart options to the report.
1474 */
1475 public function addChartOptions() {
1476 if (!empty($this->_charts)) {
1477 $this->addElement('select', "charts", ts('Chart'), $this->_charts);
1478 $this->assign('charts', $this->_charts);
1479 $this->addElement('submit', $this->_chartButtonName, ts('View'));
1480 }
1481 }
1482
1483 /**
1484 * Add group by options to the report.
1485 */
1486 public function addGroupBys() {
1487 $options = $freqElements = array();
1488
1489 foreach ($this->_columns as $tableName => $table) {
1490 if (array_key_exists('group_bys', $table)) {
1491 foreach ($table['group_bys'] as $fieldName => $field) {
1492 if (!empty($field) && empty($field['no_display'])) {
1493 $options[$field['title']] = $fieldName;
1494 if (!empty($field['frequency'])) {
1495 $freqElements[$field['title']] = $fieldName;
1496 }
1497 }
1498 }
1499 }
1500 }
1501 $this->addCheckBox("group_bys", ts('Group by columns'), $options, NULL,
1502 NULL, NULL, NULL, $this->_fourColumnAttribute
1503 );
1504 $this->assign('groupByElements', $options);
1505 if (!empty($options)) {
1506 $this->tabs['GroupBy'] = array(
1507 'title' => ts('Grouping'),
1508 'tpl' => 'GroupBy',
1509 'div_label' => 'group-by-elements',
1510 );
1511 }
1512
1513 foreach ($freqElements as $name) {
1514 $this->addElement('select', "group_bys_freq[$name]",
1515 ts('Frequency'), $this->_groupByDateFreq
1516 );
1517 }
1518 }
1519
1520 /**
1521 * Add data for order by tab.
1522 */
1523 public function addOrderBys() {
1524 $options = array();
1525 foreach ($this->_columns as $tableName => $table) {
1526
1527 // Report developer may define any column to order by; include these as order-by options.
1528 if (array_key_exists('order_bys', $table)) {
1529 foreach ($table['order_bys'] as $fieldName => $field) {
1530 if (!empty($field)) {
1531 $options[$fieldName] = $field['title'];
1532 }
1533 }
1534 }
1535
1536 // Add searchable custom fields as order-by options, if so requested
1537 // (These are already indexed, so allowing to order on them is cheap.)
1538
1539 if ($this->_autoIncludeIndexedFieldsAsOrderBys &&
1540 array_key_exists('extends', $table) && !empty($table['extends'])
1541 ) {
1542 foreach ($table['fields'] as $fieldName => $field) {
1543 if (empty($field['no_display'])) {
1544 $options[$fieldName] = $field['title'];
1545 }
1546 }
1547 }
1548 }
1549
1550 asort($options);
1551
1552 $this->assign('orderByOptions', $options);
1553 if (!empty($options)) {
1554 $this->tabs['OrderBy'] = array(
1555 'title' => ts('Sorting'),
1556 'tpl' => 'OrderBy',
1557 'div_label' => 'order-by-elements',
1558 );
1559 }
1560
1561 if (!empty($options)) {
1562 $options = array(
1563 '-' => ' - none - ',
1564 ) + $options;
1565 for ($i = 1; $i <= 5; $i++) {
1566 $this->addElement('select', "order_bys[{$i}][column]", ts('Order by Column'), $options);
1567 $this->addElement('select', "order_bys[{$i}][order]", ts('Order by Order'), array(
1568 'ASC' => ts('Ascending'),
1569 'DESC' => ts('Descending'),
1570 ));
1571 $this->addElement('checkbox', "order_bys[{$i}][section]", ts('Order by Section'), FALSE, array('id' => "order_by_section_$i"));
1572 $this->addElement('checkbox', "order_bys[{$i}][pageBreak]", ts('Page Break'), FALSE, array('id' => "order_by_pagebreak_$i"));
1573 }
1574 }
1575 }
1576
1577 /**
1578 * This adds the tab referred to as Title and Format, rendered through Instance.tpl.
1579 *
1580 * @todo call this tab into the report template in the same way as OrderBy etc, ie
1581 * by adding a description of the tab to $this->tabs, causing the tab to be added in
1582 * Criteria.tpl.
1583 */
1584 public function buildInstanceAndButtons() {
1585 CRM_Report_Form_Instance::buildForm($this);
1586 $this->_actionButtonName = $this->getButtonName('submit');
1587 $this->addTaskMenu($this->getActions($this->_id));
1588
1589 $this->assign('instanceForm', $this->_instanceForm);
1590
1591 // CRM-16274 Determine if user has 'edit all contacts' or equivalent
1592 $permission = CRM_Core_Permission::getPermission();
1593 if ($this->_instanceForm && $permission == CRM_Core_Permission::EDIT &&
1594 $this->_add2groupSupported
1595 ) {
1596 $this->addElement('select', 'groups', ts('Group'),
1597 array('' => ts('Add Contacts to Group')) +
1598 CRM_Core_PseudoConstant::nestedGroup(),
1599 array('class' => 'crm-select2 crm-action-menu fa-plus huge')
1600 );
1601 $this->assign('group', TRUE);
1602 }
1603
1604 $this->addElement('submit', $this->_groupButtonName, '', array('style' => 'display: none;'));
1605
1606 $this->addChartOptions();
1607 $showResultsLabel = $this->getResultsLabel();
1608 $this->addButtons(array(
1609 array(
1610 'type' => 'submit',
1611 'name' => $showResultsLabel,
1612 'isDefault' => TRUE,
1613 ),
1614 )
1615 );
1616 }
1617
1618 /**
1619 * Has this form been submitted already?
1620 *
1621 * @return bool
1622 */
1623 public function resultsDisplayed() {
1624 $buttonName = $this->controller->getButtonName();
1625 return ($buttonName || $this->_outputMode);
1626 }
1627
1628 /**
1629 * Get the actions for this report instance.
1630 *
1631 * @param int $instanceId
1632 *
1633 * @return array
1634 */
1635 protected function getActions($instanceId) {
1636 $actions = CRM_Report_BAO_ReportInstance::getActionMetadata();
1637 if (empty($instanceId)) {
1638 $actions['report_instance.save'] = array(
1639 'title' => ts('Create Report'),
1640 'data' => array(
1641 'is_confirm' => TRUE,
1642 'confirm_title' => ts('Create Report'),
1643 'confirm_refresh_fields' => json_encode(array(
1644 'title' => array('selector' => '.crm-report-instanceForm-form-block-title', 'prepend' => ''),
1645 'description' => array('selector' => '.crm-report-instanceForm-form-block-description', 'prepend' => ''),
1646 )),
1647 ),
1648 );
1649 unset($actions['report_instance.delete']);
1650 }
1651
1652 if (!$this->_csvSupported) {
1653 unset($actions['report_instance.csv']);
1654 }
1655
1656 return $actions;
1657 }
1658
1659 /**
1660 * Main build form function.
1661 */
1662 public function buildQuickForm() {
1663 $this->addColumns();
1664
1665 $this->addFilters();
1666
1667 $this->addOptions();
1668
1669 $this->addGroupBys();
1670
1671 $this->addOrderBys();
1672
1673 $this->buildInstanceAndButtons();
1674
1675 // Add form rule for report.
1676 if (is_callable(array(
1677 $this,
1678 'formRule',
1679 ))) {
1680 $this->addFormRule(array(get_class($this), 'formRule'), $this);
1681 }
1682 $this->assignTabs();
1683 }
1684
1685 /**
1686 * A form rule function for custom data.
1687 *
1688 * The rule ensures that fields selected in group_by if any) should only be the ones
1689 * present in display/select fields criteria;
1690 * note: works if and only if any custom field selected in group_by.
1691 *
1692 * @param array $fields
1693 * @param array $ignoreFields
1694 *
1695 * @return array
1696 */
1697 public function customDataFormRule($fields, $ignoreFields = array()) {
1698 $errors = array();
1699 if (!empty($this->_customGroupExtends) && $this->_customGroupGroupBy &&
1700 !empty($fields['group_bys'])
1701 ) {
1702 foreach ($this->_columns as $tableName => $table) {
1703 if ((substr($tableName, 0, 13) == 'civicrm_value' ||
1704 substr($tableName, 0, 12) == 'custom_value') &&
1705 !empty($this->_columns[$tableName]['fields'])
1706 ) {
1707 foreach ($this->_columns[$tableName]['fields'] as $fieldName => $field) {
1708 if (array_key_exists($fieldName, $fields['group_bys']) &&
1709 !array_key_exists($fieldName, $fields['fields'])
1710 ) {
1711 $errors['fields'] = "Please make sure fields selected in 'Group by Columns' section are also selected in 'Display Columns' section.";
1712 }
1713 elseif (array_key_exists($fieldName, $fields['group_bys'])) {
1714 foreach ($fields['fields'] as $fld => $val) {
1715 if (!array_key_exists($fld, $fields['group_bys']) &&
1716 !in_array($fld, $ignoreFields)
1717 ) {
1718 $errors['fields'] = "Please ensure that fields selected in 'Display Columns' are also selected in 'Group by Columns' section.";
1719 }
1720 }
1721 }
1722 }
1723 }
1724 }
1725 }
1726 return $errors;
1727 }
1728
1729 /**
1730 * Get operators to display on form.
1731 *
1732 * Note: $fieldName param allows inheriting class to build operationPairs specific to a field.
1733 *
1734 * @param string $type
1735 * @param string $fieldName
1736 *
1737 * @return array
1738 */
1739 public function getOperationPair($type = "string", $fieldName = NULL) {
1740 // FIXME: At some point we should move these key-val pairs
1741 // to option_group and option_value table.
1742 switch ($type) {
1743 case CRM_Report_Form::OP_INT:
1744 case CRM_Report_Form::OP_FLOAT:
1745
1746 $result = array(
1747 'lte' => ts('Is less than or equal to'),
1748 'gte' => ts('Is greater than or equal to'),
1749 'bw' => ts('Is between'),
1750 'eq' => ts('Is equal to'),
1751 'lt' => ts('Is less than'),
1752 'gt' => ts('Is greater than'),
1753 'neq' => ts('Is not equal to'),
1754 'nbw' => ts('Is not between'),
1755 'nll' => ts('Is empty (Null)'),
1756 'nnll' => ts('Is not empty (Null)'),
1757 );
1758 return $result;
1759
1760 case CRM_Report_Form::OP_SELECT:
1761 $result = array(
1762 'eq' => ts('Is equal to'),
1763 );
1764 return $result;
1765
1766 case CRM_Report_Form::OP_MONTH:
1767 case CRM_Report_Form::OP_MULTISELECT:
1768 case CRM_Report_Form::OP_ENTITYREF:
1769
1770 $result = array(
1771 'in' => ts('Is one of'),
1772 'notin' => ts('Is not one of'),
1773 );
1774 return $result;
1775
1776 case CRM_Report_Form::OP_DATE:
1777
1778 $result = array(
1779 'nll' => ts('Is empty (Null)'),
1780 'nnll' => ts('Is not empty (Null)'),
1781 );
1782 return $result;
1783
1784 case CRM_Report_Form::OP_MULTISELECT_SEPARATOR:
1785 // use this operator for the values, concatenated with separator. For e.g if
1786 // multiple options for a column is stored as ^A{val1}^A{val2}^A
1787 $result = array(
1788 'mhas' => ts('Is one of'),
1789 'mnot' => ts('Is not one of'),
1790 );
1791 return $result;
1792
1793 default:
1794 // type is string
1795 $result = array(
1796 'has' => ts('Contains'),
1797 'sw' => ts('Starts with'),
1798 'ew' => ts('Ends with'),
1799 'nhas' => ts('Does not contain'),
1800 'eq' => ts('Is equal to'),
1801 'neq' => ts('Is not equal to'),
1802 'nll' => ts('Is empty (Null)'),
1803 'nnll' => ts('Is not empty (Null)'),
1804 );
1805 return $result;
1806 }
1807 }
1808
1809 /**
1810 * Build the tag filter field to display on the filters tab.
1811 */
1812 public function buildTagFilter() {
1813 $contactTags = CRM_Core_BAO_Tag::getTags($this->_tagFilterTable);
1814 if (!empty($contactTags)) {
1815 $this->_columns['civicrm_tag'] = array(
1816 'dao' => 'CRM_Core_DAO_Tag',
1817 'filters' => array(
1818 'tagid' => array(
1819 'name' => 'tag_id',
1820 'title' => ts('Tag'),
1821 'type' => CRM_Utils_Type::T_INT,
1822 'tag' => TRUE,
1823 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
1824 'options' => $contactTags,
1825 ),
1826 ),
1827 );
1828 }
1829 }
1830
1831 /**
1832 * Adds group filters to _columns (called from _Construct).
1833 */
1834 public function buildGroupFilter() {
1835 $this->_columns['civicrm_group']['filters'] = array(
1836 'gid' => array(
1837 'name' => 'group_id',
1838 'title' => ts('Group'),
1839 'type' => CRM_Utils_Type::T_INT,
1840 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
1841 'group' => TRUE,
1842 'options' => CRM_Core_PseudoConstant::nestedGroup(),
1843 ),
1844 );
1845 if (empty($this->_columns['civicrm_group']['dao'])) {
1846 $this->_columns['civicrm_group']['dao'] = 'CRM_Contact_DAO_GroupContact';
1847 }
1848 if (empty($this->_columns['civicrm_group']['alias'])) {
1849 $this->_columns['civicrm_group']['alias'] = 'cgroup';
1850 }
1851 }
1852
1853 /**
1854 * Get SQL operator from form text version.
1855 *
1856 * @param string $operator
1857 *
1858 * @return string
1859 */
1860 public function getSQLOperator($operator = "like") {
1861 switch ($operator) {
1862 case 'eq':
1863 return '=';
1864
1865 case 'lt':
1866 return '<';
1867
1868 case 'lte':
1869 return '<=';
1870
1871 case 'gt':
1872 return '>';
1873
1874 case 'gte':
1875 return '>=';
1876
1877 case 'ne':
1878 case 'neq':
1879 return '!=';
1880
1881 case 'nhas':
1882 return 'NOT LIKE';
1883
1884 case 'in':
1885 return 'IN';
1886
1887 case 'notin':
1888 return 'NOT IN';
1889
1890 case 'nll':
1891 return 'IS NULL';
1892
1893 case 'nnll':
1894 return 'IS NOT NULL';
1895
1896 default:
1897 // type is string
1898 return 'LIKE';
1899 }
1900 }
1901
1902 /**
1903 * Generate where clause.
1904 *
1905 * This can be overridden in reports for special treatment of a field
1906 *
1907 * @param array $field Field specifications
1908 * @param string $op Query operator (not an exact match to sql)
1909 * @param mixed $value
1910 * @param float $min
1911 * @param float $max
1912 *
1913 * @return null|string
1914 */
1915 public function whereClause(&$field, $op, $value, $min, $max) {
1916
1917 $type = CRM_Utils_Type::typeToString(CRM_Utils_Array::value('type', $field));
1918
1919 // CRM-18010: Ensure type of each report filters
1920 if (!$type) {
1921 trigger_error('Type is not defined for field ' . $field['name'], E_USER_WARNING);
1922 }
1923 $clause = NULL;
1924
1925 switch ($op) {
1926 case 'bw':
1927 case 'nbw':
1928 if (($min !== NULL && strlen($min) > 0) ||
1929 ($max !== NULL && strlen($max) > 0)
1930 ) {
1931 $clauses = array();
1932 if ($min) {
1933 $min = CRM_Utils_Type::escape($min, $type);
1934 if ($op == 'bw') {
1935 $clauses[] = "( {$field['dbAlias']} >= $min )";
1936 }
1937 else {
1938 $clauses[] = "( {$field['dbAlias']} < $min OR {$field['dbAlias']} IS NULL )";
1939 }
1940 }
1941 if ($max) {
1942 $max = CRM_Utils_Type::escape($max, $type);
1943 if ($op == 'bw') {
1944 $clauses[] = "( {$field['dbAlias']} <= $max )";
1945 }
1946 else {
1947 $clauses[] = "( {$field['dbAlias']} > $max )";
1948 }
1949 }
1950
1951 if (!empty($clauses)) {
1952 if ($op == 'bw') {
1953 $clause = implode(' AND ', $clauses);
1954 }
1955 else {
1956 $clause = '(' . implode('OR', $clauses) . ')';
1957 }
1958 }
1959 }
1960 break;
1961
1962 case 'has':
1963 case 'nhas':
1964 if ($value !== NULL && strlen($value) > 0) {
1965 $value = CRM_Utils_Type::escape($value, $type);
1966 if (strpos($value, '%') === FALSE) {
1967 $value = "'%{$value}%'";
1968 }
1969 else {
1970 $value = "'{$value}'";
1971 }
1972 $sqlOP = $this->getSQLOperator($op);
1973 $clause = "( {$field['dbAlias']} $sqlOP $value )";
1974 }
1975 break;
1976
1977 case 'in':
1978 case 'notin':
1979 if ((is_string($value) || is_numeric($value)) && strlen($value)) {
1980 $value = explode(',', $value);
1981 }
1982 if ($value !== NULL && is_array($value) && count($value) > 0) {
1983 $sqlOP = $this->getSQLOperator($op);
1984 if (CRM_Utils_Array::value('type', $field) ==
1985 CRM_Utils_Type::T_STRING
1986 ) {
1987 //cycle through selections and escape values
1988 foreach ($value as $key => $selection) {
1989 $value[$key] = CRM_Utils_Type::escape($selection, $type);
1990 }
1991 $clause
1992 = "( {$field['dbAlias']} $sqlOP ( '" . implode("' , '", $value) .
1993 "') )";
1994 }
1995 else {
1996 // for numerical values
1997 $clause = "{$field['dbAlias']} $sqlOP (" . implode(', ', $value) .
1998 ")";
1999 }
2000 if ($op == 'notin') {
2001 $clause = "( " . $clause . " OR {$field['dbAlias']} IS NULL )";
2002 }
2003 else {
2004 $clause = "( " . $clause . " )";
2005 }
2006 }
2007 break;
2008
2009 case 'mhas':
2010 case 'mnot':
2011 // multiple has or multiple not
2012 if ($value !== NULL && count($value) > 0) {
2013 $value = CRM_Utils_Type::escapeAll($value, $type);
2014 $operator = $op == 'mnot' ? 'NOT' : '';
2015 $regexp = "([[:cntrl:]]|^)" . implode('([[:cntrl:]]|$)|([[:cntrl:]]|^)', (array) $value) . "([[:cntrl:]]|$)";
2016 $clause = "{$field['dbAlias']} {$operator} REGEXP '{$regexp}'";
2017 }
2018 break;
2019
2020 case 'sw':
2021 case 'ew':
2022 if ($value !== NULL && strlen($value) > 0) {
2023 $value = CRM_Utils_Type::escape($value, $type);
2024 if (strpos($value, '%') === FALSE) {
2025 if ($op == 'sw') {
2026 $value = "'{$value}%'";
2027 }
2028 else {
2029 $value = "'%{$value}'";
2030 }
2031 }
2032 else {
2033 $value = "'{$value}'";
2034 }
2035 $sqlOP = $this->getSQLOperator($op);
2036 $clause = "( {$field['dbAlias']} $sqlOP $value )";
2037 }
2038 break;
2039
2040 case 'nll':
2041 case 'nnll':
2042 $sqlOP = $this->getSQLOperator($op);
2043 $clause = "( {$field['dbAlias']} $sqlOP )";
2044 break;
2045
2046 case 'eq':
2047 case 'neq':
2048 case 'ne':
2049 //CRM-18457: some custom field passes value in array format against binary operator
2050 if (is_array($value) && count($value)) {
2051 $value = $value[0];
2052 }
2053
2054 default:
2055 if ($value !== NULL && $value !== '') {
2056 if (isset($field['clause'])) {
2057 // FIXME: we not doing escape here. Better solution is to use two
2058 // different types - data-type and filter-type
2059 $clause = $field['clause'];
2060 }
2061 elseif (!is_array($value)) {
2062 $value = CRM_Utils_Type::escape($value, $type);
2063 $sqlOP = $this->getSQLOperator($op);
2064 if ($field['type'] == CRM_Utils_Type::T_STRING) {
2065 $value = "'{$value}'";
2066 }
2067 $clause = "( {$field['dbAlias']} $sqlOP $value )";
2068 }
2069 }
2070 break;
2071 }
2072
2073 //dev/core/544 Add report support for multiple contact subTypes
2074 if ($field['name'] == 'contact_sub_type' && $clause) {
2075 $clause = $this->whereSubtypeClause($field, $value, $op);
2076 }
2077 if (!empty($field['group']) && $clause) {
2078 $clause = $this->whereGroupClause($field, $value, $op);
2079 }
2080 elseif (!empty($field['tag']) && $clause) {
2081 // not using left join in query because if any contact
2082 // belongs to more than one tag, results duplicate
2083 // entries.
2084 $clause = $this->whereTagClause($field, $value, $op);
2085 }
2086 elseif (!empty($field['membership_org']) && $clause) {
2087 $clause = $this->whereMembershipOrgClause($value, $op);
2088 }
2089 elseif (!empty($field['membership_type']) && $clause) {
2090 $clause = $this->whereMembershipTypeClause($value, $op);
2091 }
2092 return $clause;
2093 }
2094
2095 /**
2096 * Get SQL where clause for contact subtypes
2097 * @param string $field
2098 * @param mixed $value
2099 * @param string $op SQL Operator
2100 *
2101 * @return string
2102 */
2103 public function whereSubtypeClause($field, $value, $op) {
2104 $clause = '( ';
2105 $subtypeFilters = count($value);
2106 for ($i = 0; $i < $subtypeFilters; $i++) {
2107 $clause .= "{$field['dbAlias']} LIKE '%$value[$i]%'";
2108 if ($i !== ($subtypeFilters - 1)) {
2109 $clause .= " OR ";
2110 }
2111 }
2112 $clause .= ' )';
2113 return $clause;
2114 }
2115
2116 /**
2117 * Get SQL where clause for a date field.
2118 *
2119 * @param string $fieldName
2120 * @param string $relative
2121 * @param string $from
2122 * @param string $to
2123 * @param string $type
2124 * @param string $fromTime
2125 * @param string $toTime
2126 *
2127 * @return null|string
2128 */
2129 public function dateClause(
2130 $fieldName,
2131 $relative, $from, $to, $type = NULL, $fromTime = NULL, $toTime = NULL
2132 ) {
2133 $clauses = array();
2134 if (in_array($relative, array_keys($this->getOperationPair(CRM_Report_Form::OP_DATE)))) {
2135 $sqlOP = $this->getSQLOperator($relative);
2136 return "( {$fieldName} {$sqlOP} )";
2137 }
2138
2139 list($from, $to) = $this->getFromTo($relative, $from, $to, $fromTime, $toTime);
2140
2141 if ($from) {
2142 $clauses[] = "( {$fieldName} >= $from )";
2143 }
2144
2145 if ($to) {
2146 $clauses[] = "( {$fieldName} <= {$to} )";
2147 }
2148
2149 if (!empty($clauses)) {
2150 return implode(' AND ', $clauses);
2151 }
2152
2153 return NULL;
2154 }
2155
2156 /**
2157 * Get values for from and to for date ranges.
2158 *
2159 * @deprecated
2160 *
2161 * @param bool $relative
2162 * @param string $from
2163 * @param string $to
2164 * @param string $fromTime
2165 * @param string $toTime
2166 *
2167 * @return array
2168 */
2169 public function getFromTo($relative, $from, $to, $fromTime = NULL, $toTime = NULL) {
2170 if (empty($toTime)) {
2171 // odd legacy behaviour to treat NULL as 'end of the day'
2172 // recommend updating reports to call CRM_Utils_Date::getFromTo
2173 //directly (default on the function is the actual default there).
2174 $toTime = '235959';
2175 }
2176 return CRM_Utils_Date::getFromTo($relative, $from, $to, $fromTime, $toTime);
2177 }
2178
2179 /**
2180 * Alter display of rows.
2181 *
2182 * Iterate through the rows retrieved via SQL and make changes for display purposes,
2183 * such as rendering contacts as links.
2184 *
2185 * @param array $rows
2186 * Rows generated by SQL, with an array for each row.
2187 */
2188 public function alterDisplay(&$rows) {
2189 }
2190
2191 /**
2192 * Alter the way in which custom data fields are displayed.
2193 *
2194 * @param array $rows
2195 */
2196 public function alterCustomDataDisplay(&$rows) {
2197 // custom code to alter rows having custom values
2198 if (empty($this->_customGroupExtends)) {
2199 return;
2200 }
2201
2202 $customFields = array();
2203 $customFieldIds = array();
2204 foreach ($this->_params['fields'] as $fieldAlias => $value) {
2205 if ($fieldId = CRM_Core_BAO_CustomField::getKeyID($fieldAlias)) {
2206 $customFieldIds[$fieldAlias] = $fieldId;
2207 }
2208 }
2209 if (empty($customFieldIds)) {
2210 return;
2211 }
2212
2213 // skip for type date and ContactReference since date format is already handled
2214 $query = "
2215 SELECT cg.table_name, cf.id
2216 FROM civicrm_custom_field cf
2217 INNER JOIN civicrm_custom_group cg ON cg.id = cf.custom_group_id
2218 WHERE cg.extends IN ('" . implode("','", $this->_customGroupExtends) . "') AND
2219 cg.is_active = 1 AND
2220 cf.is_active = 1 AND
2221 cf.is_searchable = 1 AND
2222 cf.data_type NOT IN ('ContactReference', 'Date') AND
2223 cf.id IN (" . implode(",", $customFieldIds) . ")";
2224
2225 $dao = CRM_Core_DAO::executeQuery($query);
2226 while ($dao->fetch()) {
2227 $customFields[$dao->table_name . '_custom_' . $dao->id] = $dao->id;
2228 }
2229 $dao->free();
2230
2231 $entryFound = FALSE;
2232 foreach ($rows as $rowNum => $row) {
2233 foreach ($row as $tableCol => $val) {
2234 if (array_key_exists($tableCol, $customFields)) {
2235 $rows[$rowNum][$tableCol] = CRM_Core_BAO_CustomField::displayValue($val, $customFields[$tableCol]);
2236 $entryFound = TRUE;
2237 }
2238 }
2239
2240 // skip looking further in rows, if first row itself doesn't
2241 // have the column we need
2242 if (!$entryFound) {
2243 break;
2244 }
2245 }
2246 }
2247
2248 /**
2249 * Remove duplicate rows.
2250 *
2251 * @param array $rows
2252 */
2253 public function removeDuplicates(&$rows) {
2254 if (empty($this->_noRepeats)) {
2255 return;
2256 }
2257 $checkList = array();
2258
2259 foreach ($rows as $key => $list) {
2260 foreach ($list as $colName => $colVal) {
2261 if (array_key_exists($colName, $checkList) &&
2262 $checkList[$colName] == $colVal
2263 ) {
2264 $rows[$key][$colName] = "";
2265 }
2266 if (in_array($colName, $this->_noRepeats)) {
2267 $checkList[$colName] = $colVal;
2268 }
2269 }
2270 }
2271 }
2272
2273 /**
2274 * Fix subtotal display.
2275 *
2276 * @param array $row
2277 * @param array $fields
2278 * @param bool $subtotal
2279 */
2280 public function fixSubTotalDisplay(&$row, $fields, $subtotal = TRUE) {
2281 foreach ($row as $colName => $colVal) {
2282 if (in_array($colName, $fields)) {
2283 }
2284 elseif (isset($this->_columnHeaders[$colName])) {
2285 if ($subtotal) {
2286 $row[$colName] = 'Subtotal';
2287 $subtotal = FALSE;
2288 }
2289 else {
2290 unset($row[$colName]);
2291 }
2292 }
2293 }
2294 }
2295
2296 /**
2297 * Calculate grant total.
2298 *
2299 * @param array $rows
2300 *
2301 * @return bool
2302 */
2303 public function grandTotal(&$rows) {
2304 if (!$this->_rollup || count($rows) == 1) {
2305 return FALSE;
2306 }
2307
2308 $this->moveSummaryColumnsToTheRightHandSide();
2309
2310 if ($this->_limit && count($rows) >= self::ROW_COUNT_LIMIT) {
2311 return FALSE;
2312 }
2313
2314 $this->rollupRow = array_pop($rows);
2315
2316 foreach ($this->_columnHeaders as $fld => $val) {
2317 if (!in_array($fld, $this->_statFields)) {
2318 if (!$this->_grandFlag) {
2319 $this->rollupRow[$fld] = ts('Grand Total');
2320 $this->_grandFlag = TRUE;
2321 }
2322 else {
2323 $this->rollupRow[$fld] = "";
2324 }
2325 }
2326 }
2327
2328 $this->assign('grandStat', $this->rollupRow);
2329 return TRUE;
2330 }
2331
2332 /**
2333 * Format display output.
2334 *
2335 * @param array $rows
2336 * @param bool $pager
2337 */
2338 public function formatDisplay(&$rows, $pager = TRUE) {
2339 // set pager based on if any limit was applied in the query.
2340 if ($pager) {
2341 $this->setPager();
2342 }
2343
2344 // allow building charts if any
2345 if (!empty($this->_params['charts']) && !empty($rows)) {
2346 $this->buildChart($rows);
2347 $this->assign('chartEnabled', TRUE);
2348 $this->_chartId = "{$this->_params['charts']}_" .
2349 ($this->_id ? $this->_id : substr(get_class($this), 16)) . '_' .
2350 session_id();
2351 $this->assign('chartId', $this->_chartId);
2352 }
2353
2354 // unset columns not to be displayed.
2355 foreach ($this->_columnHeaders as $key => $value) {
2356 if (!empty($value['no_display'])) {
2357 unset($this->_columnHeaders[$key]);
2358 }
2359 }
2360
2361 // unset columns not to be displayed.
2362 if (!empty($rows)) {
2363 foreach ($this->_noDisplay as $noDisplayField) {
2364 foreach ($rows as $rowNum => $row) {
2365 unset($this->_columnHeaders[$noDisplayField]);
2366 }
2367 }
2368 }
2369
2370 // build array of section totals
2371 $this->sectionTotals();
2372
2373 // process grand-total row
2374 $this->grandTotal($rows);
2375
2376 // Find alter display functions.
2377 $firstRow = reset($rows);
2378 if ($firstRow) {
2379 $selectedFields = array_keys($firstRow);
2380 $alterFunctions = $alterMap = $alterSpecs = array();
2381 foreach ($this->_columns as $tableName => $table) {
2382 if (array_key_exists('metadata', $table)) {
2383 foreach ($table['metadata'] as $field => $specs) {
2384 if (in_array($tableName . '_' . $field, $selectedFields)) {
2385 if (array_key_exists('alter_display', $specs)) {
2386 $alterFunctions[$tableName . '_' . $field] = $specs['alter_display'];
2387 $alterMap[$tableName . '_' . $field] = $field;
2388 $alterSpecs[$tableName . '_' . $field] = NULL;
2389 }
2390 // Add any alters that can be intuited from the field specs.
2391 // So far only boolean but a lot more could be.
2392 if (empty($alterSpecs[$tableName . '_' . $field]) && isset($specs['type']) && $specs['type'] == CRM_Utils_Type::T_BOOLEAN) {
2393 $alterFunctions[$tableName . '_' . $field] = 'alterBoolean';
2394 $alterMap[$tableName . '_' . $field] = $field;
2395 $alterSpecs[$tableName . '_' . $field] = NULL;
2396 }
2397 }
2398 }
2399 }
2400 }
2401
2402 // Run the alter display functions
2403 foreach ($rows as $index => & $row) {
2404 foreach ($row as $selectedField => $value) {
2405 if (array_key_exists($selectedField, $alterFunctions)) {
2406 $rows[$index][$selectedField] = $this->{$alterFunctions[$selectedField]}($value, $row, $selectedField, $alterMap[$selectedField], $alterSpecs[$selectedField]);
2407 }
2408 }
2409 }
2410 }
2411
2412 // use this method for formatting rows for display purpose.
2413 $this->alterDisplay($rows);
2414 CRM_Utils_Hook::alterReportVar('rows', $rows, $this);
2415
2416 // use this method for formatting custom rows for display purpose.
2417 $this->alterCustomDataDisplay($rows);
2418 }
2419
2420 /**
2421 * @param $value
2422 * @param $row
2423 * @param $selectedfield
2424 * @param $criteriaFieldName
2425 *
2426 * @return array
2427 */
2428 protected function alterStateProvinceID($value, &$row, $selectedfield, $criteriaFieldName) {
2429 $url = CRM_Utils_System::url(CRM_Utils_System::currentPath(), "reset=1&force=1&{$criteriaFieldName}_op=in&{$criteriaFieldName}_value={$value}", $this->_absoluteUrl);
2430 $row[$selectedfield . '_link'] = $url;
2431 $row[$selectedfield . '_hover'] = ts("%1 for this state.", array(
2432 1 => $value,
2433 ));
2434
2435 $states = CRM_Core_PseudoConstant::stateProvince($value, FALSE);
2436 if (!is_array($states)) {
2437 return $states;
2438 }
2439 }
2440
2441 /**
2442 * @param $value
2443 * @param $row
2444 * @param $selectedField
2445 * @param $criteriaFieldName
2446 *
2447 * @return array
2448 */
2449 protected function alterCountryID($value, &$row, $selectedField, $criteriaFieldName) {
2450 $url = CRM_Utils_System::url(CRM_Utils_System::currentPath(), "reset=1&force=1&{$criteriaFieldName}_op=in&{$criteriaFieldName}_value={$value}", $this->_absoluteUrl);
2451 $row[$selectedField . '_link'] = $url;
2452 $row[$selectedField . '_hover'] = ts("%1 for this country.", array(
2453 1 => $value,
2454 ));
2455 $countries = CRM_Core_PseudoConstant::country($value, FALSE);
2456 if (!is_array($countries)) {
2457 return $countries;
2458 }
2459 }
2460
2461 /**
2462 * @param $value
2463 * @param $row
2464 * @param $selectedfield
2465 * @param $criteriaFieldName
2466 *
2467 * @return array
2468 */
2469 protected function alterCountyID($value, &$row, $selectedfield, $criteriaFieldName) {
2470 $url = CRM_Utils_System::url(CRM_Utils_System::currentPath(), "reset=1&force=1&{$criteriaFieldName}_op=in&{$criteriaFieldName}_value={$value}", $this->_absoluteUrl);
2471 $row[$selectedfield . '_link'] = $url;
2472 $row[$selectedfield . '_hover'] = ts("%1 for this county.", array(
2473 1 => $value,
2474 ));
2475 $counties = CRM_Core_PseudoConstant::county($value, FALSE);
2476 if (!is_array($counties)) {
2477 return $counties;
2478 }
2479 }
2480
2481 /**
2482 * @param $value
2483 * @param $row
2484 * @param $selectedfield
2485 * @param $criteriaFieldName
2486 *
2487 * @return mixed
2488 */
2489 protected function alterLocationTypeID($value, &$row, $selectedfield, $criteriaFieldName) {
2490 return CRM_Core_PseudoConstant::getLabel('CRM_Core_DAO_Address', 'location_type_id', $value);
2491 }
2492
2493 /**
2494 * @param $value
2495 * @param $row
2496 * @param $fieldname
2497 *
2498 * @return mixed
2499 */
2500 protected function alterContactID($value, &$row, $fieldname) {
2501 $nameField = substr($fieldname, 0, -2) . 'name';
2502 static $first = TRUE;
2503 static $viewContactList = FALSE;
2504 if ($first) {
2505 $viewContactList = CRM_Core_Permission::check('access CiviCRM');
2506 $first = FALSE;
2507 }
2508 if (!$viewContactList) {
2509 return $value;
2510 }
2511 if (array_key_exists($nameField, $row)) {
2512 $row[$nameField . '_link'] = CRM_Utils_System::url("civicrm/contact/view", 'reset=1&cid=' . $value, $this->_absoluteUrl);
2513 }
2514 else {
2515 $row[$fieldname . '_link'] = CRM_Utils_System::url("civicrm/contact/view", 'reset=1&cid=' . $value, $this->_absoluteUrl);
2516 }
2517 return $value;
2518 }
2519
2520 /**
2521 * @param $value
2522 *
2523 * @return mixed
2524 */
2525 protected function alterBoolean($value) {
2526 $options = array(0 => '', 1 => ts('Yes'));
2527 if (isset($options[$value])) {
2528 return $options[$value];
2529 }
2530 return $value;
2531 }
2532
2533 /**
2534 * Build chart.
2535 *
2536 * @param array $rows
2537 */
2538 public function buildChart(&$rows) {
2539 // override this method for building charts.
2540 }
2541
2542 // select() method below has been added recently (v3.3), and many of the report templates might
2543 // still be having their own select() method. We should fix them as and when encountered and move
2544 // towards generalizing the select() method below.
2545
2546 /**
2547 * Generate the SELECT clause and set class variable $_select.
2548 */
2549 public function select() {
2550 $select = $this->_selectAliases = array();
2551 $this->storeGroupByArray();
2552
2553 foreach ($this->_columns as $tableName => $table) {
2554 if (array_key_exists('fields', $table)) {
2555 foreach ($table['fields'] as $fieldName => $field) {
2556 if ($tableName == 'civicrm_address') {
2557 // deprecated, use $this->isTableSelected.
2558 $this->_addressField = TRUE;
2559 }
2560 if ($tableName == 'civicrm_email') {
2561 $this->_emailField = TRUE;
2562 }
2563 if ($tableName == 'civicrm_phone') {
2564 $this->_phoneField = TRUE;
2565 }
2566
2567 if (!empty($field['required']) ||
2568 !empty($this->_params['fields'][$fieldName])
2569 ) {
2570
2571 // 1. In many cases we want select clause to be built in slightly different way
2572 // for a particular field of a particular type.
2573 // 2. This method when used should receive params by reference and modify $this->_columnHeaders
2574 // as needed.
2575 $selectClause = $this->selectClause($tableName, 'fields', $fieldName, $field);
2576 if ($selectClause) {
2577 $select[] = $selectClause;
2578 continue;
2579 }
2580
2581 // include statistics columns only if set
2582 if (!empty($field['statistics']) && !empty($this->_groupByArray)) {
2583 $select = $this->addStatisticsToSelect($field, $tableName, $fieldName, $select);
2584 }
2585 else {
2586
2587 $selectClause = $this->getSelectClauseWithGroupConcatIfNotGroupedBy($tableName, $fieldName, $field);
2588 if ($selectClause) {
2589 $select[] = $selectClause;
2590 }
2591 else {
2592 $select = $this->addBasicFieldToSelect($tableName, $fieldName, $field, $select);
2593 }
2594 }
2595 }
2596 }
2597 }
2598
2599 // select for group bys
2600 if (array_key_exists('group_bys', $table)) {
2601 foreach ($table['group_bys'] as $fieldName => $field) {
2602
2603 if ($tableName == 'civicrm_address') {
2604 $this->_addressField = TRUE;
2605 }
2606 if ($tableName == 'civicrm_email') {
2607 $this->_emailField = TRUE;
2608 }
2609 if ($tableName == 'civicrm_phone') {
2610 $this->_phoneField = TRUE;
2611 }
2612 // 1. In many cases we want select clause to be built in slightly different way
2613 // for a particular field of a particular type.
2614 // 2. This method when used should receive params by reference and modify $this->_columnHeaders
2615 // as needed.
2616 $selectClause = $this->selectClause($tableName, 'group_bys', $fieldName, $field);
2617 if ($selectClause) {
2618 $select[] = $selectClause;
2619 continue;
2620 }
2621
2622 if (!empty($this->_params['group_bys']) &&
2623 !empty($this->_params['group_bys'][$fieldName]) &&
2624 !empty($this->_params['group_bys_freq'])
2625 ) {
2626 switch (CRM_Utils_Array::value($fieldName, $this->_params['group_bys_freq'])) {
2627 case 'YEARWEEK':
2628 $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL WEEKDAY({$field['dbAlias']}) DAY) AS {$tableName}_{$fieldName}_start";
2629 $select[] = "YEARWEEK({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
2630 $select[] = "WEEKOFYEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
2631 $field['title'] = 'Week';
2632 break;
2633
2634 case 'YEAR':
2635 $select[] = "MAKEDATE(YEAR({$field['dbAlias']}), 1) AS {$tableName}_{$fieldName}_start";
2636 $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
2637 $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
2638 $field['title'] = 'Year';
2639 break;
2640
2641 case 'MONTH':
2642 $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL (DAYOFMONTH({$field['dbAlias']})-1) DAY) as {$tableName}_{$fieldName}_start";
2643 $select[] = "MONTH({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
2644 $select[] = "MONTHNAME({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
2645 $field['title'] = 'Month';
2646 break;
2647
2648 case 'QUARTER':
2649 $select[] = "STR_TO_DATE(CONCAT( 3 * QUARTER( {$field['dbAlias']} ) -2 , '/', '1', '/', YEAR( {$field['dbAlias']} ) ), '%m/%d/%Y') AS {$tableName}_{$fieldName}_start";
2650 $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
2651 $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
2652 $field['title'] = 'Quarter';
2653 break;
2654 }
2655 // for graphs and charts -
2656 if (!empty($this->_params['group_bys_freq'][$fieldName])) {
2657 $this->_interval = $field['title'];
2658 $this->_columnHeaders["{$tableName}_{$fieldName}_start"]['title']
2659 = $field['title'] . ' Beginning';
2660 $this->_columnHeaders["{$tableName}_{$fieldName}_start"]['type'] = $field['type'];
2661 $this->_columnHeaders["{$tableName}_{$fieldName}_start"]['group_by'] = $this->_params['group_bys_freq'][$fieldName];
2662
2663 // just to make sure these values are transferred to rows.
2664 // since we 'll need them for calculation purpose,
2665 // e.g making subtotals look nicer or graphs
2666 $this->_columnHeaders["{$tableName}_{$fieldName}_interval"] = array('no_display' => TRUE);
2667 $this->_columnHeaders["{$tableName}_{$fieldName}_subtotal"] = array('no_display' => TRUE);
2668 }
2669 }
2670 }
2671 }
2672 }
2673
2674 if (empty($select)) {
2675 // CRM-21412 Do not give fatal error on report when no fields selected
2676 $select = array(1);
2677 }
2678
2679 $this->_selectClauses = $select;
2680 $this->_select = "SELECT " . implode(', ', $select) . " ";
2681 }
2682
2683 /**
2684 * Build select clause for a single field.
2685 *
2686 * @param string $tableName
2687 * @param string $tableKey
2688 * @param string $fieldName
2689 * @param string $field
2690 *
2691 * @return bool
2692 */
2693 public function selectClause(&$tableName, $tableKey, &$fieldName, &$field) {
2694 if (!empty($field['pseudofield'])) {
2695 $alias = "{$tableName}_{$fieldName}";
2696 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
2697 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
2698 $this->_columnHeaders["{$tableName}_{$fieldName}"]['dbAlias'] = CRM_Utils_Array::value('dbAlias', $field);
2699 $this->_selectAliases[] = $alias;
2700 return ' 1 as ' . $alias;
2701 }
2702 return FALSE;
2703 }
2704
2705 /**
2706 * Build where clause.
2707 */
2708 public function where() {
2709 $this->storeWhereHavingClauseArray();
2710
2711 if (empty($this->_whereClauses)) {
2712 $this->_where = "WHERE ( 1 ) ";
2713 $this->_having = "";
2714 }
2715 else {
2716 $this->_where = "WHERE " . implode(' AND ', $this->_whereClauses);
2717 }
2718
2719 if ($this->_aclWhere) {
2720 $this->_where .= " AND {$this->_aclWhere} ";
2721 }
2722
2723 if (!empty($this->_havingClauses)) {
2724 // use this clause to construct group by clause.
2725 $this->_having = "HAVING " . implode(' AND ', $this->_havingClauses);
2726 }
2727 }
2728
2729 /**
2730 * Store Where clauses into an array.
2731 *
2732 * Breaking out this step makes over-riding more flexible as the clauses can be used in constructing a
2733 * temp table that may not be part of the final where clause or added
2734 * in other functions
2735 */
2736 public function storeWhereHavingClauseArray() {
2737 foreach ($this->_columns as $tableName => $table) {
2738 if (array_key_exists('filters', $table)) {
2739 foreach ($table['filters'] as $fieldName => $field) {
2740 // respect pseudofield to filter spec so fields can be marked as
2741 // not to be handled here
2742 if (!empty($field['pseudofield'])) {
2743 continue;
2744 }
2745 $clause = $this->generateFilterClause($field, $fieldName);
2746
2747 if (!empty($clause)) {
2748 if (!empty($field['having'])) {
2749 $this->_havingClauses[] = $clause;
2750 }
2751 else {
2752 $this->_whereClauses[] = $clause;
2753 }
2754 }
2755 }
2756 }
2757 }
2758
2759 }
2760
2761 /**
2762 * Set output mode.
2763 */
2764 public function processReportMode() {
2765 $this->setOutputMode();
2766
2767 $this->_sendmail
2768 = CRM_Utils_Request::retrieve(
2769 'sendmail',
2770 'Boolean',
2771 CRM_Core_DAO::$_nullObject
2772 );
2773
2774 $this->_absoluteUrl = FALSE;
2775 $printOnly = FALSE;
2776 $this->assign('printOnly', FALSE);
2777
2778 if ($this->_outputMode == 'print' ||
2779 ($this->_sendmail && !$this->_outputMode)
2780 ) {
2781 $this->assign('printOnly', TRUE);
2782 $printOnly = TRUE;
2783 $this->addPaging = FALSE;
2784 $this->assign('outputMode', 'print');
2785 $this->_outputMode = 'print';
2786 if ($this->_sendmail) {
2787 $this->_absoluteUrl = TRUE;
2788 }
2789 }
2790 elseif ($this->_outputMode == 'pdf') {
2791 $printOnly = TRUE;
2792 $this->addPaging = FALSE;
2793 $this->_absoluteUrl = TRUE;
2794 }
2795 elseif ($this->_outputMode == 'csv') {
2796 $printOnly = TRUE;
2797 $this->_absoluteUrl = TRUE;
2798 $this->addPaging = FALSE;
2799 }
2800 elseif ($this->_outputMode == 'group') {
2801 $this->assign('outputMode', 'group');
2802 }
2803 elseif ($this->_outputMode == 'create_report' && $this->_criteriaForm) {
2804 $this->assign('outputMode', 'create_report');
2805 }
2806 elseif ($this->_outputMode == 'copy' && $this->_criteriaForm) {
2807 $this->_createNew = TRUE;
2808 }
2809
2810 $this->assign('outputMode', $this->_outputMode);
2811 $this->assign('printOnly', $printOnly);
2812 // Get today's date to include in printed reports
2813 if ($printOnly) {
2814 $reportDate = CRM_Utils_Date::customFormat(date('Y-m-d H:i'));
2815 $this->assign('reportDate', $reportDate);
2816 }
2817 }
2818
2819 /**
2820 * Post Processing function for Form.
2821 *
2822 * postProcessCommon should be used to set other variables from input as the api accesses that function.
2823 * This function is not accessed when the api calls the report.
2824 */
2825 public function beginPostProcess() {
2826 $this->setParams($this->controller->exportValues($this->_name));
2827 if (empty($this->_params) &&
2828 $this->_force
2829 ) {
2830 $this->setParams($this->_formValues);
2831 }
2832
2833 // hack to fix params when submitted from dashboard, CRM-8532
2834 // fields array is missing because form building etc is skipped
2835 // in dashboard mode for report
2836 //@todo - this could be done in the dashboard no we have a setter
2837 if (empty($this->_params['fields']) && !$this->_noFields) {
2838 $this->setParams($this->_formValues);
2839 }
2840
2841 $this->processReportMode();
2842
2843 if ($this->_outputMode == 'save' || $this->_outputMode == 'copy') {
2844 $this->_createNew = ($this->_outputMode == 'copy');
2845 CRM_Report_Form_Instance::postProcess($this);
2846 }
2847 if ($this->_outputMode == 'delete') {
2848 CRM_Report_BAO_ReportInstance::doFormDelete($this->_id, 'civicrm/report/list?reset=1', 'civicrm/report/list?reset=1');
2849 }
2850
2851 $this->_formValues = $this->_params;
2852
2853 $this->beginPostProcessCommon();
2854 }
2855
2856 /**
2857 * BeginPostProcess function run in both report mode and non-report mode (api).
2858 */
2859 public function beginPostProcessCommon() {
2860 }
2861
2862 /**
2863 * Build the report query.
2864 *
2865 * @param bool $applyLimit
2866 *
2867 * @return string
2868 */
2869 public function buildQuery($applyLimit = TRUE) {
2870 $this->buildGroupTempTable();
2871 $this->select();
2872 $this->from();
2873 $this->customDataFrom();
2874 $this->buildPermissionClause();
2875 $this->where();
2876 $this->groupBy();
2877 $this->orderBy();
2878
2879 foreach ($this->unselectedOrderByColumns() as $alias => $field) {
2880 $clause = $this->getSelectClauseWithGroupConcatIfNotGroupedBy($field['table_name'], $field['name'], $field);
2881 if (!$clause) {
2882 $clause = "{$field['dbAlias']} as {$alias}";
2883 }
2884 $this->_select .= ", $clause ";
2885 }
2886
2887 if ($applyLimit && empty($this->_params['charts'])) {
2888 $this->limit();
2889 }
2890 CRM_Utils_Hook::alterReportVar('sql', $this, $this);
2891
2892 $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy} {$this->_limit}";
2893 $this->addToDeveloperTab($sql);
2894 return $sql;
2895 }
2896
2897 /**
2898 * Build group by clause.
2899 */
2900 public function groupBy() {
2901 $this->storeGroupByArray();
2902
2903 if (!empty($this->_groupByArray)) {
2904 if ($this->optimisedForOnlyFullGroupBy) {
2905 // We should probably deprecate this code path. What happens here is that
2906 // the group by is amended to reflect the select columns. This often breaks the
2907 // results. Retrofitting group strict group by onto existing report classes
2908 // went badly.
2909 $this->_groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, $this->_groupByArray);
2910 }
2911 else {
2912 $this->_groupBy = ' GROUP BY ' . implode($this->_groupByArray);
2913 }
2914 }
2915 }
2916
2917 /**
2918 * Build order by clause.
2919 */
2920 public function orderBy() {
2921 $this->_orderBy = "";
2922 $this->_sections = array();
2923 $this->storeOrderByArray();
2924 if (!empty($this->_orderByArray) && !$this->_rollup == 'WITH ROLLUP') {
2925 $this->_orderBy = "ORDER BY " . implode(', ', $this->_orderByArray);
2926 }
2927 $this->assign('sections', $this->_sections);
2928 }
2929
2930 /**
2931 * Extract order by fields and store as an array.
2932 *
2933 * In some cases other functions want to know which fields are selected for ordering by
2934 * Separating this into a separate function allows it to be called separately from constructing
2935 * the order by clause
2936 */
2937 public function storeOrderByArray() {
2938 $orderBys = array();
2939
2940 if (!empty($this->_params['order_bys']) &&
2941 is_array($this->_params['order_bys']) &&
2942 !empty($this->_params['order_bys'])
2943 ) {
2944
2945 // Process order_bys in user-specified order
2946 foreach ($this->_params['order_bys'] as $orderBy) {
2947 $orderByField = array();
2948 foreach ($this->_columns as $tableName => $table) {
2949 if (array_key_exists('order_bys', $table)) {
2950 // For DAO columns defined in $this->_columns
2951 $fields = $table['order_bys'];
2952 }
2953 elseif (array_key_exists('extends', $table)) {
2954 // For custom fields referenced in $this->_customGroupExtends
2955 $fields = CRM_Utils_Array::value('fields', $table, array());
2956 }
2957 else {
2958 continue;
2959 }
2960 if (!empty($fields) && is_array($fields)) {
2961 foreach ($fields as $fieldName => $field) {
2962 if ($fieldName == $orderBy['column']) {
2963 $orderByField = array_merge($field, $orderBy);
2964 $orderByField['tplField'] = "{$tableName}_{$fieldName}";
2965 break 2;
2966 }
2967 }
2968 }
2969 }
2970
2971 if (!empty($orderByField)) {
2972 $this->_orderByFields[$orderByField['tplField']] = $orderByField;
2973 if ($this->groupConcatTested) {
2974 $orderBys[$orderByField['tplField']] = "{$orderByField['tplField']} {$orderBy['order']}";
2975 }
2976 else {
2977 // Not sure when this is preferable to using tplField (which has
2978 // definitely been tested to work in cases then this does not.
2979 // in caution not switching unless report has been tested for
2980 // group concat functionality.
2981 $orderBys[$orderByField['tplField']] = "{$orderByField['dbAlias']} {$orderBy['order']}";
2982 }
2983
2984 // Record any section headers for assignment to the template
2985 if (!empty($orderBy['section'])) {
2986 $orderByField['pageBreak'] = CRM_Utils_Array::value('pageBreak', $orderBy);
2987 $this->_sections[$orderByField['tplField']] = $orderByField;
2988 }
2989 }
2990 }
2991 }
2992
2993 $this->_orderByArray = $orderBys;
2994
2995 $this->assign('sections', $this->_sections);
2996 }
2997
2998 /**
2999 * Determine unselected columns.
3000 *
3001 * @return array
3002 */
3003 public function unselectedOrderByColumns() {
3004 return array_diff_key($this->_orderByFields, $this->getSelectColumns());
3005 }
3006
3007 /**
3008 * Determine unselected columns.
3009 *
3010 * @return array
3011 */
3012 public function unselectedSectionColumns() {
3013 if (is_array($this->_sections)) {
3014 return array_diff_key($this->_sections, $this->getSelectColumns());
3015 }
3016 else {
3017 return array();
3018 }
3019 }
3020
3021 /**
3022 * Build output rows.
3023 *
3024 * @param string $sql
3025 * @param array $rows
3026 */
3027 public function buildRows($sql, &$rows) {
3028 if (!$this->optimisedForOnlyFullGroupBy) {
3029 CRM_Core_DAO::disableFullGroupByMode();
3030 }
3031 $dao = CRM_Core_DAO::executeQuery($sql);
3032 if (stristr($this->_select, 'SQL_CALC_FOUND_ROWS')) {
3033 $this->_rowsFound = CRM_Core_DAO::singleValueQuery('SELECT FOUND_ROWS()');
3034 }
3035 CRM_Core_DAO::reenableFullGroupByMode();
3036 if (!is_array($rows)) {
3037 $rows = array();
3038 }
3039
3040 // use this method to modify $this->_columnHeaders
3041 $this->modifyColumnHeaders();
3042
3043 $unselectedSectionColumns = $this->unselectedSectionColumns();
3044
3045 while ($dao->fetch()) {
3046 $row = array();
3047 foreach ($this->_columnHeaders as $key => $value) {
3048 if (property_exists($dao, $key)) {
3049 $row[$key] = $dao->$key;
3050 }
3051 }
3052
3053 // section headers not selected for display need to be added to row
3054 foreach ($unselectedSectionColumns as $key => $values) {
3055 if (property_exists($dao, $key)) {
3056 $row[$key] = $dao->$key;
3057 }
3058 }
3059
3060 $rows[] = $row;
3061 }
3062 }
3063
3064 /**
3065 * Calculate section totals.
3066 *
3067 * When "order by" fields are marked as sections, this assigns to the template
3068 * an array of total counts for each section. This data is used by the Smarty
3069 * plugin {sectionTotal}.
3070 */
3071 public function sectionTotals() {
3072
3073 // Reports using order_bys with sections must populate $this->_selectAliases in select() method.
3074 if (empty($this->_selectAliases)) {
3075 return;
3076 }
3077
3078 if (!empty($this->_sections)) {
3079 // build the query with no LIMIT clause
3080 $select = str_ireplace('SELECT SQL_CALC_FOUND_ROWS ', 'SELECT ', $this->_select);
3081 $sql = "{$select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy}";
3082
3083 // pull section aliases out of $this->_sections
3084 $sectionAliases = array_keys($this->_sections);
3085
3086 $ifnulls = array();
3087 foreach (array_merge($sectionAliases, $this->_selectAliases) as $alias) {
3088 $ifnulls[] = "ifnull($alias, '') as $alias";
3089 }
3090 $this->_select = "SELECT " . implode(", ", $ifnulls);
3091 $this->_select = CRM_Contact_BAO_Query::appendAnyValueToSelect($ifnulls, $sectionAliases);
3092
3093 // Group (un-limited) report by all aliases and get counts. This might
3094 // be done more efficiently when the contents of $sql are known, ie. by
3095 // overriding this method in the report class.
3096
3097 $query = $this->_select .
3098 ", count(*) as ct from ($sql) as subquery group by " .
3099 implode(", ", $sectionAliases);
3100
3101 // initialize array of total counts
3102 $totals = array();
3103 $dao = CRM_Core_DAO::executeQuery($query);
3104 while ($dao->fetch()) {
3105
3106 // let $this->_alterDisplay translate any integer ids to human-readable values.
3107 $rows[0] = $dao->toArray();
3108 $this->alterDisplay($rows);
3109 $row = $rows[0];
3110
3111 // add totals for all permutations of section values
3112 $values = array();
3113 $i = 1;
3114 $aliasCount = count($sectionAliases);
3115 foreach ($sectionAliases as $alias) {
3116 $values[] = $row[$alias];
3117 $key = implode(CRM_Core_DAO::VALUE_SEPARATOR, $values);
3118 if ($i == $aliasCount) {
3119 // the last alias is the lowest-level section header; use count as-is
3120 $totals[$key] = $dao->ct;
3121 }
3122 else {
3123 // other aliases are higher level; roll count into their total
3124 $totals[$key] += $dao->ct;
3125 }
3126 }
3127 }
3128 $this->assign('sectionTotals', $totals);
3129 }
3130 }
3131
3132 /**
3133 * Modify column headers.
3134 */
3135 public function modifyColumnHeaders() {
3136 // use this method to modify $this->_columnHeaders
3137 }
3138
3139 /**
3140 * Move totals columns to the right edge of the table.
3141 *
3142 * It seems like a more logical layout to have any totals columns on the far right regardless of
3143 * the location of the rest of their table.
3144 */
3145 public function moveSummaryColumnsToTheRightHandSide() {
3146 $statHeaders = (array_intersect_key($this->_columnHeaders, array_flip($this->_statFields)));
3147 $this->_columnHeaders = array_merge(array_diff_key($this->_columnHeaders, $statHeaders), $this->_columnHeaders, $statHeaders);
3148 }
3149
3150 /**
3151 * Assign rows to the template.
3152 *
3153 * @param array $rows
3154 */
3155 public function doTemplateAssignment(&$rows) {
3156 $this->assign_by_ref('columnHeaders', $this->_columnHeaders);
3157 $this->assign_by_ref('rows', $rows);
3158 $this->assign('statistics', $this->statistics($rows));
3159 }
3160
3161 /**
3162 * Build report statistics.
3163 *
3164 * Override this method to build your own statistics.
3165 *
3166 * @param array $rows
3167 *
3168 * @return array
3169 */
3170 public function statistics(&$rows) {
3171 $statistics = array();
3172
3173 $count = count($rows);
3174 // Why do we increment the count for rollup seems to artificially inflate the count.
3175 // It seems perhaps intentional to include the summary row in the count of results - although
3176 // this just seems odd.
3177 if ($this->_rollup && ($this->_rollup != '') && $this->_grandFlag) {
3178 $count++;
3179 }
3180
3181 $this->countStat($statistics, $count);
3182
3183 $this->groupByStat($statistics);
3184
3185 $this->filterStat($statistics);
3186
3187 return $statistics;
3188 }
3189
3190 /**
3191 * Add count statistics.
3192 *
3193 * @param array $statistics
3194 * @param int $count
3195 */
3196 public function countStat(&$statistics, $count) {
3197 $statistics['counts']['rowCount'] = array(
3198 'title' => ts('Row(s) Listed'),
3199 'value' => $count,
3200 );
3201
3202 if ($this->_rowsFound && ($this->_rowsFound > $count)) {
3203 $statistics['counts']['rowsFound'] = array(
3204 'title' => ts('Total Row(s)'),
3205 'value' => $this->_rowsFound,
3206 );
3207 }
3208 }
3209
3210 /**
3211 * Add group by statistics.
3212 *
3213 * @param array $statistics
3214 */
3215 public function groupByStat(&$statistics) {
3216 if (!empty($this->_params['group_bys']) &&
3217 is_array($this->_params['group_bys']) &&
3218 !empty($this->_params['group_bys'])
3219 ) {
3220 foreach ($this->_columns as $tableName => $table) {
3221 if (array_key_exists('group_bys', $table)) {
3222 foreach ($table['group_bys'] as $fieldName => $field) {
3223 if (!empty($this->_params['group_bys'][$fieldName])) {
3224 $combinations[] = $field['title'];
3225 }
3226 }
3227 }
3228 }
3229 $statistics['groups'][] = array(
3230 'title' => ts('Grouping(s)'),
3231 'value' => implode(' & ', $combinations),
3232 );
3233 }
3234 }
3235
3236 /**
3237 * Filter statistics.
3238 *
3239 * @param array $statistics
3240 */
3241 public function filterStat(&$statistics) {
3242 foreach ($this->_columns as $tableName => $table) {
3243 if (array_key_exists('filters', $table)) {
3244 foreach ($table['filters'] as $fieldName => $field) {
3245 if ((CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE ||
3246 CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_TIME) &&
3247 CRM_Utils_Array::value('operatorType', $field) !=
3248 CRM_Report_Form::OP_MONTH
3249 ) {
3250 list($from, $to)
3251 = $this->getFromTo(
3252 CRM_Utils_Array::value("{$fieldName}_relative", $this->_params),
3253 CRM_Utils_Array::value("{$fieldName}_from", $this->_params),
3254 CRM_Utils_Array::value("{$fieldName}_to", $this->_params),
3255 CRM_Utils_Array::value("{$fieldName}_from_time", $this->_params),
3256 CRM_Utils_Array::value("{$fieldName}_to_time", $this->_params)
3257 );
3258 $from_time_format = !empty($this->_params["{$fieldName}_from_time"]) ? 'h' : 'd';
3259 $from = CRM_Utils_Date::customFormat($from, NULL, array($from_time_format));
3260
3261 $to_time_format = !empty($this->_params["{$fieldName}_to_time"]) ? 'h' : 'd';
3262 $to = CRM_Utils_Date::customFormat($to, NULL, array($to_time_format));
3263
3264 if ($from || $to) {
3265 $statistics['filters'][] = array(
3266 'title' => $field['title'],
3267 'value' => ts("Between %1 and %2", array(1 => $from, 2 => $to)),
3268 );
3269 }
3270 elseif (in_array($rel = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params),
3271 array_keys($this->getOperationPair(CRM_Report_Form::OP_DATE))
3272 )) {
3273 $pair = $this->getOperationPair(CRM_Report_Form::OP_DATE);
3274 $statistics['filters'][] = array(
3275 'title' => $field['title'],
3276 'value' => $pair[$rel],
3277 );
3278 }
3279 }
3280 else {
3281 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
3282 $value = NULL;
3283 if ($op) {
3284 $pair = $this->getOperationPair(
3285 CRM_Utils_Array::value('operatorType', $field),
3286 $fieldName
3287 );
3288 $min = CRM_Utils_Array::value("{$fieldName}_min", $this->_params);
3289 $max = CRM_Utils_Array::value("{$fieldName}_max", $this->_params);
3290 $val = CRM_Utils_Array::value("{$fieldName}_value", $this->_params);
3291 if (in_array($op, array('bw', 'nbw')) && ($min || $max)) {
3292 $value = "{$pair[$op]} $min " . ts('and') . " $max";
3293 }
3294 elseif ($val && CRM_Utils_Array::value('operatorType', $field) & self::OP_ENTITYREF) {
3295 $this->setEntityRefDefaults($field, $tableName);
3296 $result = civicrm_api3($field['attributes']['entity'], 'getlist',
3297 array('id' => $val) +
3298 CRM_Utils_Array::value('api', $field['attributes'], array()));
3299 $values = array();
3300 foreach ($result['values'] as $v) {
3301 $values[] = $v['label'];
3302 }
3303 $value = "{$pair[$op]} " . implode(', ', $values);
3304 }
3305 elseif ($op == 'nll' || $op == 'nnll') {
3306 $value = $pair[$op];
3307 }
3308 elseif (is_array($val) && (!empty($val))) {
3309 $options = CRM_Utils_Array::value('options', $field, array());
3310 foreach ($val as $key => $valIds) {
3311 if (isset($options[$valIds])) {
3312 $val[$key] = $options[$valIds];
3313 }
3314 }
3315 $pair[$op] = (count($val) == 1) ? (($op == 'notin' || $op ==
3316 'mnot') ? ts('Is Not') : ts('Is')) : CRM_Utils_Array::value($op, $pair);
3317 $val = implode(', ', $val);
3318 $value = "{$pair[$op]} " . $val;
3319 }
3320 elseif (!is_array($val) && (!empty($val) || $val == '0') &&
3321 isset($field['options']) &&
3322 is_array($field['options']) && !empty($field['options'])
3323 ) {
3324 $value = CRM_Utils_Array::value($op, $pair) . " " .
3325 CRM_Utils_Array::value($val, $field['options'], $val);
3326 }
3327 elseif ($val) {
3328 $value = CRM_Utils_Array::value($op, $pair) . " " . $val;
3329 }
3330 }
3331 if ($value && empty($field['no_display'])) {
3332 $statistics['filters'][] = array(
3333 'title' => CRM_Utils_Array::value('title', $field),
3334 'value' => $value,
3335 );
3336 }
3337 }
3338 }
3339 }
3340 }
3341 }
3342
3343 /**
3344 * End post processing.
3345 *
3346 * @param array|null $rows
3347 */
3348 public function endPostProcess(&$rows = NULL) {
3349 $this->assign('report_class', get_class($this));
3350 if ($this->_storeResultSet) {
3351 $this->_resultSet = $rows;
3352 }
3353
3354 if ($this->_outputMode == 'print' ||
3355 $this->_outputMode == 'pdf' ||
3356 $this->_sendmail
3357 ) {
3358
3359 $content = $this->compileContent();
3360 $url = CRM_Utils_System::url("civicrm/report/instance/{$this->_id}",
3361 "reset=1", TRUE
3362 );
3363
3364 if ($this->_sendmail) {
3365 $config = CRM_Core_Config::singleton();
3366 $attachments = array();
3367
3368 if ($this->_outputMode == 'csv') {
3369 $content
3370 = $this->_formValues['report_header'] . '<p>' . ts('Report URL') .
3371 ": {$url}</p>" . '<p>' .
3372 ts('The report is attached as a CSV file.') . '</p>' .
3373 $this->_formValues['report_footer'];
3374
3375 $csvFullFilename = $config->templateCompileDir .
3376 CRM_Utils_File::makeFileName('CiviReport.csv');
3377 $csvContent = CRM_Report_Utils_Report::makeCsv($this, $rows);
3378 file_put_contents($csvFullFilename, $csvContent);
3379 $attachments[] = array(
3380 'fullPath' => $csvFullFilename,
3381 'mime_type' => 'text/csv',
3382 'cleanName' => 'CiviReport.csv',
3383 );
3384 }
3385 if ($this->_outputMode == 'pdf') {
3386 // generate PDF content
3387 $pdfFullFilename = $config->templateCompileDir .
3388 CRM_Utils_File::makeFileName('CiviReport.pdf');
3389 file_put_contents($pdfFullFilename,
3390 CRM_Utils_PDF_Utils::html2pdf($content, "CiviReport.pdf",
3391 TRUE, array('orientation' => 'landscape')
3392 )
3393 );
3394 // generate Email Content
3395 $content
3396 = $this->_formValues['report_header'] . '<p>' . ts('Report URL') .
3397 ": {$url}</p>" . '<p>' .
3398 ts('The report is attached as a PDF file.') . '</p>' .
3399 $this->_formValues['report_footer'];
3400
3401 $attachments[] = array(
3402 'fullPath' => $pdfFullFilename,
3403 'mime_type' => 'application/pdf',
3404 'cleanName' => 'CiviReport.pdf',
3405 );
3406 }
3407
3408 if (CRM_Report_Utils_Report::mailReport($content, $this->_id,
3409 $this->_outputMode, $attachments
3410 )
3411 ) {
3412 CRM_Core_Session::setStatus(ts("Report mail has been sent."), ts('Sent'), 'success');
3413 }
3414 else {
3415 CRM_Core_Session::setStatus(ts("Report mail could not be sent."), ts('Mail Error'), 'error');
3416 }
3417 return;
3418 }
3419 elseif ($this->_outputMode == 'print') {
3420 echo $content;
3421 }
3422 else {
3423 if ($chartType = CRM_Utils_Array::value('charts', $this->_params)) {
3424 $config = CRM_Core_Config::singleton();
3425 //get chart image name
3426 $chartImg = $this->_chartId . '.png';
3427 //get image url path
3428 $uploadUrl
3429 = str_replace('/persist/contribute/', '/persist/', $config->imageUploadURL) .
3430 'openFlashChart/';
3431 $uploadUrl .= $chartImg;
3432 //get image doc path to overwrite
3433 $uploadImg
3434 = str_replace('/persist/contribute/', '/persist/', $config->imageUploadDir) .
3435 'openFlashChart/' . $chartImg;
3436 //Load the image
3437 $chart = imagecreatefrompng($uploadUrl);
3438 //convert it into formatted png
3439 CRM_Utils_System::setHttpHeader('Content-type', 'image/png');
3440 //overwrite with same image
3441 imagepng($chart, $uploadImg);
3442 //delete the object
3443 imagedestroy($chart);
3444 }
3445 CRM_Utils_PDF_Utils::html2pdf($content, "CiviReport.pdf", FALSE, array('orientation' => 'landscape'));
3446 }
3447 CRM_Utils_System::civiExit();
3448 }
3449 elseif ($this->_outputMode == 'csv') {
3450 CRM_Report_Utils_Report::export2csv($this, $rows);
3451 }
3452 elseif ($this->_outputMode == 'group') {
3453 $group = $this->_params['groups'];
3454 $this->add2group($group);
3455 }
3456 }
3457
3458 /**
3459 * Set store result set indicator to TRUE.
3460 *
3461 * @todo explain what this does
3462 */
3463 public function storeResultSet() {
3464 $this->_storeResultSet = TRUE;
3465 }
3466
3467 /**
3468 * Get result set.
3469 *
3470 * @return bool
3471 */
3472 public function getResultSet() {
3473 return $this->_resultSet;
3474 }
3475
3476 /**
3477 * Get the sql used to generate the report.
3478 *
3479 * @return string
3480 */
3481 public function getReportSql() {
3482 return $this->sqlArray;
3483 }
3484
3485 /**
3486 * Use the form name to create the tpl file name.
3487 *
3488 * @return string
3489 */
3490 public function getTemplateFileName() {
3491 $defaultTpl = parent::getTemplateFileName();
3492 $template = CRM_Core_Smarty::singleton();
3493 if (!$template->template_exists($defaultTpl)) {
3494 $defaultTpl = 'CRM/Report/Form.tpl';
3495 }
3496 return $defaultTpl;
3497 }
3498
3499 /**
3500 * Compile the report content.
3501 *
3502 * Although this function is super-short it is useful to keep separate so it can be over-ridden by report classes.
3503 *
3504 * @return string
3505 */
3506 public function compileContent() {
3507 $templateFile = $this->getHookedTemplateFileName();
3508 return CRM_Utils_Array::value('report_header', $this->_formValues) .
3509 CRM_Core_Form::$_template->fetch($templateFile) .
3510 CRM_Utils_Array::value('report_footer', $this->_formValues);
3511 }
3512
3513
3514 /**
3515 * Post process function.
3516 */
3517 public function postProcess() {
3518 // get ready with post process params
3519 $this->beginPostProcess();
3520
3521 // build query
3522 $sql = $this->buildQuery();
3523
3524 // build array of result based on column headers. This method also allows
3525 // modifying column headers before using it to build result set i.e $rows.
3526 $rows = array();
3527 $this->buildRows($sql, $rows);
3528
3529 // format result set.
3530 $this->formatDisplay($rows);
3531
3532 // assign variables to templates
3533 $this->doTemplateAssignment($rows);
3534
3535 // do print / pdf / instance stuff if needed
3536 $this->endPostProcess($rows);
3537 }
3538
3539 /**
3540 * Set limit.
3541 *
3542 * @param int $rowCount
3543 *
3544 * @return array
3545 */
3546 public function limit($rowCount = self::ROW_COUNT_LIMIT) {
3547 // lets do the pager if in html mode
3548 $this->_limit = NULL;
3549
3550 // CRM-14115, over-ride row count if rowCount is specified in URL
3551 if ($this->_dashBoardRowCount) {
3552 $rowCount = $this->_dashBoardRowCount;
3553 }
3554 if ($this->addPaging) {
3555 $this->_select = preg_replace('/SELECT(\s+SQL_CALC_FOUND_ROWS)?\s+/i', 'SELECT SQL_CALC_FOUND_ROWS ', $this->_select);
3556
3557 $pageId = CRM_Utils_Request::retrieve('crmPID', 'Integer');
3558
3559 // @todo all http vars should be extracted in the preProcess
3560 // - not randomly in the class
3561 if (!$pageId && !empty($_POST)) {
3562 if (isset($_POST['PagerBottomButton']) && isset($_POST['crmPID_B'])) {
3563 $pageId = max((int) $_POST['crmPID_B'], 1);
3564 }
3565 elseif (isset($_POST['PagerTopButton']) && isset($_POST['crmPID'])) {
3566 $pageId = max((int) $_POST['crmPID'], 1);
3567 }
3568 unset($_POST['crmPID_B'], $_POST['crmPID']);
3569 }
3570
3571 $pageId = $pageId ? $pageId : 1;
3572 $this->set(CRM_Utils_Pager::PAGE_ID, $pageId);
3573 $offset = ($pageId - 1) * $rowCount;
3574
3575 $offset = CRM_Utils_Type::escape($offset, 'Int');
3576 $rowCount = CRM_Utils_Type::escape($rowCount, 'Int');
3577
3578 $this->_limit = " LIMIT $offset, $rowCount";
3579 return array($offset, $rowCount);
3580 }
3581 if ($this->_limitValue) {
3582 if ($this->_offsetValue) {
3583 $this->_limit = " LIMIT {$this->_offsetValue}, {$this->_limitValue} ";
3584 }
3585 else {
3586 $this->_limit = " LIMIT " . $this->_limitValue;
3587 }
3588 }
3589 }
3590
3591 /**
3592 * Set pager.
3593 *
3594 * @param int $rowCount
3595 */
3596 public function setPager($rowCount = self::ROW_COUNT_LIMIT) {
3597 // CRM-14115, over-ride row count if rowCount is specified in URL
3598 if ($this->_dashBoardRowCount) {
3599 $rowCount = $this->_dashBoardRowCount;
3600 }
3601
3602 if ($this->_limit && ($this->_limit != '')) {
3603 if (!$this->_rowsFound) {
3604 $sql = "SELECT FOUND_ROWS();";
3605 $this->_rowsFound = CRM_Core_DAO::singleValueQuery($sql);
3606 }
3607 $params = array(
3608 'total' => $this->_rowsFound,
3609 'rowCount' => $rowCount,
3610 'status' => ts('Records') . ' %%StatusMessage%%',
3611 'buttonBottom' => 'PagerBottomButton',
3612 'buttonTop' => 'PagerTopButton',
3613 );
3614 if (!empty($this->controller)) {
3615 // This happens when being called from the api Really we want the api to be able to
3616 // pass paging parameters, but at this stage just preventing test crashes.
3617 $params['pageID'] = $this->get(CRM_Utils_Pager::PAGE_ID);
3618 }
3619
3620 $pager = new CRM_Utils_Pager($params);
3621 $this->assign_by_ref('pager', $pager);
3622 $this->ajaxResponse['totalRows'] = $this->_rowsFound;
3623 }
3624 }
3625
3626 /**
3627 * Build a group filter with contempt for large data sets.
3628 *
3629 * This function has been retained as it takes time to migrate the reports over
3630 * to the new method which will not crash on large datasets.
3631 *
3632 * @deprecated
3633 *
3634 * @param string $field
3635 * @param mixed $value
3636 * @param string $op
3637 *
3638 * @return string
3639 */
3640 public function legacySlowGroupFilterClause($field, $value, $op) {
3641 $smartGroupQuery = "";
3642
3643 $group = new CRM_Contact_DAO_Group();
3644 $group->is_active = 1;
3645 $group->find();
3646 $smartGroups = array();
3647 while ($group->fetch()) {
3648 if (in_array($group->id, (array) $this->_params['gid_value']) &&
3649 $group->saved_search_id
3650 ) {
3651 $smartGroups[] = $group->id;
3652 }
3653 }
3654
3655 CRM_Contact_BAO_GroupContactCache::check($smartGroups);
3656
3657 $smartGroupQuery = '';
3658 if (!empty($smartGroups)) {
3659 $smartGroups = implode(',', $smartGroups);
3660 $smartGroupQuery = " UNION DISTINCT
3661 SELECT DISTINCT smartgroup_contact.contact_id
3662 FROM civicrm_group_contact_cache smartgroup_contact
3663 WHERE smartgroup_contact.group_id IN ({$smartGroups}) ";
3664 }
3665
3666 $sqlOp = $this->getSQLOperator($op);
3667 if (!is_array($value)) {
3668 $value = array($value);
3669 }
3670 //include child groups if any
3671 $value = array_merge($value, CRM_Contact_BAO_Group::getChildGroupIds($value));
3672
3673 $clause = "{$field['dbAlias']} IN (" . implode(', ', $value) . ")";
3674
3675 $contactAlias = $this->_aliases['civicrm_contact'];
3676 if (!empty($this->relationType) && $this->relationType == 'b_a') {
3677 $contactAlias = $this->_aliases['civicrm_contact_b'];
3678 }
3679 return " {$contactAlias}.id {$sqlOp} (
3680 SELECT DISTINCT {$this->_aliases['civicrm_group']}.contact_id
3681 FROM civicrm_group_contact {$this->_aliases['civicrm_group']}
3682 WHERE {$clause} AND {$this->_aliases['civicrm_group']}.status = 'Added'
3683 {$smartGroupQuery} ) ";
3684 }
3685
3686 /**
3687 * Build where clause for groups.
3688 *
3689 * @param string $field
3690 * @param mixed $value
3691 * @param string $op
3692 *
3693 * @return string
3694 */
3695 public function whereGroupClause($field, $value, $op) {
3696 if ($this->groupFilterNotOptimised) {
3697 return $this->legacySlowGroupFilterClause($field, $value, $op);
3698 }
3699 if ($op === 'notin') {
3700 return " group_temp_table.id IS NULL ";
3701 }
3702 // We will have used an inner join instead.
3703 return "1";
3704 }
3705
3706
3707 /**
3708 * Create a table of the contact ids included by the group filter.
3709 *
3710 * This function is called by both the api (tests) and the UI.
3711 */
3712 public function buildGroupTempTable() {
3713 if (!empty($this->groupTempTable) || empty($this->_params['gid_value']) || $this->groupFilterNotOptimised) {
3714 return;
3715 }
3716 $filteredGroups = (array) $this->_params['gid_value'];
3717
3718 $groups = civicrm_api3('Group', 'get', array(
3719 'is_active' => 1,
3720 'id' => array('IN' => $filteredGroups),
3721 'saved_search_id' => array('>' => 0),
3722 'return' => 'id',
3723 ));
3724 $smartGroups = array_keys($groups['values']);
3725
3726 $query = "
3727 SELECT DISTINCT group_contact.contact_id as id
3728 FROM civicrm_group_contact group_contact
3729 WHERE group_contact.group_id IN (" . implode(', ', $filteredGroups) . ")
3730 AND group_contact.status = 'Added' ";
3731
3732 if (!empty($smartGroups)) {
3733 CRM_Contact_BAO_GroupContactCache::check($smartGroups);
3734 $smartGroups = implode(',', $smartGroups);
3735 $query .= "
3736 UNION DISTINCT
3737 SELECT smartgroup_contact.contact_id as id
3738 FROM civicrm_group_contact_cache smartgroup_contact
3739 WHERE smartgroup_contact.group_id IN ({$smartGroups}) ";
3740 }
3741
3742 $this->groupTempTable = $this->createTemporaryTable('rptgrp', $query);
3743 CRM_Core_DAO::executeQuery("ALTER TABLE $this->groupTempTable ADD INDEX i_id(id)");
3744 }
3745
3746 /**
3747 * Execute query and add it to the developer tab.
3748 *
3749 * @param string $query
3750 * @param array $params
3751 *
3752 * @return \CRM_Core_DAO|object
3753 */
3754 protected function executeReportQuery($query, $params = array()) {
3755 $this->addToDeveloperTab($query);
3756 return CRM_Core_DAO::executeQuery($query, $params);
3757 }
3758
3759 /**
3760 * Build where clause for tags.
3761 *
3762 * @param string $field
3763 * @param mixed $value
3764 * @param string $op
3765 *
3766 * @return string
3767 */
3768 public function whereTagClause($field, $value, $op) {
3769 // not using left join in query because if any contact
3770 // belongs to more than one tag, results duplicate
3771 // entries.
3772 $sqlOp = $this->getSQLOperator($op);
3773 if (!is_array($value)) {
3774 $value = array($value);
3775 }
3776 $clause = "{$field['dbAlias']} IN (" . implode(', ', $value) . ")";
3777 $entity_table = $this->_tagFilterTable;
3778 return " {$this->_aliases[$entity_table]}.id {$sqlOp} (
3779 SELECT DISTINCT {$this->_aliases['civicrm_tag']}.entity_id
3780 FROM civicrm_entity_tag {$this->_aliases['civicrm_tag']}
3781 WHERE entity_table = '$entity_table' AND {$clause} ) ";
3782 }
3783
3784 /**
3785 * Generate membership organization clause.
3786 *
3787 * @param mixed $value
3788 * @param string $op SQL Operator
3789 *
3790 * @return string
3791 */
3792 public function whereMembershipOrgClause($value, $op) {
3793 $sqlOp = $this->getSQLOperator($op);
3794 if (!is_array($value)) {
3795 $value = array($value);
3796 }
3797
3798 $tmp_membership_org_sql_list = implode(', ', $value);
3799 return " {$this->_aliases['civicrm_contact']}.id {$sqlOp} (
3800 SELECT DISTINCT mem.contact_id
3801 FROM civicrm_membership mem
3802 LEFT JOIN civicrm_membership_status mem_status ON mem.status_id = mem_status.id
3803 LEFT JOIN civicrm_membership_type mt ON mem.membership_type_id = mt.id
3804 WHERE mt.member_of_contact_id IN (" .
3805 $tmp_membership_org_sql_list . ")
3806 AND mt.is_active = '1'
3807 AND mem_status.is_current_member = '1'
3808 AND mem_status.is_active = '1' ) ";
3809 }
3810
3811 /**
3812 * Generate Membership Type SQL Clause.
3813 *
3814 * @param mixed $value
3815 * @param string $op
3816 *
3817 * @return string
3818 * SQL query string
3819 */
3820 public function whereMembershipTypeClause($value, $op) {
3821 $sqlOp = $this->getSQLOperator($op);
3822 if (!is_array($value)) {
3823 $value = array($value);
3824 }
3825
3826 $tmp_membership_sql_list = implode(', ', $value);
3827 return " {$this->_aliases['civicrm_contact']}.id {$sqlOp} (
3828 SELECT DISTINCT mem.contact_id
3829 FROM civicrm_membership mem
3830 LEFT JOIN civicrm_membership_status mem_status ON mem.status_id = mem_status.id
3831 LEFT JOIN civicrm_membership_type mt ON mem.membership_type_id = mt.id
3832 WHERE mem.membership_type_id IN (" .
3833 $tmp_membership_sql_list . ")
3834 AND mt.is_active = '1'
3835 AND mem_status.is_current_member = '1'
3836 AND mem_status.is_active = '1' ) ";
3837 }
3838
3839 /**
3840 * Buld contact acl clause
3841 * @deprecated in favor of buildPermissionClause
3842 *
3843 * @param string $tableAlias
3844 */
3845 public function buildACLClause($tableAlias = 'contact_a') {
3846 list($this->_aclFrom, $this->_aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause($tableAlias);
3847 }
3848
3849 /**
3850 * Build the permision clause for all entities in this report
3851 */
3852 public function buildPermissionClause() {
3853 $ret = array();
3854 foreach ($this->selectedTables() as $tableName) {
3855 $baoName = str_replace('_DAO_', '_BAO_', CRM_Core_DAO_AllCoreTables::getClassForTable($tableName));
3856 if ($baoName && class_exists($baoName) && !empty($this->_columns[$tableName]['alias'])) {
3857 $tableAlias = $this->_columns[$tableName]['alias'];
3858 $clauses = array_filter($baoName::getSelectWhereClause($tableAlias));
3859 foreach ($clauses as $field => $clause) {
3860 // Skip contact_id field if redundant
3861 if ($field != 'contact_id' || !in_array('civicrm_contact', $this->selectedTables())) {
3862 $ret["$tableName.$field"] = $clause;
3863 }
3864 }
3865 }
3866 }
3867 // Override output from buildACLClause
3868 $this->_aclFrom = NULL;
3869 $this->_aclWhere = implode(' AND ', $ret);
3870 }
3871
3872 /**
3873 * Add custom data to the columns.
3874 *
3875 * @param bool $addFields
3876 * @param array $permCustomGroupIds
3877 */
3878 public function addCustomDataToColumns($addFields = TRUE, $permCustomGroupIds = array()) {
3879 if (empty($this->_customGroupExtends)) {
3880 return;
3881 }
3882 if (!is_array($this->_customGroupExtends)) {
3883 $this->_customGroupExtends = array($this->_customGroupExtends);
3884 }
3885 $customGroupWhere = '';
3886 if (!empty($permCustomGroupIds)) {
3887 $customGroupWhere = "cg.id IN (" . implode(',', $permCustomGroupIds) .
3888 ") AND";
3889 }
3890 $sql = "
3891 SELECT cg.table_name, cg.title, cg.extends, cf.id as cf_id, cf.label,
3892 cf.column_name, cf.data_type, cf.html_type, cf.option_group_id, cf.time_format
3893 FROM civicrm_custom_group cg
3894 INNER JOIN civicrm_custom_field cf ON cg.id = cf.custom_group_id
3895 WHERE cg.extends IN ('" . implode("','", $this->_customGroupExtends) . "') AND
3896 {$customGroupWhere}
3897 cg.is_active = 1 AND
3898 cf.is_active = 1 AND
3899 cf.is_searchable = 1
3900 ORDER BY cg.weight, cf.weight";
3901 $customDAO = CRM_Core_DAO::executeQuery($sql);
3902
3903 $curTable = NULL;
3904 while ($customDAO->fetch()) {
3905 if ($customDAO->table_name != $curTable) {
3906 $curTable = $customDAO->table_name;
3907 $curFields = $curFilters = array();
3908
3909 // dummy dao object
3910 $this->_columns[$curTable]['dao'] = 'CRM_Contact_DAO_Contact';
3911 $this->_columns[$curTable]['extends'] = $customDAO->extends;
3912 $this->_columns[$curTable]['grouping'] = $customDAO->table_name;
3913 $this->_columns[$curTable]['group_title'] = $customDAO->title;
3914
3915 foreach (array('fields', 'filters', 'group_bys') as $colKey) {
3916 if (!array_key_exists($colKey, $this->_columns[$curTable])) {
3917 $this->_columns[$curTable][$colKey] = array();
3918 }
3919 }
3920 }
3921 $fieldName = 'custom_' . $customDAO->cf_id;
3922
3923 if ($addFields) {
3924 // this makes aliasing work in favor
3925 $curFields[$fieldName] = array(
3926 'name' => $customDAO->column_name,
3927 'title' => $customDAO->label,
3928 'dataType' => $customDAO->data_type,
3929 'htmlType' => $customDAO->html_type,
3930 );
3931 }
3932 if ($this->_customGroupFilters) {
3933 // this makes aliasing work in favor
3934 $curFilters[$fieldName] = array(
3935 'name' => $customDAO->column_name,
3936 'title' => $customDAO->label,
3937 'dataType' => $customDAO->data_type,
3938 'htmlType' => $customDAO->html_type,
3939 );
3940 }
3941
3942 switch ($customDAO->data_type) {
3943 case 'Date':
3944 // filters
3945 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form::OP_DATE;
3946 $curFilters[$fieldName]['type'] = CRM_Utils_Type::T_DATE;
3947 // CRM-6946, show time part for datetime date fields
3948 if ($customDAO->time_format) {
3949 $curFields[$fieldName]['type'] = CRM_Utils_Type::T_TIMESTAMP;
3950 }
3951 break;
3952
3953 case 'Boolean':
3954 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form::OP_SELECT;
3955 $curFilters[$fieldName]['options'] = array('' => ts('- select -')) + CRM_Core_PseudoConstant::get('CRM_Core_BAO_CustomField', 'custom_' . $customDAO->cf_id, array(), 'search');
3956 $curFilters[$fieldName]['type'] = CRM_Utils_Type::T_INT;
3957 break;
3958
3959 case 'Int':
3960 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form::OP_INT;
3961 $curFilters[$fieldName]['type'] = CRM_Utils_Type::T_INT;
3962 break;
3963
3964 case 'Money':
3965 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form::OP_FLOAT;
3966 $curFilters[$fieldName]['type'] = CRM_Utils_Type::T_MONEY;
3967 $curFields[$fieldName]['type'] = CRM_Utils_Type::T_MONEY;
3968 break;
3969
3970 case 'Float':
3971 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form::OP_FLOAT;
3972 $curFilters[$fieldName]['type'] = CRM_Utils_Type::T_FLOAT;
3973 break;
3974
3975 case 'String':
3976 case 'StateProvince':
3977 case 'Country':
3978 $curFilters[$fieldName]['type'] = CRM_Utils_Type::T_STRING;
3979
3980 $options = CRM_Core_PseudoConstant::get('CRM_Core_BAO_CustomField', 'custom_' . $customDAO->cf_id, array(), 'search');
3981 if ((is_array($options) && count($options) != 0) || (!is_array($options) && $options !== FALSE)) {
3982 $curFilters[$fieldName]['operatorType'] = CRM_Core_BAO_CustomField::isSerialized($customDAO) ? CRM_Report_Form::OP_MULTISELECT_SEPARATOR : CRM_Report_Form::OP_MULTISELECT;
3983 $curFilters[$fieldName]['options'] = $options;
3984 }
3985 break;
3986
3987 case 'ContactReference':
3988 $curFilters[$fieldName]['type'] = CRM_Utils_Type::T_STRING;
3989 $curFilters[$fieldName]['name'] = 'display_name';
3990 $curFilters[$fieldName]['alias'] = "contact_{$fieldName}_civireport";
3991
3992 $curFields[$fieldName]['type'] = CRM_Utils_Type::T_STRING;
3993 $curFields[$fieldName]['name'] = 'display_name';
3994 $curFields[$fieldName]['alias'] = "contact_{$fieldName}_civireport";
3995 break;
3996
3997 default:
3998 $curFields[$fieldName]['type'] = CRM_Utils_Type::T_STRING;
3999 $curFilters[$fieldName]['type'] = CRM_Utils_Type::T_STRING;
4000 }
4001
4002 // CRM-19401 fix
4003 if ($customDAO->html_type == 'Select' && !array_key_exists('options', $curFilters[$fieldName])) {
4004 $options = CRM_Core_PseudoConstant::get('CRM_Core_BAO_CustomField', 'custom_' . $customDAO->cf_id, array(), 'search');
4005 if ($options !== FALSE) {
4006 $curFilters[$fieldName]['operatorType'] = CRM_Core_BAO_CustomField::isSerialized($customDAO) ? CRM_Report_Form::OP_MULTISELECT_SEPARATOR : CRM_Report_Form::OP_MULTISELECT;
4007 $curFilters[$fieldName]['options'] = $options;
4008 }
4009 }
4010
4011 if (!array_key_exists('type', $curFields[$fieldName])) {
4012 $curFields[$fieldName]['type'] = CRM_Utils_Array::value('type', $curFilters[$fieldName], array());
4013 }
4014
4015 if ($addFields) {
4016 $this->_columns[$curTable]['fields'] = array_merge($this->_columns[$curTable]['fields'], $curFields);
4017 }
4018 if ($this->_customGroupFilters) {
4019 $this->_columns[$curTable]['filters'] = array_merge($this->_columns[$curTable]['filters'], $curFilters);
4020 }
4021 if ($this->_customGroupGroupBy) {
4022 $this->_columns[$curTable]['group_bys'] = array_merge($this->_columns[$curTable]['group_bys'], $curFields);
4023 }
4024 }
4025 }
4026
4027 /**
4028 * Build custom data from clause.
4029 *
4030 * @param bool $joinsForFiltersOnly
4031 * Only include joins to support filters. This would be used if creating a table of contacts to include first.
4032 */
4033 public function customDataFrom($joinsForFiltersOnly = FALSE) {
4034 if (empty($this->_customGroupExtends)) {
4035 return;
4036 }
4037 $mapper = CRM_Core_BAO_CustomQuery::$extendsMap;
4038 //CRM-18276 GROUP_CONCAT could be used with singleValueQuery and then exploded,
4039 //but by default that truncates to 1024 characters, which causes errors with installs with lots of custom field sets
4040 $customTables = array();
4041 $customTablesDAO = CRM_Core_DAO::executeQuery("SELECT table_name FROM civicrm_custom_group");
4042 while ($customTablesDAO->fetch()) {
4043 $customTables[] = $customTablesDAO->table_name;
4044 }
4045
4046 foreach ($this->_columns as $table => $prop) {
4047 if (in_array($table, $customTables)) {
4048 $extendsTable = $mapper[$prop['extends']];
4049 // Check field is required for rendering the report.
4050 if ((!$this->isFieldSelected($prop)) || ($joinsForFiltersOnly && !$this->isFieldFiltered($prop))) {
4051 continue;
4052 }
4053 $baseJoin = CRM_Utils_Array::value($prop['extends'], $this->_customGroupExtendsJoin, "{$this->_aliases[$extendsTable]}.id");
4054
4055 $customJoin = is_array($this->_customGroupJoin) ? $this->_customGroupJoin[$table] : $this->_customGroupJoin;
4056 $this->_from .= "
4057 {$customJoin} {$table} {$this->_aliases[$table]} ON {$this->_aliases[$table]}.entity_id = {$baseJoin}";
4058 // handle for ContactReference
4059 if (array_key_exists('fields', $prop)) {
4060 foreach ($prop['fields'] as $fieldName => $field) {
4061 if (CRM_Utils_Array::value('dataType', $field) ==
4062 'ContactReference'
4063 ) {
4064 $columnName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', CRM_Core_BAO_CustomField::getKeyID($fieldName), 'column_name');
4065 $this->_from .= "
4066 LEFT JOIN civicrm_contact {$field['alias']} ON {$field['alias']}.id = {$this->_aliases[$table]}.{$columnName} ";
4067 }
4068 }
4069 }
4070 }
4071 }
4072 }
4073
4074 /**
4075 * Check if the field is selected.
4076 *
4077 * @param string $prop
4078 *
4079 * @return bool
4080 */
4081 public function isFieldSelected($prop) {
4082 if (empty($prop)) {
4083 return FALSE;
4084 }
4085
4086 if (!empty($this->_params['fields'])) {
4087 foreach (array_keys($prop['fields']) as $fieldAlias) {
4088 $customFieldId = CRM_Core_BAO_CustomField::getKeyID($fieldAlias);
4089 if ($customFieldId) {
4090 if (array_key_exists($fieldAlias, $this->_params['fields'])) {
4091 return TRUE;
4092 }
4093
4094 //might be survey response field.
4095 if (!empty($this->_params['fields']['survey_response']) &&
4096 !empty($prop['fields'][$fieldAlias]['isSurveyResponseField'])
4097 ) {
4098 return TRUE;
4099 }
4100 }
4101 }
4102 }
4103
4104 if (!empty($this->_params['group_bys']) && $this->_customGroupGroupBy) {
4105 foreach (array_keys($prop['group_bys']) as $fieldAlias) {
4106 if (array_key_exists($fieldAlias, $this->_params['group_bys']) &&
4107 CRM_Core_BAO_CustomField::getKeyID($fieldAlias)
4108 ) {
4109 return TRUE;
4110 }
4111 }
4112 }
4113
4114 if (!empty($this->_params['order_bys'])) {
4115 foreach (array_keys($prop['fields']) as $fieldAlias) {
4116 foreach ($this->_params['order_bys'] as $orderBy) {
4117 if ($fieldAlias == $orderBy['column'] &&
4118 CRM_Core_BAO_CustomField::getKeyID($fieldAlias)
4119 ) {
4120 return TRUE;
4121 }
4122 }
4123 }
4124 }
4125 return $this->isFieldFiltered($prop);
4126
4127 }
4128
4129 /**
4130 * Check if the field is used as a filter.
4131 *
4132 * @param string $prop
4133 *
4134 * @return bool
4135 */
4136 protected function isFieldFiltered($prop) {
4137 if (!empty($prop['filters']) && $this->_customGroupFilters) {
4138 foreach ($prop['filters'] as $fieldAlias => $val) {
4139 foreach (array(
4140 'value',
4141 'min',
4142 'max',
4143 'relative',
4144 'from',
4145 'to',
4146 ) as $attach) {
4147 if (isset($this->_params[$fieldAlias . '_' . $attach]) &&
4148 (!empty($this->_params[$fieldAlias . '_' . $attach])
4149 || ($attach != 'relative' &&
4150 $this->_params[$fieldAlias . '_' . $attach] == '0')
4151 )
4152 ) {
4153 return TRUE;
4154 }
4155 }
4156 if (!empty($this->_params[$fieldAlias . '_op']) &&
4157 in_array($this->_params[$fieldAlias . '_op'], array('nll', 'nnll'))
4158 ) {
4159 return TRUE;
4160 }
4161 }
4162 }
4163
4164 return FALSE;
4165 }
4166
4167 /**
4168 * Check for empty order_by configurations and remove them.
4169 *
4170 * Also set template to hide them.
4171 *
4172 * @param array $formValues
4173 */
4174 public function preProcessOrderBy(&$formValues) {
4175 // Object to show/hide form elements
4176 $_showHide = new CRM_Core_ShowHideBlocks('', '');
4177
4178 $_showHide->addShow('optionField_1');
4179
4180 // Cycle through order_by options; skip any empty ones, and hide them as well
4181 $n = 1;
4182
4183 if (!empty($formValues['order_bys'])) {
4184 foreach ($formValues['order_bys'] as $order_by) {
4185 if ($order_by['column'] && $order_by['column'] != '-') {
4186 $_showHide->addShow('optionField_' . $n);
4187 $orderBys[$n] = $order_by;
4188 $n++;
4189 }
4190 }
4191 }
4192 for ($i = $n; $i <= 5; $i++) {
4193 if ($i > 1) {
4194 $_showHide->addHide('optionField_' . $i);
4195 }
4196 }
4197
4198 // overwrite order_by options with modified values
4199 if (!empty($orderBys)) {
4200 $formValues['order_bys'] = $orderBys;
4201 }
4202 else {
4203 $formValues['order_bys'] = array(1 => array('column' => '-'));
4204 }
4205
4206 // assign show/hide data to template
4207 $_showHide->addToTemplate();
4208 }
4209
4210 /**
4211 * Check if table name has columns in SELECT clause.
4212 *
4213 * @param string $tableName
4214 * Name of table (index of $this->_columns array).
4215 *
4216 * @return bool
4217 */
4218 public function isTableSelected($tableName) {
4219 return in_array($tableName, $this->selectedTables());
4220 }
4221
4222 /**
4223 * Check if table name has columns in WHERE or HAVING clause.
4224 *
4225 * @param string $tableName
4226 * Name of table (index of $this->_columns array).
4227 *
4228 * @return bool
4229 */
4230 public function isTableFiltered($tableName) {
4231 // Cause the array to be generated if not previously done.
4232 if (!$this->_selectedTables && !$this->filteredTables) {
4233 $this->selectedTables();
4234 }
4235 return in_array($tableName, $this->filteredTables);
4236 }
4237
4238 /**
4239 * Fetch array of DAO tables having columns included in SELECT or ORDER BY clause.
4240 *
4241 * If the array is unset it will be built.
4242 *
4243 * @return array
4244 * selectedTables
4245 */
4246 public function selectedTables() {
4247 if (!$this->_selectedTables) {
4248 $orderByColumns = array();
4249 if (array_key_exists('order_bys', $this->_params) &&
4250 is_array($this->_params['order_bys'])
4251 ) {
4252 foreach ($this->_params['order_bys'] as $orderBy) {
4253 $orderByColumns[] = $orderBy['column'];
4254 }
4255 }
4256
4257 foreach ($this->_columns as $tableName => $table) {
4258 if (array_key_exists('fields', $table)) {
4259 foreach ($table['fields'] as $fieldName => $field) {
4260 if (!empty($field['required']) ||
4261 !empty($this->_params['fields'][$fieldName])
4262 ) {
4263 $this->_selectedTables[] = $tableName;
4264 break;
4265 }
4266 }
4267 }
4268 if (array_key_exists('order_bys', $table)) {
4269 foreach ($table['order_bys'] as $orderByName => $orderBy) {
4270 if (in_array($orderByName, $orderByColumns)) {
4271 $this->_selectedTables[] = $tableName;
4272 break;
4273 }
4274 }
4275 }
4276 if (array_key_exists('filters', $table)) {
4277 foreach ($table['filters'] as $filterName => $filter) {
4278 if (!empty($this->_params["{$filterName}_value"])
4279 || !empty($this->_params["{$filterName}_relative"])
4280 || CRM_Utils_Array::value("{$filterName}_op", $this->_params) ==
4281 'nll'
4282 || CRM_Utils_Array::value("{$filterName}_op", $this->_params) ==
4283 'nnll'
4284 ) {
4285 $this->_selectedTables[] = $tableName;
4286 $this->filteredTables[] = $tableName;
4287 break;
4288 }
4289 }
4290 }
4291 }
4292 }
4293 return $this->_selectedTables;
4294 }
4295
4296 /**
4297 * Add campaign fields.
4298 *
4299 * @param bool $groupBy
4300 * Add GroupBy? Not appropriate for detail report.
4301 * @param bool $orderBy
4302 * Add OrderBy? Not appropriate for detail report.
4303 * @param bool $filters
4304 *
4305 */
4306 public function addCampaignFields($entityTable = 'civicrm_contribution', $groupBy = FALSE, $orderBy = FALSE, $filters = TRUE) {
4307 // Check if CiviCampaign is a) enabled and b) has active campaigns
4308 $config = CRM_Core_Config::singleton();
4309 $campaignEnabled = in_array('CiviCampaign', $config->enableComponents);
4310 if ($campaignEnabled) {
4311 $getCampaigns = CRM_Campaign_BAO_Campaign::getPermissionedCampaigns(NULL, NULL, FALSE, FALSE, TRUE);
4312 // If we have a campaign, build out the relevant elements
4313 if (!empty($getCampaigns['campaigns'])) {
4314 $this->campaigns = $getCampaigns['campaigns'];
4315 asort($this->campaigns);
4316 $this->_columns[$entityTable]['fields']['campaign_id'] = array('title' => ts('Campaign'), 'default' => 'false');
4317 if ($filters) {
4318 $this->_columns[$entityTable]['filters']['campaign_id'] = array(
4319 'title' => ts('Campaign'),
4320 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
4321 'options' => $this->campaigns,
4322 'type' => CRM_Utils_Type::T_INT,
4323 );
4324 }
4325
4326 if ($groupBy) {
4327 $this->_columns[$entityTable]['group_bys']['campaign_id'] = array('title' => ts('Campaign'));
4328 }
4329
4330 if ($orderBy) {
4331 $this->_columns[$entityTable]['order_bys']['campaign_id'] = array('title' => ts('Campaign'));
4332 }
4333 }
4334 }
4335 }
4336
4337 /**
4338 * Add address fields.
4339 *
4340 * @deprecated - use getAddressColumns which is a more accurate description
4341 * and also accepts an array of options rather than a long list
4342 *
4343 * adding address fields to construct function in reports
4344 *
4345 * @param bool $groupBy
4346 * Add GroupBy? Not appropriate for detail report.
4347 * @param bool $orderBy
4348 * Add GroupBy? Not appropriate for detail report.
4349 * @param bool $filters
4350 * @param array $defaults
4351 *
4352 * @return array
4353 * address fields for construct clause
4354 */
4355 public function addAddressFields($groupBy = TRUE, $orderBy = FALSE, $filters = TRUE, $defaults = array('country_id' => TRUE)) {
4356 $defaultAddressFields = array(
4357 'street_address' => ts('Street Address'),
4358 'supplemental_address_1' => ts('Supplementary Address Field 1'),
4359 'supplemental_address_2' => ts('Supplementary Address Field 2'),
4360 'supplemental_address_3' => ts('Supplementary Address Field 3'),
4361 'street_number' => ts('Street Number'),
4362 'street_name' => ts('Street Name'),
4363 'street_unit' => ts('Street Unit'),
4364 'city' => ts('City'),
4365 'postal_code' => ts('Postal Code'),
4366 'postal_code_suffix' => ts('Postal Code Suffix'),
4367 'country_id' => ts('Country'),
4368 'state_province_id' => ts('State/Province'),
4369 'county_id' => ts('County'),
4370 );
4371 $addressFields = array(
4372 'civicrm_address' => array(
4373 'dao' => 'CRM_Core_DAO_Address',
4374 'fields' => array(
4375 'address_name' => array(
4376 'title' => ts('Address Name'),
4377 'default' => CRM_Utils_Array::value('name', $defaults, FALSE),
4378 'name' => 'name',
4379 ),
4380 ),
4381 'grouping' => 'location-fields',
4382 ),
4383 );
4384 foreach ($defaultAddressFields as $fieldName => $fieldLabel) {
4385 $addressFields['civicrm_address']['fields'][$fieldName] = array(
4386 'title' => $fieldLabel,
4387 'default' => CRM_Utils_Array::value($fieldName, $defaults, FALSE),
4388 );
4389 }
4390
4391 $street_address_filters = $general_address_filters = array();
4392 if ($filters) {
4393 // Address filter depends on whether street address parsing is enabled.
4394 // (CRM-18696)
4395 $addressOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
4396 'address_options'
4397 );
4398 if ($addressOptions['street_address_parsing']) {
4399 $street_address_filters = array(
4400 'street_number' => array(
4401 'title' => ts('Street Number'),
4402 'type' => CRM_Utils_Type::T_INT,
4403 'name' => 'street_number',
4404 ),
4405 'street_name' => array(
4406 'title' => ts('Street Name'),
4407 'name' => 'street_name',
4408 'type' => CRM_Utils_Type::T_STRING,
4409 ),
4410 );
4411 }
4412 else {
4413 $street_address_filters = array(
4414 'street_address' => array(
4415 'title' => ts('Street Address'),
4416 'type' => CRM_Utils_Type::T_STRING,
4417 'name' => 'street_address',
4418 ),
4419 );
4420 }
4421 $general_address_filters = array(
4422 'postal_code' => array(
4423 'title' => ts('Postal Code'),
4424 'type' => CRM_Utils_Type::T_STRING,
4425 'name' => 'postal_code',
4426 ),
4427 'city' => array(
4428 'title' => ts('City'),
4429 'type' => CRM_Utils_Type::T_STRING,
4430 'name' => 'city',
4431 ),
4432 'country_id' => array(
4433 'name' => 'country_id',
4434 'title' => ts('Country'),
4435 'type' => CRM_Utils_Type::T_INT,
4436 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
4437 'options' => CRM_Core_PseudoConstant::country(),
4438 ),
4439 'state_province_id' => array(
4440 'name' => 'state_province_id',
4441 'title' => ts('State/Province'),
4442 'type' => CRM_Utils_Type::T_INT,
4443 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
4444 'options' => array(),
4445 ),
4446 'county_id' => array(
4447 'name' => 'county_id',
4448 'title' => ts('County'),
4449 'type' => CRM_Utils_Type::T_INT,
4450 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
4451 'options' => array(),
4452 ),
4453 );
4454 }
4455 $addressFields['civicrm_address']['filters'] = array_merge(
4456 $street_address_filters,
4457 $general_address_filters);
4458
4459 if ($orderBy) {
4460 $addressFields['civicrm_address']['order_bys'] = array(
4461 'street_name' => array('title' => ts('Street Name')),
4462 'street_number' => array('title' => ts('Odd / Even Street Number')),
4463 'street_address' => NULL,
4464 'city' => NULL,
4465 'postal_code' => NULL,
4466 );
4467 }
4468
4469 if ($groupBy) {
4470 $addressFields['civicrm_address']['group_bys'] = array(
4471 'street_address' => NULL,
4472 'city' => NULL,
4473 'postal_code' => NULL,
4474 'state_province_id' => array(
4475 'title' => ts('State/Province'),
4476 ),
4477 'country_id' => array(
4478 'title' => ts('Country'),
4479 ),
4480 'county_id' => array(
4481 'title' => ts('County'),
4482 ),
4483 );
4484 }
4485 return $addressFields;
4486 }
4487
4488 /**
4489 * Do AlterDisplay processing on Address Fields.
4490 * If there are multiple address field values then
4491 * on basis of provided separator the code values are translated into respective labels
4492 *
4493 * @param array $row
4494 * @param array $rows
4495 * @param int $rowNum
4496 * @param string $baseUrl
4497 * @param string $linkText
4498 * @param string $separator
4499 *
4500 * @return bool
4501 */
4502 public function alterDisplayAddressFields(&$row, &$rows, &$rowNum, $baseUrl, $linkText, $separator = ',') {
4503 $criteriaQueryParams = CRM_Report_Utils_Report::getPreviewCriteriaQueryParams($this->_defaults, $this->_params);
4504 $entryFound = FALSE;
4505 $columnMap = array(
4506 'civicrm_address_country_id' => 'country',
4507 'civicrm_address_county_id' => 'county',
4508 'civicrm_address_state_province_id' => 'stateProvince',
4509 );
4510 foreach ($columnMap as $fieldName => $fnName) {
4511 if (array_key_exists($fieldName, $row)) {
4512 if ($values = $row[$fieldName]) {
4513 $values = (array) explode($separator, $values);
4514 $rows[$rowNum][$fieldName] = [];
4515 $addressField = $fnName == 'stateProvince' ? 'state' : $fnName;
4516 foreach ($values as $value) {
4517 $rows[$rowNum][$fieldName][] = CRM_Core_PseudoConstant::$fnName($value);
4518 }
4519 $rows[$rowNum][$fieldName] = implode($separator, $rows[$rowNum][$fieldName]);
4520 if ($baseUrl) {
4521 $url = CRM_Report_Utils_Report::getNextUrl($baseUrl,
4522 sprintf("reset=1&force=1&%s&%s_op=in&%s_value=%s",
4523 $criteriaQueryParams,
4524 str_replace('civicrm_address_', '', $fieldName),
4525 str_replace('civicrm_address_', '', $fieldName),
4526 implode(',', $values)
4527 ), $this->_absoluteUrl, $this->_id
4528 );
4529 $rows[$rowNum]["{$fieldName}_link"] = $url;
4530 $rows[$rowNum]["{$fieldName}_hover"] = ts("%1 for this %2.", array(1 => $linkText, 2 => $addressField));
4531 }
4532 }
4533 $entryFound = TRUE;
4534 }
4535 }
4536
4537 return $entryFound;
4538 }
4539
4540 /**
4541 * Do AlterDisplay processing on Address Fields.
4542 *
4543 * @param array $row
4544 * @param array $rows
4545 * @param int $rowNum
4546 * @param string $baseUrl
4547 * @param string $linkText
4548 *
4549 * @return bool
4550 */
4551 public function alterDisplayContactFields(&$row, &$rows, &$rowNum, $baseUrl, $linkText) {
4552 $entryFound = FALSE;
4553 // There is no reason not to add links for all fields but it seems a bit odd to be able to click on
4554 // 'Mrs'. Also, we don't have metadata about the title. So, add selectively to addLinks.
4555 $addLinks = array('gender_id' => 'Gender');
4556 foreach (array('prefix_id', 'suffix_id', 'gender_id', 'contact_sub_type', 'preferred_language') as $fieldName) {
4557 if (array_key_exists('civicrm_contact_' . $fieldName, $row)) {
4558 if (($value = $row['civicrm_contact_' . $fieldName]) != FALSE) {
4559 $rowValues = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
4560 $rowLabels = array();
4561 foreach ($rowValues as $rowValue) {
4562 if ($rowValue) {
4563 $rowLabels[] = CRM_Core_PseudoConstant::getLabel('CRM_Contact_BAO_Contact', $fieldName, $rowValue);
4564 }
4565 }
4566 $rows[$rowNum]['civicrm_contact_' . $fieldName] = implode(', ', $rowLabels);
4567 if ($baseUrl && ($title = CRM_Utils_Array::value($fieldName, $addLinks)) != FALSE) {
4568 $this->addLinkToRow($rows[$rowNum], $baseUrl, $linkText, $value, $fieldName, 'civicrm_contact', $title);
4569 }
4570 }
4571 $entryFound = TRUE;
4572 }
4573 }
4574 $yesNoFields = array(
4575 'do_not_email', 'is_deceased', 'do_not_phone', 'do_not_sms', 'do_not_mail', 'is_opt_out',
4576 );
4577 foreach ($yesNoFields as $fieldName) {
4578 if (array_key_exists('civicrm_contact_' . $fieldName, $row)) {
4579 // Since these are essentially 'negative fields' it feels like it
4580 // makes sense to only highlight the exceptions hence no 'No'.
4581 $rows[$rowNum]['civicrm_contact_' . $fieldName] = !empty($rows[$rowNum]['civicrm_contact_' . $fieldName]) ? ts('Yes') : '';
4582 $entryFound = TRUE;
4583 }
4584 }
4585
4586 // Handle employer id
4587 if (array_key_exists('civicrm_contact_employer_id', $row)) {
4588 $employerId = $row['civicrm_contact_employer_id'];
4589 if ($employerId) {
4590 $rows[$rowNum]['civicrm_contact_employer_id'] = CRM_Contact_BAO_Contact::displayName($employerId);
4591 $rows[$rowNum]['civicrm_contact_employer_id_link'] = CRM_Utils_System::url('civicrm/contact/view', 'reset=1&cid=' . $employerId, $this->_absoluteUrl);
4592 $rows[$rowNum]['civicrm_contact_employer_id_hover'] = ts('View Contact Summary for Employer.');
4593 $entryFound = TRUE;
4594 }
4595 }
4596
4597 return $entryFound;
4598 }
4599
4600 /**
4601 * Adjusts dates passed in to YEAR() for fiscal year.
4602 *
4603 * @param string $fieldName
4604 *
4605 * @return string
4606 */
4607 public function fiscalYearOffset($fieldName) {
4608 $config = CRM_Core_Config::singleton();
4609 $fy = $config->fiscalYearStart;
4610 if (CRM_Utils_Array::value('yid_op', $this->_params) == 'calendar' ||
4611 ($fy['d'] == 1 && $fy['M'] == 1)
4612 ) {
4613 return "YEAR( $fieldName )";
4614 }
4615 return "YEAR( $fieldName - INTERVAL " . ($fy['M'] - 1) . " MONTH" .
4616 ($fy['d'] > 1 ? (" - INTERVAL " . ($fy['d'] - 1) . " DAY") : '') . " )";
4617 }
4618
4619 /**
4620 * Add Address into From Table if required.
4621 *
4622 * @deprecated use joinAddressFromContact
4623 * (left here in case extensions use it).
4624 */
4625 public function addAddressFromClause() {
4626 CRM_Core_Error::deprecatedFunctionWarning('CRM_Report_Form::joinAddressFromContact');
4627 // include address field if address column is to be included
4628 if ((isset($this->_addressField) &&
4629 $this->_addressField
4630 ) ||
4631 $this->isTableSelected('civicrm_address')
4632 ) {
4633 $this->_from .= "
4634 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
4635 ON ({$this->_aliases['civicrm_contact']}.id =
4636 {$this->_aliases['civicrm_address']}.contact_id) AND
4637 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
4638 }
4639 }
4640
4641 /**
4642 * Add Phone into From Table if required.
4643 *
4644 * @deprecated use joinPhoneFromContact
4645 * (left here in case extensions use it).
4646 */
4647 public function addPhoneFromClause() {
4648 CRM_Core_Error::deprecatedFunctionWarning('CRM_Report_Form::joinPhoneFromContact');
4649 // include address field if address column is to be included
4650 if ($this->isTableSelected('civicrm_phone')) {
4651 $this->_from .= "
4652 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
4653 ON ({$this->_aliases['civicrm_contact']}.id =
4654 {$this->_aliases['civicrm_phone']}.contact_id) AND
4655 {$this->_aliases['civicrm_phone']}.is_primary = 1\n";
4656 }
4657 }
4658
4659 /**
4660 * Add Address into From Table if required.
4661 *
4662 * Prefix will be added to both tables as
4663 * it is assumed you are using it to get address of a secondary contact.
4664 *
4665 * @param string $prefix
4666 * @param array $extra Additional options.
4667 * Not currently used in core but may be used in override extensions.
4668 */
4669 protected function joinAddressFromContact($prefix = '', $extra = array()) {
4670 $addressTables = ['civicrm_address', 'civicrm_country', 'civicrm_worldregion', 'civicrm_state_province'];
4671 $isJoinRequired = $this->_addressField;
4672 foreach ($addressTables as $addressTable) {
4673 if ($this->isTableSelected($prefix . $addressTable)) {
4674 $isJoinRequired = TRUE;
4675 }
4676 }
4677 if ($isJoinRequired) {
4678 $this->_from .= "
4679 LEFT JOIN civicrm_address {$this->_aliases[$prefix . 'civicrm_address']}
4680 ON ({$this->_aliases[$prefix . 'civicrm_contact']}.id =
4681 {$this->_aliases[$prefix . 'civicrm_address']}.contact_id) AND
4682 {$this->_aliases[$prefix . 'civicrm_address']}.is_primary = 1\n";
4683 }
4684 }
4685
4686 /**
4687 * Add Country into From Table if required.
4688 *
4689 * Prefix will be added to both tables as
4690 * it is assumed you are using it to get address of a secondary contact.
4691 *
4692 * @param string $prefix
4693 * @param array $extra Additional options.
4694 * Not currently used in core but may be used in override extensions.
4695 */
4696 protected function joinCountryFromAddress($prefix = '', $extra = array()) {
4697 // include country field if country column is to be included
4698 if ($this->isTableSelected($prefix . 'civicrm_country') || $this->isTableSelected($prefix . 'civicrm_worldregion')) {
4699 if (empty($this->_aliases[$prefix . 'civicrm_country'])) {
4700 $this->_aliases[$prefix . 'civicrm_country'] = $prefix . '_report_country';
4701 }
4702 $this->_from .= "
4703 LEFT JOIN civicrm_country {$this->_aliases[$prefix . 'civicrm_country']}
4704 ON {$this->_aliases[$prefix . 'civicrm_address']}.country_id = {$this->_aliases[$prefix . 'civicrm_country']}.id AND
4705 {$this->_aliases[$prefix . 'civicrm_address']}.is_primary = 1 ";
4706 }
4707 }
4708
4709 /**
4710 * Add Phone into From Table if required.
4711 *
4712 * Prefix will be added to both tables as
4713 * it is assumed you are using it to get address of a secondary contact.
4714 *
4715 * @param string $prefix
4716 * @param array $extra Additional options.
4717 * Not currently used in core but may be used in override extensions.
4718 */
4719 protected function joinPhoneFromContact($prefix = '', $extra = array()) {
4720 // include phone field if phone column is to be included
4721 if ($this->isTableSelected($prefix . 'civicrm_phone')) {
4722 $this->_from .= "
4723 LEFT JOIN civicrm_phone {$this->_aliases[$prefix . 'civicrm_phone']}
4724 ON {$this->_aliases[$prefix . 'civicrm_contact']}.id = {$this->_aliases[$prefix . 'civicrm_phone']}.contact_id AND
4725 {$this->_aliases[$prefix . 'civicrm_phone']}.is_primary = 1\n";
4726 }
4727 }
4728
4729 /**
4730 * Add Email into From Table if required.
4731 *
4732 * Prefix will be added to both tables as
4733 * it is assumed you are using it to get address of a secondary contact.
4734 *
4735 * @param string $prefix
4736 * @param array $extra Additional options.
4737 * Not currently used in core but may be used in override extensions.
4738 */
4739 protected function joinEmailFromContact($prefix = '', $extra = array()) {
4740 // include email field if email column is to be included
4741 if ($this->isTableSelected($prefix . 'civicrm_email')) {
4742 $this->_from .= "
4743 LEFT JOIN civicrm_email {$this->_aliases[$prefix . 'civicrm_email']}
4744 ON ({$this->_aliases[$prefix . 'civicrm_contact']}.id = {$this->_aliases[$prefix . 'civicrm_email']}.contact_id AND
4745 {$this->_aliases[$prefix . 'civicrm_email']}.is_primary = 1) ";
4746 }
4747 }
4748
4749 /**
4750 * Add Financial Transaction into From Table if required.
4751 */
4752 public function addFinancialTrxnFromClause() {
4753 if ($this->isTableSelected('civicrm_financial_trxn')) {
4754 $this->_from .= "
4755 LEFT JOIN civicrm_entity_financial_trxn eftcc
4756 ON ({$this->_aliases['civicrm_contribution']}.id = eftcc.entity_id AND
4757 eftcc.entity_table = 'civicrm_contribution')
4758 LEFT JOIN civicrm_financial_trxn {$this->_aliases['civicrm_financial_trxn']}
4759 ON {$this->_aliases['civicrm_financial_trxn']}.id = eftcc.financial_trxn_id \n";
4760 }
4761 }
4762
4763 /**
4764 * Get phone columns to add to array.
4765 *
4766 * @param array $options
4767 * - prefix Prefix to add to table (in case of more than one instance of the table)
4768 * - prefix_label Label to give columns from this phone table instance
4769 *
4770 * @return array
4771 * phone columns definition
4772 */
4773 public function getPhoneColumns($options = array()) {
4774 $defaultOptions = array(
4775 'prefix' => '',
4776 'prefix_label' => '',
4777 );
4778
4779 $options = array_merge($defaultOptions, $options);
4780
4781 $fields = array(
4782 $options['prefix'] . 'civicrm_phone' => array(
4783 'dao' => 'CRM_Core_DAO_Phone',
4784 'fields' => array(
4785 $options['prefix'] . 'phone' => array(
4786 'title' => $options['prefix_label'] . ts('Phone'),
4787 'name' => 'phone',
4788 ),
4789 ),
4790 ),
4791 );
4792 return $fields;
4793 }
4794
4795 /**
4796 * Get a standard set of contact fields.
4797 * @deprecated - use getColumns('Contact') instead
4798 * @return array
4799 */
4800 public function getBasicContactFields() {
4801 return array(
4802 'sort_name' => array(
4803 'title' => ts('Contact Name'),
4804 'required' => TRUE,
4805 'default' => TRUE,
4806 ),
4807 'id' => array(
4808 'no_display' => TRUE,
4809 'required' => TRUE,
4810 ),
4811 'prefix_id' => array(
4812 'title' => ts('Contact Prefix'),
4813 ),
4814 'first_name' => array(
4815 'title' => ts('First Name'),
4816 ),
4817 'nick_name' => array(
4818 'title' => ts('Nick Name'),
4819 ),
4820 'middle_name' => array(
4821 'title' => ts('Middle Name'),
4822 ),
4823 'last_name' => array(
4824 'title' => ts('Last Name'),
4825 ),
4826 'suffix_id' => array(
4827 'title' => ts('Contact Suffix'),
4828 ),
4829 'postal_greeting_display' => array('title' => ts('Postal Greeting')),
4830 'email_greeting_display' => array('title' => ts('Email Greeting')),
4831 'addressee_display' => array('title' => ts('Addressee')),
4832 'contact_type' => array(
4833 'title' => ts('Contact Type'),
4834 ),
4835 'contact_sub_type' => array(
4836 'title' => ts('Contact Subtype'),
4837 ),
4838 'gender_id' => array(
4839 'title' => ts('Gender'),
4840 ),
4841 'birth_date' => array(
4842 'title' => ts('Birth Date'),
4843 ),
4844 'age' => array(
4845 'title' => ts('Age'),
4846 'dbAlias' => 'TIMESTAMPDIFF(YEAR, contact_civireport.birth_date, CURDATE())',
4847 ),
4848 'job_title' => array(
4849 'title' => ts('Contact Job title'),
4850 ),
4851 'organization_name' => array(
4852 'title' => ts('Organization Name'),
4853 ),
4854 'external_identifier' => array(
4855 'title' => ts('Contact identifier from external system'),
4856 ),
4857 'do_not_email' => array(),
4858 'do_not_phone' => array(),
4859 'do_not_mail' => array(),
4860 'do_not_sms' => array(),
4861 'is_opt_out' => array(),
4862 'is_deceased' => array(),
4863 'preferred_language' => array(),
4864 'employer_id' => array(
4865 'title' => ts('Current Employer'),
4866 ),
4867 );
4868 }
4869
4870 /**
4871 * Get a standard set of contact filters.
4872 *
4873 * @param array $defaults
4874 *
4875 * @return array
4876 */
4877 public function getBasicContactFilters($defaults = array()) {
4878 return array(
4879 'sort_name' => array(
4880 'title' => ts('Contact Name'),
4881 ),
4882 'source' => array(
4883 'title' => ts('Contact Source'),
4884 'type' => CRM_Utils_Type::T_STRING,
4885 ),
4886 'id' => array(
4887 'title' => ts('Contact ID'),
4888 'no_display' => TRUE,
4889 ),
4890 'gender_id' => array(
4891 'title' => ts('Gender'),
4892 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
4893 'options' => CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'),
4894 ),
4895 'birth_date' => array(
4896 'title' => ts('Birth Date'),
4897 'operatorType' => CRM_Report_Form::OP_DATE,
4898 ),
4899 'contact_type' => array(
4900 'title' => ts('Contact Type'),
4901 ),
4902 'contact_sub_type' => array(
4903 'title' => ts('Contact Subtype'),
4904 ),
4905 'modified_date' => array(
4906 'title' => ts('Contact Modified'),
4907 'operatorType' => CRM_Report_Form::OP_DATE,
4908 'type' => CRM_Utils_Type::T_DATE,
4909 ),
4910 'is_deceased' => array(
4911 'title' => ts('Deceased'),
4912 'type' => CRM_Utils_Type::T_BOOLEAN,
4913 'default' => CRM_Utils_Array::value('deceased', $defaults, 0),
4914 ),
4915 'do_not_email' => array(
4916 'title' => ts('Do not email'),
4917 'type' => CRM_Utils_Type::T_BOOLEAN,
4918 ),
4919 'do_not_phone' => array(
4920 'title' => ts('Do not phone'),
4921 'type' => CRM_Utils_Type::T_BOOLEAN,
4922 ),
4923 'do_not_mail' => array(
4924 'title' => ts('Do not mail'),
4925 'type' => CRM_Utils_Type::T_BOOLEAN,
4926 ),
4927 'do_not_sms' => array(
4928 'title' => ts('Do not SMS'),
4929 'type' => CRM_Utils_Type::T_BOOLEAN,
4930 ),
4931 'is_opt_out' => array(
4932 'title' => ts('Do not bulk email'),
4933 'type' => CRM_Utils_Type::T_BOOLEAN,
4934 ),
4935 'preferred_language' => array(
4936 'title' => ts('Preferred Language'),
4937 ),
4938 'is_deleted' => array(
4939 'no_display' => TRUE,
4940 'default' => 0,
4941 'type' => CRM_Utils_Type::T_BOOLEAN,
4942 ),
4943 );
4944 }
4945
4946 /**
4947 * Add contact to group.
4948 *
4949 * @param int $groupID
4950 */
4951 public function add2group($groupID) {
4952 if (is_numeric($groupID) && isset($this->_aliases['civicrm_contact'])) {
4953 $select = "SELECT DISTINCT {$this->_aliases['civicrm_contact']}.id AS addtogroup_contact_id, ";
4954 $select = preg_replace('/SELECT(\s+SQL_CALC_FOUND_ROWS)?\s+/i', $select, $this->_select);
4955 $sql = "{$select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy}";
4956 $sql = str_replace('WITH ROLLUP', '', $sql);
4957 $dao = CRM_Core_DAO::executeQuery($sql);
4958
4959 $contact_ids = array();
4960 // Add resulting contacts to group
4961 while ($dao->fetch()) {
4962 if ($dao->addtogroup_contact_id) {
4963 $contact_ids[$dao->addtogroup_contact_id] = $dao->addtogroup_contact_id;
4964 }
4965 }
4966
4967 if (!empty($contact_ids)) {
4968 CRM_Contact_BAO_GroupContact::addContactsToGroup($contact_ids, $groupID);
4969 CRM_Core_Session::setStatus(ts("Listed contact(s) have been added to the selected group."), ts('Contacts Added'), 'success');
4970 }
4971 else {
4972 CRM_Core_Session::setStatus(ts("The listed records(s) cannot be added to the group."));
4973 }
4974 }
4975 }
4976
4977 /**
4978 * Show charts on print screen.
4979 */
4980 public static function uploadChartImage() {
4981 // upload strictly for '.png' images
4982 $name = trim(basename(CRM_Utils_Request::retrieve('name', 'String', CRM_Core_DAO::$_nullObject, FALSE, NULL, 'GET')));
4983 if (preg_match('/\.png$/', $name)) {
4984
4985 // Get the RAW .png from the input.
4986 $httpRawPostData = file_get_contents("php://input");
4987
4988 // prepare the directory
4989 $config = CRM_Core_Config::singleton();
4990 $defaultPath
4991 = str_replace('/persist/contribute/', '/persist/', $config->imageUploadDir) .
4992 '/openFlashChart/';
4993 if (!file_exists($defaultPath)) {
4994 mkdir($defaultPath, 0777, TRUE);
4995 }
4996
4997 // full path to the saved image including filename
4998 $destination = $defaultPath . $name;
4999
5000 //write and save
5001 $jfh = fopen($destination, 'w') or die("can't open file");
5002 fwrite($jfh, $httpRawPostData);
5003 fclose($jfh);
5004 CRM_Utils_System::civiExit();
5005 }
5006 }
5007
5008 /**
5009 * Apply common settings to entityRef fields.
5010 *
5011 * @param array $field
5012 * @param string $table
5013 */
5014 public function setEntityRefDefaults(&$field, $table) {
5015 $field['attributes'] = $field['attributes'] ? $field['attributes'] : array();
5016 $field['attributes'] += array(
5017 'entity' => CRM_Core_DAO_AllCoreTables::getBriefName(CRM_Core_DAO_AllCoreTables::getClassForTable($table)),
5018 'multiple' => TRUE,
5019 'placeholder' => ts('- select -'),
5020 );
5021 }
5022
5023 /**
5024 * Add link fields to the row.
5025 *
5026 * Function adds the _link & _hover fields to the row.
5027 *
5028 * @param array $row
5029 * @param string $baseUrl
5030 * @param string $linkText
5031 * @param string $value
5032 * @param string $fieldName
5033 * @param string $tablePrefix
5034 * @param string $fieldLabel
5035 *
5036 * @return mixed
5037 */
5038 protected function addLinkToRow(&$row, $baseUrl, $linkText, $value, $fieldName, $tablePrefix, $fieldLabel) {
5039 $criteriaQueryParams = CRM_Report_Utils_Report::getPreviewCriteriaQueryParams($this->_defaults, $this->_params);
5040 $url = CRM_Report_Utils_Report::getNextUrl($baseUrl,
5041 "reset=1&force=1&{$criteriaQueryParams}&" .
5042 $fieldName . "_op=in&{$fieldName}_value={$value}",
5043 $this->_absoluteUrl, $this->_id
5044 );
5045 $row["{$tablePrefix}_{$fieldName}_link"] = $url;
5046 $row["{$tablePrefix}_{$fieldName}_hover"] = ts("%1 for this %2.",
5047 array(1 => $linkText, 2 => $fieldLabel)
5048 );
5049 }
5050
5051 /**
5052 * Get label for show results buttons.
5053 *
5054 * @return string
5055 */
5056 public function getResultsLabel() {
5057 $showResultsLabel = $this->resultsDisplayed() ? ts('Refresh results') : ts('View results');
5058 return $showResultsLabel;
5059 }
5060
5061 /**
5062 * Determine the output mode from the url or input.
5063 *
5064 * Output could be
5065 * - pdf : Render as pdf
5066 * - csv : Render as csv
5067 * - print : Render in print format
5068 * - save : save the report and display the new report
5069 * - copy : save the report as a new instance and display that.
5070 * - group : go to the add to group screen.
5071 *
5072 * Potentially chart variations could also be included but the complexity
5073 * is that we might print a bar chart as a pdf.
5074 */
5075 protected function setOutputMode() {
5076 $this->_outputMode = str_replace('report_instance.', '', CRM_Utils_Request::retrieve(
5077 'output',
5078 'String',
5079 CRM_Core_DAO::$_nullObject,
5080 FALSE,
5081 CRM_Utils_Array::value('task', $this->_params)
5082 ));
5083 // if contacts are added to group
5084 if (!empty($this->_params['groups']) && empty($this->_outputMode)) {
5085 $this->_outputMode = 'group';
5086 }
5087 if (isset($this->_params['task'])) {
5088 unset($this->_params['task']);
5089 }
5090 }
5091
5092 /**
5093 * CRM-17793 - Alter DateTime section header to group by date from the datetime field.
5094 *
5095 * @param $tempTable
5096 * @param $columnName
5097 */
5098 public function alterSectionHeaderForDateTime($tempTable, $columnName) {
5099 // add new column with date value for the datetime field
5100 $tempQuery = "ALTER TABLE {$tempTable} ADD COLUMN {$columnName}_date VARCHAR(128)";
5101 CRM_Core_DAO::executeQuery($tempQuery);
5102 $updateQuery = "UPDATE {$tempTable} SET {$columnName}_date = date({$columnName})";
5103 CRM_Core_DAO::executeQuery($updateQuery);
5104 $this->_selectClauses[] = "{$columnName}_date";
5105 $this->_select .= ", {$columnName}_date";
5106 $this->_sections["{$columnName}_date"] = $this->_sections["{$columnName}"];
5107 unset($this->_sections["{$columnName}"]);
5108 $this->assign('sections', $this->_sections);
5109 }
5110
5111 /**
5112 * Get an array of the columns that have been selected for display.
5113 *
5114 * @return array
5115 */
5116 public function getSelectColumns() {
5117 $selectColumns = array();
5118 foreach ($this->_columns as $tableName => $table) {
5119 if (array_key_exists('fields', $table)) {
5120 foreach ($table['fields'] as $fieldName => $field) {
5121 if (!empty($field['required']) ||
5122 !empty($this->_params['fields'][$fieldName])
5123 ) {
5124
5125 $selectColumns["{$tableName}_{$fieldName}"] = 1;
5126 }
5127 }
5128 }
5129 }
5130 return $selectColumns;
5131 }
5132
5133 /**
5134 * Add location tables to the query if they are used for filtering.
5135 *
5136 * This is for when we are running the query separately for filtering and retrieving display fields.
5137 */
5138 public function selectivelyAddLocationTablesJoinsToFilterQuery() {
5139 if ($this->isTableFiltered('civicrm_email')) {
5140 $this->_from .= "
5141 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
5142 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id
5143 AND {$this->_aliases['civicrm_email']}.is_primary = 1";
5144 }
5145 if ($this->isTableFiltered('civicrm_phone')) {
5146 $this->_from .= "
5147 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
5148 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id
5149 AND {$this->_aliases['civicrm_phone']}.is_primary = 1";
5150 }
5151 if ($this->isTableFiltered('civicrm_address')) {
5152 $this->_from .= "
5153 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
5154 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id)
5155 AND {$this->_aliases['civicrm_address']}.is_primary = 1\n";
5156 }
5157 }
5158
5159 /**
5160 * Set the base table for the FROM clause.
5161 *
5162 * Sets up the from clause, allowing for the possibility it might be a
5163 * temp table pre-filtered by groups if a group filter is in use.
5164 *
5165 * @param string $baseTable
5166 * @param string $field
5167 * @param null $tableAlias
5168 */
5169 public function setFromBase($baseTable, $field = 'id', $tableAlias = NULL) {
5170 if (!$tableAlias) {
5171 $tableAlias = $this->_aliases[$baseTable];
5172 }
5173 $this->_from = $this->_from = " FROM $baseTable $tableAlias ";
5174 $this->joinGroupTempTable($baseTable, $field, $tableAlias);
5175 $this->_from .= " {$this->_aclFrom} ";
5176 }
5177
5178 /**
5179 * Join the temp table contacting contacts who are members of the filtered groups.
5180 *
5181 * If we are using an IN filter we use an inner join, otherwise a left join.
5182 *
5183 * @param string $baseTable
5184 * @param string $field
5185 * @param string $tableAlias
5186 */
5187 public function joinGroupTempTable($baseTable, $field, $tableAlias) {
5188 if ($this->groupTempTable) {
5189 if ($this->_params['gid_op'] == 'in') {
5190 $this->_from = " FROM $this->groupTempTable group_temp_table INNER JOIN $baseTable $tableAlias
5191 ON group_temp_table.id = $tableAlias.{$field} ";
5192 }
5193 else {
5194 $this->_from .= "
5195 LEFT JOIN $this->groupTempTable group_temp_table
5196 ON $tableAlias.{$field} = group_temp_table.id ";
5197 }
5198 }
5199 }
5200
5201 /**
5202 * Get all labels for fields that are used in a group concat.
5203 *
5204 * @param string $options
5205 * comma separated option values.
5206 * @param string $baoName
5207 * The BAO name for the field.
5208 * @param string $fieldName
5209 * The name of the field for which labels should be retrieved.
5210 *
5211 * return string
5212 */
5213 public function getLabels($options, $baoName, $fieldName) {
5214 $types = explode(',', $options);
5215 $labels = array();
5216 foreach ($types as $value) {
5217 $labels[$value] = CRM_Core_PseudoConstant::getLabel($baoName, $fieldName, $value);
5218 }
5219 return implode(', ', array_filter($labels));
5220 }
5221
5222 /**
5223 * Add statistics columns.
5224 *
5225 * If a group by is in play then add columns for the statistics fields.
5226 *
5227 * This would lead to a new field in the $row such as $fieldName_sum and a new, matching
5228 * column header field.
5229 *
5230 * @param array $field
5231 * @param string $tableName
5232 * @param string $fieldName
5233 * @param array $select
5234 *
5235 * @return array
5236 */
5237 protected function addStatisticsToSelect($field, $tableName, $fieldName, $select) {
5238 foreach ($field['statistics'] as $stat => $label) {
5239 $alias = "{$tableName}_{$fieldName}_{$stat}";
5240 switch (strtolower($stat)) {
5241 case 'max':
5242 case 'sum':
5243 $select[] = "$stat({$field['dbAlias']}) as $alias";
5244 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
5245 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
5246 $this->_statFields[$label] = $alias;
5247 $this->_selectAliases[] = $alias;
5248 break;
5249
5250 case 'count':
5251 $select[] = "COUNT({$field['dbAlias']}) as $alias";
5252 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
5253 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type::T_INT;
5254 $this->_statFields[$label] = $alias;
5255 $this->_selectAliases[] = $alias;
5256 break;
5257
5258 case 'count_distinct':
5259 $select[] = "COUNT(DISTINCT {$field['dbAlias']}) as $alias";
5260 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
5261 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type::T_INT;
5262 $this->_statFields[$label] = $alias;
5263 $this->_selectAliases[] = $alias;
5264 break;
5265
5266 case 'avg':
5267 $select[] = "ROUND(AVG({$field['dbAlias']}),2) as $alias";
5268 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
5269 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
5270 $this->_statFields[$label] = $alias;
5271 $this->_selectAliases[] = $alias;
5272 break;
5273 }
5274 }
5275 return $select;
5276 }
5277
5278 /**
5279 * Add a basic field to the select clause.
5280 *
5281 * @param string $tableName
5282 * @param string $fieldName
5283 * @param array $field
5284 * @param string $select
5285 * @return array
5286 */
5287 protected function addBasicFieldToSelect($tableName, $fieldName, $field, $select) {
5288 $alias = "{$tableName}_{$fieldName}";
5289 $select[] = "{$field['dbAlias']} as $alias";
5290 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
5291 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
5292 $this->_selectAliases[] = $alias;
5293 return $select;
5294 }
5295
5296 /**
5297 * Set table alias.
5298 *
5299 * @param array $table
5300 * @param string $tableName
5301 *
5302 * @return string
5303 * Alias for table.
5304 */
5305 protected function setTableAlias($table, $tableName) {
5306 if (!isset($table['alias'])) {
5307 $this->_columns[$tableName]['alias'] = substr($tableName, 8) .
5308 '_civireport';
5309 }
5310 else {
5311 $this->_columns[$tableName]['alias'] = $table['alias'] . '_civireport';
5312 }
5313
5314 $this->_aliases[$tableName] = $this->_columns[$tableName]['alias'];
5315 return $this->_aliases[$tableName];
5316 }
5317
5318 /**
5319 * Function to add columns to reports.
5320 *
5321 * This is ported from extended reports, which also adds join filters to the options.
5322 *
5323 * @param string $type
5324 * @param array $options
5325 * - prefix - A string to prepend to the table name
5326 * - prefix_label A string to prepend to the fields
5327 * - fields (bool) - should the fields for this table be made available
5328 * - group_by (bool) - should the group bys for this table be made available.
5329 * - order_by (bool) - should the group bys for this table be made available.
5330 * - filters (bool) - should the filters for this table by made available.
5331 * - fields_defaults (array) array of fields that should be displayed by default.
5332 * - filters_defaults (array) array of fields that should be filtered by default.
5333 * - join_filters (array) fields available for filtering joins (requires additional custom code).
5334 * - join_fields (array) fields available from join (requires additional custom code).
5335 * - group_by_defaults (array) array of group bys that should be applied by default.
5336 * - order_by_defaults (array) array of order bys that should be applied by default.
5337 * - custom_fields (array) array of entity types for custom fields (not usually required).
5338 * - contact_type (string) optional restriction on contact type for some tables.
5339 * - fields_excluded (array) fields that are in the generic set for the table but not in the report.
5340 *
5341 * @return array
5342 */
5343 protected function getColumns($type, $options = array()) {
5344 $defaultOptions = array(
5345 'prefix' => '',
5346 'prefix_label' => '',
5347 'fields' => TRUE,
5348 'group_bys' => FALSE,
5349 'order_bys' => TRUE,
5350 'filters' => TRUE,
5351 'join_filters' => FALSE,
5352 'fields_defaults' => array(),
5353 'filters_defaults' => array(),
5354 'group_bys_defaults' => array(),
5355 'order_bys_defaults' => array(),
5356 );
5357 $options = array_merge($defaultOptions, $options);
5358
5359 $fn = 'get' . $type . 'Columns';
5360 return $this->$fn($options);
5361 }
5362
5363 /**
5364 * Get columns for contact table.
5365 *
5366 * @param array $options
5367 *
5368 * @return array
5369 */
5370 protected function getContactColumns($options = array()) {
5371 $defaultOptions = array(
5372 'custom_fields' => array('Individual', 'Contact', 'Organization'),
5373 'fields_defaults' => array('display_name', 'id'),
5374 'order_bys_defaults' => array('sort_name ASC'),
5375 'contact_type' => NULL,
5376 );
5377
5378 $options = array_merge($defaultOptions, $options);
5379
5380 $tableAlias = $options['prefix'] . 'contact';
5381
5382 $spec = array(
5383 $options['prefix'] . 'display_name' => array(
5384 'name' => 'display_name',
5385 'title' => $options['prefix_label'] . ts('Contact Name'),
5386 'is_fields' => TRUE,
5387 ),
5388 $options['prefix'] . 'sort_name' => array(
5389 'name' => 'sort_name',
5390 'title' => $options['prefix_label'] . ts('Contact Name (in sort format)'),
5391 'is_fields' => TRUE,
5392 'is_filters' => TRUE,
5393 'is_order_bys' => TRUE,
5394 ),
5395 $options['prefix'] . 'id' => array(
5396 'name' => 'id',
5397 'title' => $options['prefix_label'] . ts('Contact ID'),
5398 'alter_display' => 'alterContactID',
5399 'type' => CRM_Utils_Type::T_INT,
5400 'is_order_bys' => TRUE,
5401 'is_group_bys' => TRUE,
5402 'is_fields' => TRUE,
5403 'is_filters' => TRUE,
5404 ),
5405 $options['prefix'] . 'external_identifier' => array(
5406 'name' => 'external_identifier',
5407 'title' => $options['prefix_label'] . ts('External ID'),
5408 'type' => CRM_Utils_Type::T_INT,
5409 'is_fields' => TRUE,
5410 ),
5411 $options['prefix'] . 'contact_type' => array(
5412 'title' => $options['prefix_label'] . ts('Contact Type'),
5413 'name' => 'contact_type',
5414 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
5415 'options' => CRM_Contact_BAO_Contact::buildOptions('contact_type'),
5416 'is_fields' => TRUE,
5417 'is_filters' => TRUE,
5418 'is_group_bys' => TRUE,
5419 ),
5420 $options['prefix'] . 'contact_sub_type' => array(
5421 'title' => $options['prefix_label'] . ts('Contact Sub Type'),
5422 'name' => 'contact_sub_type',
5423 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
5424 'options' => CRM_Contact_BAO_Contact::buildOptions('contact_sub_type'),
5425 'is_fields' => TRUE,
5426 'is_filters' => TRUE,
5427 'is_group_bys' => TRUE,
5428 ),
5429 $options['prefix'] . 'is_deleted' => array(
5430 'title' => $options['prefix_label'] . ts('Is deleted'),
5431 'name' => 'is_deleted',
5432 'type' => CRM_Utils_Type::T_BOOLEAN,
5433 'is_fields' => FALSE,
5434 'is_filters' => TRUE,
5435 'is_group_bys' => FALSE,
5436 ),
5437 $options['prefix'] . 'external_identifier' => array(
5438 'title' => $options['prefix_label'] . ts('Contact identifier from external system'),
5439 'name' => 'external_identifier',
5440 'is_fields' => TRUE,
5441 'is_filters' => FALSE,
5442 'is_group_bys' => FALSE,
5443 'is_order_bys' => TRUE,
5444 ),
5445 $options['prefix'] . 'preferred_language' => array(
5446 'title' => $options['prefix_label'] . ts('Preferred Language'),
5447 'name' => 'preferred_language',
5448 'is_fields' => TRUE,
5449 'is_filters' => TRUE,
5450 'is_group_bys' => TRUE,
5451 'is_order_bys' => TRUE,
5452 ),
5453 );
5454 foreach ([
5455 'postal_greeting_display' => 'Postal Greeting',
5456 'email_greeting_display' => 'Email Greeting',
5457 'addressee_display' => 'Addressee',
5458 ] as $field => $title) {
5459 $spec[$options['prefix'] . $field] = array(
5460 'title' => $options['prefix_label'] . ts($title),
5461 'name' => $field,
5462 'is_fields' => TRUE,
5463 'is_filters' => FALSE,
5464 'is_group_bys' => FALSE,
5465 );
5466 }
5467 foreach (['do_not_email', 'do_not_phone', 'do_not_mail', 'do_not_sms', 'is_opt_out'] as $field) {
5468 $spec[$options['prefix'] . $field] = [
5469 'name' => $field,
5470 'type' => CRM_Utils_Type::T_BOOLEAN,
5471 'is_fields' => TRUE,
5472 'is_filters' => TRUE,
5473 'is_group_bys' => FALSE,
5474 ];
5475 }
5476 $individualFields = array(
5477 $options['prefix'] . 'first_name' => array(
5478 'name' => 'first_name',
5479 'title' => $options['prefix_label'] . ts('First Name'),
5480 'is_fields' => TRUE,
5481 'is_filters' => TRUE,
5482 'is_order_bys' => TRUE,
5483 ),
5484 $options['prefix'] . 'middle_name' => array(
5485 'name' => 'middle_name',
5486 'title' => $options['prefix_label'] . ts('Middle Name'),
5487 'is_fields' => TRUE,
5488 ),
5489 $options['prefix'] . 'last_name' => array(
5490 'name' => 'last_name',
5491 'title' => $options['prefix_label'] . ts('Last Name'),
5492 'default_order' => 'ASC',
5493 'is_fields' => TRUE,
5494 ),
5495 $options['prefix'] . 'nick_name' => array(
5496 'name' => 'nick_name',
5497 'title' => $options['prefix_label'] . ts('Nick Name'),
5498 'is_fields' => TRUE,
5499 ),
5500 $options['prefix'] . 'prefix_id' => array(
5501 'name' => 'prefix_id',
5502 'title' => $options['prefix_label'] . ts('Prefix'),
5503 'options' => CRM_Contact_BAO_Contact::buildOptions('prefix_id'),
5504 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
5505 'is_fields' => TRUE,
5506 'is_filters' => TRUE,
5507 ),
5508 $options['prefix'] . 'suffix_id' => array(
5509 'name' => 'suffix_id',
5510 'title' => $options['prefix_label'] . ts('Suffix'),
5511 'options' => CRM_Contact_BAO_Contact::buildOptions('suffix_id'),
5512 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
5513 'is_fields' => TRUE,
5514 'is_filters' => TRUE,
5515 ),
5516 $options['prefix'] . 'gender_id' => array(
5517 'name' => 'gender_id',
5518 'title' => $options['prefix_label'] . ts('Gender'),
5519 'options' => CRM_Contact_BAO_Contact::buildOptions('gender_id'),
5520 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
5521 'is_fields' => TRUE,
5522 'is_filters' => TRUE,
5523 ),
5524 'birth_date' => array(
5525 'title' => $options['prefix_label'] . ts('Birth Date'),
5526 'operatorType' => CRM_Report_Form::OP_DATE,
5527 'type' => CRM_Utils_Type::T_DATE,
5528 'is_fields' => TRUE,
5529 'is_filters' => TRUE,
5530 ),
5531 'age' => array(
5532 'title' => $options['prefix_label'] . ts('Age'),
5533 'dbAlias' => 'TIMESTAMPDIFF(YEAR, ' . $tableAlias . '_civireport.birth_date, CURDATE())',
5534 'type' => CRM_Utils_Type::T_INT,
5535 'is_fields' => TRUE,
5536 ),
5537 $options['prefix'] . 'is_deceased' => array(
5538 'title' => $options['prefix_label'] . ts('Is deceased'),
5539 'name' => 'is_deceased',
5540 'type' => CRM_Utils_Type::T_BOOLEAN,
5541 'is_fields' => FALSE,
5542 'is_filters' => TRUE,
5543 'is_group_bys' => FALSE,
5544 ),
5545 $options['prefix'] . 'job_title' => array(
5546 'name' => 'job_title',
5547 'is_fields' => TRUE,
5548 'is_filters' => FALSE,
5549 'is_group_bys' => FALSE,
5550 ),
5551 $options['prefix'] . 'employer_id' => array(
5552 'title' => $options['prefix_label'] . ts('Current Employer'),
5553 'type' => CRM_Utils_Type::T_INT,
5554 'name' => 'employer_id',
5555 'is_fields' => TRUE,
5556 'is_filters' => FALSE,
5557 'is_group_bys' => TRUE,
5558 ),
5559 );
5560 if (!$options['contact_type'] || $options['contact_type'] === 'Individual') {
5561 $spec = array_merge($spec, $individualFields);
5562 }
5563
5564 if (!empty($options['custom_fields'])) {
5565 $this->_customGroupExtended[$options['prefix'] . 'civicrm_contact'] = array(
5566 'extends' => $options['custom_fields'],
5567 'title' => $options['prefix_label'],
5568 'filters' => $options['filters'],
5569 'prefix' => $options['prefix'],
5570 'prefix_label' => $options['prefix_label'],
5571 );
5572 }
5573
5574 return $this->buildColumns($spec, $options['prefix'] . 'civicrm_contact', 'CRM_Contact_DAO_Contact', $tableAlias, $this->getDefaultsFromOptions($options), $options);
5575 }
5576
5577 /**
5578 * Get address columns to add to array.
5579 *
5580 * @param array $options
5581 * - prefix Prefix to add to table (in case of more than one instance of the table)
5582 * - prefix_label Label to give columns from this address table instance
5583 * - group_bys enable these fields for group by - default false
5584 * - order_bys enable these fields for order by
5585 * - filters enable these fields for filtering
5586 *
5587 * @return array address columns definition
5588 */
5589 protected function getAddressColumns($options = array()) {
5590 $defaultOptions = array(
5591 'prefix' => '',
5592 'prefix_label' => '',
5593 'fields' => TRUE,
5594 'group_bys' => FALSE,
5595 'order_bys' => TRUE,
5596 'filters' => TRUE,
5597 'join_filters' => FALSE,
5598 'fields_defaults' => array(),
5599 'filters_defaults' => array(),
5600 'group_bys_defaults' => array(),
5601 'order_bys_defaults' => array(),
5602 );
5603
5604 $options = array_merge($defaultOptions, $options);
5605 $defaults = $this->getDefaultsFromOptions($options);
5606 $tableAlias = $options['prefix'] . 'address';
5607
5608 $spec = array(
5609 $options['prefix'] . 'name' => array(
5610 'title' => ts($options['prefix_label'] . 'Address Name'),
5611 'name' => 'name',
5612 'is_fields' => TRUE,
5613 ),
5614 $options['prefix'] . 'street_number' => array(
5615 'name' => 'street_number',
5616 'title' => ts($options['prefix_label'] . 'Street Number'),
5617 'type' => 1,
5618 'is_fields' => TRUE,
5619 ),
5620 $options['prefix'] . 'odd_street_number' => array(
5621 'title' => ts('Odd / Even Street Number'),
5622 'name' => 'odd_street_number',
5623 'type' => CRM_Utils_Type::T_INT,
5624 'no_display' => TRUE,
5625 'required' => TRUE,
5626 'dbAlias' => '(address_civireport.street_number % 2)',
5627 'is_fields' => TRUE,
5628 'is_order_bys' => TRUE,
5629 ),
5630 $options['prefix'] . 'street_name' => array(
5631 'name' => 'street_name',
5632 'title' => ts($options['prefix_label'] . 'Street Name'),
5633 'type' => 1,
5634 'is_fields' => TRUE,
5635 'is_filters' => TRUE,
5636 'operator' => 'like',
5637 'is_order_bys' => TRUE,
5638 ),
5639 $options['prefix'] . 'street_address' => array(
5640 'title' => ts($options['prefix_label'] . 'Street Address'),
5641 'name' => 'street_address',
5642 'is_fields' => TRUE,
5643 'is_filters' => TRUE,
5644 'is_group_bys' => TRUE,
5645 ),
5646 $options['prefix'] . 'supplemental_address_1' => array(
5647 'title' => ts($options['prefix_label'] . 'Supplementary Address Field 1'),
5648 'name' => 'supplemental_address_1',
5649 'is_fields' => TRUE,
5650 ),
5651 $options['prefix'] . 'supplemental_address_2' => array(
5652 'title' => ts($options['prefix_label'] . 'Supplementary Address Field 2'),
5653 'name' => 'supplemental_address_2',
5654 'is_fields' => TRUE,
5655 ),
5656 $options['prefix'] . 'supplemental_address_3' => array(
5657 'title' => ts($options['prefix_label'] . 'Supplementary Address Field 3'),
5658 'name' => 'supplemental_address_3',
5659 'is_fields' => TRUE,
5660 ),
5661 $options['prefix'] . 'street_number' => array(
5662 'name' => 'street_number',
5663 'title' => ts($options['prefix_label'] . 'Street Number'),
5664 'type' => 1,
5665 'is_order_bys' => TRUE,
5666 'is_filters' => TRUE,
5667 'is_fields' => TRUE,
5668 ),
5669 $options['prefix'] . 'street_unit' => array(
5670 'name' => 'street_unit',
5671 'title' => ts($options['prefix_label'] . 'Street Unit'),
5672 'type' => 1,
5673 'is_fields' => TRUE,
5674 ),
5675 $options['prefix'] . 'city' => array(
5676 'title' => ts($options['prefix_label'] . 'City'),
5677 'name' => 'city',
5678 'operator' => 'like',
5679 'is_fields' => TRUE,
5680 'is_filters' => TRUE,
5681 'is_group_bys' => TRUE,
5682 'is_order_bys' => TRUE,
5683 ),
5684 $options['prefix'] . 'postal_code' => array(
5685 'title' => ts($options['prefix_label'] . 'Postal Code'),
5686 'name' => 'postal_code',
5687 'type' => 1,
5688 'is_fields' => TRUE,
5689 'is_filters' => TRUE,
5690 'is_group_bys' => TRUE,
5691 'is_order_bys' => TRUE,
5692 ),
5693 $options['prefix'] . 'postal_code_suffix' => array(
5694 'title' => ts($options['prefix_label'] . 'Postal Code Suffix'),
5695 'name' => 'postal_code',
5696 'type' => 1,
5697 'is_fields' => TRUE,
5698 'is_filters' => TRUE,
5699 'is_group_bys' => TRUE,
5700 'is_order_bys' => TRUE,
5701 ),
5702 $options['prefix'] . 'county_id' => array(
5703 'title' => ts($options['prefix_label'] . 'County'),
5704 'alter_display' => 'alterCountyID',
5705 'name' => 'county_id',
5706 'type' => CRM_Utils_Type::T_INT,
5707 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
5708 'options' => CRM_Core_PseudoConstant::county(),
5709 'is_fields' => TRUE,
5710 'is_filters' => TRUE,
5711 'is_group_bys' => TRUE,
5712 ),
5713 $options['prefix'] . 'state_province_id' => array(
5714 'title' => ts($options['prefix_label'] . 'State/Province'),
5715 'alter_display' => 'alterStateProvinceID',
5716 'name' => 'state_province_id',
5717 'type' => CRM_Utils_Type::T_INT,
5718 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
5719 'options' => CRM_Core_PseudoConstant::stateProvince(),
5720 'is_fields' => TRUE,
5721 'is_filters' => TRUE,
5722 'is_group_bys' => TRUE,
5723 ),
5724 $options['prefix'] . 'country_id' => array(
5725 'title' => ts($options['prefix_label'] . 'Country'),
5726 'alter_display' => 'alterCountryID',
5727 'name' => 'country_id',
5728 'is_fields' => TRUE,
5729 'is_filters' => TRUE,
5730 'is_group_bys' => TRUE,
5731 'type' => CRM_Utils_Type::T_INT,
5732 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
5733 'options' => CRM_Core_PseudoConstant::country(),
5734 ),
5735 $options['prefix'] . 'location_type_id' => array(
5736 'name' => 'is_primary',
5737 'title' => ts($options['prefix_label'] . 'Location Type'),
5738 'type' => CRM_Utils_Type::T_INT,
5739 'is_fields' => TRUE,
5740 'alter_display' => 'alterLocationTypeID',
5741 ),
5742 $options['prefix'] . 'id' => array(
5743 'title' => ts($options['prefix_label'] . 'ID'),
5744 'name' => 'id',
5745 'is_fields' => TRUE,
5746 ),
5747 $options['prefix'] . 'is_primary' => array(
5748 'name' => 'is_primary',
5749 'title' => ts($options['prefix_label'] . 'Primary Address?'),
5750 'type' => CRM_Utils_Type::T_BOOLEAN,
5751 'is_fields' => TRUE,
5752 ),
5753 );
5754 return $this->buildColumns($spec, $options['prefix'] . 'civicrm_address', 'CRM_Core_DAO_Address', $tableAlias, $defaults, $options);
5755 }
5756
5757 /**
5758 * Build the columns.
5759 *
5760 * The normal report class needs you to remember to do a few things that are often erratic
5761 *
5762 * 1) use a unique key for any field that might not be unique (e.g. start date, label)
5763 * - this class will prepend an alias to the key & set the 'name' if you don't set it yourself.
5764 * You can suppress the alias with 'no_field_disambiguation' if transitioning existing reports. This
5765 * means any saved filters / fields on saved report instances. This will mean that matching names from
5766 * different tables may be ambigious, but it will smooth any code transition.
5767 * - note that it assumes the value being passed in is the actual table field name
5768 *
5769 * 2) set the field & set it to no display if you don't want the field but you might want to use the field in other
5770 * contexts - the code looks up the fields array for data - so it both defines the field spec & the fields you want to show
5771 *
5772 * 3) this function also sets the 'metadata' array - the extended report class now uses this in place
5773 * of the fields array to reduce the issues caused when metadata is needed but 'fields' are not defined. Code in
5774 * the core classes can start to move towards that.
5775 *
5776 * @param array $specs
5777 * @param string $tableName
5778 * @param string $daoName
5779 * @param string $tableAlias
5780 * @param array $defaults
5781 * @param array $options
5782 *
5783 * @return array
5784 */
5785 protected function buildColumns($specs, $tableName, $daoName = NULL, $tableAlias = NULL, $defaults = array(), $options = array()) {
5786 if (!$tableAlias) {
5787 $tableAlias = str_replace('civicrm_', '', $tableName);
5788 }
5789 $types = array('filters', 'group_bys', 'order_bys', 'join_filters');
5790 $columns = array($tableName => array_fill_keys($types, array()));
5791 // The code that uses this no longer cares if it is a DAO or BAO so just call it a DAO.
5792 $columns[$tableName]['dao'] = $daoName;
5793 $columns[$tableName]['alias'] = $tableAlias;
5794
5795 foreach ($specs as $specName => $spec) {
5796 if (empty($spec['name'])) {
5797 $spec['name'] = $specName;
5798 }
5799
5800 $fieldAlias = (empty($options['no_field_disambiguation']) ? $tableAlias . '_' : '') . $specName;
5801 $columns[$tableName]['metadata'][$fieldAlias] = $spec;
5802 $columns[$tableName]['fields'][$fieldAlias] = $spec;
5803 if (isset($defaults['fields_defaults']) && in_array($spec['name'], $defaults['fields_defaults'])) {
5804 $columns[$tableName]['fields'][$fieldAlias]['default'] = TRUE;
5805 }
5806
5807 if (!$spec['is_fields'] || (isset($options['fields_excluded']) && in_array($specName, $options['fields_excluded']))) {
5808 $columns[$tableName]['fields'][$fieldAlias]['no_display'] = TRUE;
5809 }
5810
5811 if (isset($options['fields_required']) && in_array($specName, $options['fields_required'])) {
5812 $columns[$tableName]['fields'][$fieldAlias]['required'] = TRUE;
5813 }
5814
5815 foreach ($types as $type) {
5816 if ($options[$type] && !empty($spec['is_' . $type])) {
5817 $columns[$tableName][$type][$fieldAlias] = $spec;
5818 if (isset($defaults[$type . '_defaults']) && isset($defaults[$type . '_defaults'][$spec['name']])) {
5819 $columns[$tableName][$type][$fieldAlias]['default'] = $defaults[$type . '_defaults'][$spec['name']];
5820 }
5821 }
5822 }
5823 }
5824 return $columns;
5825 }
5826
5827 /**
5828 * Store group bys into array - so we can check elsewhere what is grouped.
5829 */
5830 protected function storeGroupByArray() {
5831
5832 if (!CRM_Utils_Array::value('group_bys', $this->_params)
5833 || !is_array($this->_params['group_bys'])) {
5834 $this->_params['group_bys'] = [];
5835 }
5836
5837 foreach ($this->_columns as $tableName => $table) {
5838 $table = $this->_columns[$tableName];
5839 if (array_key_exists('group_bys', $table)) {
5840 foreach ($table['group_bys'] as $fieldName => $fieldData) {
5841 $field = $this->_columns[$tableName]['metadata'][$fieldName];
5842 if (!empty($this->_params['group_bys'][$fieldName]) || !empty($fieldData['required'])) {
5843 if (!empty($field['chart'])) {
5844 $this->assign('chartSupported', TRUE);
5845 }
5846
5847 if (!empty($table['group_bys'][$fieldName]['frequency']) &&
5848 !empty($this->_params['group_bys_freq'][$fieldName])
5849 ) {
5850
5851 switch ($this->_params['group_bys_freq'][$fieldName]) {
5852 case 'FISCALYEAR':
5853 $this->_groupByArray[$tableName . '_' . $fieldName . '_start'] = self::fiscalYearOffset($field['dbAlias']);
5854
5855 case 'YEAR':
5856 $this->_groupByArray[$tableName . '_' . $fieldName . '_start'] = " {$this->_params['group_bys_freq'][$fieldName]}({$field['dbAlias']})";
5857
5858 default:
5859 $this->_groupByArray[$tableName . '_' . $fieldName . '_start'] = "EXTRACT(YEAR_{$this->_params['group_bys_freq'][$fieldName]} FROM {$field['dbAlias']})";
5860
5861 }
5862 }
5863 else {
5864 if (!in_array($field['dbAlias'], $this->_groupByArray)) {
5865 $this->_groupByArray[$tableName . '_' . $fieldName] = $field['dbAlias'];
5866 }
5867 }
5868 }
5869 }
5870
5871 }
5872 }
5873 }
5874
5875 /**
5876 * @param $options
5877 *
5878 * @return array
5879 */
5880 protected function getDefaultsFromOptions($options) {
5881 $defaults = array(
5882 'fields_defaults' => $options['fields_defaults'],
5883 'filters_defaults' => $options['filters_defaults'],
5884 'group_bys_defaults' => $options['group_bys_defaults'],
5885 'order_bys_defaults' => $options['order_bys_defaults'],
5886 );
5887 return $defaults;
5888 }
5889
5890 /**
5891 * Get the select clause for a field, wrapping in GROUP_CONCAT if appropriate.
5892 *
5893 * Full group by mode dictates that a field must either be in the group by function or
5894 * wrapped in a aggregate function. Here we wrap the field in GROUP_CONCAT if it is not in the
5895 * group concat.
5896 *
5897 * @param string $tableName
5898 * @param string $fieldName
5899 * @param string $field
5900 * @return string
5901 */
5902 protected function getSelectClauseWithGroupConcatIfNotGroupedBy($tableName, &$fieldName, &$field) {
5903 if ($this->groupConcatTested && (!empty($this->_groupByArray) || $this->isForceGroupBy)) {
5904 if ((empty($field['statistics']) || in_array('GROUP_CONCAT', $field['statistics']))) {
5905 $label = CRM_Utils_Array::value('title', $field);
5906 $alias = "{$tableName}_{$fieldName}";
5907 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $label;
5908 $this->_selectAliases[] = $alias;
5909 if (empty($this->_groupByArray[$tableName . '_' . $fieldName])) {
5910 return "GROUP_CONCAT(DISTINCT {$field['dbAlias']}) as $alias";
5911 }
5912 return "({$field['dbAlias']}) as $alias";
5913 }
5914 }
5915 }
5916
5917 /**
5918 * Generate clause for the selected filter.
5919 *
5920 * @param array $field
5921 * Field specification
5922 * @param string $fieldName
5923 * Field name.
5924 *
5925 * @return string
5926 * Relevant where clause.
5927 */
5928 protected function generateFilterClause($field, $fieldName) {
5929 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
5930 if (CRM_Utils_Array::value('operatorType', $field) ==
5931 CRM_Report_Form::OP_MONTH
5932 ) {
5933 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
5934 $value = CRM_Utils_Array::value("{$fieldName}_value", $this->_params);
5935 if (is_array($value) && !empty($value)) {
5936 return "(month({$field['dbAlias']}) $op (" . implode(', ', $value) .
5937 '))';
5938 }
5939 }
5940 else {
5941 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
5942 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
5943 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
5944 $fromTime = CRM_Utils_Array::value("{$fieldName}_from_time", $this->_params);
5945 $toTime = CRM_Utils_Array::value("{$fieldName}_to_time", $this->_params);
5946 return $this->dateClause($field['dbAlias'], $relative, $from, $to, $field['type'], $fromTime, $toTime);
5947 }
5948 }
5949 else {
5950 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
5951 if ($op) {
5952 return $this->whereClause($field,
5953 $op,
5954 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
5955 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
5956 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
5957 );
5958 }
5959 }
5960 return '';
5961 }
5962
5963 }