Another fix for timesheets.php.
[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') || ttAccessAllowed('view_client_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   $users_for_client = ttGroupHelper::getUsersForClient($user->client_id);
45   if (count($users_for_client) == 0) {
46     header('Location: access_denied.php'); // There are no users for client.
47     exit();
48   }
49 }
50 if ($request->isPost()) {
51   $userChanged = $request->getParameter('user_changed');
52   if ($userChanged && !(ttTimesheetHelper::isUserValid($request->getParameter('user')))) {
53     header('Location: access_denied.php'); // Wrong user id.
54     exit();
55   }
56 }
57 // End of access checks.
58
59 // Determine user for whom we display this page.
60 $notClient = !$user->isClient();
61 if ($request->isPost() && $userChanged) {
62   $user_id = $request->getParameter('user');
63 } else {
64   if ($notClient)
65     $user_id = $user->getUser();
66   else
67     $user_id = $users_for_client[0]['id']; // First found user for a client.
68 }
69 $group_id = $user->getGroup();
70
71 // Elements of timesheetsForm.
72 $form = new Form('timesheetsForm');
73
74 if ($user->can('view_timesheets') || $user->can('view_all_timesheets') || $user->can('view_client_timesheets')) {
75   // Prepare user list for dropdown.
76   if ($notClient) {
77     $rank = $user->can('view_all_timesheets') ? MAX_RANK : $user->getMaxRankForGroup($group_id);
78     if ($user->can('view_own_timesheets'))
79       $options = array('max_rank'=>$rank,'include_self'=>true,'self_first'=>true);
80     else
81       $options = array('max_rank'=>$rank);
82     $user_list = $user->getUsers($options);
83   } else
84     $user_list = $users_for_client; // Obtained above.
85
86   if (count($user_list) >= 1) {
87     $form->addInput(array('type'=>'combobox',
88       'onchange'=>'document.timesheetsForm.user_changed.value=1;document.timesheetsForm.submit();',
89       'name'=>'user',
90       'style'=>'width: 250px;',
91       'value'=>$user_id,
92       'data'=>$user_list,
93       'datakeys'=>array('id','name')));
94     $form->addInput(array('type'=>'hidden','name'=>'user_changed'));
95     $smarty->assign('user_dropdown', 1);
96   }
97 }
98
99 $active_timesheets = ttTimesheetHelper::getActiveTimesheets($user_id);
100 if ($notClient)
101   $inactive_timesheets = ttTimesheetHelper::getInactiveTimesheets($user_id);
102
103 $show_client = $user->isPluginEnabled('cl') && $notClient;
104
105 $smarty->assign('active_timesheets', $active_timesheets);
106 $smarty->assign('inactive_timesheets', $inactive_timesheets);
107 $smarty->assign('show_client', $show_client);
108 $smarty->assign('show_hint', $notClient);
109 $smarty->assign('show_submit_status', $notClient);
110 $smarty->assign('show_approval_status', $notClient);
111 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
112 $smarty->assign('title', $i18n->get('title.timesheets'));
113 $smarty->assign('content_page_name', 'timesheets.tpl');
114 $smarty->display('index.tpl');