posaune
[timetracker.git] / timesheet_add.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('ttTimesheetHelper');
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 // End of access checks.
43
44 if ($request->isPost()) {
45   $cl_name = trim($request->getParameter('timesheet_name'));
46   $cl_client = $request->getParameter('client');
47   $cl_project = $request->getParameter('project');
48   $cl_start = $request->getParameter('start');
49   $cl_finish = $request->getParameter('finish');
50   $cl_comment = trim($request->getParameter('comment'));
51 }
52
53 $user_id = $user->getUser();
54
55 $form = new Form('timesheetForm');
56 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'timesheet_name','style'=>'width: 250px;','value'=>$cl_name));
57
58 // Dropdown for clients if the clients plugin is enabled.
59 $showClient = $user->isPluginEnabled('cl');
60 if ($showClient) {
61   $clients = ttGroupHelper::getActiveClients();
62   $form->addInput(array('type'=>'combobox','name'=>'client','style'=>'width: 250px;','data'=>$clients,'datakeys'=>array('id','name'),'value'=>$cl_client,'empty'=>array(''=>$i18n->get('dropdown.select'))));
63 }
64 // Dropdown for projects.
65 $showProject = MODE_PROJECTS == $user->getTrackingMode() || MODE_PROJECTS_AND_TASKS == $user->getTrackingMode();
66 if ($showProject) {
67   $projects = $user->getAssignedProjects();
68   $form->addInput(array('type'=>'combobox','name'=>'project','style'=>'width: 250px;','data'=>$projects,'datakeys'=>array('id','name'),'value'=>$cl_project,'empty'=>array(''=>$i18n->get('dropdown.all'))));
69 }
70 $form->addInput(array('type'=>'datefield','maxlength'=>'20','name'=>'start','value'=>$cl_start));
71 $form->addInput(array('type'=>'datefield','maxlength'=>'20','name'=>'finish','value'=>$cl_finish));
72 $form->addInput(array('type'=>'textarea','name'=>'comment','style'=>'width: 250px; height: 40px;','value'=>$cl_comment));
73 $form->addInput(array('type'=>'submit','name'=>'btn_add','value'=>$i18n->get('button.add')));
74
75 if ($request->isPost()) {
76   // Validate user input.
77   if (!ttValidString($cl_name)) $err->add($i18n->get('error.field'), $i18n->get('label.thing_name'));
78   if (!ttValidDate($cl_start)) $err->add($i18n->get('error.field'), $i18n->get('label.start_date'));
79   if (!ttValidDate($cl_finish)) $err->add($i18n->get('error.field'), $i18n->get('label.end_date'));
80   if (!ttValidString($cl_comment, true)) $err->add($i18n->get('error.field'), $i18n->get('label.comment'));
81   if ($err->no() && ttTimesheetHelper::getTimesheetByName($cl_name)) $err->add($i18n->get('error.object_exists'));
82   $fields = array('user_id' => $user_id,
83     'name' => $cl_name,
84     'client_id' => $cl_client,
85     'project_id' => $cl_project,
86     'start_date' => $cl_start,
87     'end_date' => $cl_finish,
88     'comment' => $cl_comment);
89   if ($err->no() && !ttTimesheetHelper::timesheetItemsExist($fields)) $err->add($i18n->get('error.no_records'));
90   if ($err->no() && ttTimesheetHelper::overlaps($fields)) $err->add($i18n->get('error.overlap'));
91   // Finished validating user input.
92
93   if ($err->no()) {
94     if (ttTimesheetHelper::createTimesheet($fields)) {
95       header('Location: timesheets.php');
96       exit();
97     } else
98       $err->add($i18n->get('error.db'));
99   }
100 } // isPost
101
102 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
103 $smarty->assign('onload', 'onLoad="document.timesheetForm.timesheet_name.focus()"');
104 $smarty->assign('show_client', $showClient);
105 $smarty->assign('show_project', $showProject);
106 $smarty->assign('title', $i18n->get('title.add_timesheet'));
107 $smarty->assign('content_page_name', 'timesheet_add.tpl');
108 $smarty->display('index.tpl');