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