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