Introduced tt_entity_custom_fields table.
[timetracker.git] / user_add.php
1 <?php
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.
10 // |
11 // | There are only two ways to violate the license:
12 // |
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).
16 // |
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).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 require_once('initialize.php');
30 import('form.Form');
31 import('ttTeamHelper');
32 import('ttGroupHelper');
33 import('ttUserHelper');
34 import('form.Table');
35 import('form.TableColumn');
36 import('ttRoleHelper');
37
38 // Access checks.
39 if (!ttAccessAllowed('manage_users')) {
40   header('Location: access_denied.php');
41   exit();
42 }
43 // End of access checks.
44
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');
48
49 $show_quota = $user->isPluginEnabled('mq');
50 if ($user->isPluginEnabled('cl'))
51   $clients = ttGroupHelper::getActiveClients();
52
53 $assigned_projects = array();
54 if ($request->isPost()) {
55   $cl_name = trim($request->getParameter('name'));
56   $cl_login = trim($request->getParameter('login'));
57   if (!$auth->isPasswordExternal()) {
58     $cl_password1 = $request->getParameter('pas1');
59     $cl_password2 = $request->getParameter('pas2');
60   }
61   $cl_email = trim($request->getParameter('email'));
62   $cl_role_id = $request->getParameter('role');
63   $cl_client_id = $request->getParameter('client');
64   $cl_rate = $request->getParameter('rate');
65   $cl_quota_percent = $request->getParameter('quota_percent');
66   $cl_projects = $request->getParameter('projects');
67   if (is_array($cl_projects)) {
68     foreach ($cl_projects as $p) {
69       if (ttValidFloat($request->getParameter('rate_'.$p), true)) {
70         $project_with_rate = array();
71         $project_with_rate['id'] = $p;
72         $project_with_rate['rate'] = $request->getParameter('rate_'.$p);
73         $assigned_projects[] = $project_with_rate;
74       } else
75         $err->add($i18n->get('error.field'), 'rate_'.$p);
76     }
77   }
78 }
79
80 $form = new Form('userForm');
81 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'name','style'=>'width: 300px;','value'=>$cl_name));
82 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'login','style'=>'width: 300px;','value'=>$cl_login));
83 if (!$auth->isPasswordExternal()) {
84   $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'pas1','value'=>$cl_password1));
85   $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'pas2','value'=>$cl_password2));
86 }
87 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'email','value'=>$cl_email));
88
89 $active_roles = ttTeamHelper::getActiveRolesForUser();
90 $form->addInput(array('type'=>'combobox','onchange'=>'handleClientControl()','name'=>'role','value'=>$cl_role_id,'data'=>$active_roles,'datakeys'=>array('id', 'name')));
91 if ($user->isPluginEnabled('cl'))
92   $form->addInput(array('type'=>'combobox','name'=>'client','value'=>$cl_client_id,'data'=>$clients,'datakeys'=>array('id', 'name'),'empty'=>array(''=>$i18n->get('dropdown.select'))));
93
94 $form->addInput(array('type'=>'floatfield','maxlength'=>'10','name'=>'rate','format'=>'.2','value'=>$cl_rate));
95 if ($show_quota)
96   $form->addInput(array('type'=>'floatfield','maxlength'=>'10','name'=>'quota_percent','format'=>'.2','value'=>$cl_quota_percent));
97
98 $show_projects = MODE_PROJECTS == $user->getTrackingMode() || MODE_PROJECTS_AND_TASKS == $user->getTrackingMode();
99 if ($show_projects) {
100   $projects = ttGroupHelper::getActiveProjects();
101   if (count($projects) == 0) $show_projects = false;
102 }
103
104 // Define classes for the projects table.
105 class NameCellRenderer extends DefaultCellRenderer {
106   function render(&$table, $value, $row, $column, $selected = false) {
107     $this->setOptions(array('width'=>200));
108     $this->setValue('<label for = "'.$table->name.'_'.$row.'">'.htmlspecialchars($value).'</label>');
109     return $this->toString();
110   }
111 }
112 class RateCellRenderer extends DefaultCellRenderer {
113   function render(&$table, $value, $row, $column, $selected = false) {
114     global $assigned_projects;
115     $field = new FloatField('rate_'.$table->getValueAtName($row, 'id'));
116     $field->setFormName($table->getFormName());
117     $field->setSize(5);
118     $field->setFormat('.2');
119     foreach ($assigned_projects as $p) {
120       if ($p['id'] == $table->getValueAtName($row,'id')) $field->setValue($p['rate']);
121     }
122     $this->setValue($field->getHtml());
123     return $this->toString();
124   }
125 }
126 // Create projects table.
127 $table = new Table('projects');
128 $table->setIAScript('setDefaultRate');
129 $table->setTableOptions(array('width'=>'300','cellspacing'=>'1','cellpadding'=>'3','border'=>'0'));
130 $table->setRowOptions(array('valign'=>'top','class'=>'tableHeader'));
131 $table->setData($projects);
132 $table->setKeyField('id');
133 $table->setValue($cl_projects);
134 $table->addColumn(new TableColumn('name', $i18n->get('label.project'), new NameCellRenderer()));
135 $table->addColumn(new TableColumn('p_rate', $i18n->get('form.users.rate'), new RateCellRenderer()));
136 $form->addInputElement($table);
137
138 $form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->get('button.submit')));
139
140 if ($request->isPost()) {
141   // Validate user input.
142   if (!ttValidString($cl_name)) $err->add($i18n->get('error.field'), $i18n->get('label.person_name'));
143   if (!ttValidString($cl_login)) $err->add($i18n->get('error.field'), $i18n->get('label.login'));
144   if (!$auth->isPasswordExternal()) {
145     if (!ttValidString($cl_password1)) $err->add($i18n->get('error.field'), $i18n->get('label.password'));
146     if (!ttValidString($cl_password2)) $err->add($i18n->get('error.field'), $i18n->get('label.confirm_password'));
147     if ($cl_password1 !== $cl_password2)
148       $err->add($i18n->get('error.not_equal'), $i18n->get('label.password'), $i18n->get('label.confirm_password'));
149   }
150   if (!ttValidEmail($cl_email, true)) $err->add($i18n->get('error.field'), $i18n->get('label.email'));
151   // Require selection of a client for a client role.
152   if ($user->isPluginEnabled('cl') && ttRoleHelper::isClientRole($cl_role_id) && !$cl_client_id) $err->add($i18n->get('error.client'));
153   if (!ttValidFloat($cl_rate, true)) $err->add($i18n->get('error.field'), $i18n->get('form.users.default_rate'));
154   if (!ttValidFloat($cl_quota_percent, true)) $err->add($i18n->get('error.field'), $i18n->get('label.quota'));
155   if (!ttUserHelper::canAdd()) $err->add($i18n->get('error.user_count'));
156
157   if ($err->no()) {
158     if (!ttUserHelper::getUserByLogin($cl_login)) {
159       $fields = array(
160         'name' => $cl_name,
161         'login' => $cl_login,
162         'password' => $cl_password1,
163         'rate' => $cl_rate,
164         'quota_percent' => $cl_quota_percent,
165         'group_id' => $user->getGroup(),
166         'org_id' => $user->org_id,
167         'role_id' => $cl_role_id,
168         'client_id' => $cl_client_id,
169         'projects' => $assigned_projects,
170         'email' => $cl_email);
171       $user_id = ttUserHelper::insert($fields);
172       if ($user_id) {
173         if (!$user->exists()) {
174           // We added a user to an empty subgroup. Set new user as on behalf user.
175           // Needed for user-based things to work (such as notifications config).
176           $user->setOnBehalfUser($user_id);
177         }
178         header('Location: users.php');
179         exit();
180       } else
181         $err->add($i18n->get('error.db'));
182     } else
183       $err->add($i18n->get('error.user_exists'));
184   }
185 } // isPost
186
187 $smarty->assign('auth_external', $auth->isPasswordExternal());
188 $smarty->assign('active_roles', $active_roles);
189 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
190 $smarty->assign('onload', 'onLoad="document.userForm.name.focus();handleClientControl();"');
191 $smarty->assign('show_quota', $show_quota);
192 $smarty->assign('show_projects', $show_projects);
193 $smarty->assign('title', $i18n->get('title.add_user'));
194 $smarty->assign('content_page_name', 'user_add.tpl');
195 $smarty->display('index.tpl');