Fixed export-import for quota percent.
[timetracker.git] / mobile / project_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('ttProjectHelper');
32 import('ttGroupHelper');
33
34 // Access checks.
35 if (!ttAccessAllowed('manage_projects')) {
36   header('Location: access_denied.php');
37   exit();
38 }
39 if (MODE_PROJECTS != $user->getTrackingMode() && MODE_PROJECTS_AND_TASKS != $user->getTrackingMode()) {
40   header('Location: feature_disabled.php');
41   exit();
42 }
43 $cl_project_id = (int)$request->getParameter('id');
44 $project = ttProjectHelper::get($cl_project_id);
45 if (!$project) {
46   header('Location: access_denied.php');
47   exit();
48 }
49 // End of access checks.
50
51 $users = ttGroupHelper::getActiveUsers();
52 foreach ($users as $user_item)
53   $all_users[$user_item['id']] = $user_item['name'];
54
55 $tasks = ttGroupHelper::getActiveTasks();
56 foreach ($tasks as $task_item)
57   $all_tasks[$task_item['id']] = $task_item['name'];
58 $show_tasks = MODE_PROJECTS_AND_TASKS == $user->getTrackingMode() && count($tasks) > 0;
59
60 if ($request->isPost()) {
61   $cl_name = trim($request->getParameter('project_name'));
62   $cl_description = trim($request->getParameter('description'));
63   $cl_status = $request->getParameter('status');
64   $cl_users = $request->getParameter('users', array());
65   $cl_tasks = $request->getParameter('tasks', array());
66 } else {
67   $cl_name = $project['name'];
68   $cl_description = $project['description'];
69   $cl_status = $project['status'];
70   $cl_users = ttProjectHelper::getAssignedUsers($cl_project_id);
71   $cl_tasks = explode(',', $project['tasks']);
72 }
73
74 $form = new Form('projectForm');
75 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$cl_project_id));
76 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'project_name','value'=>$cl_name));
77 $form->addInput(array('type'=>'textarea','name'=>'description','class'=>'mobile-textarea','value'=>$cl_description));
78 $form->addInput(array('type'=>'combobox','name'=>'status','value'=>$cl_status,
79   'data'=>array(ACTIVE=>$i18n->get('dropdown.status_active'),INACTIVE=>$i18n->get('dropdown.status_inactive'))));
80 $form->addInput(array('type'=>'checkboxgroup','name'=>'users','data'=>$all_users,'layout'=>'H','value'=>$cl_users));
81 if ($show_tasks)
82   $form->addInput(array('type'=>'checkboxgroup','name'=>'tasks','data'=>$all_tasks,'layout'=>'H','value'=>$cl_tasks));
83 $form->addInput(array('type'=>'submit','name'=>'btn_save','value'=>$i18n->get('button.save')));
84 $form->addInput(array('type'=>'submit','name'=>'btn_copy','value'=>$i18n->get('button.copy')));
85 $form->addInput(array('type'=>'submit','name'=>'btn_delete','value'=>$i18n->get('label.delete')));
86
87 if ($request->isPost()) {
88   // Validate user input.
89   if (!ttValidString($cl_name)) $err->add($i18n->get('error.field'), $i18n->get('label.thing_name'));
90   if (!ttValidString($cl_description, true)) $err->add($i18n->get('error.field'), $i18n->get('label.description'));
91   if (!ttGroupHelper::validateCheckboxGroupInput($cl_users, 'tt_users')) $err->add($i18n->get('error.field'), $i18n->get('label.users'));
92   if (!ttGroupHelper::validateCheckboxGroupInput($cl_tasks, 'tt_tasks')) $err->add($i18n->get('error.field'), $i18n->get('label.tasks'));
93
94   if ($err->no()) {
95     if ($request->getParameter('btn_save')) {
96       $existing_project = ttProjectHelper::getProjectByName($cl_name);
97       if (!$existing_project || ($cl_project_id == $existing_project['id'])) {
98          // Update project information.
99          if (ttProjectHelper::update(array(
100            'id' => $cl_project_id,
101            'name' => $cl_name,
102            'description' => $cl_description,
103            'status' => $cl_status,
104            'users' => $cl_users,
105            'tasks' => $cl_tasks))) {
106            header('Location: projects.php');
107            exit();
108         } else
109            $err->add($i18n->get('error.db'));
110       } else
111         $err->add($i18n->get('error.object_exists'));
112     }
113
114     if ($request->getParameter('btn_copy')) {
115       if (!ttProjectHelper::getProjectByName($cl_name)) {
116         if (ttProjectHelper::insert(array('name' => $cl_name,
117           'description' => $cl_description,
118           'users' => $cl_users,
119           'tasks' => $cl_tasks,
120           'status' => ACTIVE))) {
121           header('Location: projects.php');
122           exit();
123         } else
124           $err->add($i18n->get('error.db'));
125       } else
126         $err->add($i18n->get('error.object_exists'));
127     }
128     
129     if ($request->getParameter('btn_delete')) {
130       header("Location: project_delete.php?id=$cl_project_id");
131       exit();
132     }
133   }
134 } // isPost
135
136 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
137 $smarty->assign('onload', 'onLoad="document.projectForm.project_name.focus()"');
138 $smarty->assign('show_users', count($users) > 0);
139 $smarty->assign('show_tasks', $show_tasks);
140 $smarty->assign('title', $i18n->get('title.edit_project'));
141 $smarty->assign('content_page_name', 'mobile/project_edit.tpl');
142 $smarty->display('mobile/index.tpl');