3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Class CRM_Report_Form
15 class CRM_Report_Form
extends CRM_Core_Form
{
16 const ROW_COUNT_LIMIT
= 50;
19 * Operator types - used for displaying filter elements
29 OP_MULTISELECT_SEPARATOR
= 66,
34 * The id of the report instance
41 * The id of the report template
45 protected $_templateID;
53 protected $_noFields = FALSE;
56 * The set of all columns in the report. An associative array
57 * with column name as the key and attributes as the value
61 protected $_columns = [];
64 * The set of filters in the report
68 protected $_filters = [];
71 * The set of optional columns in the report
75 public $_options = [];
78 * By default most reports hide contact id.
79 * Setting this to true makes it available
82 protected $_exposeContactID = TRUE;
85 * Set of statistic fields
89 protected $_statFields = [];
92 * Set of statistics data
96 protected $_statistics = [];
99 * List of fields not to be repeated during display
103 protected $_noRepeats = [];
106 * List of fields not to be displayed
110 protected $_noDisplay = [];
113 * Object type that a custom group extends
117 protected $_customGroupExtends = NULL;
118 protected $_customGroupExtendsJoin = [];
119 protected $_customGroupFilters = TRUE;
120 protected $_customGroupGroupBy = FALSE;
121 protected $_customGroupJoin = 'LEFT JOIN';
127 protected $_tagFilter = FALSE;
130 * specify entity table for tags filter
133 protected $_tagFilterTable = 'civicrm_contact';
136 * Build groups filter.
140 protected $_groupFilter = FALSE;
143 * Has the report been optimised for group filtering.
145 * The functionality for group filtering has been improved but not
146 * all reports have been adjusted to take care of it.
148 * This property exists to highlight the reports which are still using the
149 * slow method & allow group filtering to still work for them until they
152 * In order to protect extensions we have to default to TRUE - but I have
153 * separately marked every class with a groupFilter in the hope that will trigger
154 * people to fix them as they touch them.
157 * @see https://issues.civicrm.org/jira/browse/CRM-19170
159 protected $groupFilterNotOptimised = TRUE;
166 public $_navigation = [];
168 public $_drilldownReport = [];
171 * Array of tabs to display on report.
173 * E.g we define the tab title, the tpl and the tab-specific part of the css or html link.
175 * $this->tabs['OrderBy'] = array(
176 * 'title' => ts('Sorting'),
177 * 'tpl' => 'OrderBy',
178 * 'div_label' => 'order-by',
183 protected $tabs = [];
186 * Should we add paging.
190 protected $addPaging = TRUE;
192 protected $isForceGroupBy = FALSE;
194 protected $groupConcatTested = FALSE;
197 * Are we in print mode. Can be set by another outputMode, ex: sendmail.
201 public $printOnly = FALSE;
204 * An attribute for checkbox/radio form field layout
208 protected $_fourColumnAttribute = [
209 '</td><td width="25%">',
210 '</td><td width="25%">',
211 '</td><td width="25%">',
215 protected $_force = 1;
217 protected $_params = NULL;
218 protected $_formValues = NULL;
219 protected $_instanceValues = NULL;
221 protected $_instanceForm = FALSE;
222 protected $_criteriaForm = FALSE;
224 protected $_instanceButtonName = NULL;
225 protected $_createNewButtonName = NULL;
226 protected $_groupButtonName = NULL;
227 protected $_chartButtonName = NULL;
228 protected $_csvSupported = TRUE;
229 protected $_add2groupSupported = TRUE;
230 protected $_groups = NULL;
231 protected $_grandFlag = FALSE;
232 protected $_rowsFound = NULL;
233 protected $_selectAliases = [];
234 protected $_rollup = NULL;
237 * Table containing list of contact IDs within the group filter.
241 protected $groupTempTable = '';
246 protected $_aliases = [];
262 protected $_limit = NULL;
265 * This can be set to specify a limit to the number of rows
266 * Since it is currently envisaged as part of the api usage it is only being applied
267 * when $_output mode is not 'html' or 'group' so as not to have to interpret / mess with that part
268 * of the code (see limit() fn.
272 protected $_limitValue = NULL;
275 * This can be set to specify row offset
276 * See notes on _limitValue
280 protected $_offsetValue = NULL;
284 protected $_sections = NULL;
285 protected $_autoIncludeIndexedFieldsAsOrderBys = 0;
288 * Whether to generate absolute URLs (ex: in report emails).
292 public $_absoluteUrl = FALSE;
295 * Flag to indicate if result-set is to be stored in a class variable which could be retrieved using getResultSet() method.
299 protected $_storeResultSet = FALSE;
302 * When _storeResultSet Flag is set use this var to store result set in form of array
306 protected $_resultSet = [];
309 * To what frequency group-by a date column
313 protected $_groupByDateFreq = [
315 'YEARWEEK' => 'Week',
316 'QUARTER' => 'Quarter',
321 * Variables to hold the acl inner join and where clause
324 protected $_aclFrom = NULL;
325 protected $_aclWhere = NULL;
328 * Array of DAO tables having columns included in SELECT or ORDER BY clause.
330 * Where has also been added to this although perhaps the 'includes both' array should have a different name.
334 protected $_selectedTables = [];
337 * Array of DAO tables having columns included in WHERE or HAVING clause
341 protected $filteredTables;
344 * Output mode e.g 'print', 'csv', 'pdf'.
348 protected $_outputMode;
351 * Format of any chart in use.
353 * (it's unclear if this could be merged with outputMode at this stage)
359 public $_having = NULL;
360 public $_select = NULL;
361 public $_selectClauses = [];
362 public $_columnHeaders = [];
363 public $_orderBy = NULL;
364 public $_orderByFields = [];
365 public $_orderByArray = [];
367 * Array of clauses to group by.
371 protected $_groupByArray = [];
372 public $_groupBy = NULL;
373 public $_whereClauses = [];
374 public $_havingClauses = [];
377 * DashBoardRowCount Dashboard row count.
381 public $_dashBoardRowCount;
384 * Is this being called without a form controller (ie. the report is being render outside the normal form
385 * - e.g the api is retrieving the rows.
389 public $noController = FALSE;
392 * Variable to hold the currency alias.
396 protected $_currencyColumn = NULL;
401 protected $_interval;
406 protected $_sendmail;
419 * Report description.
423 public $_description;
426 * Is an address field selected.
429 * This was intended to determine if the address table should be joined in
430 * The isTableSelected function is now preferred for this purpose
432 protected $_addressField;
435 * Is an email field selected.
438 * This was intended to determine if the email table should be joined in
439 * The isTableSelected function is now preferred for this purpose
441 protected $_emailField;
444 * Is a phone field selected.
447 * This was intended to determine if the phone table should be joined in
448 * The isTableSelected function is now preferred for this purpose
450 protected $_phoneField;
453 * Create new report instance? (or update existing) on save.
457 protected $_createNew;
460 * When a grand total row has calculated the status we pop it off to here.
462 * This allows us to access it from the stats function and avoid recalculating.
466 protected $rollupRow = [];
469 * Database attributes - character set and collation.
473 protected $_databaseAttributes = ' DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci';
476 * SQL being run in this report.
478 * The sql in the report is stored in this variable in order to be displayed on the developer tab.
486 * An instruction not to add a Group By.
488 * This is relevant where the group by might be otherwise added after the code that determines the group by array.
490 * e.g. where stat fields are being added but other settings cause it to not be desirable to add a group by
491 * such as in pivot charts when no row header is set
495 protected $noGroupBy = FALSE;
498 * SQL being run in this report as an array.
500 * The sql in the report is stored in this variable in order to be returned to api & test calls.
508 * Tables created for the report that need removal afterwards.
510 * ['civicrm_temp_report_x' => ['temporary' => TRUE, 'name' => 'civicrm_temp_report_x']
513 protected $temporaryTables = [];
516 * Can this report use the sql mode ONLY_FULL_GROUP_BY.
519 public $optimisedForOnlyFullGroupBy = TRUE;
524 public function __construct() {
525 parent
::__construct();
527 $this->addClass('crm-report-form');
529 if ($this->_tagFilter
) {
530 $this->buildTagFilter();
532 if ($this->_exposeContactID
) {
533 if (array_key_exists('civicrm_contact', $this->_columns
)) {
534 $this->_columns
['civicrm_contact']['fields']['exposed_id'] = [
536 'title' => ts('Contact ID'),
542 if ($this->_groupFilter
) {
543 $this->buildGroupFilter();
546 // Get all custom groups
547 $allGroups = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_CustomField', 'custom_group_id');
549 // Get the custom groupIds for which the user has VIEW permission
550 // If the user has 'access all custom data' permission, we'll leave $permCustomGroupIds empty
551 // and addCustomDataToColumns() will allow access to all custom groups.
552 $permCustomGroupIds = [];
553 if (!CRM_Core_Permission
::check('access all custom data')) {
554 $permCustomGroupIds = CRM_ACL_API
::group(CRM_Core_Permission
::VIEW
, NULL, 'civicrm_custom_group', $allGroups, NULL);
555 // do not allow custom data for reports if user doesn't have
556 // permission to access custom data.
557 if (!empty($this->_customGroupExtends
) && empty($permCustomGroupIds)) {
558 $this->_customGroupExtends
= [];
562 // merge custom data columns to _columns list, if any
563 $this->addCustomDataToColumns(TRUE, $permCustomGroupIds);
565 // add / modify display columns, filters ..etc
566 CRM_Utils_Hook
::alterReportVar('columns', $this->_columns
, $this);
568 //assign currencyColumn variable to tpl
569 $this->assign('currencyColumn', $this->_currencyColumn
);
573 * Shared pre-process function.
575 * If overriding preProcess function this should still be called.
579 public function preProcessCommon() {
580 $this->_force
= CRM_Utils_Request
::retrieve('force', 'Boolean');
582 $this->_dashBoardRowCount
= CRM_Utils_Request
::retrieve('rowCount', 'Integer');
584 $this->_section
= CRM_Utils_Request
::retrieve('section', 'Integer');
586 $this->assign('section', $this->_section
);
587 CRM_Core_Region
::instance('page-header')->add([
588 'markup' => sprintf('<!-- Report class: [%s] -->', htmlentities(get_class($this))),
590 if (!$this->noController
) {
591 $this->setID($this->get('instanceId'));
594 $this->setID(CRM_Report_Utils_Report
::getInstanceID());
596 $this->setID(CRM_Report_Utils_Report
::getInstanceIDForPath());
600 // set qfkey so that pager picks it up and use it in the "Next > Last >>" links.
601 // FIXME: Note setting it in $_GET doesn't work, since pager generates link based on QUERY_STRING
602 $_SERVER['QUERY_STRING'] .= "&qfKey={$this->controller->_key}";
606 $this->assign('instanceId', $this->_id
);
607 $params = ['id' => $this->_id
];
608 $this->_instanceValues
= [];
609 CRM_Core_DAO
::commonRetrieve('CRM_Report_DAO_ReportInstance',
611 $this->_instanceValues
613 if (empty($this->_instanceValues
)) {
614 CRM_Core_Error
::statusBounce("Report could not be loaded.");
616 $this->_title
= $this->_instanceValues
['title'];
617 if (!empty($this->_instanceValues
['permission']) &&
618 (!(CRM_Core_Permission
::check($this->_instanceValues
['permission']) ||
619 CRM_Core_Permission
::check('administer Reports')
622 CRM_Utils_System
::permissionDenied();
623 CRM_Utils_System
::civiExit();
626 $formValues = $this->_instanceValues
['form_values'] ??
NULL;
628 $this->_formValues
= CRM_Utils_String
::unserialize($formValues);
631 $this->_formValues
= NULL;
634 $this->setOutputMode();
636 if ($this->_outputMode
== 'copy') {
637 $this->_createNew
= TRUE;
638 $this->_params
= $this->_formValues
;
639 $this->_params
['view_mode'] = 'criteria';
640 $this->_params
['title'] = $this->getTitle() . ts(' (copy created by %1 on %2)', [
641 CRM_Core_Session
::singleton()->getLoggedInContactDisplayName(),
642 CRM_Utils_Date
::customFormat(date('Y-m-d H:i')),
644 // Do not pass go. Do not collect another chance to re-run the same query.
645 CRM_Report_Form_Instance
::postProcess($this);
648 // lets always do a force if reset is found in the url.
649 // Hey why not? see CRM-17225 for more about this. The use of reset to be force is historical for reasons stated
650 // in the comment line above these 2.
651 if (!empty($_REQUEST['reset'])
652 && !in_array(CRM_Utils_Request
::retrieve('output', 'String'), ['save', 'criteria'])) {
657 $this->assign('mode', 'instance');
659 elseif (!$this->noController
) {
660 list($optionValueID, $optionValue) = CRM_Report_Utils_Report
::getValueIDFromUrl();
661 $instanceCount = CRM_Report_Utils_Report
::getInstanceCount($optionValue);
662 if (($instanceCount > 0) && $optionValueID) {
663 $this->assign('instanceUrl',
664 CRM_Utils_System
::url('civicrm/report/list',
665 "reset=1&ovid=$optionValueID"
669 if ($optionValueID) {
670 $this->_description
= CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', $optionValueID, 'description');
674 $this->assign('mode', 'template');
677 // lets display the Report Settings section
678 $this->_instanceForm
= $this->_force ||
$this->_id ||
(!empty($_POST));
680 // Do not display Report Settings section if administer Reports permission is absent OR
681 // if report instance is reserved and administer reserved reports absent
682 if (!CRM_Core_Permission
::check('administer Reports') ||
683 (!empty($this->_instanceValues
['is_reserved']) &&
684 !CRM_Core_Permission
::check('administer reserved reports'))
686 $this->_instanceForm
= FALSE;
689 $this->assign('criteriaForm', FALSE);
690 // Display Report Criteria section if user has access Report Criteria OR administer Reports AND report instance is not reserved
691 if (CRM_Core_Permission
::check('administer Reports') ||
692 CRM_Core_Permission
::check('access Report Criteria')
694 if (empty($this->_instanceValues
['is_reserved']) ||
695 CRM_Core_Permission
::check('administer reserved reports')
697 $this->assign('criteriaForm', TRUE);
698 $this->_criteriaForm
= TRUE;
702 // Special permissions check for private instance if it's not the current contact instance
704 (CRM_Report_BAO_ReportInstance
::reportIsPrivate($this->_id
) &&
705 !CRM_Report_BAO_ReportInstance
::contactIsOwner($this->_id
))) {
706 if (!CRM_Core_Permission
::check('access all private reports')) {
707 $this->_instanceForm
= FALSE;
708 $this->assign('criteriaForm', FALSE);
712 $this->_instanceButtonName
= $this->getButtonName('submit', 'save');
713 $this->_createNewButtonName
= $this->getButtonName('submit', 'next');
714 $this->_groupButtonName
= $this->getButtonName('submit', 'group');
715 $this->_chartButtonName
= $this->getButtonName('submit', 'chart');
721 public function addBreadCrumb() {
725 'title' => ts('Report Templates'),
726 'url' => CRM_Utils_System
::url('civicrm/admin/report/template/list', 'reset=1'),
730 CRM_Utils_System
::appendBreadCrumb($breadCrumbs);
734 * Pre process function.
736 * Called prior to build form.
738 public function preProcess() {
739 $this->preProcessCommon();
742 $this->addBreadCrumb();
745 foreach ($this->_columns
as $tableName => $table) {
746 $this->setTableAlias($table, $tableName);
749 // higher preference to bao object
750 $daoOrBaoName = CRM_Utils_Array
::value('bao', $table, CRM_Utils_Array
::value('dao', $table));
753 if (method_exists($daoOrBaoName, 'exportableFields')) {
754 $expFields = $daoOrBaoName::exportableFields();
757 $expFields = $daoOrBaoName::export();
761 $doNotCopy = ['required', 'default'];
763 $fieldGroups = ['fields', 'filters', 'group_bys', 'order_bys'];
764 foreach ($fieldGroups as $fieldGrp) {
765 if (!empty($table[$fieldGrp]) && is_array($table[$fieldGrp])) {
766 foreach ($table[$fieldGrp] as $fieldName => $field) {
767 // $name is the field name used to reference the BAO/DAO export fields array
768 $name = $field['name'] ??
$fieldName;
770 // Sometimes the field name key in the BAO/DAO export fields array is
771 // different from the actual database field name.
772 // Unset $field['name'] so that actual database field name can be obtained
773 // from the BAO/DAO export fields array.
774 unset($field['name']);
776 if (array_key_exists($name, $expFields)) {
777 foreach ($doNotCopy as $dnc) {
778 // unset the values we don't want to be copied.
779 unset($expFields[$name][$dnc]);
782 $this->_columns
[$tableName][$fieldGrp][$fieldName] = $expFields[$name];
785 foreach ($expFields[$name] as $property => $val) {
786 if (!array_key_exists($property, $field)) {
787 $this->_columns
[$tableName][$fieldGrp][$fieldName][$property] = $val;
794 if (!empty($field['no_repeat'])) {
795 $this->_noRepeats
[] = "{$tableName}_{$fieldName}";
797 if (!empty($field['no_display'])) {
798 $this->_noDisplay
[] = "{$tableName}_{$fieldName}";
801 // set alias = table-name, unless already set
802 $alias = $field['alias'] ??
(
803 $this->_columns
[$tableName]['alias'] ??
$tableName
805 $this->_columns
[$tableName][$fieldGrp][$fieldName]['alias'] = $alias;
807 // set name = fieldName, unless already set
808 if (!isset($this->_columns
[$tableName][$fieldGrp][$fieldName]['name'])) {
809 $this->_columns
[$tableName][$fieldGrp][$fieldName]['name'] = $name;
812 if (!isset($this->_columns
[$tableName][$fieldGrp][$fieldName]['table_name'])) {
813 $this->_columns
[$tableName][$fieldGrp][$fieldName]['table_name'] = $tableName;
816 // set dbAlias = alias.name, unless already set
817 if (!isset($this->_columns
[$tableName][$fieldGrp][$fieldName]['dbAlias'])) {
818 $this->_columns
[$tableName][$fieldGrp][$fieldName]['dbAlias']
820 $this->_columns
[$tableName][$fieldGrp][$fieldName]['name'];
823 // a few auto fills for filters
824 if ($fieldGrp == 'filters') {
825 // fill operator types
826 if (!array_key_exists('operatorType', $this->_columns
[$tableName][$fieldGrp][$fieldName])) {
827 switch (CRM_Utils_Array
::value('type', $this->_columns
[$tableName][$fieldGrp][$fieldName])) {
828 case CRM_Utils_Type
::T_MONEY
:
829 case CRM_Utils_Type
::T_FLOAT
:
830 $this->_columns
[$tableName][$fieldGrp][$fieldName]['operatorType'] = CRM_Report_Form
::OP_FLOAT
;
833 case CRM_Utils_Type
::T_INT
:
834 $this->_columns
[$tableName][$fieldGrp][$fieldName]['operatorType'] = CRM_Report_Form
::OP_INT
;
837 case CRM_Utils_Type
::T_DATE
:
838 $this->_columns
[$tableName][$fieldGrp][$fieldName]['operatorType'] = CRM_Report_Form
::OP_DATE
;
841 case CRM_Utils_Type
::T_BOOLEAN
:
842 $this->_columns
[$tableName][$fieldGrp][$fieldName]['operatorType'] = CRM_Report_Form
::OP_SELECT
;
843 if (!array_key_exists('options', $this->_columns
[$tableName][$fieldGrp][$fieldName])) {
844 $this->_columns
[$tableName][$fieldGrp][$fieldName]['options']
855 array_key_exists('pseudoconstant', $this->_columns
[$tableName][$fieldGrp][$fieldName])
857 // with multiple options operator-type is generally multi-select
858 $this->_columns
[$tableName][$fieldGrp][$fieldName]['operatorType'] = CRM_Report_Form
::OP_MULTISELECT
;
859 if (!array_key_exists('options', $this->_columns
[$tableName][$fieldGrp][$fieldName])) {
861 $this->_columns
[$tableName][$fieldGrp][$fieldName]['options'] = CRM_Core_PseudoConstant
::get($daoOrBaoName, $fieldName);
868 if (!isset($this->_columns
[$tableName]['metadata'][$fieldName])) {
869 $this->_columns
[$tableName]['metadata'][$fieldName] = $this->_columns
[$tableName][$fieldGrp][$fieldName];
872 $this->_columns
[$tableName]['metadata'][$fieldName] = array_merge($this->_columns
[$tableName][$fieldGrp][$fieldName], $this->_columns
[$tableName]['metadata'][$fieldName]);
878 // copy filters to a separate handy variable
879 if (array_key_exists('filters', $table)) {
880 $this->_filters
[$tableName] = $this->_columns
[$tableName]['filters'];
883 if (array_key_exists('group_bys', $table)) {
884 $groupBys[$tableName] = $this->_columns
[$tableName]['group_bys'];
887 if (array_key_exists('fields', $table)) {
888 $reportFields[$tableName] = $this->_columns
[$tableName]['fields'];
893 $this->setDefaultValues(FALSE);
896 CRM_Report_Utils_Get
::processFilter($this->_filters
, $this->_defaults
);
897 CRM_Report_Utils_Get
::processGroupBy($groupBys, $this->_defaults
);
898 CRM_Report_Utils_Get
::processFields($reportFields, $this->_defaults
);
899 CRM_Report_Utils_Get
::processChart($this->_defaults
);
902 $this->_formValues
= $this->_defaults
;
903 $this->postProcess();
908 * Set default values.
910 * @param bool $freeze
914 public function setDefaultValues($freeze = TRUE) {
917 // FIXME: generalizing form field naming conventions would reduce
918 // Lots of lines below.
919 foreach ($this->_columns
as $tableName => $table) {
920 if (array_key_exists('fields', $table)) {
921 foreach ($table['fields'] as $fieldName => $field) {
922 if (empty($field['no_display'])) {
923 if (!empty($field['required'])) {
925 $this->_defaults
['fields'][$fieldName] = 1;
928 // find element object, so that we could use quickform's freeze method
929 // for required elements
930 $obj = $this->getElementFromGroup("fields", $fieldName);
932 $freezeGroup[] = $obj;
936 elseif (isset($field['default'])) {
937 $this->_defaults
['fields'][$fieldName] = $field['default'];
943 if (array_key_exists('group_bys', $table)) {
944 foreach ($table['group_bys'] as $fieldName => $field) {
945 if (isset($field['default'])) {
946 if (!empty($field['frequency'])) {
947 $this->_defaults
['group_bys_freq'][$fieldName] = 'MONTH';
949 $this->_defaults
['group_bys'][$fieldName] = $field['default'];
953 if (array_key_exists('filters', $table)) {
954 foreach ($table['filters'] as $fieldName => $field) {
955 if (isset($field['default'])) {
956 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
958 if (is_array($field['default'])) {
959 $this->_defaults
["{$fieldName}_from"] = $field['default']['from'] ??
NULL;
960 $this->_defaults
["{$fieldName}_to"] = $field['default']['to'] ??
NULL;
961 $this->_defaults
["{$fieldName}_relative"] = 0;
964 $this->_defaults
["{$fieldName}_relative"] = $field['default'];
968 if ((CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_INT
) && is_array($field['default'])) {
969 $this->_defaults
["{$fieldName}_min"] = $field['default']['min'] ??
NULL;
970 $this->_defaults
["{$fieldName}_max"] = $field['default']['max'] ??
NULL;
972 $this->_defaults
["{$fieldName}_value"] = $field['default'];
975 //assign default value as "in" for multiselect
976 //operator, To freeze the select element
977 if (CRM_Utils_Array
::value('operatorType', $field) ==
978 CRM_Report_Form
::OP_MULTISELECT
980 $this->_defaults
["{$fieldName}_op"] = 'in';
982 if (CRM_Utils_Array
::value('operatorType', $field) ==
983 CRM_Report_Form
::OP_ENTITYREF
985 $this->_defaults
["{$fieldName}_op"] = 'in';
987 elseif (CRM_Utils_Array
::value('operatorType', $field) ==
988 CRM_Report_Form
::OP_MULTISELECT_SEPARATOR
990 $this->_defaults
["{$fieldName}_op"] = 'mhas';
992 elseif ($op = CRM_Utils_Array
::value('default_op', $field)) {
993 $this->_defaults
["{$fieldName}_op"] = $op;
999 empty($this->_formValues
['order_bys']) &&
1000 (array_key_exists('order_bys', $table) &&
1001 is_array($table['order_bys']))
1003 if (!array_key_exists('order_bys', $this->_defaults
)) {
1004 $this->_defaults
['order_bys'] = [];
1006 foreach ($table['order_bys'] as $fieldName => $field) {
1007 if (!empty($field['default']) ||
!empty($field['default_order']) ||
1008 CRM_Utils_Array
::value('default_is_section', $field) ||
1009 !empty($field['default_weight'])
1012 'column' => $fieldName,
1013 'order' => CRM_Utils_Array
::value('default_order', $field, 'ASC'),
1014 'section' => CRM_Utils_Array
::value('default_is_section', $field, 0),
1017 if (!empty($field['default_weight'])) {
1018 $this->_defaults
['order_bys'][(int) $field['default_weight']] = $order_by;
1021 array_unshift($this->_defaults
['order_bys'], $order_by);
1027 foreach ($this->_options
as $fieldName => $field) {
1028 if (isset($field['default'])) {
1029 $this->_defaults
['options'][$fieldName] = $field['default'];
1034 if (!empty($this->_submitValues
)) {
1035 $this->preProcessOrderBy($this->_submitValues
);
1038 $this->preProcessOrderBy($this->_defaults
);
1041 // lets finish freezing task here itself
1042 if (!empty($freezeGroup)) {
1043 foreach ($freezeGroup as $elem) {
1048 if ($this->_formValues
) {
1049 $this->_defaults
= array_merge($this->_defaults
, $this->_formValues
);
1052 if ($this->_instanceValues
) {
1053 $this->_defaults
= array_merge($this->_defaults
, $this->_instanceValues
);
1056 CRM_Report_Form_Instance
::setDefaultValues($this, $this->_defaults
);
1058 return $this->_defaults
;
1062 * Get element from group.
1064 * @param string $group
1065 * @param string $grpFieldName
1069 public function getElementFromGroup($group, $grpFieldName) {
1070 $eleObj = $this->getElement($group);
1071 foreach ($eleObj->_elements
as $index => $obj) {
1072 if ($grpFieldName == $obj->_attributes
['name']) {
1080 * Setter for $_params.
1082 * @param array $params
1084 public function setParams($params) {
1085 $this->_params
= $params;
1089 * Getter for $_params.
1091 * @return void|array $params
1093 public function getParams() {
1094 return $this->_params
;
1100 * @param int $instanceID
1102 public function setID($instanceID) {
1103 $this->_id
= $instanceID;
1111 public function getID() {
1116 * Getter for _outputMode
1118 * Note you can implement hook_civicrm_alterReportVar('actions', ...)
1119 * which indirectly allows setting _outputMode if the user chooses
1124 public function getOutputMode():string {
1125 return $this->_outputMode
;
1129 * Getter for report header form field value
1133 public function getReportHeader():string {
1134 return $this->_formValues
['report_header'] ??
'';
1138 * Getter for report footer form field value
1142 public function getReportFooter():string {
1143 return $this->_formValues
['report_footer'] ??
'';
1147 * Setter for $_force.
1149 * @param bool $isForce
1151 public function setForce($isForce) {
1152 $this->_force
= $isForce;
1156 * Setter for $_limitValue.
1158 * @param int $_limitValue
1160 public function setLimitValue($_limitValue) {
1161 $this->_limitValue
= $_limitValue;
1165 * Setter for $_offsetValue.
1167 * @param int $_offsetValue
1169 public function setOffsetValue($_offsetValue) {
1170 $this->_offsetValue
= $_offsetValue;
1174 * Setter for $addPaging.
1176 * @param bool $value
1178 public function setAddPaging($value) {
1179 $this->addPaging
= $value;
1183 * Getter for $_defaultValues.
1187 public function getDefaultValues() {
1188 return $this->_defaults
;
1192 * Remove any temporary tables.
1194 public function cleanUpTemporaryTables() {
1195 foreach ($this->temporaryTables
as $temporaryTable) {
1196 CRM_Core_DAO
::executeQuery('DROP ' . ($temporaryTable['temporary'] ?
'TEMPORARY' : '') . ' TABLE IF EXISTS ' . $temporaryTable['name']);
1201 * Create a temporary table.
1203 * This function creates a table AND adds the details to the developer tab & $this->>temporary tables.
1205 * @param string $identifier
1206 * This is the key that will be used for the table in the temporaryTables property.
1207 * @param string $sql
1208 * Sql select statement or column description (the latter requires the columns flag)
1209 * @param bool $isColumns
1210 * Is the sql describing columns to create (rather than using a select query).
1211 * @param bool $isMemory
1212 * Create a memory table rather than a normal INNODB table.
1216 public function createTemporaryTable($identifier, $sql, $isColumns = FALSE, $isMemory = FALSE) {
1217 $tempTable = CRM_Utils_SQL_TempTable
::build();
1219 $tempTable->setMemory();
1222 $tempTable->createWithColumns($sql);
1225 $tempTable->createWithQuery($sql);
1227 $name = $tempTable->getName();
1228 // Developers may force tables to be durable to assist in debugging so lets check.
1229 $isNotTrueTemporary = $tempTable->isDurable();
1230 $this->addToDeveloperTab($tempTable->getCreateSql());
1231 $this->temporaryTables
[$identifier] = ['temporary' => !$isNotTrueTemporary, 'name' => $name];
1236 * Add columns to report.
1238 public function addColumns() {
1241 foreach ($this->_columns
as $tableName => $table) {
1242 if (array_key_exists('fields', $table)) {
1243 foreach ($table['fields'] as $fieldName => $field) {
1245 if (empty($field['no_display'])) {
1246 foreach (['table', 'field'] as $var) {
1247 if (!empty($
{$var}['grouping'])) {
1248 if (!is_array($
{$var}['grouping'])) {
1249 $tableName = $
{$var}['grouping'];
1252 $tableName = array_keys($
{$var}['grouping']);
1253 $tableName = $tableName[0];
1254 $groupTitle = array_values($
{$var}['grouping']);
1255 $groupTitle = $groupTitle[0];
1260 if (!$groupTitle && isset($table['group_title'])) {
1261 $groupTitle = $table['group_title'];
1262 // Having a group_title is secret code for being a custom group
1263 // which cryptically translates to needing an accordion.
1264 // here we make that explicit.
1265 $colGroups[$tableName]['use_accordian_for_field_selection'] = TRUE;
1268 $colGroups[$tableName]['fields'][$fieldName] = $field['title'] ??
NULL;
1269 if ($groupTitle && empty($colGroups[$tableName]['group_title'])) {
1270 $colGroups[$tableName]['group_title'] = $groupTitle;
1272 $options[$fieldName] = $field['title'] ??
NULL;
1278 $this->addCheckBox("fields", ts('Select Columns'), $options, NULL,
1279 NULL, NULL, NULL, $this->_fourColumnAttribute
, TRUE
1281 if (!empty($colGroups)) {
1282 $this->tabs
['FieldSelection'] = [
1283 'title' => ts('Columns'),
1284 'tpl' => 'FieldSelection',
1285 'div_label' => 'col-groups',
1288 // Note this assignment is only really required in buildForm. It is being 'over-called'
1289 // to reduce risk of being missed due to overridden functions.
1290 $this->assign('tabs', $this->tabs
);
1293 $this->assign('colGroups', $colGroups);
1297 * Add filters to report.
1299 public function addFilters() {
1300 $filters = $filterGroups = [];
1303 foreach ($this->_filters
as $table => $attributes) {
1304 if (isset($this->_columns
[$table]['group_title'])) {
1305 // The presence of 'group_title' is secret code for 'is_a_custom_table'
1306 // which magically means to 'display in an accordian'
1307 // here we make this explicit.
1308 $filterGroups[$table] = [
1309 'group_title' => $this->_columns
[$table]['group_title'],
1310 'use_accordian_for_field_selection' => TRUE,
1314 foreach ($attributes as $fieldName => $field) {
1315 // get ready with option value pair
1316 // @ 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
1318 $operations = $this->getOperationPair(
1319 CRM_Utils_Array
::value('operatorType', $field),
1322 $filters[$table][$fieldName] = $field;
1324 switch (CRM_Utils_Array
::value('operatorType', $field)) {
1325 case CRM_Report_Form
::OP_MONTH
:
1326 if (!array_key_exists('options', $field) ||
1327 !is_array($field['options']) ||
empty($field['options'])
1329 // If there's no option list for this filter, define one.
1330 $field['options'] = [
1332 2 => ts('February'),
1339 9 => ts('September'),
1340 10 => ts('October'),
1341 11 => ts('November'),
1342 12 => ts('December'),
1344 // Add this option list to this column _columns. This is
1345 // required so that filter statistics show properly.
1346 $this->_columns
[$table]['filters'][$fieldName]['options'] = $field['options'];
1348 case CRM_Report_Form
::OP_MULTISELECT
:
1349 case CRM_Report_Form
::OP_MULTISELECT_SEPARATOR
:
1350 // assume a multi-select field
1351 if (!empty($field['options']) ||
1352 $fieldName == 'state_province_id' ||
$fieldName == 'county_id'
1354 $element = $this->addElement('select', "{$fieldName}_op", ts('Operator:'), $operations,
1355 array('onchange' => "return showHideMaxMinVal( '$fieldName', this.value );")
1358 if (count($operations) <= 1) {
1361 if ($fieldName == 'state_province_id' ||
1362 $fieldName == 'county_id'
1364 $this->addChainSelect($fieldName . '_value', [
1371 $this->addElement('select', "{$fieldName}_value", NULL, $field['options'], [
1372 'style' => 'min-width:250px',
1373 'class' => 'crm-select2 huge',
1375 'placeholder' => ts('- select -'),
1381 case CRM_Report_Form
::OP_SELECT
:
1382 // assume a select field
1383 $this->addElement('select', "{$fieldName}_op", ts('Operator:'), $operations);
1384 if (!empty($field['options'])) {
1385 $this->addElement('select', "{$fieldName}_value", NULL, $field['options']);
1389 case CRM_Report_Form
::OP_ENTITYREF
:
1390 $this->addElement('select', "{$fieldName}_op", ts('Operator:'), $operations);
1391 $this->setEntityRefDefaults($field, $table);
1392 $this->addEntityRef("{$fieldName}_value", NULL, $field['attributes']);
1395 case CRM_Report_Form
::OP_DATE
:
1396 // build datetime fields
1397 $this->addDatePickerRange($fieldName, $field['title'], FALSE, FALSE, 'From', 'To', $operations, '_to', '_from');
1400 case CRM_Report_Form
::OP_DATETIME
:
1401 // build datetime fields
1402 $this->addDatePickerRange($fieldName, $field['title'], TRUE, FALSE, 'From', 'To', $operations, '_to', '_from');
1405 case CRM_Report_Form
::OP_INT
:
1406 case CRM_Report_Form
::OP_FLOAT
:
1407 // and a min value input box
1408 $this->add('text', "{$fieldName}_min", ts('Min'));
1409 // and a max value input box
1410 $this->add('text', "{$fieldName}_max", ts('Max'));
1412 // default type is string
1413 $this->addElement('select', "{$fieldName}_op", ts('Operator:'), $operations,
1414 ['onchange' => "return showHideMaxMinVal( '$fieldName', this.value );"]
1416 // we need text box for value input
1417 $this->add('text', "{$fieldName}_value", NULL, ['class' => 'huge']);
1422 if (!empty($filters)) {
1423 $this->tabs
['Filters'] = [
1424 'title' => ts('Filters'),
1426 'div_label' => 'set-filters',
1429 $this->assign('filters', $filters);
1430 $this->assign('filterGroups', $filterGroups);
1434 * Function to assign the tabs to the template in the correct order.
1436 * We want the tabs to wind up in this order (if not overridden).
1444 protected function assignTabs() {
1452 $order = array_intersect_key(array_fill_keys($order, 1), $this->tabs
);
1453 $order = array_merge($order, $this->tabs
);
1454 $this->assign('tabs', $order);
1458 * The intent is to add a tab for developers to view the sql.
1460 * Currently using dpm.
1462 * @param string $sql
1464 public function addToDeveloperTab($sql) {
1465 if (!CRM_Core_Permission
::check('view report sql')) {
1468 $ignored_output_modes = ['pdf', 'csv', 'print'];
1469 if (in_array($this->_outputMode
, $ignored_output_modes)) {
1472 $this->tabs
['Developer'] = [
1473 'title' => ts('Developer'),
1474 'tpl' => 'Developer',
1475 'div_label' => 'set-developer',
1478 $this->assignTabs();
1479 $this->sqlFormattedArray
= [];
1480 $this->sqlArray
[] = $sql;
1481 foreach ($this->sqlArray
as $sql) {
1482 foreach (['LEFT JOIN'] as $term) {
1483 $sql = str_replace($term, '<br> ' . $term, $sql);
1485 foreach (['FROM', 'WHERE', 'GROUP BY', 'ORDER BY', 'LIMIT', ';'] as $term) {
1486 $sql = str_replace($term, '<br><br>' . $term, $sql);
1488 $this->sqlFormattedArray
[] = $sql;
1489 $this->assign('sql', implode(';<br><br><br><br>', $this->sqlFormattedArray
));
1491 $this->assign('sqlModes', $sqlModes = CRM_Utils_SQL
::getSqlModes());
1496 * Add options defined in $this->_options to the report.
1498 public function addOptions() {
1499 if (!empty($this->_options
)) {
1500 // FIXME: For now lets build all elements as checkboxes.
1501 // Once we clear with the format we can build elements based on type
1503 foreach ($this->_options
as $fieldName => $field) {
1506 if ($field['type'] == 'select') {
1507 $this->addElement('select', "{$fieldName}", $field['title'], $field['options']);
1509 elseif ($field['type'] == 'checkbox') {
1510 $options[$field['title']] = $fieldName;
1511 $this->addCheckBox($fieldName, NULL,
1513 NULL, NULL, NULL, $this->_fourColumnAttribute
1518 if (!empty($this->_options
) &&
1520 ||
($this->_id
&& CRM_Report_BAO_ReportInstance
::contactCanAdministerReport($this->_id
)))
1522 $this->tabs
['ReportOptions'] = [
1523 'title' => ts('Display Options'),
1524 'tpl' => 'ReportOptions',
1525 'div_label' => 'other-options',
1528 $this->assign('otherOptions', $this->_options
);
1532 * Add chart options to the report.
1534 public function addChartOptions() {
1535 if (!empty($this->_charts
)) {
1536 $this->addElement('select', "charts", ts('Chart'), $this->_charts
);
1537 $this->assign('charts', $this->_charts
);
1538 $this->addElement('xbutton', $this->_chartButtonName
, ts('View'), ['type' => 'submit']);
1543 * Add group by options to the report.
1545 public function addGroupBys() {
1546 $options = $freqElements = [];
1548 foreach ($this->_columns
as $tableName => $table) {
1549 if (array_key_exists('group_bys', $table)) {
1550 foreach ($table['group_bys'] as $fieldName => $field) {
1551 if (!empty($field) && empty($field['no_display'])) {
1552 $options[$field['title']] = $fieldName;
1553 if (!empty($field['frequency'])) {
1554 $freqElements[$field['title']] = $fieldName;
1560 $this->addCheckBox("group_bys", ts('Group by columns'), $options, NULL,
1561 NULL, NULL, NULL, $this->_fourColumnAttribute
1563 $this->assign('groupByElements', $options);
1564 if (!empty($options)) {
1565 $this->tabs
['GroupBy'] = [
1566 'title' => ts('Grouping'),
1568 'div_label' => 'group-by-elements',
1572 foreach ($freqElements as $name) {
1573 $this->addElement('select', "group_bys_freq[$name]",
1574 ts('Frequency'), $this->_groupByDateFreq
1580 * Add data for order by tab.
1582 public function addOrderBys() {
1584 foreach ($this->_columns
as $tableName => $table) {
1586 // Report developer may define any column to order by; include these as order-by options.
1587 if (array_key_exists('order_bys', $table)) {
1588 foreach ($table['order_bys'] as $fieldName => $field) {
1589 if (!empty($field)) {
1590 $options[$fieldName] = $field['title'];
1595 // Add searchable custom fields as order-by options, if so requested
1596 // (These are already indexed, so allowing to order on them is cheap.)
1598 if ($this->_autoIncludeIndexedFieldsAsOrderBys
&&
1599 array_key_exists('extends', $table) && !empty($table['extends'])
1601 foreach ($table['fields'] as $fieldName => $field) {
1602 if (empty($field['no_display'])) {
1603 $options[$fieldName] = $field['title'];
1611 $this->assign('orderByOptions', $options);
1612 if (!empty($options)) {
1613 $this->tabs
['OrderBy'] = [
1614 'title' => ts('Sorting'),
1616 'div_label' => 'order-by-elements',
1620 if (!empty($options)) {
1622 '-' => ' - none - ',
1624 for ($i = 1; $i <= 5; $i++
) {
1625 $this->addElement('select', "order_bys[{$i}][column]", ts('Order by Column'), $options);
1626 $this->addElement('select', "order_bys[{$i}][order]", ts('Order by Order'), [
1627 'ASC' => ts('Ascending'),
1628 'DESC' => ts('Descending'),
1630 $this->addElement('checkbox', "order_bys[{$i}][section]", ts('Order by Section'), FALSE, ['id' => "order_by_section_$i"]);
1631 $this->addElement('checkbox', "order_bys[{$i}][pageBreak]", ts('Page Break'), FALSE, ['id' => "order_by_pagebreak_$i"]);
1637 * This adds the tab referred to as Title and Format, rendered through Instance.tpl.
1639 * @todo call this tab into the report template in the same way as OrderBy etc, ie
1640 * by adding a description of the tab to $this->tabs, causing the tab to be added in
1643 public function buildInstanceAndButtons() {
1644 CRM_Report_Form_Instance
::buildForm($this);
1645 $this->_actionButtonName
= $this->getButtonName('submit');
1646 $this->addTaskMenu($this->getActions($this->_id
));
1648 $this->assign('instanceForm', $this->_instanceForm
);
1650 // CRM-16274 Determine if user has 'edit all contacts' or equivalent
1651 $permission = CRM_Core_Permission
::getPermission();
1652 if ($this->_instanceForm
&& $permission == CRM_Core_Permission
::EDIT
&&
1653 $this->_add2groupSupported
1655 $this->addElement('select', 'groups', ts('Group'),
1656 ['' => ts('Add Contacts to Group')] +
1657 CRM_Core_PseudoConstant
::nestedGroup(),
1658 ['class' => 'crm-select2 crm-action-menu fa-plus huge']
1660 $this->assign('group', TRUE);
1663 $this->addElement('xbutton', $this->_groupButtonName
, '', [
1665 'style' => 'display: none;',
1668 $this->addChartOptions();
1669 $showResultsLabel = $this->getResultsLabel();
1673 'name' => $showResultsLabel,
1674 'isDefault' => TRUE,
1680 * Has this form been submitted already?
1684 public function resultsDisplayed() {
1685 $buttonName = $this->controller
->getButtonName();
1686 return ($buttonName ||
$this->_outputMode
);
1690 * Get the actions for this report instance.
1692 * @param int $instanceId
1696 protected function getActions($instanceId) {
1697 $actions = CRM_Report_BAO_ReportInstance
::getActionMetadata();
1698 if (empty($instanceId)) {
1699 $actions['report_instance.save'] = [
1700 'title' => ts('Create Report'),
1702 'is_confirm' => TRUE,
1703 'confirm_title' => ts('Create Report'),
1704 'confirm_refresh_fields' => json_encode([
1705 'title' => ['selector' => '.crm-report-instanceForm-form-block-title', 'prepend' => ''],
1706 'description' => ['selector' => '.crm-report-instanceForm-form-block-description', 'prepend' => ''],
1710 unset($actions['report_instance.delete']);
1713 if (!$this->_csvSupported
) {
1714 unset($actions['report_instance.csv']);
1717 CRM_Utils_Hook
::alterReportVar('actions', $actions, $this);
1723 * Main build form function.
1725 public function buildQuickForm() {
1726 $this->addColumns();
1728 $this->addFilters();
1730 $this->addOptions();
1732 $this->addGroupBys();
1734 $this->addOrderBys();
1736 $this->buildInstanceAndButtons();
1738 // Add form rule for report.
1743 $this->addFormRule([get_class($this), 'formRule'], $this);
1745 $this->assignTabs();
1749 * A form rule function for custom data.
1751 * The rule ensures that fields selected in group_by if any) should only be the ones
1752 * present in display/select fields criteria;
1753 * note: works if and only if any custom field selected in group_by.
1755 * @param array $fields
1756 * @param array $ignoreFields
1760 public function customDataFormRule($fields, $ignoreFields = []) {
1762 if (!empty($this->_customGroupExtends
) && $this->_customGroupGroupBy
&&
1763 !empty($fields['group_bys'])
1765 foreach ($this->_columns
as $tableName => $table) {
1766 if ((substr($tableName, 0, 13) == 'civicrm_value' ||
1767 substr($tableName, 0, 12) == 'custom_value') &&
1768 !empty($this->_columns
[$tableName]['fields'])
1770 foreach ($this->_columns
[$tableName]['fields'] as $fieldName => $field) {
1771 if (array_key_exists($fieldName, $fields['group_bys']) &&
1772 !array_key_exists($fieldName, $fields['fields'])
1774 $errors['fields'] = "Please make sure fields selected in 'Group by Columns' section are also selected in 'Display Columns' section.";
1776 elseif (array_key_exists($fieldName, $fields['group_bys'])) {
1777 foreach ($fields['fields'] as $fld => $val) {
1778 if (!array_key_exists($fld, $fields['group_bys']) &&
1779 !in_array($fld, $ignoreFields)
1781 $errors['fields'] = "Please ensure that fields selected in 'Display Columns' are also selected in 'Group by Columns' section.";
1793 * Get operators to display on form.
1795 * Note: $fieldName param allows inheriting class to build operationPairs specific to a field.
1797 * @param string $type
1798 * @param string $fieldName
1802 public function getOperationPair($type = "string", $fieldName = NULL) {
1803 // FIXME: At some point we should move these key-val pairs
1804 // to option_group and option_value table.
1806 case CRM_Report_Form
::OP_INT
:
1807 case CRM_Report_Form
::OP_FLOAT
:
1810 'lte' => ts('Is less than or equal to'),
1811 'gte' => ts('Is greater than or equal to'),
1812 'bw' => ts('Is between'),
1813 'eq' => ts('Is equal to'),
1814 'lt' => ts('Is less than'),
1815 'gt' => ts('Is greater than'),
1816 'neq' => ts('Is not equal to'),
1817 'nbw' => ts('Is not between'),
1818 'nll' => ts('Is empty (Null)'),
1819 'nnll' => ts('Is not empty (Null)'),
1823 case CRM_Report_Form
::OP_SELECT
:
1825 'eq' => ts('Is equal to'),
1829 case CRM_Report_Form
::OP_MONTH
:
1830 case CRM_Report_Form
::OP_MULTISELECT
:
1831 case CRM_Report_Form
::OP_ENTITYREF
:
1834 'in' => ts('Is one of'),
1835 'notin' => ts('Is not one of'),
1836 'nll' => ts('Is empty (Null)'),
1837 'nnll' => ts('Is not empty (Null)'),
1841 case CRM_Report_Form
::OP_DATE
:
1844 'nll' => ts('Is empty (Null)'),
1845 'nnll' => ts('Is not empty (Null)'),
1849 case CRM_Report_Form
::OP_MULTISELECT_SEPARATOR
:
1850 // use this operator for the values, concatenated with separator. For e.g if
1851 // multiple options for a column is stored as ^A{val1}^A{val2}^A
1853 'mhas' => ts('Is one of'),
1854 'mnot' => ts('Is not one of'),
1861 'has' => ts('Contains'),
1862 'sw' => ts('Starts with'),
1863 'ew' => ts('Ends with'),
1864 'nhas' => ts('Does not contain'),
1865 'eq' => ts('Is equal to'),
1866 'neq' => ts('Is not equal to'),
1867 'nll' => ts('Is empty (Null)'),
1868 'nnll' => ts('Is not empty (Null)'),
1875 * Build the tag filter field to display on the filters tab.
1877 public function buildTagFilter() {
1878 $contactTags = CRM_Core_BAO_Tag
::getTags($this->_tagFilterTable
);
1879 if (!empty($contactTags)) {
1880 $this->_columns
['civicrm_tag'] = [
1881 'dao' => 'CRM_Core_DAO_Tag',
1885 'title' => ts('Tag'),
1886 'type' => CRM_Utils_Type
::T_INT
,
1888 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
1889 'options' => $contactTags,
1897 * Adds group filters to _columns (called from _Construct).
1899 public function buildGroupFilter() {
1900 $this->_columns
['civicrm_group']['filters'] = [
1902 'name' => 'group_id',
1903 'title' => ts('Group'),
1904 'type' => CRM_Utils_Type
::T_INT
,
1905 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
1907 'options' => CRM_Core_PseudoConstant
::nestedGroup(),
1910 if (empty($this->_columns
['civicrm_group']['dao'])) {
1911 $this->_columns
['civicrm_group']['dao'] = 'CRM_Contact_DAO_GroupContact';
1913 if (empty($this->_columns
['civicrm_group']['alias'])) {
1914 $this->_columns
['civicrm_group']['alias'] = 'cgroup';
1919 * Get SQL operator from form text version.
1921 * @param string $operator
1925 public function getSQLOperator($operator = "like") {
1926 switch ($operator) {
1959 return 'IS NOT NULL';
1968 * Generate where clause.
1970 * This can be overridden in reports for special treatment of a field
1972 * @param array $field Field specifications
1973 * @param string $op Query operator (not an exact match to sql)
1974 * @param mixed $value
1978 * @return null|string
1980 public function whereClause(&$field, $op, $value, $min, $max) {
1982 $type = CRM_Utils_Type
::typeToString(CRM_Utils_Array
::value('type', $field));
1984 // CRM-18010: Ensure type of each report filters
1986 trigger_error('Type is not defined for field ' . $field['name'], E_USER_WARNING
);
1993 if (($min !== NULL && strlen($min) > 0) ||
1994 ($max !== NULL && strlen($max) > 0)
1998 $min = CRM_Utils_Type
::escape($min, $type);
2000 $clauses[] = "( {$field['dbAlias']} >= $min )";
2003 $clauses[] = "( {$field['dbAlias']} < $min OR {$field['dbAlias']} IS NULL )";
2007 $max = CRM_Utils_Type
::escape($max, $type);
2009 $clauses[] = "( {$field['dbAlias']} <= $max )";
2012 $clauses[] = "( {$field['dbAlias']} > $max )";
2016 if (!empty($clauses)) {
2018 $clause = implode(' AND ', $clauses);
2021 $clause = '(' . implode('OR', $clauses) . ')';
2029 if ($value !== NULL && strlen($value) > 0) {
2030 $value = CRM_Utils_Type
::escape($value, $type);
2031 if (strpos($value, '%') === FALSE) {
2032 $value = "'%{$value}%'";
2035 $value = "'{$value}'";
2037 $sqlOP = $this->getSQLOperator($op);
2038 $clause = "( {$field['dbAlias']} $sqlOP $value )";
2044 if ((is_string($value) ||
is_numeric($value)) && strlen($value)) {
2045 $value = explode(',', $value);
2047 if ($value !== NULL && is_array($value) && count($value) > 0) {
2048 $sqlOP = $this->getSQLOperator($op);
2049 if (CRM_Utils_Array
::value('type', $field) ==
2050 CRM_Utils_Type
::T_STRING
2052 //cycle through selections and escape values
2053 foreach ($value as $key => $selection) {
2054 $value[$key] = CRM_Utils_Type
::escape($selection, $type);
2057 = "( {$field['dbAlias']} $sqlOP ( '" . implode("' , '", $value) .
2061 // for numerical values
2062 $clause = "{$field['dbAlias']} $sqlOP (" . implode(', ', $value) .
2065 if ($op == 'notin') {
2066 $clause = "( " . $clause . " OR {$field['dbAlias']} IS NULL )";
2069 $clause = "( " . $clause . " )";
2076 // multiple has or multiple not
2077 if ($value !== NULL && count($value) > 0) {
2078 $value = CRM_Utils_Type
::escapeAll($value, $type);
2079 $operator = $op == 'mnot' ?
'NOT' : '';
2080 $regexp = "([[:cntrl:]]|^)" . implode('([[:cntrl:]]|$)|([[:cntrl:]]|^)', (array) $value) . "([[:cntrl:]]|$)";
2081 $clause = "{$field['dbAlias']} {$operator} REGEXP '{$regexp}'";
2087 if ($value !== NULL && strlen($value) > 0) {
2088 $value = CRM_Utils_Type
::escape($value, $type);
2089 if (strpos($value, '%') === FALSE) {
2091 $value = "'{$value}%'";
2094 $value = "'%{$value}'";
2098 $value = "'{$value}'";
2100 $sqlOP = $this->getSQLOperator($op);
2101 $clause = "( {$field['dbAlias']} $sqlOP $value )";
2106 if ($type == 'String') {
2107 $sqlOP = $this->getSQLOperator($op);
2108 $clause = "( {$field['dbAlias']} $sqlOP OR {$field['dbAlias']} = '' )";
2111 $sqlOP = $this->getSQLOperator($op);
2112 $clause = "( {$field['dbAlias']} $sqlOP )";
2117 if ($type == 'String') {
2118 $sqlOP = $this->getSQLOperator($op);
2119 $clause = "( {$field['dbAlias']} $sqlOP AND {$field['dbAlias']} <> '' )";
2122 $sqlOP = $this->getSQLOperator($op);
2123 $clause = "( {$field['dbAlias']} $sqlOP )";
2130 //CRM-18457: some custom field passes value in array format against binary operator
2131 if (is_array($value) && count($value)) {
2136 if ($value !== NULL && $value !== '') {
2137 if (isset($field['clause'])) {
2138 // FIXME: we not doing escape here. Better solution is to use two
2139 // different types - data-type and filter-type
2140 $clause = $field['clause'];
2142 elseif (!is_array($value)) {
2143 $value = CRM_Utils_Type
::escape($value, $type);
2144 $sqlOP = $this->getSQLOperator($op);
2145 if ($field['type'] == CRM_Utils_Type
::T_STRING
) {
2146 $value = "'{$value}'";
2148 $clause = "( {$field['dbAlias']} $sqlOP $value )";
2154 //dev/core/544 Add report support for multiple contact subTypes
2155 if ($field['name'] == 'contact_sub_type' && $clause) {
2156 $clause = $this->whereSubtypeClause($field, $value, $op);
2158 if (!empty($field['group']) && $clause) {
2159 $clause = $this->whereGroupClause($field, $value, $op);
2161 elseif (!empty($field['tag']) && $clause) {
2162 // not using left join in query because if any contact
2163 // belongs to more than one tag, results duplicate
2165 $clause = $this->whereTagClause($field, $value, $op);
2167 elseif (!empty($field['membership_org']) && $clause) {
2168 $clause = $this->whereMembershipOrgClause($value, $op);
2170 elseif (!empty($field['membership_type']) && $clause) {
2171 $clause = $this->whereMembershipTypeClause($value, $op);
2177 * Get SQL where clause for contact subtypes
2178 * @param string $field
2179 * @param mixed $value
2180 * @param string $op SQL Operator
2184 public function whereSubtypeClause($field, $value, $op) {
2185 // Get the correct SQL operator.
2190 $clauseSeparator = ' AND ';
2196 $clauseSeparator = ' OR ';
2199 $sqlOp = $this->getSQLOperator($op);
2200 if ($sqlOp == 'IS NULL' ||
$sqlOp == 'IS NOT NULL') {
2201 $clause = "{$field['dbAlias']} $sqlOp";
2205 foreach ($value as $item) {
2206 $subclauses[] = "( {$field['dbAlias']} $sqlOp '%" . CRM_Core_DAO
::VALUE_SEPARATOR
. $item . CRM_Core_DAO
::VALUE_SEPARATOR
. "%' )";
2208 $clause = implode($clauseSeparator, $subclauses);
2210 $clause = "( $clause )";
2212 $clause = "( ( {$field['dbAlias']} IS NULL ) OR $clause )";
2218 * Get SQL where clause for a date field.
2220 * @param string $fieldName
2221 * @param string $relative
2222 * @param string $from
2224 * @param string $type
2225 * @param string $fromTime
2226 * @param string $toTime
2228 * @return null|string
2230 public function dateClause(
2232 $relative, $from, $to, $type = NULL, $fromTime = NULL, $toTime = NULL
2235 if (in_array($relative, array_keys($this->getOperationPair(CRM_Report_Form
::OP_DATE
)))) {
2236 $sqlOP = $this->getSQLOperator($relative);
2237 return "( {$fieldName} {$sqlOP} )";
2239 if (strlen($to) === 10) {
2240 // If we just have the date we assume the end of that day.
2245 list($from, $to) = $this->getFromTo($relative, $from, $to, $fromTime, $toTime);
2249 $clauses[] = "( {$fieldName} >= " . date('YmdHis', strtotime($from)) . ')';
2253 $clauses[] = "( {$fieldName} <= " . date('YmdHis', strtotime($to)) . ')';
2255 if (!empty($clauses)) {
2256 return implode(' AND ', $clauses);
2263 * Get values for from and to for date ranges.
2267 * @param bool $relative
2268 * @param string $from
2270 * @param string $fromTime
2271 * @param string $toTime
2275 public function getFromTo($relative, $from, $to, $fromTime = NULL, $toTime = NULL) {
2276 if (!empty($fromTime) ||
!empty($toTime)) {
2277 throw new CRM_Core_Exception('Report template needs to be updated as getFromTo no longer supports fromTime or ToTime Parameters');
2279 return CRM_Utils_Date
::getFromTo($relative, $from, $to);
2283 * Alter display of rows.
2285 * Iterate through the rows retrieved via SQL and make changes for display purposes,
2286 * such as rendering contacts as links.
2288 * @param array $rows
2289 * Rows generated by SQL, with an array for each row.
2291 public function alterDisplay(&$rows) {
2295 * Alter the way in which custom data fields are displayed.
2297 * @param array $rows
2299 public function alterCustomDataDisplay(&$rows) {
2300 // custom code to alter rows having custom values
2301 if (empty($this->_customGroupExtends
)) {
2306 $customFieldIds = [];
2307 foreach ($this->_params
['fields'] as $fieldAlias => $value) {
2308 if ($fieldId = CRM_Core_BAO_CustomField
::getKeyID($fieldAlias)) {
2309 $customFieldIds[$fieldAlias] = $fieldId;
2312 if (empty($customFieldIds)) {
2316 // skip for type date and ContactReference since date format is already handled
2318 SELECT cg.table_name, cf.id
2319 FROM civicrm_custom_field cf
2320 INNER JOIN civicrm_custom_group cg ON cg.id = cf.custom_group_id
2321 WHERE cg.extends IN ('" . implode("','", $this->_customGroupExtends
) . "') AND
2322 cg.is_active = 1 AND
2323 cf.is_active = 1 AND
2324 cf.is_searchable = 1 AND
2325 cf.data_type NOT IN ('ContactReference', 'Date') AND
2326 cf.id IN (" . implode(",", $customFieldIds) . ")";
2328 $dao = CRM_Core_DAO
::executeQuery($query);
2329 while ($dao->fetch()) {
2330 $customFields[$dao->table_name
. '_custom_' . $dao->id
] = $dao->id
;
2333 $entryFound = FALSE;
2334 foreach ($rows as $rowNum => $row) {
2335 foreach ($row as $tableCol => $val) {
2336 if (array_key_exists($tableCol, $customFields)) {
2337 $rows[$rowNum][$tableCol] = CRM_Core_BAO_CustomField
::displayValue($val, $customFields[$tableCol]);
2342 // skip looking further in rows, if first row itself doesn't
2343 // have the column we need
2351 * Remove duplicate rows.
2353 * @param array $rows
2355 public function removeDuplicates(&$rows) {
2356 if (empty($this->_noRepeats
)) {
2361 foreach ($rows as $key => $list) {
2362 foreach ($list as $colName => $colVal) {
2363 if (array_key_exists($colName, $checkList) &&
2364 $checkList[$colName] == $colVal
2366 $rows[$key][$colName] = "";
2368 if (in_array($colName, $this->_noRepeats
)) {
2369 $checkList[$colName] = $colVal;
2376 * Fix subtotal display.
2379 * @param array $fields
2380 * @param bool $subtotal
2382 public function fixSubTotalDisplay(&$row, $fields, $subtotal = TRUE) {
2383 foreach ($row as $colName => $colVal) {
2384 if (in_array($colName, $fields)) {
2386 elseif (isset($this->_columnHeaders
[$colName])) {
2388 $row[$colName] = 'Subtotal';
2392 unset($row[$colName]);
2399 * Calculate grant total.
2401 * @param array $rows
2405 public function grandTotal(&$rows) {
2406 if (!$this->_rollup ||
count($rows) == 1) {
2410 $this->moveSummaryColumnsToTheRightHandSide();
2412 if ($this->_limit
&& count($rows) >= self
::ROW_COUNT_LIMIT
) {
2416 $this->rollupRow
= array_pop($rows);
2418 foreach ($this->_columnHeaders
as $fld => $val) {
2419 if (!in_array($fld, $this->_statFields
)) {
2420 if (!$this->_grandFlag
) {
2421 $this->rollupRow
[$fld] = ts('Grand Total');
2422 $this->_grandFlag
= TRUE;
2425 $this->rollupRow
[$fld] = "";
2430 $this->assign('grandStat', $this->rollupRow
);
2435 * Format display output.
2437 * @param array $rows
2438 * @param bool $pager
2440 public function formatDisplay(&$rows, $pager = TRUE) {
2441 // set pager based on if any limit was applied in the query.
2446 // allow building charts if any
2447 if (!empty($this->_params
['charts']) && !empty($rows)) {
2448 $this->buildChart($rows);
2449 $this->assign('chartEnabled', TRUE);
2450 $this->_chartId
= "{$this->_params['charts']}_" .
2451 ($this->_id ?
$this->_id
: substr(get_class($this), 16)) . '_' .
2453 $this->assign('chartId', $this->_chartId
);
2456 // unset columns not to be displayed.
2457 foreach ($this->_columnHeaders
as $key => $value) {
2458 if (!empty($value['no_display'])) {
2459 unset($this->_columnHeaders
[$key]);
2463 // unset columns not to be displayed.
2464 if (!empty($rows)) {
2465 foreach ($this->_noDisplay
as $noDisplayField) {
2466 foreach ($rows as $rowNum => $row) {
2467 unset($this->_columnHeaders
[$noDisplayField]);
2472 // Find alter display functions.
2473 $firstRow = reset($rows);
2475 $selectedFields = array_keys($firstRow);
2476 $alterFunctions = $alterMap = $alterSpecs = [];
2477 foreach ($this->_columns
as $tableName => $table) {
2478 if (array_key_exists('metadata', $table)) {
2479 foreach ($table['metadata'] as $field => $specs) {
2480 if (in_array($tableName . '_' . $field, $selectedFields)) {
2481 if (array_key_exists('alter_display', $specs)) {
2482 $alterFunctions[$tableName . '_' . $field] = $specs['alter_display'];
2483 $alterMap[$tableName . '_' . $field] = $field;
2484 $alterSpecs[$tableName . '_' . $field] = NULL;
2486 // Add any alters that can be intuited from the field specs.
2487 // So far only boolean but a lot more could be.
2488 if (empty($alterSpecs[$tableName . '_' . $field]) && isset($specs['type']) && $specs['type'] == CRM_Utils_Type
::T_BOOLEAN
) {
2489 $alterFunctions[$tableName . '_' . $field] = 'alterBoolean';
2490 $alterMap[$tableName . '_' . $field] = $field;
2491 $alterSpecs[$tableName . '_' . $field] = NULL;
2498 // Run the alter display functions
2499 foreach ($rows as $index => & $row) {
2500 foreach ($row as $selectedField => $value) {
2501 if (array_key_exists($selectedField, $alterFunctions) && isset($value)) {
2502 $rows[$index][$selectedField] = $this->{$alterFunctions[$selectedField]}($value, $row, $selectedField, $alterMap[$selectedField], $alterSpecs[$selectedField]);
2508 // use this method for formatting rows for display purpose.
2509 $this->alterDisplay($rows);
2510 CRM_Utils_Hook
::alterReportVar('rows', $rows, $this);
2512 // build array of section totals
2513 $this->sectionTotals();
2515 // process grand-total row
2516 $this->grandTotal($rows);
2518 // use this method for formatting custom rows for display purpose.
2519 $this->alterCustomDataDisplay($rows);
2525 * @param $selectedfield
2526 * @param $criteriaFieldName
2530 protected function alterStateProvinceID($value, &$row, $selectedfield, $criteriaFieldName) {
2531 $url = CRM_Utils_System
::url(CRM_Utils_System
::currentPath(), "reset=1&force=1&{$criteriaFieldName}_op=in&{$criteriaFieldName}_value={$value}", $this->_absoluteUrl
);
2532 $row[$selectedfield . '_link'] = $url;
2533 $row[$selectedfield . '_hover'] = ts("%1 for this state.", [
2537 $states = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
2538 if (!is_array($states)) {
2546 * @param $selectedField
2547 * @param $criteriaFieldName
2551 protected function alterCountryID($value, &$row, $selectedField, $criteriaFieldName) {
2552 $url = CRM_Utils_System
::url(CRM_Utils_System
::currentPath(), "reset=1&force=1&{$criteriaFieldName}_op=in&{$criteriaFieldName}_value={$value}", $this->_absoluteUrl
);
2553 $row[$selectedField . '_link'] = $url;
2554 $row[$selectedField . '_hover'] = ts("%1 for this country.", [
2557 $countries = CRM_Core_PseudoConstant
::country($value, FALSE);
2558 if (!is_array($countries)) {
2566 * @param $selectedfield
2567 * @param $criteriaFieldName
2571 protected function alterCountyID($value, &$row, $selectedfield, $criteriaFieldName) {
2572 $url = CRM_Utils_System
::url(CRM_Utils_System
::currentPath(), "reset=1&force=1&{$criteriaFieldName}_op=in&{$criteriaFieldName}_value={$value}", $this->_absoluteUrl
);
2573 $row[$selectedfield . '_link'] = $url;
2574 $row[$selectedfield . '_hover'] = ts("%1 for this county.", [
2577 $counties = CRM_Core_PseudoConstant
::county($value, FALSE);
2578 if (!is_array($counties)) {
2586 * @param $selectedfield
2587 * @param $criteriaFieldName
2591 protected function alterLocationTypeID($value, &$row, $selectedfield, $criteriaFieldName) {
2592 return CRM_Core_PseudoConstant
::getLabel('CRM_Core_DAO_Address', 'location_type_id', $value);
2602 protected function alterContactID($value, &$row, $fieldname) {
2603 $nameField = substr($fieldname, 0, -2) . 'name';
2604 static $first = TRUE;
2605 static $viewContactList = FALSE;
2607 $viewContactList = CRM_Core_Permission
::check('access CiviCRM');
2610 if (!$viewContactList) {
2613 if (array_key_exists($nameField, $row)) {
2614 $row[$nameField . '_link'] = CRM_Utils_System
::url("civicrm/contact/view", 'reset=1&cid=' . $value, $this->_absoluteUrl
);
2617 $row[$fieldname . '_link'] = CRM_Utils_System
::url("civicrm/contact/view", 'reset=1&cid=' . $value, $this->_absoluteUrl
);
2629 protected function alterCommunicationtMethod($value, &$row, $fieldname) {
2630 $communicationMethods = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'preferred_communication_method');
2632 // Explode padded values.
2633 $values = CRM_utils_array
::explodePadded($value);
2634 // Flip values, compute intersection with $communicationMethods, and implode with commas.
2635 $value = implode(', ', array_intersect_key($communicationMethods, array_flip($values)));
2644 protected function alterBoolean($value) {
2645 $options = [0 => '', 1 => ts('Yes')];
2646 if (isset($options[$value])) {
2647 return $options[$value];
2655 * @param array $rows
2657 public function buildChart(&$rows) {
2658 // override this method for building charts.
2661 // select() method below has been added recently (v3.3), and many of the report templates might
2662 // still be having their own select() method. We should fix them as and when encountered and move
2663 // towards generalizing the select() method below.
2666 * Generate the SELECT clause and set class variable $_select.
2668 public function select() {
2669 $select = $this->_selectAliases
= [];
2670 $this->storeGroupByArray();
2672 foreach ($this->_columns
as $tableName => $table) {
2673 if (array_key_exists('fields', $table)) {
2674 foreach ($table['fields'] as $fieldName => $field) {
2675 if ($tableName == 'civicrm_address') {
2676 // deprecated, use $this->isTableSelected.
2677 $this->_addressField
= TRUE;
2679 if ($tableName == 'civicrm_email') {
2680 $this->_emailField
= TRUE;
2682 if ($tableName == 'civicrm_phone') {
2683 $this->_phoneField
= TRUE;
2686 if (!empty($field['required']) ||
2687 !empty($this->_params
['fields'][$fieldName])
2690 // 1. In many cases we want select clause to be built in slightly different way
2691 // for a particular field of a particular type.
2692 // 2. This method when used should receive params by reference and modify $this->_columnHeaders
2694 $selectClause = $this->selectClause($tableName, 'fields', $fieldName, $field);
2695 if ($selectClause) {
2696 $select[] = $selectClause;
2700 // include statistics columns only if set
2701 if (!empty($field['statistics']) && !empty($this->_groupByArray
)) {
2702 $select = $this->addStatisticsToSelect($field, $tableName, $fieldName, $select);
2706 $selectClause = $this->getSelectClauseWithGroupConcatIfNotGroupedBy($tableName, $fieldName, $field);
2707 if ($selectClause) {
2708 $select[] = $selectClause;
2711 $select = $this->addBasicFieldToSelect($tableName, $fieldName, $field, $select);
2718 // select for group bys
2719 if (array_key_exists('group_bys', $table)) {
2720 foreach ($table['group_bys'] as $fieldName => $field) {
2722 if ($tableName == 'civicrm_address') {
2723 $this->_addressField
= TRUE;
2725 if ($tableName == 'civicrm_email') {
2726 $this->_emailField
= TRUE;
2728 if ($tableName == 'civicrm_phone') {
2729 $this->_phoneField
= TRUE;
2731 // 1. In many cases we want select clause to be built in slightly different way
2732 // for a particular field of a particular type.
2733 // 2. This method when used should receive params by reference and modify $this->_columnHeaders
2735 $selectClause = $this->selectClause($tableName, 'group_bys', $fieldName, $field);
2736 if ($selectClause) {
2737 $select[] = $selectClause;
2741 if (!empty($this->_params
['group_bys']) &&
2742 !empty($this->_params
['group_bys'][$fieldName]) &&
2743 !empty($this->_params
['group_bys_freq'])
2745 switch (CRM_Utils_Array
::value($fieldName, $this->_params
['group_bys_freq'])) {
2747 $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL WEEKDAY({$field['dbAlias']}) DAY) AS {$tableName}_{$fieldName}_start";
2748 $select[] = "YEARWEEK({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
2749 $select[] = "WEEKOFYEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
2750 $field['title'] = 'Week';
2754 $select[] = "MAKEDATE(YEAR({$field['dbAlias']}), 1) AS {$tableName}_{$fieldName}_start";
2755 $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
2756 $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
2757 $field['title'] = 'Year';
2761 $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL (DAYOFMONTH({$field['dbAlias']})-1) DAY) as {$tableName}_{$fieldName}_start";
2762 $select[] = "MONTH({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
2763 $select[] = "MONTHNAME({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
2764 $field['title'] = 'Month';
2768 $select[] = "STR_TO_DATE(CONCAT( 3 * QUARTER( {$field['dbAlias']} ) -2 , '/', '1', '/', YEAR( {$field['dbAlias']} ) ), '%m/%d/%Y') AS {$tableName}_{$fieldName}_start";
2769 $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
2770 $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
2771 $field['title'] = 'Quarter';
2774 // for graphs and charts -
2775 if (!empty($this->_params
['group_bys_freq'][$fieldName])) {
2776 $this->_interval
= $field['title'];
2777 $this->_columnHeaders
["{$tableName}_{$fieldName}_start"]['title']
2778 = $field['title'] . ' Beginning';
2779 $this->_columnHeaders
["{$tableName}_{$fieldName}_start"]['type'] = $field['type'];
2780 $this->_columnHeaders
["{$tableName}_{$fieldName}_start"]['group_by'] = $this->_params
['group_bys_freq'][$fieldName];
2782 // just to make sure these values are transferred to rows.
2783 // since we 'll need them for calculation purpose,
2784 // e.g making subtotals look nicer or graphs
2785 $this->_columnHeaders
["{$tableName}_{$fieldName}_interval"] = ['no_display' => TRUE];
2786 $this->_columnHeaders
["{$tableName}_{$fieldName}_subtotal"] = ['no_display' => TRUE];
2793 if (empty($select)) {
2794 // CRM-21412 Do not give fatal error on report when no fields selected
2798 $this->_selectClauses
= $select;
2799 $this->_select
= "SELECT " . implode(', ', $select) . " ";
2803 * Build select clause for a single field.
2805 * @param string $tableName
2806 * @param string $tableKey
2807 * @param string $fieldName
2808 * @param string $field
2812 public function selectClause(&$tableName, $tableKey, &$fieldName, &$field) {
2813 if (!empty($field['pseudofield'])) {
2814 $alias = "{$tableName}_{$fieldName}";
2815 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'] ??
NULL;
2816 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = $field['type'] ??
NULL;
2817 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['dbAlias'] = $field['dbAlias'] ??
NULL;
2818 $this->_selectAliases
[] = $alias;
2819 return ' 1 as ' . $alias;
2825 * Build where clause.
2827 public function where() {
2828 $this->storeWhereHavingClauseArray();
2830 if (empty($this->_whereClauses
)) {
2831 $this->_where
= "WHERE ( 1 ) ";
2832 $this->_having
= "";
2835 $this->_where
= "WHERE " . implode(' AND ', $this->_whereClauses
);
2838 if ($this->_aclWhere
) {
2839 $this->_where
.= " AND {$this->_aclWhere} ";
2842 if (!empty($this->_havingClauses
)) {
2843 // use this clause to construct group by clause.
2844 $this->_having
= "HAVING " . implode(' AND ', $this->_havingClauses
);
2849 * Store Where clauses into an array.
2851 * Breaking out this step makes over-riding more flexible as the clauses can be used in constructing a
2852 * temp table that may not be part of the final where clause or added
2853 * in other functions
2855 public function storeWhereHavingClauseArray() {
2856 foreach ($this->_columns
as $tableName => $table) {
2857 if (array_key_exists('filters', $table)) {
2858 foreach ($table['filters'] as $fieldName => $field) {
2859 // respect pseudofield to filter spec so fields can be marked as
2860 // not to be handled here
2861 if (!empty($field['pseudofield'])) {
2864 $clause = $this->generateFilterClause($field, $fieldName);
2866 if (!empty($clause)) {
2867 if (!empty($field['having'])) {
2868 $this->_havingClauses
[] = $clause;
2871 $this->_whereClauses
[] = $clause;
2883 public function processReportMode() {
2884 $this->setOutputMode();
2887 = CRM_Utils_Request
::retrieve(
2890 CRM_Core_DAO
::$_nullObject
2893 if ($this->_sendmail
&& !$this->_outputMode
) {
2894 // If we're here from the mail_report job, then the default there gets
2895 // set to pdf before we get here, but if we're somehow here and sending
2896 // by email and don't have a format set, then use print.
2897 // @todo Is this on purpose - why would they be different defaults?
2898 $this->_outputMode
= 'print';
2901 // _outputMode means multiple things and can cover export to file formats,
2902 // like csv, or actions with no output, like save. So this will only set
2903 // a handler if it's one of the former. But it's also possible we have a
2904 // really interesting handler out there. But the point is we don't need to
2905 // know, just to know that a handler doesn't always get set by this call.
2906 $this->setOutputHandler();
2908 if (!empty($this->outputHandler
)) {
2909 if ($this->_sendmail
) {
2910 // If we're sending by email these are the only options that make
2912 $this->printOnly
= TRUE;
2913 $this->addPaging
= FALSE;
2914 $this->_absoluteUrl
= TRUE;
2917 // otherwise ask the handler
2918 $this->printOnly
= $this->outputHandler
->isPrintOnly();
2919 $this->addPaging
= $this->outputHandler
->isAddPaging();
2920 $this->_absoluteUrl
= $this->outputHandler
->isAbsoluteUrl();
2923 elseif ($this->_outputMode
== 'copy' && $this->_criteriaForm
) {
2924 $this->_createNew
= TRUE;
2927 $this->assign('outputMode', $this->_outputMode
);
2928 $this->assign('printOnly', $this->printOnly
);
2929 // Get today's date to include in printed reports
2930 if ($this->printOnly
) {
2931 $reportDate = CRM_Utils_Date
::customFormat(date('Y-m-d H:i'));
2932 $this->assign('reportDate', $reportDate);
2937 * Post Processing function for Form.
2939 * postProcessCommon should be used to set other variables from input as the api accesses that function.
2940 * This function is not accessed when the api calls the report.
2942 public function beginPostProcess() {
2943 $this->setParams($this->controller
->exportValues($this->_name
));
2944 if (empty($this->_params
) &&
2947 $this->setParams($this->_formValues
);
2950 // hack to fix params when submitted from dashboard, CRM-8532
2951 // fields array is missing because form building etc is skipped
2952 // in dashboard mode for report
2953 //@todo - this could be done in the dashboard no we have a setter
2954 if (empty($this->_params
['fields']) && !$this->_noFields
) {
2955 $this->setParams($this->_formValues
);
2958 $this->processReportMode();
2960 if ($this->_outputMode
== 'save' ||
$this->_outputMode
== 'copy') {
2961 $this->_createNew
= ($this->_outputMode
== 'copy');
2962 CRM_Report_Form_Instance
::postProcess($this);
2964 if ($this->_outputMode
== 'delete') {
2965 CRM_Report_BAO_ReportInstance
::doFormDelete($this->_id
, 'civicrm/report/list?reset=1', 'civicrm/report/list?reset=1');
2968 $this->_formValues
= $this->_params
;
2970 $this->beginPostProcessCommon();
2974 * BeginPostProcess function run in both report mode and non-report mode (api).
2976 public function beginPostProcessCommon() {
2980 * Build the report query.
2982 * @param bool $applyLimit
2986 public function buildQuery($applyLimit = TRUE) {
2987 $this->buildGroupTempTable();
2990 $this->customDataFrom();
2991 $this->buildPermissionClause();
2996 foreach ($this->unselectedOrderByColumns() as $alias => $field) {
2997 $clause = $this->getSelectClauseWithGroupConcatIfNotGroupedBy($field['table_name'], $field['name'], $field);
2999 $clause = "{$field['dbAlias']} as {$alias}";
3001 $this->_select
.= ", $clause ";
3004 if ($applyLimit && empty($this->_params
['charts'])) {
3007 CRM_Utils_Hook
::alterReportVar('sql', $this, $this);
3009 $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy} {$this->_limit}";
3010 $this->addToDeveloperTab($sql);
3015 * Build group by clause.
3017 public function groupBy() {
3018 $this->storeGroupByArray();
3020 if (!empty($this->_groupByArray
)) {
3021 if ($this->optimisedForOnlyFullGroupBy
) {
3022 // We should probably deprecate this code path. What happens here is that
3023 // the group by is amended to reflect the select columns. This often breaks the
3024 // results. Retrofitting group strict group by onto existing report classes
3026 $this->_groupBy
= CRM_Contact_BAO_Query
::getGroupByFromSelectColumns($this->_selectClauses
, $this->_groupByArray
);
3029 $this->_groupBy
= ' GROUP BY ' . implode($this->_groupByArray
);
3035 * Build order by clause.
3037 public function orderBy() {
3038 $this->_orderBy
= "";
3039 $this->_sections
= [];
3040 $this->storeOrderByArray();
3041 if (!empty($this->_orderByArray
) && !$this->_rollup
== 'WITH ROLLUP') {
3042 $this->_orderBy
= "ORDER BY " . implode(', ', $this->_orderByArray
);
3044 $this->assign('sections', $this->_sections
);
3048 * Extract order by fields and store as an array.
3050 * In some cases other functions want to know which fields are selected for ordering by
3051 * Separating this into a separate function allows it to be called separately from constructing
3052 * the order by clause
3054 public function storeOrderByArray() {
3057 if (!empty($this->_params
['order_bys']) &&
3058 is_array($this->_params
['order_bys']) &&
3059 !empty($this->_params
['order_bys'])
3062 // Process order_bys in user-specified order
3063 foreach ($this->_params
['order_bys'] as $orderBy) {
3065 foreach ($this->_columns
as $tableName => $table) {
3066 if (array_key_exists('order_bys', $table)) {
3067 // For DAO columns defined in $this->_columns
3068 $fields = $table['order_bys'];
3070 elseif (array_key_exists('extends', $table)) {
3071 // For custom fields referenced in $this->_customGroupExtends
3072 $fields = CRM_Utils_Array
::value('fields', $table, []);
3077 if (!empty($fields) && is_array($fields)) {
3078 foreach ($fields as $fieldName => $field) {
3079 if ($fieldName == $orderBy['column']) {
3080 $orderByField = array_merge($field, $orderBy);
3081 $orderByField['tplField'] = "{$tableName}_{$fieldName}";
3088 if (!empty($orderByField)) {
3089 $this->_orderByFields
[$orderByField['tplField']] = $orderByField;
3090 if ($this->groupConcatTested
) {
3091 $orderBys[$orderByField['tplField']] = "{$orderByField['tplField']} {$orderBy['order']}";
3094 // Not sure when this is preferable to using tplField (which has
3095 // definitely been tested to work in cases then this does not.
3096 // in caution not switching unless report has been tested for
3097 // group concat functionality.
3098 $orderBys[$orderByField['tplField']] = "{$orderByField['dbAlias']} {$orderBy['order']}";
3101 // Record any section headers for assignment to the template
3102 if (!empty($orderBy['section'])) {
3103 $orderByField['pageBreak'] = $orderBy['pageBreak'] ??
NULL;
3104 $this->_sections
[$orderByField['tplField']] = $orderByField;
3110 $this->_orderByArray
= $orderBys;
3112 $this->assign('sections', $this->_sections
);
3116 * Determine unselected columns.
3120 public function unselectedOrderByColumns() {
3121 return array_diff_key($this->_orderByFields
, $this->getSelectColumns());
3125 * Determine unselected columns.
3129 public function unselectedSectionColumns() {
3130 if (is_array($this->_sections
)) {
3131 return array_diff_key($this->_sections
, $this->getSelectColumns());
3139 * Build output rows.
3141 * @param string $sql
3142 * @param array $rows
3144 public function buildRows($sql, &$rows) {
3145 if (!$this->optimisedForOnlyFullGroupBy
) {
3146 CRM_Core_DAO
::disableFullGroupByMode();
3148 $dao = CRM_Core_DAO
::executeQuery($sql);
3149 if (stristr($this->_select
, 'SQL_CALC_FOUND_ROWS')) {
3150 $this->_rowsFound
= CRM_Core_DAO
::singleValueQuery('SELECT FOUND_ROWS()');
3152 CRM_Core_DAO
::reenableFullGroupByMode();
3153 if (!is_array($rows)) {
3157 // use this method to modify $this->_columnHeaders
3158 $this->modifyColumnHeaders();
3160 $unselectedSectionColumns = $this->unselectedSectionColumns();
3162 while ($dao->fetch()) {
3164 foreach ($this->_columnHeaders
as $key => $value) {
3165 if (property_exists($dao, $key)) {
3166 $row[$key] = $dao->$key;
3170 // section headers not selected for display need to be added to row
3171 foreach ($unselectedSectionColumns as $key => $values) {
3172 if (property_exists($dao, $key)) {
3173 $row[$key] = $dao->$key;
3182 * Calculate section totals.
3184 * When "order by" fields are marked as sections, this assigns to the template
3185 * an array of total counts for each section. This data is used by the Smarty
3186 * plugin {sectionTotal}.
3188 public function sectionTotals() {
3190 // Reports using order_bys with sections must populate $this->_selectAliases in select() method.
3191 if (empty($this->_selectAliases
)) {
3195 if (!empty($this->_sections
)) {
3196 // build the query with no LIMIT clause
3197 $select = str_ireplace('SELECT SQL_CALC_FOUND_ROWS ', 'SELECT ', $this->_select
);
3198 $sql = "{$select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy}";
3200 // pull section aliases out of $this->_sections
3201 $sectionAliases = array_keys($this->_sections
);
3204 foreach (array_merge($sectionAliases, $this->_selectAliases
) as $alias) {
3205 $ifnulls[] = "ifnull($alias, '') as $alias";
3207 $this->_select
= "SELECT " . implode(", ", $ifnulls);
3208 $this->_select
= CRM_Contact_BAO_Query
::appendAnyValueToSelect($ifnulls, $sectionAliases);
3210 // Group (un-limited) report by all aliases and get counts. This might
3211 // be done more efficiently when the contents of $sql are known, ie. by
3212 // overriding this method in the report class.
3214 $query = $this->_select
.
3215 ", count(*) as ct from ($sql) as subquery group by " .
3216 implode(", ", $sectionAliases);
3218 // initialize array of total counts
3220 $dao = CRM_Core_DAO
::executeQuery($query);
3221 while ($dao->fetch()) {
3223 // let $this->_alterDisplay translate any integer ids to human-readable values.
3224 $rows[0] = $dao->toArray();
3225 $this->alterDisplay($rows);
3228 // add totals for all permutations of section values
3231 $aliasCount = count($sectionAliases);
3232 foreach ($sectionAliases as $alias) {
3233 $values[] = $row[$alias];
3234 $key = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $values);
3235 if ($i == $aliasCount) {
3236 // the last alias is the lowest-level section header; use count as-is
3237 $totals[$key] = $dao->ct
;
3240 // other aliases are higher level; roll count into their total
3241 $totals[$key] +
= $dao->ct
;
3245 $this->assign('sectionTotals', $totals);
3250 * Modify column headers.
3252 public function modifyColumnHeaders() {
3253 // use this method to modify $this->_columnHeaders
3257 * Move totals columns to the right edge of the table.
3259 * It seems like a more logical layout to have any totals columns on the far right regardless of
3260 * the location of the rest of their table.
3262 public function moveSummaryColumnsToTheRightHandSide() {
3263 $statHeaders = (array_intersect_key($this->_columnHeaders
, array_flip($this->_statFields
)));
3264 $this->_columnHeaders
= array_merge(array_diff_key($this->_columnHeaders
, $statHeaders), $this->_columnHeaders
, $statHeaders);
3268 * Assign rows to the template.
3270 * @param array $rows
3272 public function doTemplateAssignment(&$rows) {
3273 $this->assign_by_ref('columnHeaders', $this->_columnHeaders
);
3274 $this->assign_by_ref('rows', $rows);
3275 $this->assign('statistics', $this->statistics($rows));
3279 * Build report statistics.
3281 * Override this method to build your own statistics.
3283 * @param array $rows
3287 public function statistics(&$rows) {
3290 $count = count($rows);
3291 // Why do we increment the count for rollup seems to artificially inflate the count.
3292 // It seems perhaps intentional to include the summary row in the count of results - although
3293 // this just seems odd.
3294 if ($this->_rollup
&& ($this->_rollup
!= '') && $this->_grandFlag
) {
3298 $this->countStat($statistics, $count);
3300 $this->groupByStat($statistics);
3302 $this->filterStat($statistics);
3308 * Add count statistics.
3310 * @param array $statistics
3313 public function countStat(&$statistics, $count) {
3314 $statistics['counts']['rowCount'] = [
3315 'title' => ts('Row(s) Listed'),
3319 if ($this->_rowsFound
&& ($this->_rowsFound
> $count)) {
3320 $statistics['counts']['rowsFound'] = [
3321 'title' => ts('Total Row(s)'),
3322 'value' => $this->_rowsFound
,
3328 * Add group by statistics.
3330 * @param array $statistics
3332 public function groupByStat(&$statistics) {
3333 if (!empty($this->_params
['group_bys']) &&
3334 is_array($this->_params
['group_bys']) &&
3335 !empty($this->_params
['group_bys'])
3337 foreach ($this->_columns
as $tableName => $table) {
3338 if (array_key_exists('group_bys', $table)) {
3339 foreach ($table['group_bys'] as $fieldName => $field) {
3340 if (!empty($this->_params
['group_bys'][$fieldName])) {
3341 $combinations[] = $field['title'];
3346 $statistics['groups'][] = [
3347 'title' => ts('Grouping(s)'),
3348 'value' => implode(' & ', $combinations),
3354 * Filter statistics.
3356 * @param array $statistics
3358 public function filterStat(&$statistics) {
3359 foreach ($this->_columns
as $tableName => $table) {
3360 if (array_key_exists('filters', $table)) {
3361 foreach ($table['filters'] as $fieldName => $field) {
3362 if ((CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE ||
3363 CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_TIME
) &&
3364 CRM_Utils_Array
::value('operatorType', $field) !=
3365 CRM_Report_Form
::OP_MONTH
3367 $from = $this->_params
["{$fieldName}_from"] ??
NULL;
3368 $to = $this->_params
["{$fieldName}_to"] ??
NULL;
3369 if (!empty($this->_params
["{$fieldName}_relative"])) {
3370 list($from, $to) = CRM_Utils_Date
::getFromTo($this->_params
["{$fieldName}_relative"], NULL, NULL);
3372 if (strlen($to) === 10) {
3373 // If we just have the date we assume the end of that day.
3378 $from = date('l j F Y, g:iA', strtotime($from));
3381 $to = date('l j F Y, g:iA', strtotime($to));
3383 $statistics['filters'][] = [
3384 'title' => $field['title'],
3385 'value' => ts("Between %1 and %2", [1 => $from, 2 => $to]),
3388 elseif (in_array($rel = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
),
3389 array_keys($this->getOperationPair(CRM_Report_Form
::OP_DATE
))
3391 $pair = $this->getOperationPair(CRM_Report_Form
::OP_DATE
);
3392 $statistics['filters'][] = [
3393 'title' => $field['title'],
3394 'value' => $pair[$rel],
3399 $op = $this->_params
["{$fieldName}_op"] ??
NULL;
3402 $pair = $this->getOperationPair(
3403 CRM_Utils_Array
::value('operatorType', $field),
3406 $min = $this->_params
["{$fieldName}_min"] ??
NULL;
3407 $max = $this->_params
["{$fieldName}_max"] ??
NULL;
3408 $val = $this->_params
["{$fieldName}_value"] ??
NULL;
3409 if (in_array($op, ['bw', 'nbw']) && ($min ||
$max)) {
3410 $value = "{$pair[$op]} $min " . ts('and') . " $max";
3412 elseif ($val && CRM_Utils_Array
::value('operatorType', $field) & self
::OP_ENTITYREF
) {
3413 $this->setEntityRefDefaults($field, $tableName);
3414 $result = civicrm_api3($field['attributes']['entity'], 'getlist',
3416 CRM_Utils_Array
::value('api', $field['attributes'], []));
3418 foreach ($result['values'] as $v) {
3419 $values[] = $v['label'];
3421 $value = "{$pair[$op]} " . implode(', ', $values);
3423 elseif ($op == 'nll' ||
$op == 'nnll') {
3424 $value = $pair[$op];
3426 elseif (is_array($val) && (!empty($val))) {
3427 $options = CRM_Utils_Array
::value('options', $field, []);
3428 foreach ($val as $key => $valIds) {
3429 if (isset($options[$valIds])) {
3430 $val[$key] = $options[$valIds];
3433 $pair[$op] = (count($val) == 1) ?
(($op == 'notin' ||
$op ==
3434 'mnot') ?
ts('Is Not') : ts('Is')) : CRM_Utils_Array
::value($op, $pair);
3435 $val = implode(', ', $val);
3436 $value = "{$pair[$op]} " . $val;
3438 elseif (!is_array($val) && (!empty($val) ||
$val == '0') &&
3439 isset($field['options']) &&
3440 is_array($field['options']) && !empty($field['options'])
3442 $value = CRM_Utils_Array
::value($op, $pair) . " " .
3443 CRM_Utils_Array
::value($val, $field['options'], $val);
3446 $value = CRM_Utils_Array
::value($op, $pair) . " " . $val;
3449 if ($value && empty($field['no_display'])) {
3450 $statistics['filters'][] = [
3451 'title' => $field['title'] ??
NULL,
3452 'value' => CRM_Utils_String
::htmlToText($value),
3462 * End post processing.
3464 * @param array|null $rows
3466 public function endPostProcess(&$rows = NULL) {
3467 $this->assign('report_class', get_class($this));
3469 // This is used by unit tests, where _outputMode is intentionally blank.
3470 // Is there a reason it couldn't just always do this? It effectively does
3471 // the same thing anyway by assigning it to the template (in
3472 // doTemplateAssignment()).
3473 if ($this->_storeResultSet
) {
3474 $this->_resultSet
= $rows;
3477 // Add contacts to group
3478 if ($this->_outputMode
== 'group') {
3479 $group = $this->_params
['groups'];
3480 $this->add2group($group);
3483 if ($this->_sendmail
) {
3486 elseif (!empty($this->outputHandler
)) {
3487 $this->outputHandler
->download();
3488 CRM_Utils_System
::civiExit();
3490 // else we don't need to do anything here since it must have been
3491 // outputMode=save or something like that
3496 * Set store result set indicator to TRUE.
3498 * This is used by unit tests, along with getResultSet(), to get just
3499 * the output rows unformatted.
3501 public function storeResultSet() {
3502 $this->_storeResultSet
= TRUE;
3510 public function getResultSet() {
3511 return $this->_resultSet
;
3515 * Get the sql used to generate the report.
3519 public function getReportSql() {
3520 return $this->sqlArray
;
3524 * Use the form name to create the tpl file name.
3528 public function getTemplateFileName() {
3529 $defaultTpl = parent
::getTemplateFileName();
3530 $template = CRM_Core_Smarty
::singleton();
3531 if (!$template->template_exists($defaultTpl)) {
3532 $defaultTpl = 'CRM/Report/Form.tpl';
3538 * Compile the report content.
3540 * Although this function is super-short it is useful to keep separate so it can be over-ridden by report classes.
3544 public function compileContent() {
3545 $templateFile = $this->getHookedTemplateFileName();
3546 return CRM_Utils_Array
::value('report_header', $this->_formValues
) .
3547 CRM_Core_Form
::$_template->fetch($templateFile) .
3548 CRM_Utils_Array
::value('report_footer', $this->_formValues
);
3552 * Post process function.
3554 public function postProcess() {
3555 // get ready with post process params
3556 $this->beginPostProcess();
3559 $sql = $this->buildQuery();
3561 // build array of result based on column headers. This method also allows
3562 // modifying column headers before using it to build result set i.e $rows.
3564 $this->buildRows($sql, $rows);
3566 // format result set.
3567 $this->formatDisplay($rows);
3569 // assign variables to templates
3570 $this->doTemplateAssignment($rows);
3572 // do print / pdf / instance stuff if needed
3573 $this->endPostProcess($rows);
3579 * @param int $rowCount
3583 public function limit($rowCount = self
::ROW_COUNT_LIMIT
) {
3584 // lets do the pager if in html mode
3585 $this->_limit
= NULL;
3587 // CRM-14115, over-ride row count if rowCount is specified in URL
3588 if ($this->_dashBoardRowCount
) {
3589 $rowCount = $this->_dashBoardRowCount
;
3591 if ($this->addPaging
) {
3592 $this->_select
= preg_replace('/SELECT(\s+SQL_CALC_FOUND_ROWS)?\s+/i', 'SELECT SQL_CALC_FOUND_ROWS ', $this->_select
);
3594 $pageId = CRM_Utils_Request
::retrieve('crmPID', 'Integer');
3596 // @todo all http vars should be extracted in the preProcess
3597 // - not randomly in the class
3598 if (!$pageId && !empty($_POST)) {
3599 if (isset($_POST['PagerBottomButton']) && isset($_POST['crmPID_B'])) {
3600 $pageId = max((int) $_POST['crmPID_B'], 1);
3602 elseif (isset($_POST['PagerTopButton']) && isset($_POST['crmPID'])) {
3603 $pageId = max((int) $_POST['crmPID'], 1);
3605 unset($_POST['crmPID_B'], $_POST['crmPID']);
3608 $pageId = $pageId ?
$pageId : 1;
3609 $this->set(CRM_Utils_Pager
::PAGE_ID
, $pageId);
3610 $offset = ($pageId - 1) * $rowCount;
3612 $offset = CRM_Utils_Type
::escape($offset, 'Int');
3613 $rowCount = CRM_Utils_Type
::escape($rowCount, 'Int');
3615 $this->_limit
= " LIMIT $offset, $rowCount";
3616 return [$offset, $rowCount];
3618 if ($this->_limitValue
) {
3619 if ($this->_offsetValue
) {
3620 $this->_limit
= " LIMIT {$this->_offsetValue}, {$this->_limitValue} ";
3623 $this->_limit
= " LIMIT " . $this->_limitValue
;
3631 * @param int $rowCount
3633 public function setPager($rowCount = self
::ROW_COUNT_LIMIT
) {
3634 // CRM-14115, over-ride row count if rowCount is specified in URL
3635 if ($this->_dashBoardRowCount
) {
3636 $rowCount = $this->_dashBoardRowCount
;
3639 if ($this->_limit
&& ($this->_limit
!= '')) {
3640 if (!$this->_rowsFound
) {
3641 $sql = "SELECT FOUND_ROWS();";
3642 $this->_rowsFound
= CRM_Core_DAO
::singleValueQuery($sql);
3645 'total' => $this->_rowsFound
,
3646 'rowCount' => $rowCount,
3647 'status' => ts('Records') . ' %%StatusMessage%%',
3648 'buttonBottom' => 'PagerBottomButton',
3649 'buttonTop' => 'PagerTopButton',
3651 if (!empty($this->controller
)) {
3652 // This happens when being called from the api Really we want the api to be able to
3653 // pass paging parameters, but at this stage just preventing test crashes.
3654 $params['pageID'] = $this->get(CRM_Utils_Pager
::PAGE_ID
);
3657 $pager = new CRM_Utils_Pager($params);
3658 $this->assign_by_ref('pager', $pager);
3659 $this->ajaxResponse
['totalRows'] = $this->_rowsFound
;
3664 * Build a group filter with contempt for large data sets.
3666 * This function has been retained as it takes time to migrate the reports over
3667 * to the new method which will not crash on large datasets.
3671 * @param string $field
3672 * @param mixed $value
3677 public function legacySlowGroupFilterClause($field, $value, $op) {
3678 $smartGroupQuery = "";
3680 $group = new CRM_Contact_DAO_Group();
3681 $group->is_active
= 1;
3684 while ($group->fetch()) {
3685 if (in_array($group->id
, (array) $this->_params
['gid_value']) &&
3686 $group->saved_search_id
3688 $smartGroups[] = $group->id
;
3692 CRM_Contact_BAO_GroupContactCache
::check($smartGroups);
3694 $smartGroupQuery = '';
3695 if (!empty($smartGroups)) {
3696 $smartGroups = implode(',', $smartGroups);
3697 $smartGroupQuery = " UNION DISTINCT
3698 SELECT DISTINCT smartgroup_contact.contact_id
3699 FROM civicrm_group_contact_cache smartgroup_contact
3700 WHERE smartgroup_contact.group_id IN ({$smartGroups}) ";
3703 $sqlOp = $this->getSQLOperator($op);
3704 if (!is_array($value)) {
3707 //include child groups if any
3708 $value = array_merge($value, CRM_Contact_BAO_Group
::getChildGroupIds($value));
3710 $clause = "{$field['dbAlias']} IN (" . implode(', ', $value) . ")";
3712 $contactAlias = $this->_aliases
['civicrm_contact'];
3713 if (!empty($this->relationType
) && $this->relationType
== 'b_a') {
3714 $contactAlias = $this->_aliases
['civicrm_contact_b'];
3716 return " {$contactAlias}.id {$sqlOp} (
3717 SELECT DISTINCT {$this->_aliases['civicrm_group']}.contact_id
3718 FROM civicrm_group_contact {$this->_aliases['civicrm_group']}
3719 WHERE {$clause} AND {$this->_aliases['civicrm_group']}.status = 'Added'
3720 {$smartGroupQuery} ) ";
3724 * Build where clause for groups.
3726 * @param string $field
3727 * @param mixed $value
3732 public function whereGroupClause($field, $value, $op) {
3733 if ($this->groupFilterNotOptimised
) {
3734 return $this->legacySlowGroupFilterClause($field, $value, $op);
3736 if ($op === 'notin') {
3737 return " group_temp_table.id IS NULL ";
3739 // We will have used an inner join instead.
3744 * Create a table of the contact ids included by the group filter.
3746 * This function is called by both the api (tests) and the UI.
3748 public function buildGroupTempTable() {
3749 if (!empty($this->groupTempTable
) ||
empty($this->_params
['gid_value']) ||
$this->groupFilterNotOptimised
) {
3752 $filteredGroups = (array) $this->_params
['gid_value'];
3754 $groups = civicrm_api3('Group', 'get', [
3756 'id' => ['IN' => $filteredGroups],
3757 'saved_search_id' => ['>' => 0],
3760 $smartGroups = array_keys($groups['values']);
3763 SELECT DISTINCT group_contact.contact_id as id
3764 FROM civicrm_group_contact group_contact
3765 WHERE group_contact.group_id IN (" . implode(', ', $filteredGroups) . ")
3766 AND group_contact.status = 'Added' ";
3768 if (!empty($smartGroups)) {
3769 CRM_Contact_BAO_GroupContactCache
::check($smartGroups);
3770 $smartGroups = implode(',', $smartGroups);
3773 SELECT smartgroup_contact.contact_id as id
3774 FROM civicrm_group_contact_cache smartgroup_contact
3775 WHERE smartgroup_contact.group_id IN ({$smartGroups}) ";
3778 $this->groupTempTable
= $this->createTemporaryTable('rptgrp', $query);
3779 CRM_Core_DAO
::executeQuery("ALTER TABLE $this->groupTempTable ADD INDEX i_id(id)");
3783 * Execute query and add it to the developer tab.
3785 * @param string $query
3786 * @param array $params
3788 * @return \CRM_Core_DAO|object
3790 protected function executeReportQuery($query, $params = []) {
3791 $this->addToDeveloperTab($query);
3792 return CRM_Core_DAO
::executeQuery($query, $params);
3796 * Build where clause for tags.
3798 * @param string $field
3799 * @param mixed $value
3804 public function whereTagClause($field, $value, $op) {
3805 // not using left join in query because if any contact
3806 // belongs to more than one tag, results duplicate
3808 $sqlOp = $this->getSQLOperator($op);
3809 if (!is_array($value)) {
3812 $clause = "{$field['dbAlias']} IN (" . implode(', ', $value) . ")";
3813 $entity_table = $this->_tagFilterTable
;
3814 return " {$this->_aliases[$entity_table]}.id {$sqlOp} (
3815 SELECT DISTINCT {$this->_aliases['civicrm_tag']}.entity_id
3816 FROM civicrm_entity_tag {$this->_aliases['civicrm_tag']}
3817 WHERE entity_table = '$entity_table' AND {$clause} ) ";
3821 * Generate membership organization clause.
3823 * @param mixed $value
3824 * @param string $op SQL Operator
3828 public function whereMembershipOrgClause($value, $op) {
3829 $sqlOp = $this->getSQLOperator($op);
3830 if (!is_array($value)) {
3834 $tmp_membership_org_sql_list = implode(', ', $value);
3835 return " {$this->_aliases['civicrm_contact']}.id {$sqlOp} (
3836 SELECT DISTINCT mem.contact_id
3837 FROM civicrm_membership mem
3838 LEFT JOIN civicrm_membership_status mem_status ON mem.status_id = mem_status.id
3839 LEFT JOIN civicrm_membership_type mt ON mem.membership_type_id = mt.id
3840 WHERE mt.member_of_contact_id IN (" .
3841 $tmp_membership_org_sql_list . ")
3842 AND mt.is_active = '1'
3843 AND mem_status.is_current_member = '1'
3844 AND mem_status.is_active = '1' ) ";
3848 * Generate Membership Type SQL Clause.
3850 * @param mixed $value
3856 public function whereMembershipTypeClause($value, $op) {
3857 $sqlOp = $this->getSQLOperator($op);
3858 if (!is_array($value)) {
3862 $tmp_membership_sql_list = implode(', ', $value);
3863 return " {$this->_aliases['civicrm_contact']}.id {$sqlOp} (
3864 SELECT DISTINCT mem.contact_id
3865 FROM civicrm_membership mem
3866 LEFT JOIN civicrm_membership_status mem_status ON mem.status_id = mem_status.id
3867 LEFT JOIN civicrm_membership_type mt ON mem.membership_type_id = mt.id
3868 WHERE mem.membership_type_id IN (" .
3869 $tmp_membership_sql_list . ")
3870 AND mt.is_active = '1'
3871 AND mem_status.is_current_member = '1'
3872 AND mem_status.is_active = '1' ) ";
3876 * Buld contact acl clause
3877 * @deprecated in favor of buildPermissionClause
3879 * Note that if the buildPermissionClause function is called (which most reports do from
3880 * buildQuery then the results of this function are re-calculated and overwritten.
3882 * @param string $tableAlias
3884 public function buildACLClause($tableAlias = 'contact_a') {
3885 list($this->_aclFrom
, $this->_aclWhere
) = CRM_Contact_BAO_Contact_Permission
::cacheClause($tableAlias);
3889 * Build the permision clause for all entities in this report
3891 public function buildPermissionClause() {
3893 foreach ($this->selectedTables() as $tableName) {
3894 $baoName = str_replace('_DAO_', '_BAO_', CRM_Core_DAO_AllCoreTables
::getClassForTable($tableName));
3895 if ($baoName && class_exists($baoName) && !empty($this->_columns
[$tableName]['alias'])) {
3896 $tableAlias = $this->_columns
[$tableName]['alias'];
3897 $clauses = array_filter($baoName::getSelectWhereClause($tableAlias));
3898 foreach ($clauses as $field => $clause) {
3899 // Skip contact_id field if redundant
3900 if ($field != 'contact_id' ||
!in_array('civicrm_contact', $this->selectedTables())) {
3901 $ret["$tableName.$field"] = $clause;
3906 // Override output from buildACLClause
3907 $this->_aclFrom
= NULL;
3908 $this->_aclWhere
= implode(' AND ', $ret);
3912 * Add custom data to the columns.
3914 * @param bool $addFields
3915 * @param array $permCustomGroupIds
3917 public function addCustomDataToColumns($addFields = TRUE, $permCustomGroupIds = []) {
3918 if (empty($this->_customGroupExtends
)) {
3921 if (!is_array($this->_customGroupExtends
)) {
3922 $this->_customGroupExtends
= [$this->_customGroupExtends
];
3924 $customGroupWhere = '';
3925 if (!empty($permCustomGroupIds)) {
3926 $customGroupWhere = "cg.id IN (" . implode(',', $permCustomGroupIds) .
3930 SELECT cg.table_name, cg.title, cg.extends, cf.id as cf_id, cf.label,
3931 cf.column_name, cf.data_type, cf.html_type, cf.option_group_id, cf.time_format,
3932 cf.serialize as serialize
3933 FROM civicrm_custom_group cg
3934 INNER JOIN civicrm_custom_field cf ON cg.id = cf.custom_group_id
3935 WHERE cg.extends IN ('" . implode("','", $this->_customGroupExtends
) . "') AND
3937 cg.is_active = 1 AND
3938 cf.is_active = 1 AND
3939 cf.is_searchable = 1
3940 ORDER BY cg.weight, cf.weight";
3941 $customDAO = CRM_Core_DAO
::executeQuery($sql);
3944 while ($customDAO->fetch()) {
3945 if ($customDAO->table_name
!= $curTable) {
3946 $curTable = $customDAO->table_name
;
3947 $curFields = $curFilters = [];
3950 $this->_columns
[$curTable]['dao'] = 'CRM_Contact_DAO_Contact';
3951 $this->_columns
[$curTable]['extends'] = $customDAO->extends;
3952 $this->_columns
[$curTable]['grouping'] = $customDAO->table_name
;
3953 $this->_columns
[$curTable]['group_title'] = $customDAO->title
;
3955 foreach (['fields', 'filters', 'group_bys'] as $colKey) {
3956 if (!array_key_exists($colKey, $this->_columns
[$curTable])) {
3957 $this->_columns
[$curTable][$colKey] = [];
3961 $fieldName = 'custom_' . $customDAO->cf_id
;
3964 // this makes aliasing work in favor
3965 $curFields[$fieldName] = [
3966 'name' => $customDAO->column_name
,
3967 'title' => $customDAO->label
,
3968 'dataType' => $customDAO->data_type
,
3969 'htmlType' => $customDAO->html_type
,
3972 if ($this->_customGroupFilters
) {
3973 // this makes aliasing work in favor
3974 $curFilters[$fieldName] = [
3975 'name' => $customDAO->column_name
,
3976 'title' => $customDAO->label
,
3977 'dataType' => $customDAO->data_type
,
3978 'htmlType' => $customDAO->html_type
,
3982 switch ($customDAO->data_type
) {
3985 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form
::OP_DATE
;
3986 $curFilters[$fieldName]['type'] = CRM_Utils_Type
::T_DATE
;
3987 // CRM-6946, show time part for datetime date fields
3988 if ($customDAO->time_format
) {
3989 $curFields[$fieldName]['type'] = CRM_Utils_Type
::T_TIMESTAMP
;
3994 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form
::OP_SELECT
;
3995 $curFilters[$fieldName]['options'] = ['' => ts('- select -')] + CRM_Core_PseudoConstant
::get('CRM_Core_BAO_CustomField', 'custom_' . $customDAO->cf_id
, [], 'search');
3996 $curFilters[$fieldName]['type'] = CRM_Utils_Type
::T_INT
;
4000 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form
::OP_INT
;
4001 $curFilters[$fieldName]['type'] = CRM_Utils_Type
::T_INT
;
4005 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form
::OP_FLOAT
;
4006 $curFilters[$fieldName]['type'] = CRM_Utils_Type
::T_MONEY
;
4007 // Use T_FLOAT instead of T_MONEY as the money number format happens
4008 // by calling CRM_Core_BAO_CustomField::displayValue in alterCustomDataDisplay
4009 $curFields[$fieldName]['type'] = CRM_Utils_Type
::T_FLOAT
;
4013 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form
::OP_FLOAT
;
4014 $curFilters[$fieldName]['type'] = CRM_Utils_Type
::T_FLOAT
;
4018 case 'StateProvince':
4020 $curFilters[$fieldName]['type'] = CRM_Utils_Type
::T_STRING
;
4022 $options = CRM_Core_PseudoConstant
::get('CRM_Core_BAO_CustomField', 'custom_' . $customDAO->cf_id
, [], 'search');
4023 if ((is_array($options) && count($options) != 0) ||
(!is_array($options) && $options !== FALSE)) {
4024 $curFilters[$fieldName]['operatorType'] = CRM_Core_BAO_CustomField
::isSerialized($customDAO) ? CRM_Report_Form
::OP_MULTISELECT_SEPARATOR
: CRM_Report_Form
::OP_MULTISELECT
;
4025 $curFilters[$fieldName]['options'] = $options;
4029 case 'ContactReference':
4030 $curFilters[$fieldName]['type'] = CRM_Utils_Type
::T_STRING
;
4031 $curFilters[$fieldName]['name'] = 'display_name';
4032 $curFilters[$fieldName]['alias'] = "contact_{$fieldName}_civireport";
4034 $curFields[$fieldName]['type'] = CRM_Utils_Type
::T_STRING
;
4035 $curFields[$fieldName]['name'] = 'display_name';
4036 $curFields[$fieldName]['alias'] = "contact_{$fieldName}_civireport";
4040 $curFields[$fieldName]['type'] = CRM_Utils_Type
::T_STRING
;
4041 $curFilters[$fieldName]['type'] = CRM_Utils_Type
::T_STRING
;
4045 if ($customDAO->html_type
== 'Select' && !array_key_exists('options', $curFilters[$fieldName])) {
4046 $options = CRM_Core_PseudoConstant
::get('CRM_Core_BAO_CustomField', 'custom_' . $customDAO->cf_id
, [], 'search');
4047 if ($options !== FALSE) {
4048 $curFilters[$fieldName]['operatorType'] = CRM_Core_BAO_CustomField
::isSerialized($customDAO) ? CRM_Report_Form
::OP_MULTISELECT_SEPARATOR
: CRM_Report_Form
::OP_MULTISELECT
;
4049 $curFilters[$fieldName]['options'] = $options;
4053 if (!array_key_exists('type', $curFields[$fieldName])) {
4054 $curFields[$fieldName]['type'] = CRM_Utils_Array
::value('type', $curFilters[$fieldName], []);
4058 $this->_columns
[$curTable]['fields'] = array_merge($this->_columns
[$curTable]['fields'], $curFields);
4060 if ($this->_customGroupFilters
) {
4061 $this->_columns
[$curTable]['filters'] = array_merge($this->_columns
[$curTable]['filters'], $curFilters);
4063 if ($this->_customGroupGroupBy
) {
4064 $this->_columns
[$curTable]['group_bys'] = array_merge($this->_columns
[$curTable]['group_bys'], $curFields);
4070 * Build custom data from clause.
4072 * @param bool $joinsForFiltersOnly
4073 * Only include joins to support filters. This would be used if creating a table of contacts to include first.
4075 public function customDataFrom($joinsForFiltersOnly = FALSE) {
4076 if (empty($this->_customGroupExtends
)) {
4079 $mapper = CRM_Core_BAO_CustomQuery
::$extendsMap;
4080 //CRM-18276 GROUP_CONCAT could be used with singleValueQuery and then exploded,
4081 //but by default that truncates to 1024 characters, which causes errors with installs with lots of custom field sets
4083 $customTablesDAO = CRM_Core_DAO
::executeQuery("SELECT table_name FROM civicrm_custom_group");
4084 while ($customTablesDAO->fetch()) {
4085 $customTables[] = $customTablesDAO->table_name
;
4088 foreach ($this->_columns
as $table => $prop) {
4089 if (in_array($table, $customTables)) {
4090 $extendsTable = $mapper[$prop['extends']];
4091 // Check field is required for rendering the report.
4092 if ((!$this->isFieldSelected($prop)) ||
($joinsForFiltersOnly && !$this->isFieldFiltered($prop))) {
4095 $baseJoin = CRM_Utils_Array
::value($prop['extends'], $this->_customGroupExtendsJoin
, "{$this->_aliases[$extendsTable]}.id");
4097 $customJoin = is_array($this->_customGroupJoin
) ?
$this->_customGroupJoin
[$table] : $this->_customGroupJoin
;
4099 {$customJoin} {$table} {$this->_aliases[$table]} ON {$this->_aliases[$table]}.entity_id = {$baseJoin}";
4100 // handle for ContactReference
4101 if (array_key_exists('fields', $prop)) {
4102 foreach ($prop['fields'] as $fieldName => $field) {
4103 if (CRM_Utils_Array
::value('dataType', $field) ==
4106 $columnName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField', CRM_Core_BAO_CustomField
::getKeyID($fieldName), 'column_name');
4108 LEFT JOIN civicrm_contact {$field['alias']} ON {$field['alias']}.id = {$this->_aliases[$table]}.{$columnName} ";
4117 * Check if the field is selected.
4119 * @param string $prop
4123 public function isFieldSelected($prop) {
4128 if (!empty($this->_params
['fields'])) {
4129 foreach (array_keys($prop['fields']) as $fieldAlias) {
4130 $customFieldId = CRM_Core_BAO_CustomField
::getKeyID($fieldAlias);
4131 if ($customFieldId) {
4132 if (array_key_exists($fieldAlias, $this->_params
['fields'])) {
4136 //might be survey response field.
4137 if (!empty($this->_params
['fields']['survey_response']) &&
4138 !empty($prop['fields'][$fieldAlias]['isSurveyResponseField'])
4146 if (!empty($this->_params
['group_bys']) && $this->_customGroupGroupBy
) {
4147 foreach (array_keys($prop['group_bys']) as $fieldAlias) {
4148 if (array_key_exists($fieldAlias, $this->_params
['group_bys']) &&
4149 CRM_Core_BAO_CustomField
::getKeyID($fieldAlias)
4156 if (!empty($this->_params
['order_bys'])) {
4157 foreach (array_keys($prop['fields']) as $fieldAlias) {
4158 foreach ($this->_params
['order_bys'] as $orderBy) {
4159 if ($fieldAlias == $orderBy['column'] &&
4160 CRM_Core_BAO_CustomField
::getKeyID($fieldAlias)
4167 return $this->isFieldFiltered($prop);
4172 * Check if the field is used as a filter.
4174 * @param string $prop
4178 protected function isFieldFiltered($prop) {
4179 if (!empty($prop['filters']) && $this->_customGroupFilters
) {
4180 foreach ($prop['filters'] as $fieldAlias => $val) {
4189 if (isset($this->_params
[$fieldAlias . '_' . $attach]) &&
4190 (!empty($this->_params
[$fieldAlias . '_' . $attach])
4191 ||
($attach != 'relative' &&
4192 $this->_params
[$fieldAlias . '_' . $attach] == '0')
4198 if (!empty($this->_params
[$fieldAlias . '_op']) &&
4199 in_array($this->_params
[$fieldAlias . '_op'], ['nll', 'nnll'])
4210 * Check for empty order_by configurations and remove them.
4212 * Also set template to hide them.
4214 * @param array $formValues
4216 public function preProcessOrderBy(&$formValues) {
4217 // Object to show/hide form elements
4218 $_showHide = new CRM_Core_ShowHideBlocks('', '');
4220 $_showHide->addShow('optionField_1');
4222 // Cycle through order_by options; skip any empty ones, and hide them as well
4225 if (!empty($formValues['order_bys'])) {
4226 foreach ($formValues['order_bys'] as $order_by) {
4227 if ($order_by['column'] && $order_by['column'] != '-') {
4228 $_showHide->addShow('optionField_' . $n);
4229 $orderBys[$n] = $order_by;
4234 for ($i = $n; $i <= 5; $i++
) {
4236 $_showHide->addHide('optionField_' . $i);
4240 // overwrite order_by options with modified values
4241 if (!empty($orderBys)) {
4242 $formValues['order_bys'] = $orderBys;
4245 $formValues['order_bys'] = [1 => ['column' => '-']];
4248 // assign show/hide data to template
4249 $_showHide->addToTemplate();
4253 * Check if table name has columns in SELECT clause.
4255 * @param string $tableName
4256 * Name of table (index of $this->_columns array).
4260 public function isTableSelected($tableName) {
4261 return in_array($tableName, $this->selectedTables());
4265 * Check if table name has columns in WHERE or HAVING clause.
4267 * @param string $tableName
4268 * Name of table (index of $this->_columns array).
4272 public function isTableFiltered($tableName) {
4273 // Cause the array to be generated if not previously done.
4274 if (!$this->_selectedTables
&& !$this->filteredTables
) {
4275 $this->selectedTables();
4277 return in_array($tableName, $this->filteredTables
);
4281 * Fetch array of DAO tables having columns included in SELECT or ORDER BY clause.
4283 * If the array is unset it will be built.
4288 public function selectedTables() {
4289 if (!$this->_selectedTables
) {
4290 $orderByColumns = [];
4291 if (array_key_exists('order_bys', $this->_params
) &&
4292 is_array($this->_params
['order_bys'])
4294 foreach ($this->_params
['order_bys'] as $orderBy) {
4295 $orderByColumns[] = $orderBy['column'];
4299 foreach ($this->_columns
as $tableName => $table) {
4300 if (array_key_exists('fields', $table)) {
4301 foreach ($table['fields'] as $fieldName => $field) {
4302 if (!empty($field['required']) ||
4303 !empty($this->_params
['fields'][$fieldName])
4305 $this->_selectedTables
[] = $tableName;
4310 if (array_key_exists('order_bys', $table)) {
4311 foreach ($table['order_bys'] as $orderByName => $orderBy) {
4312 if (in_array($orderByName, $orderByColumns)) {
4313 $this->_selectedTables
[] = $tableName;
4318 if (array_key_exists('filters', $table)) {
4319 foreach ($table['filters'] as $filterName => $filter) {
4320 $filterOp = $this->_params
["{$filterName}_op"] ??
'';
4321 if ((isset($this->_params
["{$filterName}_value"])
4322 && !CRM_Utils_System
::isNull($this->_params
["{$filterName}_value"]))
4323 ||
!empty($this->_params
["{$filterName}_relative"])
4324 ||
$filterOp === 'nll'
4325 ||
$filterOp === 'nnll'
4327 $this->_selectedTables
[] = $tableName;
4328 $this->filteredTables
[] = $tableName;
4335 return $this->_selectedTables
;
4339 * Add campaign fields.
4340 * @param string $entityTable
4341 * @param bool $groupBy
4342 * Add GroupBy? Not appropriate for detail report.
4343 * @param bool $orderBy
4344 * Add OrderBy? Not appropriate for detail report.
4345 * @param bool $filters
4348 public function addCampaignFields($entityTable = 'civicrm_contribution', $groupBy = FALSE, $orderBy = FALSE, $filters = TRUE) {
4349 // Check if CiviCampaign is a) enabled and b) has active campaigns
4350 $config = CRM_Core_Config
::singleton();
4351 $campaignEnabled = in_array('CiviCampaign', $config->enableComponents
);
4352 if ($campaignEnabled) {
4353 $getCampaigns = CRM_Campaign_BAO_Campaign
::getPermissionedCampaigns(NULL, NULL, FALSE, FALSE, TRUE);
4354 // If we have a campaign, build out the relevant elements
4355 if (!empty($getCampaigns['campaigns'])) {
4356 $this->campaigns
= $getCampaigns['campaigns'];
4357 asort($this->campaigns
);
4358 $this->_columns
[$entityTable]['fields']['campaign_id'] = ['title' => ts('Campaign'), 'default' => 'false'];
4360 $this->_columns
[$entityTable]['filters']['campaign_id'] = [
4361 'title' => ts('Campaign'),
4362 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
4363 'options' => $this->campaigns
,
4364 'type' => CRM_Utils_Type
::T_INT
,
4369 $this->_columns
[$entityTable]['group_bys']['campaign_id'] = ['title' => ts('Campaign')];
4373 $this->_columns
[$entityTable]['order_bys']['campaign_id'] = ['title' => ts('Campaign')];
4380 * Add address fields.
4382 * @deprecated - use getAddressColumns which is a more accurate description
4383 * and also accepts an array of options rather than a long list
4385 * adding address fields to construct function in reports
4387 * @param bool $groupBy
4388 * Add GroupBy? Not appropriate for detail report.
4389 * @param bool $orderBy
4390 * Add GroupBy? Not appropriate for detail report.
4391 * @param bool $filters
4392 * @param array $defaults
4395 * address fields for construct clause
4397 public function addAddressFields($groupBy = TRUE, $orderBy = FALSE, $filters = TRUE, $defaults = ['country_id' => TRUE]) {
4398 $defaultAddressFields = [
4399 'street_address' => ts('Street Address'),
4400 'supplemental_address_1' => ts('Supplementary Address Field 1'),
4401 'supplemental_address_2' => ts('Supplementary Address Field 2'),
4402 'supplemental_address_3' => ts('Supplementary Address Field 3'),
4403 'street_number' => ts('Street Number'),
4404 'street_name' => ts('Street Name'),
4405 'street_unit' => ts('Street Unit'),
4406 'city' => ts('City'),
4407 'postal_code' => ts('Postal Code'),
4408 'postal_code_suffix' => ts('Postal Code Suffix'),
4409 'country_id' => ts('Country'),
4410 'state_province_id' => ts('State/Province'),
4411 'county_id' => ts('County'),
4414 'civicrm_address' => [
4415 'dao' => 'CRM_Core_DAO_Address',
4418 'title' => ts('Address Name'),
4419 'default' => $defaults['name'] ??
FALSE,
4423 'grouping' => 'location-fields',
4426 foreach ($defaultAddressFields as $fieldName => $fieldLabel) {
4427 $addressFields['civicrm_address']['fields'][$fieldName] = [
4428 'title' => $fieldLabel,
4429 'default' => $defaults[$fieldName] ??
FALSE,
4433 $street_address_filters = $general_address_filters = [];
4435 // Address filter depends on whether street address parsing is enabled.
4437 $addressOptions = CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
4440 if ($addressOptions['street_address_parsing']) {
4441 $street_address_filters = [
4442 'street_number' => [
4443 'title' => ts('Street Number'),
4444 'type' => CRM_Utils_Type
::T_INT
,
4445 'name' => 'street_number',
4448 'title' => ts('Street Name'),
4449 'name' => 'street_name',
4450 'type' => CRM_Utils_Type
::T_STRING
,
4455 $street_address_filters = [
4456 'street_address' => [
4457 'title' => ts('Street Address'),
4458 'type' => CRM_Utils_Type
::T_STRING
,
4459 'name' => 'street_address',
4463 $general_address_filters = [
4465 'title' => ts('Postal Code'),
4466 'type' => CRM_Utils_Type
::T_STRING
,
4467 'name' => 'postal_code',
4470 'title' => ts('City'),
4471 'type' => CRM_Utils_Type
::T_STRING
,
4475 'name' => 'country_id',
4476 'title' => ts('Country'),
4477 'type' => CRM_Utils_Type
::T_INT
,
4478 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
4479 'options' => CRM_Core_PseudoConstant
::country(),
4481 'state_province_id' => [
4482 'name' => 'state_province_id',
4483 'title' => ts('State/Province'),
4484 'type' => CRM_Utils_Type
::T_INT
,
4485 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
4489 'name' => 'county_id',
4490 'title' => ts('County'),
4491 'type' => CRM_Utils_Type
::T_INT
,
4492 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
4497 $addressFields['civicrm_address']['filters'] = array_merge(
4498 $street_address_filters,
4499 $general_address_filters);
4502 $addressFields['civicrm_address']['order_bys'] = [
4503 'street_name' => ['title' => ts('Street Name')],
4504 'street_number' => ['title' => ts('Odd / Even Street Number')],
4505 'street_address' => NULL,
4507 'postal_code' => NULL,
4512 $addressFields['civicrm_address']['group_bys'] = [
4513 'street_address' => NULL,
4515 'postal_code' => NULL,
4516 'state_province_id' => [
4517 'title' => ts('State/Province'),
4520 'title' => ts('Country'),
4523 'title' => ts('County'),
4527 return $addressFields;
4531 * Do AlterDisplay processing on Address Fields.
4532 * If there are multiple address field values then
4533 * on basis of provided separator the code values are translated into respective labels
4536 * @param array $rows
4537 * @param int $rowNum
4538 * @param string $baseUrl
4539 * @param string $linkText
4540 * @param string $separator
4544 public function alterDisplayAddressFields(&$row, &$rows, &$rowNum, $baseUrl, $linkText, $separator = ',') {
4545 $criteriaQueryParams = CRM_Report_Utils_Report
::getPreviewCriteriaQueryParams($this->_defaults
, $this->_params
);
4546 $entryFound = FALSE;
4548 'civicrm_address_country_id' => 'country',
4549 'civicrm_address_county_id' => 'county',
4550 'civicrm_address_state_province_id' => 'stateProvince',
4552 foreach ($columnMap as $fieldName => $fnName) {
4553 if (array_key_exists($fieldName, $row)) {
4554 if ($values = $row[$fieldName]) {
4555 $values = (array) explode($separator, $values);
4556 $rows[$rowNum][$fieldName] = [];
4557 $addressField = $fnName == 'stateProvince' ?
'state' : $fnName;
4558 foreach ($values as $value) {
4559 $rows[$rowNum][$fieldName][] = CRM_Core_PseudoConstant
::$fnName($value);
4561 $rows[$rowNum][$fieldName] = implode($separator, $rows[$rowNum][$fieldName]);
4563 $url = CRM_Report_Utils_Report
::getNextUrl($baseUrl,
4564 sprintf("reset=1&force=1&%s&%s_op=in&%s_value=%s",
4565 $criteriaQueryParams,
4566 str_replace('civicrm_address_', '', $fieldName),
4567 str_replace('civicrm_address_', '', $fieldName),
4568 implode(',', $values)
4569 ), $this->_absoluteUrl
, $this->_id
4571 $rows[$rowNum]["{$fieldName}_link"] = $url;
4572 $rows[$rowNum]["{$fieldName}_hover"] = ts("%1 for this %2.", [1 => $linkText, 2 => $addressField]);
4583 * Do AlterDisplay processing on Address Fields.
4586 * @param array $rows
4587 * @param int $rowNum
4588 * @param string $baseUrl
4589 * @param string $linkText
4593 public function alterDisplayContactFields(&$row, &$rows, &$rowNum, $baseUrl, $linkText) {
4594 $entryFound = FALSE;
4595 // There is no reason not to add links for all fields but it seems a bit odd to be able to click on
4596 // 'Mrs'. Also, we don't have metadata about the title. So, add selectively to addLinks.
4597 $addLinks = ['gender_id' => 'Gender'];
4598 foreach (['prefix_id', 'suffix_id', 'gender_id', 'contact_sub_type', 'preferred_language'] as $fieldName) {
4599 if (array_key_exists('civicrm_contact_' . $fieldName, $row)) {
4600 if (($value = $row['civicrm_contact_' . $fieldName]) != FALSE) {
4601 $rowValues = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $value);
4603 foreach ($rowValues as $rowValue) {
4605 $rowLabels[] = CRM_Core_PseudoConstant
::getLabel('CRM_Contact_BAO_Contact', $fieldName, $rowValue);
4608 $rows[$rowNum]['civicrm_contact_' . $fieldName] = implode(', ', $rowLabels);
4609 if ($baseUrl && ($title = CRM_Utils_Array
::value($fieldName, $addLinks)) != FALSE) {
4610 $this->addLinkToRow($rows[$rowNum], $baseUrl, $linkText, $value, $fieldName, 'civicrm_contact', $title);
4617 'do_not_email', 'is_deceased', 'do_not_phone', 'do_not_sms', 'do_not_mail', 'do_not_trade', 'is_opt_out',
4619 foreach ($yesNoFields as $fieldName) {
4620 if (array_key_exists('civicrm_contact_' . $fieldName, $row)) {
4621 // Since these are essentially 'negative fields' it feels like it
4622 // makes sense to only highlight the exceptions hence no 'No'.
4623 $rows[$rowNum]['civicrm_contact_' . $fieldName] = !empty($rows[$rowNum]['civicrm_contact_' . $fieldName]) ?
ts('Yes') : '';
4628 // Handle employer id
4629 if (array_key_exists('civicrm_contact_employer_id', $row)) {
4630 $employerId = $row['civicrm_contact_employer_id'];
4632 $rows[$rowNum]['civicrm_contact_employer_id'] = CRM_Contact_BAO_Contact
::displayName($employerId);
4633 $rows[$rowNum]['civicrm_contact_employer_id_link'] = CRM_Utils_System
::url('civicrm/contact/view', 'reset=1&cid=' . $employerId, $this->_absoluteUrl
);
4634 $rows[$rowNum]['civicrm_contact_employer_id_hover'] = ts('View Contact Summary for Employer.');
4643 * Adjusts dates passed in to YEAR() for fiscal year.
4645 * @param string $fieldName
4649 public function fiscalYearOffset($fieldName) {
4650 $config = CRM_Core_Config
::singleton();
4651 $fy = $config->fiscalYearStart
;
4652 if (CRM_Utils_Array
::value('yid_op', $this->_params
) == 'calendar' ||
4653 ($fy['d'] == 1 && $fy['M'] == 1)
4655 return "YEAR( $fieldName )";
4657 return "YEAR( $fieldName - INTERVAL " . ($fy['M'] - 1) . " MONTH" .
4658 ($fy['d'] > 1 ?
(" - INTERVAL " . ($fy['d'] - 1) . " DAY") : '') . " )";
4662 * Add Address into From Table if required.
4664 * Prefix will be added to both tables as
4665 * it is assumed you are using it to get address of a secondary contact.
4667 * @param string $prefix
4668 * @param array $extra Additional options.
4669 * Not currently used in core but may be used in override extensions.
4671 protected function joinAddressFromContact($prefix = '', $extra = []) {
4672 $defaults = ['primary_only' => TRUE];
4673 $params = array_merge($defaults, $extra);
4674 $addressTables = ['civicrm_address', 'civicrm_country', 'civicrm_worldregion', 'civicrm_state_province'];
4675 $isJoinRequired = $this->_addressField
;
4676 foreach ($addressTables as $addressTable) {
4677 if ($this->isTableSelected($prefix . $addressTable)) {
4678 $isJoinRequired = TRUE;
4681 if ($isJoinRequired) {
4683 LEFT JOIN civicrm_address {$this->_aliases[$prefix . 'civicrm_address']}
4684 ON ({$this->_aliases[$prefix . 'civicrm_contact']}.id =
4685 {$this->_aliases[$prefix . 'civicrm_address']}.contact_id)";
4686 if ($params['primary_only']) {
4688 {$this->_aliases[$prefix . 'civicrm_address']}.is_primary = 1\n";
4690 $this->_from
.= $fromJoin;
4695 * Add Country into From Table if required.
4697 * Prefix will be added to both tables as
4698 * it is assumed you are using it to get address of a secondary contact.
4700 * @param string $prefix
4701 * @param array $extra Additional options.
4702 * Not currently used in core but may be used in override extensions.
4704 protected function joinCountryFromAddress($prefix = '', $extra = []) {
4705 $defaults = ['primary_only' => TRUE];
4706 $params = array_merge($defaults, $extra);
4707 // include country field if country column is to be included
4708 if ($this->isTableSelected($prefix . 'civicrm_country') ||
$this->isTableSelected($prefix . 'civicrm_worldregion')) {
4709 if (empty($this->_aliases
[$prefix . 'civicrm_country'])) {
4710 $this->_aliases
[$prefix . 'civicrm_country'] = $prefix . '_report_country';
4713 LEFT JOIN civicrm_country {$this->_aliases[$prefix . 'civicrm_country']}
4714 ON {$this->_aliases[$prefix . 'civicrm_address']}.country_id = {$this->_aliases[$prefix . 'civicrm_country']}.id";
4715 if ($params['primary_only']) {
4717 {$this->_aliases[$prefix . 'civicrm_address']}.is_primary = 1 ";
4719 $this->_from
.= $fromJoin;
4724 * Add Phone into From Table if required.
4726 * Prefix will be added to both tables as
4727 * it is assumed you are using it to get address of a secondary contact.
4729 * @param string $prefix
4730 * @param array $extra Additional options.
4731 * Not currently used in core but may be used in override extensions.
4733 protected function joinPhoneFromContact($prefix = '', $extra = []) {
4734 $defaults = ['primary_only' => TRUE];
4735 $params = array_merge($defaults, $extra);
4736 // include phone field if phone column is to be included
4737 if ($this->isTableSelected($prefix . 'civicrm_phone')) {
4739 LEFT JOIN civicrm_phone {$this->_aliases[$prefix . 'civicrm_phone']}
4740 ON {$this->_aliases[$prefix . 'civicrm_contact']}.id = {$this->_aliases[$prefix . 'civicrm_phone']}.contact_id";
4741 if ($params['primary_only']) {
4743 {$this->_aliases[$prefix . 'civicrm_phone']}.is_primary = 1\n";
4745 $this->_from
.= $fromJoin;
4750 * Add Email into From Table if required.
4752 * Prefix will be added to both tables as
4753 * it is assumed you are using it to get address of a secondary contact.
4755 * @param string $prefix
4756 * @param array $extra Additional options.
4757 * Not currently used in core but may be used in override extensions.
4759 protected function joinEmailFromContact($prefix = '', $extra = []) {
4760 $defaults = ['primary_only' => TRUE];
4761 $params = array_merge($defaults, $extra);
4762 // include email field if email column is to be included
4763 if ($this->isTableSelected($prefix . 'civicrm_email')) {
4765 LEFT JOIN civicrm_email {$this->_aliases[$prefix . 'civicrm_email']}
4766 ON {$this->_aliases[$prefix . 'civicrm_contact']}.id = {$this->_aliases[$prefix . 'civicrm_email']}.contact_id";
4767 if ($params['primary_only']) {
4769 {$this->_aliases[$prefix . 'civicrm_email']}.is_primary = 1 ";
4771 $this->_from
.= $fromJoin;
4776 * Add Financial Transaction into From Table if required.
4778 public function addFinancialTrxnFromClause() {
4779 if ($this->isTableSelected('civicrm_financial_trxn')) {
4781 LEFT JOIN civicrm_entity_financial_trxn eftcc
4782 ON ({$this->_aliases['civicrm_contribution']}.id = eftcc.entity_id AND
4783 eftcc.entity_table = 'civicrm_contribution')
4784 LEFT JOIN civicrm_financial_trxn {$this->_aliases['civicrm_financial_trxn']}
4785 ON {$this->_aliases['civicrm_financial_trxn']}.id = eftcc.financial_trxn_id \n";
4790 * Get phone columns to add to array.
4792 * @param array $options
4793 * - prefix Prefix to add to table (in case of more than one instance of the table)
4794 * - prefix_label Label to give columns from this phone table instance
4797 * phone columns definition
4799 public function getPhoneColumns($options = []) {
4802 'prefix_label' => '',
4805 $options = array_merge($defaultOptions, $options);
4808 $options['prefix'] . 'civicrm_phone' => [
4809 'dao' => 'CRM_Core_DAO_Phone',
4811 $options['prefix'] . 'phone' => [
4812 'title' => $options['prefix_label'] . ts('Phone'),
4822 * Get a standard set of contact fields.
4823 * @deprecated - use getColumns('Contact') instead
4826 public function getBasicContactFields() {
4829 'title' => ts('Contact Name'),
4834 'no_display' => TRUE,
4838 'title' => ts('Contact Prefix'),
4841 'title' => ts('First Name'),
4844 'title' => ts('Nick Name'),
4847 'title' => ts('Middle Name'),
4850 'title' => ts('Last Name'),
4853 'title' => ts('Contact Suffix'),
4855 'postal_greeting_display' => ['title' => ts('Postal Greeting')],
4856 'email_greeting_display' => ['title' => ts('Email Greeting')],
4857 'addressee_display' => ['title' => ts('Addressee')],
4859 'title' => ts('Contact Type'),
4861 'contact_sub_type' => [
4862 'title' => ts('Contact Subtype'),
4865 'title' => ts('Gender'),
4868 'title' => ts('Birth Date'),
4871 'title' => ts('Age'),
4872 'dbAlias' => 'TIMESTAMPDIFF(YEAR, contact_civireport.birth_date, CURDATE())',
4875 'title' => ts('Contact Job title'),
4877 'organization_name' => [
4878 'title' => ts('Organization Name'),
4880 'external_identifier' => [
4881 'title' => ts('Contact identifier from external system'),
4883 'do_not_email' => [],
4884 'do_not_phone' => [],
4885 'do_not_mail' => [],
4887 'do_not_trade' => [],
4889 'is_deceased' => [],
4890 'preferred_language' => [],
4892 'title' => ts('Current Employer'),
4898 * Get a standard set of contact filters.
4900 * @param array $defaults
4904 public function getBasicContactFilters($defaults = []) {
4907 'title' => ts('Contact Name'),
4910 'title' => ts('Contact Source'),
4911 'type' => CRM_Utils_Type
::T_STRING
,
4914 'title' => ts('Contact ID'),
4915 'no_display' => TRUE,
4918 'title' => ts('Gender'),
4919 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
4920 'options' => CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id'),
4923 'title' => ts('Birth Date'),
4924 'operatorType' => CRM_Report_Form
::OP_DATE
,
4927 'title' => ts('Contact Type'),
4929 'contact_sub_type' => [
4930 'title' => ts('Contact Subtype'),
4932 'modified_date' => [
4933 'title' => ts('Contact Modified'),
4934 'operatorType' => CRM_Report_Form
::OP_DATE
,
4935 'type' => CRM_Utils_Type
::T_DATE
,
4938 'title' => ts('Deceased'),
4939 'type' => CRM_Utils_Type
::T_BOOLEAN
,
4940 'default' => CRM_Utils_Array
::value('deceased', $defaults, 0),
4943 'title' => ts('Do not email'),
4944 'type' => CRM_Utils_Type
::T_BOOLEAN
,
4947 'title' => ts('Do not phone'),
4948 'type' => CRM_Utils_Type
::T_BOOLEAN
,
4951 'title' => ts('Do not mail'),
4952 'type' => CRM_Utils_Type
::T_BOOLEAN
,
4955 'title' => ts('Do not SMS'),
4956 'type' => CRM_Utils_Type
::T_BOOLEAN
,
4959 'title' => ts('Do not Trade'),
4960 'type' => CRM_Utils_Type
::T_BOOLEAN
,
4963 'title' => ts('Do not bulk email'),
4964 'type' => CRM_Utils_Type
::T_BOOLEAN
,
4966 'preferred_language' => [
4967 'title' => ts('Preferred Language'),
4970 'no_display' => TRUE,
4972 'type' => CRM_Utils_Type
::T_BOOLEAN
,
4978 * Add contact to group.
4980 * @param int $groupID
4982 public function add2group($groupID) {
4983 if (is_numeric($groupID) && isset($this->_aliases
['civicrm_contact'])) {
4984 $select = "SELECT DISTINCT {$this->_aliases['civicrm_contact']}.id AS addtogroup_contact_id, ";
4985 $select = preg_replace('/SELECT(\s+SQL_CALC_FOUND_ROWS)?\s+/i', $select, $this->_select
);
4986 $sql = "{$select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy}";
4987 $sql = str_replace('WITH ROLLUP', '', $sql);
4988 if (!$this->optimisedForOnlyFullGroupBy
) {
4989 CRM_Core_DAO
::disableFullGroupByMode();
4991 $dao = CRM_Core_DAO
::executeQuery($sql);
4992 CRM_Core_DAO
::reenableFullGroupByMode();
4995 // Add resulting contacts to group
4996 while ($dao->fetch()) {
4997 if ($dao->addtogroup_contact_id
) {
4998 $contact_ids[$dao->addtogroup_contact_id
] = $dao->addtogroup_contact_id
;
5002 if (!empty($contact_ids)) {
5003 CRM_Contact_BAO_GroupContact
::addContactsToGroup($contact_ids, $groupID);
5004 CRM_Core_Session
::setStatus(ts("Listed contact(s) have been added to the selected group."), ts('Contacts Added'), 'success');
5007 CRM_Core_Session
::setStatus(ts("The listed records(s) cannot be added to the group."));
5013 * Apply common settings to entityRef fields.
5015 * @param array $field
5016 * @param string $table
5018 public function setEntityRefDefaults(&$field, $table) {
5019 $field['attributes'] = $field['attributes'] ?
$field['attributes'] : [];
5020 $field['attributes'] +
= [
5021 'entity' => CRM_Core_DAO_AllCoreTables
::getBriefName(CRM_Core_DAO_AllCoreTables
::getClassForTable($table)),
5023 'placeholder' => ts('- select -'),
5028 * Add link fields to the row.
5030 * Function adds the _link & _hover fields to the row.
5033 * @param string $baseUrl
5034 * @param string $linkText
5035 * @param string $value
5036 * @param string $fieldName
5037 * @param string $tablePrefix
5038 * @param string $fieldLabel
5042 protected function addLinkToRow(&$row, $baseUrl, $linkText, $value, $fieldName, $tablePrefix, $fieldLabel) {
5043 $criteriaQueryParams = CRM_Report_Utils_Report
::getPreviewCriteriaQueryParams($this->_defaults
, $this->_params
);
5044 $url = CRM_Report_Utils_Report
::getNextUrl($baseUrl,
5045 "reset=1&force=1&{$criteriaQueryParams}&" .
5046 $fieldName . "_op=in&{$fieldName}_value={$value}",
5047 $this->_absoluteUrl
, $this->_id
5049 $row["{$tablePrefix}_{$fieldName}_link"] = $url;
5050 $row["{$tablePrefix}_{$fieldName}_hover"] = ts("%1 for this %2.",
5051 [1 => $linkText, 2 => $fieldLabel]
5056 * Get label for show results buttons.
5060 public function getResultsLabel() {
5061 $showResultsLabel = $this->resultsDisplayed() ?
ts('Refresh results') : ts('View results');
5062 return $showResultsLabel;
5066 * Determine the output mode from the url or input.
5069 * - pdf : Render as pdf
5070 * - csv : Render as csv
5071 * - print : Render in print format
5072 * - save : save the report and display the new report
5073 * - copy : save the report as a new instance and display that.
5074 * - group : go to the add to group screen.
5076 * Potentially chart variations could also be included but the complexity
5077 * is that we might print a bar chart as a pdf.
5079 protected function setOutputMode() {
5080 $this->_outputMode
= str_replace('report_instance.', '', CRM_Utils_Request
::retrieve(
5083 CRM_Core_DAO
::$_nullObject,
5085 CRM_Utils_Array
::value('task', $this->_params
)
5087 // if contacts are added to group
5088 if (!empty($this->_params
['groups']) && empty($this->_outputMode
)) {
5089 $this->_outputMode
= 'group';
5091 if (isset($this->_params
['task'])) {
5092 unset($this->_params
['task']);
5097 * CRM-17793 - Alter DateTime section header to group by date from the datetime field.
5100 * @param $columnName
5102 public function alterSectionHeaderForDateTime($tempTable, $columnName) {
5103 // add new column with date value for the datetime field
5104 $tempQuery = "ALTER TABLE {$tempTable} ADD COLUMN {$columnName}_date VARCHAR(128)";
5105 CRM_Core_DAO
::executeQuery($tempQuery);
5106 $updateQuery = "UPDATE {$tempTable} SET {$columnName}_date = date({$columnName})";
5107 CRM_Core_DAO
::executeQuery($updateQuery);
5108 $this->_selectClauses
[] = "{$columnName}_date";
5109 $this->_select
.= ", {$columnName}_date";
5110 $this->_sections
["{$columnName}_date"] = $this->_sections
["{$columnName}"];
5111 unset($this->_sections
["{$columnName}"]);
5112 $this->assign('sections', $this->_sections
);
5116 * Get an array of the columns that have been selected for display.
5120 public function getSelectColumns() {
5121 $selectColumns = [];
5122 foreach ($this->_columns
as $tableName => $table) {
5123 if (array_key_exists('fields', $table)) {
5124 foreach ($table['fields'] as $fieldName => $field) {
5125 if (!empty($field['required']) ||
5126 !empty($this->_params
['fields'][$fieldName])
5129 $selectColumns["{$tableName}_{$fieldName}"] = 1;
5134 return $selectColumns;
5138 * Add location tables to the query if they are used for filtering.
5140 * This is for when we are running the query separately for filtering and retrieving display fields.
5142 public function selectivelyAddLocationTablesJoinsToFilterQuery() {
5143 if ($this->isTableFiltered('civicrm_email')) {
5145 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
5146 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id
5147 AND {$this->_aliases['civicrm_email']}.is_primary = 1";
5149 if ($this->isTableFiltered('civicrm_phone')) {
5151 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
5152 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id
5153 AND {$this->_aliases['civicrm_phone']}.is_primary = 1";
5155 if ($this->isTableFiltered('civicrm_address')) {
5157 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
5158 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id)
5159 AND {$this->_aliases['civicrm_address']}.is_primary = 1\n";
5164 * Set the base table for the FROM clause.
5166 * Sets up the from clause, allowing for the possibility it might be a
5167 * temp table pre-filtered by groups if a group filter is in use.
5169 * @param string $baseTable
5170 * @param string $field
5171 * @param null $tableAlias
5173 public function setFromBase($baseTable, $field = 'id', $tableAlias = NULL) {
5175 $tableAlias = $this->_aliases
[$baseTable];
5177 $this->_from
= $this->_from
= " FROM $baseTable $tableAlias ";
5178 $this->joinGroupTempTable($baseTable, $field, $tableAlias);
5179 $this->_from
.= " {$this->_aclFrom} ";
5183 * Join the temp table contacting contacts who are members of the filtered groups.
5185 * If we are using an IN filter we use an inner join, otherwise a left join.
5187 * @param string $baseTable
5188 * @param string $field
5189 * @param string $tableAlias
5191 public function joinGroupTempTable($baseTable, $field, $tableAlias) {
5192 if ($this->groupTempTable
) {
5193 if ($this->_params
['gid_op'] == 'in') {
5194 $this->_from
= " FROM $this->groupTempTable group_temp_table INNER JOIN $baseTable $tableAlias
5195 ON group_temp_table.id = $tableAlias.{$field} ";
5199 LEFT JOIN $this->groupTempTable group_temp_table
5200 ON $tableAlias.{$field} = group_temp_table.id ";
5206 * Get all labels for fields that are used in a group concat.
5208 * @param string $options
5209 * comma separated option values.
5210 * @param string $baoName
5211 * The BAO name for the field.
5212 * @param string $fieldName
5213 * The name of the field for which labels should be retrieved.
5217 public function getLabels($options, $baoName, $fieldName) {
5218 $types = explode(',', $options);
5220 foreach ($types as $value) {
5221 $labels[$value] = CRM_Core_PseudoConstant
::getLabel($baoName, $fieldName, $value);
5223 return implode(', ', array_filter($labels));
5227 * Add statistics columns.
5229 * If a group by is in play then add columns for the statistics fields.
5231 * This would lead to a new field in the $row such as $fieldName_sum and a new, matching
5232 * column header field.
5234 * @param array $field
5235 * @param string $tableName
5236 * @param string $fieldName
5237 * @param array $select
5241 protected function addStatisticsToSelect($field, $tableName, $fieldName, $select) {
5242 foreach ($field['statistics'] as $stat => $label) {
5243 $alias = "{$tableName}_{$fieldName}_{$stat}";
5244 switch (strtolower($stat)) {
5247 $select[] = "$stat({$field['dbAlias']}) as $alias";
5248 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
5249 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
5250 $this->_statFields
[$label] = $alias;
5251 $this->_selectAliases
[] = $alias;
5255 $select[] = "COUNT({$field['dbAlias']}) as $alias";
5256 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
5257 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type
::T_INT
;
5258 $this->_statFields
[$label] = $alias;
5259 $this->_selectAliases
[] = $alias;
5262 case 'count_distinct':
5263 $select[] = "COUNT(DISTINCT {$field['dbAlias']}) as $alias";
5264 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
5265 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type
::T_INT
;
5266 $this->_statFields
[$label] = $alias;
5267 $this->_selectAliases
[] = $alias;
5271 $select[] = "ROUND(AVG({$field['dbAlias']}),2) as $alias";
5272 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
5273 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
5274 $this->_statFields
[$label] = $alias;
5275 $this->_selectAliases
[] = $alias;
5283 * Add a basic field to the select clause.
5285 * @param string $tableName
5286 * @param string $fieldName
5287 * @param array $field
5288 * @param string $select
5291 protected function addBasicFieldToSelect($tableName, $fieldName, $field, $select) {
5292 $alias = "{$tableName}_{$fieldName}";
5293 $select[] = "{$field['dbAlias']} as $alias";
5294 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $field['title'] ??
NULL;
5295 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = $field['type'] ??
NULL;
5296 $this->_selectAliases
[] = $alias;
5303 * @param array $table
5304 * @param string $tableName
5309 protected function setTableAlias($table, $tableName) {
5310 if (!isset($table['alias'])) {
5311 $this->_columns
[$tableName]['alias'] = substr($tableName, 8) .
5315 $this->_columns
[$tableName]['alias'] = $table['alias'] . '_civireport';
5318 $this->_aliases
[$tableName] = $this->_columns
[$tableName]['alias'];
5319 return $this->_aliases
[$tableName];
5323 * Function to add columns to reports.
5325 * This is ported from extended reports, which also adds join filters to the options.
5327 * @param string $type
5328 * @param array $options
5329 * - prefix - A string to prepend to the table name
5330 * - prefix_label A string to prepend to the fields
5331 * - fields (bool) - should the fields for this table be made available
5332 * - group_by (bool) - should the group bys for this table be made available.
5333 * - order_by (bool) - should the group bys for this table be made available.
5334 * - filters (bool) - should the filters for this table by made available.
5335 * - fields_defaults (array) array of fields that should be displayed by default.
5336 * - filters_defaults (array) array of fields that should be filtered by default.
5337 * - join_filters (array) fields available for filtering joins (requires additional custom code).
5338 * - join_fields (array) fields available from join (requires additional custom code).
5339 * - group_by_defaults (array) array of group bys that should be applied by default.
5340 * - order_by_defaults (array) array of order bys that should be applied by default.
5341 * - custom_fields (array) array of entity types for custom fields (not usually required).
5342 * - contact_type (string) optional restriction on contact type for some tables.
5343 * - fields_excluded (array) fields that are in the generic set for the table but not in the report.
5347 protected function getColumns($type, $options = []) {
5350 'prefix_label' => '',
5352 'group_bys' => FALSE,
5353 'order_bys' => TRUE,
5355 'join_filters' => FALSE,
5356 'fields_defaults' => [],
5357 'filters_defaults' => [],
5358 'group_bys_defaults' => [],
5359 'order_bys_defaults' => [],
5361 $options = array_merge($defaultOptions, $options);
5363 $fn = 'get' . $type . 'Columns';
5364 return $this->$fn($options);
5368 * Get columns for contact table.
5370 * @param array $options
5374 protected function getContactColumns($options = []) {
5376 'custom_fields' => ['Individual', 'Contact', 'Organization'],
5377 'fields_defaults' => ['display_name', 'id'],
5378 'order_bys_defaults' => ['sort_name ASC'],
5379 'contact_type' => NULL,
5382 $options = array_merge($defaultOptions, $options);
5384 $tableAlias = $options['prefix'] . 'contact';
5387 $options['prefix'] . 'display_name' => [
5388 'name' => 'display_name',
5389 'title' => $options['prefix_label'] . ts('Contact Name'),
5390 'is_fields' => TRUE,
5392 $options['prefix'] . 'sort_name' => [
5393 'name' => 'sort_name',
5394 'title' => $options['prefix_label'] . ts('Contact Name (in sort format)'),
5395 'is_fields' => TRUE,
5396 'is_filters' => TRUE,
5397 'is_order_bys' => TRUE,
5399 $options['prefix'] . 'id' => [
5401 'title' => $options['prefix_label'] . ts('Contact ID'),
5402 'alter_display' => 'alterContactID',
5403 'type' => CRM_Utils_Type
::T_INT
,
5404 'is_order_bys' => TRUE,
5405 'is_group_bys' => TRUE,
5406 'is_fields' => TRUE,
5407 'is_filters' => TRUE,
5409 $options['prefix'] . 'external_identifier' => [
5410 'name' => 'external_identifier',
5411 'title' => $options['prefix_label'] . ts('External ID'),
5412 'type' => CRM_Utils_Type
::T_INT
,
5413 'is_fields' => TRUE,
5415 $options['prefix'] . 'contact_type' => [
5416 'title' => $options['prefix_label'] . ts('Contact Type'),
5417 'name' => 'contact_type',
5418 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
5419 'options' => CRM_Contact_BAO_Contact
::buildOptions('contact_type'),
5420 'is_fields' => TRUE,
5421 'is_filters' => TRUE,
5422 'is_group_bys' => TRUE,
5424 $options['prefix'] . 'contact_sub_type' => [
5425 'title' => $options['prefix_label'] . ts('Contact Sub Type'),
5426 'name' => 'contact_sub_type',
5427 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
5428 'options' => CRM_Contact_BAO_Contact
::buildOptions('contact_sub_type'),
5429 'is_fields' => TRUE,
5430 'is_filters' => TRUE,
5431 'is_group_bys' => TRUE,
5433 $options['prefix'] . 'is_deleted' => [
5434 'title' => $options['prefix_label'] . ts('Is deleted'),
5435 'name' => 'is_deleted',
5436 'type' => CRM_Utils_Type
::T_BOOLEAN
,
5437 'is_fields' => FALSE,
5438 'is_filters' => TRUE,
5439 'is_group_bys' => FALSE,
5441 $options['prefix'] . 'external_identifier' => [
5442 'title' => $options['prefix_label'] . ts('Contact identifier from external system'),
5443 'name' => 'external_identifier',
5444 'is_fields' => TRUE,
5445 'is_filters' => FALSE,
5446 'is_group_bys' => FALSE,
5447 'is_order_bys' => TRUE,
5449 $options['prefix'] . 'preferred_language' => [
5450 'title' => $options['prefix_label'] . ts('Preferred Language'),
5451 'name' => 'preferred_language',
5452 'is_fields' => TRUE,
5453 'is_filters' => TRUE,
5454 'is_group_bys' => TRUE,
5455 'is_order_bys' => TRUE,
5457 $options['prefix'] . 'preferred_communication_method' => [
5458 'title' => $options['prefix_label'] . ts('Preferred Communication Method'),
5459 'alter_display' => 'alterCommunicationtMethod',
5460 'name' => 'preferred_communication_method',
5461 'is_fields' => TRUE,
5462 'is_filters' => FALSE,
5463 'is_group_bys' => FALSE,
5464 'is_order_bys' => FALSE,
5468 'postal_greeting_display' => 'Postal Greeting',
5469 'email_greeting_display' => 'Email Greeting',
5470 'addressee_display' => 'Addressee',
5471 ] as $field => $title) {
5472 $spec[$options['prefix'] . $field] = [
5473 'title' => $options['prefix_label'] . ts($title),
5475 'is_fields' => TRUE,
5476 'is_filters' => FALSE,
5477 'is_group_bys' => FALSE,
5480 foreach (['do_not_email', 'do_not_phone', 'do_not_mail', 'do_not_sms', 'do_not_trade', 'is_opt_out'] as $field) {
5481 $spec[$options['prefix'] . $field] = [
5483 'type' => CRM_Utils_Type
::T_BOOLEAN
,
5484 'is_fields' => TRUE,
5485 'is_filters' => TRUE,
5486 'is_group_bys' => FALSE,
5489 $individualFields = [
5490 $options['prefix'] . 'first_name' => [
5491 'name' => 'first_name',
5492 'title' => $options['prefix_label'] . ts('First Name'),
5493 'is_fields' => TRUE,
5494 'is_filters' => TRUE,
5495 'is_order_bys' => TRUE,
5497 $options['prefix'] . 'middle_name' => [
5498 'name' => 'middle_name',
5499 'title' => $options['prefix_label'] . ts('Middle Name'),
5500 'is_fields' => TRUE,
5502 $options['prefix'] . 'last_name' => [
5503 'name' => 'last_name',
5504 'title' => $options['prefix_label'] . ts('Last Name'),
5505 'default_order' => 'ASC',
5506 'is_fields' => TRUE,
5508 $options['prefix'] . 'nick_name' => [
5509 'name' => 'nick_name',
5510 'title' => $options['prefix_label'] . ts('Nick Name'),
5511 'is_fields' => TRUE,
5513 $options['prefix'] . 'prefix_id' => [
5514 'name' => 'prefix_id',
5515 'title' => $options['prefix_label'] . ts('Prefix'),
5516 'options' => CRM_Contact_BAO_Contact
::buildOptions('prefix_id'),
5517 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
5518 'is_fields' => TRUE,
5519 'is_filters' => TRUE,
5521 $options['prefix'] . 'suffix_id' => [
5522 'name' => 'suffix_id',
5523 'title' => $options['prefix_label'] . ts('Suffix'),
5524 'options' => CRM_Contact_BAO_Contact
::buildOptions('suffix_id'),
5525 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
5526 'is_fields' => TRUE,
5527 'is_filters' => TRUE,
5529 $options['prefix'] . 'gender_id' => [
5530 'name' => 'gender_id',
5531 'title' => $options['prefix_label'] . ts('Gender'),
5532 'options' => CRM_Contact_BAO_Contact
::buildOptions('gender_id'),
5533 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
5534 'is_fields' => TRUE,
5535 'is_filters' => TRUE,
5538 'title' => $options['prefix_label'] . ts('Birth Date'),
5539 'operatorType' => CRM_Report_Form
::OP_DATE
,
5540 'type' => CRM_Utils_Type
::T_DATE
,
5541 'is_fields' => TRUE,
5542 'is_filters' => TRUE,
5545 'title' => $options['prefix_label'] . ts('Age'),
5546 'dbAlias' => 'TIMESTAMPDIFF(YEAR, ' . $tableAlias . '_civireport.birth_date, CURDATE())',
5547 'type' => CRM_Utils_Type
::T_INT
,
5548 'is_fields' => TRUE,
5550 $options['prefix'] . 'is_deceased' => [
5551 'title' => $options['prefix_label'] . ts('Is deceased'),
5552 'name' => 'is_deceased',
5553 'type' => CRM_Utils_Type
::T_BOOLEAN
,
5554 'is_fields' => FALSE,
5555 'is_filters' => TRUE,
5556 'is_group_bys' => FALSE,
5558 $options['prefix'] . 'job_title' => [
5559 'name' => 'job_title',
5560 'is_fields' => TRUE,
5561 'is_filters' => FALSE,
5562 'is_group_bys' => FALSE,
5564 $options['prefix'] . 'employer_id' => [
5565 'title' => $options['prefix_label'] . ts('Current Employer'),
5566 'type' => CRM_Utils_Type
::T_INT
,
5567 'name' => 'employer_id',
5568 'is_fields' => TRUE,
5569 'is_filters' => FALSE,
5570 'is_group_bys' => TRUE,
5573 if (!$options['contact_type'] ||
$options['contact_type'] === 'Individual') {
5574 $spec = array_merge($spec, $individualFields);
5577 if (!empty($options['custom_fields'])) {
5578 $this->_customGroupExtended
[$options['prefix'] . 'civicrm_contact'] = [
5579 'extends' => $options['custom_fields'],
5580 'title' => $options['prefix_label'],
5581 'filters' => $options['filters'],
5582 'prefix' => $options['prefix'],
5583 'prefix_label' => $options['prefix_label'],
5587 return $this->buildColumns($spec, $options['prefix'] . 'civicrm_contact', 'CRM_Contact_DAO_Contact', $tableAlias, $this->getDefaultsFromOptions($options), $options);
5591 * Get address columns to add to array.
5593 * @param array $options
5594 * - prefix Prefix to add to table (in case of more than one instance of the table)
5595 * - prefix_label Label to give columns from this address table instance
5596 * - group_bys enable these fields for group by - default false
5597 * - order_bys enable these fields for order by
5598 * - filters enable these fields for filtering
5600 * @return array address columns definition
5602 protected function getAddressColumns($options = []) {
5605 'prefix_label' => '',
5607 'group_bys' => FALSE,
5608 'order_bys' => TRUE,
5610 'join_filters' => FALSE,
5611 'fields_defaults' => [],
5612 'filters_defaults' => [],
5613 'group_bys_defaults' => [],
5614 'order_bys_defaults' => [],
5617 $options = array_merge($defaultOptions, $options);
5618 $defaults = $this->getDefaultsFromOptions($options);
5619 $tableAlias = $options['prefix'] . 'address';
5622 $options['prefix'] . 'name' => [
5623 'title' => $options['prefix_label'] . ts('Address Name'),
5625 'is_fields' => TRUE,
5627 $options['prefix'] . 'street_number' => [
5628 'name' => 'street_number',
5629 'title' => $options['prefix_label'] . ts('Street Number'),
5631 'is_fields' => TRUE,
5633 $options['prefix'] . 'odd_street_number' => [
5634 'title' => ts('Odd / Even Street Number'),
5635 'name' => 'odd_street_number',
5636 'type' => CRM_Utils_Type
::T_INT
,
5637 'no_display' => TRUE,
5639 'dbAlias' => "({$tableAlias}_civireport.street_number % 2)",
5640 'is_fields' => TRUE,
5641 'is_order_bys' => TRUE,
5643 $options['prefix'] . 'street_name' => [
5644 'name' => 'street_name',
5645 'title' => $options['prefix_label'] . ts('Street Name'),
5647 'is_fields' => TRUE,
5648 'is_filters' => TRUE,
5649 'operator' => 'like',
5650 'is_order_bys' => TRUE,
5652 $options['prefix'] . 'street_address' => [
5653 'title' => $options['prefix_label'] . ts('Street Address'),
5654 'name' => 'street_address',
5655 'is_fields' => TRUE,
5656 'is_filters' => TRUE,
5657 'is_group_bys' => TRUE,
5659 $options['prefix'] . 'supplemental_address_1' => [
5660 'title' => $options['prefix_label'] . ts('Supplementary Address Field 1'),
5661 'name' => 'supplemental_address_1',
5662 'is_fields' => TRUE,
5664 $options['prefix'] . 'supplemental_address_2' => [
5665 'title' => $options['prefix_label'] . ts('Supplementary Address Field 2'),
5666 'name' => 'supplemental_address_2',
5667 'is_fields' => TRUE,
5669 $options['prefix'] . 'supplemental_address_3' => [
5670 'title' => $options['prefix_label'] . ts('Supplementary Address Field 3'),
5671 'name' => 'supplemental_address_3',
5672 'is_fields' => TRUE,
5674 $options['prefix'] . 'street_number' => [
5675 'name' => 'street_number',
5676 'title' => $options['prefix_label'] . ts('Street Number'),
5678 'is_order_bys' => TRUE,
5679 'is_filters' => TRUE,
5680 'is_fields' => TRUE,
5682 $options['prefix'] . 'street_unit' => [
5683 'name' => 'street_unit',
5684 'title' => $options['prefix_label'] . ts('Street Unit'),
5686 'is_fields' => TRUE,
5688 $options['prefix'] . 'city' => [
5689 'title' => $options['prefix_label'] . ts('City'),
5691 'operator' => 'like',
5692 'is_fields' => TRUE,
5693 'is_filters' => TRUE,
5694 'is_group_bys' => TRUE,
5695 'is_order_bys' => TRUE,
5697 $options['prefix'] . 'postal_code' => [
5698 'title' => $options['prefix_label'] . ts('Postal Code'),
5699 'name' => 'postal_code',
5701 'is_fields' => TRUE,
5702 'is_filters' => TRUE,
5703 'is_group_bys' => TRUE,
5704 'is_order_bys' => TRUE,
5706 $options['prefix'] . 'postal_code_suffix' => [
5707 'title' => $options['prefix_label'] . ts('Postal Code Suffix'),
5708 'name' => 'postal_code_suffix',
5710 'is_fields' => TRUE,
5711 'is_filters' => TRUE,
5712 'is_group_bys' => TRUE,
5713 'is_order_bys' => TRUE,
5715 $options['prefix'] . 'county_id' => [
5716 'title' => $options['prefix_label'] . ts('County'),
5717 'alter_display' => 'alterCountyID',
5718 'name' => 'county_id',
5719 'type' => CRM_Utils_Type
::T_INT
,
5720 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
5721 'options' => CRM_Core_PseudoConstant
::county(),
5722 'is_fields' => TRUE,
5723 'is_filters' => TRUE,
5724 'is_group_bys' => TRUE,
5726 $options['prefix'] . 'state_province_id' => [
5727 'title' => $options['prefix_label'] . ts('State/Province'),
5728 'alter_display' => 'alterStateProvinceID',
5729 'name' => 'state_province_id',
5730 'type' => CRM_Utils_Type
::T_INT
,
5731 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
5732 'options' => CRM_Core_PseudoConstant
::stateProvince(),
5733 'is_fields' => TRUE,
5734 'is_filters' => TRUE,
5735 'is_group_bys' => TRUE,
5737 $options['prefix'] . 'country_id' => [
5738 'title' => $options['prefix_label'] . ts('Country'),
5739 'alter_display' => 'alterCountryID',
5740 'name' => 'country_id',
5741 'is_fields' => TRUE,
5742 'is_filters' => TRUE,
5743 'is_group_bys' => TRUE,
5744 'type' => CRM_Utils_Type
::T_INT
,
5745 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
5746 'options' => CRM_Core_PseudoConstant
::country(),
5748 $options['prefix'] . 'location_type_id' => [
5749 'name' => 'location_type_id',
5750 'title' => $options['prefix_label'] . ts('Location Type'),
5751 'type' => CRM_Utils_Type
::T_INT
,
5752 'is_fields' => TRUE,
5753 'alter_display' => 'alterLocationTypeID',
5755 $options['prefix'] . 'id' => [
5756 'title' => $options['prefix_label'] . ts('ID'),
5758 'is_fields' => TRUE,
5760 $options['prefix'] . 'is_primary' => [
5761 'name' => 'is_primary',
5762 'title' => $options['prefix_label'] . ts('Primary Address?'),
5763 'type' => CRM_Utils_Type
::T_BOOLEAN
,
5764 'is_fields' => TRUE,
5767 return $this->buildColumns($spec, $options['prefix'] . 'civicrm_address', 'CRM_Core_DAO_Address', $tableAlias, $defaults, $options);
5771 * Build the columns.
5773 * The normal report class needs you to remember to do a few things that are often erratic
5775 * 1) use a unique key for any field that might not be unique (e.g. start date, label)
5776 * - this class will prepend an alias to the key & set the 'name' if you don't set it yourself.
5777 * You can suppress the alias with 'no_field_disambiguation' if transitioning existing reports. This
5778 * means any saved filters / fields on saved report instances. This will mean that matching names from
5779 * different tables may be ambigious, but it will smooth any code transition.
5780 * - note that it assumes the value being passed in is the actual table field name
5782 * 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
5783 * contexts - the code looks up the fields array for data - so it both defines the field spec & the fields you want to show
5785 * 3) this function also sets the 'metadata' array - the extended report class now uses this in place
5786 * of the fields array to reduce the issues caused when metadata is needed but 'fields' are not defined. Code in
5787 * the core classes can start to move towards that.
5789 * @param array $specs
5790 * @param string $tableName
5791 * @param string $daoName
5792 * @param string $tableAlias
5793 * @param array $defaults
5794 * @param array $options
5798 protected function buildColumns($specs, $tableName, $daoName = NULL, $tableAlias = NULL, $defaults = [], $options = []) {
5800 $tableAlias = str_replace('civicrm_', '', $tableName);
5802 $types = ['filters', 'group_bys', 'order_bys', 'join_filters'];
5803 $columns = [$tableName => array_fill_keys($types, [])];
5804 // The code that uses this no longer cares if it is a DAO or BAO so just call it a DAO.
5805 $columns[$tableName]['dao'] = $daoName;
5806 $columns[$tableName]['alias'] = $tableAlias;
5808 foreach ($specs as $specName => $spec) {
5809 if (empty($spec['name'])) {
5810 $spec['name'] = $specName;
5813 $fieldAlias = (empty($options['no_field_disambiguation']) ?
$tableAlias . '_' : '') . $specName;
5814 $columns[$tableName]['metadata'][$fieldAlias] = $spec;
5815 $columns[$tableName]['fields'][$fieldAlias] = $spec;
5816 if (isset($defaults['fields_defaults']) && in_array($spec['name'], $defaults['fields_defaults'])) {
5817 $columns[$tableName]['fields'][$fieldAlias]['default'] = TRUE;
5820 if (!$spec['is_fields'] ||
(isset($options['fields_excluded']) && in_array($specName, $options['fields_excluded']))) {
5821 $columns[$tableName]['fields'][$fieldAlias]['no_display'] = TRUE;
5824 if (isset($options['fields_required']) && in_array($specName, $options['fields_required'])) {
5825 $columns[$tableName]['fields'][$fieldAlias]['required'] = TRUE;
5828 foreach ($types as $type) {
5829 if ($options[$type] && !empty($spec['is_' . $type])) {
5830 $columns[$tableName][$type][$fieldAlias] = $spec;
5831 if (isset($defaults[$type . '_defaults']) && isset($defaults[$type . '_defaults'][$spec['name']])) {
5832 $columns[$tableName][$type][$fieldAlias]['default'] = $defaults[$type . '_defaults'][$spec['name']];
5841 * Store group bys into array - so we can check elsewhere what is grouped.
5843 protected function storeGroupByArray() {
5845 if (empty($this->_params
['group_bys'])
5846 ||
!is_array($this->_params
['group_bys'])) {
5847 $this->_params
['group_bys'] = [];
5850 foreach ($this->_columns
as $tableName => $table) {
5851 $table = $this->_columns
[$tableName];
5852 if (array_key_exists('group_bys', $table)) {
5853 foreach ($table['group_bys'] as $fieldName => $fieldData) {
5854 $field = $this->_columns
[$tableName]['metadata'][$fieldName];
5855 if (!empty($this->_params
['group_bys'][$fieldName]) ||
!empty($fieldData['required'])) {
5856 if (!empty($field['chart'])) {
5857 $this->assign('chartSupported', TRUE);
5860 if (!empty($table['group_bys'][$fieldName]['frequency']) &&
5861 !empty($this->_params
['group_bys_freq'][$fieldName])
5864 switch ($this->_params
['group_bys_freq'][$fieldName]) {
5866 $this->_groupByArray
[$tableName . '_' . $fieldName . '_start'] = self
::fiscalYearOffset($field['dbAlias']);
5870 $this->_groupByArray
[$tableName . '_' . $fieldName . '_start'] = " YEAR({$field['dbAlias']})";
5874 $this->_groupByArray
[$tableName . '_' . $fieldName . '_start'] = "YEAR({$field['dbAlias']}), QUARTER({$field['dbAlias']})";
5878 $this->_groupByArray
[$tableName . '_' . $fieldName . '_start'] = "YEARWEEK({$field['dbAlias']})";
5882 $this->_groupByArray
[$tableName . '_' . $fieldName . '_start'] = "EXTRACT(YEAR_MONTH FROM {$field['dbAlias']})";
5886 $this->_groupByArray
[$tableName . '_' . $fieldName . '_start'] = "DATE({$field['dbAlias']})";
5891 if (!in_array($field['dbAlias'], $this->_groupByArray
)) {
5892 $this->_groupByArray
[$tableName . '_' . $fieldName] = $field['dbAlias'];
5907 protected function getDefaultsFromOptions($options) {
5909 'fields_defaults' => $options['fields_defaults'],
5910 'filters_defaults' => $options['filters_defaults'],
5911 'group_bys_defaults' => $options['group_bys_defaults'],
5912 'order_bys_defaults' => $options['order_bys_defaults'],
5918 * Get the select clause for a field, wrapping in GROUP_CONCAT if appropriate.
5920 * Full group by mode dictates that a field must either be in the group by function or
5921 * wrapped in a aggregate function. Here we wrap the field in GROUP_CONCAT if it is not in the
5924 * @param string $tableName
5925 * @param string $fieldName
5926 * @param string $field
5929 protected function getSelectClauseWithGroupConcatIfNotGroupedBy($tableName, &$fieldName, &$field) {
5930 if ($this->groupConcatTested
&& (!empty($this->_groupByArray
) ||
$this->isForceGroupBy
)) {
5931 if ((empty($field['statistics']) ||
in_array('GROUP_CONCAT', $field['statistics']))) {
5932 $label = $field['title'] ??
NULL;
5933 $alias = $field['tplField'] ??
"{$tableName}_{$fieldName}";
5934 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $label;
5935 $this->_selectAliases
[] = $alias;
5936 if (empty($this->_groupByArray
[$tableName . '_' . $fieldName])) {
5937 return "GROUP_CONCAT(DISTINCT {$field['dbAlias']}) as $alias";
5939 return "({$field['dbAlias']}) as $alias";
5945 * Generate clause for the selected filter.
5947 * @param array $field
5948 * Field specification
5949 * @param string $fieldName
5953 * Relevant where clause.
5955 protected function generateFilterClause($field, $fieldName) {
5956 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
5957 if (CRM_Utils_Array
::value('operatorType', $field) ==
5958 CRM_Report_Form
::OP_MONTH
5960 $op = $this->_params
["{$fieldName}_op"] ??
NULL;
5961 $value = $this->_params
["{$fieldName}_value"] ??
NULL;
5962 if (is_array($value) && !empty($value)) {
5963 return "(month({$field['dbAlias']}) $op (" . implode(', ', $value) .
5968 $relative = $this->_params
["{$fieldName}_relative"] ??
NULL;
5969 $from = $this->_params
["{$fieldName}_from"] ??
NULL;
5970 $to = $this->_params
["{$fieldName}_to"] ??
NULL;
5971 $fromTime = $this->_params
["{$fieldName}_from_time"] ??
NULL;
5972 $toTime = $this->_params
["{$fieldName}_to_time"] ??
NULL;
5973 return $this->dateClause($field['dbAlias'], $relative, $from, $to, $field['type'], $fromTime, $toTime);
5977 $op = $this->_params
["{$fieldName}_op"] ??
NULL;
5979 return $this->whereClause($field,
5981 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
5982 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
5983 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
5991 * Retrieve a suitable object from the factory depending on the report
5992 * parameters, which typically might just be dependent on outputMode.
5994 * If there is no suitable output handler, e.g. if outputMode is "copy",
5995 * then this sets it to NULL.
5997 public function setOutputHandler() {
5998 $this->outputHandler
= \Civi\Report\OutputHandlerFactory
::singleton()->create($this);
6002 * Send report by email
6004 public function sendEmail() {
6005 if (empty($this->outputHandler
)) {
6006 // It's possible to end up here with outputMode unset, so we use
6007 // the "print" handler which was the default before, i.e. include
6008 // it as html in the body.
6009 $oldOutputMode = $this->_outputMode ??
NULL;
6010 $this->_outputMode
= 'print';
6011 $this->setOutputHandler();
6012 $this->_outputMode
= $oldOutputMode;
6015 $mailBody = $this->outputHandler
->getMailBody();
6018 $attachmentFileName = $this->outputHandler
->getFileName();
6019 // It's not always in the form of an attachment, e.g. for 'print' the
6020 // output ends up in $mailBody above.
6021 if ($attachmentFileName) {
6022 $fullFilename = CRM_Core_Config
::singleton()->templateCompileDir
. CRM_Utils_File
::makeFileName($attachmentFileName);
6023 file_put_contents($fullFilename, $this->outputHandler
->getOutputString());
6025 'fullPath' => $fullFilename,
6026 'mime_type' => $this->outputHandler
->getMimeType(),
6027 'cleanName' => $attachmentFileName,
6028 'charset' => $this->outputHandler
->getCharset(),
6033 // @todo outputMode doesn't seem to get used by mailReport, which is good
6034 // since it shouldn't have any outputMode-related `if` statements in it.
6035 // Someday could remove the param from the function call.
6036 if (CRM_Report_Utils_Report
::mailReport($mailBody, $this->_id
, $this->_outputMode
, $attachments)) {
6037 CRM_Core_Session
::setStatus(ts("Report mail has been sent."), ts('Sent'), 'success');
6040 CRM_Core_Session
::setStatus(ts("Report mail could not be sent."), ts('Mail Error'), 'error');