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