Improved getUserDetails to not return info for deleted users.
[timetracker.git] / mobile / user_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('ttUserHelper');
34 import('form.Table');
35 import('form.TableColumn');
36
37 // Access check.
38 if (!ttAccessAllowed('manage_users')) {
39   header('Location: access_denied.php');
40   exit();
41 }
42
43 // Get user id we are editing from the request.
44 $user_id = (int) $request->getParameter('id');
45 // Get user details.
46 $user_details = ttUserHelper::getUserDetails($user_id);
47
48 // Security checks.
49 if (!$user_details || // No details.
50      $user_details['rank'] > $user->rank || // User has a bigger rank.
51      ($user_details['rank'] == $user->rank && $user_details['id'] <> $user->id) // Same rank but not us.
52    ) {
53   header('Location: access_denied.php');
54   exit();
55 }
56
57 if ($user->isPluginEnabled('cl'))
58   $clients = ttTeamHelper::getActiveClients($user->team_id);
59
60 $projects = ttTeamHelper::getActiveProjects($user->team_id);
61 $assigned_projects = array();
62
63 if ($request->isPost()) {
64   $cl_name = trim($request->getParameter('name'));
65   $cl_login = trim($request->getParameter('login'));
66   if (!$auth->isPasswordExternal()) {
67     $cl_password1 = $request->getParameter('pas1');
68     $cl_password2 = $request->getParameter('pas2');
69   }
70   $cl_email = trim($request->getParameter('email'));
71   $cl_role_id = $request->getParameter('role');
72   $cl_client_id = $request->getParameter('client');
73   $cl_status = $request->getParameter('status');
74   $cl_rate = $request->getParameter('rate');
75   $cl_projects = $request->getParameter('projects');
76   if (is_array($cl_projects)) {
77     foreach ($cl_projects as $p) {
78       if (ttValidFloat($request->getParameter('rate_'.$p), true)) {
79         $project_with_rate = array();
80         $project_with_rate['id'] = $p;
81         $project_with_rate['rate'] = $request->getParameter('rate_'.$p);
82         $assigned_projects[] = $project_with_rate;
83       } else
84         $err->add($i18n->get('error.field'), 'rate_'.$p);
85     }
86   }
87 } else {
88   $cl_name = $user_details['name'];
89   $cl_login = $user_details['login'];
90   $cl_email = $user_details['email'];
91   $cl_rate = str_replace('.', $user->decimal_mark, $user_details['rate']);
92   $cl_role_id = $user_details['role_id'];
93   $cl_client_id = $user_details['client_id'];
94   $cl_status = $user_details['status'];
95   $cl_projects = array();
96   $assigned_projects = ttProjectHelper::getAssignedProjects($user_id);
97   foreach($assigned_projects as $p) {
98     $cl_projects[] = $p['id'];
99   }
100 }
101
102 $form = new Form('userForm');
103 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'name','value'=>$cl_name));
104 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'login','value'=>$cl_login));
105 if (!$auth->isPasswordExternal()) {
106   $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'pas1','value'=>$cl_password1));
107   $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'pas2','value'=>$cl_password2));
108 }
109 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'email','value'=>$cl_email));
110
111 $active_roles = ttTeamHelper::getActiveRolesForUser();
112 $form->addInput(array('type'=>'combobox','onchange'=>'handleClientControl()','name'=>'role','value'=>$cl_role_id,'data'=>$active_roles,'datakeys'=>array('id', 'name')));
113 if ($user->isPluginEnabled('cl'))
114   $form->addInput(array('type'=>'combobox','name'=>'client','value'=>$cl_client_id,'data'=>$clients,'datakeys'=>array('id', 'name'),'empty'=>array(''=>$i18n->get('dropdown.select'))));
115
116 $form->addInput(array('type'=>'combobox','name'=>'status','value'=>$cl_status,
117   'data'=>array(ACTIVE=>$i18n->get('dropdown.status_active'),INACTIVE=>$i18n->get('dropdown.status_inactive'))));
118 $form->addInput(array('type'=>'floatfield','maxlength'=>'10','name'=>'rate','format'=>'.2','value'=>$cl_rate));
119
120 // Define classes for the projects table.
121 class NameCellRenderer extends DefaultCellRenderer {
122   function render(&$table, $value, $row, $column, $selected = false) {
123     $this->setOptions(array('width'=>200,'valign'=>'top'));
124     $this->setValue('<label for = "'.$table->getName().'_'.$row.'">'.htmlspecialchars($value).'</label>');
125     return $this->toString();
126   }
127 }
128 class RateCellRenderer extends DefaultCellRenderer {
129   function render(&$table, $value, $row, $column, $selected = false) {
130     global $assigned_projects;
131     $field = new FloatField('rate_'.$table->getValueAtName($row,'id'));
132     $field->setFormName($table->getFormName());
133     $field->setSize(5);
134     $field->setFormat('.2');
135     foreach ($assigned_projects as $p) {
136       if ($p['id'] == $table->getValueAtName($row,'id')) $field->setValue($p['rate']);
137     }
138     $this->setValue($field->getHtml());
139     return $this->toString();
140   }
141 }
142 // Create projects table.
143 $table = new Table('projects');
144 $table->setIAScript('setRate');
145 $table->setTableOptions(array('width'=>'100%','cellspacing'=>'1','cellpadding'=>'3','border'=>'0'));
146 $table->setRowOptions(array('valign'=>'top','class'=>'tableHeader'));
147 $table->setData($projects);
148 $table->setKeyField('id');
149 $table->setValue($cl_projects);
150 $table->addColumn(new TableColumn('name', $i18n->get('label.project'), new NameCellRenderer()));
151 $table->addColumn(new TableColumn('p_rate', $i18n->get('form.users.rate'), new RateCellRenderer()));
152 $form->addInputElement($table);
153
154 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$user_id));
155 $form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->get('button.save')));
156 $form->addInput(array('type'=>'submit','name'=>'btn_delete','value'=>$i18n->get('label.delete')));
157
158 if ($request->isPost()) {
159   if ($request->getParameter('btn_submit')) {
160     // Validate user input.
161     if (!ttValidString($cl_name)) $err->add($i18n->get('error.field'), $i18n->get('label.person_name'));
162     if (!ttValidString($cl_login)) $err->add($i18n->get('error.field'), $i18n->get('label.login'));
163     if (!$auth->isPasswordExternal() && ($cl_password1 || $cl_password2)) {
164       if (!ttValidString($cl_password1)) $err->add($i18n->get('error.field'), $i18n->get('label.password'));
165       if (!ttValidString($cl_password2)) $err->add($i18n->get('error.field'), $i18n->get('label.confirm_password'));
166       if ($cl_password1 !== $cl_password2)
167         $err->add($i18n->get('error.not_equal'), $i18n->get('label.password'), $i18n->get('label.confirm_password'));
168     }
169     if (!ttValidEmail($cl_email, true)) $err->add($i18n->get('error.field'), $i18n->get('label.email'));
170     if (!ttValidFloat($cl_rate, true)) $err->add($i18n->get('error.field'), $i18n->get('form.users.default_rate'));
171   
172     if ($err->no()) {
173       $existing_user = ttUserHelper::getUserByLogin($cl_login);
174       if (!$existing_user || ($user_id == $existing_user['id'])) {
175   
176         $fields = array(
177           'name' => $cl_name,
178           'login' => $cl_login,
179           'password' => $cl_password1,
180           'email' => $cl_email,
181           'status' => $cl_status,
182           'rate' => $cl_rate,
183           'projects' => $assigned_projects);
184         if (in_array('manage_users', $user->rights)) {
185           $fields['role_id'] = $cl_role_id;
186           $fields['client_id'] = $cl_client_id;
187         }
188   
189         if (ttUserHelper::update($user_id, $fields)) {
190   
191           // If our own login changed, set new one in cookie to remember it.
192           if (($user_id == $user->id) && ($user->login != $cl_login)) {
193             setcookie('tt_login', $cl_login, time() + COOKIE_EXPIRE, '/');
194           }
195   
196           // In case the name of the "on behalf" user has changed - set it in session.
197           if (($user->behalf_id == $user_id) && ($user->behalf_name != $cl_name)) {
198             $_SESSION['behalf_name'] = $cl_name;
199           }
200   
201           // If we deactivated our own account, do housekeeping and logout.
202           if ($user->id == $user_id && !is_null($cl_status) && $cl_status == INACTIVE) {
203             // Remove tt_login cookie that stores login name.
204             unset($_COOKIE['tt_login']);
205             setcookie('tt_login', NULL, -1);
206   
207             $auth->doLogout();
208             header('Location: login.php');
209             exit();
210           }
211   
212           header('Location: users.php');
213           exit();
214   
215         } else
216           $err->add($i18n->get('error.db'));
217       } else
218         $err->add($i18n->get('error.user_exists'));
219     }
220   }
221   
222   if ($request->getParameter('btn_delete')) {
223     header("Location: user_delete.php?id=$user_id");
224     exit();
225   }
226 } // isPost
227
228 $rates = ttProjectHelper::getRates($user_id);
229 $smarty->assign('rates', $rates);
230
231 $smarty->assign('auth_external', $auth->isPasswordExternal());
232 $smarty->assign('active_roles', $active_roles);
233 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
234 $smarty->assign('onload', 'onLoad="document.userForm.name.focus();handleClientControl();"');
235 $smarty->assign('user_id', $user_id);
236 $smarty->assign('title', $i18n->get('title.edit_user'));
237 $smarty->assign('content_page_name', 'mobile/user_edit.tpl');
238 $smarty->display('mobile/index.tpl');