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