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