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