A bit of cleanup with ongoing timesheet redesign.
[timetracker.git] / timesheets.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('ttGroupHelper');
32 import('ttTimesheetHelper');
33
34 // Access checks.
35 if (!(ttAccessAllowed('view_own_timesheets') || ttAccessAllowed('view_timesheets') || ttAccessAllowed('view_all_timesheets'))) {
36   header('Location: access_denied.php');
37   exit();
38 }
39 if (!$user->isPluginEnabled('ts')) {
40   header('Location: feature_disabled.php');
41   exit();
42 }
43 if ($user->isClient()) {
44   header('Location: access_denied.php'); // No timesheets for clients.
45   exit();
46 }
47 if ($request->isPost()) {
48   $userChanged = $request->getParameter('user_changed'); // Reused in multiple places below.
49   if ($userChanged && !($user->can('view_timesheets') && $user->isUserValid($request->getParameter('user')))) {
50     header('Location: access_denied.php'); // Group changed, but no rght or wrong user id. TODO: research relevance of this...
51     exit();
52   }
53 }
54 // End of access checks.
55
56 // Determine user for which we display this page.
57 if ($request->isPost() && $userChanged) {
58   $user_id = $request->getParameter('user');
59   $user->setOnBehalfUser($user_id);
60 } else {
61   $user_id = $user->getUser();
62 }
63
64 $group_id = $user->getGroup();
65
66 // Elements of timesheetsForm.
67 $form = new Form('timesheetsForm');
68
69 if ($user->can('view_timesheets') || $user->can('view_all_timesheets')) {
70   $rank = $user->getMaxRankForGroup($group_id);
71   if ($user->can('view_own_timesheets'))
72     $options = array('status'=>ACTIVE,'max_rank'=>$rank,'include_self'=>true,'self_first'=>true);
73   else
74     $options = array('status'=>ACTIVE,'max_rank'=>$rank);
75   $user_list = $user->getUsers($options);
76   if (count($user_list) >= 1) {
77     $form->addInput(array('type'=>'combobox',
78       'onchange'=>'document.timesheetsForm.user_changed.value=1;document.timesheetsForm.submit();',
79       'name'=>'user',
80       'style'=>'width: 250px;',
81       'value'=>$user_id,
82       'data'=>$user_list,
83       'datakeys'=>array('id','name')));
84     $form->addInput(array('type'=>'hidden','name'=>'user_changed'));
85     $smarty->assign('user_dropdown', 1);
86   }
87 }
88
89 $active_timesheets = ttTimesheetHelper::getActiveTimesheets($user_id);
90 $inactive_timesheets = ttTimesheetHelper::getInactiveTimesheets($user_id);
91
92 $showClient = $user->isPluginEnabled('cl');
93 $canEdit = $user->can('manage_own_timesheets') || $user->can('manage_timesheets') || $user->can('manage_all_timesheets');
94
95 $smarty->assign('active_timesheets', $active_timesheets);
96 $smarty->assign('inactive_timesheets', $inactive_timesheets);
97 $smarty->assign('show_client', $showClient);
98 $smarty->assign('can_edit', $canEdit);
99 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
100 $smarty->assign('title', $i18n->get('title.timesheets'));
101 $smarty->assign('content_page_name', 'timesheets.tpl');
102 $smarty->display('index.tpl');