Merged prepareReportBody and prepareFavReportBody into one function.
[timetracker.git] / report_send.php
1 <?php
2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
10 // |
11 // | There are only two ways to violate the license:
12 // |
13 // | 1. To redistribute this code in source form, with the copyright
14 // |    notice or license removed or altered. (Distributing in compiled
15 // |    forms without embedded copyright notices is permitted).
16 // |
17 // | 2. To redistribute modified versions of this code in *any* form
18 // |    that bears insufficient indications that the modifications are
19 // |    not the work of the original author(s).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 require_once('initialize.php');
30 import('form.Form');
31 import('form.ActionForm');
32 import('ttSysConfig');
33 import('ttReportHelper');
34
35 // Access check.
36 if (!(ttAccessAllowed('view_own_reports') || ttAccessAllowed('view_reports'))) {
37   header('Location: access_denied.php');
38   exit();
39 }
40
41 $sc = new ttSysConfig($user->id);
42
43 if ($request->isPost()) {
44   $cl_receiver = trim($request->getParameter('receiver'));
45   $cl_cc = trim($request->getParameter('cc'));
46   $cl_subject = trim($request->getParameter('subject'));
47   $cl_comment = trim($request->getParameter('comment'));
48 } else {
49   $cl_receiver = $sc->getValue(SYSC_LAST_REPORT_EMAIL);
50   $cl_cc = $sc->getValue(SYSC_LAST_REPORT_CC);
51   $cl_subject = $i18n->get('form.mail.report_subject');
52 }
53
54 $form = new Form('mailForm');
55 $form->addInput(array('type'=>'text','name'=>'receiver','style'=>'width: 300px;','value'=>$cl_receiver));
56 $form->addInput(array('type'=>'text','name'=>'cc','style'=>'width: 300px;','value'=>$cl_cc));
57 $form->addInput(array('type'=>'text','name'=>'subject','style'=>'width: 300px;','value'=>$cl_subject));
58 $form->addInput(array('type'=>'textarea','name'=>'comment','maxlength'=>'250','style'=>'width: 300px; height: 60px;'));
59 $form->addInput(array('type'=>'submit','name'=>'btn_send','value'=>$i18n->get('button.send')));
60
61 if ($request->isPost()) {
62   // Validate user input.
63   if (!ttValidEmailList($cl_receiver)) $err->add($i18n->get('error.field'), $i18n->get('form.mail.to'));
64   if (!ttValidEmailList($cl_cc, true)) $err->add($i18n->get('error.field'), $i18n->get('label.cc'));
65   if (!ttValidString($cl_subject)) $err->add($i18n->get('error.field'), $i18n->get('label.subject'));
66   if (!ttValidString($cl_comment, true)) $err->add($i18n->get('error.field'), $i18n->get('label.comment'));
67
68   if ($err->no()) {
69     // Save last report emails for future use.
70     $sc->setValue(SYSC_LAST_REPORT_EMAIL, $cl_receiver);
71     $sc->setValue(SYSC_LAST_REPORT_CC, $cl_cc);
72
73     // Obtain session bean with report attributes.
74     $bean = new ActionForm('reportBean', new Form('reportForm'));
75     $options = ttReportHelper::getReportOptions($bean);
76
77     // Prepare report body.
78     $body = ttReportHelper::prepareReportBody($options, $cl_comment);
79
80     import('mail.Mailer');
81     $mailer = new Mailer();
82     $mailer->setCharSet(CHARSET);
83     $mailer->setContentType('text/html');
84     $mailer->setSender(SENDER);
85     $mailer->setReceiver($cl_receiver);
86     if (isset($cl_cc))
87       $mailer->setReceiverCC($cl_cc);
88     if (!empty($user->bcc_email))
89       $mailer->setReceiverBCC($user->bcc_email);
90     $mailer->setMailMode(MAIL_MODE);
91     if ($mailer->send($cl_subject, $body))
92       $msg->add($i18n->get('form.mail.report_sent'));
93     else
94       $err->add($i18n->get('error.mail_send'));
95   }
96 }
97
98 $smarty->assign('sender', SENDER);
99 if (function_exists('imap_mime_header_decode')) {
100   $elements = imap_mime_header_decode(SENDER);
101   if (count($elements) > 1) {
102       // Reassign sender.
103       $new = $elements[count($elements) - 2]->text;
104       $smarty->assign('sender', $elements[count($elements) - 2]->text);
105   }
106 }
107
108 $smarty->assign('title', $i18n->get('title.send_report'));
109 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
110 $smarty->assign('onload', 'onLoad="document.mailForm.'.($cl_receiver?'comment':'receiver').'.focus()"');
111 $smarty->assign('content_page_name', 'mail.tpl');
112 $smarty->display('index.tpl');