Renamed a function for clarity.
[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) || !$user->isPluginEnabled('iv')) {
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->isPost()) {
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->isPost()) {
68   // Validate user input.
69   if (!ttValidEmailList($cl_receiver)) $err->add($i18n->getKey('error.field'), $i18n->getKey('form.mail.to'));
70   if (!ttValidEmailList($cl_cc, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('form.mail.cc'));
71   if (!ttValidString($cl_subject)) $err->add($i18n->getKey('error.field'), $i18n->getKey('form.mail.subject'));
72   if (!ttValidString($cl_comment, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.comment'));
73
74   if ($err->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->setMailMode(MAIL_MODE);
90     if ($mailer->send($cl_subject, $body))
91       $msg->add($i18n->getKey('form.mail.invoice_sent'));
92     else
93       $err->add($i18n->getKey('error.mail_send'));
94   }
95 } // isPost
96
97 $smarty->assign('sender', SENDER);
98 if (function_exists('imap_mime_header_decode')) {
99   // Decode sender in case it is encoded. PHP IMAP extension must be installed for us to get here.
100   $elements = imap_mime_header_decode(SENDER);
101   if (count($elements) > 1) {
102       // Reassign sender.
103       $smarty->assign('sender', $elements[count($elements) - 2]->text);
104   }
105 }
106
107 $smarty->assign('title', $i18n->getKey('title.send_invoice'));
108 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
109 $smarty->assign('onload', 'onLoad="document.mailForm.'.($cl_receiver?'comment':'receiver').'.focus()"');
110 $smarty->assign('content_page_name', 'mail.tpl');
111 $smarty->display('index.tpl');