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');
37 if (!ttAccessCheck(right_manage_team)) {
38 header('Location: access_denied.php');
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');
46 if ($user->isPluginEnabled('cl'))
47 $clients = ttTeamHelper::getActiveClients($user->team_id);
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');
57 $cl_email = trim($request->getParameter('email'));
58 $cl_role = $request->getParameter('role');
59 if (!$cl_role) $cl_role = ROLE_USER;
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 $roles[ROLE_USER] = $i18n->getKey('label.user');
86 $roles[ROLE_COMANAGER] = $i18n->getKey('form.users.comanager');
87 if ($user->isPluginEnabled('cl'))
88 $roles[ROLE_CLIENT] = $i18n->getKey('label.client');
89 $form->addInput(array('type'=>'combobox','onchange'=>'handleClientControl()','name'=>'role','value'=>$cl_role,'data'=>$roles));
90 if ($user->isPluginEnabled('cl'))
91 $form->addInput(array('type'=>'combobox','name'=>'client','value'=>$cl_client_id,'data'=>$clients,'datakeys'=>array('id', 'name'),'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
93 $form->addInput(array('type'=>'floatfield','maxlength'=>'10','name'=>'rate','format'=>'.2','value'=>$cl_rate));
95 $projects = ttTeamHelper::getActiveProjects($user->team_id);
97 // Define classes for the projects table.
98 class NameCellRenderer extends DefaultCellRenderer {
99 function render(&$table, $value, $row, $column, $selected = false) {
100 $this->setOptions(array('width'=>200));
101 $this->setValue('<label for = "'.$table->name.'_'.$row.'">'.htmlspecialchars($value).'</label>');
102 return $this->toString();
105 class RateCellRenderer extends DefaultCellRenderer {
106 function render(&$table, $value, $row, $column, $selected = false) {
107 global $assigned_projects;
108 $field = new FloatField('rate_'.$table->getValueAtName($row, 'id'));
109 $field->setFormName($table->getFormName());
111 $field->setFormat('.2');
112 foreach ($assigned_projects as $p) {
113 if ($p['id'] == $table->getValueAtName($row,'id')) $field->setValue($p['rate']);
115 $this->setValue($field->getHtml());
116 return $this->toString();
119 // Create projects table.
120 $table = new Table('projects');
121 $table->setIAScript('setDefaultRate');
122 $table->setTableOptions(array('width'=>'100%','cellspacing'=>'1','cellpadding'=>'3','border'=>'0'));
123 $table->setRowOptions(array('valign'=>'top','class'=>'tableHeader'));
124 $table->setData($projects);
125 $table->setKeyField('id');
126 $table->setValue($cl_projects);
127 $table->addColumn(new TableColumn('name', $i18n->getKey('label.project'), new NameCellRenderer()));
128 $table->addColumn(new TableColumn('p_rate', $i18n->getKey('form.users.rate'), new RateCellRenderer()));
129 $form->addInputElement($table);
131 $form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->getKey('button.submit')));
133 if ($request->isPost()) {
134 // Validate user input.
135 if (!ttValidString($cl_name)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.person_name'));
136 if (!ttValidString($cl_login)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.login'));
137 if (!$auth->isPasswordExternal()) {
138 if (!ttValidString($cl_password1)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.password'));
139 if (!ttValidString($cl_password2)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.confirm_password'));
140 if ($cl_password1 !== $cl_password2)
141 $err->add($i18n->getKey('error.not_equal'), $i18n->getKey('label.password'), $i18n->getKey('label.confirm_password'));
143 if (!ttValidEmail($cl_email, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.email'));
144 // Require selection of a client for a client role.
145 if ($user->isPluginEnabled('cl') && $cl_role == ROLE_CLIENT && !$cl_client_id) $err->add($i18n->getKey('error.client'));
146 if (!ttValidFloat($cl_rate, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('form.users.default_rate'));
149 if (!ttUserHelper::getUserByLogin($cl_login)) {
152 'login' => $cl_login,
153 'password' => $cl_password1,
155 'team_id' => $user->team_id,
157 'client_id' => $cl_client_id,
158 'projects' => $assigned_projects,
159 'email' => $cl_email);
160 if (ttUserHelper::insert($fields)) {
161 header('Location: users.php');
164 $err->add($i18n->getKey('error.db'));
166 $err->add($i18n->getKey('error.user_exists'));
170 $smarty->assign('auth_external', $auth->isPasswordExternal());
171 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
172 $smarty->assign('onload', 'onLoad="document.userForm.name.focus();handleClientControl();"');
173 $smarty->assign('title', $i18n->getKey('title.add_user'));
174 $smarty->assign('content_page_name', 'user_add.tpl');
175 $smarty->display('index.tpl');