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