Fixed a comment.
[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('ttUserConfig');
33
34 // Access checks.
35 if (!(ttAccessAllowed('manage_invoices') || ttAccessAllowed('view_client_invoices'))) {
36   header('Location: access_denied.php');
37   exit();
38 }
39 if (!$user->isPluginEnabled('iv')) {
40   header('Location: feature_disabled.php');
41   exit();
42 }
43 $cl_invoice_id = (int)$request->getParameter('id');
44 $invoice = ttInvoiceHelper::getInvoice($cl_invoice_id);
45 if (!$invoice) {
46   header('Location: access_denied.php');
47   exit();
48 }
49 // End of access checks.
50
51 $uc = new ttUserConfig();
52
53 if ($request->isPost()) {
54   $cl_receiver = trim($request->getParameter('receiver'));
55   $cl_cc = trim($request->getParameter('cc'));
56   $cl_subject = trim($request->getParameter('subject'));
57   $cl_comment = trim($request->getParameter('comment'));
58 } else {
59   $cl_receiver = $uc->getValue(SYSC_LAST_INVOICE_EMAIL);
60   $cl_cc = $uc->getValue(SYSC_LAST_INVOICE_CC);
61   $cl_subject = $i18n->get('title.invoice').' '.$invoice['name'].', '.$user->group_name;
62 }
63
64 $form = new Form('mailForm');
65 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$cl_invoice_id));
66 $form->addInput(array('type'=>'text','name'=>'receiver','style'=>'width: 300px;','value'=>$cl_receiver));
67 $form->addInput(array('type'=>'text','name'=>'cc','style'=>'width: 300px;','value'=>$cl_cc));
68 $form->addInput(array('type'=>'text','name'=>'subject','style'=>'width: 300px;','value'=>$cl_subject));
69 $form->addInput(array('type'=>'textarea','name'=>'comment','maxlength'=>'250','style'=>'width: 300px; height: 60px;'));
70 $form->addInput(array('type'=>'submit','name'=>'btn_send','value'=>$i18n->get('button.send')));
71
72 if ($request->isPost()) {
73   // Validate user input.
74   if (!ttValidEmailList($cl_receiver)) $err->add($i18n->get('error.field'), $i18n->get('form.mail.to'));
75   if (!ttValidEmailList($cl_cc, true)) $err->add($i18n->get('error.field'), $i18n->get('label.cc'));
76   if (!ttValidString($cl_subject)) $err->add($i18n->get('error.field'), $i18n->get('label.subject'));
77   if (!ttValidString($cl_comment, true)) $err->add($i18n->get('error.field'), $i18n->get('label.comment'));
78
79   if ($err->no()) {
80     // Save last invoice emails for future use.
81     $uc->setValue(SYSC_LAST_INVOICE_EMAIL, $cl_receiver);
82     $uc->setValue(SYSC_LAST_INVOICE_CC, $cl_cc);
83
84     $body = ttInvoiceHelper::prepareInvoiceBody($cl_invoice_id, $cl_comment);
85
86     import('mail.Mailer');
87     $mailer = new Mailer();
88     $mailer->setCharSet(CHARSET);
89     $mailer->setContentType('text/html');
90     $mailer->setSender(SENDER);
91     $mailer->setReceiver($cl_receiver);
92     if (isset($cl_cc))
93       $mailer->setReceiverCC($cl_cc);
94     if (!empty($user->bcc_email))
95       $mailer->setReceiverBCC($user->bcc_email);
96     $mailer->setMailMode(MAIL_MODE);
97     if ($mailer->send($cl_subject, $body))
98       $msg->add($i18n->get('form.mail.invoice_sent'));
99     else
100       $err->add($i18n->get('error.mail_send'));
101   }
102 } // isPost
103
104 $smarty->assign('sender', SENDER);
105 if (function_exists('imap_mime_header_decode')) {
106   // Decode sender in case it is encoded. PHP IMAP extension must be installed for us to get here.
107   $elements = imap_mime_header_decode(SENDER);
108   if (count($elements) > 1) {
109       // Reassign sender.
110       $smarty->assign('sender', $elements[count($elements) - 2]->text);
111   }
112 }
113
114 $smarty->assign('title', $i18n->get('title.send_invoice'));
115 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
116 $smarty->assign('onload', 'onLoad="document.mailForm.'.($cl_receiver?'comment':'receiver').'.focus()"');
117 $smarty->assign('content_page_name', 'mail.tpl');
118 $smarty->display('index.tpl');