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