Switched to using shorter ActionErrors functions
[timetracker.git] / invoice_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('ttInvoiceHelper');
32 import('ttSysConfig');
33
34 // Access check.
35 if (!ttAccessCheck(right_view_invoices)) {
36   header('Location: access_denied.php');
37   exit();
38 }
39
40 $cl_invoice_id = (int)$request->getParameter('id');
41 $invoice = ttInvoiceHelper::getInvoice($cl_invoice_id); 
42 $sc = new ttSysConfig($user->id);
43
44 // Security check.
45 if (!$cl_invoice_id || !$invoice)
46   die ($i18n->getKey('error.sys'));
47
48 if ($request->getMethod() == 'POST') {
49   $cl_receiver = trim($request->getParameter('receiver'));
50   $cl_cc = trim($request->getParameter('cc'));
51   $cl_subject = trim($request->getParameter('subject'));
52   $cl_comment = trim($request->getParameter('comment'));
53 } else {
54   $cl_receiver = $sc->getValue(SYSC_LAST_INVOICE_EMAIL);
55   $cl_cc = $sc->getValue(SYSC_LAST_INVOICE_CC);
56   $cl_subject = $i18n->getKey('title.invoice').' '.$invoice['name'].', '.$user->team;
57 }
58
59 $form = new Form('mailForm');
60 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$cl_invoice_id));
61 $form->addInput(array('type'=>'text','name'=>'receiver','style'=>'width: 300px;','value'=>$cl_receiver));
62 $form->addInput(array('type'=>'text','name'=>'cc','style'=>'width: 300px;','value'=>$cl_cc));
63 $form->addInput(array('type'=>'text','name'=>'subject','style'=>'width: 300px;','value'=>$cl_subject));
64 $form->addInput(array('type'=>'textarea','name'=>'comment','maxlength'=>'250','style'=>'width: 300px; height: 60px;'));
65 $form->addInput(array('type'=>'submit','name'=>'btn_send','value'=>$i18n->getKey('button.send')));
66
67 if ($request->getMethod() == 'POST') {
68   // Validate user input.
69   if (!ttValidEmailList($cl_receiver)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('form.mail.to'));
70   if (!ttValidEmailList($cl_cc, true)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('form.mail.cc'));
71   if (!ttValidString($cl_subject)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('form.mail.subject'));
72   if (!ttValidString($cl_comment, true)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.comment'));
73
74   if ($errors->no()) {
75     // Save last invoice emails for future use.
76     $sc->setValue(SYSC_LAST_INVOICE_EMAIL, $cl_receiver);
77     $sc->setValue(SYSC_LAST_INVOICE_CC, $cl_cc);
78
79     $body = ttInvoiceHelper::prepareInvoiceBody($cl_invoice_id, $cl_comment);
80
81     import('mail.Mailer');
82     $mailer = new Mailer();
83     $mailer->setCharSet(CHARSET);
84     $mailer->setContentType('text/html');
85     $mailer->setSender(SENDER);
86     $mailer->setReceiver($cl_receiver);
87     if (isset($cl_cc))
88       $mailer->setReceiverCC($cl_cc);
89     $mailer->setSendType(MAIL_MODE);
90     if ($mailer->send($cl_subject, $body))
91       $messages->add($i18n->getKey('form.mail.invoice_sent'));
92     else
93       $errors->add($i18n->getKey('error.mail_send'));
94   }
95 } // POST
96
97 $smarty->assign('title', $i18n->getKey('title.send_invoice'));
98 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
99 $smarty->assign('onload', 'onLoad="document.mailForm.'.($cl_receiver?'comment':'receiver').'.focus()"');
100 $smarty->assign('content_page_name', 'mail.tpl');
101 $smarty->display('index.tpl');