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