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