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('ttGroupHelper');
34 import('ttUserHelper');
36 import('form.TableColumn');
37 import('ttRoleHelper');
40 if (!ttAccessAllowed('manage_users')) {
41 header('Location: access_denied.php');
44 $user_id = (int)$request->getParameter('id');
45 $user_details = $user->getUserDetails($user_id);
47 header('Location: access_denied.php');
50 // End of access checks.
52 if ($user->isPluginEnabled('cl'))
53 $clients = ttGroupHelper::getActiveClients();
55 $projects = ttGroupHelper::getActiveProjects();
56 $assigned_projects = array();
58 if ($request->isPost()) {
59 $cl_name = trim($request->getParameter('name'));
60 $cl_login = trim($request->getParameter('login'));
61 if (!$auth->isPasswordExternal()) {
62 $cl_password1 = $request->getParameter('pas1');
63 $cl_password2 = $request->getParameter('pas2');
65 $cl_email = trim($request->getParameter('email'));
66 $cl_role_id = $request->getParameter('role');
67 $cl_client_id = $request->getParameter('client');
68 $cl_status = $request->getParameter('status');
69 $cl_rate = $request->getParameter('rate');
70 $cl_projects = $request->getParameter('projects');
71 if (is_array($cl_projects)) {
72 foreach ($cl_projects as $p) {
73 if (ttValidFloat($request->getParameter('rate_'.$p), true)) {
74 $project_with_rate = array();
75 $project_with_rate['id'] = $p;
76 $project_with_rate['rate'] = $request->getParameter('rate_'.$p);
77 $assigned_projects[] = $project_with_rate;
79 $err->add($i18n->get('error.field'), 'rate_'.$p);
83 $cl_name = $user_details['name'];
84 $cl_login = $user_details['login'];
85 $cl_email = $user_details['email'];
86 $cl_rate = str_replace('.', $user->getDecimalMark(), $user_details['rate']);
87 $cl_role_id = $user_details['role_id'];
88 $cl_client_id = $user_details['client_id'];
89 $cl_status = $user_details['status'];
90 $cl_projects = array();
91 $assigned_projects = ttProjectHelper::getAssignedProjects($user_id);
92 foreach($assigned_projects as $p) {
93 $cl_projects[] = $p['id'];
97 $form = new Form('userForm');
98 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'name','style'=>'width: 300px;','value'=>$cl_name));
99 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'login','style'=>'width: 300px;','value'=>$cl_login));
100 if (!$auth->isPasswordExternal()) {
101 $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'pas1','value'=>$cl_password1));
102 $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'pas2','value'=>$cl_password2));
104 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'email','style'=>'width: 300px;','value'=>$cl_email));
106 $active_roles = ttTeamHelper::getActiveRolesForUser();
107 $form->addInput(array('type'=>'combobox','onchange'=>'handleClientControl()','name'=>'role','value'=>$cl_role_id,'data'=>$active_roles, 'datakeys'=>array('id', 'name')));
108 if ($user->isPluginEnabled('cl'))
109 $form->addInput(array('type'=>'combobox','name'=>'client','value'=>$cl_client_id,'data'=>$clients,'datakeys'=>array('id', 'name'),'empty'=>array(''=>$i18n->get('dropdown.select'))));
111 $form->addInput(array('type'=>'combobox','name'=>'status','value'=>$cl_status,
112 'data'=>array(ACTIVE=>$i18n->get('dropdown.status_active'),INACTIVE=>$i18n->get('dropdown.status_inactive'))));
113 $form->addInput(array('type'=>'floatfield','maxlength'=>'10','name'=>'rate','format'=>'.2','value'=>$cl_rate));
115 // Define classes for the projects table.
116 class NameCellRenderer extends DefaultCellRenderer {
117 function render(&$table, $value, $row, $column, $selected = false) {
118 $this->setOptions(array('width'=>200));
119 $this->setValue('<label for = "'.$table->name.'_'.$row.'">'.htmlspecialchars($value).'</label>');
120 return $this->toString();
123 class RateCellRenderer extends DefaultCellRenderer {
124 function render(&$table, $value, $row, $column, $selected = false) {
125 global $assigned_projects;
127 $field = new FloatField('rate_'.$table->getValueAtName($row,'id'));
128 $field->setFormName($table->getFormName());
130 $field->setFormat('.2');
131 foreach ($assigned_projects as $p) {
132 if ($p['id'] == $table->getValueAtName($row,'id')) $field->setValue($p['rate']);
134 $this->setValue($field->getHtml());
135 return $this->toString();
138 // Create projects table.
139 $table = new Table('projects');
140 $table->setIAScript('setRate');
141 $table->setTableOptions(array('width'=>'100%','cellspacing'=>'1','cellpadding'=>'3','border'=>'0'));
142 $table->setRowOptions(array('valign'=>'top','class'=>'tableHeader'));
143 $table->setData($projects);
144 $table->setKeyField('id');
145 $table->setValue($cl_projects);
146 $table->addColumn(new TableColumn('name', $i18n->get('label.project'), new NameCellRenderer()));
147 $table->addColumn(new TableColumn('p_rate', $i18n->get('form.users.rate'), new RateCellRenderer()));
148 $form->addInputElement($table);
150 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$user_id));
151 $form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->get('button.save')));
153 if ($request->isPost()) {
154 // Validate user input.
155 if (!ttValidString($cl_name)) $err->add($i18n->get('error.field'), $i18n->get('label.person_name'));
156 if (!ttValidString($cl_login)) $err->add($i18n->get('error.field'), $i18n->get('label.login'));
157 if (!$auth->isPasswordExternal() && ($cl_password1 || $cl_password2)) {
158 if (!ttValidString($cl_password1)) $err->add($i18n->get('error.field'), $i18n->get('label.password'));
159 if (!ttValidString($cl_password2)) $err->add($i18n->get('error.field'), $i18n->get('label.confirm_password'));
160 if ($cl_password1 !== $cl_password2)
161 $err->add($i18n->get('error.not_equal'), $i18n->get('label.password'), $i18n->get('label.confirm_password'));
163 if (!ttValidEmail($cl_email, true)) $err->add($i18n->get('error.field'), $i18n->get('label.email'));
164 // Require selection of a client for a client role.
165 if ($user->isPluginEnabled('cl') && ttRoleHelper::isClientRole($cl_role_id) && !$cl_client_id) $err->add($i18n->get('error.client'));
166 if (!ttValidFloat($cl_rate, true)) $err->add($i18n->get('error.field'), $i18n->get('form.users.default_rate'));
169 $existing_user = ttUserHelper::getUserByLogin($cl_login);
170 if (!$existing_user || ($user_id == $existing_user['id'])) {
174 'login' => $cl_login,
175 'password' => $cl_password1,
176 'email' => $cl_email,
177 'status' => $cl_status,
179 'projects' => $assigned_projects);
180 if (in_array('manage_users', $user->rights) && $cl_role_id) {
181 $fields['role_id'] = $cl_role_id;
182 $fields['client_id'] = $cl_client_id;
185 if (ttUserHelper::update($user_id, $fields)) {
187 // If our own login changed, set new one in cookie to remember it.
188 if (($user_id == $user->id) && ($user->login != $cl_login)) {
189 setcookie('tt_login', $cl_login, time() + COOKIE_EXPIRE, '/');
192 // In case the name of the "on behalf" user has changed - set it in session.
193 if (($user->behalf_id == $user_id) && ($user->behalf_name != $cl_name)) {
194 $_SESSION['behalf_name'] = $cl_name;
197 // If we deactivated our own account, do housekeeping and logout.
198 if ($user->id == $user_id && !is_null($cl_status) && $cl_status == INACTIVE) {
199 // Remove tt_login cookie that stores login name.
200 unset($_COOKIE['tt_login']);
201 setcookie('tt_login', NULL, -1);
204 header('Location: login.php');
208 header('Location: users.php');
212 $err->add($i18n->get('error.db'));
214 $err->add($i18n->get('error.user_exists'));
219 if ($user->id == $user_id && $user->can('swap_roles')) {
220 $users_for_swap = ttTeamHelper::getUsersForSwap();
221 if (is_array($users_for_swap) && sizeof($users_for_swap) > 0)
225 $rates = ttProjectHelper::getRates($user_id);
226 $smarty->assign('rates', $rates);
228 $smarty->assign('auth_external', $auth->isPasswordExternal());
229 $smarty->assign('active_roles', $active_roles);
230 $smarty->assign('can_swap', $can_swap);
231 $smarty->assign('show_projects', count($projects) > 0);
232 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
233 $smarty->assign('onload', 'onLoad="document.userForm.name.focus();handleClientControl();"');
234 $smarty->assign('user_id', $user_id);
235 $smarty->assign('title', $i18n->get('title.edit_user'));
236 $smarty->assign('content_page_name', 'user_edit.tpl');
237 $smarty->display('index.tpl');