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 Time Tracker.
47 @include('plugins/limit/user_add.php');
49 $show_quota = $user->isPluginEnabled('mq');
50 if ($user->isPluginEnabled('cl'))
51 $clients = ttGroupHelper::getActiveClients();
53 // Use custom fields plugin if it is enabled.
54 if ($user->isPluginEnabled('cf')) {
55 require_once('plugins/CustomFields.class.php');
56 $custom_fields = new CustomFields();
57 $smarty->assign('custom_fields', $custom_fields);
60 $assigned_projects = array();
61 if ($request->isPost()) {
62 $cl_name = trim($request->getParameter('name'));
63 $cl_login = trim($request->getParameter('login'));
64 if (!$auth->isPasswordExternal()) {
65 $cl_password1 = $request->getParameter('pas1');
66 $cl_password2 = $request->getParameter('pas2');
68 $cl_email = trim($request->getParameter('email'));
69 $cl_role_id = $request->getParameter('role');
70 $cl_client_id = $request->getParameter('client');
71 $cl_rate = $request->getParameter('rate');
72 $cl_quota_percent = $request->getParameter('quota_percent');
73 $cl_projects = $request->getParameter('projects');
74 if (is_array($cl_projects)) {
75 foreach ($cl_projects as $p) {
76 if (ttValidFloat($request->getParameter('rate_'.$p), true)) {
77 $project_with_rate = array();
78 $project_with_rate['id'] = $p;
79 $project_with_rate['rate'] = $request->getParameter('rate_'.$p);
80 $assigned_projects[] = $project_with_rate;
82 $err->add($i18n->get('error.field'), 'rate_'.$p);
87 $form = new Form('userForm');
88 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'name','style'=>'width: 300px;','value'=>$cl_name));
89 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'login','style'=>'width: 300px;','value'=>$cl_login));
90 if (!$auth->isPasswordExternal()) {
91 $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'pas1','value'=>$cl_password1));
92 $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'pas2','value'=>$cl_password2));
94 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'email','value'=>$cl_email));
96 $active_roles = ttTeamHelper::getActiveRolesForUser();
97 $form->addInput(array('type'=>'combobox','onchange'=>'handleClientControl()','name'=>'role','value'=>$cl_role_id,'data'=>$active_roles,'datakeys'=>array('id', 'name')));
98 if ($user->isPluginEnabled('cl'))
99 $form->addInput(array('type'=>'combobox','name'=>'client','value'=>$cl_client_id,'data'=>$clients,'datakeys'=>array('id', 'name'),'empty'=>array(''=>$i18n->get('dropdown.select'))));
101 // If we have custom fields - add controls for them.
102 if ($custom_fields && $custom_fields->userFields) {
103 foreach ($custom_fields->userFields as $userField) {
104 $field_name = 'user_field_'.$userField['id'];
105 if ($userField['type'] == CustomFields::TYPE_TEXT) {
106 $form->addInput(array('type'=>'text','name'=>$field_name));
107 } elseif ($userField['type'] == CustomFields::TYPE_DROPDOWN) {
108 $form->addInput(array('type'=>'combobox','name'=>$field_name,
109 'style'=>'width: 250px;',
110 'data'=>CustomFields::getOptions($userField['id']),
111 'empty'=>array(''=>$i18n->get('dropdown.select'))));
116 $form->addInput(array('type'=>'floatfield','maxlength'=>'10','name'=>'rate','format'=>'.2','value'=>$cl_rate));
118 $form->addInput(array('type'=>'floatfield','maxlength'=>'10','name'=>'quota_percent','format'=>'.2','value'=>$cl_quota_percent));
120 $show_projects = MODE_PROJECTS == $user->getTrackingMode() || MODE_PROJECTS_AND_TASKS == $user->getTrackingMode();
121 if ($show_projects) {
122 $projects = ttGroupHelper::getActiveProjects();
123 if (count($projects) == 0) $show_projects = false;
126 // Define classes for the projects table.
127 class NameCellRenderer extends DefaultCellRenderer {
128 function render(&$table, $value, $row, $column, $selected = false) {
129 $this->setOptions(array('width'=>200));
130 $this->setValue('<label for = "'.$table->name.'_'.$row.'">'.htmlspecialchars($value).'</label>');
131 return $this->toString();
134 class RateCellRenderer extends DefaultCellRenderer {
135 function render(&$table, $value, $row, $column, $selected = false) {
136 global $assigned_projects;
137 $field = new FloatField('rate_'.$table->getValueAtName($row, 'id'));
138 $field->setFormName($table->getFormName());
140 $field->setFormat('.2');
141 foreach ($assigned_projects as $p) {
142 if ($p['id'] == $table->getValueAtName($row,'id')) $field->setValue($p['rate']);
144 $this->setValue($field->getHtml());
145 return $this->toString();
148 // Create projects table.
149 $table = new Table('projects');
150 $table->setIAScript('setDefaultRate');
151 $table->setTableOptions(array('width'=>'300','cellspacing'=>'1','cellpadding'=>'3','border'=>'0'));
152 $table->setRowOptions(array('valign'=>'top','class'=>'tableHeader'));
153 $table->setData($projects);
154 $table->setKeyField('id');
155 $table->setValue($cl_projects);
156 $table->addColumn(new TableColumn('name', $i18n->get('label.project'), new NameCellRenderer()));
157 $table->addColumn(new TableColumn('p_rate', $i18n->get('form.users.rate'), new RateCellRenderer()));
158 $form->addInputElement($table);
160 $form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->get('button.submit')));
162 if ($request->isPost()) {
163 // Validate user input.
164 if (!ttValidString($cl_name)) $err->add($i18n->get('error.field'), $i18n->get('label.person_name'));
165 if (!ttValidString($cl_login)) $err->add($i18n->get('error.field'), $i18n->get('label.login'));
166 if (!$auth->isPasswordExternal()) {
167 if (!ttValidString($cl_password1)) $err->add($i18n->get('error.field'), $i18n->get('label.password'));
168 if (!ttValidString($cl_password2)) $err->add($i18n->get('error.field'), $i18n->get('label.confirm_password'));
169 if ($cl_password1 !== $cl_password2)
170 $err->add($i18n->get('error.not_equal'), $i18n->get('label.password'), $i18n->get('label.confirm_password'));
172 if (!ttValidEmail($cl_email, true)) $err->add($i18n->get('error.field'), $i18n->get('label.email'));
173 // Require selection of a client for a client role.
174 if ($user->isPluginEnabled('cl') && ttRoleHelper::isClientRole($cl_role_id) && !$cl_client_id) $err->add($i18n->get('error.client'));
175 if (!ttValidFloat($cl_rate, true)) $err->add($i18n->get('error.field'), $i18n->get('form.users.default_rate'));
176 if (!ttValidFloat($cl_quota_percent, true)) $err->add($i18n->get('error.field'), $i18n->get('label.quota'));
177 if (!ttUserHelper::canAdd()) $err->add($i18n->get('error.user_count'));
180 if (!ttUserHelper::getUserByLogin($cl_login)) {
183 'login' => $cl_login,
184 'password' => $cl_password1,
186 'quota_percent' => $cl_quota_percent,
187 'group_id' => $user->getGroup(),
188 'org_id' => $user->org_id,
189 'role_id' => $cl_role_id,
190 'client_id' => $cl_client_id,
191 'projects' => $assigned_projects,
192 'email' => $cl_email);
193 $user_id = ttUserHelper::insert($fields);
195 if (!$user->exists()) {
196 // We added a user to an empty subgroup. Set new user as on behalf user.
197 // Needed for user-based things to work (such as notifications config).
198 $user->setOnBehalfUser($user_id);
200 header('Location: users.php');
203 $err->add($i18n->get('error.db'));
205 $err->add($i18n->get('error.user_exists'));
209 $smarty->assign('auth_external', $auth->isPasswordExternal());
210 $smarty->assign('active_roles', $active_roles);
211 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
212 $smarty->assign('onload', 'onLoad="document.userForm.name.focus();handleClientControl();"');
213 $smarty->assign('show_quota', $show_quota);
214 $smarty->assign('show_projects', $show_projects);
215 $smarty->assign('title', $i18n->get('title.add_user'));
216 $smarty->assign('content_page_name', 'user_add.tpl');
217 $smarty->display('index.tpl');