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_quota_percent = $request->getParameter('quota_percent');
72 // If we have user custom fields - collect input.
73 if ($custom_fields && $custom_fields->userFields) {
74 foreach ($custom_fields->userFields as $userField) {
75 $control_name = 'user_field_'.$userField['id'];
76 $userCustomFields[$userField['id']] = array('field_id' => $userField['id'],
77 'control_name' => $control_name,
78 'label' => $userField['label'],
79 'type' => $userField['type'],
80 'required' => $userField['required'],
81 'value' => trim($request->getParameter($control_name)));
84 $cl_rate = $request->getParameter('rate');
85 $cl_projects = $request->getParameter('projects');
86 if (is_array($cl_projects)) {
87 foreach ($cl_projects as $p) {
88 if (ttValidFloat($request->getParameter('rate_'.$p), true)) {
89 $project_with_rate = array();
90 $project_with_rate['id'] = $p;
91 $project_with_rate['rate'] = $request->getParameter('rate_'.$p);
92 $assigned_projects[] = $project_with_rate;
94 $err->add($i18n->get('error.field'), 'rate_'.$p);
99 $form = new Form('userForm');
100 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'name','style'=>'width: 300px;','value'=>$cl_name));
101 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'login','style'=>'width: 300px;','value'=>$cl_login));
102 if (!$auth->isPasswordExternal()) {
103 $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'pas1','value'=>$cl_password1));
104 $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'pas2','value'=>$cl_password2));
106 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'email','value'=>$cl_email));
108 $active_roles = ttTeamHelper::getActiveRolesForUser();
109 $form->addInput(array('type'=>'combobox','onchange'=>'handleClientControl()','name'=>'role','value'=>$cl_role_id,'data'=>$active_roles,'datakeys'=>array('id', 'name')));
110 if ($user->isPluginEnabled('cl'))
111 $form->addInput(array('type'=>'combobox','name'=>'client','value'=>$cl_client_id,'data'=>$clients,'datakeys'=>array('id', 'name'),'empty'=>array(''=>$i18n->get('dropdown.select'))));
113 // If we have custom fields - add controls for them.
114 if ($custom_fields && $custom_fields->userFields) {
115 foreach ($custom_fields->userFields as $userField) {
116 $field_name = 'user_field_'.$userField['id'];
117 if ($userField['type'] == CustomFields::TYPE_TEXT) {
118 $form->addInput(array('type'=>'text','name'=>$field_name,'value'=>$userCustomFields[$userField['id']]['value']));
119 } elseif ($userField['type'] == CustomFields::TYPE_DROPDOWN) {
120 $form->addInput(array('type'=>'combobox','name'=>$field_name,
121 'style'=>'width: 250px;',
122 'data'=>CustomFields::getOptions($userField['id']),
123 'value'=>$userCustomFields[$userField['id']]['value'],
124 'empty'=>array(''=>$i18n->get('dropdown.select'))));
129 $form->addInput(array('type'=>'floatfield','maxlength'=>'10','name'=>'rate','format'=>'.2','value'=>$cl_rate));
131 $form->addInput(array('type'=>'floatfield','maxlength'=>'10','name'=>'quota_percent','format'=>'.2','value'=>$cl_quota_percent));
133 $show_projects = MODE_PROJECTS == $user->getTrackingMode() || MODE_PROJECTS_AND_TASKS == $user->getTrackingMode();
134 if ($show_projects) {
135 $projects = ttGroupHelper::getActiveProjects();
136 if (count($projects) == 0) $show_projects = false;
139 // Define classes for the projects table.
140 class NameCellRenderer extends DefaultCellRenderer {
141 function render(&$table, $value, $row, $column, $selected = false) {
142 $this->setOptions(array('width'=>200));
143 $this->setValue('<label for = "'.$table->name.'_'.$row.'">'.htmlspecialchars($value).'</label>');
144 return $this->toString();
147 class RateCellRenderer extends DefaultCellRenderer {
148 function render(&$table, $value, $row, $column, $selected = false) {
149 global $assigned_projects;
150 $field = new FloatField('rate_'.$table->getValueAtName($row, 'id'));
151 $field->setFormName($table->getFormName());
153 $field->setFormat('.2');
154 foreach ($assigned_projects as $p) {
155 if ($p['id'] == $table->getValueAtName($row,'id')) $field->setValue($p['rate']);
157 $this->setValue($field->getHtml());
158 return $this->toString();
161 // Create projects table.
162 $table = new Table('projects');
163 $table->setIAScript('setDefaultRate');
164 $table->setTableOptions(array('width'=>'300','cellspacing'=>'1','cellpadding'=>'3','border'=>'0'));
165 $table->setRowOptions(array('valign'=>'top','class'=>'tableHeader'));
166 $table->setData($projects);
167 $table->setKeyField('id');
168 $table->setValue($cl_projects);
169 $table->addColumn(new TableColumn('name', $i18n->get('label.project'), new NameCellRenderer()));
170 $table->addColumn(new TableColumn('p_rate', $i18n->get('form.users.rate'), new RateCellRenderer()));
171 $form->addInputElement($table);
173 $form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->get('button.submit')));
175 if ($request->isPost()) {
176 // Validate user input.
177 if (!ttValidString($cl_name)) $err->add($i18n->get('error.field'), $i18n->get('label.person_name'));
178 if (!ttValidString($cl_login)) $err->add($i18n->get('error.field'), $i18n->get('label.login'));
179 if (!$auth->isPasswordExternal()) {
180 if (!ttValidString($cl_password1)) $err->add($i18n->get('error.field'), $i18n->get('label.password'));
181 if (!ttValidString($cl_password2)) $err->add($i18n->get('error.field'), $i18n->get('label.confirm_password'));
182 if ($cl_password1 !== $cl_password2)
183 $err->add($i18n->get('error.not_equal'), $i18n->get('label.password'), $i18n->get('label.confirm_password'));
185 if (!ttValidEmail($cl_email, true)) $err->add($i18n->get('error.field'), $i18n->get('label.email'));
186 // Require selection of a client for a client role.
187 if ($user->isPluginEnabled('cl') && ttRoleHelper::isClientRole($cl_role_id) && !$cl_client_id) $err->add($i18n->get('error.client'));
188 if (!ttValidFloat($cl_quota_percent, true)) $err->add($i18n->get('error.field'), $i18n->get('label.quota'));
189 // Validate input in user custom fields.
190 if ($custom_fields && $custom_fields->userFields) {
191 foreach ($userCustomFields as $userField) {
192 // Validation is the same for text and dropdown fields.
193 if (!ttValidString($userField['value'], !$userField['required'])) $err->add($i18n->get('error.field'), htmlspecialchars($userField['label']));
196 if (!ttValidFloat($cl_rate, true)) $err->add($i18n->get('error.field'), $i18n->get('form.users.default_rate'));
197 if (!ttUserHelper::canAdd()) $err->add($i18n->get('error.user_count'));
200 if (!ttUserHelper::getUserByLogin($cl_login)) {
203 'login' => $cl_login,
204 'password' => $cl_password1,
206 'quota_percent' => $cl_quota_percent,
207 'group_id' => $user->getGroup(),
208 'org_id' => $user->org_id,
209 'role_id' => $cl_role_id,
210 'client_id' => $cl_client_id,
211 'projects' => $assigned_projects,
212 'email' => $cl_email);
213 $user_id = ttUserHelper::insert($fields);
215 // Insert user custom fields if we have them.
217 if ($user_id && $custom_fields && $custom_fields->userFields) {
218 $result = $custom_fields->insertEntityFields(CustomFields::ENTITY_USER, $user_id, $userCustomFields);
221 if ($user_id && $result) {
222 if (!$user->exists()) {
223 // We added a user to an empty subgroup. Set new user as on behalf user.
224 // Needed for user-based things to work (such as notifications config).
225 $user->setOnBehalfUser($user_id);
227 header('Location: users.php');
230 $err->add($i18n->get('error.db'));
232 $err->add($i18n->get('error.user_exists'));
236 $smarty->assign('auth_external', $auth->isPasswordExternal());
237 $smarty->assign('active_roles', $active_roles);
238 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
239 $smarty->assign('onload', 'onLoad="document.userForm.name.focus();handleClientControl();"');
240 $smarty->assign('show_quota', $show_quota);
241 $smarty->assign('show_projects', $show_projects);
242 $smarty->assign('title', $i18n->get('title.add_user'));
243 $smarty->assign('content_page_name', 'user_add.tpl');
244 $smarty->display('index.tpl');