More refactoring on timesheet code.
[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 }
55
56 $options = ttTimesheetHelper::getReportOptions($timesheet);
57 $subtotals = ttReportHelper::getSubtotals($options);
58 $totals = ttReportHelper::getTotals($options);
59
60 // Determine which controls to show and obtain date for them.
61 $showSubmit = !$timesheet['submit_status'];
62 if ($showSubmit) {
63   $approvers = ttTimesheetHelper::getApprovers($timesheet['user_id']);
64   $showApprovers = count($approvers) >= 1;
65 }
66 $canApprove = $user->can('approve_timesheets') || $user->can('approve_own_timesheets');
67 $showApprove = $timesheet['submit_status'] && $timesheet['approve_status'] == null;
68
69 // Add a form with controls.
70 $form = new Form('timesheetForm');
71 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$timesheet['id']));
72
73 if ($showSubmit) {
74   if ($showApprovers) {
75     $form->addInput(array('type'=>'combobox',
76       'name'=>'approver',
77       'style'=>'width: 200px;',
78       'data'=>$approvers,
79       'datakeys'=>array('id','name')));
80   }
81   $form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->get('button.submit')));
82 }
83
84 if ($showApprove) {
85   $form->addInput(array('type'=>'textarea','name'=>'comment','maxlength'=>'250','style'=>'width: 300px; height: 60px;'));
86   $form->addInput(array('type'=>'submit','name'=>'btn_approve','value'=>$i18n->get('button.approve')));
87   $form->addInput(array('type'=>'submit','name'=>'btn_disapprove','value'=>$i18n->get('button.disapprove')));
88 }
89
90 // Submit.
91 if ($request->isPost()) {
92   if ($request->getParameter('btn_submit')) {
93     $fields = array('timesheet_id' => $timesheet['id'],
94       'approver_id' => $approver_id); // TODO: obtain (and check) approver id above during access checks.
95     if (ttTimesheetHelper::submitTimesheet($fields)) {
96       // Redirect to self.
97       header('Location: timesheet_view.php?id='.$timesheet['id']);
98       exit();
99     } else
100       $err->add($i18n->get('error.db'));
101   }
102
103   if ($request->getParameter('btn_approve')) {
104     $fields = array('timesheet_id' => $timesheet['id'],
105       'comment' => $cl_comment);
106     if (ttTimesheetHelper::approveTimesheet($fields)) {
107       // Redirect to self.
108       header('Location: timesheet_view.php?id='.$timesheet['id']);
109       exit();
110     } else
111       $err->add($i18n->get('error.db'));
112   }
113
114   if ($request->getParameter('btn_disapprove')) {
115     $fields = array('timesheet_id' => $timesheet['id'],
116       'comment' => $cl_comment);
117     if (ttTimesheetHelper::disapproveTimesheet($fields)) {
118       // Redirect to self.
119       header('Location: timesheet_view.php?id='.$timesheet['id']);
120       exit();
121     } else
122       $err->add($i18n->get('error.db'));
123   }
124 }
125
126 $smarty->assign('not_client', $notClient);
127 $smarty->assign('group_by_header', ttReportHelper::makeGroupByHeader($options));
128 $smarty->assign('timesheet', $timesheet);
129 $smarty->assign('subtotals', $subtotals);
130 $smarty->assign('totals', $totals);
131 $smarty->assign('show_approvers', $showApprovers);
132 $smarty->assign('show_submit', $showSubmit);
133 $smarty->assign('show_approve', $showApprove);
134 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
135 $smarty->assign('title', $i18n->get('title.timesheet').": ".$timesheet['start_date']." - ".$timesheet['end_date']);
136 $smarty->assign('content_page_name', 'timesheet_view.tpl');
137 $smarty->display('index.tpl');