More improvements to access checks.
[timetracker.git] / task_edit.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('ttTeamHelper');
32 import('ttTaskHelper');
33
34 // Access checks.
35 if (!ttAccessAllowed('manage_tasks')) {
36   header('Location: access_denied.php');
37   exit();
38 }
39 if (MODE_PROJECTS_AND_TASKS != $user->tracking_mode) {
40   header('Location: feature_disabled.php');
41   exit();
42 }
43
44 $cl_task_id = (int)$request->getParameter('id');
45 $projects = ttTeamHelper::getActiveProjects($user->team_id);
46
47 if ($request->isPost()) {
48   $cl_name = trim($request->getParameter('name'));
49   $cl_description = trim($request->getParameter('description'));
50   $cl_status = $request->getParameter('status');
51   $cl_projects = $request->getParameter('projects');
52 } else {
53   $task = ttTaskHelper::get($cl_task_id);
54   $cl_name = $task['name'];
55   $cl_description = $task['description'];
56   $cl_status = $task['status'];
57
58   $assigned_projects = ttTaskHelper::getAssignedProjects($cl_task_id);
59   foreach ($assigned_projects as $project_item)
60     $cl_projects[] = $project_item['id'];
61 }
62
63 $form = new Form('taskForm');
64 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$cl_task_id));
65 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'name','style'=>'width: 250px;','value'=>$cl_name));
66 $form->addInput(array('type'=>'textarea','name'=>'description','style'=>'width: 250px; height: 40px;','value'=>$cl_description));
67 $form->addInput(array('type'=>'combobox','name'=>'status','value'=>$cl_status,
68   'data'=>array(ACTIVE=>$i18n->get('dropdown.status_active'),INACTIVE=>$i18n->get('dropdown.status_inactive'))));
69 $form->addInput(array('type'=>'checkboxgroup','name'=>'projects','layout'=>'H','data'=>$projects,'datakeys'=>array('id','name'),'value'=>$cl_projects));
70 $form->addInput(array('type'=>'submit','name'=>'btn_save','value'=>$i18n->get('button.save')));
71 $form->addInput(array('type'=>'submit','name'=>'btn_copy','value'=>$i18n->get('button.copy')));
72
73 if ($request->isPost()) {
74   // Validate user input.
75   if (!ttValidString($cl_name)) $err->add($i18n->get('error.field'), $i18n->get('label.thing_name'));
76   if (!ttValidString($cl_description, true)) $err->add($i18n->get('error.field'), $i18n->get('label.description'));
77
78   if ($err->no()) {
79     if ($request->getParameter('btn_save')) {
80       $existing_task = ttTaskHelper::getTaskByName($cl_name);
81       if (!$existing_task || ($cl_task_id == $existing_task['id'])) {
82         // Update task information.
83         if (ttTaskHelper::update(array(
84           'task_id' => $cl_task_id,
85           'name' => $cl_name,
86           'description' => $cl_description,
87           'status' => $cl_status,
88           'projects' => $cl_projects))) {
89           header('Location: tasks.php');
90           exit();
91         } else
92           $err->add($i18n->get('error.db'));
93       } else
94         $err->add($i18n->get('error.task_exists'));
95     }
96
97     if ($request->getParameter('btn_copy')) {
98       if (!ttTaskHelper::getTaskByName($cl_name)) {
99         if (ttTaskHelper::insert(array(
100           'team_id' => $user->team_id,
101           'name' => $cl_name,
102           'description' => $cl_description,
103           'status' => $cl_status,
104           'projects' => $cl_projects))) {
105           header('Location: tasks.php');
106           exit();
107         } else
108           $err->add($i18n->get('error.db'));
109       } else
110         $err->add($i18n->get('error.task_exists'));
111     }
112   }
113 } // isPost
114
115 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
116 $smarty->assign('title', $i18n->get('title.edit_task'));
117 $smarty->assign('content_page_name', 'task_edit.tpl');
118 $smarty->display('index.tpl');