Work in progress creating a repo
[timetracker.git] / 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
34 // Access check.
35 if (!ttAccessCheck(right_manage_team)) {
36   header('Location: access_denied.php');
37   exit();
38 }
39
40 $cl_project_id = (int)$request->getParameter('id');
41
42 $users = ttTeamHelper::getActiveUsers();
43 foreach ($users as $user_item)
44   $all_users[$user_item['id']] = $user_item['name'];
45
46 $tasks = ttTeamHelper::getActiveTasks($user->team_id);
47 foreach ($tasks as $task_item)
48   $all_tasks[$task_item['id']] = $task_item['name'];
49
50 if ($request->getMethod() == 'POST') {
51   $cl_name = trim($request->getParameter('project_name'));
52   $cl_description = trim($request->getParameter('description'));
53   $cl_status = $request->getParameter('status');
54   $cl_users = $request->getParameter('users', array());
55   $cl_tasks = $request->getParameter('tasks', array());
56 } else {
57   $project = ttProjectHelper::get($cl_project_id);
58   $cl_name = $project['name'];
59   $cl_description = $project['description'];
60   $cl_status = $project['status'];
61   
62   $mdb2 = getConnection();
63   $sql = "select user_id from tt_user_project_binds where status = 1 and project_id = $cl_project_id";
64   $res = $mdb2->query($sql);
65   if (is_a($res, 'PEAR_Error'))
66     die($res->getMessage());
67   while ($row = $res->fetchRow())
68     $cl_users[] = $row['user_id'];
69     
70   $cl_tasks = explode(',', $project['tasks']);
71 }
72
73 $form = new Form('projectForm');
74 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$cl_project_id));
75 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'project_name','style'=>'width: 250px;','value'=>$cl_name));
76 $form->addInput(array('type'=>'textarea','name'=>'description','style'=>'width: 250px; height: 40px;','value'=>$cl_description));
77 $form->addInput(array('type'=>'combobox','name'=>'status','value'=>$cl_status,
78   'data'=>array(ACTIVE=>$i18n->getKey('dropdown.status_active'),INACTIVE=>$i18n->getKey('dropdown.status_inactive'))));
79 $form->addInput(array('type'=>'checkboxgroup','name'=>'users','data'=>$all_users,'layout'=>'H','value'=>$cl_users));
80 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode)
81   $form->addInput(array('type'=>'checkboxgroup','name'=>'tasks','data'=>$all_tasks,'layout'=>'H','value'=>$cl_tasks));
82 $form->addInput(array('type'=>'submit','name'=>'btn_save','value'=>$i18n->getKey('button.save')));
83 $form->addInput(array('type'=>'submit','name'=>'btn_copy','value'=>$i18n->getKey('button.copy')));
84   
85 if ($request->getMethod() == 'POST') {
86   // Validate user input.
87   if (!ttValidString($cl_name)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.thing_name'));
88   if (!ttValidString($cl_description, true)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.description'));
89   
90   if ($errors->isEmpty()) {
91     if ($request->getParameter('btn_save')) {
92       $existing_project = ttProjectHelper::getProjectByName($cl_name);
93       if (!$existing_project || ($cl_project_id == $existing_project['id'])) {
94          // Update project information.
95          if (ttProjectHelper::update(array(
96            'id' => $cl_project_id,
97            'name' => $cl_name,
98            'description' => $cl_description,
99            'status' => $cl_status,
100            'users' => $cl_users,
101            'tasks' => $cl_tasks))) {
102            header('Location: projects.php');
103            exit();
104         } else
105            $errors->add($i18n->getKey('error.db'));
106       } else
107         $errors->add($i18n->getKey('error.project_exists'));
108     }
109
110     if ($request->getParameter('btn_copy')) {
111       if (!ttProjectHelper::getProjectByName($cl_name)) {
112         if (ttProjectHelper::insert(array(
113           'team_id' => $user->team_id,
114           'name' => $cl_name,
115           'description' => $cl_description,
116           'users' => $cl_users,
117           'tasks' => $cl_tasks,
118           'status' => ACTIVE))) {
119           header('Location: projects.php');
120           exit();
121         } else
122           $errors->add($i18n->getKey('error.db'));
123       } else
124         $errors->add($i18n->getKey('error.project_exists'));
125     }
126   }
127 } // post
128
129 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
130 $smarty->assign('onload', 'onLoad="document.projectForm.name.focus()"');
131 $smarty->assign('title', $i18n->getKey('title.edit_project'));
132 $smarty->assign('content_page_name', 'project_edit.tpl');
133 $smarty->display('index.tpl');
134 ?>