3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 = array();
80 * The set of filters in the report
84 protected $_filters = array();
87 * The set of optional columns in the report
91 protected $_options = array();
94 * By default most reports hide contact id.
95 * Setting this to true makes it available
97 protected $_exposeContactID = TRUE;
100 * Set of statistic fields
104 protected $_statFields = array();
107 * Set of statistics data
111 protected $_statistics = array();
114 * List of fields not to be repeated during display
118 protected $_noRepeats = array();
121 * List of fields not to be displayed
125 protected $_noDisplay = array();
128 * Object type that a custom group extends
132 protected $_customGroupExtends = NULL;
133 protected $_customGroupExtendsJoin = array();
134 protected $_customGroupFilters = TRUE;
135 protected $_customGroupGroupBy = FALSE;
136 protected $_customGroupJoin = 'LEFT JOIN';
141 protected $_tagFilter = FALSE;
144 * specify entity table for tags filter
146 protected $_tagFilterTable = 'civicrm_contact';
149 * Build groups filter.
153 protected $_groupFilter = FALSE;
160 public $_navigation = array();
162 public $_drilldownReport = array();
165 * Array of tabs to display on report.
167 * E.g we define the tab title, the tpl and the tab-specific part of the css or html link.
169 * $this->tabs['OrderBy'] = array(
170 * 'title' => ts('Sorting'),
171 * 'tpl' => 'OrderBy',
172 * 'div_label' => 'order-by',
177 protected $tabs = array();
180 * An attribute for checkbox/radio form field layout
184 protected $_fourColumnAttribute = array(
185 '</td><td width="25%">',
186 '</td><td width="25%">',
187 '</td><td width="25%">',
191 protected $_force = 1;
193 protected $_params = NULL;
194 protected $_formValues = NULL;
195 protected $_instanceValues = NULL;
197 protected $_instanceForm = FALSE;
198 protected $_criteriaForm = FALSE;
200 protected $_instanceButtonName = NULL;
201 protected $_createNewButtonName = NULL;
202 protected $_printButtonName = NULL;
203 protected $_pdfButtonName = NULL;
204 protected $_csvButtonName = NULL;
205 protected $_groupButtonName = NULL;
206 protected $_chartButtonName = NULL;
207 protected $_csvSupported = TRUE;
208 protected $_add2groupSupported = TRUE;
209 protected $_groups = NULL;
210 protected $_grandFlag = FALSE;
211 protected $_rowsFound = NULL;
212 protected $_selectAliases = array();
213 protected $_rollup = NULL;
218 protected $_aliases = array();
234 protected $_limit = NULL;
237 * This can be set to specify a limit to the number of rows
238 * Since it is currently envisaged as part of the api usage it is only being applied
239 * when $_output mode is not 'html' or 'group' so as not to have to interpret / mess with that part
240 * of the code (see limit() fn
243 protected $_limitValue = NULL;
246 * This can be set to specify row offset
247 * See notes on _limitValue
250 protected $_offsetValue = NULL;
254 protected $_sections = NULL;
255 protected $_autoIncludeIndexedFieldsAsOrderBys = 0;
256 protected $_absoluteUrl = FALSE;
259 * Flag to indicate if result-set is to be stored in a class variable which could be retrieved using getResultSet() method.
263 protected $_storeResultSet = FALSE;
266 * When _storeResultSet Flag is set use this var to store result set in form of array
270 protected $_resultSet = array();
273 * To what frequency group-by a date column
277 protected $_groupByDateFreq = array(
279 'YEARWEEK' => 'Week',
280 'QUARTER' => 'Quarter',
285 * Variables to hold the acl inner join and where clause
287 protected $_aclFrom = NULL;
288 protected $_aclWhere = NULL;
291 * Array of DAO tables having columns included in SELECT or ORDER BY clause
295 protected $_selectedTables;
298 * Output mode e.g 'print', 'csv', 'pdf'
301 protected $_outputMode;
303 public $_having = NULL;
304 public $_select = NULL;
305 public $_selectClauses = array();
306 public $_columnHeaders = array();
307 public $_orderBy = NULL;
308 public $_orderByFields = array();
309 public $_orderByArray = array();
310 public $_groupBy = NULL;
311 public $_whereClauses = array();
312 public $_havingClauses = array();
315 * DashBoardRowCount Dashboard row count
318 public $_dashBoardRowCount;
321 * Is this being called without a form controller (ie. the report is being render outside the normal form
322 * - e.g the api is retrieving the rows
325 public $noController = FALSE;
328 * Variable to hold the currency alias
330 protected $_currencyColumn = NULL;
335 protected $_interval;
340 protected $_sendmail;
353 * @var string Report description.
355 public $_description;
358 * @var bool Is an address field selected.
359 * This was intended to determine if the address table should be joined in
360 * The isTableSelected function is now preferred for this purpose
362 protected $_addressField;
365 * @var bool Is an email field selected.
366 * This was intended to determine if the email table should be joined in
367 * The isTableSelected function is now preferred for this purpose
369 protected $_emailField;
372 * @var bool Is a phone field selected.
373 * This was intended to determine if the phone table should be joined in
374 * The isTableSelected function is now preferred for this purpose
376 protected $_phoneField;
379 * @var bool Create new report instance? (or update existing) on save.
381 protected $_createNew;
386 public function __construct() {
387 parent
::__construct();
389 $this->addClass('crm-report-form');
391 if ($this->_tagFilter
) {
392 $this->buildTagFilter();
394 if ($this->_exposeContactID
) {
395 if (array_key_exists('civicrm_contact', $this->_columns
)) {
396 $this->_columns
['civicrm_contact']['fields']['exposed_id'] = array(
398 'title' => 'Contact ID',
404 if ($this->_groupFilter
) {
405 $this->buildGroupFilter();
408 // Get all custom groups
409 $allGroups = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_CustomField', 'custom_group_id');
411 // Get the custom groupIds for which the user has VIEW permission
412 // If the user has 'access all custom data' permission, we'll leave $permCustomGroupIds empty
413 // and addCustomDataToColumns() will allow access to all custom groups.
414 $permCustomGroupIds = array();
415 if (!CRM_Core_Permission
::check('access all custom data')) {
416 $permCustomGroupIds = CRM_ACL_API
::group(CRM_Core_Permission
::VIEW
, NULL, 'civicrm_custom_group', $allGroups, NULL);
417 // do not allow custom data for reports if user doesn't have
418 // permission to access custom data.
419 if (!empty($this->_customGroupExtends
) && empty($permCustomGroupIds)) {
420 $this->_customGroupExtends
= array();
424 // merge custom data columns to _columns list, if any
425 $this->addCustomDataToColumns(TRUE, $permCustomGroupIds);
427 // add / modify display columns, filters ..etc
428 CRM_Utils_Hook
::alterReportVar('columns', $this->_columns
, $this);
430 //assign currencyColumn variable to tpl
431 $this->assign('currencyColumn', $this->_currencyColumn
);
435 * Shared pre-process function.
437 * If overriding preProcess function this should still be called.
441 public function preProcessCommon() {
443 = CRM_Utils_Request
::retrieve(
446 CRM_Core_DAO
::$_nullObject
449 $this->_dashBoardRowCount
450 = CRM_Utils_Request
::retrieve(
453 CRM_Core_DAO
::$_nullObject
456 $this->_section
= CRM_Utils_Request
::retrieve('section', 'Integer', CRM_Core_DAO
::$_nullObject);
458 $this->assign('section', $this->_section
);
459 CRM_Core_Region
::instance('page-header')->add(array(
460 'markup' => sprintf('<!-- Report class: [%s] -->', htmlentities(get_class($this))),
462 if (!$this->noController
) {
463 $this->setID($this->get('instanceId'));
466 $this->setID(CRM_Report_Utils_Report
::getInstanceID());
468 $this->setID(CRM_Report_Utils_Report
::getInstanceIDForPath());
472 // set qfkey so that pager picks it up and use it in the "Next > Last >>" links.
473 // FIXME: Note setting it in $_GET doesn't work, since pager generates link based on QUERY_STRING
474 $_SERVER['QUERY_STRING'] .= "&qfKey={$this->controller->_key}";
478 $this->assign('instanceId', $this->_id
);
479 $params = array('id' => $this->_id
);
480 $this->_instanceValues
= array();
481 CRM_Core_DAO
::commonRetrieve('CRM_Report_DAO_ReportInstance',
483 $this->_instanceValues
485 if (empty($this->_instanceValues
)) {
486 CRM_Core_Error
::fatal("Report could not be loaded.");
488 $this->_title
= $this->_instanceValues
['title'];
489 if (!empty($this->_instanceValues
['permission']) &&
490 (!(CRM_Core_Permission
::check($this->_instanceValues
['permission']) ||
491 CRM_Core_Permission
::check('administer Reports')
494 CRM_Utils_System
::permissionDenied();
495 CRM_Utils_System
::civiExit();
498 $formValues = CRM_Utils_Array
::value('form_values', $this->_instanceValues
);
500 $this->_formValues
= unserialize($formValues);
503 $this->_formValues
= NULL;
506 // lets always do a force if reset is found in the url.
507 if (!empty($_REQUEST['reset'])) {
512 $this->assign('mode', 'instance');
514 elseif (!$this->noController
) {
515 list($optionValueID, $optionValue) = CRM_Report_Utils_Report
::getValueIDFromUrl();
516 $instanceCount = CRM_Report_Utils_Report
::getInstanceCount($optionValue);
517 if (($instanceCount > 0) && $optionValueID) {
518 $this->assign('instanceUrl',
519 CRM_Utils_System
::url('civicrm/report/list',
520 "reset=1&ovid=$optionValueID"
524 if ($optionValueID) {
525 $this->_description
= CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', $optionValueID, 'description');
529 $this->assign('mode', 'template');
532 // lets display the Report Settings section
533 $this->_instanceForm
= $this->_force ||
$this->_id ||
(!empty($_POST));
535 // Do not display Report Settings section if administer Reports permission is absent OR
536 // if report instance is reserved and administer reserved reports absent
537 if (!CRM_Core_Permission
::check('administer Reports') ||
538 ($this->_instanceValues
['is_reserved'] &&
539 !CRM_Core_Permission
::check('administer reserved reports'))
541 $this->_instanceForm
= FALSE;
544 $this->assign('criteriaForm', FALSE);
545 // Display Report Criteria section if user has access Report Criteria OR administer Reports AND report instance is not reserved
546 if (CRM_Core_Permission
::check('administer Reports') ||
547 CRM_Core_Permission
::check('access Report Criteria')
549 if (!$this->_instanceValues
['is_reserved'] ||
550 CRM_Core_Permission
::check('administer reserved reports')
552 $this->assign('criteriaForm', TRUE);
553 $this->_criteriaForm
= TRUE;
557 $this->_instanceButtonName
= $this->getButtonName('submit', 'save');
558 $this->_createNewButtonName
= $this->getButtonName('submit', 'next');
559 $this->_printButtonName
= $this->getButtonName('submit', 'print');
560 $this->_pdfButtonName
= $this->getButtonName('submit', 'pdf');
561 $this->_csvButtonName
= $this->getButtonName('submit', 'csv');
562 $this->_groupButtonName
= $this->getButtonName('submit', 'group');
563 $this->_chartButtonName
= $this->getButtonName('submit', 'chart');
569 public function addBreadCrumb() {
573 'title' => ts('Report Templates'),
574 'url' => CRM_Utils_System
::url('civicrm/admin/report/template/list', 'reset=1'),
578 CRM_Utils_System
::appendBreadCrumb($breadCrumbs);
582 * Pre process function.
584 * Called prior to build form.
586 public function preProcess() {
587 $this->preProcessCommon();
590 $this->addBreadCrumb();
593 foreach ($this->_columns
as $tableName => $table) {
595 if (!isset($table['alias'])) {
596 $this->_columns
[$tableName]['alias'] = substr($tableName, 8) .
600 $this->_columns
[$tableName]['alias'] = $table['alias'] . '_civireport';
603 $this->_aliases
[$tableName] = $this->_columns
[$tableName]['alias'];
605 $daoOrBaoName = NULL;
606 // higher preference to bao object
607 if (array_key_exists('bao', $table)) {
608 $daoOrBaoName = $table['bao'];
609 $expFields = $daoOrBaoName::exportableFields();
611 elseif (array_key_exists('dao', $table)) {
612 $daoOrBaoName = $table['dao'];
613 $expFields = $daoOrBaoName::export();
616 $expFields = array();
619 $doNotCopy = array('required');
621 $fieldGroups = array('fields', 'filters', 'group_bys', 'order_bys');
622 foreach ($fieldGroups as $fieldGrp) {
623 if (!empty($table[$fieldGrp]) && is_array($table[$fieldGrp])) {
624 foreach ($table[$fieldGrp] as $fieldName => $field) {
625 // $name is the field name used to reference the BAO/DAO export fields array
626 $name = isset($field['name']) ?
$field['name'] : $fieldName;
628 // Sometimes the field name key in the BAO/DAO export fields array is
629 // different from the actual database field name.
630 // Unset $field['name'] so that actual database field name can be obtained
631 // from the BAO/DAO export fields array.
632 unset($field['name']);
634 if (array_key_exists($name, $expFields)) {
635 foreach ($doNotCopy as $dnc) {
636 // unset the values we don't want to be copied.
637 unset($expFields[$name][$dnc]);
640 $this->_columns
[$tableName][$fieldGrp][$fieldName] = $expFields[$name];
643 foreach ($expFields[$name] as $property => $val) {
644 if (!array_key_exists($property, $field)) {
645 $this->_columns
[$tableName][$fieldGrp][$fieldName][$property] = $val;
652 if (!empty($field['no_repeat'])) {
653 $this->_noRepeats
[] = "{$tableName}_{$fieldName}";
655 if (!empty($field['no_display'])) {
656 $this->_noDisplay
[] = "{$tableName}_{$fieldName}";
659 // set alias = table-name, unless already set
660 $alias = isset($field['alias']) ?
$field['alias'] : (
661 isset($this->_columns
[$tableName]['alias']) ?
$this->_columns
[$tableName]['alias'] : $tableName
663 $this->_columns
[$tableName][$fieldGrp][$fieldName]['alias'] = $alias;
665 // set name = fieldName, unless already set
666 if (!isset($this->_columns
[$tableName][$fieldGrp][$fieldName]['name'])) {
667 $this->_columns
[$tableName][$fieldGrp][$fieldName]['name'] = $name;
670 // set dbAlias = alias.name, unless already set
671 if (!isset($this->_columns
[$tableName][$fieldGrp][$fieldName]['dbAlias'])) {
672 $this->_columns
[$tableName][$fieldGrp][$fieldName]['dbAlias']
674 $this->_columns
[$tableName][$fieldGrp][$fieldName]['name'];
677 // a few auto fills for filters
678 if ($fieldGrp == 'filters') {
679 // fill operator types
680 if (!array_key_exists('operatorType', $this->_columns
[$tableName][$fieldGrp][$fieldName])) {
681 switch (CRM_Utils_Array
::value('type', $this->_columns
[$tableName][$fieldGrp][$fieldName])) {
682 case CRM_Utils_Type
::T_MONEY
:
683 case CRM_Utils_Type
::T_FLOAT
:
684 $this->_columns
[$tableName][$fieldGrp][$fieldName]['operatorType'] = CRM_Report_Form
::OP_FLOAT
;
687 case CRM_Utils_Type
::T_INT
:
688 $this->_columns
[$tableName][$fieldGrp][$fieldName]['operatorType'] = CRM_Report_Form
::OP_INT
;
691 case CRM_Utils_Type
::T_DATE
:
692 $this->_columns
[$tableName][$fieldGrp][$fieldName]['operatorType'] = CRM_Report_Form
::OP_DATE
;
695 case CRM_Utils_Type
::T_BOOLEAN
:
696 $this->_columns
[$tableName][$fieldGrp][$fieldName]['operatorType'] = CRM_Report_Form
::OP_SELECT
;
697 if (!array_key_exists('options', $this->_columns
[$tableName][$fieldGrp][$fieldName])) {
698 $this->_columns
[$tableName][$fieldGrp][$fieldName]['options']
709 array_key_exists('pseudoconstant', $this->_columns
[$tableName][$fieldGrp][$fieldName])
711 // with multiple options operator-type is generally multi-select
712 $this->_columns
[$tableName][$fieldGrp][$fieldName]['operatorType'] = CRM_Report_Form
::OP_MULTISELECT
;
713 if (!array_key_exists('options', $this->_columns
[$tableName][$fieldGrp][$fieldName])) {
715 $this->_columns
[$tableName][$fieldGrp][$fieldName]['options'] = CRM_Core_PseudoConstant
::get($daoOrBaoName, $fieldName);
726 // copy filters to a separate handy variable
727 if (array_key_exists('filters', $table)) {
728 $this->_filters
[$tableName] = $this->_columns
[$tableName]['filters'];
731 if (array_key_exists('group_bys', $table)) {
732 $groupBys[$tableName] = $this->_columns
[$tableName]['group_bys'];
735 if (array_key_exists('fields', $table)) {
736 $reportFields[$tableName] = $this->_columns
[$tableName]['fields'];
741 $this->setDefaultValues(FALSE);
744 CRM_Report_Utils_Get
::processFilter($this->_filters
, $this->_defaults
);
745 CRM_Report_Utils_Get
::processGroupBy($groupBys, $this->_defaults
);
746 CRM_Report_Utils_Get
::processFields($reportFields, $this->_defaults
);
747 CRM_Report_Utils_Get
::processChart($this->_defaults
);
750 $this->_formValues
= $this->_defaults
;
751 $this->postProcess();
756 * Set default values.
758 * @param bool $freeze
762 public function setDefaultValues($freeze = TRUE) {
763 $freezeGroup = array();
765 // FIXME: generalizing form field naming conventions would reduce
766 // Lots of lines below.
767 foreach ($this->_columns
as $tableName => $table) {
768 if (array_key_exists('fields', $table)) {
769 foreach ($table['fields'] as $fieldName => $field) {
770 if (empty($field['no_display'])) {
771 if (isset($field['required'])) {
773 $this->_defaults
['fields'][$fieldName] = 1;
776 // find element object, so that we could use quickform's freeze method
777 // for required elements
778 $obj = $this->getElementFromGroup("fields", $fieldName);
780 $freezeGroup[] = $obj;
784 elseif (isset($field['default'])) {
785 $this->_defaults
['fields'][$fieldName] = $field['default'];
791 if (array_key_exists('group_bys', $table)) {
792 foreach ($table['group_bys'] as $fieldName => $field) {
793 if (isset($field['default'])) {
794 if (!empty($field['frequency'])) {
795 $this->_defaults
['group_bys_freq'][$fieldName] = 'MONTH';
797 $this->_defaults
['group_bys'][$fieldName] = $field['default'];
801 if (array_key_exists('filters', $table)) {
802 foreach ($table['filters'] as $fieldName => $field) {
803 if (isset($field['default'])) {
804 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
806 if (is_array($field['default'])) {
807 $this->_defaults
["{$fieldName}_from"] = CRM_Utils_Array
::value('from', $field['default']);
808 $this->_defaults
["{$fieldName}_to"] = CRM_Utils_Array
::value('to', $field['default']);
809 $this->_defaults
["{$fieldName}_relative"] = 0;
812 $this->_defaults
["{$fieldName}_relative"] = $field['default'];
816 $this->_defaults
["{$fieldName}_value"] = $field['default'];
819 //assign default value as "in" for multiselect
820 //operator, To freeze the select element
821 if (CRM_Utils_Array
::value('operatorType', $field) ==
822 CRM_Report_Form
::OP_MULTISELECT
824 $this->_defaults
["{$fieldName}_op"] = 'in';
826 if (CRM_Utils_Array
::value('operatorType', $field) ==
827 CRM_Report_Form
::OP_ENTITYREF
829 $this->_defaults
["{$fieldName}_op"] = 'in';
831 elseif (CRM_Utils_Array
::value('operatorType', $field) ==
832 CRM_Report_Form
::OP_MULTISELECT_SEPARATOR
834 $this->_defaults
["{$fieldName}_op"] = 'mhas';
836 elseif ($op = CRM_Utils_Array
::value('default_op', $field)) {
837 $this->_defaults
["{$fieldName}_op"] = $op;
843 empty($this->_formValues
['order_bys']) &&
844 (array_key_exists('order_bys', $table) &&
845 is_array($table['order_bys']))
847 if (!array_key_exists('order_bys', $this->_defaults
)) {
848 $this->_defaults
['order_bys'] = array();
850 foreach ($table['order_bys'] as $fieldName => $field) {
851 if (!empty($field['default']) ||
!empty($field['default_order']) ||
852 CRM_Utils_Array
::value('default_is_section', $field) ||
853 !empty($field['default_weight'])
856 'column' => $fieldName,
857 'order' => CRM_Utils_Array
::value('default_order', $field, 'ASC'),
858 'section' => CRM_Utils_Array
::value('default_is_section', $field, 0),
861 if (!empty($field['default_weight'])) {
862 $this->_defaults
['order_bys'][(int) $field['default_weight']] = $order_by;
865 array_unshift($this->_defaults
['order_bys'], $order_by);
871 foreach ($this->_options
as $fieldName => $field) {
872 if (isset($field['default'])) {
873 $this->_defaults
['options'][$fieldName] = $field['default'];
878 if (!empty($this->_submitValues
)) {
879 $this->preProcessOrderBy($this->_submitValues
);
882 $this->preProcessOrderBy($this->_defaults
);
885 // lets finish freezing task here itself
886 if (!empty($freezeGroup)) {
887 foreach ($freezeGroup as $elem) {
892 if ($this->_formValues
) {
893 $this->_defaults
= array_merge($this->_defaults
, $this->_formValues
);
896 if ($this->_instanceValues
) {
897 $this->_defaults
= array_merge($this->_defaults
, $this->_instanceValues
);
900 CRM_Report_Form_Instance
::setDefaultValues($this, $this->_defaults
);
902 return $this->_defaults
;
906 * Get element from group.
908 * @param string $group
909 * @param string $grpFieldName
913 public function getElementFromGroup($group, $grpFieldName) {
914 $eleObj = $this->getElement($group);
915 foreach ($eleObj->_elements
as $index => $obj) {
916 if ($grpFieldName == $obj->_attributes
['name']) {
924 * Setter for $_params.
926 * @param array $params
928 public function setParams($params) {
929 $this->_params
= $params;
935 * @param int $instanceID
937 public function setID($instanceID) {
938 $this->_id
= $instanceID;
942 * Setter for $_force.
944 * @param bool $isForce
946 public function setForce($isForce) {
947 $this->_force
= $isForce;
951 * Setter for $_limitValue.
953 * @param int $_limitValue
955 public function setLimitValue($_limitValue) {
956 $this->_limitValue
= $_limitValue;
960 * Setter for $_offsetValue.
962 * @param int $_offsetValue
964 public function setOffsetValue($_offsetValue) {
965 $this->_offsetValue
= $_offsetValue;
969 * Getter for $_defaultValues.
973 public function getDefaultValues() {
974 return $this->_defaults
;
978 * Add columns to report.
980 public function addColumns() {
983 foreach ($this->_columns
as $tableName => $table) {
984 if (array_key_exists('fields', $table)) {
985 foreach ($table['fields'] as $fieldName => $field) {
987 if (empty($field['no_display'])) {
988 foreach (array('table', 'field') as $var) {
989 if (!empty($
{$var}['grouping'])) {
990 if (!is_array($
{$var}['grouping'])) {
991 $tableName = $
{$var}['grouping'];
994 $tableName = array_keys($
{$var}['grouping']);
995 $tableName = $tableName[0];
996 $groupTitle = array_values($
{$var}['grouping']);
997 $groupTitle = $groupTitle[0];
1002 if (!$groupTitle && isset($table['group_title'])) {
1003 $groupTitle = $table['group_title'];
1004 // Having a group_title is secret code for being a custom group
1005 // which cryptically translates to needing an accordion.
1006 // here we make that explicit.
1007 $colGroups[$tableName]['use_accordian_for_field_selection'] = TRUE;
1010 $colGroups[$tableName]['fields'][$fieldName] = CRM_Utils_Array
::value('title', $field);
1011 if ($groupTitle && empty($colGroups[$tableName]['group_title'])) {
1012 $colGroups[$tableName]['group_title'] = $groupTitle;
1014 $options[$fieldName] = CRM_Utils_Array
::value('title', $field);
1020 $this->addCheckBox("fields", ts('Select Columns'), $options, NULL,
1021 NULL, NULL, NULL, $this->_fourColumnAttribute
, TRUE
1023 if (!empty($colGroups)) {
1024 $this->tabs
['FieldSelection'] = array(
1025 'title' => ts('Columns'),
1026 'tpl' => 'FieldSelection',
1027 'div_label' => 'col-groups',
1030 // Note this assignment is only really required in buildForm. It is being 'over-called'
1031 // to reduce risk of being missed due to overridden functions.
1032 $this->assign('tabs', $this->tabs
);
1035 $this->assign('colGroups', $colGroups);
1039 * Add filters to report.
1041 public function addFilters() {
1042 $filters = $filterGroups = array();
1045 foreach ($this->_filters
as $table => $attributes) {
1046 if (isset($this->_columns
[$table]['group_title'])) {
1047 // The presence of 'group_title' is secret code for 'is_a_custom_table'
1048 // which magically means to 'display in an accordian'
1049 // here we make this explicit.
1050 $filterGroups[$table] = array(
1051 'group_title' => $this->_columns
[$table]['group_title'],
1052 'use_accordian_for_field_selection' => TRUE,
1056 foreach ($attributes as $fieldName => $field) {
1057 // get ready with option value pair
1058 // @ 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
1060 $operations = $this->getOperationPair(
1061 CRM_Utils_Array
::value('operatorType', $field),
1064 $filters[$table][$fieldName] = $field;
1066 switch (CRM_Utils_Array
::value('operatorType', $field)) {
1067 case CRM_Report_Form
::OP_MONTH
:
1068 if (!array_key_exists('options', $field) ||
1069 !is_array($field['options']) ||
empty($field['options'])
1071 // If there's no option list for this filter, define one.
1072 $field['options'] = array(
1074 2 => ts('February'),
1081 9 => ts('September'),
1082 10 => ts('October'),
1083 11 => ts('November'),
1084 12 => ts('December'),
1086 // Add this option list to this column _columns. This is
1087 // required so that filter statistics show properly.
1088 $this->_columns
[$table]['filters'][$fieldName]['options'] = $field['options'];
1090 case CRM_Report_Form
::OP_MULTISELECT
:
1091 case CRM_Report_Form
::OP_MULTISELECT_SEPARATOR
:
1092 // assume a multi-select field
1093 if (!empty($field['options']) ||
1094 $fieldName == 'state_province_id' ||
$fieldName == 'county_id'
1096 $element = $this->addElement('select', "{$fieldName}_op", ts('Operator:'), $operations);
1097 if (count($operations) <= 1) {
1100 if ($fieldName == 'state_province_id' ||
1101 $fieldName == 'county_id'
1103 $this->addChainSelect($fieldName . '_value', array(
1110 $this->addElement('select', "{$fieldName}_value", NULL, $field['options'], array(
1111 'style' => 'min-width:250px',
1112 'class' => 'crm-select2 huge',
1114 'placeholder' => ts('- select -'),
1120 case CRM_Report_Form
::OP_SELECT
:
1121 // assume a select field
1122 $this->addElement('select', "{$fieldName}_op", ts('Operator:'), $operations);
1123 if (!empty($field['options'])) {
1124 $this->addElement('select', "{$fieldName}_value", NULL, $field['options']);
1128 case CRM_Report_Form
::OP_ENTITYREF
:
1129 $this->addElement('select', "{$fieldName}_op", ts('Operator:'), $operations);
1130 $this->setEntityRefDefaults($field, $table);
1131 $this->addEntityRef("{$fieldName}_value", NULL, $field['attributes']);
1134 case CRM_Report_Form
::OP_DATE
:
1135 // build datetime fields
1136 CRM_Core_Form_Date
::buildDateRange($this, $fieldName, $count, '_from', '_to', 'From:', FALSE, $operations);
1140 case CRM_Report_Form
::OP_DATETIME
:
1141 // build datetime fields
1142 CRM_Core_Form_Date
::buildDateRange($this, $fieldName, $count, '_from', '_to', 'From:', FALSE, $operations, 'searchDate', TRUE);
1146 case CRM_Report_Form
::OP_INT
:
1147 case CRM_Report_Form
::OP_FLOAT
:
1148 // and a min value input box
1149 $this->add('text', "{$fieldName}_min", ts('Min'));
1150 // and a max value input box
1151 $this->add('text', "{$fieldName}_max", ts('Max'));
1153 // default type is string
1154 $this->addElement('select', "{$fieldName}_op", ts('Operator:'), $operations,
1155 array('onchange' => "return showHideMaxMinVal( '$fieldName', this.value );")
1157 // we need text box for value input
1158 $this->add('text', "{$fieldName}_value", NULL, array('class' => 'huge'));
1163 if (!empty($filters)) {
1164 $this->tabs
['Filters'] = array(
1165 'title' => ts('Filters'),
1167 'div_label' => 'set-filters',
1170 $this->assign('filters', $filters);
1171 $this->assign('filterGroups', $filterGroups);
1175 * Function to assign the tabs to the template in the correct order.
1177 * We want the tabs to wind up in this order (if not overridden).
1185 protected function assignTabs() {
1193 $order = array_intersect_key(array_fill_keys($order, 1), $this->tabs
);
1194 $order = array_merge($order, $this->tabs
);
1195 $this->assign('tabs', $order);
1199 * Add options defined in $this->_options to the report.
1201 public function addOptions() {
1202 if (!empty($this->_options
)) {
1203 // FIXME: For now lets build all elements as checkboxes.
1204 // Once we clear with the format we can build elements based on type
1206 foreach ($this->_options
as $fieldName => $field) {
1209 if ($field['type'] == 'select') {
1210 $this->addElement('select', "{$fieldName}", $field['title'], $field['options']);
1212 elseif ($field['type'] == 'checkbox') {
1213 $options[$field['title']] = $fieldName;
1214 $this->addCheckBox($fieldName, NULL,
1216 NULL, NULL, NULL, $this->_fourColumnAttribute
1221 if (!empty($this->_options
)) {
1222 $this->tabs
['ReportOptions'] = array(
1223 'title' => ts('Display Options'),
1224 'tpl' => 'ReportOptions',
1225 'div_label' => 'other-options',
1228 $this->assign('otherOptions', $this->_options
);
1232 * Add chart options to the report.
1234 public function addChartOptions() {
1235 if (!empty($this->_charts
)) {
1236 $this->addElement('select', "charts", ts('Chart'), $this->_charts
);
1237 $this->assign('charts', $this->_charts
);
1238 $this->addElement('submit', $this->_chartButtonName
, ts('View'));
1243 * Add group by options to the report.
1245 public function addGroupBys() {
1246 $options = $freqElements = array();
1248 foreach ($this->_columns
as $tableName => $table) {
1249 if (array_key_exists('group_bys', $table)) {
1250 foreach ($table['group_bys'] as $fieldName => $field) {
1251 if (!empty($field)) {
1252 $options[$field['title']] = $fieldName;
1253 if (!empty($field['frequency'])) {
1254 $freqElements[$field['title']] = $fieldName;
1260 $this->addCheckBox("group_bys", ts('Group by columns'), $options, NULL,
1261 NULL, NULL, NULL, $this->_fourColumnAttribute
1263 $this->assign('groupByElements', $options);
1264 if (!empty($options)) {
1265 $this->tabs
['GroupBy'] = array(
1266 'title' => ts('Grouping'),
1268 'div_label' => 'group-by-elements',
1272 foreach ($freqElements as $name) {
1273 $this->addElement('select', "group_bys_freq[$name]",
1274 ts('Frequency'), $this->_groupByDateFreq
1280 * Add data for order by tab.
1282 public function addOrderBys() {
1284 foreach ($this->_columns
as $tableName => $table) {
1286 // Report developer may define any column to order by; include these as order-by options.
1287 if (array_key_exists('order_bys', $table)) {
1288 foreach ($table['order_bys'] as $fieldName => $field) {
1289 if (!empty($field)) {
1290 $options[$fieldName] = $field['title'];
1295 // Add searchable custom fields as order-by options, if so requested
1296 // (These are already indexed, so allowing to order on them is cheap.)
1298 if ($this->_autoIncludeIndexedFieldsAsOrderBys
&&
1299 array_key_exists('extends', $table) && !empty($table['extends'])
1301 foreach ($table['fields'] as $fieldName => $field) {
1302 if (empty($field['no_display'])) {
1303 $options[$fieldName] = $field['title'];
1311 $this->assign('orderByOptions', $options);
1312 if (!empty($options)) {
1313 $this->tabs
['OrderBy'] = array(
1314 'title' => ts('Sorting'),
1316 'div_label' => 'order-by-elements',
1320 if (!empty($options)) {
1322 '-' => ' - none - ',
1324 for ($i = 1; $i <= 5; $i++
) {
1325 $this->addElement('select', "order_bys[{$i}][column]", ts('Order by Column'), $options);
1326 $this->addElement('select', "order_bys[{$i}][order]", ts('Order by Order'), array(
1327 'ASC' => 'Ascending',
1328 'DESC' => 'Descending',
1330 $this->addElement('checkbox', "order_bys[{$i}][section]", ts('Order by Section'), FALSE, array('id' => "order_by_section_$i"));
1331 $this->addElement('checkbox', "order_bys[{$i}][pageBreak]", ts('Page Break'), FALSE, array('id' => "order_by_pagebreak_$i"));
1337 * This adds the tab referred to as Title and Format, rendered through Instance.tpl.
1339 * @todo call this tab into the report template in the same way as OrderBy etc, ie
1340 * by adding a description of the tab to $this->tabs, causing the tab to be added in
1343 public function buildInstanceAndButtons() {
1344 CRM_Report_Form_Instance
::buildForm($this);
1346 $label = $this->_id ?
ts('Update Report') : ts('Create Report');
1348 $this->addElement('submit', $this->_instanceButtonName
, $label);
1349 $this->addElement('submit', $this->_printButtonName
, ts('Print Report'));
1350 $this->addElement('submit', $this->_pdfButtonName
, ts('PDF'));
1353 $this->addElement('submit', $this->_createNewButtonName
,
1354 ts('Save a Copy') . '...');
1356 $this->assign('instanceForm', $this->_instanceForm
);
1358 $label = $this->_id ?
ts('Print Report') : ts('Print Preview');
1359 $this->addElement('submit', $this->_printButtonName
, $label);
1361 $label = $this->_id ?
ts('PDF') : ts('Preview PDF');
1362 $this->addElement('submit', $this->_pdfButtonName
, $label);
1364 $label = $this->_id ?
ts('Export to CSV') : ts('Preview CSV');
1366 if ($this->_csvSupported
) {
1367 $this->addElement('submit', $this->_csvButtonName
, $label);
1370 if (CRM_Core_Permission
::check('administer Reports') &&
1371 $this->_add2groupSupported
1373 $this->addElement('select', 'groups', ts('Group'),
1374 array('' => ts('Add Contacts to Group')) +
1375 CRM_Core_PseudoConstant
::nestedGroup(),
1376 array('class' => 'crm-select2 crm-action-menu action-icon-plus huge')
1378 $this->assign('group', TRUE);
1381 $this->addElement('submit', $this->_groupButtonName
, '', array('style' => 'display: none;'));
1383 $this->addChartOptions();
1384 $this->addButtons(array(
1387 'name' => ts('Preview Report'),
1388 'isDefault' => TRUE,
1395 * Main build form function.
1397 public function buildQuickForm() {
1398 $this->addColumns();
1400 $this->addFilters();
1402 $this->addOptions();
1404 $this->addGroupBys();
1406 $this->addOrderBys();
1408 $this->buildInstanceAndButtons();
1410 // Add form rule for report.
1411 if (is_callable(array(
1415 $this->addFormRule(array(get_class($this), 'formRule'), $this);
1417 $this->assignTabs();
1421 * A form rule function for custom data.
1423 * The rule ensures that fields selected in group_by if any) should only be the ones
1424 * present in display/select fields criteria;
1425 * note: works if and only if any custom field selected in group_by.
1427 * @param array $fields
1428 * @param array $ignoreFields
1432 public function customDataFormRule($fields, $ignoreFields = array()) {
1434 if (!empty($this->_customGroupExtends
) && $this->_customGroupGroupBy
&&
1435 !empty($fields['group_bys'])
1437 foreach ($this->_columns
as $tableName => $table) {
1438 if ((substr($tableName, 0, 13) == 'civicrm_value' ||
1439 substr($tableName, 0, 12) == 'custom_value') &&
1440 !empty($this->_columns
[$tableName]['fields'])
1442 foreach ($this->_columns
[$tableName]['fields'] as $fieldName => $field) {
1443 if (array_key_exists($fieldName, $fields['group_bys']) &&
1444 !array_key_exists($fieldName, $fields['fields'])
1446 $errors['fields'] = "Please make sure fields selected in 'Group by Columns' section are also selected in 'Display Columns' section.";
1448 elseif (array_key_exists($fieldName, $fields['group_bys'])) {
1449 foreach ($fields['fields'] as $fld => $val) {
1450 if (!array_key_exists($fld, $fields['group_bys']) &&
1451 !in_array($fld, $ignoreFields)
1453 $errors['fields'] = "Please ensure that fields selected in 'Display Columns' are also selected in 'Group by Columns' section.";
1465 * Get operators to display on form.
1467 * Note: $fieldName param allows inheriting class to build operationPairs specific to a field.
1469 * @param string $type
1470 * @param string $fieldName
1474 public function getOperationPair($type = "string", $fieldName = NULL) {
1475 // FIXME: At some point we should move these key-val pairs
1476 // to option_group and option_value table.
1478 case CRM_Report_Form
::OP_INT
:
1479 case CRM_Report_Form
::OP_FLOAT
:
1482 'lte' => ts('Is less than or equal to'),
1483 'gte' => ts('Is greater than or equal to'),
1484 'bw' => ts('Is between'),
1485 'eq' => ts('Is equal to'),
1486 'lt' => ts('Is less than'),
1487 'gt' => ts('Is greater than'),
1488 'neq' => ts('Is not equal to'),
1489 'nbw' => ts('Is not between'),
1490 'nll' => ts('Is empty (Null)'),
1491 'nnll' => ts('Is not empty (Null)'),
1495 case CRM_Report_Form
::OP_SELECT
:
1497 'eq' => ts('Is equal to'),
1501 case CRM_Report_Form
::OP_MONTH
:
1502 case CRM_Report_Form
::OP_MULTISELECT
:
1503 case CRM_Report_Form
::OP_ENTITYREF
:
1506 'in' => ts('Is one of'),
1507 'notin' => ts('Is not one of'),
1511 case CRM_Report_Form
::OP_DATE
:
1514 'nll' => ts('Is empty (Null)'),
1515 'nnll' => ts('Is not empty (Null)'),
1519 case CRM_Report_Form
::OP_MULTISELECT_SEPARATOR
:
1520 // use this operator for the values, concatenated with separator. For e.g if
1521 // multiple options for a column is stored as ^A{val1}^A{val2}^A
1523 'mhas' => ts('Is one of'),
1524 'mnot' => ts('Is not one of'),
1531 'has' => ts('Contains'),
1532 'sw' => ts('Starts with'),
1533 'ew' => ts('Ends with'),
1534 'nhas' => ts('Does not contain'),
1535 'eq' => ts('Is equal to'),
1536 'neq' => ts('Is not equal to'),
1537 'nll' => ts('Is empty (Null)'),
1538 'nnll' => ts('Is not empty (Null)'),
1545 * Build the tag filter field to display on the filters tab.
1547 public function buildTagFilter() {
1548 $contactTags = CRM_Core_BAO_Tag
::getTags($this->_tagFilterTable
);
1549 if (!empty($contactTags)) {
1550 $this->_columns
['civicrm_tag'] = array(
1551 'dao' => 'CRM_Core_DAO_Tag',
1555 'title' => ts('Tag'),
1557 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
1558 'options' => $contactTags,
1566 * Adds group filters to _columns (called from _Construct).
1568 public function buildGroupFilter() {
1569 $this->_columns
['civicrm_group']['filters'] = array(
1571 'name' => 'group_id',
1572 'title' => ts('Group'),
1573 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
1575 'options' => CRM_Core_PseudoConstant
::nestedGroup(),
1578 if (empty($this->_columns
['civicrm_group']['dao'])) {
1579 $this->_columns
['civicrm_group']['dao'] = 'CRM_Contact_DAO_GroupContact';
1581 if (empty($this->_columns
['civicrm_group']['alias'])) {
1582 $this->_columns
['civicrm_group']['alias'] = 'cgroup';
1587 * Get SQL operator from form text version.
1589 * @param string $operator
1593 public function getSQLOperator($operator = "like") {
1594 switch ($operator) {
1627 return 'IS NOT NULL';
1636 * Generate where clause.
1638 * This can be overridden in reports for special treatment of a field
1640 * @param array $field Field specifications
1641 * @param string $op Query operator (not an exact match to sql)
1642 * @param mixed $value
1646 * @return null|string
1648 public function whereClause(&$field, $op, $value, $min, $max) {
1650 $type = CRM_Utils_Type
::typeToString(CRM_Utils_Array
::value('type', $field));
1656 if (($min !== NULL && strlen($min) > 0) ||
1657 ($max !== NULL && strlen($max) > 0)
1659 $min = CRM_Utils_Type
::escape($min, $type);
1660 $max = CRM_Utils_Type
::escape($max, $type);
1664 $clauses[] = "( {$field['dbAlias']} >= $min )";
1667 $clauses[] = "( {$field['dbAlias']} < $min )";
1672 $clauses[] = "( {$field['dbAlias']} <= $max )";
1675 $clauses[] = "( {$field['dbAlias']} > $max )";
1679 if (!empty($clauses)) {
1681 $clause = implode(' AND ', $clauses);
1684 $clause = implode(' OR ', $clauses);
1692 if ($value !== NULL && strlen($value) > 0) {
1693 $value = CRM_Utils_Type
::escape($value, $type);
1694 if (strpos($value, '%') === FALSE) {
1695 $value = "'%{$value}%'";
1698 $value = "'{$value}'";
1700 $sqlOP = $this->getSQLOperator($op);
1701 $clause = "( {$field['dbAlias']} $sqlOP $value )";
1707 if (is_string($value) && strlen($value)) {
1708 $value = explode(',', $value);
1710 if ($value !== NULL && is_array($value) && count($value) > 0) {
1711 $sqlOP = $this->getSQLOperator($op);
1712 if (CRM_Utils_Array
::value('type', $field) ==
1713 CRM_Utils_Type
::T_STRING
1715 //cycle through selections and escape values
1716 foreach ($value as $key => $selection) {
1717 $value[$key] = CRM_Utils_Type
::escape($selection, $type);
1720 = "( {$field['dbAlias']} $sqlOP ( '" . implode("' , '", $value) .
1724 // for numerical values
1725 $clause = "{$field['dbAlias']} $sqlOP (" . implode(', ', $value) .
1728 if ($op == 'notin') {
1729 $clause = "( " . $clause . " OR {$field['dbAlias']} IS NULL )";
1732 $clause = "( " . $clause . " )";
1738 // mhas == multiple has
1739 if ($value !== NULL && count($value) > 0) {
1740 $sqlOP = $this->getSQLOperator($op);
1742 = "{$field['dbAlias']} REGEXP '[[:cntrl:]]" . implode('|', $value) .
1748 // mnot == multiple is not one of
1749 if ($value !== NULL && count($value) > 0) {
1750 $sqlOP = $this->getSQLOperator($op);
1752 = "( {$field['dbAlias']} NOT REGEXP '[[:cntrl:]]" . implode('|', $value) .
1753 "[[:cntrl:]]' OR {$field['dbAlias']} IS NULL )";
1759 if ($value !== NULL && strlen($value) > 0) {
1760 $value = CRM_Utils_Type
::escape($value, $type);
1761 if (strpos($value, '%') === FALSE) {
1763 $value = "'{$value}%'";
1766 $value = "'%{$value}'";
1770 $value = "'{$value}'";
1772 $sqlOP = $this->getSQLOperator($op);
1773 $clause = "( {$field['dbAlias']} $sqlOP $value )";
1779 $sqlOP = $this->getSQLOperator($op);
1780 $clause = "( {$field['dbAlias']} $sqlOP )";
1784 if ($value !== NULL && strlen($value) > 0) {
1785 if (isset($field['clause'])) {
1786 // FIXME: we not doing escape here. Better solution is to use two
1787 // different types - data-type and filter-type
1788 $clause = $field['clause'];
1791 $value = CRM_Utils_Type
::escape($value, $type);
1792 $sqlOP = $this->getSQLOperator($op);
1793 if ($field['type'] == CRM_Utils_Type
::T_STRING
) {
1794 $value = "'{$value}'";
1796 $clause = "( {$field['dbAlias']} $sqlOP $value )";
1802 if (!empty($field['group']) && $clause) {
1803 $clause = $this->whereGroupClause($field, $value, $op);
1805 elseif (!empty($field['tag']) && $clause) {
1806 // not using left join in query because if any contact
1807 // belongs to more than one tag, results duplicate
1809 $clause = $this->whereTagClause($field, $value, $op);
1811 elseif (!empty($field['membership_org']) && $clause) {
1812 $clause = $this->whereMembershipOrgClause($value, $op);
1814 elseif (!empty($field['membership_type']) && $clause) {
1815 $clause = $this->whereMembershipTypeClause($value, $op);
1821 * Get SQL where clause for a date field.
1823 * @param string $fieldName
1824 * @param string $relative
1825 * @param string $from
1827 * @param string $type
1828 * @param string $fromTime
1829 * @param string $toTime
1831 * @return null|string
1833 public function dateClause(
1835 $relative, $from, $to, $type = NULL, $fromTime = NULL, $toTime = NULL
1838 if (in_array($relative, array_keys($this->getOperationPair(CRM_Report_Form
::OP_DATE
)))) {
1839 $sqlOP = $this->getSQLOperator($relative);
1840 return "( {$fieldName} {$sqlOP} )";
1843 list($from, $to) = $this->getFromTo($relative, $from, $to, $fromTime, $toTime);
1846 $from = ($type == CRM_Utils_Type
::T_DATE
) ?
substr($from, 0, 8) : $from;
1847 $clauses[] = "( {$fieldName} >= $from )";
1851 $to = ($type == CRM_Utils_Type
::T_DATE
) ?
substr($to, 0, 8) : $to;
1852 $clauses[] = "( {$fieldName} <= {$to} )";
1855 if (!empty($clauses)) {
1856 return implode(' AND ', $clauses);
1863 * Possibly unused function.
1865 * @todo - could not find any instances where this is called
1867 * @param bool $relative
1868 * @param string $from
1871 * @return string|NULL
1873 public function dateDisplay($relative, $from, $to) {
1874 list($from, $to) = $this->getFromTo($relative, $from, $to);
1877 $clauses[] = CRM_Utils_Date
::customFormat($from, NULL, array('m', 'M'));
1880 $clauses[] = 'Past';
1884 $clauses[] = CRM_Utils_Date
::customFormat($to, NULL, array('m', 'M'));
1887 $clauses[] = 'Today';
1890 if (!empty($clauses)) {
1891 return implode(' - ', $clauses);
1898 * Get values for from and to for date ranges.
1900 * @param bool $relative
1901 * @param string $from
1903 * @param string $fromTime
1904 * @param string $toTime
1908 public function getFromTo($relative, $from, $to, $fromTime = NULL, $toTime = NULL) {
1909 if (empty($toTime)) {
1912 //FIX ME not working for relative
1914 list($term, $unit) = CRM_Utils_System
::explode('.', $relative, 2);
1915 $dateRange = CRM_Utils_Date
::relativeToAbsolute($term, $unit);
1916 $from = substr($dateRange['from'], 0, 8);
1917 //Take only Date Part, Sometime Time part is also present in 'to'
1918 $to = substr($dateRange['to'], 0, 8);
1920 $from = CRM_Utils_Date
::processDate($from, $fromTime);
1921 $to = CRM_Utils_Date
::processDate($to, $toTime);
1922 return array($from, $to);
1926 * Alter display of rows.
1928 * Iterate through the rows retrieved via SQL and make changes for display purposes,
1929 * such as rendering contacts as links.
1931 * @param array $rows
1932 * Rows generated by SQL, with an array for each row.
1934 public function alterDisplay(&$rows) {
1938 * Alter the way in which custom data fields are displayed.
1940 * @param array $rows
1942 public function alterCustomDataDisplay(&$rows) {
1943 // custom code to alter rows having custom values
1944 if (empty($this->_customGroupExtends
)) {
1948 $customFieldIds = array();
1949 foreach ($this->_params
['fields'] as $fieldAlias => $value) {
1950 if ($fieldId = CRM_Core_BAO_CustomField
::getKeyID($fieldAlias)) {
1951 $customFieldIds[$fieldAlias] = $fieldId;
1954 if (empty($customFieldIds)) {
1958 $customFields = $fieldValueMap = array();
1959 $customFieldCols = array(
1967 // skip for type date and ContactReference since date format is already handled
1969 SELECT cg.table_name, cf." . implode(", cf.", $customFieldCols) . ", ov.value, ov.label
1970 FROM civicrm_custom_field cf
1971 INNER JOIN civicrm_custom_group cg ON cg.id = cf.custom_group_id
1972 LEFT JOIN civicrm_option_value ov ON cf.option_group_id = ov.option_group_id
1973 WHERE cg.extends IN ('" . implode("','", $this->_customGroupExtends
) . "') AND
1974 cg.is_active = 1 AND
1975 cf.is_active = 1 AND
1976 cf.is_searchable = 1 AND
1977 cf.data_type NOT IN ('ContactReference', 'Date') AND
1978 cf.id IN (" . implode(",", $customFieldIds) . ")";
1980 $dao = CRM_Core_DAO
::executeQuery($query);
1981 while ($dao->fetch()) {
1982 foreach ($customFieldCols as $key) {
1983 $customFields[$dao->table_name
. '_custom_' .
1984 $dao->id
][$key] = $dao->$key;
1986 if ($dao->option_group_id
) {
1987 $fieldValueMap[$dao->option_group_id
][$dao->value
] = $dao->label
;
1992 $entryFound = FALSE;
1993 foreach ($rows as $rowNum => $row) {
1994 foreach ($row as $tableCol => $val) {
1995 if (array_key_exists($tableCol, $customFields)) {
1996 $rows[$rowNum][$tableCol] = $this->formatCustomValues($val, $customFields[$tableCol], $fieldValueMap);
2001 // skip looking further in rows, if first row itself doesn't
2002 // have the column we need
2010 * Format custom values.
2012 * @param mixed $value
2013 * @param array $customField
2014 * @param array $fieldValueMap
2016 * @return float|string|void
2018 public function formatCustomValues($value, $customField, $fieldValueMap) {
2019 if (CRM_Utils_System
::isNull($value)) {
2023 $htmlType = $customField['html_type'];
2025 switch ($customField['data_type']) {
2027 if ($value == '1') {
2028 $retValue = ts('Yes');
2031 $retValue = ts('No');
2036 $retValue = CRM_Utils_System
::formatWikiURL($value);
2048 if ($htmlType == 'Text') {
2049 $retValue = (float) $value;
2053 if ($htmlType == 'Text') {
2054 $retValue = CRM_Utils_Money
::format($value, NULL, '%a');
2059 if (in_array($htmlType, array(
2066 case 'StateProvince':
2069 switch ($htmlType) {
2070 case 'Multi-Select Country':
2071 $value = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $value);
2072 $customData = array();
2073 foreach ($value as $val) {
2075 $customData[] = CRM_Core_PseudoConstant
::country($val, FALSE);
2078 $retValue = implode(', ', $customData);
2081 case 'Select Country':
2082 $retValue = CRM_Core_PseudoConstant
::country($value, FALSE);
2085 case 'Select State/Province':
2086 $retValue = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
2089 case 'Multi-Select State/Province':
2090 $value = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $value);
2091 $customData = array();
2092 foreach ($value as $val) {
2094 $customData[] = CRM_Core_PseudoConstant
::stateProvince($val, FALSE);
2097 $retValue = implode(', ', $customData);
2102 case 'Autocomplete-Select':
2103 $retValue = $fieldValueMap[$customField['option_group_id']][$value];
2107 case 'AdvMulti-Select':
2108 case 'Multi-Select':
2109 $value = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $value);
2110 $customData = array();
2111 foreach ($value as $val) {
2113 $customData[] = $fieldValueMap[$customField['option_group_id']][$val];
2116 $retValue = implode(', ', $customData);
2132 * Remove duplicate rows.
2134 * @param array $rows
2136 public function removeDuplicates(&$rows) {
2137 if (empty($this->_noRepeats
)) {
2140 $checkList = array();
2142 foreach ($rows as $key => $list) {
2143 foreach ($list as $colName => $colVal) {
2144 if (array_key_exists($colName, $checkList) &&
2145 $checkList[$colName] == $colVal
2147 $rows[$key][$colName] = "";
2149 if (in_array($colName, $this->_noRepeats
)) {
2150 $checkList[$colName] = $colVal;
2157 * Fix subtotal display.
2160 * @param array $fields
2161 * @param bool $subtotal
2163 public function fixSubTotalDisplay(&$row, $fields, $subtotal = TRUE) {
2164 foreach ($row as $colName => $colVal) {
2165 if (in_array($colName, $fields)) {
2167 elseif (isset($this->_columnHeaders
[$colName])) {
2169 $row[$colName] = "Subtotal";
2173 unset($row[$colName]);
2180 * Calculate grant total.
2182 * @param array $rows
2186 public function grandTotal(&$rows) {
2187 if (!$this->_rollup ||
($this->_rollup
== '') ||
2188 ($this->_limit
&& count($rows) >= self
::ROW_COUNT_LIMIT
)
2192 $lastRow = array_pop($rows);
2194 foreach ($this->_columnHeaders
as $fld => $val) {
2195 if (!in_array($fld, $this->_statFields
)) {
2196 if (!$this->_grandFlag
) {
2197 $lastRow[$fld] = "Grand Total";
2198 $this->_grandFlag
= TRUE;
2201 $lastRow[$fld] = "";
2206 $this->assign('grandStat', $lastRow);
2211 * Format display output.
2213 * @param array $rows
2214 * @param bool $pager
2216 public function formatDisplay(&$rows, $pager = TRUE) {
2217 // set pager based on if any limit was applied in the query.
2222 // allow building charts if any
2223 if (!empty($this->_params
['charts']) && !empty($rows)) {
2224 $this->buildChart($rows);
2225 $this->assign('chartEnabled', TRUE);
2226 $this->_chartId
= "{$this->_params['charts']}_" .
2227 ($this->_id ?
$this->_id
: substr(get_class($this), 16)) . '_' .
2229 $this->assign('chartId', $this->_chartId
);
2232 // unset columns not to be displayed.
2233 foreach ($this->_columnHeaders
as $key => $value) {
2234 if (!empty($value['no_display'])) {
2235 unset($this->_columnHeaders
[$key]);
2239 // unset columns not to be displayed.
2240 if (!empty($rows)) {
2241 foreach ($this->_noDisplay
as $noDisplayField) {
2242 foreach ($rows as $rowNum => $row) {
2243 unset($this->_columnHeaders
[$noDisplayField]);
2248 // build array of section totals
2249 $this->sectionTotals();
2251 // process grand-total row
2252 $this->grandTotal($rows);
2254 // use this method for formatting rows for display purpose.
2255 $this->alterDisplay($rows);
2256 CRM_Utils_Hook
::alterReportVar('rows', $rows, $this);
2258 // use this method for formatting custom rows for display purpose.
2259 $this->alterCustomDataDisplay($rows);
2265 * @param array $rows
2267 public function buildChart(&$rows) {
2268 // override this method for building charts.
2271 // select() method below has been added recently (v3.3), and many of the report templates might
2272 // still be having their own select() method. We should fix them as and when encountered and move
2273 // towards generalizing the select() method below.
2276 * Generate the SELECT clause and set class variable $_select.
2278 public function select() {
2279 $select = $this->_selectAliases
= array();
2281 foreach ($this->_columns
as $tableName => $table) {
2282 if (array_key_exists('fields', $table)) {
2283 foreach ($table['fields'] as $fieldName => $field) {
2284 if ($tableName == 'civicrm_address') {
2285 $this->_addressField
= TRUE;
2287 if ($tableName == 'civicrm_email') {
2288 $this->_emailField
= TRUE;
2290 if ($tableName == 'civicrm_phone') {
2291 $this->_phoneField
= TRUE;
2294 if (!empty($field['required']) ||
2295 !empty($this->_params
['fields'][$fieldName])
2298 // 1. In many cases we want select clause to be built in slightly different way
2299 // for a particular field of a particular type.
2300 // 2. This method when used should receive params by reference and modify $this->_columnHeaders
2302 $selectClause = $this->selectClause($tableName, 'fields', $fieldName, $field);
2303 if ($selectClause) {
2304 $select[] = $selectClause;
2308 // include statistics columns only if set
2309 if (!empty($field['statistics'])) {
2310 foreach ($field['statistics'] as $stat => $label) {
2311 $alias = "{$tableName}_{$fieldName}_{$stat}";
2312 switch (strtolower($stat)) {
2315 $select[] = "$stat({$field['dbAlias']}) as $alias";
2316 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
2317 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
2318 $this->_statFields
[$label] = $alias;
2319 $this->_selectAliases
[] = $alias;
2323 $select[] = "COUNT({$field['dbAlias']}) as $alias";
2324 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
2325 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type
::T_INT
;
2326 $this->_statFields
[$label] = $alias;
2327 $this->_selectAliases
[] = $alias;
2330 case 'count_distinct':
2331 $select[] = "COUNT(DISTINCT {$field['dbAlias']}) as $alias";
2332 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
2333 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type
::T_INT
;
2334 $this->_statFields
[$label] = $alias;
2335 $this->_selectAliases
[] = $alias;
2339 $select[] = "ROUND(AVG({$field['dbAlias']}),2) as $alias";
2340 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
2341 $this->_columnHeaders
["{$tableName}_{$fieldName}_{$stat}"]['type'] = $field['type'];
2342 $this->_statFields
[$label] = $alias;
2343 $this->_selectAliases
[] = $alias;
2349 $alias = "{$tableName}_{$fieldName}";
2350 $select[] = "{$field['dbAlias']} as $alias";
2351 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
2352 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
2353 $this->_selectAliases
[] = $alias;
2359 // select for group bys
2360 if (array_key_exists('group_bys', $table)) {
2361 foreach ($table['group_bys'] as $fieldName => $field) {
2363 if ($tableName == 'civicrm_address') {
2364 $this->_addressField
= TRUE;
2366 if ($tableName == 'civicrm_email') {
2367 $this->_emailField
= TRUE;
2369 if ($tableName == 'civicrm_phone') {
2370 $this->_phoneField
= TRUE;
2372 // 1. In many cases we want select clause to be built in slightly different way
2373 // for a particular field of a particular type.
2374 // 2. This method when used should receive params by reference and modify $this->_columnHeaders
2376 $selectClause = $this->selectClause($tableName, 'group_bys', $fieldName, $field);
2377 if ($selectClause) {
2378 $select[] = $selectClause;
2382 if (!empty($this->_params
['group_bys']) &&
2383 !empty($this->_params
['group_bys'][$fieldName]) &&
2384 !empty($this->_params
['group_bys_freq'])
2386 switch (CRM_Utils_Array
::value($fieldName, $this->_params
['group_bys_freq'])) {
2388 $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL WEEKDAY({$field['dbAlias']}) DAY) AS {$tableName}_{$fieldName}_start";
2389 $select[] = "YEARWEEK({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
2390 $select[] = "WEEKOFYEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
2391 $field['title'] = 'Week';
2395 $select[] = "MAKEDATE(YEAR({$field['dbAlias']}), 1) AS {$tableName}_{$fieldName}_start";
2396 $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
2397 $select[] = "YEAR({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
2398 $field['title'] = 'Year';
2402 $select[] = "DATE_SUB({$field['dbAlias']}, INTERVAL (DAYOFMONTH({$field['dbAlias']})-1) DAY) as {$tableName}_{$fieldName}_start";
2403 $select[] = "MONTH({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
2404 $select[] = "MONTHNAME({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
2405 $field['title'] = 'Month';
2409 $select[] = "STR_TO_DATE(CONCAT( 3 * QUARTER( {$field['dbAlias']} ) -2 , '/', '1', '/', YEAR( {$field['dbAlias']} ) ), '%m/%d/%Y') AS {$tableName}_{$fieldName}_start";
2410 $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_subtotal";
2411 $select[] = "QUARTER({$field['dbAlias']}) AS {$tableName}_{$fieldName}_interval";
2412 $field['title'] = 'Quarter';
2415 // for graphs and charts -
2416 if (!empty($this->_params
['group_bys_freq'][$fieldName])) {
2417 $this->_interval
= $field['title'];
2418 $this->_columnHeaders
["{$tableName}_{$fieldName}_start"]['title']
2419 = $field['title'] . ' Beginning';
2420 $this->_columnHeaders
["{$tableName}_{$fieldName}_start"]['type'] = $field['type'];
2421 $this->_columnHeaders
["{$tableName}_{$fieldName}_start"]['group_by'] = $this->_params
['group_bys_freq'][$fieldName];
2423 // just to make sure these values are transferred to rows.
2424 // since we 'll need them for calculation purpose,
2425 // e.g making subtotals look nicer or graphs
2426 $this->_columnHeaders
["{$tableName}_{$fieldName}_interval"] = array('no_display' => TRUE);
2427 $this->_columnHeaders
["{$tableName}_{$fieldName}_subtotal"] = array('no_display' => TRUE);
2434 $this->_selectClauses
= $select;
2435 $this->_select
= "SELECT " . implode(', ', $select) . " ";
2439 * Build select clause for a single field.
2441 * @param string $tableName
2442 * @param string $tableKey
2443 * @param string $fieldName
2444 * @param string $field
2448 public function selectClause(&$tableName, $tableKey, &$fieldName, &$field) {
2453 * Build where clause.
2455 public function where() {
2456 $this->storeWhereHavingClauseArray();
2458 if (empty($this->_whereClauses
)) {
2459 $this->_where
= "WHERE ( 1 ) ";
2460 $this->_having
= "";
2463 $this->_where
= "WHERE " . implode(' AND ', $this->_whereClauses
);
2466 if ($this->_aclWhere
) {
2467 $this->_where
.= " AND {$this->_aclWhere} ";
2470 if (!empty($this->_havingClauses
)) {
2471 // use this clause to construct group by clause.
2472 $this->_having
= "HAVING " . implode(' AND ', $this->_havingClauses
);
2477 * Store Where clauses into an array.
2479 * Breaking out this step makes over-riding more flexible as the clauses can be used in constructing a
2480 * temp table that may not be part of the final where clause or added
2481 * in other functions
2483 public function storeWhereHavingClauseArray() {
2484 foreach ($this->_columns
as $tableName => $table) {
2485 if (array_key_exists('filters', $table)) {
2486 foreach ($table['filters'] as $fieldName => $field) {
2487 // respect pseudofield to filter spec so fields can be marked as
2488 // not to be handled here
2489 if (!empty($field['pseudofield'])) {
2493 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
2494 if (CRM_Utils_Array
::value('operatorType', $field) ==
2495 CRM_Report_Form
::OP_MONTH
2497 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
2498 $value = CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
);
2499 if (is_array($value) && !empty($value)) {
2501 = "(month({$field['dbAlias']}) $op (" . implode(', ', $value) .
2506 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
2507 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
2508 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
2509 $fromTime = CRM_Utils_Array
::value("{$fieldName}_from_time", $this->_params
);
2510 $toTime = CRM_Utils_Array
::value("{$fieldName}_to_time", $this->_params
);
2511 $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to, $field['type'], $fromTime, $toTime);
2515 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
2517 $clause = $this->whereClause($field,
2519 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
2520 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
2521 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
2526 if (!empty($clause)) {
2527 if (!empty($field['having'])) {
2528 $this->_havingClauses
[] = $clause;
2531 $this->_whereClauses
[] = $clause;
2543 public function processReportMode() {
2544 $buttonName = $this->controller
->getButtonName();
2546 $output = CRM_Utils_Request
::retrieve(
2549 CRM_Core_DAO
::$_nullObject
2553 = CRM_Utils_Request
::retrieve(
2556 CRM_Core_DAO
::$_nullObject
2559 $this->_absoluteUrl
= FALSE;
2561 $this->assign('printOnly', FALSE);
2563 if ($this->_printButtonName
== $buttonName ||
$output == 'print' ||
2564 ($this->_sendmail
&& !$output)
2566 $this->assign('printOnly', TRUE);
2568 $this->assign('outputMode', 'print');
2569 $this->_outputMode
= 'print';
2570 if ($this->_sendmail
) {
2571 $this->_absoluteUrl
= TRUE;
2574 elseif ($this->_pdfButtonName
== $buttonName ||
$output == 'pdf') {
2575 $this->assign('printOnly', TRUE);
2577 $this->assign('outputMode', 'pdf');
2578 $this->_outputMode
= 'pdf';
2579 $this->_absoluteUrl
= TRUE;
2581 elseif ($this->_csvButtonName
== $buttonName ||
$output == 'csv') {
2582 $this->assign('printOnly', TRUE);
2584 $this->assign('outputMode', 'csv');
2585 $this->_outputMode
= 'csv';
2586 $this->_absoluteUrl
= TRUE;
2588 elseif ($this->_groupButtonName
== $buttonName ||
$output == 'group') {
2589 $this->assign('outputMode', 'group');
2590 $this->_outputMode
= 'group';
2592 elseif ($output == 'create_report' && $this->_criteriaForm
) {
2593 $this->assign('outputMode', 'create_report');
2594 $this->_outputMode
= 'create_report';
2597 $this->assign('outputMode', 'html');
2598 $this->_outputMode
= 'html';
2601 // Get today's date to include in printed reports
2603 $reportDate = CRM_Utils_Date
::customFormat(date('Y-m-d H:i'));
2604 $this->assign('reportDate', $reportDate);
2609 * Post Processing function for Form.
2611 * postProcessCommon should be used to set other variables from input as the api accesses that function.
2613 public function beginPostProcess() {
2614 $this->setParams($this->controller
->exportValues($this->_name
));
2616 if (empty($this->_params
) &&
2619 $this->setParams($this->_formValues
);
2622 // hack to fix params when submitted from dashboard, CRM-8532
2623 // fields array is missing because form building etc is skipped
2624 // in dashboard mode for report
2625 //@todo - this could be done in the dashboard no we have a setter
2626 if (empty($this->_params
['fields']) && !$this->_noFields
) {
2627 $this->setParams($this->_formValues
);
2630 $this->_formValues
= $this->_params
;
2631 if (CRM_Core_Permission
::check('administer Reports') &&
2632 isset($this->_id
) &&
2633 ($this->_instanceButtonName
==
2634 $this->controller
->getButtonName() . '_save' ||
2635 $this->_chartButtonName
== $this->controller
->getButtonName()
2638 $this->assign('updateReportButton', TRUE);
2640 $this->processReportMode();
2641 $this->beginPostProcessCommon();
2645 * BeginPostProcess function run in both report mode and non-report mode (api).
2647 public function beginPostProcessCommon() {}
2650 * Build the report query.
2652 * @param bool $applyLimit
2656 public function buildQuery($applyLimit = TRUE) {
2659 $this->customDataFrom();
2664 // order_by columns not selected for display need to be included in SELECT
2665 $unselectedSectionColumns = $this->unselectedSectionColumns();
2666 foreach ($unselectedSectionColumns as $alias => $section) {
2667 $this->_select
.= ", {$section['dbAlias']} as {$alias}";
2670 if ($applyLimit && empty($this->_params
['charts'])) {
2673 CRM_Utils_Hook
::alterReportVar('sql', $this, $this);
2675 $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy} {$this->_limit}";
2680 * Build group by clause.
2682 public function groupBy() {
2683 $groupBys = array();
2684 if (!empty($this->_params
['group_bys']) &&
2685 is_array($this->_params
['group_bys']) &&
2686 !empty($this->_params
['group_bys'])
2688 foreach ($this->_columns
as $tableName => $table) {
2689 if (array_key_exists('group_bys', $table)) {
2690 foreach ($table['group_bys'] as $fieldName => $field) {
2691 if (!empty($this->_params
['group_bys'][$fieldName])) {
2692 $groupBys[] = $field['dbAlias'];
2699 if (!empty($groupBys)) {
2700 $this->_groupBy
= "GROUP BY " . implode(', ', $groupBys);
2705 * Build order by clause.
2707 public function orderBy() {
2708 $this->_orderBy
= "";
2709 $this->_sections
= array();
2710 $this->storeOrderByArray();
2711 if (!empty($this->_orderByArray
) && !$this->_rollup
== 'WITH ROLLUP') {
2712 $this->_orderBy
= "ORDER BY " . implode(', ', $this->_orderByArray
);
2714 $this->assign('sections', $this->_sections
);
2718 * Extract order by fields and store as an array.
2720 * In some cases other functions want to know which fields are selected for ordering by
2721 * Separating this into a separate function allows it to be called separately from constructing
2722 * the order by clause
2724 public function storeOrderByArray() {
2725 $orderBys = array();
2727 if (!empty($this->_params
['order_bys']) &&
2728 is_array($this->_params
['order_bys']) &&
2729 !empty($this->_params
['order_bys'])
2732 // Process order_bys in user-specified order
2733 foreach ($this->_params
['order_bys'] as $orderBy) {
2734 $orderByField = array();
2735 foreach ($this->_columns
as $tableName => $table) {
2736 if (array_key_exists('order_bys', $table)) {
2737 // For DAO columns defined in $this->_columns
2738 $fields = $table['order_bys'];
2740 elseif (array_key_exists('extends', $table)) {
2741 // For custom fields referenced in $this->_customGroupExtends
2742 $fields = CRM_Utils_Array
::value('fields', $table, array());
2747 if (!empty($fields) && is_array($fields)) {
2748 foreach ($fields as $fieldName => $field) {
2749 if ($fieldName == $orderBy['column']) {
2750 $orderByField = array_merge($field, $orderBy);
2751 $orderByField['tplField'] = "{$tableName}_{$fieldName}";
2758 if (!empty($orderByField)) {
2759 $this->_orderByFields
[] = $orderByField;
2760 $orderBys[] = "{$orderByField['dbAlias']} {$orderBy['order']}";
2762 // Record any section headers for assignment to the template
2763 if (!empty($orderBy['section'])) {
2764 $orderByField['pageBreak'] = CRM_Utils_Array
::value('pageBreak', $orderBy);
2765 $this->_sections
[$orderByField['tplField']] = $orderByField;
2771 $this->_orderByArray
= $orderBys;
2773 $this->assign('sections', $this->_sections
);
2777 * Determine unselected columns.
2781 public function unselectedSectionColumns() {
2782 $selectColumns = array();
2783 foreach ($this->_columns
as $tableName => $table) {
2784 if (array_key_exists('fields', $table)) {
2785 foreach ($table['fields'] as $fieldName => $field) {
2786 if (!empty($field['required']) ||
2787 !empty($this->_params
['fields'][$fieldName])
2790 $selectColumns["{$tableName}_{$fieldName}"] = 1;
2796 if (is_array($this->_sections
)) {
2797 return array_diff_key($this->_sections
, $selectColumns);
2805 * Build output rows.
2807 * @param string $sql
2808 * @param array $rows
2810 public function buildRows($sql, &$rows) {
2811 $dao = CRM_Core_DAO
::executeQuery($sql);
2812 if (!is_array($rows)) {
2816 // use this method to modify $this->_columnHeaders
2817 $this->modifyColumnHeaders();
2819 $unselectedSectionColumns = $this->unselectedSectionColumns();
2821 while ($dao->fetch()) {
2823 foreach ($this->_columnHeaders
as $key => $value) {
2824 if (property_exists($dao, $key)) {
2825 $row[$key] = $dao->$key;
2829 // section headers not selected for display need to be added to row
2830 foreach ($unselectedSectionColumns as $key => $values) {
2831 if (property_exists($dao, $key)) {
2832 $row[$key] = $dao->$key;
2841 * Calculate section totals.
2843 * When "order by" fields are marked as sections, this assigns to the template
2844 * an array of total counts for each section. This data is used by the Smarty
2845 * plugin {sectionTotal}.
2847 public function sectionTotals() {
2849 // Reports using order_bys with sections must populate $this->_selectAliases in select() method.
2850 if (empty($this->_selectAliases
)) {
2854 if (!empty($this->_sections
)) {
2855 // build the query with no LIMIT clause
2856 $select = str_ireplace('SELECT SQL_CALC_FOUND_ROWS ', 'SELECT ', $this->_select
);
2857 $sql = "{$select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy}";
2859 // pull section aliases out of $this->_sections
2860 $sectionAliases = array_keys($this->_sections
);
2863 foreach (array_merge($sectionAliases, $this->_selectAliases
) as $alias) {
2864 $ifnulls[] = "ifnull($alias, '') as $alias";
2867 // Group (un-limited) report by all aliases and get counts. This might
2868 // be done more efficiently when the contents of $sql are known, ie. by
2869 // overriding this method in the report class.
2871 $query = "select " . implode(", ", $ifnulls) .
2872 ", count(*) as ct from ($sql) as subquery group by " .
2873 implode(", ", $sectionAliases);
2875 // initialize array of total counts
2877 $dao = CRM_Core_DAO
::executeQuery($query);
2878 while ($dao->fetch()) {
2880 // let $this->_alterDisplay translate any integer ids to human-readable values.
2881 $rows[0] = $dao->toArray();
2882 $this->alterDisplay($rows);
2885 // add totals for all permutations of section values
2888 $aliasCount = count($sectionAliases);
2889 foreach ($sectionAliases as $alias) {
2890 $values[] = $row[$alias];
2891 $key = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $values);
2892 if ($i == $aliasCount) {
2893 // the last alias is the lowest-level section header; use count as-is
2894 $totals[$key] = $dao->ct
;
2897 // other aliases are higher level; roll count into their total
2898 $totals[$key] +
= $dao->ct
;
2902 $this->assign('sectionTotals', $totals);
2907 * Modify column headers.
2909 public function modifyColumnHeaders() {
2910 // use this method to modify $this->_columnHeaders
2914 * Assign rows to the template.
2916 * @param array $rows
2918 public function doTemplateAssignment(&$rows) {
2919 $this->assign_by_ref('columnHeaders', $this->_columnHeaders
);
2920 $this->assign_by_ref('rows', $rows);
2921 $this->assign('statistics', $this->statistics($rows));
2925 * Build report statistics.
2927 * Override this method to build your own statistics.
2929 * @param array $rows
2933 public function statistics(&$rows) {
2934 $statistics = array();
2936 $count = count($rows);
2938 if ($this->_rollup
&& ($this->_rollup
!= '') && $this->_grandFlag
) {
2942 $this->countStat($statistics, $count);
2944 $this->groupByStat($statistics);
2946 $this->filterStat($statistics);
2952 * Add count statistics.
2954 * @param array $statistics
2957 public function countStat(&$statistics, $count) {
2958 $statistics['counts']['rowCount'] = array(
2959 'title' => ts('Row(s) Listed'),
2963 if ($this->_rowsFound
&& ($this->_rowsFound
> $count)) {
2964 $statistics['counts']['rowsFound'] = array(
2965 'title' => ts('Total Row(s)'),
2966 'value' => $this->_rowsFound
,
2972 * Add group by statistics.
2974 * @param array $statistics
2976 public function groupByStat(&$statistics) {
2977 if (!empty($this->_params
['group_bys']) &&
2978 is_array($this->_params
['group_bys']) &&
2979 !empty($this->_params
['group_bys'])
2981 foreach ($this->_columns
as $tableName => $table) {
2982 if (array_key_exists('group_bys', $table)) {
2983 foreach ($table['group_bys'] as $fieldName => $field) {
2984 if (!empty($this->_params
['group_bys'][$fieldName])) {
2985 $combinations[] = $field['title'];
2990 $statistics['groups'][] = array(
2991 'title' => ts('Grouping(s)'),
2992 'value' => implode(' & ', $combinations),
2998 * Filter statistics.
3000 * @param array $statistics
3002 public function filterStat(&$statistics) {
3003 foreach ($this->_columns
as $tableName => $table) {
3004 if (array_key_exists('filters', $table)) {
3005 foreach ($table['filters'] as $fieldName => $field) {
3006 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
&&
3007 CRM_Utils_Array
::value('operatorType', $field) !=
3008 CRM_Report_Form
::OP_MONTH
3012 CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
),
3013 CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
),
3014 CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
),
3015 CRM_Utils_Array
::value("{$fieldName}_from_time", $this->_params
),
3016 CRM_Utils_Array
::value("{$fieldName}_to_time", $this->_params
)
3018 $from_time_format = !empty($this->_params
["{$fieldName}_from_time"]) ?
'h' : 'd';
3019 $from = CRM_Utils_Date
::customFormat($from, NULL, array($from_time_format));
3021 $to_time_format = !empty($this->_params
["{$fieldName}_to_time"]) ?
'h' : 'd';
3022 $to = CRM_Utils_Date
::customFormat($to, NULL, array($to_time_format));
3025 $statistics['filters'][] = array(
3026 'title' => $field['title'],
3027 'value' => ts("Between %1 and %2", array(1 => $from, 2 => $to)),
3030 elseif (in_array($rel = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
),
3031 array_keys($this->getOperationPair(CRM_Report_Form
::OP_DATE
))
3033 $pair = $this->getOperationPair(CRM_Report_Form
::OP_DATE
);
3034 $statistics['filters'][] = array(
3035 'title' => $field['title'],
3036 'value' => $pair[$rel],
3041 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
3044 $pair = $this->getOperationPair(
3045 CRM_Utils_Array
::value('operatorType', $field),
3048 $min = CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
);
3049 $max = CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
);
3050 $val = CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
);
3051 if (in_array($op, array('bw', 'nbw')) && ($min ||
$max)) {
3052 $value = "{$pair[$op]} $min " . ts('and') . " $max";
3054 elseif ($val && CRM_Utils_Array
::value('operatorType', $field) & self
::OP_ENTITYREF
) {
3055 $this->setEntityRefDefaults($field, $tableName);
3056 $result = civicrm_api3($field['attributes']['entity'], 'getlist',
3057 array('id' => $val) +
3058 CRM_Utils_Array
::value('api', $field['attributes'], array()));
3060 foreach ($result['values'] as $v) {
3061 $values[] = $v['label'];
3063 $value = "{$pair[$op]} " . implode(', ', $values);
3065 elseif ($op == 'nll' ||
$op == 'nnll') {
3066 $value = $pair[$op];
3068 elseif (is_array($val) && (!empty($val))) {
3069 $options = CRM_Utils_Array
::value('options', $field, array());
3070 foreach ($val as $key => $valIds) {
3071 if (isset($options[$valIds])) {
3072 $val[$key] = $options[$valIds];
3075 $pair[$op] = (count($val) == 1) ?
(($op == 'notin' ||
$op ==
3076 'mnot') ?
ts('Is Not') : ts('Is')) : CRM_Utils_Array
::value($op, $pair);
3077 $val = implode(', ', $val);
3078 $value = "{$pair[$op]} " . $val;
3080 elseif (!is_array($val) && (!empty($val) ||
$val == '0') &&
3081 isset($field['options']) &&
3082 is_array($field['options']) && !empty($field['options'])
3084 $value = CRM_Utils_Array
::value($op, $pair) . " " .
3085 CRM_Utils_Array
::value($val, $field['options'], $val);
3088 $value = CRM_Utils_Array
::value($op, $pair) . " " . $val;
3092 $statistics['filters'][] = array(
3093 'title' => CRM_Utils_Array
::value('title', $field),
3104 * End post processing.
3106 * @param array|null $rows
3108 public function endPostProcess(&$rows = NULL) {
3109 if ($this->_storeResultSet
) {
3110 $this->_resultSet
= $rows;
3113 if ($this->_outputMode
== 'print' ||
3114 $this->_outputMode
== 'pdf' ||
3118 $content = $this->compileContent();
3119 $url = CRM_Utils_System
::url("civicrm/report/instance/{$this->_id}",
3123 if ($this->_sendmail
) {
3124 $config = CRM_Core_Config
::singleton();
3125 $attachments = array();
3127 if ($this->_outputMode
== 'csv') {
3129 = $this->_formValues
['report_header'] . '<p>' . ts('Report URL') .
3130 ": {$url}</p>" . '<p>' .
3131 ts('The report is attached as a CSV file.') . '</p>' .
3132 $this->_formValues
['report_footer'];
3134 $csvFullFilename = $config->templateCompileDir
.
3135 CRM_Utils_File
::makeFileName('CiviReport.csv');
3136 $csvContent = CRM_Report_Utils_Report
::makeCsv($this, $rows);
3137 file_put_contents($csvFullFilename, $csvContent);
3138 $attachments[] = array(
3139 'fullPath' => $csvFullFilename,
3140 'mime_type' => 'text/csv',
3141 'cleanName' => 'CiviReport.csv',
3144 if ($this->_outputMode
== 'pdf') {
3145 // generate PDF content
3146 $pdfFullFilename = $config->templateCompileDir
.
3147 CRM_Utils_File
::makeFileName('CiviReport.pdf');
3148 file_put_contents($pdfFullFilename,
3149 CRM_Utils_PDF_Utils
::html2pdf($content, "CiviReport.pdf",
3150 TRUE, array('orientation' => 'landscape')
3153 // generate Email Content
3155 = $this->_formValues
['report_header'] . '<p>' . ts('Report URL') .
3156 ": {$url}</p>" . '<p>' .
3157 ts('The report is attached as a PDF file.') . '</p>' .
3158 $this->_formValues
['report_footer'];
3160 $attachments[] = array(
3161 'fullPath' => $pdfFullFilename,
3162 'mime_type' => 'application/pdf',
3163 'cleanName' => 'CiviReport.pdf',
3167 if (CRM_Report_Utils_Report
::mailReport($content, $this->_id
,
3168 $this->_outputMode
, $attachments
3171 CRM_Core_Session
::setStatus(ts("Report mail has been sent."), ts('Sent'), 'success');
3174 CRM_Core_Session
::setStatus(ts("Report mail could not be sent."), ts('Mail Error'), 'error');
3178 elseif ($this->_outputMode
== 'print') {
3182 if ($chartType = CRM_Utils_Array
::value('charts', $this->_params
)) {
3183 $config = CRM_Core_Config
::singleton();
3184 //get chart image name
3185 $chartImg = $this->_chartId
. '.png';
3186 //get image url path
3188 = str_replace('/persist/contribute/', '/persist/', $config->imageUploadURL
) .
3190 $uploadUrl .= $chartImg;
3191 //get image doc path to overwrite
3193 = str_replace('/persist/contribute/', '/persist/', $config->imageUploadDir
) .
3194 'openFlashChart/' . $chartImg;
3196 $chart = imagecreatefrompng($uploadUrl);
3197 //convert it into formatted png
3198 CRM_Utils_System
::setHttpHeader('Content-type', 'image/png');
3199 //overwrite with same image
3200 imagepng($chart, $uploadImg);
3202 imagedestroy($chart);
3204 CRM_Utils_PDF_Utils
::html2pdf($content, "CiviReport.pdf", FALSE, array('orientation' => 'landscape'));
3206 CRM_Utils_System
::civiExit();
3208 elseif ($this->_outputMode
== 'csv') {
3209 CRM_Report_Utils_Report
::export2csv($this, $rows);
3211 elseif ($this->_outputMode
== 'group') {
3212 $group = $this->_params
['groups'];
3213 $this->add2group($group);
3215 elseif ($this->_instanceButtonName
== $this->controller
->getButtonName()) {
3216 CRM_Report_Form_Instance
::postProcess($this);
3218 elseif ($this->_createNewButtonName
== $this->controller
->getButtonName() ||
3219 $this->_outputMode
== 'create_report'
3221 $this->_createNew
= TRUE;
3222 CRM_Report_Form_Instance
::postProcess($this);
3227 * Set store result set indicator to TRUE.
3229 * @todo explain what this does
3231 public function storeResultSet() {
3232 $this->_storeResultSet
= TRUE;
3240 public function getResultSet() {
3241 return $this->_resultSet
;
3245 * Use the form name to create the tpl file name.
3249 public function getTemplateFileName() {
3250 $defaultTpl = parent
::getTemplateFileName();
3251 $template = CRM_Core_Smarty
::singleton();
3252 if (!$template->template_exists($defaultTpl)) {
3253 $defaultTpl = 'CRM/Report/Form.tpl';
3259 * Compile the report content.
3261 * Although this function is super-short it is useful to keep separate so it can be over-ridden by report classes.
3265 public function compileContent() {
3266 $templateFile = $this->getHookedTemplateFileName();
3267 return $this->_formValues
['report_header'] .
3268 CRM_Core_Form
::$_template->fetch($templateFile) .
3269 $this->_formValues
['report_footer'];
3274 * Post process function.
3276 public function postProcess() {
3277 // get ready with post process params
3278 $this->beginPostProcess();
3281 $sql = $this->buildQuery();
3283 // build array of result based on column headers. This method also allows
3284 // modifying column headers before using it to build result set i.e $rows.
3286 $this->buildRows($sql, $rows);
3288 // format result set.
3289 $this->formatDisplay($rows);
3291 // assign variables to templates
3292 $this->doTemplateAssignment($rows);
3294 // do print / pdf / instance stuff if needed
3295 $this->endPostProcess($rows);
3301 * @param int $rowCount
3305 public function limit($rowCount = self
::ROW_COUNT_LIMIT
) {
3306 // lets do the pager if in html mode
3307 $this->_limit
= NULL;
3309 // CRM-14115, over-ride row count if rowCount is specified in URL
3310 if ($this->_dashBoardRowCount
) {
3311 $rowCount = $this->_dashBoardRowCount
;
3313 if ($this->_outputMode
== 'html' ||
$this->_outputMode
== 'group') {
3314 $this->_select
= str_ireplace('SELECT ', 'SELECT SQL_CALC_FOUND_ROWS ', $this->_select
);
3316 $pageId = CRM_Utils_Request
::retrieve('crmPID', 'Integer', CRM_Core_DAO
::$_nullObject);
3318 // @todo all http vars should be extracted in the preProcess
3319 // - not randomly in the class
3320 if (!$pageId && !empty($_POST)) {
3321 if (isset($_POST['PagerBottomButton']) && isset($_POST['crmPID_B'])) {
3322 $pageId = max((int) $_POST['crmPID_B'], 1);
3324 elseif (isset($_POST['PagerTopButton']) && isset($_POST['crmPID'])) {
3325 $pageId = max((int) $_POST['crmPID'], 1);
3327 unset($_POST['crmPID_B'], $_POST['crmPID']);
3330 $pageId = $pageId ?
$pageId : 1;
3331 $this->set(CRM_Utils_Pager
::PAGE_ID
, $pageId);
3332 $offset = ($pageId - 1) * $rowCount;
3334 $offset = CRM_Utils_Type
::escape($offset, 'Int');
3335 $rowCount = CRM_Utils_Type
::escape($rowCount, 'Int');
3337 $this->_limit
= " LIMIT $offset, $rowCount";
3338 return array($offset, $rowCount);
3340 if ($this->_limitValue
) {
3341 if ($this->_offsetValue
) {
3342 $this->_limit
= " LIMIT {$this->_offsetValue}, {$this->_limitValue} ";
3345 $this->_limit
= " LIMIT " . $this->_limitValue
;
3353 * @param int $rowCount
3355 public function setPager($rowCount = self
::ROW_COUNT_LIMIT
) {
3357 // CRM-14115, over-ride row count if rowCount is specified in URL
3358 if ($this->_dashBoardRowCount
) {
3359 $rowCount = $this->_dashBoardRowCount
;
3362 if ($this->_limit
&& ($this->_limit
!= '')) {
3363 $sql = "SELECT FOUND_ROWS();";
3364 $this->_rowsFound
= CRM_Core_DAO
::singleValueQuery($sql);
3366 'total' => $this->_rowsFound
,
3367 'rowCount' => $rowCount,
3368 'status' => ts('Records') . ' %%StatusMessage%%',
3369 'buttonBottom' => 'PagerBottomButton',
3370 'buttonTop' => 'PagerTopButton',
3371 'pageID' => $this->get(CRM_Utils_Pager
::PAGE_ID
),
3374 $pager = new CRM_Utils_Pager($params);
3375 $this->assign_by_ref('pager', $pager);
3376 $this->ajaxResponse
['totalRows'] = $this->_rowsFound
;
3381 * Build where clause for groups.
3383 * @param string $field
3384 * @param mixed $value
3389 public function whereGroupClause($field, $value, $op) {
3391 $smartGroupQuery = "";
3393 $group = new CRM_Contact_DAO_Group();
3394 $group->is_active
= 1;
3396 $smartGroups = array();
3397 while ($group->fetch()) {
3398 if (in_array($group->id
, $this->_params
['gid_value']) &&
3399 $group->saved_search_id
3401 $smartGroups[] = $group->id
;
3405 CRM_Contact_BAO_GroupContactCache
::check($smartGroups);
3407 $smartGroupQuery = '';
3408 if (!empty($smartGroups)) {
3409 $smartGroups = implode(',', $smartGroups);
3410 $smartGroupQuery = " UNION DISTINCT
3411 SELECT DISTINCT smartgroup_contact.contact_id
3412 FROM civicrm_group_contact_cache smartgroup_contact
3413 WHERE smartgroup_contact.group_id IN ({$smartGroups}) ";
3416 $sqlOp = $this->getSQLOperator($op);
3417 if (!is_array($value)) {
3418 $value = array($value);
3420 $clause = "{$field['dbAlias']} IN (" . implode(', ', $value) . ")";
3422 $contactAlias = $this->_aliases
['civicrm_contact'];
3423 if (!empty($this->relationType
) && $this->relationType
== 'b_a') {
3424 $contactAlias = $this->_aliases
['civicrm_contact_b'];
3426 return " {$contactAlias}.id {$sqlOp} (
3427 SELECT DISTINCT {$this->_aliases['civicrm_group']}.contact_id
3428 FROM civicrm_group_contact {$this->_aliases['civicrm_group']}
3429 WHERE {$clause} AND {$this->_aliases['civicrm_group']}.status = 'Added'
3430 {$smartGroupQuery} ) ";
3434 * Build where clause for tags.
3436 * @param string $field
3437 * @param mixed $value
3442 public function whereTagClause($field, $value, $op) {
3443 // not using left join in query because if any contact
3444 // belongs to more than one tag, results duplicate
3446 $sqlOp = $this->getSQLOperator($op);
3447 if (!is_array($value)) {
3448 $value = array($value);
3450 $clause = "{$field['dbAlias']} IN (" . implode(', ', $value) . ")";
3451 $entity_table = $this->_tagFilterTable
;
3452 return " {$this->_aliases[$entity_table]}.id {$sqlOp} (
3453 SELECT DISTINCT {$this->_aliases['civicrm_tag']}.entity_id
3454 FROM civicrm_entity_tag {$this->_aliases['civicrm_tag']}
3455 WHERE entity_table = '$entity_table' AND {$clause} ) ";
3459 * Generate membership organization clause.
3461 * @param mixed $value
3462 * @param string $op SQL Operator
3466 public function whereMembershipOrgClause($value, $op) {
3467 $sqlOp = $this->getSQLOperator($op);
3468 if (!is_array($value)) {
3469 $value = array($value);
3472 $tmp_membership_org_sql_list = implode(', ', $value);
3473 return " {$this->_aliases['civicrm_contact']}.id {$sqlOp} (
3474 SELECT DISTINCT mem.contact_id
3475 FROM civicrm_membership mem
3476 LEFT JOIN civicrm_membership_status mem_status ON mem.status_id = mem_status.id
3477 LEFT JOIN civicrm_membership_type mt ON mem.membership_type_id = mt.id
3478 WHERE mt.member_of_contact_id IN (" .
3479 $tmp_membership_org_sql_list . ")
3480 AND mt.is_active = '1'
3481 AND mem_status.is_current_member = '1'
3482 AND mem_status.is_active = '1' ) ";
3486 * Generate Membership Type SQL Clause.
3488 * @param mixed $value
3494 public function whereMembershipTypeClause($value, $op) {
3495 $sqlOp = $this->getSQLOperator($op);
3496 if (!is_array($value)) {
3497 $value = array($value);
3500 $tmp_membership_sql_list = implode(', ', $value);
3501 return " {$this->_aliases['civicrm_contact']}.id {$sqlOp} (
3502 SELECT DISTINCT mem.contact_id
3503 FROM civicrm_membership mem
3504 LEFT JOIN civicrm_membership_status mem_status ON mem.status_id = mem_status.id
3505 LEFT JOIN civicrm_membership_type mt ON mem.membership_type_id = mt.id
3506 WHERE mem.membership_type_id IN (" .
3507 $tmp_membership_sql_list . ")
3508 AND mt.is_active = '1'
3509 AND mem_status.is_current_member = '1'
3510 AND mem_status.is_active = '1' ) ";
3514 * Build acl clauses.
3516 * @param string $tableAlias
3518 public function buildACLClause($tableAlias = 'contact_a') {
3519 list($this->_aclFrom
, $this->_aclWhere
) = CRM_Contact_BAO_Contact_Permission
::cacheClause($tableAlias);
3523 * Add custom data to the columns.
3525 * @param bool $addFields
3526 * @param array $permCustomGroupIds
3528 public function addCustomDataToColumns($addFields = TRUE, $permCustomGroupIds = array()) {
3529 if (empty($this->_customGroupExtends
)) {
3532 if (!is_array($this->_customGroupExtends
)) {
3533 $this->_customGroupExtends
= array($this->_customGroupExtends
);
3535 $customGroupWhere = '';
3536 if (!empty($permCustomGroupIds)) {
3537 $customGroupWhere = "cg.id IN (" . implode(',', $permCustomGroupIds) .
3541 SELECT cg.table_name, cg.title, cg.extends, cf.id as cf_id, cf.label,
3542 cf.column_name, cf.data_type, cf.html_type, cf.option_group_id, cf.time_format
3543 FROM civicrm_custom_group cg
3544 INNER JOIN civicrm_custom_field cf ON cg.id = cf.custom_group_id
3545 WHERE cg.extends IN ('" . implode("','", $this->_customGroupExtends
) . "') AND
3547 cg.is_active = 1 AND
3548 cf.is_active = 1 AND
3549 cf.is_searchable = 1
3550 ORDER BY cg.weight, cf.weight";
3551 $customDAO = CRM_Core_DAO
::executeQuery($sql);
3554 while ($customDAO->fetch()) {
3555 if ($customDAO->table_name
!= $curTable) {
3556 $curTable = $customDAO->table_name
;
3557 $curFields = $curFilters = array();
3560 $this->_columns
[$curTable]['dao'] = 'CRM_Contact_DAO_Contact';
3561 $this->_columns
[$curTable]['extends'] = $customDAO->extends;
3562 $this->_columns
[$curTable]['grouping'] = $customDAO->table_name
;
3563 $this->_columns
[$curTable]['group_title'] = $customDAO->title
;
3570 if (!array_key_exists($colKey, $this->_columns
[$curTable])) {
3571 $this->_columns
[$curTable][$colKey] = array();
3575 $fieldName = 'custom_' . $customDAO->cf_id
;
3578 // this makes aliasing work in favor
3579 $curFields[$fieldName] = array(
3580 'name' => $customDAO->column_name
,
3581 'title' => $customDAO->label
,
3582 'dataType' => $customDAO->data_type
,
3583 'htmlType' => $customDAO->html_type
,
3586 if ($this->_customGroupFilters
) {
3587 // this makes aliasing work in favor
3588 $curFilters[$fieldName] = array(
3589 'name' => $customDAO->column_name
,
3590 'title' => $customDAO->label
,
3591 'dataType' => $customDAO->data_type
,
3592 'htmlType' => $customDAO->html_type
,
3596 switch ($customDAO->data_type
) {
3599 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form
::OP_DATE
;
3600 $curFilters[$fieldName]['type'] = CRM_Utils_Type
::T_DATE
;
3601 // CRM-6946, show time part for datetime date fields
3602 if ($customDAO->time_format
) {
3603 $curFields[$fieldName]['type'] = CRM_Utils_Type
::T_TIMESTAMP
;
3608 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form
::OP_SELECT
;
3609 $curFilters[$fieldName]['options'] = array(
3610 '' => ts('- select -'),
3614 $curFilters[$fieldName]['type'] = CRM_Utils_Type
::T_INT
;
3618 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form
::OP_INT
;
3619 $curFilters[$fieldName]['type'] = CRM_Utils_Type
::T_INT
;
3623 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form
::OP_FLOAT
;
3624 $curFilters[$fieldName]['type'] = CRM_Utils_Type
::T_MONEY
;
3628 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form
::OP_FLOAT
;
3629 $curFilters[$fieldName]['type'] = CRM_Utils_Type
::T_FLOAT
;
3633 $curFilters[$fieldName]['type'] = CRM_Utils_Type
::T_STRING
;
3635 if (!empty($customDAO->option_group_id
)) {
3636 if (in_array($customDAO->html_type
, array(
3641 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form
::OP_MULTISELECT_SEPARATOR
;
3644 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form
::OP_MULTISELECT
;
3646 if ($this->_customGroupFilters
) {
3647 $curFilters[$fieldName]['options'] = array();
3648 $ogDAO = CRM_Core_DAO
::executeQuery("SELECT ov.value, ov.label FROM civicrm_option_value ov WHERE ov.option_group_id = %1 ORDER BY ov.weight", array(
3650 $customDAO->option_group_id
,
3654 while ($ogDAO->fetch()) {
3655 $curFilters[$fieldName]['options'][$ogDAO->value
] = $ogDAO->label
;
3657 CRM_Utils_Hook
::customFieldOptions($customDAO->cf_id
, $curFilters[$fieldName]['options'], FALSE);
3662 case 'StateProvince':
3663 if (in_array($customDAO->html_type
, array(
3664 'Multi-Select State/Province',
3666 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form
::OP_MULTISELECT_SEPARATOR
;
3669 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form
::OP_MULTISELECT
;
3671 $curFilters[$fieldName]['options'] = CRM_Core_PseudoConstant
::stateProvince();
3675 if (in_array($customDAO->html_type
, array(
3676 'Multi-Select Country',
3678 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form
::OP_MULTISELECT_SEPARATOR
;
3681 $curFilters[$fieldName]['operatorType'] = CRM_Report_Form
::OP_MULTISELECT
;
3683 $curFilters[$fieldName]['options'] = CRM_Core_PseudoConstant
::country();
3686 case 'ContactReference':
3687 $curFilters[$fieldName]['type'] = CRM_Utils_Type
::T_STRING
;
3688 $curFilters[$fieldName]['name'] = 'display_name';
3689 $curFilters[$fieldName]['alias'] = "contact_{$fieldName}_civireport";
3691 $curFields[$fieldName]['type'] = CRM_Utils_Type
::T_STRING
;
3692 $curFields[$fieldName]['name'] = 'display_name';
3693 $curFields[$fieldName]['alias'] = "contact_{$fieldName}_civireport";
3697 $curFields[$fieldName]['type'] = CRM_Utils_Type
::T_STRING
;
3698 $curFilters[$fieldName]['type'] = CRM_Utils_Type
::T_STRING
;
3701 if (!array_key_exists('type', $curFields[$fieldName])) {
3702 $curFields[$fieldName]['type'] = CRM_Utils_Array
::value('type', $curFilters[$fieldName], array());
3706 $this->_columns
[$curTable]['fields'] = array_merge($this->_columns
[$curTable]['fields'], $curFields);
3708 if ($this->_customGroupFilters
) {
3709 $this->_columns
[$curTable]['filters'] = array_merge($this->_columns
[$curTable]['filters'], $curFilters);
3711 if ($this->_customGroupGroupBy
) {
3712 $this->_columns
[$curTable]['group_bys'] = array_merge($this->_columns
[$curTable]['group_bys'], $curFields);
3718 * Build custom data from clause.
3720 public function customDataFrom() {
3721 if (empty($this->_customGroupExtends
)) {
3724 $mapper = CRM_Core_BAO_CustomQuery
::$extendsMap;
3726 foreach ($this->_columns
as $table => $prop) {
3727 if (substr($table, 0, 13) == 'civicrm_value' ||
3728 substr($table, 0, 12) == 'custom_value'
3730 $extendsTable = $mapper[$prop['extends']];
3732 // check field is in params
3733 if (!$this->isFieldSelected($prop)) {
3736 $baseJoin = CRM_Utils_Array
::value($prop['extends'], $this->_customGroupExtendsJoin
, "{$this->_aliases[$extendsTable]}.id");
3738 $customJoin = is_array($this->_customGroupJoin
) ?
$this->_customGroupJoin
[$table] : $this->_customGroupJoin
;
3740 {$customJoin} {$table} {$this->_aliases[$table]} ON {$this->_aliases[$table]}.entity_id = {$baseJoin}";
3741 // handle for ContactReference
3742 if (array_key_exists('fields', $prop)) {
3743 foreach ($prop['fields'] as $fieldName => $field) {
3744 if (CRM_Utils_Array
::value('dataType', $field) ==
3747 $columnName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField', CRM_Core_BAO_CustomField
::getKeyID($fieldName), 'column_name');
3749 LEFT JOIN civicrm_contact {$field['alias']} ON {$field['alias']}.id = {$this->_aliases[$table]}.{$columnName} ";
3758 * Check if the field is selected.
3760 * @param string $prop
3764 public function isFieldSelected($prop) {
3769 if (!empty($this->_params
['fields'])) {
3770 foreach (array_keys($prop['fields']) as $fieldAlias) {
3771 $customFieldId = CRM_Core_BAO_CustomField
::getKeyID($fieldAlias);
3772 if ($customFieldId) {
3773 if (array_key_exists($fieldAlias, $this->_params
['fields'])) {
3777 //might be survey response field.
3778 if (!empty($this->_params
['fields']['survey_response']) &&
3779 !empty($prop['fields'][$fieldAlias]['isSurveyResponseField'])
3787 if (!empty($this->_params
['group_bys']) && $this->_customGroupGroupBy
) {
3788 foreach (array_keys($prop['group_bys']) as $fieldAlias) {
3789 if (array_key_exists($fieldAlias, $this->_params
['group_bys']) &&
3790 CRM_Core_BAO_CustomField
::getKeyID($fieldAlias)
3797 if (!empty($this->_params
['order_bys'])) {
3798 foreach (array_keys($prop['fields']) as $fieldAlias) {
3799 foreach ($this->_params
['order_bys'] as $orderBy) {
3800 if ($fieldAlias == $orderBy['column'] &&
3801 CRM_Core_BAO_CustomField
::getKeyID($fieldAlias)
3809 if (!empty($prop['filters']) && $this->_customGroupFilters
) {
3810 foreach ($prop['filters'] as $fieldAlias => $val) {
3819 if (isset($this->_params
[$fieldAlias . '_' . $attach]) &&
3820 (!empty($this->_params
[$fieldAlias . '_' . $attach])
3821 ||
($attach != 'relative' &&
3822 $this->_params
[$fieldAlias . '_' . $attach] == '0')
3828 if (!empty($this->_params
[$fieldAlias . '_op']) &&
3829 in_array($this->_params
[$fieldAlias . '_op'], array('nll', 'nnll'))
3840 * Check for empty order_by configurations and remove them; also set
3841 * template to hide them.
3843 * @param array $formValues
3845 public function preProcessOrderBy(&$formValues) {
3846 // Object to show/hide form elements
3847 $_showHide = new CRM_Core_ShowHideBlocks('', '');
3849 $_showHide->addShow('optionField_1');
3851 // Cycle through order_by options; skip any empty ones, and hide them as well
3854 if (!empty($formValues['order_bys'])) {
3855 foreach ($formValues['order_bys'] as $order_by) {
3856 if ($order_by['column'] && $order_by['column'] != '-') {
3857 $_showHide->addShow('optionField_' . $n);
3858 $orderBys[$n] = $order_by;
3863 for ($i = $n; $i <= 5; $i++
) {
3865 $_showHide->addHide('optionField_' . $i);
3869 // overwrite order_by options with modified values
3870 if (!empty($orderBys)) {
3871 $formValues['order_bys'] = $orderBys;
3874 $formValues['order_bys'] = array(1 => array('column' => '-'));
3877 // assign show/hide data to template
3878 $_showHide->addToTemplate();
3882 * Check if table name has columns in SELECT clause.
3884 * @param string $tableName
3885 * Name of table (index of $this->_columns array).
3889 public function isTableSelected($tableName) {
3890 return in_array($tableName, $this->selectedTables());
3894 * Fetch array of DAO tables having columns included in SELECT or ORDER BY clause.
3896 * If the array is unset it will be built.
3901 public function selectedTables() {
3902 if (!$this->_selectedTables
) {
3903 $orderByColumns = array();
3904 if (array_key_exists('order_bys', $this->_params
) &&
3905 is_array($this->_params
['order_bys'])
3907 foreach ($this->_params
['order_bys'] as $orderBy) {
3908 $orderByColumns[] = $orderBy['column'];
3912 foreach ($this->_columns
as $tableName => $table) {
3913 if (array_key_exists('fields', $table)) {
3914 foreach ($table['fields'] as $fieldName => $field) {
3915 if (!empty($field['required']) ||
3916 !empty($this->_params
['fields'][$fieldName])
3918 $this->_selectedTables
[] = $tableName;
3923 if (array_key_exists('order_bys', $table)) {
3924 foreach ($table['order_bys'] as $orderByName => $orderBy) {
3925 if (in_array($orderByName, $orderByColumns)) {
3926 $this->_selectedTables
[] = $tableName;
3931 if (array_key_exists('filters', $table)) {
3932 foreach ($table['filters'] as $filterName => $filter) {
3933 if (!empty($this->_params
["{$filterName}_value"]) ||
3934 CRM_Utils_Array
::value("{$filterName}_op", $this->_params
) ==
3936 CRM_Utils_Array
::value("{$filterName}_op", $this->_params
) ==
3939 $this->_selectedTables
[] = $tableName;
3946 return $this->_selectedTables
;
3950 * Add address fields.
3952 * @deprecated - use getAddressColumns which is a more accurate description
3953 * and also accepts an array of options rather than a long list
3955 * adding address fields to construct function in reports
3957 * @param bool $groupBy
3958 * Add GroupBy? Not appropriate for detail report.
3959 * @param bool $orderBy
3960 * Add GroupBy? Not appropriate for detail report.
3961 * @param bool $filters
3962 * @param array $defaults
3965 * address fields for construct clause
3967 public function addAddressFields($groupBy = TRUE, $orderBy = FALSE, $filters = TRUE, $defaults = array('country_id' => TRUE)) {
3968 $addressFields = array(
3969 'civicrm_address' => array(
3970 'dao' => 'CRM_Core_DAO_Address',
3973 'title' => ts('Address Name'),
3974 'default' => CRM_Utils_Array
::value('name', $defaults, FALSE),
3976 'street_address' => array(
3977 'title' => ts('Street Address'),
3978 'default' => CRM_Utils_Array
::value('street_address', $defaults, FALSE),
3980 'supplemental_address_1' => array(
3981 'title' => ts('Supplementary Address Field 1'),
3982 'default' => CRM_Utils_Array
::value('supplemental_address_1', $defaults, FALSE),
3984 'supplemental_address_2' => array(
3985 'title' => ts('Supplementary Address Field 2'),
3986 'default' => CRM_Utils_Array
::value('supplemental_address_2', $defaults, FALSE),
3988 'street_number' => array(
3989 'name' => 'street_number',
3990 'title' => ts('Street Number'),
3992 'default' => CRM_Utils_Array
::value('street_number', $defaults, FALSE),
3994 'street_name' => array(
3995 'name' => 'street_name',
3996 'title' => ts('Street Name'),
3998 'default' => CRM_Utils_Array
::value('street_name', $defaults, FALSE),
4000 'street_unit' => array(
4001 'name' => 'street_unit',
4002 'title' => ts('Street Unit'),
4004 'default' => CRM_Utils_Array
::value('street_unit', $defaults, FALSE),
4007 'title' => ts('City'),
4008 'default' => CRM_Utils_Array
::value('city', $defaults, FALSE),
4010 'postal_code' => array(
4011 'title' => ts('Postal Code'),
4012 'default' => CRM_Utils_Array
::value('postal_code', $defaults, FALSE),
4014 'postal_code_suffix' => array(
4015 'title' => ts('Postal Code Suffix'),
4016 'default' => CRM_Utils_Array
::value('postal_code_suffix', $defaults, FALSE),
4018 'country_id' => array(
4019 'title' => ts('Country'),
4020 'default' => CRM_Utils_Array
::value('country_id', $defaults, FALSE),
4022 'state_province_id' => array(
4023 'title' => ts('State/Province'),
4024 'default' => CRM_Utils_Array
::value('state_province_id', $defaults, FALSE),
4026 'county_id' => array(
4027 'title' => ts('County'),
4028 'default' => CRM_Utils_Array
::value('county_id', $defaults, FALSE),
4031 'grouping' => 'location-fields',
4036 $addressFields['civicrm_address']['filters'] = array(
4037 'street_number' => array(
4038 'title' => ts('Street Number'),
4040 'name' => 'street_number',
4042 'street_name' => array(
4043 'title' => ts('Street Name'),
4044 'name' => 'street_name',
4045 'operator' => 'like',
4047 'postal_code' => array(
4048 'title' => ts('Postal Code'),
4050 'name' => 'postal_code',
4053 'title' => ts('City'),
4054 'operator' => 'like',
4057 'country_id' => array(
4058 'name' => 'country_id',
4059 'title' => ts('Country'),
4060 'type' => CRM_Utils_Type
::T_INT
,
4061 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
4062 'options' => CRM_Core_PseudoConstant
::country(),
4064 'state_province_id' => array(
4065 'name' => 'state_province_id',
4066 'title' => ts('State/Province'),
4067 'type' => CRM_Utils_Type
::T_INT
,
4068 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
4069 'options' => array(),
4071 'county_id' => array(
4072 'name' => 'county_id',
4073 'title' => ts('County'),
4074 'type' => CRM_Utils_Type
::T_INT
,
4075 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
4076 'options' => array(),
4082 $addressFields['civicrm_address']['order_bys'] = array(
4083 'street_name' => array('title' => ts('Street Name')),
4084 'street_number' => array('title' => 'Odd / Even Street Number'),
4085 'street_address' => NULL,
4087 'postal_code' => NULL,
4092 $addressFields['civicrm_address']['group_bys'] = array(
4093 'street_address' => NULL,
4095 'postal_code' => NULL,
4096 'state_province_id' => array(
4097 'title' => ts('State/Province'),
4099 'country_id' => array(
4100 'title' => ts('Country'),
4102 'county_id' => array(
4103 'title' => ts('County'),
4107 return $addressFields;
4111 * Do AlterDisplay processing on Address Fields.
4114 * @param array $rows
4115 * @param int $rowNum
4116 * @param string $baseUrl
4117 * @param string $urltxt
4121 public function alterDisplayAddressFields(&$row, &$rows, &$rowNum, $baseUrl, $urltxt) {
4122 $criteriaQueryParams = CRM_Report_Utils_Report
::getPreviewCriteriaQueryParams($this->_defaults
, $this->_params
);
4123 $entryFound = FALSE;
4125 if (array_key_exists('civicrm_address_country_id', $row)) {
4126 if ($value = $row['civicrm_address_country_id']) {
4127 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value, FALSE);
4128 $url = CRM_Report_Utils_Report
::getNextUrl($baseUrl,
4129 "reset=1&force=1&{$criteriaQueryParams}&" .
4130 "country_id_op=in&country_id_value={$value}",
4131 $this->_absoluteUrl
, $this->_id
4133 $rows[$rowNum]['civicrm_address_country_id_link'] = $url;
4134 $rows[$rowNum]['civicrm_address_country_id_hover'] = ts("%1 for this country.",
4141 if (array_key_exists('civicrm_address_county_id', $row)) {
4142 if ($value = $row['civicrm_address_county_id']) {
4143 $rows[$rowNum]['civicrm_address_county_id'] = CRM_Core_PseudoConstant
::county($value, FALSE);
4144 $url = CRM_Report_Utils_Report
::getNextUrl($baseUrl,
4145 "reset=1&force=1&{$criteriaQueryParams}&" .
4146 "county_id_op=in&county_id_value={$value}",
4147 $this->_absoluteUrl
, $this->_id
4149 $rows[$rowNum]['civicrm_address_county_id_link'] = $url;
4150 $rows[$rowNum]['civicrm_address_county_id_hover'] = ts("%1 for this county.",
4156 // handle state province
4157 if (array_key_exists('civicrm_address_state_province_id', $row)) {
4158 if ($value = $row['civicrm_address_state_province_id']) {
4159 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value, FALSE);
4161 $url = CRM_Report_Utils_Report
::getNextUrl($baseUrl,
4162 "reset=1&force=1&{$criteriaQueryParams}&state_province_id_op=in&state_province_id_value={$value}",
4163 $this->_absoluteUrl
, $this->_id
4165 $rows[$rowNum]['civicrm_address_state_province_id_link'] = $url;
4166 $rows[$rowNum]['civicrm_address_state_province_id_hover'] = ts("%1 for this state.",
4177 * Adjusts dates passed in to YEAR() for fiscal year.
4179 * @param string $fieldName
4183 public function fiscalYearOffset($fieldName) {
4184 $config = CRM_Core_Config
::singleton();
4185 $fy = $config->fiscalYearStart
;
4186 if (CRM_Utils_Array
::value('yid_op', $this->_params
) == 'calendar' ||
4187 ($fy['d'] == 1 && $fy['M'] == 1)
4189 return "YEAR( $fieldName )";
4191 return "YEAR( $fieldName - INTERVAL " . ($fy['M'] - 1) . " MONTH" .
4192 ($fy['d'] > 1 ?
(" - INTERVAL " . ($fy['d'] - 1) . " DAY") : '') . " )";
4196 * Add Address into From Table if required.
4198 public function addAddressFromClause() {
4199 // include address field if address column is to be included
4200 if ((isset($this->_addressField
) &&
4201 $this->_addressField
4203 $this->isTableSelected('civicrm_address')
4206 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
4207 ON ({$this->_aliases['civicrm_contact']}.id =
4208 {$this->_aliases['civicrm_address']}.contact_id) AND
4209 {$this->_aliases['civicrm_address']}.is_primary = 1\n";
4214 * Add Phone into From Table if required.
4216 public function addPhoneFromClause() {
4217 // include address field if address column is to be included
4218 if ($this->isTableSelected('civicrm_phone')
4221 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
4222 ON ({$this->_aliases['civicrm_contact']}.id =
4223 {$this->_aliases['civicrm_phone']}.contact_id) AND
4224 {$this->_aliases['civicrm_phone']}.is_primary = 1\n";
4229 * Get phone columns to add to array.
4231 * @param array $options
4232 * - prefix Prefix to add to table (in case of more than one instance of the table)
4233 * - prefix_label Label to give columns from this phone table instance
4236 * phone columns definition
4238 public function getPhoneColumns($options = array()) {
4239 $defaultOptions = array(
4241 'prefix_label' => '',
4244 $options = array_merge($defaultOptions, $options);
4247 $options['prefix'] . 'civicrm_phone' => array(
4248 'dao' => 'CRM_Core_DAO_Phone',
4250 $options['prefix'] . 'phone' => array(
4251 'title' => ts($options['prefix_label'] . 'Phone'),
4261 * Get address columns to add to array.
4263 * @param array $options
4264 * - prefix Prefix to add to table (in case of more than one instance of the table)
4265 * - prefix_label Label to give columns from this address table instance
4268 * address columns definition
4270 public function getAddressColumns($options = array()) {
4273 'prefix_label' => '',
4277 'defaults' => array(),
4279 return $this->addAddressFields(
4280 $options['group_by'],
4281 $options['order_by'],
4282 $options['filters'],
4283 $options['defaults']
4288 * Add contact to group.
4290 * @param int $groupID
4292 public function add2group($groupID) {
4293 if (is_numeric($groupID) && isset($this->_aliases
['civicrm_contact'])) {
4294 $select = "SELECT DISTINCT {$this->_aliases['civicrm_contact']}.id AS addtogroup_contact_id, ";
4295 $select = str_ireplace('SELECT SQL_CALC_FOUND_ROWS ', $select, $this->_select
);
4297 $sql = "{$select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy}";
4298 $sql = str_replace('WITH ROLLUP', '', $sql);
4299 $dao = CRM_Core_DAO
::executeQuery($sql);
4301 $contact_ids = array();
4302 // Add resulting contacts to group
4303 while ($dao->fetch()) {
4304 if ($dao->addtogroup_contact_id
) {
4305 $contact_ids[$dao->addtogroup_contact_id
] = $dao->addtogroup_contact_id
;
4309 if (!empty($contact_ids)) {
4310 CRM_Contact_BAO_GroupContact
::addContactsToGroup($contact_ids, $groupID);
4311 CRM_Core_Session
::setStatus(ts("Listed contact(s) have been added to the selected group."), ts('Contacts Added'), 'success');
4314 CRM_Core_Session
::setStatus(ts("The listed records(s) cannot be added to the group."));
4320 * Show charts on print screen.
4322 public static function uploadChartImage() {
4323 // upload strictly for '.png' images
4324 $name = trim(basename(CRM_Utils_Request
::retrieve('name', 'String', CRM_Core_DAO
::$_nullObject, FALSE, NULL, 'GET')));
4325 if (preg_match('/\.png$/', $name)) {
4327 // POST data is usually string data, but we are passing a RAW .png
4328 // so PHP is a bit confused and $_POST is empty. But it has saved
4329 // the raw bits into $HTTP_RAW_POST_DATA
4331 $httpRawPostData = $GLOBALS['HTTP_RAW_POST_DATA'];
4333 // prepare the directory
4334 $config = CRM_Core_Config
::singleton();
4336 = str_replace('/persist/contribute/', '/persist/', $config->imageUploadDir
) .
4338 if (!file_exists($defaultPath)) {
4339 mkdir($defaultPath, 0777, TRUE);
4342 // full path to the saved image including filename
4343 $destination = $defaultPath . $name;
4346 $jfh = fopen($destination, 'w') or die("can't open file");
4347 fwrite($jfh, $httpRawPostData);
4349 CRM_Utils_System
::civiExit();
4354 * Apply common settings to entityRef fields.
4356 * @param array $field
4357 * @param string $table
4359 private function setEntityRefDefaults(&$field, $table) {
4360 $field['attributes'] = $field['attributes'] ?
$field['attributes'] : array();
4361 $field['attributes'] +
= array(
4362 'entity' => CRM_Core_DAO_AllCoreTables
::getBriefName(CRM_Core_DAO_AllCoreTables
::getClassForTable($table)),
4364 'placeholder' => ts('- select -'),