Some renaming for ongoing report refactoring.
[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     // Prepare report body.
76     $body = ttReportHelper::prepareReportBody($bean, $cl_comment);
77
78     import('mail.Mailer');
79     $mailer = new Mailer();
80     $mailer->setCharSet(CHARSET);
81     $mailer->setContentType('text/html');
82     $mailer->setSender(SENDER);
83     $mailer->setReceiver($cl_receiver);
84     if (isset($cl_cc))
85       $mailer->setReceiverCC($cl_cc);
86     if (!empty($user->bcc_email))
87       $mailer->setReceiverBCC($user->bcc_email);
88     $mailer->setMailMode(MAIL_MODE);
89     if ($mailer->send($cl_subject, $body))
90       $msg->add($i18n->get('form.mail.report_sent'));
91     else
92       $err->add($i18n->get('error.mail_send'));
93   }
94 }
95
96 $smarty->assign('sender', SENDER);
97 if (function_exists('imap_mime_header_decode')) {
98   $elements = imap_mime_header_decode(SENDER);
99   if (count($elements) > 1) {
100       // Reassign sender.
101       $new = $elements[count($elements) - 2]->text;
102       $smarty->assign('sender', $elements[count($elements) - 2]->text);
103   }
104 }
105
106 $smarty->assign('title', $i18n->get('title.send_report'));
107 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
108 $smarty->assign('onload', 'onLoad="document.mailForm.'.($cl_receiver?'comment':'receiver').'.focus()"');
109 $smarty->assign('content_page_name', 'mail.tpl');
110 $smarty->display('index.tpl');