sla: Stupid Rasmus and his needles and haystacks
[ninja.git] / application / models / send_report.php
blob1ddacd8233f4ca20d77e91d235d69660d70db7dd
1 <?php
3 /**
4 * Model for sending out reports in different ways
5 */
6 class Send_report_Model extends Model {
7 /**
8 * @param $data The actual data to send
9 * @param $filename The filename to use when sending this
10 * @param $format The filetype - 'pdf', 'csv' or 'html'
11 * @param $recipient one email or a string composed of comma separated strings
12 * @throws RuntimeException if file is not readable
13 * @return boolean
15 static function send($data, $filename, $format, $recipient)
18 $to = $recipient;
19 if (strstr($to, ',')) {
20 $recipient = explode(',', $to);
21 if (is_array($recipient) && !empty($recipient)) {
22 unset($to);
23 foreach ($recipient as $user) {
24 $to[$user] = $user;
29 $config = Kohana::config('reports');
30 $mail_sender_address = $config['from_email'];
32 if (!empty($mail_sender_address)) {
33 $from = $mail_sender_address;
34 } else {
35 $hostname = exec('hostname --long');
36 $from = !empty($config['from']) ? $config['from'] : Kohana::config('config.product_name');
37 $from = str_replace(' ', '', trim($from));
38 if (empty($hostname) && $hostname != '(none)') {
39 // unable to get a valid hostname
40 $from = $from . '@localhost';
41 } else {
42 $from = $from . '@'.$hostname;
46 $plain = sprintf(_('Scheduled report sent from %s'),!empty($config['from']) ? $config['from'] : $from);
47 $subject = _('Scheduled report').": $filename";
49 switch ($format) {
50 case 'pdf':
51 $mime = 'application/pdf';
52 break;
53 case 'csv':
54 $mime = 'application/csv';
55 break;
56 case 'html':
57 $mime = 'application/csv';
58 break;
59 default:
60 $mime = 'application/binary';
61 break;
64 # $mail_sent will contain the nr of mail sent - not used at the moment
65 $mail_sent = email::send_report($to, $from, $subject, $plain, $mime, $filename, $data);
67 return (boolean) $mail_sent;