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