Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | |
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
06b69b18 | 5 | | CiviCRM version 4.5 | |
6a488035 | 6 | +--------------------------------------------------------------------+ |
06b69b18 | 7 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
8 | +--------------------------------------------------------------------+ |
9 | | This file is a part of CiviCRM. | | |
10 | | | | |
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. | | |
14 | | | | |
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. | | |
19 | | | | |
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 | +--------------------------------------------------------------------+ | |
27 | */ | |
28 | ||
29 | /** | |
30 | * | |
31 | * @package CRM | |
06b69b18 | 32 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
33 | * $Id$ |
34 | * | |
35 | */ | |
36 | class CRM_Report_Utils_Report { | |
37 | ||
74cf4551 EM |
38 | /** |
39 | * @param null $instanceID | |
40 | * | |
41 | * @return null|string | |
42 | */ | |
6a488035 TO |
43 | static function getValueFromUrl($instanceID = NULL) { |
44 | if ($instanceID) { | |
0b25329b | 45 | $optionVal = CRM_Core_DAO::getFieldValue('CRM_Report_DAO_ReportInstance', |
6a488035 TO |
46 | $instanceID, |
47 | 'report_id' | |
48 | ); | |
49 | } | |
50 | else { | |
51 | $config = CRM_Core_Config::singleton(); | |
52 | $args = explode('/', $_GET[$config->userFrameworkURLVar]); | |
53 | ||
54 | // remove 'civicrm/report' from args | |
55 | array_shift($args); | |
56 | array_shift($args); | |
57 | ||
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); | |
61 | } | |
62 | return $optionVal; | |
63 | } | |
64 | ||
74cf4551 EM |
65 | /** |
66 | * @param null $instanceID | |
67 | * | |
68 | * @return array|bool | |
69 | */ | |
6a488035 TO |
70 | static function getValueIDFromUrl($instanceID = NULL) { |
71 | $optionVal = self::getValueFromUrl($instanceID); | |
72 | ||
73 | if ($optionVal) { | |
74 | $templateInfo = CRM_Core_OptionGroup::getRowValues('report_template', "{$optionVal}", 'value'); | |
877022f7 | 75 | return array(CRM_Utils_Array::value('id', $templateInfo), $optionVal); |
6a488035 TO |
76 | } |
77 | ||
78 | return FALSE; | |
79 | } | |
80 | ||
74cf4551 EM |
81 | /** |
82 | * @param $optionVal | |
83 | * | |
84 | * @return mixed | |
85 | */ | |
6a488035 TO |
86 | static function getInstanceIDForValue($optionVal) { |
87 | static $valId = array(); | |
88 | ||
89 | if (!array_key_exists($optionVal, $valId)) { | |
90 | $sql = " | |
91 | SELECT MIN(id) FROM civicrm_report_instance | |
92 | WHERE report_id = %1"; | |
93 | ||
94 | $params = array(1 => array($optionVal, 'String')); | |
95 | $valId[$optionVal] = CRM_Core_DAO::singleValueQuery($sql, $params); | |
96 | } | |
97 | return $valId[$optionVal]; | |
98 | } | |
99 | ||
74cf4551 EM |
100 | /** |
101 | * @param null $path | |
102 | * | |
103 | * @return mixed | |
104 | */ | |
6a488035 TO |
105 | static function getInstanceIDForPath($path = NULL) { |
106 | static $valId = array(); | |
107 | ||
108 | // if $path is null, try to get it from url | |
109 | $path = self::getInstancePath(); | |
110 | ||
111 | if ($path && !array_key_exists($path, $valId)) { | |
112 | $sql = " | |
113 | SELECT MIN(id) FROM civicrm_report_instance | |
114 | WHERE TRIM(BOTH '/' FROM CONCAT(report_id, '/', name)) = %1"; | |
115 | ||
116 | $params = array(1 => array($path, 'String')); | |
117 | $valId[$path] = CRM_Core_DAO::singleValueQuery($sql, $params); | |
118 | } | |
119 | return CRM_Utils_Array::value($path, $valId); | |
120 | } | |
121 | ||
74cf4551 EM |
122 | /** |
123 | * @param $urlValue | |
124 | * @param string $query | |
125 | * @param bool $absolute | |
126 | * @param null $instanceID | |
127 | * @param array $drilldownReport | |
128 | * | |
129 | * @return bool|string | |
130 | */ | |
6a488035 TO |
131 | static function getNextUrl($urlValue, $query = 'reset=1', $absolute = FALSE, $instanceID = NULL, $drilldownReport = array()) { |
132 | if ($instanceID) { | |
133 | $drilldownInstanceID = false; | |
6001af1f | 134 | if (array_key_exists($urlValue, $drilldownReport)) |
0b25329b | 135 | $drilldownInstanceID = CRM_Core_DAO::getFieldValue('CRM_Report_DAO_ReportInstance', $instanceID, 'drilldown_id', 'id'); |
6a488035 | 136 | |
6001af1f | 137 | if (!$drilldownInstanceID) |
6a488035 TO |
138 | $drilldownInstanceID = self::getInstanceIDForValue($urlValue); |
139 | ||
140 | if ($drilldownInstanceID) { | |
141 | return CRM_Utils_System::url("civicrm/report/instance/{$drilldownInstanceID}", | |
142 | "{$query}", $absolute | |
143 | ); | |
144 | } | |
145 | else { | |
146 | return FALSE; | |
147 | } | |
148 | } | |
149 | else { | |
150 | return CRM_Utils_System::url("civicrm/report/" . trim($urlValue, '/'), | |
151 | $query, $absolute | |
152 | ); | |
153 | } | |
154 | } | |
155 | ||
156 | // get instance count for a template | |
74cf4551 EM |
157 | /** |
158 | * @param $optionVal | |
159 | * | |
160 | * @return int|null|string | |
161 | */ | |
6a488035 | 162 | static function getInstanceCount($optionVal) { |
ae555e90 DS |
163 | if (empty($optionVal)) return 0; |
164 | ||
6a488035 TO |
165 | $sql = " |
166 | SELECT count(inst.id) | |
167 | FROM civicrm_report_instance inst | |
168 | WHERE inst.report_id = %1"; | |
169 | ||
170 | $params = array(1 => array($optionVal, 'String')); | |
171 | $count = CRM_Core_DAO::singleValueQuery($sql, $params); | |
172 | return $count; | |
173 | } | |
174 | ||
74cf4551 EM |
175 | /** |
176 | * @param $fileContent | |
177 | * @param null $instanceID | |
178 | * @param string $outputMode | |
179 | * @param array $attachments | |
180 | * | |
181 | * @return bool | |
182 | */ | |
c490a46a | 183 | static function mailReport($fileContent, $instanceID = NULL, $outputMode = 'html', $attachments = array()) { |
6a488035 TO |
184 | if (!$instanceID) { |
185 | return FALSE; | |
186 | } | |
187 | ||
188 | list($domainEmailName, | |
189 | $domainEmailAddress | |
190 | ) = CRM_Core_BAO_Domain::getNameAndEmail(); | |
191 | ||
192 | $params = array('id' => $instanceID); | |
193 | $instanceInfo = array(); | |
0b25329b | 194 | CRM_Core_DAO::commonRetrieve('CRM_Report_DAO_ReportInstance', |
6a488035 TO |
195 | $params, |
196 | $instanceInfo | |
197 | ); | |
198 | ||
199 | $params = array(); | |
200 | $params['groupName'] = 'Report Email Sender'; | |
201 | $params['from'] = '"' . $domainEmailName . '" <' . $domainEmailAddress . '>'; | |
202 | //$domainEmailName; | |
203 | $params['toName'] = ""; | |
204 | $params['toEmail'] = CRM_Utils_Array::value('email_to', $instanceInfo); | |
205 | $params['cc'] = CRM_Utils_Array::value('email_cc', $instanceInfo); | |
206 | $params['subject'] = CRM_Utils_Array::value('email_subject', $instanceInfo); | |
a7488080 | 207 | if (empty($instanceInfo['attachments'])) { |
6a488035 TO |
208 | $instanceInfo['attachments'] = array(); |
209 | } | |
210 | $params['attachments'] = array_merge(CRM_Utils_Array::value('attachments', $instanceInfo), $attachments); | |
211 | $params['text'] = ''; | |
212 | $params['html'] = $fileContent; | |
213 | ||
214 | return CRM_Utils_Mail::send($params); | |
215 | } | |
216 | ||
74cf4551 | 217 | /** |
c490a46a | 218 | * @param CRM_Core_Form $form |
74cf4551 EM |
219 | * @param $rows |
220 | */ | |
6a488035 TO |
221 | static function export2csv(&$form, &$rows) { |
222 | //Mark as a CSV file. | |
223 | header('Content-Type: text/csv'); | |
224 | ||
225 | //Force a download and name the file using the current timestamp. | |
226 | $datetime = date('Ymd-Gi', $_SERVER['REQUEST_TIME']); | |
227 | header('Content-Disposition: attachment; filename=Report_' . $datetime . '.csv'); | |
228 | echo self::makeCsv($form, $rows); | |
229 | CRM_Utils_System::civiExit(); | |
230 | } | |
231 | ||
232 | /** | |
233 | * Utility function for export2csv and CRM_Report_Form::endPostProcess | |
234 | * - make CSV file content and return as string. | |
235 | */ | |
236 | static function makeCsv(&$form, &$rows) { | |
237 | $config = CRM_Core_Config::singleton(); | |
238 | $csv = ''; | |
239 | ||
240 | // Add headers if this is the first row. | |
241 | $columnHeaders = array_keys($form->_columnHeaders); | |
242 | ||
243 | // Replace internal header names with friendly ones, where available. | |
244 | foreach ($columnHeaders as $header) { | |
245 | if (isset($form->_columnHeaders[$header])) { | |
246 | $headers[] = '"' . html_entity_decode(strip_tags($form->_columnHeaders[$header]['title'])) . '"'; | |
247 | } | |
248 | } | |
249 | // Add the headers. | |
250 | $csv .= implode($config->fieldSeparator, | |
251 | $headers | |
252 | ) . "\r\n"; | |
253 | ||
254 | $displayRows = array(); | |
255 | $value = NULL; | |
256 | foreach ($rows as $row) { | |
257 | foreach ($columnHeaders as $k => $v) { | |
258 | $value = CRM_Utils_Array::value($v, $row); | |
259 | if (isset($value)) { | |
260 | // Remove HTML, unencode entities, and escape quotation marks. | |
261 | $value = str_replace('"', '""', html_entity_decode(strip_tags($value))); | |
262 | ||
263 | if (CRM_Utils_Array::value('type', $form->_columnHeaders[$v]) & 4) { | |
264 | if (CRM_Utils_Array::value('group_by', $form->_columnHeaders[$v]) == 'MONTH' || | |
265 | CRM_Utils_Array::value('group_by', $form->_columnHeaders[$v]) == 'QUARTER' | |
266 | ) { | |
267 | $value = CRM_Utils_Date::customFormat($value, $config->dateformatPartial); | |
268 | } | |
269 | elseif (CRM_Utils_Array::value('group_by', $form->_columnHeaders[$v]) == 'YEAR') { | |
270 | $value = CRM_Utils_Date::customFormat($value, $config->dateformatYear); | |
271 | } | |
272 | else { | |
273 | $value = CRM_Utils_Date::customFormat($value, '%Y-%m-%d'); | |
274 | } | |
275 | } | |
276 | elseif (CRM_Utils_Array::value('type', $form->_columnHeaders[$v]) == 1024) { | |
277 | $value = CRM_Utils_Money::format($value); | |
278 | } | |
279 | $displayRows[$v] = '"' . $value . '"'; | |
280 | } | |
281 | else { | |
282 | $displayRows[$v] = " "; | |
283 | } | |
284 | } | |
285 | // Add the data row. | |
286 | $csv .= implode($config->fieldSeparator, | |
287 | $displayRows | |
288 | ) . "\r\n"; | |
289 | } | |
290 | ||
291 | return $csv; | |
292 | } | |
293 | ||
74cf4551 EM |
294 | /** |
295 | * @return mixed | |
296 | */ | |
6a488035 TO |
297 | static function getInstanceID() { |
298 | ||
299 | $config = CRM_Core_Config::singleton(); | |
300 | $arg = explode('/', $_GET[$config->userFrameworkURLVar]); | |
301 | ||
302 | if ($arg[1] == 'report' && | |
303 | CRM_Utils_Array::value(2, $arg) == 'instance' | |
304 | ) { | |
305 | if (CRM_Utils_Rule::positiveInteger($arg[3])) { | |
306 | return $arg[3]; | |
307 | } | |
308 | } | |
309 | } | |
310 | ||
74cf4551 EM |
311 | /** |
312 | * @return string | |
313 | */ | |
6a488035 TO |
314 | static function getInstancePath() { |
315 | $config = CRM_Core_Config::singleton(); | |
316 | $arg = explode('/', $_GET[$config->userFrameworkURLVar]); | |
317 | ||
318 | if ($arg[1] == 'report' && | |
319 | CRM_Utils_Array::value(2, $arg) == 'instance' | |
320 | ) { | |
321 | unset($arg[0], $arg[1], $arg[2]); | |
322 | $path = trim(CRM_Utils_Type::escape(implode('/', $arg), 'String'), '/'); | |
323 | return $path; | |
324 | } | |
325 | } | |
326 | ||
74cf4551 EM |
327 | /** |
328 | * @param $instanceId | |
329 | * | |
330 | * @return bool | |
331 | */ | |
6a488035 TO |
332 | static function isInstancePermissioned($instanceId) { |
333 | if (!$instanceId) { | |
334 | return TRUE; | |
335 | } | |
336 | ||
337 | $instanceValues = array(); | |
338 | $params = array('id' => $instanceId); | |
0b25329b | 339 | CRM_Core_DAO::commonRetrieve('CRM_Report_DAO_ReportInstance', |
6a488035 TO |
340 | $params, |
341 | $instanceValues | |
342 | ); | |
343 | ||
344 | if (!empty($instanceValues['permission']) && | |
345 | (!(CRM_Core_Permission::check($instanceValues['permission']) || | |
346 | CRM_Core_Permission::check('administer Reports') | |
347 | )) | |
348 | ) { | |
349 | return FALSE; | |
350 | } | |
351 | ||
352 | return TRUE; | |
353 | } | |
354 | ||
355 | /** | |
356 | * Check if the user can view a report instance based on their role(s) | |
357 | * | |
358 | * @instanceId string $str the report instance to check | |
359 | * | |
fd31fa4c EM |
360 | * @param $instanceId |
361 | * | |
6a488035 TO |
362 | * @return boolean true if yes, else false |
363 | * @static | |
364 | * @access public | |
365 | */ | |
366 | static function isInstanceGroupRoleAllowed($instanceId) { | |
367 | if (!$instanceId) { | |
368 | return TRUE; | |
369 | } | |
370 | ||
371 | $instanceValues = array(); | |
372 | $params = array('id' => $instanceId); | |
0b25329b | 373 | CRM_Core_DAO::commonRetrieve('CRM_Report_DAO_ReportInstance', |
6a488035 TO |
374 | $params, |
375 | $instanceValues | |
376 | ); | |
377 | //transform grouprole to array | |
378 | if (!empty($instanceValues['grouprole'])) { | |
379 | $grouprole_array = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
380 | $instanceValues['grouprole'] | |
381 | ); | |
382 | if (!CRM_Core_Permission::checkGroupRole($grouprole_array) && | |
383 | !CRM_Core_Permission::check('administer Reports') | |
384 | ) { | |
385 | return FALSE; | |
386 | } | |
387 | } | |
388 | return TRUE; | |
389 | } | |
390 | ||
74cf4551 | 391 | /** |
c490a46a | 392 | * @param array $params |
74cf4551 EM |
393 | * |
394 | * @return array | |
395 | */ | |
6a488035 | 396 | static function processReport($params) { |
6a488035 TO |
397 | $instanceId = CRM_Utils_Array::value('instanceId', $params); |
398 | ||
399 | // hack for now, CRM-8358 | |
400 | $_REQUEST['instanceId'] = $instanceId; | |
401 | $_REQUEST['sendmail'] = CRM_Utils_Array::value('sendmail', $params, 1); | |
884605ca | 402 | |
6a488035 TO |
403 | // if cron is run from terminal --output is reserved, and therefore we would provide another name 'format' |
404 | $_REQUEST['output'] = CRM_Utils_Array::value('format', $params, CRM_Utils_Array::value('output', $params, 'pdf')); | |
405 | $_REQUEST['reset'] = CRM_Utils_Array::value('reset', $params, 1); | |
406 | ||
407 | $optionVal = self::getValueFromUrl($instanceId); | |
408 | $messages = array("Report Mail Triggered..."); | |
409 | ||
410 | $templateInfo = CRM_Core_OptionGroup::getRowValues('report_template', $optionVal, 'value'); | |
411 | $obj = new CRM_Report_Page_Instance(); | |
412 | $is_error = 0; | |
413 | if (strstr(CRM_Utils_Array::value('name', $templateInfo), '_Form')) { | |
414 | $instanceInfo = array(); | |
0b25329b | 415 | CRM_Report_BAO_ReportInstance::retrieve(array('id' => $instanceId), $instanceInfo); |
6a488035 TO |
416 | |
417 | if (!empty($instanceInfo['title'])) { | |
418 | $obj->assign('reportTitle', $instanceInfo['title']); | |
419 | } | |
420 | else { | |
421 | $obj->assign('reportTitle', $templateInfo['label']); | |
422 | } | |
423 | ||
424 | $wrapper = new CRM_Utils_Wrapper(); | |
884605ca DL |
425 | $arguments = array( |
426 | 'urlToSession' => array( | |
427 | array( | |
428 | 'urlVar' => 'instanceId', | |
429 | 'type' => 'Positive', | |
430 | 'sessionVar' => 'instanceId', | |
431 | 'default' => 'null', | |
432 | ), | |
6001af1f | 433 | ), |
884605ca | 434 | 'ignoreKey' => TRUE |
6001af1f | 435 | ); |
6a488035 TO |
436 | $messages[] = $wrapper->run($templateInfo['name'], NULL, $arguments); |
437 | } | |
438 | else { | |
439 | $is_error = 1; | |
440 | if (!$instanceId) { | |
441 | $messages[] = 'Required parameter missing: instanceId'; | |
442 | } | |
443 | else { | |
444 | $messages[] = 'Did not find valid instance to execute'; | |
445 | } | |
446 | } | |
447 | ||
448 | $result = array( | |
449 | 'is_error' => $is_error, | |
450 | 'messages' => implode("\n", $messages), | |
451 | ); | |
452 | return $result; | |
453 | } | |
454 | ||
455 | /** | |
456 | * Build a URL query string containing all report filter criteria that are | |
457 | * stipulated in $_GET or in a report Preview, but which haven't yet been | |
458 | * saved in the report instance. | |
459 | * | |
460 | * @param array $defaults The report criteria that aren't coming in as submitted form values, as in CRM_Report_Form::_defaults | |
461 | * @param array $params All effective report criteria, as in CRM_Report_Form::_params | |
462 | * | |
463 | * @return string URL query string | |
464 | */ | |
c490a46a | 465 | static function getPreviewCriteriaQueryParams($defaults = array(), $params = array()) { |
6a488035 TO |
466 | static $query_string; |
467 | if (!isset($query_string)) { | |
468 | if (!empty($params)) { | |
469 | $url_params = $op_values = $string_values = $process_params = array(); | |
470 | ||
471 | // We'll only use $params that are different from what's in $default. | |
472 | foreach ($params as $field_name => $field_value) { | |
473 | if (!array_key_exists($field_name, $defaults) || $defaults[$field_name] != $field_value) { | |
474 | $process_params[$field_name] = $field_value; | |
475 | } | |
476 | } | |
477 | // Criteria stipulated in $_GET will be in $defaults even if they're not | |
478 | // saved, so we can't easily tell if they're saved or not. So just include them. | |
479 | $process_params += $_GET; | |
480 | ||
481 | // All $process_params should be passed on if they have an effective value | |
482 | // (in other words, there's no point in propagating blank filters). | |
483 | foreach ($process_params as $field_name => $field_value) { | |
484 | $suffix_position = strrpos($field_name, '_'); | |
485 | $suffix = substr($field_name, $suffix_position); | |
486 | $basename = substr($field_name, 0, $suffix_position); | |
487 | if ($suffix == '_min' || $suffix == '_max' || | |
488 | $suffix == '_from' || $suffix == '_to' || | |
489 | $suffix == '_relative' | |
490 | ) { | |
491 | // For these types, we only keep them if they have a value. | |
492 | if (!empty($field_value)) { | |
493 | $url_params[$field_name] = $field_value; | |
494 | } | |
495 | } | |
496 | elseif ($suffix == '_value') { | |
497 | // These filters can have an effect even without a value | |
498 | // (e.g., values for 'nll' and 'nnll' ops are blank), | |
499 | // so store them temporarily and examine below. | |
500 | $string_values[$basename] = $field_value; | |
501 | $op_values[$basename] = CRM_Utils_Array::value("{$basename}_op", $params); | |
502 | } | |
503 | elseif ($suffix == '_op') { | |
504 | // These filters can have an effect even without a value | |
505 | // (e.g., values for 'nll' and 'nnll' ops are blank), | |
506 | // so store them temporarily and examine below. | |
507 | $op_values[$basename] = $field_value; | |
508 | $string_values[$basename] = $params["{$basename}_value"]; | |
509 | } | |
510 | } | |
511 | ||
512 | // Check the *_value and *_op criteria and include them if | |
513 | // they'll have an effective value. | |
514 | foreach ($op_values as $basename => $field_value) { | |
515 | if ($field_value == 'nll' || $field_value == 'nnll') { | |
516 | // 'nll' and 'nnll' filters should be included even with empty values. | |
517 | $url_params["{$basename}_op"] = $field_value; | |
518 | } | |
519 | elseif ($string_values[$basename]) { | |
520 | // Other filters are only included if they have a value. | |
521 | $url_params["{$basename}_op"] = $field_value; | |
522 | $url_params["{$basename}_value"] = (is_array($string_values[$basename]) ? implode(',', $string_values[$basename]) : $string_values[$basename]); | |
523 | } | |
524 | } | |
525 | $query_string = http_build_query($url_params); | |
526 | } | |
527 | else { | |
528 | $query_string = ''; | |
529 | } | |
530 | } | |
531 | return $query_string; | |
532 | } | |
533 | ||
74cf4551 EM |
534 | /** |
535 | * @param $reportUrl | |
536 | * | |
537 | * @return mixed | |
538 | */ | |
6a488035 TO |
539 | static function getInstanceList($reportUrl) { |
540 | static $instanceDetails = array(); | |
6001af1f | 541 | |
6a488035 TO |
542 | if (!array_key_exists($reportUrl, $instanceDetails )) { |
543 | $instanceDetails[$reportUrl] = array(); | |
544 | ||
545 | $sql = " | |
546 | SELECT id, title FROM civicrm_report_instance | |
547 | WHERE report_id = %1"; | |
548 | $params = array(1 => array($reportUrl, 'String')); | |
549 | $result = CRM_Core_DAO::executeQuery($sql, $params); | |
550 | while( $result->fetch()) { | |
551 | $instanceDetails[$reportUrl][$result->id] = $result->title . " (ID: {$result->id})"; | |
552 | } | |
553 | } | |
554 | return $instanceDetails[$reportUrl]; | |
555 | } | |
556 | } |