3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * Class CRM_Report_Form
31 class CRM_Report_Form
extends CRM_Core_Form
{
32 const ROW_COUNT_LIMIT
= 50;
35 * Operator types - used for displaying filter elements
45 OP_MULTISELECT_SEPARATOR
= 66,
50 * The id of the report instance
57 * The id of the report template
61 protected $_templateID;
69 protected $_noFields = FALSE;
72 * The set of all columns in the report. An associative array
73 * with column name as the key and attributes as the value
77 protected $_columns = [];
80 * The set of filters in the report
84 protected $_filters = [];
87 * The set of optional columns in the report
91 public $_options = [];
94 * By default most reports hide contact id.
95 * Setting this to true makes it available
98 protected $_exposeContactID = TRUE;
101 * Set of statistic fields
105 protected $_statFields = [];
108 * Set of statistics data
112 protected $_statistics = [];
115 * List of fields not to be repeated during display
119 protected $_noRepeats = [];
122 * List of fields not to be displayed
126 protected $_noDisplay = [];
129 * Object type that a custom group extends
133 protected $_customGroupExtends = NULL;
134 protected $_customGroupExtendsJoin = [];
135 protected $_customGroupFilters = TRUE;
136 protected $_customGroupGroupBy = FALSE;
137 protected $_customGroupJoin = 'LEFT JOIN';
143 protected $_tagFilter = FALSE;
146 * specify entity table for tags filter
149 protected $_tagFilterTable = 'civicrm_contact';
152 * Build groups filter.
156 protected $_groupFilter = FALSE;
159 * Has the report been optimised for group filtering.
161 * The functionality for group filtering has been improved but not
162 * all reports have been adjusted to take care of it.
164 * This property exists to highlight the reports which are still using the
165 * slow method & allow group filtering to still work for them until they
168 * In order to protect extensions we have to default to TRUE - but I have
169 * separately marked every class with a groupFilter in the hope that will trigger
170 * people to fix them as they touch them.
176 protected $groupFilterNotOptimised = TRUE;
183 public $_navigation = [];
185 public $_drilldownReport = [];
188 * Array of tabs to display on report.
190 * E.g we define the tab title, the tpl and the tab-specific part of the css or html link.
192 * $this->tabs['OrderBy'] = array(
193 * 'title' => ts('Sorting'),
194 * 'tpl' => 'OrderBy',
195 * 'div_label' => 'order-by',
200 protected $tabs = [];
203 * Should we add paging.
207 protected $addPaging = TRUE;
209 protected $isForceGroupBy = FALSE;
211 protected $groupConcatTested = FALSE;
214 * An attribute for checkbox/radio form field layout
218 protected $_fourColumnAttribute = [
219 '</td><td width="25%">',
220 '</td><td width="25%">',
221 '</td><td width="25%">',
225 protected $_force = 1;
227 protected $_params = NULL;
228 protected $_formValues = NULL;
229 protected $_instanceValues = NULL;
231 protected $_instanceForm = FALSE;
232 protected $_criteriaForm = FALSE;
234 protected $_instanceButtonName = NULL;
235 protected $_createNewButtonName = NULL;
236 protected $_printButtonName = NULL;
237 protected $_pdfButtonName = NULL;
238 protected $_csvButtonName = NULL;
239 protected $_groupButtonName = NULL;
240 protected $_chartButtonName = NULL;
241 protected $_csvSupported = TRUE;
242 protected $_add2groupSupported = TRUE;
243 protected $_groups = NULL;
244 protected $_grandFlag = FALSE;
245 protected $_rowsFound = NULL;
246 protected $_selectAliases = [];
247 protected $_rollup = NULL;
250 * Table containing list of contact IDs within the group filter.
254 protected $groupTempTable = '';
259 protected $_aliases = [];
275 protected $_limit = NULL;
278 * This can be set to specify a limit to the number of rows
279 * Since it is currently envisaged as part of the api usage it is only being applied
280 * when $_output mode is not 'html' or 'group' so as not to have to interpret / mess with that part
281 * of the code (see limit() fn.
285 protected $_limitValue = NULL;
288 * This can be set to specify row offset
289 * See notes on _limitValue
292 protected $_offsetValue = NULL;
296 protected $_sections = NULL;
297 protected $_autoIncludeIndexedFieldsAsOrderBys = 0;
298 protected $_absoluteUrl = FALSE;
301 * Flag to indicate if result-set is to be stored in a class variable which could be retrieved using getResultSet() method.
305 protected $_storeResultSet = FALSE;
308 * When _storeResultSet Flag is set use this var to store result set in form of array
312 protected $_resultSet = [];
315 * To what frequency group-by a date column
319 protected $_groupByDateFreq = [
321 'YEARWEEK' => 'Week',
322 'QUARTER' => 'Quarter',
327 * Variables to hold the acl inner join and where clause
330 protected $_aclFrom = NULL;
331 protected $_aclWhere = NULL;
334 * Array of DAO tables having columns included in SELECT or ORDER BY clause.
336 * Where has also been added to this although perhaps the 'includes both' array should have a different name.
340 protected $_selectedTables = [];
343 * Array of DAO tables having columns included in WHERE or HAVING clause
347 protected $filteredTables;
350 * Output mode e.g 'print', 'csv', 'pdf'.
354 protected $_outputMode;
357 * Format of any chart in use.
359 * (it's unclear if this could be merged with outputMode at this stage)
365 public $_having = NULL;
366 public $_select = NULL;
367 public $_selectClauses = [];
368 public $_columnHeaders = [];
369 public $_orderBy = NULL;
370 public $_orderByFields = [];
371 public $_orderByArray = [];
373 * Array of clauses to group by.
377 protected $_groupByArray = [];
378 public $_groupBy = NULL;
379 public $_whereClauses = [];
380 public $_havingClauses = [];
383 * DashBoardRowCount Dashboard row count
386 public $_dashBoardRowCount;
389 * Is this being called without a form controller (ie. the report is being render outside the normal form
390 * - e.g the api is retrieving the rows
393 public $noController = FALSE;
396 * Variable to hold the currency alias
399 protected $_currencyColumn = NULL;
404 protected $_interval;
409 protected $_sendmail;
422 * @var string Report description.
424 public $_description;
427 * @var bool Is an address field selected.
428 * This was intended to determine if the address table should be joined in
429 * The isTableSelected function is now preferred for this purpose
431 protected $_addressField;
434 * @var bool Is an email field selected.
435 * This was intended to determine if the email table should be joined in
436 * The isTableSelected function is now preferred for this purpose
438 protected $_emailField;
441 * @var bool Is a phone field selected.
442 * This was intended to determine if the phone table should be joined in
443 * The isTableSelected function is now preferred for this purpose
445 protected $_phoneField;
448 * @var bool Create new report instance? (or update existing) on save.
450 protected $_createNew;
453 * When a grand total row has calculated the status we pop it off to here.
455 * This allows us to access it from the stats function and avoid recalculating.
458 protected $rollupRow = [];
461 * @var string Database attributes - character set and collation
463 protected $_databaseAttributes = ' DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci';
466 * SQL being run in this report.
468 * The sql in the report is stored in this variable in order to be displayed on the developer tab.
476 * An instruction not to add a Group By.
478 * This is relevant where the group by might be otherwise added after the code that determines the group by array.
480 * e.g. where stat fields are being added but other settings cause it to not be desirable to add a group by
481 * such as in pivot charts when no row header is set
485 protected $noGroupBy = FALSE;
488 * SQL being run in this report as an array.
490 * The sql in the report is stored in this variable in order to be returned to api & test calls.
498 * Tables created for the report that need removal afterwards.
500 * ['civicrm_temp_report_x' => ['temporary' => TRUE, 'name' => 'civicrm_temp_report_x']
503 protected $temporaryTables = [];
506 * Can this report use the sql mode ONLY_FULL_GROUP_BY.
509 public $optimisedForOnlyFullGroupBy = TRUE;
514 public function __construct() {
515 parent
::__construct();
517 $this->addClass('crm-report-form');
519 if ($this->_tagFilter
) {
520 $this->buildTagFilter();
522 if ($this->_exposeContactID
) {
523 if (array_key_exists('civicrm_contact', $this->_columns
)) {
524 $this->_columns
['civicrm_contact']['fields']['exposed_id'] = [
526 'title' => ts('Contact ID'),
532 if ($this->_groupFilter
) {
533 $this->buildGroupFilter();
536 // Get all custom groups
537 $allGroups = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_CustomField', 'custom_group_id');
539 // Get the custom groupIds for which the user has VIEW permission
540 // If the user has 'access all custom data' permission, we'll leave $permCustomGroupIds empty
541 // and addCustomDataToColumns() will allow access to all custom groups.
542 $permCustomGroupIds = [];
543 if (!CRM_Core_Permission
::check('access all custom data')) {
544 $permCustomGroupIds = CRM_ACL_API
::group(CRM_Core_Permission
::VIEW
, NULL, 'civicrm_custom_group', $allGroups, NULL);
545 // do not allow custom data for reports if user doesn't have
546 // permission to access custom data.
547 if (!empty($this->_customGroupExtends
) && empty($permCustomGroupIds)) {
548 $this->_customGroupExtends
= [];
552 // merge custom data columns to _columns list, if any
553 $this->addCustomDataToColumns(TRUE, $permCustomGroupIds);
555 // add / modify display columns, filters ..etc
556 CRM_Utils_Hook
::alterReportVar('columns', $this->_columns
, $this);
558 //assign currencyColumn variable to tpl
559 $this->assign('currencyColumn', $this->_currencyColumn
);
563 * Shared pre-process function.
565 * If overriding preProcess function this should still be called.
569 public function preProcessCommon() {
570 $this->_force
= CRM_Utils_Request
::retrieve('force', 'Boolean');
572 $this->_dashBoardRowCount
= CRM_Utils_Request
::retrieve('rowCount', 'Integer');
574 $this->_section
= CRM_Utils_Request
::retrieve('section', 'Integer');
576 $this->assign('section', $this->_section
);
577 CRM_Core_Region
::instance('page-header')->add([
578 'markup' => sprintf('<!-- Report class: [%s] -->', htmlentities(get_class($this))),
580 if (!$this->noController
) {
581 $this->setID($this->get('instanceId'));
584 $this->setID(CRM_Report_Utils_Report
::getInstanceID());
586 $this->setID(CRM_Report_Utils_Report
::getInstanceIDForPath());
590 // set qfkey so that pager picks it up and use it in the "Next > Last >>" links.
591 // FIXME: Note setting it in $_GET doesn't work, since pager generates link based on QUERY_STRING
592 $_SERVER['QUERY_STRING'] .= "&qfKey={$this->controller->_key}";
596 $this->assign('instanceId', $this->_id
);
597 $params = ['id' => $this->_id
];
598 $this->_instanceValues
= [];
599 CRM_Core_DAO
::commonRetrieve('CRM_Report_DAO_ReportInstance',
601 $this->_instanceValues
603 if (empty($this->_instanceValues
)) {
604 CRM_Core_Error
::fatal("Report could not be loaded.");
606 $this->_title
= $this->_instanceValues
['title'];
607 if (!empty($this->_instanceValues
['permission']) &&
608 (!(CRM_Core_Permission
::check($this->_instanceValues
['permission']) ||
609 CRM_Core_Permission
::check('administer Reports')
612 CRM_Utils_System
::permissionDenied();
613 CRM_Utils_System
::civiExit();
616 $formValues = CRM_Utils_Array
::value('form_values', $this->_instanceValues
);
618 $this->_formValues
= unserialize($formValues);
621 $this->_formValues
= NULL;
624 $this->setOutputMode();
626 if ($this->_outputMode
== 'copy') {
627 $this->_createNew
= TRUE;
628 $this->_params
= $this->_formValues
;
629 $this->_params
['view_mode'] = 'criteria';
630 $this->_params
['title'] = $this->getTitle() . ts(' (copy created by %1 on %2)', [
631 CRM_Core_Session
::singleton()->getLoggedInContactDisplayName(),
632 CRM_Utils_Date
::customFormat(date('Y-m-d H:i')),
634 // Do not pass go. Do not collect another chance to re-run the same query.
635 CRM_Report_Form_Instance
::postProcess($this);
638 // lets always do a force if reset is found in the url.
639 // Hey why not? see CRM-17225 for more about this. The use of reset to be force is historical for reasons stated
640 // in the comment line above these 2.
641 if (!empty($_REQUEST['reset'])
642 && !in_array(CRM_Utils_Request
::retrieve('output', 'String'), ['save', 'criteria'])) {
647 $this->assign('mode', 'instance');
649 elseif (!$this->noController
) {
650 list($optionValueID, $optionValue) = CRM_Report_Utils_Report
::getValueIDFromUrl();
651 $instanceCount = CRM_Report_Utils_Report
::getInstanceCount($optionValue);
652 if (($instanceCount > 0) && $optionValueID) {
653 $this->assign('instanceUrl',
654 CRM_Utils_System
::url('civicrm/report/list',
655 "reset=1&ovid=$optionValueID"
659 if ($optionValueID) {
660 $this->_description
= CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', $optionValueID, 'description');
664 $this->assign('mode', 'template');
667 // lets display the Report Settings section
668 $this->_instanceForm
= $this->_force ||
$this->_id ||
(!empty($_POST));
670 // Do not display Report Settings section if administer Reports permission is absent OR
671 // if report instance is reserved and administer reserved reports absent
672 if (!CRM_Core_Permission
::check('administer Reports') ||
673 ($this->_instanceValues
['is_reserved'] &&
674 !CRM_Core_Permission
::check('administer reserved reports'))
676 $this->_instanceForm
= FALSE;
679 $this->assign('criteriaForm', FALSE);
680 // Display Report Criteria section if user has access Report Criteria OR administer Reports AND report instance is not reserved
681 if (CRM_Core_Permission
::check('administer Reports') ||
682 CRM_Core_Permission
::check('access Report Criteria')
684 if (!$this->_instanceValues
['is_reserved'] ||
685 CRM_Core_Permission
::check('administer reserved reports')
687 $this->assign('criteriaForm', TRUE);
688 $this->_criteriaForm
= TRUE;
692 // Special permissions check for private instance if it's not the current contact instance
694 (CRM_Report_BAO_ReportInstance
::reportIsPrivate($this->_id
) &&
695 !CRM_Report_BAO_ReportInstance
::contactIsOwner($this->_id
))) {
696 if (!CRM_Core_Permission
::check('access all private reports')) {
697 $this->_instanceForm
= FALSE;
698 $this->assign('criteriaForm', FALSE);
702 $this->_instanceButtonName
= $this->getButtonName('submit', 'save');
703 $this->_createNewButtonName
= $this->getButtonName('submit', 'next');
704 $this->_groupButtonName
= $this->getButtonName('submit', 'group');
705 $this->_chartButtonName
= $this->getButtonName('submit', 'chart');
711 public function addBreadCrumb() {
715 'title' => ts('Report Templates'),
716 'url' => CRM_Utils_System
::url('civicrm/admin/report/template/list', 'reset=1'),
720 CRM_Utils_System
::appendBreadCrumb($breadCrumbs);
724 * Pre process function.
726 * Called prior to build form.
728 public function preProcess() {
729 $this->preProcessCommon();
732 $this->addBreadCrumb();
735 foreach ($this->_columns
as $tableName => $table) {
736 $this->setTableAlias($table, $tableName);
739 // higher preference to bao object
740 $daoOrBaoName = CRM_Utils_Array
::value('bao', $table, CRM_Utils_Array
::value('dao', $table));
743 if (method_exists($daoOrBaoName, 'exportableFields')) {
744 $expFields = $daoOrBaoName::exportableFields();
747 $expFields = $daoOrBaoName::export();
751 $doNotCopy = ['required', 'default'];
753 $fieldGroups = ['fields', 'filters', 'group_bys', 'order_bys'];
754 foreach ($fieldGroups as $fieldGrp) {
755 if (!empty($table[$fieldGrp]) && is_array($table[$fieldGrp])) {
756 foreach ($table[$fieldGrp] as $fieldName => $field) {
757 // $name is the field name used to reference the BAO/DAO export fields array
758 $name = isset($field['name']) ?
$field['name'] : $fieldName;
760 // Sometimes the field name key in the BAO/DAO export fields array is
761 // different from the actual database field name.
762 // Unset $field['name'] so that actual database field name can be obtained
763 // from the BAO/DAO export fields array.
764 unset($field['name']);
766 if (array_key_exists($name, $expFields)) {
767 foreach ($doNotCopy as $dnc) {
768 // unset the values we don't want to be copied.
769 unset($expFields[$name][$dnc]);
772 $this->_columns
[$tableName][$fieldGrp][$fieldName] = $expFields[$name];
775 foreach ($expFields[$name] as $property => $val) {
776 if (!array_key_exists($property, $field)) {
777 $this->_columns
[$tableName][$fieldGrp][$fieldName][$property] = $val;
784 if (!empty($field['no_repeat'])) {
785 $this->_noRepeats
[] = "{$tableName}_{$fieldName}";
787 if (!empty($field['no_display'])) {
788 $this->_noDisplay
[] = "{$tableName}_{$fieldName}";
791 // set alias = table-name, unless already set
792 $alias = isset($field['alias']) ?
$field['alias'] : (
793 isset($this->_columns
[$tableName]['alias']) ?
$this->_columns
[$tableName]['alias'] : $tableName
795 $this->_columns
[$tableName][$fieldGrp][$fieldName]['alias'] = $alias;
797 // set name = fieldName, unless already set
798 if (!isset($this->_columns
[$tableName][$fieldGrp][$fieldName]['name'])) {
799 $this->_columns
[$tableName][$fieldGrp][$fieldName]['name'] = $name;
802 if (!isset($this->_columns
[$tableName][$fieldGrp][$fieldName]['table_name'])) {
803 $this->_columns
[$tableName][$fieldGrp][$fieldName]['table_name'] = $tableName;
806 // set dbAlias = alias.name, unless already set
807 if (!isset($this->_columns
[$tableName][$fieldGrp][$fieldName]['dbAlias'])) {
808 $this->_columns
[$tableName][$fieldGrp][$fieldName]['dbAlias']
810 $this->_columns
[$tableName][$fieldGrp][$fieldName]['name'];
813 // a few auto fills for filters
814 if ($fieldGrp == 'filters') {
815 // fill operator types
816 if (!array_key_exists('operatorType', $this->_columns
[$tableName][$fieldGrp][$fieldName])) {
817 switch (CRM_Utils_Array
::value('type', $this->_columns
[$tableName][$fieldGrp][$fieldName])) {
818 case CRM_Utils_Type
::T_MONEY
:
819 case CRM_Utils_Type
::T_FLOAT
:
820 $this->_columns
[$tableName][$fieldGrp][$fieldName]['operatorType'] = CRM_Report_Form
::OP_FLOAT
;
823 case CRM_Utils_Type
::T_INT
:
824 $this->_columns
[$tableName][$fieldGrp][$fieldName]['operatorType'] = CRM_Report_Form
::OP_INT
;
827 case CRM_Utils_Type
::T_DATE
:
828 $this->_columns
[$tableName][$fieldGrp][$fieldName]['operatorType'] = CRM_Report_Form
::OP_DATE
;
831 case CRM_Utils_Type
::T_BOOLEAN
:
832 $this->_columns
[$tableName][$fieldGrp][$fieldName]['operatorType'] = CRM_Report_Form
::OP_SELECT
;
833 if (!array_key_exists('options', $this->_columns
[$tableName][$fieldGrp][$fieldName])) {
834 $this->_columns
[$tableName][$fieldGrp][$fieldName]['options']
845 array_key_exists('pseudoconstant', $this->_columns
[$tableName][$fieldGrp][$fieldName])
847 // with multiple options operator-type is generally multi-select
848 $this->_columns
[$tableName][$fieldGrp][$fieldName]['operatorType'] = CRM_Report_Form
::OP_MULTISELECT
;
849 if (!array_key_exists('options', $this->_columns
[$tableName][$fieldGrp][$fieldName])) {
851 $this->_columns
[$tableName][$fieldGrp][$fieldName]['options'] = CRM_Core_PseudoConstant
::get($daoOrBaoName, $fieldName);
858 if (!isset($this->_columns
[$tableName]['metadata'][$fieldName])) {
859 $this->_columns
[$tableName]['metadata'][$fieldName] = $this->_columns
[$tableName][$fieldGrp][$fieldName];
862 $this->_columns
[$tableName]['metadata'][$fieldName] = array_merge($this->_columns
[$tableName][$fieldGrp][$fieldName], $this->_columns
[$tableName]['metadata'][$fieldName]);
868 // copy filters to a separate handy variable
869 if (array_key_exists('filters', $table)) {
870 $this->_filters
[$tableName] = $this->_columns
[$tableName]['filters'];
873 if (array_key_exists('group_bys', $table)) {
874 $groupBys[$tableName] = $this->_columns
[$tableName]['group_bys'];
877 if (array_key_exists('fields', $table)) {
878 $reportFields[$tableName] = $this->_columns
[$tableName]['fields'];
883 $this->setDefaultValues(FALSE);
886 CRM_Report_Utils_Get
::processFilter($this->_filters
, $this->_defaults
);
887 CRM_Report_Utils_Get
::processGroupBy($groupBys, $this->_defaults
);
888 CRM_Report_Utils_Get
::processFields($reportFields, $this->_defaults
);
889 CRM_Report_Utils_Get
::processChart($this->_defaults
);
892 $this->_formValues
= $this->_defaults
;
893 $this->postProcess();
898 * Set default values.
900 * @param bool $freeze
904 public function setDefaultValues($freeze = TRUE) {
907 // FIXME: generalizing form field naming conventions would reduce
908 // Lots of lines below.
909 foreach ($this->_columns
as $tableName => $table) {
910 if (array_key_exists('fields', $table)) {
911 foreach ($table['fields'] as $fieldName => $field) {
912 if (empty($field['no_display'])) {
913 if (!empty($field['required'])) {
915 $this->_defaults
['fields'][$fieldName] = 1;
918 // find element object, so that we could use quickform's freeze method
919 // for required elements
920 $obj = $this->getElementFromGroup("fields", $fieldName);
922 $freezeGroup[] = $obj;
926 elseif (isset($field['default'])) {
927 $this->_defaults
['fields'][$fieldName] = $field['default'];
933 if (array_key_exists('group_bys', $table)) {
934 foreach ($table['group_bys'] as $fieldName => $field) {
935 if (isset($field['default'])) {
936 if (!empty($field['frequency'])) {
937 $this->_defaults
['group_bys_freq'][$fieldName] = 'MONTH';
939 $this->_defaults
['group_bys'][$fieldName] = $field['default'];
943 if (array_key_exists('filters', $table)) {
944 foreach ($table['filters'] as $fieldName => $field) {
945 if (isset($field['default'])) {
946 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
948 if (is_array($field['default'])) {
949 $this->_defaults
["{$fieldName}_from"] = CRM_Utils_Array
::value('from', $field['default']);
950 $this->_defaults
["{$fieldName}_to"] = CRM_Utils_Array
::value('to', $field['default']);
951 $this->_defaults
["{$fieldName}_relative"] = 0;
954 $this->_defaults
["{$fieldName}_relative"] = $field['default'];
958 if ((CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_INT
) && is_array($field['default'])) {
959 $this->_defaults
["{$fieldName}_min"] = CRM_Utils_Array
::value('min', $field['default']);
960 $this->_defaults
["{$fieldName}_max"] = CRM_Utils_Array
::value('max', $field['default']);
962 $this->_defaults
["{$fieldName}_value"] = $field['default'];
965 //assign default value as "in" for multiselect
966 //operator, To freeze the select element
967 if (CRM_Utils_Array
::value('operatorType', $field) ==
968 CRM_Report_Form
::OP_MULTISELECT
970 $this->_defaults
["{$fieldName}_op"] = 'in';
972 if (CRM_Utils_Array
::value('operatorType', $field) ==
973 CRM_Report_Form
::OP_ENTITYREF
975 $this->_defaults
["{$fieldName}_op"] = 'in';
977 elseif (CRM_Utils_Array
::value('operatorType', $field) ==
978 CRM_Report_Form
::OP_MULTISELECT_SEPARATOR
980 $this->_defaults
["{$fieldName}_op"] = 'mhas';
982 elseif ($op = CRM_Utils_Array
::value('default_op', $field)) {
983 $this->_defaults
["{$fieldName}_op"] = $op;
989 empty($this->_formValues
['order_bys']) &&
990 (array_key_exists('order_bys', $table) &&
991 is_array($table['order_bys']))
993 if (!array_key_exists('order_bys', $this->_defaults
)) {
994 $this->_defaults
['order_bys'] = [];
996 foreach ($table['order_bys'] as $fieldName => $field) {
997 if (!empty($field['default']) ||
!empty($field['default_order']) ||
998 CRM_Utils_Array
::value('default_is_section', $field) ||
999 !empty($field['default_weight'])
1002 'column' => $fieldName,
1003 'order' => CRM_Utils_Array
::value('default_order', $field, 'ASC'),
1004 'section' => CRM_Utils_Array
::value('default_is_section', $field, 0),
1007 if (!empty($field['default_weight'])) {
1008 $this->_defaults
['order_bys'][(int) $field['default_weight']] = $order_by;
1011 array_unshift($this->_defaults
['order_bys'], $order_by);
1017 foreach ($this->_options
as $fieldName => $field) {
1018 if (isset($field['default'])) {
1019 $this->_defaults
['options'][$fieldName] = $field['default'];
1024 if (!empty($this->_submitValues
)) {
1025 $this->preProcessOrderBy($this->_submitValues
);
1028 $this->preProcessOrderBy($this->_defaults
);
1031 // lets finish freezing task here itself
1032 if (!empty($freezeGroup)) {
1033 foreach ($freezeGroup as $elem) {
1038 if ($this->_formValues
) {
1039 $this->_defaults
= array_merge($this->_defaults
, $this->_formValues
);
1042 if ($this->_instanceValues
) {
1043 $this->_defaults
= array_merge($this->_defaults
, $this->_instanceValues
);
1046 CRM_Report_Form_Instance
::setDefaultValues($this, $this->_defaults
);
1048 return $this->_defaults
;
1052 * Get element from group.
1054 * @param string $group
1055 * @param string $grpFieldName
1059 public function getElementFromGroup($group, $grpFieldName) {
1060 $eleObj = $this->getElement($group);
1061 foreach ($eleObj->_elements
as $index => $obj) {
1062 if ($grpFieldName == $obj->_attributes
['name']) {
1070 * Setter for $_params.
1072 * @param array $params
1074 public function setParams($params) {
1075 $this->_params
= $params;
1079 * Getter for $_params.
1081 * @return void|array $params
1083 public function getParams() {
1084 return $this->_params
;
1090 * @param int $instanceID
1092 public function setID($instanceID) {
1093 $this->_id
= $instanceID;
1097 * Setter for $_force.
1099 * @param bool $isForce
1101 public function setForce($isForce) {
1102 $this->_force
= $isForce;
1106 * Setter for $_limitValue.
1108 * @param int $_limitValue
1110 public function setLimitValue($_limitValue) {
1111 $this->_limitValue
= $_limitValue;
1115 * Setter for $_offsetValue.
1117 * @param int $_offsetValue
1119 public function setOffsetValue($_offsetValue) {
1120 $this->_offsetValue
= $_offsetValue;
1124 * Setter for $addPaging.
1126 * @param bool $value
1128 public function setAddPaging($value) {
1129 $this->addPaging
= $value;
1133 * Getter for $_defaultValues.
1137 public function getDefaultValues() {
1138 return $this->_defaults
;
1142 * Remove any temporary tables.
1144 public function cleanUpTemporaryTables() {
1145 foreach ($this->temporaryTables
as $temporaryTable) {
1146 CRM_Core_DAO
::executeQuery('DROP ' . ($temporaryTable['temporary'] ?
'TEMPORARY' : '') . ' TABLE IF EXISTS ' . $temporaryTable['name']);
1151 * Create a temporary table.
1153 * This function creates a table AND adds the details to the developer tab & $this->>temporary tables.
1155 * @param string $identifier
1156 * This is the key that will be used for the table in the temporaryTables property.
1157 * @param string $sql
1158 * Sql select statement or column description (the latter requires the columns flag)
1159 * @param bool $isColumns
1160 * Is the sql describing columns to create (rather than using a select query).
1161 * @param bool $isMemory
1162 * Create a memory table rather than a normal INNODB table.
1166 public function createTemporaryTable($identifier, $sql, $isColumns = FALSE, $isMemory = FALSE) {
1167 $tempTable = CRM_Utils_SQL_TempTable
::build()->setUtf8();
1169 $tempTable->setMemory();
1172 $tempTable->createWithColumns($sql);
1175 $tempTable->createWithQuery($sql);
1177 $name = $tempTable->getName();
1178 // Developers may force tables to be durable to assist in debugging so lets check.
1179 $isNotTrueTemporary = $tempTable->isDurable();
1180 $this->addToDeveloperTab($tempTable->getCreateSql());
1181 $this->temporaryTables
[$identifier] = ['temporary' => !$isNotTrueTemporary, 'name' => $name];
1186 * Add columns to report.
1188 public function addColumns() {
1191 foreach ($this->_columns
as $tableName => $table) {
1192 if (array_key_exists('fields', $table)) {
1193 foreach ($table['fields'] as $fieldName => $field) {
1195 if (empty($field['no_display'])) {
1196 foreach (['table', 'field'] as $var) {
1197 if (!empty($
{$var}['grouping'])) {
1198 if (!is_array($
{$var}['grouping'])) {
1199 $tableName = $
{$var}['grouping'];
1202 $tableName = array_keys($
{$var}['grouping']);
1203 $tableName = $tableName[0];
1204 $groupTitle = array_values($
{$var}['grouping']);
1205 $groupTitle = $groupTitle[0];
1210 if (!$groupTitle && isset($table['group_title'])) {
1211 $groupTitle = $table['group_title'];
1212 // Having a group_title is secret code for being a custom group
1213 // which cryptically translates to needing an accordion.
1214 // here we make that explicit.
1215 $colGroups[$tableName]['use_accordian_for_field_selection'] = TRUE;
1218 $colGroups[$tableName]['fields'][$fieldName] = CRM_Utils_Array
::value('title', $field);
1219 if ($groupTitle && empty($colGroups[$tableName]['group_title'])) {
1220 $colGroups[$tableName]['group_title'] = $groupTitle;
1222 $options[$fieldName] = CRM_Utils_Array
::value('title', $field);
1228 $this->addCheckBox("fields", ts('Select Columns'), $options, NULL,
1229 NULL, NULL, NULL, $this->_fourColumnAttribute
, TRUE
1231 if (!empty($colGroups)) {
1232 $this->tabs
['FieldSelection'] = [
1233 'title' => ts('Columns'),
1234 'tpl' => 'FieldSelection',
1235 'div_label' => 'col-groups',
1238 // Note this assignment is only really required in buildForm. It is being 'over-called'
1239 // to reduce risk of being missed due to overridden functions.
1240 $this->assign('tabs', $this->tabs
);
1243 $this->assign('colGroups', $colGroups);
1247 * Add filters to report.
1249 public function addFilters() {
1250 $filters = $filterGroups = [];
1253 foreach ($this->_filters
as $table => $attributes) {
1254 if (isset($this->_columns
[$table]['group_title'])) {
1255 // The presence of 'group_title' is secret code for 'is_a_custom_table'
1256 // which magically means to 'display in an accordian'
1257 // here we make this explicit.
1258 $filterGroups[$table] = [
1259 'group_title' => $this->_columns
[$table]['group_title'],
1260 'use_accordian_for_field_selection' => TRUE,
1264 foreach ($attributes as $fieldName => $field) {
1265 // get ready with option value pair
1266 // @ 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
1268 $operations = $this->getOperationPair(
1269 CRM_Utils_Array
::value('operatorType', $field),
1272 $filters[$table][$fieldName] = $field;
1274 switch (CRM_Utils_Array
::value('operatorType', $field)) {
1275 case CRM_Report_Form
::OP_MONTH
:
1276 if (!array_key_exists('options', $field) ||
1277 !is_array($field['options']) ||
empty($field['options'])
1279 // If there's no option list for this filter, define one.
1280 $field['options'] = [
1282 2 => ts('February'),
1289 9 => ts('September'),
1290 10 => ts('October'),
1291 11 => ts('November'),
1292 12 => ts('December'),
1294 // Add this option list to this column _columns. This is
1295 // required so that filter statistics show properly.
1296 $this->_columns
[$table]['filters'][$fieldName]['options'] = $field['options'];
1298 case CRM_Report_Form
::OP_MULTISELECT
:
1299 case CRM_Report_Form
::OP_MULTISELECT_SEPARATOR
:
1300 // assume a multi-select field
1301 if (!empty($field['options']) ||
1302 $fieldName == 'state_province_id' ||
$fieldName == 'county_id'
1304 $element = $this->addElement('select', "{$fieldName}_op", ts('Operator:'), $operations);
1305 if (count($operations) <= 1) {
1308 if ($fieldName == 'state_province_id' ||
1309 $fieldName == 'county_id'
1311 $this->addChainSelect($fieldName . '_value', [
1318 $this->addElement('select', "{$fieldName}_value", NULL, $field['options'], [
1319 'style' => 'min-width:250px',
1320 'class' => 'crm-select2 huge',
1322 'placeholder' => ts('- select -'),
1328 case CRM_Report_Form
::OP_SELECT
:
1329 // assume a select field
1330 $this->addElement('select', "{$fieldName}_op", ts('Operator:'), $operations);
1331 if (!empty($field['options'])) {
1332 $this->addElement('select', "{$fieldName}_value", NULL, $field['options']);
1336 case CRM_Report_Form
::OP_ENTITYREF
:
1337 $this->addElement('select', "{$fieldName}_op", ts('Operator:'), $operations);
1338 $this->setEntityRefDefaults($field, $table);
1339 $this->addEntityRef("{$fieldName}_value", NULL, $field['attributes']);
1342 case CRM_Report_Form
::OP_DATE
:
1343 // build datetime fields
1344 CRM_Core_Form_Date
::buildDateRange($this, $fieldName, $count, '_from', '_to', ts('From:'), FALSE, $operations);
1348 case CRM_Report_Form
::OP_DATETIME
:
1349 // build datetime fields
1350 CRM_Core_Form_Date
::buildDateRange($this, $fieldName, $count, '_from', '_to', ts('From:'), FALSE, $operations, 'searchDate', TRUE);
1354 case CRM_Report_Form
::OP_INT
:
1355 case CRM_Report_Form
::OP_FLOAT
:
1356 // and a min value input box
1357 $this->add('text', "{$fieldName}_min", ts('Min'));
1358 // and a max value input box
1359 $this->add('text', "{$fieldName}_max", ts('Max'));
1361 // default type is string
1362 $this->addElement('select', "{$fieldName}_op", ts('Operator:'), $operations,
1363 ['onchange' => "return showHideMaxMinVal( '$fieldName', this.value );"]
1365 // we need text box for value input
1366 $this->add('text', "{$fieldName}_value", NULL, ['class' => 'huge']);
1371 if (!empty($filters)) {
1372 $this->tabs
['Filters'] = [
1373 'title' => ts('Filters'),
1375 'div_label' => 'set-filters',
1378 $this->assign('filters', $filters);
1379 $this->assign('filterGroups', $filterGroups);
1383 * Function to assign the tabs to the template in the correct order.
1385 * We want the tabs to wind up in this order (if not overridden).
1393 protected function assignTabs() {
1401 $order = array_intersect_key(array_fill_keys($order, 1), $this->tabs
);
1402 $order = array_merge($order, $this->tabs
);
1403 $this->assign('tabs', $order);
1407 * The intent is to add a tab for developers to view the sql.
1409 * Currently using dpm.
1411 * @param string $sql
1413 public function addToDeveloperTab($sql) {
1414 if (!CRM_Core_Permission
::check('view report sql')) {
1417 $ignored_output_modes = ['pdf', 'csv', 'print'];
1418 if (in_array($this->_outputMode
, $ignored_output_modes)) {
1421 $this->tabs
['Developer'] = [
1422 'title' => ts('Developer'),
1423 'tpl' => 'Developer',
1424 'div_label' => 'set-developer',
1427 $this->assignTabs();
1428 $this->sqlArray
[] = $sql;
1429 foreach ($this->sqlArray
as $sql) {
1430 foreach (['LEFT JOIN'] as $term) {
1431 $sql = str_replace($term, '<br> ' . $term, $sql);
1433 foreach (['FROM', 'WHERE', 'GROUP BY', 'ORDER BY', 'LIMIT', ';'] as $term) {
1434 $sql = str_replace($term, '<br><br>' . $term, $sql);
1436 $this->sqlFormattedArray
[] = $sql;
1437 $this->assign('sql', implode(';<br><br><br><br>', $this->sqlFormattedArray
));
1439 $this->assign('sqlModes', $sqlModes = CRM_Utils_SQL
::getSqlModes());
1444 * Add options defined in $this->_options to the report.
1446 public function addOptions() {
1447 if (!empty($this->_options
)) {
1448 // FIXME: For now lets build all elements as checkboxes.
1449 // Once we clear with the format we can build elements based on type
1451 foreach ($this->_options
as $fieldName => $field) {
1454 if ($field['type'] == 'select') {
1455 $this->addElement('select', "{$fieldName}", $field['title'], $field['options']);
1457 elseif ($field['type'] == 'checkbox') {
1458 $options[$field['title']] = $fieldName;
1459 $this->addCheckBox($fieldName, NULL,
1461 NULL, NULL, NULL, $this->_fourColumnAttribute
1466 if (!empty($this->_options
) &&
1468 ||
($this->_id
&& CRM_Report_BAO_ReportInstance
::contactCanAdministerReport($this->_id
)))
1470 $this->tabs
['ReportOptions'] = [
1471 'title' => ts('Display Options'),
1472 'tpl' => 'ReportOptions',
1473 'div_label' => 'other-options',
1476 $this->assign('otherOptions', $this->_options
);
1480 * Add chart options to the report.
1482 public function addChartOptions() {
1483 if (!empty($this->_charts
)) {
1484 $this->addElement('select', "charts", ts('Chart'), $this->_charts
);
1485 $this->assign('charts', $this->_charts
);
1486 $this->addElement('submit', $this->_chartButtonName
, ts('View'));
1491 * Add group by options to the report.
1493 public function addGroupBys() {
1494 $options = $freqElements = [];
1496 foreach ($this->_columns
as $tableName => $table) {
1497 if (array_key_exists('group_bys', $table)) {
1498 foreach ($table['group_bys'] as $fieldName => $field) {
1499 if (!empty($field) && empty($field['no_display'])) {
1500 $options[$field['title']] = $fieldName;
1501 if (!empty($field['frequency'])) {
1502 $freqElements[$field['title']] = $fieldName;
1508 $this->addCheckBox("group_bys", ts('Group by columns'), $options, NULL,
1509 NULL, NULL, NULL, $this->_fourColumnAttribute
1511 $this->assign('groupByElements', $options);
1512 if (!empty($options)) {
1513 $this->tabs
['GroupBy'] = [
1514 'title' => ts('Grouping'),
1516 'div_label' => 'group-by-elements',
1520 foreach ($freqElements as $name) {
1521 $this->addElement('select', "group_bys_freq[$name]",
1522 ts('Frequency'), $this->_groupByDateFreq
1528 * Add data for order by tab.
1530 public function addOrderBys() {
1532 foreach ($this->_columns
as $tableName => $table) {
1534 // Report developer may define any column to order by; include these as order-by options.
1535 if (array_key_exists('order_bys', $table)) {
1536 foreach ($table['order_bys'] as $fieldName => $field) {
1537 if (!empty($field)) {
1538 $options[$fieldName] = $field['title'];
1543 // Add searchable custom fields as order-by options, if so requested
1544 // (These are already indexed, so allowing to order on them is cheap.)
1546 if ($this->_autoIncludeIndexedFieldsAsOrderBys
&&
1547 array_key_exists('extends', $table) && !empty($table['extends'])
1549 foreach ($table['fields'] as $fieldName => $field) {
1550 if (empty($field['no_display'])) {
1551 $options[$fieldName] = $field['title'];
1559 $this->assign('orderByOptions', $options);
1560 if (!empty($options)) {
1561 $this->tabs
['OrderBy'] = [
1562 'title' => ts('Sorting'),
1564 'div_label' => 'order-by-elements',
1568 if (!empty($options)) {
1570 '-' => ' - none - ',
1572 for ($i = 1; $i <= 5; $i++
) {
1573 $this->addElement('select', "order_bys[{$i}][column]", ts('Order by Column'), $options);
1574 $this->addElement('select', "order_bys[{$i}][order]", ts('Order by Order'), [
1575 'ASC' => ts('Ascending'),
1576 'DESC' => ts('Descending'),
1578 $this->addElement('checkbox', "order_bys[{$i}][section]", ts('Order by Section'), FALSE, ['id' => "order_by_section_$i"]);
1579 $this->addElement('checkbox', "order_bys[{$i}][pageBreak]", ts('Page Break'), FALSE, ['id' => "order_by_pagebreak_$i"]);
1585 * This adds the tab referred to as Title and Format, rendered through Instance.tpl.
1587 * @todo call this tab into the report template in the same way as OrderBy etc, ie
1588 * by adding a description of the tab to $this->tabs, causing the tab to be added in
1591 public function buildInstanceAndButtons() {
1592 CRM_Report_Form_Instance
::buildForm($this);
1593 $this->_actionButtonName
= $this->getButtonName('submit');
1594 $this->addTaskMenu($this->getActions($this->_id
));
1596 $this->assign('instanceForm', $this->_instanceForm
);
1598 // CRM-16274 Determine if user has 'edit all contacts' or equivalent
1599 $permission = CRM_Core_Permission
::getPermission();
1600 if ($this->_instanceForm
&& $permission == CRM_Core_Permission
::EDIT
&&
1601 $this->_add2groupSupported
1603 $this->addElement('select', 'groups', ts('Group'),
1604 ['' => ts('Add Contacts to Group')] +
1605 CRM_Core_PseudoConstant
::nestedGroup(),
1606 ['class' => 'crm-select2 crm-action-menu fa-plus huge']
1608 $this->assign('group', TRUE);
1611 $this->addElement('submit', $this->_groupButtonName
, '', ['style' => 'display: none;']);
1613 $this->addChartOptions();
1614 $showResultsLabel = $this->getResultsLabel();
1618 'name' => $showResultsLabel,
1619 'isDefault' => TRUE,
1625 * Has this form been submitted already?
1629 public function resultsDisplayed() {
1630 $buttonName = $this->controller
->getButtonName();
1631 return ($buttonName ||
$this->_outputMode
);
1635 * Get the actions for this report instance.
1637 * @param int $instanceId
1641 protected function getActions($instanceId) {
1642 $actions = CRM_Report_BAO_ReportInstance
::getActionMetadata();
1643 if (empty($instanceId)) {
1644 $actions['report_instance.save'] = [
1645 'title' => ts('Create Report'),
1647 'is_confirm' => TRUE,
1648 'confirm_title' => ts('Create Report'),
1649 'confirm_refresh_fields' => json_encode([
1650 'title' => ['selector' => '.crm-report-instanceForm-form-block-title', 'prepend' => ''],
1651 'description' => ['selector' => '.crm-report-instanceForm-form-block-description', 'prepend' => ''],
1655 unset($actions['report_instance.delete']);
1658 if (!$this->_csvSupported
) {
1659 unset($actions['report_instance.csv']);
1666 * Main build form function.
1668 public function buildQuickForm() {
1669 $this->addColumns();
1671 $this->addFilters();
1673 $this->addOptions();
1675 $this->addGroupBys();
1677 $this->addOrderBys();
1679 $this->buildInstanceAndButtons();
1681 // Add form rule for report.
1686 $this->addFormRule([get_class($this), 'formRule'], $this);
1688 $this->assignTabs();
1692 * A form rule function for custom data.
1694 * The rule ensures that fields selected in group_by if any) should only be the ones
1695 * present in display/select fields criteria;
1696 * note: works if and only if any custom field selected in group_by.
1698 * @param array $fields
1699 * @param array $ignoreFields
1703 public function customDataFormRule($fields, $ignoreFields = []) {
1705 if (!empty($this->_customGroupExtends
) && $this->_customGroupGroupBy
&&
1706 !empty($fields['group_bys'])
1708 foreach ($this->_columns
as $tableName => $table) {
1709 if ((substr($tableName, 0, 13) == 'civicrm_value' ||
1710 substr($tableName, 0, 12) == 'custom_value') &&
1711 !empty($this->_columns
[$tableName]['fields'])
1713 foreach ($this->_columns
[$tableName]['fields'] as $fieldName => $field) {
1714 if (array_key_exists($fieldName, $fields['group_bys']) &&
1715 !array_key_exists($fieldName, $fields['fields'])
1717 $errors['fields'] = "Please make sure fields selected in 'Group by Columns' section are also selected in 'Display Columns' section.";
1719 elseif (array_key_exists($fieldName, $fields['group_bys'])) {
1720 foreach ($fields['fields'] as $fld => $val) {
1721 if (!array_key_exists($fld, $fields['group_bys']) &&
1722 !in_array($fld, $ignoreFields)
1724 $errors['fields'] = "Please ensure that fields selected in 'Display Columns' are also selected in 'Group by Columns' section.";
1736 * Get operators to display on form.
1738 * Note: $fieldName param allows inheriting class to build operationPairs specific to a field.
1740 * @param string $type
1741 * @param string $fieldName
1745 public function getOperationPair($type = "string", $fieldName = NULL) {
1746 // FIXME: At some point we should move these key-val pairs
1747 // to option_group and option_value table.
1749 case CRM_Report_Form
::OP_INT
:
1750 case CRM_Report_Form
::OP_FLOAT
:
1753 'lte' => ts('Is less than or equal to'),
1754 'gte' => ts('Is greater than or equal to'),
1755 'bw' => ts('Is between'),
1756 'eq' => ts('Is equal to'),
1757 'lt' => ts('Is less than'),
1758 'gt' => ts('Is greater than'),
1759 'neq' => ts('Is not equal to'),
1760 'nbw' => ts('Is not between'),
1761 'nll' => ts('Is empty (Null)'),
1762 'nnll' => ts('Is not empty (Null)'),
1766 case CRM_Report_Form
::OP_SELECT
:
1768 'eq' => ts('Is equal to'),
1772 case CRM_Report_Form
::OP_MONTH
:
1773 case CRM_Report_Form
::OP_MULTISELECT
:
1774 case CRM_Report_Form
::OP_ENTITYREF
:
1777 'in' => ts('Is one of'),
1778 'notin' => ts('Is not one of'),
1782 case CRM_Report_Form
::OP_DATE
:
1785 'nll' => ts('Is empty (Null)'),
1786 'nnll' => ts('Is not empty (Null)'),
1790 case CRM_Report_Form
::OP_MULTISELECT_SEPARATOR
:
1791 // use this operator for the values, concatenated with separator. For e.g if
1792 // multiple options for a column is stored as ^A{val1}^A{val2}^A
1794 'mhas' => ts('Is one of'),
1795 'mnot' => ts('Is not one of'),
1802 'has' => ts('Contains'),
1803 'sw' => ts('Starts with'),
1804 'ew' => ts('Ends with'),
1805 'nhas' => ts('Does not contain'),
1806 'eq' => ts('Is equal to'),
1807 'neq' => ts('Is not equal to'),
1808 'nll' => ts('Is empty (Null)'),
1809 'nnll' => ts('Is not empty (Null)'),
1816 * Build the tag filter field to display on the filters tab.
1818 public function buildTagFilter() {
1819 $contactTags = CRM_Core_BAO_Tag
::getTags($this->_tagFilterTable
);
1820 if (!empty($contactTags)) {
1821 $this->_columns
['civicrm_tag'] = [
1822 'dao' => 'CRM_Core_DAO_Tag',
1826 'title' => ts('Tag'),
1827 'type' => CRM_Utils_Type
::T_INT
,
1829 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
1830 'options' => $contactTags,
1838 * Adds group filters to _columns (called from _Construct).
1840 public function buildGroupFilter() {
1841 $this->_columns
['civicrm_group']['filters'] = [
1843 'name' => 'group_id',
1844 'title' => ts('Group'),
1845 'type' => CRM_Utils_Type
::T_INT
,
1846 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
1848 'options' => CRM_Core_PseudoConstant
::nestedGroup(),
1851 if (empty($this->_columns
['civicrm_group']['dao'])) {
1852 $this->_columns
['civicrm_group']['dao'] = 'CRM_Contact_DAO_GroupContact';
1854 if (empty($this->_columns
['civicrm_group']['alias'])) {
1855 $this->_columns
['civicrm_group']['alias'] = 'cgroup';
1860 * Get SQL operator from form text version.
1862 * @param string $operator
1866 public function getSQLOperator($operator = "like") {
1867 switch ($operator) {
1900 return 'IS NOT NULL';
1909 * Generate where clause.
1911 * This can be overridden in reports for special treatment of a field
1913 * @param array $field Field specifications
1914 * @param string $op Query operator (not an exact match to sql)
1915 * @param mixed $value
1919 * @return null|string
1921 public function whereClause(&$field, $op, $value, $min, $max) {
1923 $type = CRM_Utils_Type
::typeToString(CRM_Utils_Array
::value('type', $field));
1925 // CRM-18010: Ensure type of each report filters
1927 trigger_error('Type is not defined for field ' . $field['name'], E_USER_WARNING
);
1934 if (($min !== NULL && strlen($min) > 0) ||
1935 ($max !== NULL && strlen($max) > 0)
1939 $min = CRM_Utils_Type
::escape($min, $type);
1941 $clauses[] = "( {$field['dbAlias']} >= $min )";
1944 $clauses[] = "( {$field['dbAlias']} < $min OR {$field['dbAlias']} IS NULL )";
1948 $max = CRM_Utils_Type
::escape($max, $type);
1950 $clauses[] = "( {$field['dbAlias']} <= $max )";
1953 $clauses[] = "( {$field['dbAlias']} > $max )";
1957 if (!empty($clauses)) {
1959 $clause = implode(' AND ', $clauses);
1962 $clause = '(' . implode('OR', $clauses) . ')';
1970 if ($value !== NULL && strlen($value) > 0) {
1971 $value = CRM_Utils_Type
::escape($value, $type);
1972 if (strpos($value, '%') === FALSE) {
1973 $value = "'%{$value}%'";
1976 $value = "'{$value}'";
1978 $sqlOP = $this->getSQLOperator($op);
1979 $clause = "( {$field['dbAlias']} $sqlOP $value )";
1985 if ((is_string($value) ||
is_numeric($value)) && strlen($value)) {
1986 $value = explode(',', $value);
1988 if ($value !== NULL && is_array($value) && count($value) > 0) {
1989 $sqlOP = $this->getSQLOperator($op);
1990 if (CRM_Utils_Array
::value('type', $field) ==
1991 CRM_Utils_Type
::T_STRING
1993 //cycle through selections and escape values
1994 foreach ($value as $key => $selection) {
1995 $value[$key] = CRM_Utils_Type
::escape($selection, $type);
1998 = "( {$field['dbAlias']} $sqlOP ( '" . implode("' , '", $value) .
2002 // for numerical values
2003 $clause = "{$field['dbAlias']} $sqlOP (" . implode(', ', $value) .
2006 if ($op == 'notin') {
2007 $clause = "( " . $clause . " OR {$field['dbAlias']} IS NULL )";
2010 $clause = "( " . $clause . " )";
2017 // multiple has or multiple not
2018 if ($value !== NULL && count($value) > 0) {
2019 $value = CRM_Utils_Type
::escapeAll($value, $type);
2020 $operator = $op == 'mnot' ?
'NOT' : '';
2021 $regexp = "([[:cntrl:]]|^)" . implode('([[:cntrl:]]|$)|([[:cntrl:]]|^)', (array) $value) . "([[:cntrl:]]|$)";
2022 $clause = "{$field['dbAlias']} {$operator} REGEXP '{$regexp}'";
2028 if ($value !== NULL && strlen($value) > 0) {
2029 $value = CRM_Utils_Type
::escape($value, $type);
2030 if (strpos($value, '%') === FALSE) {
2032 $value = "'{$value}%'";
2035 $value = "'%{$value}'";
2039 $value = "'{$value}'";
2041 $sqlOP = $this->getSQLOperator($op);
2042 $clause = "( {$field['dbAlias']} $sqlOP $value )";
2048 $sqlOP = $this->getSQLOperator($op);
2049 $clause = "( {$field['dbAlias']} $sqlOP )";
2055 //CRM-18457: some custom field passes value in array format against binary operator
2056 if (is_array($value) && count($value)) {
2061 if ($value !== NULL && $value !== '') {
2062 if (isset($field['clause'])) {
2063 // FIXME: we not doing escape here. Better solution is to use two
2064 // different types - data-type and filter-type
2065 $clause = $field['clause'];
2067 elseif (!is_array($value)) {
2068 $value = CRM_Utils_Type
::escape($value, $type);
2069 $sqlOP = $this->getSQLOperator($op);
2070 if ($field['type'] == CRM_Utils_Type
::T_STRING
) {
2071 $value = "'{$value}'";
2073 $clause = "( {$field['dbAlias']} $sqlOP $value )";
2079 //dev/core/544 Add report support for multiple contact subTypes
2080 if ($field['name'] == 'contact_sub_type' && $clause) {
2081 $clause = $this->whereSubtypeClause($field, $value, $op);
2083 if (!empty($field['group']) && $clause) {
2084 $clause = $this->whereGroupClause($field, $value, $op);
2086 elseif (!empty($field['tag']) && $clause) {
2087 // not using left join in query because if any contact
2088 // belongs to more than one tag, results duplicate
2090 $clause = $this->whereTagClause($field, $value, $op);
2092 elseif (!empty($field['membership_org']) && $clause) {
2093 $clause = $this->whereMembershipOrgClause($value, $op);
2095 elseif (!empty($field['membership_type']) && $clause) {
2096 $clause = $this->whereMembershipTypeClause($value, $op);
2102 * Get SQL where clause for contact subtypes
2103 * @param string $field
2104 * @param mixed $value
2105 * @param string $op SQL Operator
2109 public function whereSubtypeClause($field, $value, $op) {
2111 $subtypeFilters = count($value);
2112 for ($i = 0; $i < $subtypeFilters; $i++
) {
2113 $clause .= "{$field['dbAlias']} LIKE '%$value[$i]%'";
2114 if ($i !== ($subtypeFilters - 1)) {
2123 * Get SQL where clause for a date field.
2125 * @param string $fieldName
2126 * @param string $relative
2127 * @param string $from
2129 * @param string $type
2130 * @param string $fromTime
2131 * @param string $toTime
2133 * @return null|string
2135 public function dateClause(
2137 $relative, $from, $to, $type = NULL, $fromTime = NULL, $toTime = NULL
2140 if (in_array($relative, array_keys($this->getOperationPair(CRM_Report_Form
::OP_DATE
)))) {
2141 $sqlOP = $this->getSQLOperator($relative);
2142 return "( {$fieldName} {$sqlOP} )";
2145 list($from, $to) = $this->getFromTo($relative, $from, $to, $fromTime, $toTime);
2148 $clauses[] = "( {$fieldName} >= $from )";
2152 $clauses[] = "( {$fieldName} <= {$to} )";
2155 if (!empty($clauses)) {
2156 return implode(' AND ', $clauses);
2163 * Get values for from and to for date ranges.
2167 * @param bool $relative
2168 * @param string $from
2170 * @param string $fromTime
2171 * @param string $toTime
2175 public function getFromTo($relative, $from, $to, $fromTime = NULL, $toTime = NULL) {
2176 if (empty($toTime)) {
2177 // odd legacy behaviour to treat NULL as 'end of the day'
2178 // recommend updating reports to call CRM_Utils_Date::getFromTo
2179 //directly (default on the function is the actual default there).
2182 return CRM_Utils_Date
::getFromTo($relative, $from, $to, $fromTime, $toTime);
2186 * Alter display of rows.
2188 * Iterate through the rows retrieved via SQL and make changes for display purposes,
2189 * such as rendering contacts as links.
2191 * @param array $rows
2192 * Rows generated by SQL, with an array for each row.
2194 public function alterDisplay(&$rows) {
2198 * Alter the way in which custom data fields are displayed.
2200 * @param array $rows
2202 public function alterCustomDataDisplay(&$rows) {
2203 // custom code to alter rows having custom values
2204 if (empty($this->_customGroupExtends
)) {
2209 $customFieldIds = [];
2210 foreach ($this->_params
['fields'] as $fieldAlias => $value) {
2211 if ($fieldId = CRM_Core_BAO_CustomField
::getKeyID($fieldAlias)) {
2212 $customFieldIds[$fieldAlias] = $fieldId;
2215 if (empty($customFieldIds)) {
2219 // skip for type date and ContactReference since date format is already handled
2221 SELECT cg.table_name, cf.id
2222 FROM civicrm_custom_field cf
2223 INNER JOIN civicrm_custom_group cg ON cg.id = cf.custom_group_id
2224 WHERE cg.extends IN ('" . implode("','", $this->_customGroupExtends
) . "') AND
2225 cg.is_active = 1 AND
2226 cf.is_active = 1 AND
2227 cf.is_searchable = 1 AND
2228 cf.data_type NOT IN ('ContactReference', 'Date') AND
2229 cf.id IN (" . implode(",", $customFieldIds) . ")";
2231 $dao = CRM_Core_DAO
::executeQuery($query);
2232 while ($dao->fetch()) {
2233 $customFields[$dao->table_name
. '_custom_' . $dao->id
] = $dao->id
;
2237 $entryFound = FALSE;
2238 foreach ($rows as $rowNum => $row) {
2239 foreach ($row as $tableCol => $val) {
2240 if (array_key_exists($tableCol, $customFields)) {
2241 $rows[$rowNum][$tableCol] = CRM_Core_BAO_CustomField
::displayValue($val, $customFields[$tableCol]);
2246 // skip looking further in rows, if first row itself doesn't
2247 // have the column we need
2255 * Remove duplicate rows.
2257 * @param array $rows
2259 public function removeDuplicates(&$rows) {
2260 if (empty($this->_noRepeats
)) {
2265 foreach ($rows as $key => $list) {
2266 foreach ($list as $colName => $colVal) {
2267 if (array_key_exists($colName, $checkList) &&
2268 $checkList[$colName] == $colVal
2270 $rows[$key][$colName] = "";
2272 if (in_array($colName, $this->_noRepeats
)) {
2273 $checkList[$colName] = $colVal;
2280 * Fix subtotal display.
2283 * @param array $fields
2284 * @param bool $subtotal
2286 public function fixSubTotalDisplay(&$row, $fields, $subtotal = TRUE) {
2287 foreach ($row as $colName => $colVal) {
2288 if (in_array($colName, $fields)) {
2290 elseif (isset($this->_columnHeaders
[$colName])) {
2292 $row[$colName] = 'Subtotal';
2296 unset($row[$colName]);
2303 * Calculate grant total.
2305 * @param array $rows
2309 public function grandTotal(&$rows) {
2310 if (!$this->_rollup ||
count($rows) == 1) {
2314 $this->moveSummaryColumnsToTheRightHandSide();
2316 if ($this->_limit
&& count($rows) >= self
::ROW_COUNT_LIMIT
) {
2320 $this->rollupRow
= array_pop($rows);
2322 foreach ($this->_columnHeaders
as $fld => $val) {
2323 if (!in_array($fld, $this->_statFields
)) {
2324 if (!$this->_grandFlag
) {
2325 $this->rollupRow
[$fld] = ts('Grand Total');
2326 $this->_grandFlag
= TRUE;
2329 $this->rollupRow
[$fld] = "";
2334 $this->assign('grandStat', $this->rollupRow
);
2339 * Format display output.
2341 * @param array $rows
2342 * @param bool $pager
2344 public function formatDisplay(&$rows, $pager = TRUE) {
2345 // set pager based on if any limit was applied in the query.
2350 // allow building charts if any
2351 if (!empty($this->_params
['charts']) && !empty($rows)) {
2352 $this->buildChart($rows);
2353 $this->assign('chartEnabled', TRUE);
2354 $this->_chartId
= "{$this->_params['charts']}_" .
2355 ($this->_id ?
$this->_id
: substr(get_class($this), 16)) . '_' .
2357 $this->assign('chartId', $this->_chartId
);
2360 // unset columns not to be displayed.
2361 foreach ($this->_columnHeaders
as $key => $value) {
2362 if (!empty($value['no_display'])) {
2363 unset($this->_columnHeaders
[$key]);
2367 // unset columns not to be displayed.
2368 if (!empty($rows)) {
2369 foreach ($this->_noDisplay
as $noDisplayField) {
2370 foreach ($rows as $rowNum => $row) {
2371 unset($this->_columnHeaders
[$noDisplayField]);
2376 // build array of section totals
2377 $this->sectionTotals();
2379 // process grand-total row
2380 $this->grandTotal($rows);
2382 // Find alter display functions.
2383 $firstRow = reset($rows);
2385 $selectedFields = array_keys($firstRow);
2386 $alterFunctions = $alterMap = $alterSpecs = [];
2387 foreach ($this->_columns
as $tableName => $table) {
2388 if (array_key_exists('metadata', $table)) {
2389 foreach ($table['metadata'] as $field => $specs) {
2390 if (in_array($tableName . '_' . $field, $selectedFields)) {
2391 if (array_key_exists('alter_display', $specs)) {
2392 $alterFunctions[$tableName . '_' . $field] = $specs['alter_display'];
2393 $alterMap[$tableName . '_' . $field] = $field;
2394 $alterSpecs[$tableName . '_' . $field] = NULL;
2396 // Add any alters that can be intuited from the field specs.
2397 // So far only boolean but a lot more could be.
2398 if (empty($alterSpecs[$tableName . '_' . $field]) && isset($specs['type']) && $specs['type'] == CRM_Utils_Type
::T_BOOLEAN
) {
2399 $alterFunctions[$tableName . '_' . $field] = 'alterBoolean';
2400 $alterMap[$tableName . '_' . $field] = $field;
2401 $alterSpecs[$tableName . '_' . $field] = NULL;
2408 // Run the alter display functions
2409 foreach ($rows as $index => & $row) {
2410 foreach ($row as $selectedField => $value) {
2411 if (array_key_exists($selectedField, $alterFunctions)) {
2412 $rows[$index][$selectedField] = $this->{$alterFunctions[$selectedField]}($value, $row, $selectedField, $alterMap[$selectedField], $alterSpecs[$selectedField]);
2418 // use this method for formatting rows for display purpose.
2419 $this->alterDisplay($rows);
2420 CRM_Utils_Hook
::alterReportVar('rows', $rows, $this);
2422 // use this method for formatting custom rows for display purpose.
2423 $this->alterCustomDataDisplay($rows);
2429 * @param $selectedfield
2430 * @param $criteriaFieldName
2434 protected function alterStateProvinceID($value, &$row, $selectedfield, $criteriaFieldName) {
2435 $url = CRM_Utils_System
::url(CRM_Utils_System
::currentPath(), "reset=1&force=1&{$criteriaFieldName}_op=in&{$criteriaFieldName}_value={$value}", $this->_absoluteUrl
);
2436 $row[$selectedfield . '_link'] = $url;
2437 $row[$selectedfield . '_hover'] = ts("%1 for this state.", [
2441 $states = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
2442 if (!is_array($states)) {
2450 * @param $selectedField
2451 * @param $criteriaFieldName
2455 protected function alterCountryID($value, &$row, $selectedField, $criteriaFieldName) {
2456 $url = CRM_Utils_System
::url(CRM_Utils_System
::currentPath(), "reset=1&force=1&{$criteriaFieldName}_op=in&{$criteriaFieldName}_value={$value}", $this->_absoluteUrl
);
2457 $row[$selectedField . '_link'] = $url;
2458 $row[$selectedField . '_hover'] = ts("%1 for this country.", [
2461 $countries = CRM_Core_PseudoConstant
::country($value, FALSE);
2462 if (!is_array($countries)) {
2470 * @param $selectedfield
2471 * @param $criteriaFieldName
2475 protected function alterCountyID($value, &$row, $selectedfield, $criteriaFieldName) {
2476 $url = CRM_Utils_System
::url(CRM_Utils_System
::currentPath(), "reset=1&force=1&{$criteriaFieldName}_op=in&{$criteriaFieldName}_value={$value}", $this->_absoluteUrl
);
2477 $row[$selectedfield . '_link'] = $url;
2478 $row[$selectedfield . '_hover'] = ts("%1 for this county.", [
2481 $counties = CRM_Core_PseudoConstant
::county($value, FALSE);
2482 if (!is_array($counties)) {
2490 * @param $selectedfield
2491 * @param $criteriaFieldName
2495 protected function alterLocationTypeID($value, &$row, $selectedfield, $criteriaFieldName) {
2496 return CRM_Core_PseudoConstant
::getLabel('CRM_Core_DAO_Address', 'location_type_id', $value);
2506 protected function alterContactID($value, &$row, $fieldname) {
2507 $nameField = substr($fieldname, 0, -2) . 'name';
2508 static $first = TRUE;
2509 static $viewContactList = FALSE;
2511 $viewContactList = CRM_Core_Permission
::check('access CiviCRM');
2514 if (!$viewContactList) {
2517 if (array_key_exists($nameField, $row)) {
2518 $row[$nameField . '_link'] = CRM_Utils_System
::url("civicrm/contact/view", 'reset=1&cid=' . $value, $this->_absoluteUrl
);
2521 $row[$fieldname . '_link'] = CRM_Utils_System
::url("civicrm/contact/view", 'reset=1&cid=' . $value, $this->_absoluteUrl
);
2531 protected function alterBoolean($value) {
2532 $options = [0 => '', 1 => ts('Yes')];
2533 if (isset($options[$value])) {
2534 return $options[$value];
2542 * @param array $rows
2544 public function buildChart(&$rows) {
2545 // override this method for building charts.
2548 // select() method below has been added recently (v3.3), and many of the report templates might
2549 // still be having their own select() method. We should fix them as and when encountered and move
2550 // towards generalizing the select() method below.
2553 * Generate the SELECT clause and set class variable $_select.
2555 public function select() {
2556 $select = $this->_selectAliases
= [];
2557 $this->storeGroupByArray();
2559 foreach ($this->_columns
as $tableName => $table) {
2560 if (array_key_exists('fields', $table)) {
2561 foreach ($table['fields'] as $fieldName => $field) {
2562 if ($tableName == 'civicrm_address') {
2563 // deprecated, use $this->isTableSelected.
2564 $this->_addressField
= TRUE;
2566 if ($tableName == 'civicrm_email') {
2567 $this->_emailField
= TRUE;
2569 if ($tableName == 'civicrm_phone') {
2570 $this->_phoneField
= TRUE;
2573 if (!empty($field['required']) ||
2574 !empty($this->_params
['fields'][$fieldName])
2577 // 1. In many cases we want select clause to be built in slightly different way
2578 // for a particular field of a particular type.
2579 // 2. This method when used should receive params by reference and modify $this->_columnHeaders
2581 $selectClause = $this->selectClause($tableName, 'fields', $fieldName, $field);
2582 if ($selectClause) {
2583 $select[] = $selectClause;
2587 // include statistics columns only if set
2588 if (!empty($field['statistics']) && !empty($this->_groupByArray
)) {
2589 $select = $this->addStatisticsToSelect($field, $tableName, $fieldName, $select);
2593 $selectClause = $this->getSelectClauseWithGroupConcatIfNotGroupedBy($tableName, $fieldName, $field);
2594 if ($selectClause) {
2595 $select[] = $selectClause;
2598 $select = $this->addBasicFieldToSelect($tableName, $fieldName, $field, $select);
2605 // select for group bys
2606 if (array_key_exists('group_bys', $table)) {
2607 foreach ($table['group_bys'] as $fieldName => $field) {
2609 if ($tableName == 'civicrm_address') {
2610 $this->_addressField
= TRUE;
2612 if ($tableName == 'civicrm_email') {
2613 $this->_emailField
= TRUE;
2615 if ($tableName == 'civicrm_phone') {
2616 $this->_phoneField
= TRUE;
2618 // 1. In many cases we want select clause to be built in slightly different way
2619 // for a particular field of a particular type.
2620 // 2. This method when used should receive params by reference and modify $this->_columnHeaders
2622 $selectClause = $this->selectClause($tableName, 'group_bys', $fieldName, $field);
2623 if ($selectClause) {
2624 $select[] = $selectClause;
2628 if (!empty($this->_params
['group_bys']) &&
2629 !empty($this->_params
['group_bys'][$fieldName]) &&
2630 !empty($this->_params
['group_bys_freq'])
2632 switch (CRM_Utils_Array
::value($fieldName, $this->_params
['group_bys_freq'])) {
2634 $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL WEEKDAY({$field['dbAlias']}) DAY) AS {$tableName}_{$fieldName}_start";
2635 $select[] = "YEARWEEK({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
2636 $select[] = "WEEKOFYEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
2637 $field['title'] = 'Week';
2641 $select[] = "MAKEDATE(YEAR({$field['dbAlias']}), 1) AS {$tableName}_{$fieldName}_start";
2642 $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
2643 $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
2644 $field['title'] = 'Year';
2648 $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL (DAYOFMONTH({$field['dbAlias']})-1) DAY) as {$tableName}_{$fieldName}_start";
2649 $select[] = "MONTH({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
2650 $select[] = "MONTHNAME({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
2651 $field['title'] = 'Month';
2655 $select[] = "STR_TO_DATE(CONCAT( 3 * QUARTER( {$field['dbAlias']} ) -2 , '/', '1', '/', YEAR( {$field['dbAlias']} ) ), '%m/%d/%Y') AS {$tableName}_{$fieldName}_start";
2656 $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
2657 $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
2658 $field['title'] = 'Quarter';
2661 // for graphs and charts -
2662 if (!empty($this->_params
['group_bys_freq'][$fieldName])) {
2663 $this->_interval
= $field['title'];
2664 $this->_columnHeaders
["{$tableName}_{$fieldName}_start"]['title']
2665 = $field['title'] . ' Beginning';
2666 $this->_columnHeaders
["{$tableName}_{$fieldName}_start"]['type'] = $field['type'];
2667 $this->_columnHeaders
["{$tableName}_{$fieldName}_start"]['group_by'] = $this->_params
['group_bys_freq'][$fieldName];
2669 // just to make sure these values are transferred to rows.
2670 // since we 'll need them for calculation purpose,
2671 // e.g making subtotals look nicer or graphs
2672 $this->_columnHeaders
["{$tableName}_{$fieldName}_interval"] = ['no_display' => TRUE];
2673 $this->_columnHeaders
["{$tableName}_{$fieldName}_subtotal"] = ['no_display' => TRUE];
2680 if (empty($select)) {
2681 // CRM-21412 Do not give fatal error on report when no fields selected
2685 $this->_selectClauses
= $select;
2686 $this->_select
= "SELECT " . implode(', ', $select) . " ";
2690 * Build select clause for a single field.
2692 * @param string $tableName
2693 * @param string $tableKey
2694 * @param string $fieldName
2695 * @param string $field
2699 public function selectClause(&$tableName, $tableKey, &$fieldName, &$field) {
2700 if (!empty($field['pseudofield'])) {
2701 $alias = "{$tableName}_{$fieldName}";
2702 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
2703 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
2704 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['dbAlias'] = CRM_Utils_Array
::value('dbAlias', $field);
2705 $this->_selectAliases
[] = $alias;
2706 return ' 1 as ' . $alias;
2712 * Build where clause.
2714 public function where() {
2715 $this->storeWhereHavingClauseArray();
2717 if (empty($this->_whereClauses
)) {
2718 $this->_where
= "WHERE ( 1 ) ";
2719 $this->_having
= "";
2722 $this->_where
= "WHERE " . implode(' AND ', $this->_whereClauses
);
2725 if ($this->_aclWhere
) {
2726 $this->_where
.= " AND {$this->_aclWhere} ";
2729 if (!empty($this->_havingClauses
)) {
2730 // use this clause to construct group by clause.
2731 $this->_having
= "HAVING " . implode(' AND ', $this->_havingClauses
);
2736 * Store Where clauses into an array.
2738 * Breaking out this step makes over-riding more flexible as the clauses can be used in constructing a
2739 * temp table that may not be part of the final where clause or added
2740 * in other functions
2742 public function storeWhereHavingClauseArray() {
2743 foreach ($this->_columns
as $tableName => $table) {
2744 if (array_key_exists('filters', $table)) {
2745 foreach ($table['filters'] as $fieldName => $field) {
2746 // respect pseudofield to filter spec so fields can be marked as
2747 // not to be handled here
2748 if (!empty($field['pseudofield'])) {
2751 $clause = $this->generateFilterClause($field, $fieldName);
2753 if (!empty($clause)) {
2754 if (!empty($field['having'])) {
2755 $this->_havingClauses
[] = $clause;
2758 $this->_whereClauses
[] = $clause;
2770 public function processReportMode() {
2771 $this->setOutputMode();
2774 = CRM_Utils_Request
::retrieve(
2777 CRM_Core_DAO
::$_nullObject
2780 $this->_absoluteUrl
= FALSE;
2782 $this->assign('printOnly', FALSE);
2784 if ($this->_outputMode
== 'print' ||
2785 ($this->_sendmail
&& !$this->_outputMode
)
2787 $this->assign('printOnly', TRUE);
2789 $this->addPaging
= FALSE;
2790 $this->assign('outputMode', 'print');
2791 $this->_outputMode
= 'print';
2792 if ($this->_sendmail
) {
2793 $this->_absoluteUrl
= TRUE;
2796 elseif ($this->_outputMode
== 'pdf') {
2798 $this->addPaging
= FALSE;
2799 $this->_absoluteUrl
= TRUE;
2801 elseif ($this->_outputMode
== 'csv') {
2803 $this->_absoluteUrl
= TRUE;
2804 $this->addPaging
= FALSE;
2806 elseif ($this->_outputMode
== 'group') {
2807 $this->assign('outputMode', 'group');
2809 elseif ($this->_outputMode
== 'create_report' && $this->_criteriaForm
) {
2810 $this->assign('outputMode', 'create_report');
2812 elseif ($this->_outputMode
== 'copy' && $this->_criteriaForm
) {
2813 $this->_createNew
= TRUE;
2816 $this->assign('outputMode', $this->_outputMode
);
2817 $this->assign('printOnly', $printOnly);
2818 // Get today's date to include in printed reports
2820 $reportDate = CRM_Utils_Date
::customFormat(date('Y-m-d H:i'));
2821 $this->assign('reportDate', $reportDate);
2826 * Post Processing function for Form.
2828 * postProcessCommon should be used to set other variables from input as the api accesses that function.
2829 * This function is not accessed when the api calls the report.
2831 public function beginPostProcess() {
2832 $this->setParams($this->controller
->exportValues($this->_name
));
2833 if (empty($this->_params
) &&
2836 $this->setParams($this->_formValues
);
2839 // hack to fix params when submitted from dashboard, CRM-8532
2840 // fields array is missing because form building etc is skipped
2841 // in dashboard mode for report
2842 //@todo - this could be done in the dashboard no we have a setter
2843 if (empty($this->_params
['fields']) && !$this->_noFields
) {
2844 $this->setParams($this->_formValues
);
2847 $this->processReportMode();
2849 if ($this->_outputMode
== 'save' ||
$this->_outputMode
== 'copy') {
2850 $this->_createNew
= ($this->_outputMode
== 'copy');
2851 CRM_Report_Form_Instance
::postProcess($this);
2853 if ($this->_outputMode
== 'delete') {
2854 CRM_Report_BAO_ReportInstance
::doFormDelete($this->_id
, 'civicrm/report/list?reset=1', 'civicrm/report/list?reset=1');
2857 $this->_formValues
= $this->_params
;
2859 $this->beginPostProcessCommon();
2863 * BeginPostProcess function run in both report mode and non-report mode (api).
2865 public function beginPostProcessCommon() {
2869 * Build the report query.
2871 * @param bool $applyLimit
2875 public function buildQuery($applyLimit = TRUE) {
2876 $this->buildGroupTempTable();
2879 $this->customDataFrom();
2880 $this->buildPermissionClause();
2885 foreach ($this->unselectedOrderByColumns() as $alias => $field) {
2886 $clause = $this->getSelectClauseWithGroupConcatIfNotGroupedBy($field['table_name'], $field['name'], $field);
2888 $clause = "{$field['dbAlias']} as {$alias}";
2890 $this->_select
.= ", $clause ";
2893 if ($applyLimit && empty($this->_params
['charts'])) {
2896 CRM_Utils_Hook
::alterReportVar('sql', $this, $this);
2898 $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy} {$this->_limit}";
2899 $this->addToDeveloperTab($sql);
2904 * Build group by clause.
2906 public function groupBy() {
2907 $this->storeGroupByArray();
2909 if (!empty($this->_groupByArray
)) {
2910 if ($this->optimisedForOnlyFullGroupBy
) {
2911 // We should probably deprecate this code path. What happens here is that
2912 // the group by is amended to reflect the select columns. This often breaks the
2913 // results. Retrofitting group strict group by onto existing report classes
2915 $this->_groupBy
= CRM_Contact_BAO_Query
::getGroupByFromSelectColumns($this->_selectClauses
, $this->_groupByArray
);
2918 $this->_groupBy
= ' GROUP BY ' . implode($this->_groupByArray
);
2924 * Build order by clause.
2926 public function orderBy() {
2927 $this->_orderBy
= "";
2928 $this->_sections
= [];
2929 $this->storeOrderByArray();
2930 if (!empty($this->_orderByArray
) && !$this->_rollup
== 'WITH ROLLUP') {
2931 $this->_orderBy
= "ORDER BY " . implode(', ', $this->_orderByArray
);
2933 $this->assign('sections', $this->_sections
);
2937 * Extract order by fields and store as an array.
2939 * In some cases other functions want to know which fields are selected for ordering by
2940 * Separating this into a separate function allows it to be called separately from constructing
2941 * the order by clause
2943 public function storeOrderByArray() {
2946 if (!empty($this->_params
['order_bys']) &&
2947 is_array($this->_params
['order_bys']) &&
2948 !empty($this->_params
['order_bys'])
2951 // Process order_bys in user-specified order
2952 foreach ($this->_params
['order_bys'] as $orderBy) {
2954 foreach ($this->_columns
as $tableName => $table) {
2955 if (array_key_exists('order_bys', $table)) {
2956 // For DAO columns defined in $this->_columns
2957 $fields = $table['order_bys'];
2959 elseif (array_key_exists('extends', $table)) {
2960 // For custom fields referenced in $this->_customGroupExtends
2961 $fields = CRM_Utils_Array
::value('fields', $table, []);
2966 if (!empty($fields) && is_array($fields)) {
2967 foreach ($fields as $fieldName => $field) {
2968 if ($fieldName == $orderBy['column']) {
2969 $orderByField = array_merge($field, $orderBy);
2970 $orderByField['tplField'] = "{$tableName}_{$fieldName}";
2977 if (!empty($orderByField)) {
2978 $this->_orderByFields
[$orderByField['tplField']] = $orderByField;
2979 if ($this->groupConcatTested
) {
2980 $orderBys[$orderByField['tplField']] = "{$orderByField['tplField']} {$orderBy['order']}";
2983 // Not sure when this is preferable to using tplField (which has
2984 // definitely been tested to work in cases then this does not.
2985 // in caution not switching unless report has been tested for
2986 // group concat functionality.
2987 $orderBys[$orderByField['tplField']] = "{$orderByField['dbAlias']} {$orderBy['order']}";
2990 // Record any section headers for assignment to the template
2991 if (!empty($orderBy['section'])) {
2992 $orderByField['pageBreak'] = CRM_Utils_Array
::value('pageBreak', $orderBy);
2993 $this->_sections
[$orderByField['tplField']] = $orderByField;
2999 $this->_orderByArray
= $orderBys;
3001 $this->assign('sections', $this->_sections
);
3005 * Determine unselected columns.
3009 public function unselectedOrderByColumns() {
3010 return array_diff_key($this->_orderByFields
, $this->getSelectColumns());
3014 * Determine unselected columns.
3018 public function unselectedSectionColumns() {
3019 if (is_array($this->_sections
)) {
3020 return array_diff_key($this->_sections
, $this->getSelectColumns());
3028 * Build output rows.
3030 * @param string $sql
3031 * @param array $rows
3033 public function buildRows($sql, &$rows) {
3034 if (!$this->optimisedForOnlyFullGroupBy
) {
3035 CRM_Core_DAO
::disableFullGroupByMode();
3037 $dao = CRM_Core_DAO
::executeQuery($sql);
3038 if (stristr($this->_select
, 'SQL_CALC_FOUND_ROWS')) {
3039 $this->_rowsFound
= CRM_Core_DAO
::singleValueQuery('SELECT FOUND_ROWS()');
3041 CRM_Core_DAO
::reenableFullGroupByMode();
3042 if (!is_array($rows)) {
3046 // use this method to modify $this->_columnHeaders
3047 $this->modifyColumnHeaders();
3049 $unselectedSectionColumns = $this->unselectedSectionColumns();
3051 while ($dao->fetch()) {
3053 foreach ($this->_columnHeaders
as $key => $value) {
3054 if (property_exists($dao, $key)) {
3055 $row[$key] = $dao->$key;
3059 // section headers not selected for display need to be added to row
3060 foreach ($unselectedSectionColumns as $key => $values) {
3061 if (property_exists($dao, $key)) {
3062 $row[$key] = $dao->$key;
3071 * Calculate section totals.
3073 * When "order by" fields are marked as sections, this assigns to the template
3074 * an array of total counts for each section. This data is used by the Smarty
3075 * plugin {sectionTotal}.
3077 public function sectionTotals() {
3079 // Reports using order_bys with sections must populate $this->_selectAliases in select() method.
3080 if (empty($this->_selectAliases
)) {
3084 if (!empty($this->_sections
)) {
3085 // build the query with no LIMIT clause
3086 $select = str_ireplace('SELECT SQL_CALC_FOUND_ROWS ', 'SELECT ', $this->_select
);
3087 $sql = "{$select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy}";
3089 // pull section aliases out of $this->_sections
3090 $sectionAliases = array_keys($this->_sections
);
3093 foreach (array_merge($sectionAliases, $this->_selectAliases
) as $alias) {
3094 $ifnulls[] = "ifnull($alias, '') as $alias";
3096 $this->_select
= "SELECT " . implode(", ", $ifnulls);
3097 $this->_select
= CRM_Contact_BAO_Query
::appendAnyValueToSelect($ifnulls, $sectionAliases);
3099 // Group (un-limited) report by all aliases and get counts. This might
3100 // be done more efficiently when the contents of $sql are known, ie. by
3101 // overriding this method in the report class.
3103 $query = $this->_select
.
3104 ", count(*) as ct from ($sql) as subquery group by " .
3105 implode(", ", $sectionAliases);
3107 // initialize array of total counts
3109 $dao = CRM_Core_DAO
::executeQuery($query);
3110 while ($dao->fetch()) {
3112 // let $this->_alterDisplay translate any integer ids to human-readable values.
3113 $rows[0] = $dao->toArray();
3114 $this->alterDisplay($rows);
3117 // add totals for all permutations of section values
3120 $aliasCount = count($sectionAliases);
3121 foreach ($sectionAliases as $alias) {
3122 $values[] = $row[$alias];
3123 $key = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $values);
3124 if ($i == $aliasCount) {
3125 // the last alias is the lowest-level section header; use count as-is
3126 $totals[$key] = $dao->ct
;
3129 // other aliases are higher level; roll count into their total
3130 $totals[$key] +
= $dao->ct
;
3134 $this->assign('sectionTotals', $totals);
3139 * Modify column headers.
3141 public function modifyColumnHeaders() {
3142 // use this method to modify $this->_columnHeaders
3146 * Move totals columns to the right edge of the table.
3148 * It seems like a more logical layout to have any totals columns on the far right regardless of
3149 * the location of the rest of their table.
3151 public function moveSummaryColumnsToTheRightHandSide() {
3152 $statHeaders = (array_intersect_key($this->_columnHeaders
, array_flip($this->_statFields
)));
3153 $this->_columnHeaders
= array_merge(array_diff_key($this->_columnHeaders
, $statHeaders), $this->_columnHeaders
, $statHeaders);
3157 * Assign rows to the template.
3159 * @param array $rows
3161 public function doTemplateAssignment(&$rows) {
3162 $this->assign_by_ref('columnHeaders', $this->_columnHeaders
);
3163 $this->assign_by_ref('rows', $rows);
3164 $this->assign('statistics', $this->statistics($rows));
3168 * Build report statistics.
3170 * Override this method to build your own statistics.
3172 * @param array $rows
3176 public function statistics(&$rows) {
3179 $count = count($rows);
3180 // Why do we increment the count for rollup seems to artificially inflate the count.
3181 // It seems perhaps intentional to include the summary row in the count of results - although
3182 // this just seems odd.
3183 if ($this->_rollup
&& ($this->_rollup
!= '') && $this->_grandFlag
) {
3187 $this->countStat($statistics, $count);
3189 $this->groupByStat($statistics);
3191 $this->filterStat($statistics);
3197 * Add count statistics.
3199 * @param array $statistics
3202 public function countStat(&$statistics, $count) {
3203 $statistics['counts']['rowCount'] = [
3204 'title' => ts('Row(s) Listed'),
3208 if ($this->_rowsFound
&& ($this->_rowsFound
> $count)) {
3209 $statistics['counts']['rowsFound'] = [
3210 'title' => ts('Total Row(s)'),
3211 'value' => $this->_rowsFound
,
3217 * Add group by statistics.
3219 * @param array $statistics
3221 public function groupByStat(&$statistics) {
3222 if (!empty($this->_params
['group_bys']) &&
3223 is_array($this->_params
['group_bys']) &&
3224 !empty($this->_params
['group_bys'])
3226 foreach ($this->_columns
as $tableName => $table) {
3227 if (array_key_exists('group_bys', $table)) {
3228 foreach ($table['group_bys'] as $fieldName => $field) {
3229 if (!empty($this->_params
['group_bys'][$fieldName])) {
3230 $combinations[] = $field['title'];
3235 $statistics['groups'][] = [
3236 'title' => ts('Grouping(s)'),
3237 'value' => implode(' & ', $combinations),
3243 * Filter statistics.
3245 * @param array $statistics
3247 public function filterStat(&$statistics) {
3248 foreach ($this->_columns
as $tableName => $table) {
3249 if (array_key_exists('filters', $table)) {
3250 foreach ($table['filters'] as $fieldName => $field) {
3251 if ((CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE ||
3252 CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_TIME
) &&
3253 CRM_Utils_Array
::value('operatorType', $field) !=
3254 CRM_Report_Form
::OP_MONTH
3258 CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
),
3259 CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
),
3260 CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
),
3261 CRM_Utils_Array
::value("{$fieldName}_from_time", $this->_params
),
3262 CRM_Utils_Array
::value("{$fieldName}_to_time", $this->_params
)
3264 $from_time_format = !empty($this->_params
["{$fieldName}_from_time"]) ?
'h' : 'd';
3265 $from = CRM_Utils_Date
::customFormat($from, NULL, [$from_time_format]);
3267 $to_time_format = !empty($this->_params
["{$fieldName}_to_time"]) ?
'h' : 'd';
3268 $to = CRM_Utils_Date
::customFormat($to, NULL, [$to_time_format]);
3271 $statistics['filters'][] = [
3272 'title' => $field['title'],
3273 'value' => ts("Between %1 and %2", [1 => $from, 2 => $to]),
3276 elseif (in_array($rel = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
),
3277 array_keys($this->getOperationPair(CRM_Report_Form
::OP_DATE
))
3279 $pair = $this->getOperationPair(CRM_Report_Form
::OP_DATE
);
3280 $statistics['filters'][] = [
3281 'title' => $field['title'],
3282 'value' => $pair[$rel],
3287 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
3290 $pair = $this->getOperationPair(
3291 CRM_Utils_Array
::value('operatorType', $field),
3294 $min = CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
);
3295 $max = CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
);
3296 $val = CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
);
3297 if (in_array($op, ['bw', 'nbw']) && ($min ||
$max)) {
3298 $value = "{$pair[$op]} $min " . ts('and') . " $max";
3300 elseif ($val && CRM_Utils_Array
::value('operatorType', $field) & self
::OP_ENTITYREF
) {
3301 $this->setEntityRefDefaults($field, $tableName);
3302 $result = civicrm_api3($field['attributes']['entity'], 'getlist',
3304 CRM_Utils_Array
::value('api', $field['attributes'], []));
3306 foreach ($result['values'] as $v) {
3307 $values[] = $v['label'];
3309 $value = "{$pair[$op]} " . implode(', ', $values);
3311 elseif ($op == 'nll' ||
$op == 'nnll') {
3312 $value = $pair[$op];
3314 elseif (is_array($val) && (!empty($val))) {
3315 $options = CRM_Utils_Array
::value('options', $field, []);
3316 foreach ($val as $key => $valIds) {
3317 if (isset($options[$valIds])) {
3318 $val[$key] = $options[$valIds];
3321 $pair[$op] = (count($val) == 1) ?
(($op == 'notin' ||
$op ==
3322 'mnot') ?
ts('Is Not') : ts('Is')) : CRM_Utils_Array
::value($op, $pair);
3323 $val = implode(', ', $val);
3324 $value = "{$pair[$op]} " . $val;
3326 elseif (!is_array($val) && (!empty($val) ||
$val == '0') &&
3327 isset($field['options']) &&
3328 is_array($field['options']) && !empty($field['options'])
3330 $value = CRM_Utils_Array
::value($op, $pair) . " " .
3331 CRM_Utils_Array
::value($val, $field['options'], $val);
3334 $value = CRM_Utils_Array
::value($op, $pair) . " " . $val;
3337 if ($value && empty($field['no_display'])) {
3338 $statistics['filters'][] = [
3339 'title' => CRM_Utils_Array
::value('title', $field),
3350 * End post processing.
3352 * @param array|null $rows
3354 public function endPostProcess(&$rows = NULL) {
3355 $this->assign('report_class', get_class($this));
3356 if ($this->_storeResultSet
) {
3357 $this->_resultSet
= $rows;
3360 if ($this->_outputMode
== 'print' ||
3361 $this->_outputMode
== 'pdf' ||
3365 $content = $this->compileContent();
3366 $url = CRM_Utils_System
::url("civicrm/report/instance/{$this->_id}",
3370 if ($this->_sendmail
) {
3371 $config = CRM_Core_Config
::singleton();
3374 if ($this->_outputMode
== 'csv') {
3376 = $this->_formValues
['report_header'] . '<p>' . ts('Report URL') .
3377 ": {$url}</p>" . '<p>' .
3378 ts('The report is attached as a CSV file.') . '</p>' .
3379 $this->_formValues
['report_footer'];
3381 $csvFullFilename = $config->templateCompileDir
.
3382 CRM_Utils_File
::makeFileName('CiviReport.csv');
3383 $csvContent = CRM_Report_Utils_Report
::makeCsv($this, $rows);
3384 file_put_contents($csvFullFilename, $csvContent);
3386 'fullPath' => $csvFullFilename,
3387 'mime_type' => 'text/csv',
3388 'cleanName' => 'CiviReport.csv',
3391 if ($this->_outputMode
== 'pdf') {
3392 // generate PDF content
3393 $pdfFullFilename = $config->templateCompileDir
.
3394 CRM_Utils_File
::makeFileName('CiviReport.pdf');
3395 file_put_contents($pdfFullFilename,
3396 CRM_Utils_PDF_Utils
::html2pdf($content, "CiviReport.pdf",
3397 TRUE, ['orientation' => 'landscape']
3400 // generate Email Content
3402 = $this->_formValues
['report_header'] . '<p>' . ts('Report URL') .
3403 ": {$url}</p>" . '<p>' .
3404 ts('The report is attached as a PDF file.') . '</p>' .
3405 $this->_formValues
['report_footer'];
3408 'fullPath' => $pdfFullFilename,
3409 'mime_type' => 'application/pdf',
3410 'cleanName' => 'CiviReport.pdf',
3414 if (CRM_Report_Utils_Report
::mailReport($content, $this->_id
,
3415 $this->_outputMode
, $attachments
3418 CRM_Core_Session
::setStatus(ts("Report mail has been sent."), ts('Sent'), 'success');
3421 CRM_Core_Session
::setStatus(ts("Report mail could not be sent."), ts('Mail Error'), 'error');
3425 elseif ($this->_outputMode
== 'print') {
3429 if ($chartType = CRM_Utils_Array
::value('charts', $this->_params
)) {
3430 $config = CRM_Core_Config
::singleton();
3431 //get chart image name
3432 $chartImg = $this->_chartId
. '.png';
3433 //get image url path
3435 = str_replace('/persist/contribute/', '/persist/', $config->imageUploadURL
) .
3437 $uploadUrl .= $chartImg;
3438 //get image doc path to overwrite
3440 = str_replace('/persist/contribute/', '/persist/', $config->imageUploadDir
) .
3441 'openFlashChart/' . $chartImg;
3443 $chart = imagecreatefrompng($uploadUrl);
3444 //convert it into formatted png
3445 CRM_Utils_System
::setHttpHeader('Content-type', 'image/png');
3446 //overwrite with same image
3447 imagepng($chart, $uploadImg);
3449 imagedestroy($chart);
3451 CRM_Utils_PDF_Utils
::html2pdf($content, "CiviReport.pdf", FALSE, ['orientation' => 'landscape']);
3453 CRM_Utils_System
::civiExit();
3455 elseif ($this->_outputMode
== 'csv') {
3456 CRM_Report_Utils_Report
::export2csv($this, $rows);
3458 elseif ($this->_outputMode
== 'group') {
3459 $group = $this->_params
['groups'];
3460 $this->add2group($group);
3465 * Set store result set indicator to TRUE.
3467 * @todo explain what this does
3469 public function storeResultSet() {
3470 $this->_storeResultSet
= TRUE;
3478 public function getResultSet() {
3479 return $this->_resultSet
;
3483 * Get the sql used to generate the report.
3487 public function getReportSql() {
3488 return $this->sqlArray
;
3492 * Use the form name to create the tpl file name.
3496 public function getTemplateFileName() {
3497 $defaultTpl = parent
::getTemplateFileName();
3498 $template = CRM_Core_Smarty
::singleton();
3499 if (!$template->template_exists($defaultTpl)) {
3500 $defaultTpl = 'CRM/Report/Form.tpl';
3506 * Compile the report content.
3508 * Although this function is super-short it is useful to keep separate so it can be over-ridden by report classes.
3512 public function compileContent() {
3513 $templateFile = $this->getHookedTemplateFileName();
3514 return CRM_Utils_Array
::value('report_header', $this->_formValues
) .
3515 CRM_Core_Form
::$_template->fetch($templateFile) .
3516 CRM_Utils_Array
::value('report_footer', $this->_formValues
);
3520 * Post process function.
3522 public function postProcess() {
3523 // get ready with post process params
3524 $this->beginPostProcess();
3527 $sql = $this->buildQuery();
3529 // build array of result based on column headers. This method also allows
3530 // modifying column headers before using it to build result set i.e $rows.
3532 $this->buildRows($sql, $rows);
3534 // format result set.
3535 $this->formatDisplay($rows);
3537 // assign variables to templates
3538 $this->doTemplateAssignment($rows);
3540 // do print / pdf / instance stuff if needed
3541 $this->endPostProcess($rows);
3547 * @param int $rowCount
3551 public function limit($rowCount = self
::ROW_COUNT_LIMIT
) {
3552 // lets do the pager if in html mode
3553 $this->_limit
= NULL;
3555 // CRM-14115, over-ride row count if rowCount is specified in URL
3556 if ($this->_dashBoardRowCount
) {
3557 $rowCount = $this->_dashBoardRowCount
;
3559 if ($this->addPaging
) {
3560 $this->_select
= preg_replace('/SELECT(\s+SQL_CALC_FOUND_ROWS)?\s+/i', 'SELECT SQL_CALC_FOUND_ROWS ', $this->_select
);
3562 $pageId = CRM_Utils_Request
::retrieve('crmPID', 'Integer');
3564 // @todo all http vars should be extracted in the preProcess
3565 // - not randomly in the class
3566 if (!$pageId && !empty($_POST)) {
3567 if (isset($_POST['PagerBottomButton']) && isset($_POST['crmPID_B'])) {
3568 $pageId = max((int) $_POST['crmPID_B'], 1);
3570 elseif (isset($_POST['PagerTopButton']) && isset($_POST['crmPID'])) {
3571 $pageId = max((int) $_POST['crmPID'], 1);
3573 unset($_POST['crmPID_B'], $_POST['crmPID']);
3576 $pageId = $pageId ?
$pageId : 1;
3577 $this->set(CRM_Utils_Pager
::PAGE_ID
, $pageId);
3578 $offset = ($pageId - 1) * $rowCount;
3580 $offset = CRM_Utils_Type
::escape($offset, 'Int');
3581 $rowCount = CRM_Utils_Type
::escape($rowCount, 'Int');
3583 $this->_limit
= " LIMIT $offset, $rowCount";
3584 return [$offset, $rowCount];
3586 if ($this->_limitValue
) {
3587 if ($this->_offsetValue
) {
3588 $this->_limit
= " LIMIT {$this->_offsetValue}, {$this->_limitValue} ";
3591 $this->_limit
= " LIMIT " . $this->_limitValue
;
3599 * @param int $rowCount
3601 public function setPager($rowCount = self
::ROW_COUNT_LIMIT
) {
3602 // CRM-14115, over-ride row count if rowCount is specified in URL
3603 if ($this->_dashBoardRowCount
) {
3604 $rowCount = $this->_dashBoardRowCount
;
3607 if ($this->_limit
&& ($this->_limit
!= '')) {
3608 if (!$this->_rowsFound
) {
3609 $sql = "SELECT FOUND_ROWS();";
3610 $this->_rowsFound
= CRM_Core_DAO
::singleValueQuery($sql);
3613 'total' => $this->_rowsFound
,
3614 'rowCount' => $rowCount,
3615 'status' => ts('Records') . ' %%StatusMessage%%',
3616 'buttonBottom' => 'PagerBottomButton',
3617 'buttonTop' => 'PagerTopButton',
3619 if (!empty($this->controller
)) {
3620 // This happens when being called from the api Really we want the api to be able to
3621 // pass paging parameters, but at this stage just preventing test crashes.
3622 $params['pageID'] = $this->get(CRM_Utils_Pager
::PAGE_ID
);
3625 $pager = new CRM_Utils_Pager($params);
3626 $this->assign_by_ref('pager', $pager);
3627 $this->ajaxResponse
['totalRows'] = $this->_rowsFound
;
3632 * Build a group filter with contempt for large data sets.
3634 * This function has been retained as it takes time to migrate the reports over
3635 * to the new method which will not crash on large datasets.
3639 * @param string $field
3640 * @param mixed $value
3645 public function legacySlowGroupFilterClause($field, $value, $op) {
3646 $smartGroupQuery = "";
3648 $group = new CRM_Contact_DAO_Group();
3649 $group->is_active
= 1;
3652 while ($group->fetch()) {
3653 if (in_array($group->id
, (array) $this->_params
['gid_value']) &&
3654 $group->saved_search_id
3656 $smartGroups[] = $group->id
;
3660 CRM_Contact_BAO_GroupContactCache
::check($smartGroups);
3662 $smartGroupQuery = '';
3663 if (!empty($smartGroups)) {
3664 $smartGroups = implode(',', $smartGroups);
3665 $smartGroupQuery = " UNION DISTINCT
3666 SELECT DISTINCT smartgroup_contact.contact_id
3667 FROM civicrm_group_contact_cache smartgroup_contact
3668 WHERE smartgroup_contact.group_id IN ({$smartGroups}) ";
3671 $sqlOp = $this->getSQLOperator($op);
3672 if (!is_array($value)) {
3675 //include child groups if any
3676 $value = array_merge($value, CRM_Contact_BAO_Group
::getChildGroupIds($value));
3678 $clause = "{$field['dbAlias']} IN (" . implode(', ', $value) . ")";
3680 $contactAlias = $this->_aliases
['civicrm_contact'];
3681 if (!empty($this->relationType
) && $this->relationType
== 'b_a') {
3682 $contactAlias = $this->_aliases
['civicrm_contact_b'];
3684 return " {$contactAlias}.id {$sqlOp} (
3685 SELECT DISTINCT {$this->_aliases['civicrm_group']}.contact_id
3686 FROM civicrm_group_contact {$this->_aliases['civicrm_group']}
3687 WHERE {$clause} AND {$this->_aliases['civicrm_group']}.status = 'Added'
3688 {$smartGroupQuery} ) ";
3692 * Build where clause for groups.
3694 * @param string $field
3695 * @param mixed $value
3700 public function whereGroupClause($field, $value, $op) {
3701 if ($this->groupFilterNotOptimised
) {
3702 return $this->legacySlowGroupFilterClause($field, $value, $op);
3704 if ($op === 'notin') {
3705 return " group_temp_table.id IS NULL ";
3707 // We will have used an inner join instead.
3712 * Create a table of the contact ids included by the group filter.
3714 * This function is called by both the api (tests) and the UI.
3716 public function buildGroupTempTable() {
3717 if (!empty($this->groupTempTable
) ||
empty($this->_params
['gid_value']) ||
$this->groupFilterNotOptimised
) {
3720 $filteredGroups = (array) $this->_params
['gid_value'];
3722 $groups = civicrm_api3('Group', 'get', [
3724 'id' => ['IN' => $filteredGroups],
3725 'saved_search_id' => ['>' => 0],
3728 $smartGroups = array_keys($groups['values']);
3731 SELECT DISTINCT group_contact.contact_id as id
3732 FROM civicrm_group_contact group_contact
3733 WHERE group_contact.group_id IN (" . implode(', ', $filteredGroups) . ")
3734 AND group_contact.status = 'Added' ";
3736 if (!empty($smartGroups)) {
3737 CRM_Contact_BAO_GroupContactCache
::check($smartGroups);
3738 $smartGroups = implode(',', $smartGroups);
3741 SELECT smartgroup_contact.contact_id as id
3742 FROM civicrm_group_contact_cache smartgroup_contact
3743 WHERE smartgroup_contact.group_id IN ({$smartGroups}) ";
3746 $this->groupTempTable
= $this->createTemporaryTable('rptgrp', $query);
3747 CRM_Core_DAO
::executeQuery("ALTER TABLE $this->groupTempTable ADD INDEX i_id(id)");
3751 * Execute query and add it to the developer tab.
3753 * @param string $query
3754 * @param array $params
3756 * @return \CRM_Core_DAO|object
3758 protected function executeReportQuery($query, $params = []) {
3759 $this->addToDeveloperTab($query);
3760 return CRM_Core_DAO
::executeQuery($query, $params);
3764 * Build where clause for tags.
3766 * @param string $field
3767 * @param mixed $value
3772 public function whereTagClause($field, $value, $op) {
3773 // not using left join in query because if any contact
3774 // belongs to more than one tag, results duplicate
3776 $sqlOp = $this->getSQLOperator($op);
3777 if (!is_array($value)) {
3780 $clause = "{$field['dbAlias']} IN (" . implode(', ', $value) . ")";
3781 $entity_table = $this->_tagFilterTable
;
3782 return " {$this->_aliases[$entity_table]}.id {$sqlOp} (
3783 SELECT DISTINCT {$this->_aliases['civicrm_tag']}.entity_id
3784 FROM civicrm_entity_tag {$this->_aliases['civicrm_tag']}
3785 WHERE entity_table = '$entity_table' AND {$clause} ) ";
3789 * Generate membership organization clause.
3791 * @param mixed $value
3792 * @param string $op SQL Operator
3796 public function whereMembershipOrgClause($value, $op) {
3797 $sqlOp = $this->getSQLOperator($op);
3798 if (!is_array($value)) {
3802 $tmp_membership_org_sql_list = implode(', ', $value);
3803 return " {$this->_aliases['civicrm_contact']}.id {$sqlOp} (
3804 SELECT DISTINCT mem.contact_id
3805 FROM civicrm_membership mem
3806 LEFT JOIN civicrm_membership_status mem_status ON mem.status_id = mem_status.id
3807 LEFT JOIN civicrm_membership_type mt ON mem.membership_type_id = mt.id
3808 WHERE mt.member_of_contact_id IN (" .
3809 $tmp_membership_org_sql_list . ")
3810 AND mt.is_active = '1'
3811 AND mem_status.is_current_member = '1'
3812 AND mem_status.is_active = '1' ) ";
3816 * Generate Membership Type SQL Clause.
3818 * @param mixed $value
3824 public function whereMembershipTypeClause($value, $op) {
3825 $sqlOp = $this->getSQLOperator($op);
3826 if (!is_array($value)) {
3830 $tmp_membership_sql_list = implode(', ', $value);
3831 return " {$this->_aliases['civicrm_contact']}.id {$sqlOp} (
3832 SELECT DISTINCT mem.contact_id
3833 FROM civicrm_membership mem
3834 LEFT JOIN civicrm_membership_status mem_status ON mem.status_id = mem_status.id
3835 LEFT JOIN civicrm_membership_type mt ON mem.membership_type_id = mt.id
3836 WHERE mem.membership_type_id IN (" .
3837 $tmp_membership_sql_list . ")
3838 AND mt.is_active = '1'
3839 AND mem_status.is_current_member = '1'
3840 AND mem_status.is_active = '1' ) ";
3844 * Buld contact acl clause
3845 * @deprecated in favor of buildPermissionClause
3847 * @param string $tableAlias
3849 public function buildACLClause($tableAlias = 'contact_a') {
3850 list($this->_aclFrom
, $this->_aclWhere
) = CRM_Contact_BAO_Contact_Permission
::cacheClause($tableAlias);
3854 * Build the permision clause for all entities in this report
3856 public function buildPermissionClause() {
3858 foreach ($this->selectedTables() as $tableName) {
3859 $baoName = str_replace('_DAO_', '_BAO_', CRM_Core_DAO_AllCoreTables
::getClassForTable($tableName));
3860 if ($baoName && class_exists($baoName) && !empty($this->_columns
[$tableName]['alias'])) {
3861 $tableAlias = $this->_columns
[$tableName]['alias'];
3862 $clauses = array_filter($baoName::getSelectWhereClause($tableAlias));
3863 foreach ($clauses as $field => $clause) {
3864 // Skip contact_id field if redundant
3865 if ($field != 'contact_id' ||
!in_array('civicrm_contact', $this->selectedTables())) {
3866 $ret["$tableName.$field"] = $clause;
3871 // Override output from buildACLClause
3872 $this->_aclFrom
= NULL;
3873 $this->_aclWhere
= implode(' AND ', $ret);
3877 * Add custom data to the columns.
3879 * @param bool $addFields
3880 * @param array $permCustomGroupIds
3882 public function addCustomDataToColumns($addFields = TRUE, $permCustomGroupIds = []) {
3883 if (empty($this->_customGroupExtends
)) {
3886 if (!is_array($this->_customGroupExtends
)) {
3887 $this->_customGroupExtends
= [$this->_customGroupExtends
];
3889 $customGroupWhere = '';
3890 if (!empty($permCustomGroupIds)) {
3891 $customGroupWhere = "cg.id IN (" . implode(',', $permCustomGroupIds) .
3895 SELECT cg.table_name, cg.title, cg.extends, cf.id as cf_id, cf.label,
3896 cf.column_name, cf.data_type, cf.html_type, cf.option_group_id, cf.time_format
3897 FROM civicrm_custom_group cg
3898 INNER JOIN civicrm_custom_field cf ON cg.id = cf.custom_group_id
3899 WHERE cg.extends IN ('" . implode("','", $this->_customGroupExtends
) . "') AND
3901 cg.is_active = 1 AND
3902 cf.is_active = 1 AND
3903 cf.is_searchable = 1
3904 ORDER BY cg.weight, cf.weight";
3905 $customDAO = CRM_Core_DAO
::executeQuery($sql);
3908 while ($customDAO->fetch()) {
3909 if ($customDAO->table_name
!= $curTable) {
3910 $curTable = $customDAO->table_name
;
3911 $curFields = $curFilters = [];
3914 $this->_columns
[$curTable]['dao'] = 'CRM_Contact_DAO_Contact';
3915 $this->_columns
[$curTable]['extends'] = $customDAO->extends;
3916 $this->_columns
[$curTable]['grouping'] = $customDAO->table_name
;
3917 $this->_columns
[$curTable]['group_title'] = $customDAO->title
;
3919 foreach (['fields', 'filters', 'group_bys'] as $colKey) {
3920 if (!array_key_exists($colKey, $this->_columns
[$curTable])) {
3921 $this->_columns
[$curTable][$colKey] = [];
3925 $fieldName = 'custom_' . $customDAO->cf_id
;
3928 // this makes aliasing work in favor
3929 $curFields[$fieldName] = [
3930 'name' => $customDAO->column_name
,
3931 'title' => $customDAO->label
,
3932 'dataType' => $customDAO->data_type
,
3933 'htmlType' => $customDAO->html_type
,
3936 if ($this->_customGroupFilters
) {
3937 // this makes aliasing work in favor
3938 $curFilters[$fieldName] = [
3939 'name' => $customDAO->column_name
,
3940 'title' => $customDAO->label
,
3941 'dataType' => $customDAO->data_type
,
3942 'htmlType' => $customDAO->html_type
,
3946 switch ($customDAO->data_type
) {
3949 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form
::OP_DATE
;
3950 $curFilters[$fieldName]['type'] = CRM_Utils_Type
::T_DATE
;
3951 // CRM-6946, show time part for datetime date fields
3952 if ($customDAO->time_format
) {
3953 $curFields[$fieldName]['type'] = CRM_Utils_Type
::T_TIMESTAMP
;
3958 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form
::OP_SELECT
;
3959 $curFilters[$fieldName]['options'] = ['' => ts('- select -')] + CRM_Core_PseudoConstant
::get('CRM_Core_BAO_CustomField', 'custom_' . $customDAO->cf_id
, [], 'search');
3960 $curFilters[$fieldName]['type'] = CRM_Utils_Type
::T_INT
;
3964 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form
::OP_INT
;
3965 $curFilters[$fieldName]['type'] = CRM_Utils_Type
::T_INT
;
3969 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form
::OP_FLOAT
;
3970 $curFilters[$fieldName]['type'] = CRM_Utils_Type
::T_MONEY
;
3971 $curFields[$fieldName]['type'] = CRM_Utils_Type
::T_MONEY
;
3975 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form
::OP_FLOAT
;
3976 $curFilters[$fieldName]['type'] = CRM_Utils_Type
::T_FLOAT
;
3980 case 'StateProvince':
3982 $curFilters[$fieldName]['type'] = CRM_Utils_Type
::T_STRING
;
3984 $options = CRM_Core_PseudoConstant
::get('CRM_Core_BAO_CustomField', 'custom_' . $customDAO->cf_id
, [], 'search');
3985 if ((is_array($options) && count($options) != 0) ||
(!is_array($options) && $options !== FALSE)) {
3986 $curFilters[$fieldName]['operatorType'] = CRM_Core_BAO_CustomField
::isSerialized($customDAO) ? CRM_Report_Form
::OP_MULTISELECT_SEPARATOR
: CRM_Report_Form
::OP_MULTISELECT
;
3987 $curFilters[$fieldName]['options'] = $options;
3991 case 'ContactReference':
3992 $curFilters[$fieldName]['type'] = CRM_Utils_Type
::T_STRING
;
3993 $curFilters[$fieldName]['name'] = 'display_name';
3994 $curFilters[$fieldName]['alias'] = "contact_{$fieldName}_civireport";
3996 $curFields[$fieldName]['type'] = CRM_Utils_Type
::T_STRING
;
3997 $curFields[$fieldName]['name'] = 'display_name';
3998 $curFields[$fieldName]['alias'] = "contact_{$fieldName}_civireport";
4002 $curFields[$fieldName]['type'] = CRM_Utils_Type
::T_STRING
;
4003 $curFilters[$fieldName]['type'] = CRM_Utils_Type
::T_STRING
;
4007 if ($customDAO->html_type
== 'Select' && !array_key_exists('options', $curFilters[$fieldName])) {
4008 $options = CRM_Core_PseudoConstant
::get('CRM_Core_BAO_CustomField', 'custom_' . $customDAO->cf_id
, [], 'search');
4009 if ($options !== FALSE) {
4010 $curFilters[$fieldName]['operatorType'] = CRM_Core_BAO_CustomField
::isSerialized($customDAO) ? CRM_Report_Form
::OP_MULTISELECT_SEPARATOR
: CRM_Report_Form
::OP_MULTISELECT
;
4011 $curFilters[$fieldName]['options'] = $options;
4015 if (!array_key_exists('type', $curFields[$fieldName])) {
4016 $curFields[$fieldName]['type'] = CRM_Utils_Array
::value('type', $curFilters[$fieldName], []);
4020 $this->_columns
[$curTable]['fields'] = array_merge($this->_columns
[$curTable]['fields'], $curFields);
4022 if ($this->_customGroupFilters
) {
4023 $this->_columns
[$curTable]['filters'] = array_merge($this->_columns
[$curTable]['filters'], $curFilters);
4025 if ($this->_customGroupGroupBy
) {
4026 $this->_columns
[$curTable]['group_bys'] = array_merge($this->_columns
[$curTable]['group_bys'], $curFields);
4032 * Build custom data from clause.
4034 * @param bool $joinsForFiltersOnly
4035 * Only include joins to support filters. This would be used if creating a table of contacts to include first.
4037 public function customDataFrom($joinsForFiltersOnly = FALSE) {
4038 if (empty($this->_customGroupExtends
)) {
4041 $mapper = CRM_Core_BAO_CustomQuery
::$extendsMap;
4042 //CRM-18276 GROUP_CONCAT could be used with singleValueQuery and then exploded,
4043 //but by default that truncates to 1024 characters, which causes errors with installs with lots of custom field sets
4045 $customTablesDAO = CRM_Core_DAO
::executeQuery("SELECT table_name FROM civicrm_custom_group");
4046 while ($customTablesDAO->fetch()) {
4047 $customTables[] = $customTablesDAO->table_name
;
4050 foreach ($this->_columns
as $table => $prop) {
4051 if (in_array($table, $customTables)) {
4052 $extendsTable = $mapper[$prop['extends']];
4053 // Check field is required for rendering the report.
4054 if ((!$this->isFieldSelected($prop)) ||
($joinsForFiltersOnly && !$this->isFieldFiltered($prop))) {
4057 $baseJoin = CRM_Utils_Array
::value($prop['extends'], $this->_customGroupExtendsJoin
, "{$this->_aliases[$extendsTable]}.id");
4059 $customJoin = is_array($this->_customGroupJoin
) ?
$this->_customGroupJoin
[$table] : $this->_customGroupJoin
;
4061 {$customJoin} {$table} {$this->_aliases[$table]} ON {$this->_aliases[$table]}.entity_id = {$baseJoin}";
4062 // handle for ContactReference
4063 if (array_key_exists('fields', $prop)) {
4064 foreach ($prop['fields'] as $fieldName => $field) {
4065 if (CRM_Utils_Array
::value('dataType', $field) ==
4068 $columnName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField', CRM_Core_BAO_CustomField
::getKeyID($fieldName), 'column_name');
4070 LEFT JOIN civicrm_contact {$field['alias']} ON {$field['alias']}.id = {$this->_aliases[$table]}.{$columnName} ";
4079 * Check if the field is selected.
4081 * @param string $prop
4085 public function isFieldSelected($prop) {
4090 if (!empty($this->_params
['fields'])) {
4091 foreach (array_keys($prop['fields']) as $fieldAlias) {
4092 $customFieldId = CRM_Core_BAO_CustomField
::getKeyID($fieldAlias);
4093 if ($customFieldId) {
4094 if (array_key_exists($fieldAlias, $this->_params
['fields'])) {
4098 //might be survey response field.
4099 if (!empty($this->_params
['fields']['survey_response']) &&
4100 !empty($prop['fields'][$fieldAlias]['isSurveyResponseField'])
4108 if (!empty($this->_params
['group_bys']) && $this->_customGroupGroupBy
) {
4109 foreach (array_keys($prop['group_bys']) as $fieldAlias) {
4110 if (array_key_exists($fieldAlias, $this->_params
['group_bys']) &&
4111 CRM_Core_BAO_CustomField
::getKeyID($fieldAlias)
4118 if (!empty($this->_params
['order_bys'])) {
4119 foreach (array_keys($prop['fields']) as $fieldAlias) {
4120 foreach ($this->_params
['order_bys'] as $orderBy) {
4121 if ($fieldAlias == $orderBy['column'] &&
4122 CRM_Core_BAO_CustomField
::getKeyID($fieldAlias)
4129 return $this->isFieldFiltered($prop);
4134 * Check if the field is used as a filter.
4136 * @param string $prop
4140 protected function isFieldFiltered($prop) {
4141 if (!empty($prop['filters']) && $this->_customGroupFilters
) {
4142 foreach ($prop['filters'] as $fieldAlias => $val) {
4151 if (isset($this->_params
[$fieldAlias . '_' . $attach]) &&
4152 (!empty($this->_params
[$fieldAlias . '_' . $attach])
4153 ||
($attach != 'relative' &&
4154 $this->_params
[$fieldAlias . '_' . $attach] == '0')
4160 if (!empty($this->_params
[$fieldAlias . '_op']) &&
4161 in_array($this->_params
[$fieldAlias . '_op'], ['nll', 'nnll'])
4172 * Check for empty order_by configurations and remove them.
4174 * Also set template to hide them.
4176 * @param array $formValues
4178 public function preProcessOrderBy(&$formValues) {
4179 // Object to show/hide form elements
4180 $_showHide = new CRM_Core_ShowHideBlocks('', '');
4182 $_showHide->addShow('optionField_1');
4184 // Cycle through order_by options; skip any empty ones, and hide them as well
4187 if (!empty($formValues['order_bys'])) {
4188 foreach ($formValues['order_bys'] as $order_by) {
4189 if ($order_by['column'] && $order_by['column'] != '-') {
4190 $_showHide->addShow('optionField_' . $n);
4191 $orderBys[$n] = $order_by;
4196 for ($i = $n; $i <= 5; $i++
) {
4198 $_showHide->addHide('optionField_' . $i);
4202 // overwrite order_by options with modified values
4203 if (!empty($orderBys)) {
4204 $formValues['order_bys'] = $orderBys;
4207 $formValues['order_bys'] = [1 => ['column' => '-']];
4210 // assign show/hide data to template
4211 $_showHide->addToTemplate();
4215 * Check if table name has columns in SELECT clause.
4217 * @param string $tableName
4218 * Name of table (index of $this->_columns array).
4222 public function isTableSelected($tableName) {
4223 return in_array($tableName, $this->selectedTables());
4227 * Check if table name has columns in WHERE or HAVING clause.
4229 * @param string $tableName
4230 * Name of table (index of $this->_columns array).
4234 public function isTableFiltered($tableName) {
4235 // Cause the array to be generated if not previously done.
4236 if (!$this->_selectedTables
&& !$this->filteredTables
) {
4237 $this->selectedTables();
4239 return in_array($tableName, $this->filteredTables
);
4243 * Fetch array of DAO tables having columns included in SELECT or ORDER BY clause.
4245 * If the array is unset it will be built.
4250 public function selectedTables() {
4251 if (!$this->_selectedTables
) {
4252 $orderByColumns = [];
4253 if (array_key_exists('order_bys', $this->_params
) &&
4254 is_array($this->_params
['order_bys'])
4256 foreach ($this->_params
['order_bys'] as $orderBy) {
4257 $orderByColumns[] = $orderBy['column'];
4261 foreach ($this->_columns
as $tableName => $table) {
4262 if (array_key_exists('fields', $table)) {
4263 foreach ($table['fields'] as $fieldName => $field) {
4264 if (!empty($field['required']) ||
4265 !empty($this->_params
['fields'][$fieldName])
4267 $this->_selectedTables
[] = $tableName;
4272 if (array_key_exists('order_bys', $table)) {
4273 foreach ($table['order_bys'] as $orderByName => $orderBy) {
4274 if (in_array($orderByName, $orderByColumns)) {
4275 $this->_selectedTables
[] = $tableName;
4280 if (array_key_exists('filters', $table)) {
4281 foreach ($table['filters'] as $filterName => $filter) {
4282 if (!empty($this->_params
["{$filterName}_value"])
4283 ||
!empty($this->_params
["{$filterName}_relative"])
4284 || CRM_Utils_Array
::value("{$filterName}_op", $this->_params
) ==
4286 || CRM_Utils_Array
::value("{$filterName}_op", $this->_params
) ==
4289 $this->_selectedTables
[] = $tableName;
4290 $this->filteredTables
[] = $tableName;
4297 return $this->_selectedTables
;
4301 * Add campaign fields.
4302 * @param string $entityTable
4303 * @param bool $groupBy
4304 * Add GroupBy? Not appropriate for detail report.
4305 * @param bool $orderBy
4306 * Add OrderBy? Not appropriate for detail report.
4307 * @param bool $filters
4310 public function addCampaignFields($entityTable = 'civicrm_contribution', $groupBy = FALSE, $orderBy = FALSE, $filters = TRUE) {
4311 // Check if CiviCampaign is a) enabled and b) has active campaigns
4312 $config = CRM_Core_Config
::singleton();
4313 $campaignEnabled = in_array('CiviCampaign', $config->enableComponents
);
4314 if ($campaignEnabled) {
4315 $getCampaigns = CRM_Campaign_BAO_Campaign
::getPermissionedCampaigns(NULL, NULL, FALSE, FALSE, TRUE);
4316 // If we have a campaign, build out the relevant elements
4317 if (!empty($getCampaigns['campaigns'])) {
4318 $this->campaigns
= $getCampaigns['campaigns'];
4319 asort($this->campaigns
);
4320 $this->_columns
[$entityTable]['fields']['campaign_id'] = ['title' => ts('Campaign'), 'default' => 'false'];
4322 $this->_columns
[$entityTable]['filters']['campaign_id'] = [
4323 'title' => ts('Campaign'),
4324 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
4325 'options' => $this->campaigns
,
4326 'type' => CRM_Utils_Type
::T_INT
,
4331 $this->_columns
[$entityTable]['group_bys']['campaign_id'] = ['title' => ts('Campaign')];
4335 $this->_columns
[$entityTable]['order_bys']['campaign_id'] = ['title' => ts('Campaign')];
4342 * Add address fields.
4344 * @deprecated - use getAddressColumns which is a more accurate description
4345 * and also accepts an array of options rather than a long list
4347 * adding address fields to construct function in reports
4349 * @param bool $groupBy
4350 * Add GroupBy? Not appropriate for detail report.
4351 * @param bool $orderBy
4352 * Add GroupBy? Not appropriate for detail report.
4353 * @param bool $filters
4354 * @param array $defaults
4357 * address fields for construct clause
4359 public function addAddressFields($groupBy = TRUE, $orderBy = FALSE, $filters = TRUE, $defaults = ['country_id' => TRUE]) {
4360 $defaultAddressFields = [
4361 'street_address' => ts('Street Address'),
4362 'supplemental_address_1' => ts('Supplementary Address Field 1'),
4363 'supplemental_address_2' => ts('Supplementary Address Field 2'),
4364 'supplemental_address_3' => ts('Supplementary Address Field 3'),
4365 'street_number' => ts('Street Number'),
4366 'street_name' => ts('Street Name'),
4367 'street_unit' => ts('Street Unit'),
4368 'city' => ts('City'),
4369 'postal_code' => ts('Postal Code'),
4370 'postal_code_suffix' => ts('Postal Code Suffix'),
4371 'country_id' => ts('Country'),
4372 'state_province_id' => ts('State/Province'),
4373 'county_id' => ts('County'),
4376 'civicrm_address' => [
4377 'dao' => 'CRM_Core_DAO_Address',
4380 'title' => ts('Address Name'),
4381 'default' => CRM_Utils_Array
::value('name', $defaults, FALSE),
4385 'grouping' => 'location-fields',
4388 foreach ($defaultAddressFields as $fieldName => $fieldLabel) {
4389 $addressFields['civicrm_address']['fields'][$fieldName] = [
4390 'title' => $fieldLabel,
4391 'default' => CRM_Utils_Array
::value($fieldName, $defaults, FALSE),
4395 $street_address_filters = $general_address_filters = [];
4397 // Address filter depends on whether street address parsing is enabled.
4399 $addressOptions = CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
4402 if ($addressOptions['street_address_parsing']) {
4403 $street_address_filters = [
4404 'street_number' => [
4405 'title' => ts('Street Number'),
4406 'type' => CRM_Utils_Type
::T_INT
,
4407 'name' => 'street_number',
4410 'title' => ts('Street Name'),
4411 'name' => 'street_name',
4412 'type' => CRM_Utils_Type
::T_STRING
,
4417 $street_address_filters = [
4418 'street_address' => [
4419 'title' => ts('Street Address'),
4420 'type' => CRM_Utils_Type
::T_STRING
,
4421 'name' => 'street_address',
4425 $general_address_filters = [
4427 'title' => ts('Postal Code'),
4428 'type' => CRM_Utils_Type
::T_STRING
,
4429 'name' => 'postal_code',
4432 'title' => ts('City'),
4433 'type' => CRM_Utils_Type
::T_STRING
,
4437 'name' => 'country_id',
4438 'title' => ts('Country'),
4439 'type' => CRM_Utils_Type
::T_INT
,
4440 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
4441 'options' => CRM_Core_PseudoConstant
::country(),
4443 'state_province_id' => [
4444 'name' => 'state_province_id',
4445 'title' => ts('State/Province'),
4446 'type' => CRM_Utils_Type
::T_INT
,
4447 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
4451 'name' => 'county_id',
4452 'title' => ts('County'),
4453 'type' => CRM_Utils_Type
::T_INT
,
4454 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
4459 $addressFields['civicrm_address']['filters'] = array_merge(
4460 $street_address_filters,
4461 $general_address_filters);
4464 $addressFields['civicrm_address']['order_bys'] = [
4465 'street_name' => ['title' => ts('Street Name')],
4466 'street_number' => ['title' => ts('Odd / Even Street Number')],
4467 'street_address' => NULL,
4469 'postal_code' => NULL,
4474 $addressFields['civicrm_address']['group_bys'] = [
4475 'street_address' => NULL,
4477 'postal_code' => NULL,
4478 'state_province_id' => [
4479 'title' => ts('State/Province'),
4482 'title' => ts('Country'),
4485 'title' => ts('County'),
4489 return $addressFields;
4493 * Do AlterDisplay processing on Address Fields.
4494 * If there are multiple address field values then
4495 * on basis of provided separator the code values are translated into respective labels
4498 * @param array $rows
4499 * @param int $rowNum
4500 * @param string $baseUrl
4501 * @param string $linkText
4502 * @param string $separator
4506 public function alterDisplayAddressFields(&$row, &$rows, &$rowNum, $baseUrl, $linkText, $separator = ',') {
4507 $criteriaQueryParams = CRM_Report_Utils_Report
::getPreviewCriteriaQueryParams($this->_defaults
, $this->_params
);
4508 $entryFound = FALSE;
4510 'civicrm_address_country_id' => 'country',
4511 'civicrm_address_county_id' => 'county',
4512 'civicrm_address_state_province_id' => 'stateProvince',
4514 foreach ($columnMap as $fieldName => $fnName) {
4515 if (array_key_exists($fieldName, $row)) {
4516 if ($values = $row[$fieldName]) {
4517 $values = (array) explode($separator, $values);
4518 $rows[$rowNum][$fieldName] = [];
4519 $addressField = $fnName == 'stateProvince' ?
'state' : $fnName;
4520 foreach ($values as $value) {
4521 $rows[$rowNum][$fieldName][] = CRM_Core_PseudoConstant
::$fnName($value);
4523 $rows[$rowNum][$fieldName] = implode($separator, $rows[$rowNum][$fieldName]);
4525 $url = CRM_Report_Utils_Report
::getNextUrl($baseUrl,
4526 sprintf("reset=1&force=1&%s&%s_op=in&%s_value=%s",
4527 $criteriaQueryParams,
4528 str_replace('civicrm_address_', '', $fieldName),
4529 str_replace('civicrm_address_', '', $fieldName),
4530 implode(',', $values)
4531 ), $this->_absoluteUrl
, $this->_id
4533 $rows[$rowNum]["{$fieldName}_link"] = $url;
4534 $rows[$rowNum]["{$fieldName}_hover"] = ts("%1 for this %2.", [1 => $linkText, 2 => $addressField]);
4545 * Do AlterDisplay processing on Address Fields.
4548 * @param array $rows
4549 * @param int $rowNum
4550 * @param string $baseUrl
4551 * @param string $linkText
4555 public function alterDisplayContactFields(&$row, &$rows, &$rowNum, $baseUrl, $linkText) {
4556 $entryFound = FALSE;
4557 // There is no reason not to add links for all fields but it seems a bit odd to be able to click on
4558 // 'Mrs'. Also, we don't have metadata about the title. So, add selectively to addLinks.
4559 $addLinks = ['gender_id' => 'Gender'];
4560 foreach (['prefix_id', 'suffix_id', 'gender_id', 'contact_sub_type', 'preferred_language'] as $fieldName) {
4561 if (array_key_exists('civicrm_contact_' . $fieldName, $row)) {
4562 if (($value = $row['civicrm_contact_' . $fieldName]) != FALSE) {
4563 $rowValues = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $value);
4565 foreach ($rowValues as $rowValue) {
4567 $rowLabels[] = CRM_Core_PseudoConstant
::getLabel('CRM_Contact_BAO_Contact', $fieldName, $rowValue);
4570 $rows[$rowNum]['civicrm_contact_' . $fieldName] = implode(', ', $rowLabels);
4571 if ($baseUrl && ($title = CRM_Utils_Array
::value($fieldName, $addLinks)) != FALSE) {
4572 $this->addLinkToRow($rows[$rowNum], $baseUrl, $linkText, $value, $fieldName, 'civicrm_contact', $title);
4579 'do_not_email', 'is_deceased', 'do_not_phone', 'do_not_sms', 'do_not_mail', 'is_opt_out',
4581 foreach ($yesNoFields as $fieldName) {
4582 if (array_key_exists('civicrm_contact_' . $fieldName, $row)) {
4583 // Since these are essentially 'negative fields' it feels like it
4584 // makes sense to only highlight the exceptions hence no 'No'.
4585 $rows[$rowNum]['civicrm_contact_' . $fieldName] = !empty($rows[$rowNum]['civicrm_contact_' . $fieldName]) ?
ts('Yes') : '';
4590 // Handle employer id
4591 if (array_key_exists('civicrm_contact_employer_id', $row)) {
4592 $employerId = $row['civicrm_contact_employer_id'];
4594 $rows[$rowNum]['civicrm_contact_employer_id'] = CRM_Contact_BAO_Contact
::displayName($employerId);
4595 $rows[$rowNum]['civicrm_contact_employer_id_link'] = CRM_Utils_System
::url('civicrm/contact/view', 'reset=1&cid=' . $employerId, $this->_absoluteUrl
);
4596 $rows[$rowNum]['civicrm_contact_employer_id_hover'] = ts('View Contact Summary for Employer.');
4605 * Adjusts dates passed in to YEAR() for fiscal year.
4607 * @param string $fieldName
4611 public function fiscalYearOffset($fieldName) {
4612 $config = CRM_Core_Config
::singleton();
4613 $fy = $config->fiscalYearStart
;
4614 if (CRM_Utils_Array
::value('yid_op', $this->_params
) == 'calendar' ||
4615 ($fy['d'] == 1 && $fy['M'] == 1)
4617 return "YEAR( $fieldName )";
4619 return "YEAR( $fieldName - INTERVAL " . ($fy['M'] - 1) . " MONTH" .
4620 ($fy['d'] > 1 ?
(" - INTERVAL " . ($fy['d'] - 1) . " DAY") : '') . " )";
4624 * Add Address into From Table if required.
4626 * @deprecated use joinAddressFromContact
4627 * (left here in case extensions use it).
4629 public function addAddressFromClause() {
4630 CRM_Core_Error
::deprecatedFunctionWarning('CRM_Report_Form::joinAddressFromContact');
4631 // include address field if address column is to be included
4632 if ((isset($this->_addressField
) &&
4633 $this->_addressField
4635 $this->isTableSelected('civicrm_address')
4638 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
4639 ON ({$this->_aliases['civicrm_contact']}.id =
4640 {$this->_aliases['civicrm_address']}.contact_id) AND
4641 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
4646 * Add Phone into From Table if required.
4648 * @deprecated use joinPhoneFromContact
4649 * (left here in case extensions use it).
4651 public function addPhoneFromClause() {
4652 CRM_Core_Error
::deprecatedFunctionWarning('CRM_Report_Form::joinPhoneFromContact');
4653 // include address field if address column is to be included
4654 if ($this->isTableSelected('civicrm_phone')) {
4656 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
4657 ON ({$this->_aliases['civicrm_contact']}.id =
4658 {$this->_aliases['civicrm_phone']}.contact_id) AND
4659 {$this->_aliases['civicrm_phone']}.is_primary = 1\n";
4664 * Add Address into From Table if required.
4666 * Prefix will be added to both tables as
4667 * it is assumed you are using it to get address of a secondary contact.
4669 * @param string $prefix
4670 * @param array $extra Additional options.
4671 * Not currently used in core but may be used in override extensions.
4673 protected function joinAddressFromContact($prefix = '', $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) AND
4686 {$this->_aliases[$prefix . 'civicrm_address']}.is_primary = 1\n";
4691 * Add Country into From Table if required.
4693 * Prefix will be added to both tables as
4694 * it is assumed you are using it to get address of a secondary contact.
4696 * @param string $prefix
4697 * @param array $extra Additional options.
4698 * Not currently used in core but may be used in override extensions.
4700 protected function joinCountryFromAddress($prefix = '', $extra = []) {
4701 // include country field if country column is to be included
4702 if ($this->isTableSelected($prefix . 'civicrm_country') ||
$this->isTableSelected($prefix . 'civicrm_worldregion')) {
4703 if (empty($this->_aliases
[$prefix . 'civicrm_country'])) {
4704 $this->_aliases
[$prefix . 'civicrm_country'] = $prefix . '_report_country';
4707 LEFT JOIN civicrm_country {$this->_aliases[$prefix . 'civicrm_country']}
4708 ON {$this->_aliases[$prefix . 'civicrm_address']}.country_id = {$this->_aliases[$prefix . 'civicrm_country']}.id AND
4709 {$this->_aliases[$prefix . 'civicrm_address']}.is_primary = 1 ";
4714 * Add Phone into From Table if required.
4716 * Prefix will be added to both tables as
4717 * it is assumed you are using it to get address of a secondary contact.
4719 * @param string $prefix
4720 * @param array $extra Additional options.
4721 * Not currently used in core but may be used in override extensions.
4723 protected function joinPhoneFromContact($prefix = '', $extra = []) {
4724 // include phone field if phone column is to be included
4725 if ($this->isTableSelected($prefix . 'civicrm_phone')) {
4727 LEFT JOIN civicrm_phone {$this->_aliases[$prefix . 'civicrm_phone']}
4728 ON {$this->_aliases[$prefix . 'civicrm_contact']}.id = {$this->_aliases[$prefix . 'civicrm_phone']}.contact_id AND
4729 {$this->_aliases[$prefix . 'civicrm_phone']}.is_primary = 1\n";
4734 * Add Email into From Table if required.
4736 * Prefix will be added to both tables as
4737 * it is assumed you are using it to get address of a secondary contact.
4739 * @param string $prefix
4740 * @param array $extra Additional options.
4741 * Not currently used in core but may be used in override extensions.
4743 protected function joinEmailFromContact($prefix = '', $extra = []) {
4744 // include email field if email column is to be included
4745 if ($this->isTableSelected($prefix . 'civicrm_email')) {
4747 LEFT JOIN civicrm_email {$this->_aliases[$prefix . 'civicrm_email']}
4748 ON ({$this->_aliases[$prefix . 'civicrm_contact']}.id = {$this->_aliases[$prefix . 'civicrm_email']}.contact_id AND
4749 {$this->_aliases[$prefix . 'civicrm_email']}.is_primary = 1) ";
4754 * Add Financial Transaction into From Table if required.
4756 public function addFinancialTrxnFromClause() {
4757 if ($this->isTableSelected('civicrm_financial_trxn')) {
4759 LEFT JOIN civicrm_entity_financial_trxn eftcc
4760 ON ({$this->_aliases['civicrm_contribution']}.id = eftcc.entity_id AND
4761 eftcc.entity_table = 'civicrm_contribution')
4762 LEFT JOIN civicrm_financial_trxn {$this->_aliases['civicrm_financial_trxn']}
4763 ON {$this->_aliases['civicrm_financial_trxn']}.id = eftcc.financial_trxn_id \n";
4768 * Get phone columns to add to array.
4770 * @param array $options
4771 * - prefix Prefix to add to table (in case of more than one instance of the table)
4772 * - prefix_label Label to give columns from this phone table instance
4775 * phone columns definition
4777 public function getPhoneColumns($options = []) {
4780 'prefix_label' => '',
4783 $options = array_merge($defaultOptions, $options);
4786 $options['prefix'] . 'civicrm_phone' => [
4787 'dao' => 'CRM_Core_DAO_Phone',
4789 $options['prefix'] . 'phone' => [
4790 'title' => $options['prefix_label'] . ts('Phone'),
4800 * Get a standard set of contact fields.
4801 * @deprecated - use getColumns('Contact') instead
4804 public function getBasicContactFields() {
4807 'title' => ts('Contact Name'),
4812 'no_display' => TRUE,
4816 'title' => ts('Contact Prefix'),
4819 'title' => ts('First Name'),
4822 'title' => ts('Nick Name'),
4825 'title' => ts('Middle Name'),
4828 'title' => ts('Last Name'),
4831 'title' => ts('Contact Suffix'),
4833 'postal_greeting_display' => ['title' => ts('Postal Greeting')],
4834 'email_greeting_display' => ['title' => ts('Email Greeting')],
4835 'addressee_display' => ['title' => ts('Addressee')],
4837 'title' => ts('Contact Type'),
4839 'contact_sub_type' => [
4840 'title' => ts('Contact Subtype'),
4843 'title' => ts('Gender'),
4846 'title' => ts('Birth Date'),
4849 'title' => ts('Age'),
4850 'dbAlias' => 'TIMESTAMPDIFF(YEAR, contact_civireport.birth_date, CURDATE())',
4853 'title' => ts('Contact Job title'),
4855 'organization_name' => [
4856 'title' => ts('Organization Name'),
4858 'external_identifier' => [
4859 'title' => ts('Contact identifier from external system'),
4861 'do_not_email' => [],
4862 'do_not_phone' => [],
4863 'do_not_mail' => [],
4866 'is_deceased' => [],
4867 'preferred_language' => [],
4869 'title' => ts('Current Employer'),
4875 * Get a standard set of contact filters.
4877 * @param array $defaults
4881 public function getBasicContactFilters($defaults = []) {
4884 'title' => ts('Contact Name'),
4887 'title' => ts('Contact Source'),
4888 'type' => CRM_Utils_Type
::T_STRING
,
4891 'title' => ts('Contact ID'),
4892 'no_display' => TRUE,
4895 'title' => ts('Gender'),
4896 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
4897 'options' => CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id'),
4900 'title' => ts('Birth Date'),
4901 'operatorType' => CRM_Report_Form
::OP_DATE
,
4904 'title' => ts('Contact Type'),
4906 'contact_sub_type' => [
4907 'title' => ts('Contact Subtype'),
4909 'modified_date' => [
4910 'title' => ts('Contact Modified'),
4911 'operatorType' => CRM_Report_Form
::OP_DATE
,
4912 'type' => CRM_Utils_Type
::T_DATE
,
4915 'title' => ts('Deceased'),
4916 'type' => CRM_Utils_Type
::T_BOOLEAN
,
4917 'default' => CRM_Utils_Array
::value('deceased', $defaults, 0),
4920 'title' => ts('Do not email'),
4921 'type' => CRM_Utils_Type
::T_BOOLEAN
,
4924 'title' => ts('Do not phone'),
4925 'type' => CRM_Utils_Type
::T_BOOLEAN
,
4928 'title' => ts('Do not mail'),
4929 'type' => CRM_Utils_Type
::T_BOOLEAN
,
4932 'title' => ts('Do not SMS'),
4933 'type' => CRM_Utils_Type
::T_BOOLEAN
,
4936 'title' => ts('Do not bulk email'),
4937 'type' => CRM_Utils_Type
::T_BOOLEAN
,
4939 'preferred_language' => [
4940 'title' => ts('Preferred Language'),
4943 'no_display' => TRUE,
4945 'type' => CRM_Utils_Type
::T_BOOLEAN
,
4951 * Add contact to group.
4953 * @param int $groupID
4955 public function add2group($groupID) {
4956 if (is_numeric($groupID) && isset($this->_aliases
['civicrm_contact'])) {
4957 $select = "SELECT DISTINCT {$this->_aliases['civicrm_contact']}.id AS addtogroup_contact_id, ";
4958 $select = preg_replace('/SELECT(\s+SQL_CALC_FOUND_ROWS)?\s+/i', $select, $this->_select
);
4959 $sql = "{$select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy}";
4960 $sql = str_replace('WITH ROLLUP', '', $sql);
4961 $dao = CRM_Core_DAO
::executeQuery($sql);
4964 // Add resulting contacts to group
4965 while ($dao->fetch()) {
4966 if ($dao->addtogroup_contact_id
) {
4967 $contact_ids[$dao->addtogroup_contact_id
] = $dao->addtogroup_contact_id
;
4971 if (!empty($contact_ids)) {
4972 CRM_Contact_BAO_GroupContact
::addContactsToGroup($contact_ids, $groupID);
4973 CRM_Core_Session
::setStatus(ts("Listed contact(s) have been added to the selected group."), ts('Contacts Added'), 'success');
4976 CRM_Core_Session
::setStatus(ts("The listed records(s) cannot be added to the group."));
4982 * Show charts on print screen.
4984 public static function uploadChartImage() {
4985 // upload strictly for '.png' images
4986 $name = trim(basename(CRM_Utils_Request
::retrieve('name', 'String', CRM_Core_DAO
::$_nullObject, FALSE, NULL, 'GET')));
4987 if (preg_match('/\.png$/', $name)) {
4989 // Get the RAW .png from the input.
4990 $httpRawPostData = file_get_contents("php://input");
4992 // prepare the directory
4993 $config = CRM_Core_Config
::singleton();
4995 = str_replace('/persist/contribute/', '/persist/', $config->imageUploadDir
) .
4997 if (!file_exists($defaultPath)) {
4998 mkdir($defaultPath, 0777, TRUE);
5001 // full path to the saved image including filename
5002 $destination = $defaultPath . $name;
5005 $jfh = fopen($destination, 'w') or die("can't open file");
5006 fwrite($jfh, $httpRawPostData);
5008 CRM_Utils_System
::civiExit();
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'] = CRM_Utils_Array
::value('title', $field);
5295 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
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,
5459 'postal_greeting_display' => 'Postal Greeting',
5460 'email_greeting_display' => 'Email Greeting',
5461 'addressee_display' => 'Addressee',
5462 ] as $field => $title) {
5463 $spec[$options['prefix'] . $field] = [
5464 'title' => $options['prefix_label'] . ts($title),
5466 'is_fields' => TRUE,
5467 'is_filters' => FALSE,
5468 'is_group_bys' => FALSE,
5471 foreach (['do_not_email', 'do_not_phone', 'do_not_mail', 'do_not_sms', 'is_opt_out'] as $field) {
5472 $spec[$options['prefix'] . $field] = [
5474 'type' => CRM_Utils_Type
::T_BOOLEAN
,
5475 'is_fields' => TRUE,
5476 'is_filters' => TRUE,
5477 'is_group_bys' => FALSE,
5480 $individualFields = [
5481 $options['prefix'] . 'first_name' => [
5482 'name' => 'first_name',
5483 'title' => $options['prefix_label'] . ts('First Name'),
5484 'is_fields' => TRUE,
5485 'is_filters' => TRUE,
5486 'is_order_bys' => TRUE,
5488 $options['prefix'] . 'middle_name' => [
5489 'name' => 'middle_name',
5490 'title' => $options['prefix_label'] . ts('Middle Name'),
5491 'is_fields' => TRUE,
5493 $options['prefix'] . 'last_name' => [
5494 'name' => 'last_name',
5495 'title' => $options['prefix_label'] . ts('Last Name'),
5496 'default_order' => 'ASC',
5497 'is_fields' => TRUE,
5499 $options['prefix'] . 'nick_name' => [
5500 'name' => 'nick_name',
5501 'title' => $options['prefix_label'] . ts('Nick Name'),
5502 'is_fields' => TRUE,
5504 $options['prefix'] . 'prefix_id' => [
5505 'name' => 'prefix_id',
5506 'title' => $options['prefix_label'] . ts('Prefix'),
5507 'options' => CRM_Contact_BAO_Contact
::buildOptions('prefix_id'),
5508 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
5509 'is_fields' => TRUE,
5510 'is_filters' => TRUE,
5512 $options['prefix'] . 'suffix_id' => [
5513 'name' => 'suffix_id',
5514 'title' => $options['prefix_label'] . ts('Suffix'),
5515 'options' => CRM_Contact_BAO_Contact
::buildOptions('suffix_id'),
5516 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
5517 'is_fields' => TRUE,
5518 'is_filters' => TRUE,
5520 $options['prefix'] . 'gender_id' => [
5521 'name' => 'gender_id',
5522 'title' => $options['prefix_label'] . ts('Gender'),
5523 'options' => CRM_Contact_BAO_Contact
::buildOptions('gender_id'),
5524 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
5525 'is_fields' => TRUE,
5526 'is_filters' => TRUE,
5529 'title' => $options['prefix_label'] . ts('Birth Date'),
5530 'operatorType' => CRM_Report_Form
::OP_DATE
,
5531 'type' => CRM_Utils_Type
::T_DATE
,
5532 'is_fields' => TRUE,
5533 'is_filters' => TRUE,
5536 'title' => $options['prefix_label'] . ts('Age'),
5537 'dbAlias' => 'TIMESTAMPDIFF(YEAR, ' . $tableAlias . '_civireport.birth_date, CURDATE())',
5538 'type' => CRM_Utils_Type
::T_INT
,
5539 'is_fields' => TRUE,
5541 $options['prefix'] . 'is_deceased' => [
5542 'title' => $options['prefix_label'] . ts('Is deceased'),
5543 'name' => 'is_deceased',
5544 'type' => CRM_Utils_Type
::T_BOOLEAN
,
5545 'is_fields' => FALSE,
5546 'is_filters' => TRUE,
5547 'is_group_bys' => FALSE,
5549 $options['prefix'] . 'job_title' => [
5550 'name' => 'job_title',
5551 'is_fields' => TRUE,
5552 'is_filters' => FALSE,
5553 'is_group_bys' => FALSE,
5555 $options['prefix'] . 'employer_id' => [
5556 'title' => $options['prefix_label'] . ts('Current Employer'),
5557 'type' => CRM_Utils_Type
::T_INT
,
5558 'name' => 'employer_id',
5559 'is_fields' => TRUE,
5560 'is_filters' => FALSE,
5561 'is_group_bys' => TRUE,
5564 if (!$options['contact_type'] ||
$options['contact_type'] === 'Individual') {
5565 $spec = array_merge($spec, $individualFields);
5568 if (!empty($options['custom_fields'])) {
5569 $this->_customGroupExtended
[$options['prefix'] . 'civicrm_contact'] = [
5570 'extends' => $options['custom_fields'],
5571 'title' => $options['prefix_label'],
5572 'filters' => $options['filters'],
5573 'prefix' => $options['prefix'],
5574 'prefix_label' => $options['prefix_label'],
5578 return $this->buildColumns($spec, $options['prefix'] . 'civicrm_contact', 'CRM_Contact_DAO_Contact', $tableAlias, $this->getDefaultsFromOptions($options), $options);
5582 * Get address columns to add to array.
5584 * @param array $options
5585 * - prefix Prefix to add to table (in case of more than one instance of the table)
5586 * - prefix_label Label to give columns from this address table instance
5587 * - group_bys enable these fields for group by - default false
5588 * - order_bys enable these fields for order by
5589 * - filters enable these fields for filtering
5591 * @return array address columns definition
5593 protected function getAddressColumns($options = []) {
5596 'prefix_label' => '',
5598 'group_bys' => FALSE,
5599 'order_bys' => TRUE,
5601 'join_filters' => FALSE,
5602 'fields_defaults' => [],
5603 'filters_defaults' => [],
5604 'group_bys_defaults' => [],
5605 'order_bys_defaults' => [],
5608 $options = array_merge($defaultOptions, $options);
5609 $defaults = $this->getDefaultsFromOptions($options);
5610 $tableAlias = $options['prefix'] . 'address';
5613 $options['prefix'] . 'name' => [
5614 'title' => ts($options['prefix_label'] . 'Address Name'),
5616 'is_fields' => TRUE,
5618 $options['prefix'] . 'street_number' => [
5619 'name' => 'street_number',
5620 'title' => ts($options['prefix_label'] . 'Street Number'),
5622 'is_fields' => TRUE,
5624 $options['prefix'] . 'odd_street_number' => [
5625 'title' => ts('Odd / Even Street Number'),
5626 'name' => 'odd_street_number',
5627 'type' => CRM_Utils_Type
::T_INT
,
5628 'no_display' => TRUE,
5630 'dbAlias' => '(address_civireport.street_number % 2)',
5631 'is_fields' => TRUE,
5632 'is_order_bys' => TRUE,
5634 $options['prefix'] . 'street_name' => [
5635 'name' => 'street_name',
5636 'title' => ts($options['prefix_label'] . 'Street Name'),
5638 'is_fields' => TRUE,
5639 'is_filters' => TRUE,
5640 'operator' => 'like',
5641 'is_order_bys' => TRUE,
5643 $options['prefix'] . 'street_address' => [
5644 'title' => ts($options['prefix_label'] . 'Street Address'),
5645 'name' => 'street_address',
5646 'is_fields' => TRUE,
5647 'is_filters' => TRUE,
5648 'is_group_bys' => TRUE,
5650 $options['prefix'] . 'supplemental_address_1' => [
5651 'title' => ts($options['prefix_label'] . 'Supplementary Address Field 1'),
5652 'name' => 'supplemental_address_1',
5653 'is_fields' => TRUE,
5655 $options['prefix'] . 'supplemental_address_2' => [
5656 'title' => ts($options['prefix_label'] . 'Supplementary Address Field 2'),
5657 'name' => 'supplemental_address_2',
5658 'is_fields' => TRUE,
5660 $options['prefix'] . 'supplemental_address_3' => [
5661 'title' => ts($options['prefix_label'] . 'Supplementary Address Field 3'),
5662 'name' => 'supplemental_address_3',
5663 'is_fields' => TRUE,
5665 $options['prefix'] . 'street_number' => [
5666 'name' => 'street_number',
5667 'title' => ts($options['prefix_label'] . 'Street Number'),
5669 'is_order_bys' => TRUE,
5670 'is_filters' => TRUE,
5671 'is_fields' => TRUE,
5673 $options['prefix'] . 'street_unit' => [
5674 'name' => 'street_unit',
5675 'title' => ts($options['prefix_label'] . 'Street Unit'),
5677 'is_fields' => TRUE,
5679 $options['prefix'] . 'city' => [
5680 'title' => ts($options['prefix_label'] . 'City'),
5682 'operator' => 'like',
5683 'is_fields' => TRUE,
5684 'is_filters' => TRUE,
5685 'is_group_bys' => TRUE,
5686 'is_order_bys' => TRUE,
5688 $options['prefix'] . 'postal_code' => [
5689 'title' => ts($options['prefix_label'] . 'Postal Code'),
5690 'name' => 'postal_code',
5692 'is_fields' => TRUE,
5693 'is_filters' => TRUE,
5694 'is_group_bys' => TRUE,
5695 'is_order_bys' => TRUE,
5697 $options['prefix'] . 'postal_code_suffix' => [
5698 'title' => ts($options['prefix_label'] . 'Postal Code Suffix'),
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'] . 'county_id' => [
5707 'title' => ts($options['prefix_label'] . 'County'),
5708 'alter_display' => 'alterCountyID',
5709 'name' => 'county_id',
5710 'type' => CRM_Utils_Type
::T_INT
,
5711 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
5712 'options' => CRM_Core_PseudoConstant
::county(),
5713 'is_fields' => TRUE,
5714 'is_filters' => TRUE,
5715 'is_group_bys' => TRUE,
5717 $options['prefix'] . 'state_province_id' => [
5718 'title' => ts($options['prefix_label'] . 'State/Province'),
5719 'alter_display' => 'alterStateProvinceID',
5720 'name' => 'state_province_id',
5721 'type' => CRM_Utils_Type
::T_INT
,
5722 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
5723 'options' => CRM_Core_PseudoConstant
::stateProvince(),
5724 'is_fields' => TRUE,
5725 'is_filters' => TRUE,
5726 'is_group_bys' => TRUE,
5728 $options['prefix'] . 'country_id' => [
5729 'title' => ts($options['prefix_label'] . 'Country'),
5730 'alter_display' => 'alterCountryID',
5731 'name' => 'country_id',
5732 'is_fields' => TRUE,
5733 'is_filters' => TRUE,
5734 'is_group_bys' => TRUE,
5735 'type' => CRM_Utils_Type
::T_INT
,
5736 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
5737 'options' => CRM_Core_PseudoConstant
::country(),
5739 $options['prefix'] . 'location_type_id' => [
5740 'name' => 'is_primary',
5741 'title' => ts($options['prefix_label'] . 'Location Type'),
5742 'type' => CRM_Utils_Type
::T_INT
,
5743 'is_fields' => TRUE,
5744 'alter_display' => 'alterLocationTypeID',
5746 $options['prefix'] . 'id' => [
5747 'title' => ts($options['prefix_label'] . 'ID'),
5749 'is_fields' => TRUE,
5751 $options['prefix'] . 'is_primary' => [
5752 'name' => 'is_primary',
5753 'title' => ts($options['prefix_label'] . 'Primary Address?'),
5754 'type' => CRM_Utils_Type
::T_BOOLEAN
,
5755 'is_fields' => TRUE,
5758 return $this->buildColumns($spec, $options['prefix'] . 'civicrm_address', 'CRM_Core_DAO_Address', $tableAlias, $defaults, $options);
5762 * Build the columns.
5764 * The normal report class needs you to remember to do a few things that are often erratic
5766 * 1) use a unique key for any field that might not be unique (e.g. start date, label)
5767 * - this class will prepend an alias to the key & set the 'name' if you don't set it yourself.
5768 * You can suppress the alias with 'no_field_disambiguation' if transitioning existing reports. This
5769 * means any saved filters / fields on saved report instances. This will mean that matching names from
5770 * different tables may be ambigious, but it will smooth any code transition.
5771 * - note that it assumes the value being passed in is the actual table field name
5773 * 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
5774 * contexts - the code looks up the fields array for data - so it both defines the field spec & the fields you want to show
5776 * 3) this function also sets the 'metadata' array - the extended report class now uses this in place
5777 * of the fields array to reduce the issues caused when metadata is needed but 'fields' are not defined. Code in
5778 * the core classes can start to move towards that.
5780 * @param array $specs
5781 * @param string $tableName
5782 * @param string $daoName
5783 * @param string $tableAlias
5784 * @param array $defaults
5785 * @param array $options
5789 protected function buildColumns($specs, $tableName, $daoName = NULL, $tableAlias = NULL, $defaults = [], $options = []) {
5791 $tableAlias = str_replace('civicrm_', '', $tableName);
5793 $types = ['filters', 'group_bys', 'order_bys', 'join_filters'];
5794 $columns = [$tableName => array_fill_keys($types, [])];
5795 // The code that uses this no longer cares if it is a DAO or BAO so just call it a DAO.
5796 $columns[$tableName]['dao'] = $daoName;
5797 $columns[$tableName]['alias'] = $tableAlias;
5799 foreach ($specs as $specName => $spec) {
5800 if (empty($spec['name'])) {
5801 $spec['name'] = $specName;
5804 $fieldAlias = (empty($options['no_field_disambiguation']) ?
$tableAlias . '_' : '') . $specName;
5805 $columns[$tableName]['metadata'][$fieldAlias] = $spec;
5806 $columns[$tableName]['fields'][$fieldAlias] = $spec;
5807 if (isset($defaults['fields_defaults']) && in_array($spec['name'], $defaults['fields_defaults'])) {
5808 $columns[$tableName]['fields'][$fieldAlias]['default'] = TRUE;
5811 if (!$spec['is_fields'] ||
(isset($options['fields_excluded']) && in_array($specName, $options['fields_excluded']))) {
5812 $columns[$tableName]['fields'][$fieldAlias]['no_display'] = TRUE;
5815 if (isset($options['fields_required']) && in_array($specName, $options['fields_required'])) {
5816 $columns[$tableName]['fields'][$fieldAlias]['required'] = TRUE;
5819 foreach ($types as $type) {
5820 if ($options[$type] && !empty($spec['is_' . $type])) {
5821 $columns[$tableName][$type][$fieldAlias] = $spec;
5822 if (isset($defaults[$type . '_defaults']) && isset($defaults[$type . '_defaults'][$spec['name']])) {
5823 $columns[$tableName][$type][$fieldAlias]['default'] = $defaults[$type . '_defaults'][$spec['name']];
5832 * Store group bys into array - so we can check elsewhere what is grouped.
5834 protected function storeGroupByArray() {
5836 if (!CRM_Utils_Array
::value('group_bys', $this->_params
)
5837 ||
!is_array($this->_params
['group_bys'])) {
5838 $this->_params
['group_bys'] = [];
5841 foreach ($this->_columns
as $tableName => $table) {
5842 $table = $this->_columns
[$tableName];
5843 if (array_key_exists('group_bys', $table)) {
5844 foreach ($table['group_bys'] as $fieldName => $fieldData) {
5845 $field = $this->_columns
[$tableName]['metadata'][$fieldName];
5846 if (!empty($this->_params
['group_bys'][$fieldName]) ||
!empty($fieldData['required'])) {
5847 if (!empty($field['chart'])) {
5848 $this->assign('chartSupported', TRUE);
5851 if (!empty($table['group_bys'][$fieldName]['frequency']) &&
5852 !empty($this->_params
['group_bys_freq'][$fieldName])
5855 switch ($this->_params
['group_bys_freq'][$fieldName]) {
5857 $this->_groupByArray
[$tableName . '_' . $fieldName . '_start'] = self
::fiscalYearOffset($field['dbAlias']);
5860 $this->_groupByArray
[$tableName . '_' . $fieldName . '_start'] = " {$this->_params['group_bys_freq'][$fieldName]}({$field['dbAlias']})";
5863 $this->_groupByArray
[$tableName . '_' . $fieldName . '_start'] = "EXTRACT(YEAR_{$this->_params['group_bys_freq'][$fieldName]} FROM {$field['dbAlias']})";
5868 if (!in_array($field['dbAlias'], $this->_groupByArray
)) {
5869 $this->_groupByArray
[$tableName . '_' . $fieldName] = $field['dbAlias'];
5884 protected function getDefaultsFromOptions($options) {
5886 'fields_defaults' => $options['fields_defaults'],
5887 'filters_defaults' => $options['filters_defaults'],
5888 'group_bys_defaults' => $options['group_bys_defaults'],
5889 'order_bys_defaults' => $options['order_bys_defaults'],
5895 * Get the select clause for a field, wrapping in GROUP_CONCAT if appropriate.
5897 * Full group by mode dictates that a field must either be in the group by function or
5898 * wrapped in a aggregate function. Here we wrap the field in GROUP_CONCAT if it is not in the
5901 * @param string $tableName
5902 * @param string $fieldName
5903 * @param string $field
5906 protected function getSelectClauseWithGroupConcatIfNotGroupedBy($tableName, &$fieldName, &$field) {
5907 if ($this->groupConcatTested
&& (!empty($this->_groupByArray
) ||
$this->isForceGroupBy
)) {
5908 if ((empty($field['statistics']) ||
in_array('GROUP_CONCAT', $field['statistics']))) {
5909 $label = CRM_Utils_Array
::value('title', $field);
5910 $alias = "{$tableName}_{$fieldName}";
5911 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $label;
5912 $this->_selectAliases
[] = $alias;
5913 if (empty($this->_groupByArray
[$tableName . '_' . $fieldName])) {
5914 return "GROUP_CONCAT(DISTINCT {$field['dbAlias']}) as $alias";
5916 return "({$field['dbAlias']}) as $alias";
5922 * Generate clause for the selected filter.
5924 * @param array $field
5925 * Field specification
5926 * @param string $fieldName
5930 * Relevant where clause.
5932 protected function generateFilterClause($field, $fieldName) {
5933 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
5934 if (CRM_Utils_Array
::value('operatorType', $field) ==
5935 CRM_Report_Form
::OP_MONTH
5937 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
5938 $value = CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
);
5939 if (is_array($value) && !empty($value)) {
5940 return "(month({$field['dbAlias']}) $op (" . implode(', ', $value) .
5945 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
5946 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
5947 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
5948 $fromTime = CRM_Utils_Array
::value("{$fieldName}_from_time", $this->_params
);
5949 $toTime = CRM_Utils_Array
::value("{$fieldName}_to_time", $this->_params
);
5950 return $this->dateClause($field['dbAlias'], $relative, $from, $to, $field['type'], $fromTime, $toTime);
5954 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
5956 return $this->whereClause($field,
5958 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
5959 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
5960 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)