Refactoring - ongoing.
[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('ttTeamHelper');
33 import('ttGroupHelper');
34
35 // Access checks.
36 if (!ttAccessAllowed('manage_projects')) {
37   header('Location: access_denied.php');
38   exit();
39 }
40 if (MODE_PROJECTS != $user->tracking_mode && MODE_PROJECTS_AND_TASKS != $user->tracking_mode) {
41   header('Location: feature_disabled.php');
42   exit();
43 }
44 $cl_project_id = (int)$request->getParameter('id');
45 $project = ttProjectHelper::get($cl_project_id);
46 if (!$project) {
47   header('Location: access_denied.php');
48   exit();
49 }
50 // End of access checks.
51
52 $users = ttGroupHelper::getActiveUsers();
53 foreach ($users as $user_item)
54   $all_users[$user_item['id']] = $user_item['name'];
55
56 $tasks = ttTeamHelper::getActiveTasks($user->group_id);
57 foreach ($tasks as $task_item)
58   $all_tasks[$task_item['id']] = $task_item['name'];
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
71   $mdb2 = getConnection();
72   $sql = "select user_id from tt_user_project_binds where status = 1 and project_id = $cl_project_id";
73   $res = $mdb2->query($sql);
74   if (is_a($res, 'PEAR_Error'))
75     die($res->getMessage());
76   while ($row = $res->fetchRow())
77     $cl_users[] = $row['user_id'];
78
79   $cl_tasks = explode(',', $project['tasks']);
80 }
81
82 $form = new Form('projectForm');
83 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$cl_project_id));
84 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'project_name','value'=>$cl_name));
85 $form->addInput(array('type'=>'textarea','name'=>'description','class'=>'mobile-textarea','value'=>$cl_description));
86 $form->addInput(array('type'=>'combobox','name'=>'status','value'=>$cl_status,
87   'data'=>array(ACTIVE=>$i18n->get('dropdown.status_active'),INACTIVE=>$i18n->get('dropdown.status_inactive'))));
88 $form->addInput(array('type'=>'checkboxgroup','name'=>'users','data'=>$all_users,'layout'=>'H','value'=>$cl_users));
89 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode)
90   $form->addInput(array('type'=>'checkboxgroup','name'=>'tasks','data'=>$all_tasks,'layout'=>'H','value'=>$cl_tasks));
91 $form->addInput(array('type'=>'submit','name'=>'btn_save','value'=>$i18n->get('button.save')));
92 $form->addInput(array('type'=>'submit','name'=>'btn_copy','value'=>$i18n->get('button.copy')));
93 $form->addInput(array('type'=>'submit','name'=>'btn_delete','value'=>$i18n->get('label.delete')));
94
95 if ($request->isPost()) {
96   // Validate user input.
97   if (!ttValidString($cl_name)) $err->add($i18n->get('error.field'), $i18n->get('label.thing_name'));
98   if (!ttValidString($cl_description, true)) $err->add($i18n->get('error.field'), $i18n->get('label.description'));
99
100   if ($err->no()) {
101     if ($request->getParameter('btn_save')) {
102       $existing_project = ttProjectHelper::getProjectByName($cl_name);
103       if (!$existing_project || ($cl_project_id == $existing_project['id'])) {
104          // Update project information.
105          if (ttProjectHelper::update(array(
106            'id' => $cl_project_id,
107            'name' => $cl_name,
108            'description' => $cl_description,
109            'status' => $cl_status,
110            'users' => $cl_users,
111            'tasks' => $cl_tasks))) {
112            header('Location: projects.php');
113            exit();
114         } else
115            $err->add($i18n->get('error.db'));
116       } else
117         $err->add($i18n->get('error.object_exists'));
118     }
119
120     if ($request->getParameter('btn_copy')) {
121       if (!ttProjectHelper::getProjectByName($cl_name)) {
122         if (ttProjectHelper::insert(array(
123           'group_id' => $user->getGroup(),
124           'org_id' => $user->org_id,
125           'name' => $cl_name,
126           'description' => $cl_description,
127           'users' => $cl_users,
128           'tasks' => $cl_tasks,
129           'status' => ACTIVE))) {
130           header('Location: projects.php');
131           exit();
132         } else
133           $err->add($i18n->get('error.db'));
134       } else
135         $err->add($i18n->get('error.object_exists'));
136     }
137     
138     if ($request->getParameter('btn_delete')) {
139       header("Location: project_delete.php?id=$cl_project_id");
140       exit();
141     }
142   }
143 } // isPost
144
145 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
146 $smarty->assign('onload', 'onLoad="document.projectForm.project_name.focus()"');
147 $smarty->assign('title', $i18n->get('title.edit_project'));
148 $smarty->assign('content_page_name', 'mobile/project_edit.tpl');
149 $smarty->display('mobile/index.tpl');