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