posaune
[timetracker.git] / work_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('ttProjectHelper');
32 import('ttGroupHelper');
33 import('ttFileHelper');
34 import('ttWorkHelper');
35
36 // Access checks.
37 if (!ttAccessAllowed('manage_work')) {
38   header('Location: access_denied.php');
39   exit();
40 }
41 if (!$user->isPluginEnabled('wk')) {
42   header('Location: feature_disabled.php');
43   exit();
44 }
45 // End of access checks.
46
47 $showFiles = $user->isPluginEnabled('at');
48
49 if ($request->isPost()) {
50   $cl_name = trim($request->getParameter('work_name'));
51   $cl_description = trim($request->getParameter('description'));
52   $cl_currency = $request->getParameter('currency');
53 }
54
55 $form = new Form('workForm');
56 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'work_name','style'=>'width: 250px;','value'=>$cl_name));
57 $form->addInput(array('type'=>'textarea','name'=>'description','style'=>'width: 250px; height: 40px;','value'=>$cl_description));
58 if ($showFiles)
59   $form->addInput(array('type'=>'upload','name'=>'newfile','value'=>$i18n->get('button.submit')));
60 // Add a dropdown for currency.
61 $currencies = ttWorkHelper::getCurrencies();
62 $form->addInput(array('type'=>'combobox','name'=>'currency','data'=>$currencies,'datakeys'=>array('id','name'),'value'=>$cl_currency));
63
64 // TODO: design how to handle one-time vs ongoing work. Apparently, with a conditional display of relevant controls.
65 // Ongoing work - rate per hour control.
66 // One-time work - budget dropdown control.
67 // When selection changes, we hide and show required controls.
68
69 // TODO: design how to handle categories and sub-categories.
70 // One major complication is localization of names.
71
72 // Coding and design are currently ongoing.
73
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 (!ttValidString($cl_description, true)) $err->add($i18n->get('error.field'), $i18n->get('label.description'));
79   if (!ttGroupHelper::validateCheckboxGroupInput($cl_users, 'tt_users')) $err->add($i18n->get('error.field'), $i18n->get('label.users'));
80   if (!ttGroupHelper::validateCheckboxGroupInput($cl_tasks, 'tt_tasks')) $err->add($i18n->get('error.field'), $i18n->get('label.tasks'));
81
82   if ($err->no()) {
83     if (!ttProjectHelper::getProjectByName($cl_name)) {
84       $id = ttProjectHelper::insert(array('name' => $cl_name,
85         'description' => $cl_description,
86         'users' => $cl_users,
87         'tasks' => $cl_tasks,
88         'status' => ACTIVE));
89
90       // Put a new file in storage if we have it.
91       if ($id && $showFiles && $_FILES['newfile']['name']) {
92         $fileHelper = new ttFileHelper($err);
93         $fields = array('entity_type'=>'project',
94           'entity_id' => $id,
95           'file_name' => $_FILES['newfile']['name']);
96         $fileHelper->putFile($fields);
97       }
98       if ($id) {
99         header('Location: projects.php');
100         exit();
101       } else
102         $err->add($i18n->get('error.db'));
103     } else
104       $err->add($i18n->get('error.object_exists'));
105   }
106 } // isPost
107
108 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
109 $smarty->assign('onload', 'onLoad="document.projectForm.project_name.focus()"');
110 $smarty->assign('show_files', $showFiles);
111 $smarty->assign('show_users', count($users) > 0);
112 $smarty->assign('show_tasks', $show_tasks);
113 $smarty->assign('title', $i18n->get('title.add_work'));
114 $smarty->assign('content_page_name', 'work_add.tpl');
115 $smarty->display('index.tpl');