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('ttGroupHelper');
33 import('ttUserHelper');
35 import('form.TableColumn');
36 import('ttRoleHelper');
39 if (!ttAccessAllowed('manage_users')) {
40 header('Location: access_denied.php');
43 // End of access checks.
45 // Use the "limit" plugin if we have one. Ignore include errors.
46 // The "limit" plugin is not required for normal operation of the Time Tracker.
47 @include('plugins/limit/user_add.php');
49 if ($user->isPluginEnabled('cl'))
50 $clients = ttGroupHelper::getActiveClients();
52 $assigned_projects = array();
53 if ($request->isPost()) {
54 $cl_name = trim($request->getParameter('name'));
55 $cl_login = trim($request->getParameter('login'));
56 if (!$auth->isPasswordExternal()) {
57 $cl_password1 = $request->getParameter('pas1');
58 $cl_password2 = $request->getParameter('pas2');
60 $cl_email = trim($request->getParameter('email'));
61 $cl_role_id = $request->getParameter('role');
62 $cl_client_id = $request->getParameter('client');
63 $cl_rate = $request->getParameter('rate');
64 $cl_projects = $request->getParameter('projects');
65 if (is_array($cl_projects)) {
66 foreach ($cl_projects as $p) {
67 if (ttValidFloat($request->getParameter('rate_'.$p), true)) {
68 $project_with_rate = array();
69 $project_with_rate['id'] = $p;
70 $project_with_rate['rate'] = $request->getParameter('rate_'.$p);
71 $assigned_projects[] = $project_with_rate;
73 $err->add($i18n->get('error.field'), 'rate_'.$p);
78 $form = new Form('userForm');
79 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'name','style'=>'width: 300px;','value'=>$cl_name));
80 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'login','style'=>'width: 300px;','value'=>$cl_login));
81 if (!$auth->isPasswordExternal()) {
82 $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'pas1','value'=>$cl_password1));
83 $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'pas2','value'=>$cl_password2));
85 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'email','value'=>$cl_email));
87 $active_roles = ttTeamHelper::getActiveRolesForUser();
88 $form->addInput(array('type'=>'combobox','onchange'=>'handleClientControl()','name'=>'role','value'=>$cl_role_id,'data'=>$active_roles,'datakeys'=>array('id', 'name')));
89 if ($user->isPluginEnabled('cl'))
90 $form->addInput(array('type'=>'combobox','name'=>'client','value'=>$cl_client_id,'data'=>$clients,'datakeys'=>array('id', 'name'),'empty'=>array(''=>$i18n->get('dropdown.select'))));
92 $form->addInput(array('type'=>'floatfield','maxlength'=>'10','name'=>'rate','format'=>'.2','value'=>$cl_rate));
94 $projects = ttGroupHelper::getActiveProjects();
96 // Define classes for the projects table.
97 class NameCellRenderer extends DefaultCellRenderer {
98 function render(&$table, $value, $row, $column, $selected = false) {
99 $this->setOptions(array('width'=>200));
100 $this->setValue('<label for = "'.$table->name.'_'.$row.'">'.htmlspecialchars($value).'</label>');
101 return $this->toString();
104 class RateCellRenderer extends DefaultCellRenderer {
105 function render(&$table, $value, $row, $column, $selected = false) {
106 global $assigned_projects;
107 $field = new FloatField('rate_'.$table->getValueAtName($row, 'id'));
108 $field->setFormName($table->getFormName());
110 $field->setFormat('.2');
111 foreach ($assigned_projects as $p) {
112 if ($p['id'] == $table->getValueAtName($row,'id')) $field->setValue($p['rate']);
114 $this->setValue($field->getHtml());
115 return $this->toString();
118 // Create projects table.
119 $table = new Table('projects');
120 $table->setIAScript('setDefaultRate');
121 $table->setTableOptions(array('width'=>'100%','cellspacing'=>'1','cellpadding'=>'3','border'=>'0'));
122 $table->setRowOptions(array('valign'=>'top','class'=>'tableHeader'));
123 $table->setData($projects);
124 $table->setKeyField('id');
125 $table->setValue($cl_projects);
126 $table->addColumn(new TableColumn('name', $i18n->get('label.project'), new NameCellRenderer()));
127 $table->addColumn(new TableColumn('p_rate', $i18n->get('form.users.rate'), new RateCellRenderer()));
128 $form->addInputElement($table);
130 $form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->get('button.submit')));
132 if ($request->isPost()) {
133 // Validate user input.
134 if (!ttValidString($cl_name)) $err->add($i18n->get('error.field'), $i18n->get('label.person_name'));
135 if (!ttValidString($cl_login)) $err->add($i18n->get('error.field'), $i18n->get('label.login'));
136 if (!$auth->isPasswordExternal()) {
137 if (!ttValidString($cl_password1)) $err->add($i18n->get('error.field'), $i18n->get('label.password'));
138 if (!ttValidString($cl_password2)) $err->add($i18n->get('error.field'), $i18n->get('label.confirm_password'));
139 if ($cl_password1 !== $cl_password2)
140 $err->add($i18n->get('error.not_equal'), $i18n->get('label.password'), $i18n->get('label.confirm_password'));
142 if (!ttValidEmail($cl_email, true)) $err->add($i18n->get('error.field'), $i18n->get('label.email'));
143 // Require selection of a client for a client role.
144 if ($user->isPluginEnabled('cl') && ttRoleHelper::isClientRole($cl_role_id) && !$cl_client_id) $err->add($i18n->get('error.client'));
145 if (!ttValidFloat($cl_rate, true)) $err->add($i18n->get('error.field'), $i18n->get('form.users.default_rate'));
148 if (!ttUserHelper::getUserByLogin($cl_login)) {
151 'login' => $cl_login,
152 'password' => $cl_password1,
154 'group_id' => $user->getGroup(),
155 'org_id' => $user->org_id,
156 'role_id' => $cl_role_id,
157 'client_id' => $cl_client_id,
158 'projects' => $assigned_projects,
159 'email' => $cl_email);
160 $user_id = ttUserHelper::insert($fields);
162 if (!$user->exists()) {
163 // We added a user to an empty subgroup. Set new user as on behalf user.
164 // Needed for user-based things to work (such as notifications config).
165 $user->setOnBehalfUser($user_id);
167 header('Location: users.php');
170 $err->add($i18n->get('error.db'));
172 $err->add($i18n->get('error.user_exists'));
176 $smarty->assign('auth_external', $auth->isPasswordExternal());
177 $smarty->assign('active_roles', $active_roles);
178 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
179 $smarty->assign('show_projects', count($projects) > 0);
180 $smarty->assign('onload', 'onLoad="document.userForm.name.focus();handleClientControl();"');
181 $smarty->assign('title', $i18n->get('title.add_user'));
182 $smarty->assign('content_page_name', 'user_add.tpl');
183 $smarty->display('index.tpl');