4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.6 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2014
36 class CRM_Report_Utils_Report
{
39 * @param int $instanceID
43 public static function getValueFromUrl($instanceID = NULL) {
45 $optionVal = CRM_Core_DAO
::getFieldValue('CRM_Report_DAO_ReportInstance',
51 $config = CRM_Core_Config
::singleton();
52 $args = explode('/', $_GET[$config->userFrameworkURLVar
]);
54 // remove 'civicrm/report' from args
58 // put rest of arguement back in the form of url, which is how value
59 // is stored in option value table
60 $optionVal = implode('/', $args);
66 * @param int $instanceID
70 public static function getValueIDFromUrl($instanceID = NULL) {
71 $optionVal = self
::getValueFromUrl($instanceID);
74 $templateInfo = CRM_Core_OptionGroup
::getRowValues('report_template', "{$optionVal}", 'value');
75 return array(CRM_Utils_Array
::value('id', $templateInfo), $optionVal);
86 public static function getInstanceIDForValue($optionVal) {
87 static $valId = array();
89 if (!array_key_exists($optionVal, $valId)) {
91 SELECT MIN(id) FROM civicrm_report_instance
92 WHERE report_id = %1";
94 $params = array(1 => array($optionVal, 'String'));
95 $valId[$optionVal] = CRM_Core_DAO
::singleValueQuery($sql, $params);
97 return $valId[$optionVal];
105 public static function getInstanceIDForPath($path = NULL) {
106 static $valId = array();
108 // if $path is null, try to get it from url
109 $path = self
::getInstancePath();
111 if ($path && !array_key_exists($path, $valId)) {
113 SELECT MIN(id) FROM civicrm_report_instance
114 WHERE TRIM(BOTH '/' FROM CONCAT(report_id, '/', name)) = %1";
116 $params = array(1 => array($path, 'String'));
117 $valId[$path] = CRM_Core_DAO
::singleValueQuery($sql, $params);
119 return CRM_Utils_Array
::value($path, $valId);
124 * @param string $query
125 * @param bool $absolute
126 * @param int $instanceID
127 * @param array $drilldownReport
129 * @return bool|string
131 public static function getNextUrl($urlValue, $query = 'reset=1', $absolute = FALSE, $instanceID = NULL, $drilldownReport = array()) {
133 $drilldownInstanceID = FALSE;
134 if (array_key_exists($urlValue, $drilldownReport)) {
135 $drilldownInstanceID = CRM_Core_DAO
::getFieldValue('CRM_Report_DAO_ReportInstance', $instanceID, 'drilldown_id', 'id');
138 if (!$drilldownInstanceID) {
139 $drilldownInstanceID = self
::getInstanceIDForValue($urlValue);
142 if ($drilldownInstanceID) {
143 return CRM_Utils_System
::url("civicrm/report/instance/{$drilldownInstanceID}",
144 "{$query}", $absolute
152 return CRM_Utils_System
::url("civicrm/report/" . trim($urlValue, '/'),
158 // get instance count for a template
162 * @return int|null|string
164 public static function getInstanceCount($optionVal) {
165 if (empty($optionVal)) {
170 SELECT count(inst.id)
171 FROM civicrm_report_instance inst
172 WHERE inst.report_id = %1";
174 $params = array(1 => array($optionVal, 'String'));
175 $count = CRM_Core_DAO
::singleValueQuery($sql, $params);
180 * @param $fileContent
181 * @param int $instanceID
182 * @param string $outputMode
183 * @param array $attachments
187 public static function mailReport($fileContent, $instanceID = NULL, $outputMode = 'html', $attachments = array()) {
192 list($domainEmailName,
194 ) = CRM_Core_BAO_Domain
::getNameAndEmail();
196 $params = array('id' => $instanceID);
197 $instanceInfo = array();
198 CRM_Core_DAO
::commonRetrieve('CRM_Report_DAO_ReportInstance',
204 $params['groupName'] = 'Report Email Sender';
205 $params['from'] = '"' . $domainEmailName . '" <' . $domainEmailAddress . '>';
207 $params['toName'] = "";
208 $params['toEmail'] = CRM_Utils_Array
::value('email_to', $instanceInfo);
209 $params['cc'] = CRM_Utils_Array
::value('email_cc', $instanceInfo);
210 $params['subject'] = CRM_Utils_Array
::value('email_subject', $instanceInfo);
211 if (empty($instanceInfo['attachments'])) {
212 $instanceInfo['attachments'] = array();
214 $params['attachments'] = array_merge(CRM_Utils_Array
::value('attachments', $instanceInfo), $attachments);
215 $params['text'] = '';
216 $params['html'] = $fileContent;
218 return CRM_Utils_Mail
::send($params);
222 * @param CRM_Core_Form $form
225 public static function export2csv(&$form, &$rows) {
226 //Mark as a CSV file.
227 header('Content-Type: text/csv');
229 //Force a download and name the file using the current timestamp.
230 $datetime = date('Ymd-Gi', $_SERVER['REQUEST_TIME']);
231 header('Content-Disposition: attachment; filename=Report_' . $datetime . '.csv');
232 echo self
::makeCsv($form, $rows);
233 CRM_Utils_System
::civiExit();
237 * Utility function for export2csv and CRM_Report_Form::endPostProcess
238 * - make CSV file content and return as string.
240 public static function makeCsv(&$form, &$rows) {
241 $config = CRM_Core_Config
::singleton();
244 // Add headers if this is the first row.
245 $columnHeaders = array_keys($form->_columnHeaders
);
247 // Replace internal header names with friendly ones, where available.
248 foreach ($columnHeaders as $header) {
249 if (isset($form->_columnHeaders
[$header])) {
250 $headers[] = '"' . html_entity_decode(strip_tags($form->_columnHeaders
[$header]['title'])) . '"';
254 $csv .= implode($config->fieldSeparator
,
258 $displayRows = array();
260 foreach ($rows as $row) {
261 foreach ($columnHeaders as $k => $v) {
262 $value = CRM_Utils_Array
::value($v, $row);
264 // Remove HTML, unencode entities, and escape quotation marks.
265 $value = str_replace('"', '""', html_entity_decode(strip_tags($value)));
267 if (CRM_Utils_Array
::value('type', $form->_columnHeaders
[$v]) & 4) {
268 if (CRM_Utils_Array
::value('group_by', $form->_columnHeaders
[$v]) == 'MONTH' ||
269 CRM_Utils_Array
::value('group_by', $form->_columnHeaders
[$v]) == 'QUARTER'
271 $value = CRM_Utils_Date
::customFormat($value, $config->dateformatPartial
);
273 elseif (CRM_Utils_Array
::value('group_by', $form->_columnHeaders
[$v]) == 'YEAR') {
274 $value = CRM_Utils_Date
::customFormat($value, $config->dateformatYear
);
277 $value = CRM_Utils_Date
::customFormat($value, '%Y-%m-%d');
280 elseif (CRM_Utils_Array
::value('type', $form->_columnHeaders
[$v]) == 1024) {
281 $value = CRM_Utils_Money
::format($value);
283 $displayRows[$v] = '"' . $value . '"';
286 $displayRows[$v] = " ";
290 $csv .= implode($config->fieldSeparator
,
301 public static function getInstanceID() {
303 $config = CRM_Core_Config
::singleton();
304 $arg = explode('/', $_GET[$config->userFrameworkURLVar
]);
306 if ($arg[1] == 'report' &&
307 CRM_Utils_Array
::value(2, $arg) == 'instance'
309 if (CRM_Utils_Rule
::positiveInteger($arg[3])) {
318 public static function getInstancePath() {
319 $config = CRM_Core_Config
::singleton();
320 $arg = explode('/', $_GET[$config->userFrameworkURLVar
]);
322 if ($arg[1] == 'report' &&
323 CRM_Utils_Array
::value(2, $arg) == 'instance'
325 unset($arg[0], $arg[1], $arg[2]);
326 $path = trim(CRM_Utils_Type
::escape(implode('/', $arg), 'String'), '/');
332 * @param int $instanceId
336 public static function isInstancePermissioned($instanceId) {
341 $instanceValues = array();
342 $params = array('id' => $instanceId);
343 CRM_Core_DAO
::commonRetrieve('CRM_Report_DAO_ReportInstance',
348 if (!empty($instanceValues['permission']) &&
349 (!(CRM_Core_Permission
::check($instanceValues['permission']) ||
350 CRM_Core_Permission
::check('administer Reports')
360 * Check if the user can view a report instance based on their role(s)
362 * @instanceId string $str the report instance to check
364 * @param int $instanceId
366 * @return boolean true if yes, else false
369 public static function isInstanceGroupRoleAllowed($instanceId) {
374 $instanceValues = array();
375 $params = array('id' => $instanceId);
376 CRM_Core_DAO
::commonRetrieve('CRM_Report_DAO_ReportInstance',
380 //transform grouprole to array
381 if (!empty($instanceValues['grouprole'])) {
382 $grouprole_array = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
383 $instanceValues['grouprole']
385 if (!CRM_Core_Permission
::checkGroupRole($grouprole_array) &&
386 !CRM_Core_Permission
::check('administer Reports')
395 * @param array $params
399 public static function processReport($params) {
400 $instanceId = CRM_Utils_Array
::value('instanceId', $params);
402 // hack for now, CRM-8358
403 $_REQUEST['instanceId'] = $instanceId;
404 $_REQUEST['sendmail'] = CRM_Utils_Array
::value('sendmail', $params, 1);
406 // if cron is run from terminal --output is reserved, and therefore we would provide another name 'format'
407 $_REQUEST['output'] = CRM_Utils_Array
::value('format', $params, CRM_Utils_Array
::value('output', $params, 'pdf'));
408 $_REQUEST['reset'] = CRM_Utils_Array
::value('reset', $params, 1);
410 $optionVal = self
::getValueFromUrl($instanceId);
411 $messages = array("Report Mail Triggered...");
413 $templateInfo = CRM_Core_OptionGroup
::getRowValues('report_template', $optionVal, 'value');
414 $obj = new CRM_Report_Page_Instance();
416 if (strstr(CRM_Utils_Array
::value('name', $templateInfo), '_Form')) {
417 $instanceInfo = array();
418 CRM_Report_BAO_ReportInstance
::retrieve(array('id' => $instanceId), $instanceInfo);
420 if (!empty($instanceInfo['title'])) {
421 $obj->assign('reportTitle', $instanceInfo['title']);
424 $obj->assign('reportTitle', $templateInfo['label']);
427 $wrapper = new CRM_Utils_Wrapper();
429 'urlToSession' => array(
431 'urlVar' => 'instanceId',
432 'type' => 'Positive',
433 'sessionVar' => 'instanceId',
439 $messages[] = $wrapper->run($templateInfo['name'], NULL, $arguments);
444 $messages[] = 'Required parameter missing: instanceId';
447 $messages[] = 'Did not find valid instance to execute';
452 'is_error' => $is_error,
453 'messages' => implode("\n", $messages),
459 * Build a URL query string containing all report filter criteria that are
460 * stipulated in $_GET or in a report Preview, but which haven't yet been
461 * saved in the report instance.
463 * @param array $defaults
464 * The report criteria that aren't coming in as submitted form values, as in CRM_Report_Form::_defaults.
465 * @param array $params
466 * All effective report criteria, as in CRM_Report_Form::_params.
468 * @return string URL query string
470 public static function getPreviewCriteriaQueryParams($defaults = array(), $params = array()) {
471 static $query_string;
472 if (!isset($query_string)) {
473 if (!empty($params)) {
474 $url_params = $op_values = $string_values = $process_params = array();
476 // We'll only use $params that are different from what's in $default.
477 foreach ($params as $field_name => $field_value) {
478 if (!array_key_exists($field_name, $defaults) ||
$defaults[$field_name] != $field_value) {
479 $process_params[$field_name] = $field_value;
482 // Criteria stipulated in $_GET will be in $defaults even if they're not
483 // saved, so we can't easily tell if they're saved or not. So just include them.
484 $process_params +
= $_GET;
486 // All $process_params should be passed on if they have an effective value
487 // (in other words, there's no point in propagating blank filters).
488 foreach ($process_params as $field_name => $field_value) {
489 $suffix_position = strrpos($field_name, '_');
490 $suffix = substr($field_name, $suffix_position);
491 $basename = substr($field_name, 0, $suffix_position);
492 if ($suffix == '_min' ||
$suffix == '_max' ||
493 $suffix == '_from' ||
$suffix == '_to' ||
494 $suffix == '_relative'
496 // For these types, we only keep them if they have a value.
497 if (!empty($field_value)) {
498 $url_params[$field_name] = $field_value;
501 elseif ($suffix == '_value') {
502 // These filters can have an effect even without a value
503 // (e.g., values for 'nll' and 'nnll' ops are blank),
504 // so store them temporarily and examine below.
505 $string_values[$basename] = $field_value;
506 $op_values[$basename] = CRM_Utils_Array
::value("{$basename}_op", $params);
508 elseif ($suffix == '_op') {
509 // These filters can have an effect even without a value
510 // (e.g., values for 'nll' and 'nnll' ops are blank),
511 // so store them temporarily and examine below.
512 $op_values[$basename] = $field_value;
513 $string_values[$basename] = $params["{$basename}_value"];
517 // Check the *_value and *_op criteria and include them if
518 // they'll have an effective value.
519 foreach ($op_values as $basename => $field_value) {
520 if ($field_value == 'nll' ||
$field_value == 'nnll') {
521 // 'nll' and 'nnll' filters should be included even with empty values.
522 $url_params["{$basename}_op"] = $field_value;
524 elseif ($string_values[$basename]) {
525 // Other filters are only included if they have a value.
526 $url_params["{$basename}_op"] = $field_value;
527 $url_params["{$basename}_value"] = (is_array($string_values[$basename]) ?
implode(',', $string_values[$basename]) : $string_values[$basename]);
530 $query_string = http_build_query($url_params);
536 return $query_string;
544 public static function getInstanceList($reportUrl) {
545 static $instanceDetails = array();
547 if (!array_key_exists($reportUrl, $instanceDetails)) {
548 $instanceDetails[$reportUrl] = array();
551 SELECT id, title FROM civicrm_report_instance
552 WHERE report_id = %1";
553 $params = array(1 => array($reportUrl, 'String'));
554 $result = CRM_Core_DAO
::executeQuery($sql, $params);
555 while ($result->fetch()) {
556 $instanceDetails[$reportUrl][$result->id
] = $result->title
. " (ID: {$result->id})";
559 return $instanceDetails[$reportUrl];