Refactoring - ongoing.
[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 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->getTrackingMode() && MODE_PROJECTS_AND_TASKS != $user->getTrackingMode()) {
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->getGroup());
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','style'=>'width: 250px;','value'=>$cl_name));
85 $form->addInput(array('type'=>'textarea','name'=>'description','style'=>'width: 250px; height: 40px;','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->getTrackingMode())
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
94 if ($request->isPost()) {
95   // Validate user input.
96   if (!ttValidString($cl_name)) $err->add($i18n->get('error.field'), $i18n->get('label.thing_name'));
97   if (!ttValidString($cl_description, true)) $err->add($i18n->get('error.field'), $i18n->get('label.description'));
98
99   if ($err->no()) {
100     if ($request->getParameter('btn_save')) {
101       $existing_project = ttProjectHelper::getProjectByName($cl_name);
102       if (!$existing_project || ($cl_project_id == $existing_project['id'])) {
103          // Update project information.
104          if (ttProjectHelper::update(array(
105            'id' => $cl_project_id,
106            'name' => $cl_name,
107            'description' => $cl_description,
108            'status' => $cl_status,
109            'users' => $cl_users,
110            'tasks' => $cl_tasks))) {
111            header('Location: projects.php');
112            exit();
113         } else
114            $err->add($i18n->get('error.db'));
115       } else
116         $err->add($i18n->get('error.object_exists'));
117     }
118
119     if ($request->getParameter('btn_copy')) {
120       if (!ttProjectHelper::getProjectByName($cl_name)) {
121         if (ttProjectHelper::insert(array(
122           'group_id' => $user->getGroup(),
123           'org_id' => $user->org_id,
124           'name' => $cl_name,
125           'description' => $cl_description,
126           'users' => $cl_users,
127           'tasks' => $cl_tasks,
128           'status' => ACTIVE))) {
129           header('Location: projects.php');
130           exit();
131         } else
132           $err->add($i18n->get('error.db'));
133       } else
134         $err->add($i18n->get('error.object_exists'));
135     }
136   }
137 } // isPost
138
139 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
140 $smarty->assign('show_users', count($users) > 0);
141 $smarty->assign('show_tasks', count($tasks) > 0);
142 $smarty->assign('onload', 'onLoad="document.projectForm.name.focus()"');
143 $smarty->assign('title', $i18n->get('title.edit_project'));
144 $smarty->assign('content_page_name', 'project_edit.tpl');
145 $smarty->display('index.tpl');