A few bugs fixed related to role revamp.
[timetracker.git] / mobile / user_add.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('ttTeamHelper');
32 import('ttUserHelper');
33 import('form.Table');
34 import('form.TableColumn');
35
36 // Access check.
37 if (!ttAccessAllowed('manage_users')) {
38   header('Location: access_denied.php');
39   exit();
40 }
41
42 // Use the "limit" plugin if we have one. Ignore include errors.
43 // The "limit" plugin is not required for normal operation of the Time Tracker.
44 @include('plugins/limit/user_add.php');
45
46 if ($user->isPluginEnabled('cl'))
47   $clients = ttTeamHelper::getActiveClients($user->team_id);
48
49 $assigned_projects = array();
50 if ($request->isPost()) {
51   $cl_name = trim($request->getParameter('name'));
52   $cl_login = trim($request->getParameter('login'));
53   if (!$auth->isPasswordExternal()) {
54     $cl_password1 = $request->getParameter('pas1');
55     $cl_password2 = $request->getParameter('pas2');
56   }
57   $cl_email = trim($request->getParameter('email'));
58   $cl_role_id = $request->getParameter('role');
59   $cl_client_id = $request->getParameter('client');
60   $cl_rate = $request->getParameter('rate');
61   $cl_projects = $request->getParameter('projects');
62   if (is_array($cl_projects)) {
63     foreach ($cl_projects as $p) {
64       if (ttValidFloat($request->getParameter('rate_'.$p), true)) {
65         $project_with_rate = array();
66         $project_with_rate['id'] = $p;
67         $project_with_rate['rate'] = $request->getParameter('rate_'.$p);
68         $assigned_projects[] = $project_with_rate;
69       } else
70         $err->add($i18n->getKey('error.field'), 'rate_'.$p);
71     }
72   }
73 }
74
75 $form = new Form('userForm');
76 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'name','value'=>$cl_name));
77 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'login','value'=>$cl_login));
78 if (!$auth->isPasswordExternal()) {
79   $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'pas1','value'=>$cl_password1));
80   $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'pas2','value'=>$cl_password2));
81 }
82 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'email','value'=>$cl_email));
83
84 $active_roles = ttTeamHelper::getActiveRolesForUser();
85 $form->addInput(array('type'=>'combobox','onchange'=>'handleClientControl()','name'=>'role','value'=>$cl_role_id,'data'=>$active_roles,'datakeys'=>array('id', 'name')));
86 if ($user->isPluginEnabled('cl'))
87   $form->addInput(array('type'=>'combobox','name'=>'client','value'=>$cl_client_id,'data'=>$clients,'datakeys'=>array('id', 'name'),'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
88
89 $form->addInput(array('type'=>'floatfield','maxlength'=>'10','name'=>'rate','format'=>'.2','value'=>$cl_rate));
90
91 $projects = ttTeamHelper::getActiveProjects($user->team_id);
92
93 // Define classes for the projects table.
94 class NameCellRenderer extends DefaultCellRenderer {
95   function render(&$table, $value, $row, $column, $selected = false) {
96     $this->setOptions(array('width'=>200,'valign'=>'top'));
97     $this->setValue('<label for = "'.$table->getName().'_'.$row.'">'.htmlspecialchars($value).'</label>');
98     return $this->toString();
99   }
100 }
101 class RateCellRenderer extends DefaultCellRenderer {
102   function render(&$table, $value, $row, $column, $selected = false) {
103     global $assigned_projects;
104     $field = new FloatField('rate_'.$table->getValueAtName($row, 'id'));
105     $field->setFormName($table->getFormName());
106     $field->setSize(5);
107     $field->setFormat('.2');
108     foreach ($assigned_projects as $p) {
109       if ($p['id'] == $table->getValueAtName($row,'id')) $field->setValue($p['rate']);
110     }
111     $this->setValue($field->getHtml());
112     return $this->toString();
113   }
114 }
115 // Create projects table.
116 $table = new Table('projects');
117 $table->setIAScript('setDefaultRate');
118 $table->setTableOptions(array('width'=>'100%','cellspacing'=>'1','cellpadding'=>'3','border'=>'0'));
119 $table->setRowOptions(array('valign'=>'top','class'=>'tableHeader'));
120 $table->setData($projects);
121 $table->setKeyField('id');
122 $table->setValue($cl_projects);
123 $table->addColumn(new TableColumn('name', $i18n->getKey('label.project'), new NameCellRenderer()));
124 $table->addColumn(new TableColumn('p_rate', $i18n->getKey('form.users.rate'), new RateCellRenderer()));
125 $form->addInputElement($table);
126
127 $form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->getKey('button.submit')));
128
129 if ($request->isPost()) {
130   // Validate user input.
131   if (!ttValidString($cl_name)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.person_name'));
132   if (!ttValidString($cl_login)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.login'));
133   if (!$auth->isPasswordExternal()) {
134     if (!ttValidString($cl_password1)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.password'));
135     if (!ttValidString($cl_password2)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.confirm_password'));
136     if ($cl_password1 !== $cl_password2)
137       $err->add($i18n->getKey('error.not_equal'), $i18n->getKey('label.password'), $i18n->getKey('label.confirm_password'));
138   }
139   if (!ttValidEmail($cl_email, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.email'));
140   if (!ttValidFloat($cl_rate, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('form.users.default_rate'));
141
142   if ($err->no()) {
143     if (!ttUserHelper::getUserByLogin($cl_login)) {
144       $fields = array(
145         'name' => $cl_name,
146         'login' => $cl_login,
147         'password' => $cl_password1,
148         'rate' => $cl_rate,
149         'team_id' => $user->team_id,
150         'role_id' => $cl_role_id,
151         'client_id' => $cl_client_id,
152         'projects' => $assigned_projects,
153         'email' => $cl_email);
154       if (ttUserHelper::insert($fields)) {
155         header('Location: users.php');
156         exit();
157       } else
158         $err->add($i18n->getKey('error.db'));
159     } else
160       $err->add($i18n->getKey('error.user_exists'));
161   }
162 } // isPost
163
164 $smarty->assign('auth_external', $auth->isPasswordExternal());
165 $smarty->assign('active_roles', $active_roles);
166 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
167 $smarty->assign('onload', 'onLoad="document.userForm.name.focus();handleClientControl();"');
168 $smarty->assign('title', $i18n->getKey('title.add_user'));
169 $smarty->assign('content_page_name', 'mobile/user_add.tpl');
170 $smarty->display('mobile/index.tpl');