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.
11 // | There are only two ways to violate the license:
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).
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).
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
24 // +----------------------------------------------------------------------+
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
29 require_once('initialize.php');
31 import('ttProjectHelper');
32 import('ttTeamHelper');
33 import('ttUserHelper');
35 import('form.TableColumn');
36 import('ttRoleHelper');
39 if (!ttAccessCheck(right_manage_team)) {
40 header('Location: access_denied.php');
44 // Get user id we are editing from the request.
45 $user_id = (int) $request->getParameter('id');
48 $user_details = ttUserHelper::getUserDetails($user_id);
51 $ok_to_go = $user->canManageTeam(); // Are we authorized for user management?
52 if ($ok_to_go) $ok_to_go = $ok_to_go && $user_details; // Are we editing a real user?
53 if ($ok_to_go) $ok_to_go = $ok_to_go && ($user->team_id == $user_details['team_id']); // User belongs to our team?
54 if ($ok_to_go && $user->isCoManager() && (ROLE_COMANAGER == $user_details['role']))
55 $ok_to_go = ($user->id == $user_details['id']); // Comanager is not allowed to edit other comanagers.
56 if ($ok_to_go && $user->isCoManager() && (ROLE_MANAGER == $user_details['role']))
57 $ok_to_go = false; // Comanager is not allowed to edit a manager.
59 die ($i18n->getKey('error.sys'));
62 if ($user->isPluginEnabled('cl'))
63 $clients = ttTeamHelper::getActiveClients($user->team_id);
65 $projects = ttTeamHelper::getActiveProjects($user->team_id);
66 $assigned_projects = array();
68 if ($request->isPost()) {
69 $cl_name = trim($request->getParameter('name'));
70 $cl_login = trim($request->getParameter('login'));
71 if (!$auth->isPasswordExternal()) {
72 $cl_password1 = $request->getParameter('pas1');
73 $cl_password2 = $request->getParameter('pas2');
75 $cl_email = trim($request->getParameter('email'));
76 $cl_role = $request->getParameter('role');
77 $cl_client_id = $request->getParameter('client');
78 $cl_status = $request->getParameter('status');
79 $cl_rate = $request->getParameter('rate');
80 $cl_projects = $request->getParameter('projects');
81 if (is_array($cl_projects)) {
82 foreach ($cl_projects as $p) {
83 if (ttValidFloat($request->getParameter('rate_'.$p), true)) {
84 $project_with_rate = array();
85 $project_with_rate['id'] = $p;
86 $project_with_rate['rate'] = $request->getParameter('rate_'.$p);
87 $assigned_projects[] = $project_with_rate;
89 $err->add($i18n->getKey('error.field'), 'rate_'.$p);
93 $cl_name = $user_details['name'];
94 $cl_login = $user_details['login'];
95 $cl_email = $user_details['email'];
96 $cl_rate = str_replace('.', $user->decimal_mark, $user_details['rate']);
97 $cl_role = $user_details['role_id'];
99 // In case role_id is not yet assigned...
100 if (!$cl_role && $user_details['role'])
101 $cl_role = ttRoleHelper::getRoleByRank($user_details['role']); // TODO: remove after roles revamp.
103 $cl_client_id = $user_details['client_id'];
104 $cl_status = $user_details['status'];
105 $cl_projects = array();
106 $assigned_projects = ttProjectHelper::getAssignedProjects($user_id);
107 foreach($assigned_projects as $p) {
108 $cl_projects[] = $p['id'];
112 $form = new Form('userForm');
113 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'name','style'=>'width: 300px;','value'=>$cl_name));
114 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'login','style'=>'width: 300px;','value'=>$cl_login));
115 if (!$auth->isPasswordExternal()) {
116 $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'pas1','value'=>$cl_password1));
117 $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'pas2','value'=>$cl_password2));
119 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'email','style'=>'width: 300px;','value'=>$cl_email));
121 $active_roles = ttTeamHelper::getActiveRolesForUser();
122 //$roles[ROLE_USER] = $i18n->getKey('label.user');
123 //$roles[ROLE_COMANAGER] = $i18n->getKey('form.users.comanager');
124 //if ($user->isPluginEnabled('cl'))
125 // $roles[ROLE_CLIENT] = $i18n->getKey('label.client');
126 $form->addInput(array('type'=>'combobox','onchange'=>'handleClientControl()','name'=>'role','value'=>$cl_role,'data'=>$active_roles, 'datakeys'=>array('id', 'name')));
127 if ($user->isPluginEnabled('cl'))
128 $form->addInput(array('type'=>'combobox','name'=>'client','value'=>$cl_client_id,'data'=>$clients,'datakeys'=>array('id', 'name'),'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
130 $form->addInput(array('type'=>'combobox','name'=>'status','value'=>$cl_status,
131 'data'=>array(ACTIVE=>$i18n->getKey('dropdown.status_active'),INACTIVE=>$i18n->getKey('dropdown.status_inactive'))));
132 $form->addInput(array('type'=>'floatfield','maxlength'=>'10','name'=>'rate','format'=>'.2','value'=>$cl_rate));
134 // Define classes for the projects table.
135 class NameCellRenderer extends DefaultCellRenderer {
136 function render(&$table, $value, $row, $column, $selected = false) {
137 $this->setOptions(array('width'=>200));
138 $this->setValue('<label for = "'.$table->name.'_'.$row.'">'.htmlspecialchars($value).'</label>');
139 return $this->toString();
142 class RateCellRenderer extends DefaultCellRenderer {
143 function render(&$table, $value, $row, $column, $selected = false) {
144 global $assigned_projects;
146 $field = new FloatField('rate_'.$table->getValueAtName($row,'id'));
147 $field->setFormName($table->getFormName());
149 $field->setFormat('.2');
150 foreach ($assigned_projects as $p) {
151 if ($p['id'] == $table->getValueAtName($row,'id')) $field->setValue($p['rate']);
153 $this->setValue($field->getHtml());
154 return $this->toString();
157 // Create projects table.
158 $table = new Table('projects');
159 $table->setIAScript('setRate');
160 $table->setTableOptions(array('width'=>'100%','cellspacing'=>'1','cellpadding'=>'3','border'=>'0'));
161 $table->setRowOptions(array('valign'=>'top','class'=>'tableHeader'));
162 $table->setData($projects);
163 $table->setKeyField('id');
164 $table->setValue($cl_projects);
165 $table->addColumn(new TableColumn('name', $i18n->getKey('label.project'), new NameCellRenderer()));
166 $table->addColumn(new TableColumn('p_rate', $i18n->getKey('form.users.rate'), new RateCellRenderer()));
167 $form->addInputElement($table);
169 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$user_id));
170 $form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->getKey('button.save')));
172 if ($request->isPost()) {
173 // Validate user input.
174 if (!ttValidString($cl_name)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.person_name'));
175 if (!ttValidString($cl_login)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.login'));
176 if (!$auth->isPasswordExternal() && ($cl_password1 || $cl_password2)) {
177 if (!ttValidString($cl_password1)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.password'));
178 if (!ttValidString($cl_password2)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.confirm_password'));
179 if ($cl_password1 !== $cl_password2)
180 $err->add($i18n->getKey('error.not_equal'), $i18n->getKey('label.password'), $i18n->getKey('label.confirm_password'));
182 if (!ttValidEmail($cl_email, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.email'));
183 // Require selection of a client for a client role.
184 if ($user->isPluginEnabled('cl') && ttRoleHelper::isClientRole($cl_role) && !$cl_client_id) $err->add($i18n->getKey('error.client'));
185 if (!ttValidFloat($cl_rate, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('form.users.default_rate'));
188 $existing_user = ttUserHelper::getUserByLogin($cl_login);
189 if (!$existing_user || ($user_id == $existing_user['id'])) {
193 'login' => $cl_login,
194 'password' => $cl_password1,
195 'email' => $cl_email,
196 'status' => $cl_status,
198 'projects' => $assigned_projects);
199 if (right_assign_roles & $user->rights && $cl_role) {
200 // Get legacy role value.
201 $legacy_role = ttRoleHelper::getLegacyRole($cl_role); // TODO: remove after roles revamp.
202 $fields['role'] = $legacy_role;
203 $fields['role_id'] = $cl_role;
204 $fields['client_id'] = $cl_client_id;
207 if (ttUserHelper::update($user_id, $fields)) {
209 // If our own login changed, set new one in cookie to remember it.
210 if (($user_id == $user->id) && ($user->login != $cl_login)) {
211 setcookie('tt_login', $cl_login, time() + COOKIE_EXPIRE, '/');
214 // In case the name of the "on behalf" user has changed - set it in session.
215 if (($user->behalf_id == $user_id) && ($user->behalf_name != $cl_name)) {
216 $_SESSION['behalf_name'] = $cl_name;
219 // If we deactivated our own account, do housekeeping and logout.
220 if ($user->id == $user_id && !is_null($cl_status) && $cl_status == INACTIVE) {
221 // Remove tt_login cookie that stores login name.
222 unset($_COOKIE['tt_login']);
223 setcookie('tt_login', NULL, -1);
226 header('Location: login.php');
230 header('Location: users.php');
234 $err->add($i18n->getKey('error.db'));
236 $err->add($i18n->getKey('error.user_exists'));
240 $rates = ttProjectHelper::getRates($user_id);
241 $smarty->assign('rates', $rates);
243 $smarty->assign('auth_external', $auth->isPasswordExternal());
244 $smarty->assign('active_roles', $active_roles);
245 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
246 $smarty->assign('onload', 'onLoad="document.userForm.name.focus();handleClientControl();"');
247 $smarty->assign('user_id', $user_id);
248 $smarty->assign('title', $i18n->getKey('title.edit_user'));
249 $smarty->assign('content_page_name', 'user_edit.tpl');
250 $smarty->display('index.tpl');