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');
38 if (!ttAccessAllowed('manage_users')) {
39 header('Location: access_denied.php');
42 $user_id = (int)$request->getParameter('id');
43 $user_details = $user->getUser($user_id);
45 header('Location: access_denied.php');
48 // End of access checks.
50 if ($user->isPluginEnabled('cl'))
51 $clients = ttTeamHelper::getActiveClients($user->group_id);
53 $projects = ttTeamHelper::getActiveProjects($user->group_id);
54 $assigned_projects = array();
56 if ($request->isPost()) {
57 $cl_name = trim($request->getParameter('name'));
58 $cl_login = trim($request->getParameter('login'));
59 if (!$auth->isPasswordExternal()) {
60 $cl_password1 = $request->getParameter('pas1');
61 $cl_password2 = $request->getParameter('pas2');
63 $cl_email = trim($request->getParameter('email'));
64 $cl_role_id = $request->getParameter('role');
65 $cl_client_id = $request->getParameter('client');
66 $cl_status = $request->getParameter('status');
67 $cl_rate = $request->getParameter('rate');
68 $cl_projects = $request->getParameter('projects');
69 if (is_array($cl_projects)) {
70 foreach ($cl_projects as $p) {
71 if (ttValidFloat($request->getParameter('rate_'.$p), true)) {
72 $project_with_rate = array();
73 $project_with_rate['id'] = $p;
74 $project_with_rate['rate'] = $request->getParameter('rate_'.$p);
75 $assigned_projects[] = $project_with_rate;
77 $err->add($i18n->get('error.field'), 'rate_'.$p);
81 $cl_name = $user_details['name'];
82 $cl_login = $user_details['login'];
83 $cl_email = $user_details['email'];
84 $cl_rate = str_replace('.', $user->decimal_mark, $user_details['rate']);
85 $cl_role_id = $user_details['role_id'];
86 $cl_client_id = $user_details['client_id'];
87 $cl_status = $user_details['status'];
88 $cl_projects = array();
89 $assigned_projects = ttProjectHelper::getAssignedProjects($user_id);
90 foreach($assigned_projects as $p) {
91 $cl_projects[] = $p['id'];
95 $form = new Form('userForm');
96 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'name','value'=>$cl_name));
97 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'login','value'=>$cl_login));
98 if (!$auth->isPasswordExternal()) {
99 $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'pas1','value'=>$cl_password1));
100 $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'pas2','value'=>$cl_password2));
102 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'email','value'=>$cl_email));
104 $active_roles = ttTeamHelper::getActiveRolesForUser();
105 $form->addInput(array('type'=>'combobox','onchange'=>'handleClientControl()','name'=>'role','value'=>$cl_role_id,'data'=>$active_roles,'datakeys'=>array('id', 'name')));
106 if ($user->isPluginEnabled('cl'))
107 $form->addInput(array('type'=>'combobox','name'=>'client','value'=>$cl_client_id,'data'=>$clients,'datakeys'=>array('id', 'name'),'empty'=>array(''=>$i18n->get('dropdown.select'))));
109 $form->addInput(array('type'=>'combobox','name'=>'status','value'=>$cl_status,
110 'data'=>array(ACTIVE=>$i18n->get('dropdown.status_active'),INACTIVE=>$i18n->get('dropdown.status_inactive'))));
111 $form->addInput(array('type'=>'floatfield','maxlength'=>'10','name'=>'rate','format'=>'.2','value'=>$cl_rate));
113 // Define classes for the projects table.
114 class NameCellRenderer extends DefaultCellRenderer {
115 function render(&$table, $value, $row, $column, $selected = false) {
116 $this->setOptions(array('width'=>200,'valign'=>'top'));
117 $this->setValue('<label for = "'.$table->getName().'_'.$row.'">'.htmlspecialchars($value).'</label>');
118 return $this->toString();
121 class RateCellRenderer extends DefaultCellRenderer {
122 function render(&$table, $value, $row, $column, $selected = false) {
123 global $assigned_projects;
124 $field = new FloatField('rate_'.$table->getValueAtName($row,'id'));
125 $field->setFormName($table->getFormName());
127 $field->setFormat('.2');
128 foreach ($assigned_projects as $p) {
129 if ($p['id'] == $table->getValueAtName($row,'id')) $field->setValue($p['rate']);
131 $this->setValue($field->getHtml());
132 return $this->toString();
135 // Create projects table.
136 $table = new Table('projects');
137 $table->setIAScript('setRate');
138 $table->setTableOptions(array('width'=>'100%','cellspacing'=>'1','cellpadding'=>'3','border'=>'0'));
139 $table->setRowOptions(array('valign'=>'top','class'=>'tableHeader'));
140 $table->setData($projects);
141 $table->setKeyField('id');
142 $table->setValue($cl_projects);
143 $table->addColumn(new TableColumn('name', $i18n->get('label.project'), new NameCellRenderer()));
144 $table->addColumn(new TableColumn('p_rate', $i18n->get('form.users.rate'), new RateCellRenderer()));
145 $form->addInputElement($table);
147 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$user_id));
148 $form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->get('button.save')));
149 $form->addInput(array('type'=>'submit','name'=>'btn_delete','value'=>$i18n->get('label.delete')));
151 if ($request->isPost()) {
152 if ($request->getParameter('btn_submit')) {
153 // Validate user input.
154 if (!ttValidString($cl_name)) $err->add($i18n->get('error.field'), $i18n->get('label.person_name'));
155 if (!ttValidString($cl_login)) $err->add($i18n->get('error.field'), $i18n->get('label.login'));
156 if (!$auth->isPasswordExternal() && ($cl_password1 || $cl_password2)) {
157 if (!ttValidString($cl_password1)) $err->add($i18n->get('error.field'), $i18n->get('label.password'));
158 if (!ttValidString($cl_password2)) $err->add($i18n->get('error.field'), $i18n->get('label.confirm_password'));
159 if ($cl_password1 !== $cl_password2)
160 $err->add($i18n->get('error.not_equal'), $i18n->get('label.password'), $i18n->get('label.confirm_password'));
162 if (!ttValidEmail($cl_email, true)) $err->add($i18n->get('error.field'), $i18n->get('label.email'));
163 if (!ttValidFloat($cl_rate, true)) $err->add($i18n->get('error.field'), $i18n->get('form.users.default_rate'));
166 $existing_user = ttUserHelper::getUserByLogin($cl_login);
167 if (!$existing_user || ($user_id == $existing_user['id'])) {
171 'login' => $cl_login,
172 'password' => $cl_password1,
173 'email' => $cl_email,
174 'status' => $cl_status,
176 'projects' => $assigned_projects);
177 if (in_array('manage_users', $user->rights)) {
178 $fields['role_id'] = $cl_role_id;
179 $fields['client_id'] = $cl_client_id;
182 if (ttUserHelper::update($user_id, $fields)) {
184 // If our own login changed, set new one in cookie to remember it.
185 if (($user_id == $user->id) && ($user->login != $cl_login)) {
186 setcookie('tt_login', $cl_login, time() + COOKIE_EXPIRE, '/');
189 // In case the name of the "on behalf" user has changed - set it in session.
190 if (($user->behalf_id == $user_id) && ($user->behalf_name != $cl_name)) {
191 $_SESSION['behalf_name'] = $cl_name;
194 // If we deactivated our own account, do housekeeping and logout.
195 if ($user->id == $user_id && !is_null($cl_status) && $cl_status == INACTIVE) {
196 // Remove tt_login cookie that stores login name.
197 unset($_COOKIE['tt_login']);
198 setcookie('tt_login', NULL, -1);
201 header('Location: login.php');
205 header('Location: users.php');
209 $err->add($i18n->get('error.db'));
211 $err->add($i18n->get('error.user_exists'));
215 if ($request->getParameter('btn_delete')) {
216 header("Location: user_delete.php?id=$user_id");
221 $rates = ttProjectHelper::getRates($user_id);
222 $smarty->assign('rates', $rates);
224 $smarty->assign('auth_external', $auth->isPasswordExternal());
225 $smarty->assign('active_roles', $active_roles);
226 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
227 $smarty->assign('onload', 'onLoad="document.userForm.name.focus();handleClientControl();"');
228 $smarty->assign('user_id', $user_id);
229 $smarty->assign('title', $i18n->get('title.edit_user'));
230 $smarty->assign('content_page_name', 'mobile/user_edit.tpl');
231 $smarty->display('mobile/index.tpl');