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