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