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 $show_quota = $user->isPluginEnabled('mq');
53 if ($user->isPluginEnabled('cl'))
54 $clients = ttGroupHelper::getActiveClients();
56 // Use custom fields plugin if it is enabled.
57 if ($user->isPluginEnabled('cf')) {
58 require_once('plugins/CustomFields.class.php');
59 $custom_fields = new CustomFields();
60 $smarty->assign('custom_fields', $custom_fields);
63 $show_projects = MODE_PROJECTS == $user->getTrackingMode() || MODE_PROJECTS_AND_TASKS == $user->getTrackingMode();
65 $projects = ttGroupHelper::getActiveProjects();
66 if (count($projects) == 0) $show_projects = false;
68 $assigned_projects = array();
70 if ($request->isPost()) {
71 $cl_name = trim($request->getParameter('name'));
72 $cl_login = trim($request->getParameter('login'));
73 if (!$auth->isPasswordExternal()) {
74 $cl_password1 = $request->getParameter('pas1');
75 $cl_password2 = $request->getParameter('pas2');
77 $cl_email = trim($request->getParameter('email'));
78 $cl_role_id = $request->getParameter('role');
79 $cl_client_id = $request->getParameter('client');
80 $cl_status = $request->getParameter('status');
81 $cl_quota_percent = $request->getParameter('quota_percent');
82 // If we have user custom fields - collect input.
83 if ($custom_fields && $custom_fields->userFields) {
84 foreach ($custom_fields->userFields as $userField) {
85 $control_name = 'user_field_'.$userField['id'];
86 $userCustomFields[$userField['id']] = array('field_id' => $userField['id'],
87 'control_name' => $control_name,
88 'label' => $userField['label'],
89 'type' => $userField['type'],
90 'required' => $userField['required'],
91 'value' => trim($request->getParameter($control_name)));
94 $cl_rate = $request->getParameter('rate');
95 $cl_projects = $request->getParameter('projects');
96 if (is_array($cl_projects)) {
97 foreach ($cl_projects as $p) {
98 if (ttValidFloat($request->getParameter('rate_'.$p), true)) {
99 $project_with_rate = array();
100 $project_with_rate['id'] = $p;
101 $project_with_rate['rate'] = $request->getParameter('rate_'.$p);
102 $assigned_projects[] = $project_with_rate;
104 $err->add($i18n->get('error.field'), 'rate_'.$p);
108 $cl_name = $user_details['name'];
109 $cl_login = $user_details['login'];
110 $cl_email = $user_details['email'];
111 $cl_quota_percent = str_replace('.', $user->getDecimalMark(), $user_details['quota_percent']);
112 // If we have user custom fields - collect vallues from database.
113 if ($custom_fields && $custom_fields->userFields) {
114 foreach ($custom_fields->userFields as $userField) {
115 $control_name = 'user_field_'.$userField['id'];
116 $userCustomFields[$userField['id']] = array('field_id' => $userField['id'],
117 'control_name' => $control_name,
118 'label' => $userField['label'],
119 'type' => $userField['type'],
120 'required' => $userField['required'],
121 'value' => $custom_fields->getEntityFieldValue(CustomFields::ENTITY_USER, $user_id, $userField['id'], $userField['type']));
124 $cl_rate = str_replace('.', $user->getDecimalMark(), $user_details['rate']);
125 $cl_role_id = $user_details['role_id'];
126 $cl_client_id = $user_details['client_id'];
127 $cl_status = $user_details['status'];
128 $cl_projects = array();
129 $assigned_projects = ttProjectHelper::getAssignedProjects($user_id);
130 foreach($assigned_projects as $p) {
131 $cl_projects[] = $p['id'];
135 $form = new Form('userForm');
136 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'name','style'=>'width: 300px;','value'=>$cl_name));
137 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'login','style'=>'width: 300px;','value'=>$cl_login));
138 if (!$auth->isPasswordExternal()) {
139 $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'pas1','value'=>$cl_password1));
140 $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'pas2','value'=>$cl_password2));
142 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'email','style'=>'width: 300px;','value'=>$cl_email));
144 $active_roles = ttTeamHelper::getActiveRolesForUser();
145 $form->addInput(array('type'=>'combobox','onchange'=>'handleClientControl()','name'=>'role','value'=>$cl_role_id,'data'=>$active_roles, 'datakeys'=>array('id', 'name')));
146 if ($user->isPluginEnabled('cl'))
147 $form->addInput(array('type'=>'combobox','name'=>'client','value'=>$cl_client_id,'data'=>$clients,'datakeys'=>array('id', 'name'),'empty'=>array(''=>$i18n->get('dropdown.select'))));
149 // If we have custom fields - add controls for them.
150 if ($custom_fields && $custom_fields->userFields) {
151 foreach ($custom_fields->userFields as $userField) {
152 $field_name = 'user_field_'.$userField['id'];
153 if ($userField['type'] == CustomFields::TYPE_TEXT) {
154 $form->addInput(array('type'=>'text','name'=>$field_name,'value'=>$userCustomFields[$userField['id']]['value']));
155 } elseif ($userField['type'] == CustomFields::TYPE_DROPDOWN) {
156 $form->addInput(array('type'=>'combobox','name'=>$field_name,
157 'style'=>'width: 250px;',
158 'data'=>CustomFields::getOptions($userField['id']),
159 'value'=>$userCustomFields[$userField['id']]['value'],
160 'empty'=>array(''=>$i18n->get('dropdown.select'))));
165 $form->addInput(array('type'=>'combobox','name'=>'status','value'=>$cl_status,
166 'data'=>array(ACTIVE=>$i18n->get('dropdown.status_active'),INACTIVE=>$i18n->get('dropdown.status_inactive'))));
167 $form->addInput(array('type'=>'floatfield','maxlength'=>'10','name'=>'rate','format'=>'.2','value'=>$cl_rate));
169 $form->addInput(array('type'=>'floatfield','maxlength'=>'10','name'=>'quota_percent','format'=>'.2','value'=>$cl_quota_percent));
171 // Define classes for the projects table.
172 class NameCellRenderer extends DefaultCellRenderer {
173 function render(&$table, $value, $row, $column, $selected = false) {
174 $this->setOptions(array('width'=>200));
175 $this->setValue('<label for = "'.$table->name.'_'.$row.'">'.htmlspecialchars($value).'</label>');
176 return $this->toString();
179 class RateCellRenderer extends DefaultCellRenderer {
180 function render(&$table, $value, $row, $column, $selected = false) {
181 global $assigned_projects;
183 $field = new FloatField('rate_'.$table->getValueAtName($row,'id'));
184 $field->setFormName($table->getFormName());
186 $field->setFormat('.2');
187 foreach ($assigned_projects as $p) {
188 if ($p['id'] == $table->getValueAtName($row,'id')) $field->setValue($p['rate']);
190 $this->setValue($field->getHtml());
191 return $this->toString();
194 // Create projects table.
195 $table = new Table('projects');
196 $table->setIAScript('setRate');
197 $table->setTableOptions(array('width'=>'300','cellspacing'=>'1','cellpadding'=>'3','border'=>'0'));
198 $table->setRowOptions(array('valign'=>'top','class'=>'tableHeader'));
199 $table->setData($projects);
200 $table->setKeyField('id');
201 $table->setValue($cl_projects);
202 $table->addColumn(new TableColumn('name', $i18n->get('label.project'), new NameCellRenderer()));
203 $table->addColumn(new TableColumn('p_rate', $i18n->get('form.users.rate'), new RateCellRenderer()));
204 $form->addInputElement($table);
206 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$user_id));
207 $form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->get('button.save')));
209 if ($request->isPost()) {
210 // Validate user input.
211 if (!ttValidString($cl_name)) $err->add($i18n->get('error.field'), $i18n->get('label.person_name'));
212 if (!ttValidString($cl_login)) $err->add($i18n->get('error.field'), $i18n->get('label.login'));
213 if (!$auth->isPasswordExternal() && ($cl_password1 || $cl_password2)) {
214 if (!ttValidString($cl_password1)) $err->add($i18n->get('error.field'), $i18n->get('label.password'));
215 if (!ttValidString($cl_password2)) $err->add($i18n->get('error.field'), $i18n->get('label.confirm_password'));
216 if ($cl_password1 !== $cl_password2)
217 $err->add($i18n->get('error.not_equal'), $i18n->get('label.password'), $i18n->get('label.confirm_password'));
219 if (!ttValidEmail($cl_email, true)) $err->add($i18n->get('error.field'), $i18n->get('label.email'));
220 // Require selection of a client for a client role.
221 if ($user->isPluginEnabled('cl') && ttRoleHelper::isClientRole($cl_role_id) && !$cl_client_id) $err->add($i18n->get('error.client'));
222 if (!ttValidFloat($cl_quota_percent, true)) $err->add($i18n->get('error.field'), $i18n->get('label.quota'));
223 // Validate input in user custom fields.
224 if ($custom_fields && $custom_fields->userFields) {
225 foreach ($userCustomFields as $userField) {
226 // Validation is the same for text and dropdown fields.
227 if (!ttValidString($userField['value'], !$userField['required'])) $err->add($i18n->get('error.field'), htmlspecialchars($userField['label']));
230 if (!ttValidFloat($cl_rate, true)) $err->add($i18n->get('error.field'), $i18n->get('form.users.default_rate'));
233 $existing_user = ttUserHelper::getUserByLogin($cl_login);
234 if (!$existing_user || ($user_id == $existing_user['id'])) {
238 'login' => $cl_login,
239 'password' => $cl_password1,
240 'email' => $cl_email,
241 'status' => $cl_status,
243 'quota_percent' => $cl_quota_percent,
244 'projects' => $assigned_projects);
245 if (in_array('manage_users', $user->rights) && $cl_role_id) {
246 $fields['role_id'] = $cl_role_id;
247 $fields['client_id'] = $cl_client_id;
250 $result = ttUserHelper::update($user_id, $fields);
251 // Update user custom fields if we have them.
252 if ($result && $custom_fields && $custom_fields->userFields) {
253 $result = $custom_fields->updateEntityFields(CustomFields::ENTITY_USER, $user_id, $userCustomFields);
257 // If our own login changed, set new one in cookie to remember it.
258 if (($user_id == $user->id) && ($user->login != $cl_login)) {
259 setcookie('tt_login', $cl_login, time() + COOKIE_EXPIRE, '/');
262 // In case the name of the "on behalf" user has changed - set it in session.
263 if (($user->behalf_id == $user_id) && ($user->behalf_name != $cl_name)) {
264 $_SESSION['behalf_name'] = $cl_name;
267 // If we deactivated our own account, do housekeeping and logout.
268 if ($user->id == $user_id && !is_null($cl_status) && $cl_status == INACTIVE) {
269 // Remove tt_login cookie that stores login name.
270 unset($_COOKIE['tt_login']);
271 setcookie('tt_login', NULL, -1);
274 header('Location: login.php');
278 header('Location: users.php');
282 $err->add($i18n->get('error.db'));
284 $err->add($i18n->get('error.user_exists'));
289 if ($user->id == $user_id && $user->can('swap_roles')) {
290 $users_for_swap = ttTeamHelper::getUsersForSwap();
291 if (is_array($users_for_swap) && sizeof($users_for_swap) > 0)
295 $rates = ttProjectHelper::getRates($user_id);
296 $smarty->assign('rates', $rates);
298 $smarty->assign('auth_external', $auth->isPasswordExternal());
299 $smarty->assign('active_roles', $active_roles);
300 $smarty->assign('can_swap', $can_swap);
301 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
302 $smarty->assign('onload', 'onLoad="document.userForm.name.focus();handleClientControl();"');
303 $smarty->assign('show_quota', $show_quota);
304 $smarty->assign('show_projects', $show_projects);
305 $smarty->assign('user_id', $user_id);
306 $smarty->assign('title', $i18n->get('title.edit_user'));
307 $smarty->assign('content_page_name', 'user_edit.tpl');
308 $smarty->display('index.tpl');