Cleanup of white space and comment improvement
[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 (!ttAccessCheck(right_view_reports)) {
37   header('Location: access_denied.php');
38   exit();
39 }
40
41 $sc = new ttSysConfig($user->id);
42
43 if ($request->getMethod() == 'POST') {
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->getKey('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->getKey('button.send')));
60
61 if ($request->getMethod() == 'POST') {
62   // Validate user input.
63   if (!ttValidEmailList($cl_receiver)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('form.mail.to'));
64   if (!ttValidEmailList($cl_cc, true)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('form.mail.cc'));
65   if (!ttValidString($cl_subject)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('form.mail.subject'));
66   if (!ttValidString($cl_comment, true)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.comment'));
67
68   if ($errors->isEmpty()) {
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     $mailer->setSendType(MAIL_MODE);
87     if ($mailer->send($cl_subject, $body))
88       $messages->add($i18n->getKey('form.mail.report_sent'));
89     else
90       $errors->add($i18n->getKey('error.mail_send'));
91   }
92 }
93
94 $smarty->assign('title', $i18n->getKey('title.send_report'));
95 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
96 $smarty->assign('onload', 'onLoad="document.mailForm.'.($cl_receiver?'comment':'receiver').'.focus()"');
97 $smarty->assign('content_page_name', 'mail.tpl');
98 $smarty->display('index.tpl');