posaune
[timetracker.git] / timesheet_view.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('ttTimesheetHelper');
31 import('ttReportHelper');
32
33 // Access checks.
34 if (!(ttAccessAllowed('track_own_time') || ttAccessAllowed('track_time'))) {
35   header('Location: access_denied.php');
36   exit();
37 }
38 if (!$user->isPluginEnabled('ts')) {
39   header('Location: feature_disabled.php');
40   exit();
41 }
42 $cl_timesheet_id = (int)$request->getParameter('id');
43 $timesheet = ttTimesheetHelper::getTimesheet($cl_timesheet_id);
44 if (!$timesheet) {
45   header('Location: access_denied.php');
46   exit();
47 }
48 // TODO: add other checks here for timesheet being appropriate for user role.
49 // TODO: if this is a timesheet submit, validate approver id, too.
50 // End of access checks.
51
52 if ($request->isPost()) {
53   $cl_comment = trim($request->getParameter('comment'));
54   $approver_id = $request->getParameter('approver');
55 }
56
57 $options = ttTimesheetHelper::getReportOptions($timesheet);
58 $subtotals = ttReportHelper::getSubtotals($options);
59 $totals = ttReportHelper::getTotals($options);
60
61 // Determine which controls to show and obtain date for them.
62 $showSubmit = !$timesheet['submit_status'];
63 if ($showSubmit) {
64   $approvers = ttTimesheetHelper::getApprovers();
65   $showApprovers = count($approvers) >= 1;
66 }
67 $canApprove = $user->can('approve_timesheets') || $user->can('approve_own_timesheets');
68 $showApprove = $timesheet['submit_status'] && $timesheet['approve_status'] == null;
69
70 // Add a form with controls.
71 $form = new Form('timesheetForm');
72 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$timesheet['id']));
73
74 if ($showSubmit) {
75   if ($showApprovers) {
76     $form->addInput(array('type'=>'combobox',
77       'name'=>'approver',
78       'style'=>'width: 200px;',
79       'data'=>$approvers,
80       'datakeys'=>array('id','name','email')));
81   }
82   $form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->get('button.submit')));
83 }
84
85 if ($showApprove) {
86   $form->addInput(array('type'=>'textarea','name'=>'comment','maxlength'=>'250','style'=>'width: 300px; height: 60px;'));
87   $form->addInput(array('type'=>'submit','name'=>'btn_approve','value'=>$i18n->get('button.approve')));
88   $form->addInput(array('type'=>'submit','name'=>'btn_disapprove','value'=>$i18n->get('button.disapprove')));
89 }
90
91 // Submit.
92 if ($request->isPost()) {
93   if ($request->getParameter('btn_submit')) {
94     $fields = array('timesheet_id' => $timesheet['id'],
95       'approver_id' => $approver_id);
96     if (!ttTimesheetHelper::markSubmitted($fields))
97       $err->add($i18n->get('error.db'));
98     if ($err->no() && !ttTimesheetHelper::sendSubmitEmail($fields)) {
99       $err->add($i18n->get('error.mail_send'));
100     }
101     if ($err->no()) {
102       // Redirect to self.
103       header('Location: timesheet_view.php?id='.$timesheet['id']);
104       exit();
105     }
106   }
107
108   if ($request->getParameter('btn_approve')) {
109     $fields = array('timesheet_id' => $timesheet['id'],
110       'name' => $timesheet['name'],
111       'user_id' => $timesheet['user_id'],
112       'comment' => $cl_comment);
113     if (!ttTimesheetHelper::markApproved($fields))
114       $err->add($i18n->get('error.db'));
115     if ($err->no() && !ttTimesheetHelper::sendApprovedEmail($fields)) {
116       $err->add($i18n->get('error.mail_send'));
117     }
118     if ($err->no()) {
119       // Redirect to self.
120       header('Location: timesheet_view.php?id='.$timesheet['id']);
121       exit();
122     }
123   }
124
125   if ($request->getParameter('btn_disapprove')) {
126     $fields = array('timesheet_id' => $timesheet['id'],
127       'name' => $timesheet['name'],
128       'user_id' => $timesheet['user_id'],
129       'comment' => $cl_comment);
130     if (!ttTimesheetHelper::markDisapproved($fields))
131       $err->add($i18n->get('error.db'));
132     if ($err->no() && !ttTimesheetHelper::sendDisapprovedEmail($fields)) {
133       $err->add($i18n->get('error.mail_send'));
134     }
135     if ($err->no()) {
136       // Redirect to self.
137       header('Location: timesheet_view.php?id='.$timesheet['id']);
138       exit();
139     }
140   }
141 }
142
143 $smarty->assign('not_client', $notClient);
144 $smarty->assign('group_by_header', ttReportHelper::makeGroupByHeader($options));
145 $smarty->assign('timesheet', $timesheet);
146 $smarty->assign('subtotals', $subtotals);
147 $smarty->assign('totals', $totals);
148 $smarty->assign('show_approvers', $showApprovers);
149 $smarty->assign('show_submit', $showSubmit);
150 $smarty->assign('show_approve', $showApprove);
151 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
152 $smarty->assign('title', $i18n->get('title.timesheet').": ".$timesheet['start_date']." - ".$timesheet['end_date']);
153 $smarty->assign('content_page_name', 'timesheet_view.tpl');
154 $smarty->display('index.tpl');