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