Added entity type as read only field on custom field edit page.
[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 // 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);
58 }
59
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');
67   }
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;
81       } else
82         $err->add($i18n->get('error.field'), 'rate_'.$p);
83     }
84   }
85 }
86
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));
93 }
94 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'email','value'=>$cl_email));
95
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'))));
100
101 // If we have custom fields - add controls for them.
102 if ($custom_fields && $custom_fields->userFields) {
103   // Coding in progress...
104 }
105
106 $form->addInput(array('type'=>'floatfield','maxlength'=>'10','name'=>'rate','format'=>'.2','value'=>$cl_rate));
107 if ($show_quota)
108   $form->addInput(array('type'=>'floatfield','maxlength'=>'10','name'=>'quota_percent','format'=>'.2','value'=>$cl_quota_percent));
109
110 $show_projects = MODE_PROJECTS == $user->getTrackingMode() || MODE_PROJECTS_AND_TASKS == $user->getTrackingMode();
111 if ($show_projects) {
112   $projects = ttGroupHelper::getActiveProjects();
113   if (count($projects) == 0) $show_projects = false;
114 }
115
116 // Define classes for the projects table.
117 class NameCellRenderer extends DefaultCellRenderer {
118   function render(&$table, $value, $row, $column, $selected = false) {
119     $this->setOptions(array('width'=>200));
120     $this->setValue('<label for = "'.$table->name.'_'.$row.'">'.htmlspecialchars($value).'</label>');
121     return $this->toString();
122   }
123 }
124 class RateCellRenderer extends DefaultCellRenderer {
125   function render(&$table, $value, $row, $column, $selected = false) {
126     global $assigned_projects;
127     $field = new FloatField('rate_'.$table->getValueAtName($row, 'id'));
128     $field->setFormName($table->getFormName());
129     $field->setSize(5);
130     $field->setFormat('.2');
131     foreach ($assigned_projects as $p) {
132       if ($p['id'] == $table->getValueAtName($row,'id')) $field->setValue($p['rate']);
133     }
134     $this->setValue($field->getHtml());
135     return $this->toString();
136   }
137 }
138 // Create projects table.
139 $table = new Table('projects');
140 $table->setIAScript('setDefaultRate');
141 $table->setTableOptions(array('width'=>'300','cellspacing'=>'1','cellpadding'=>'3','border'=>'0'));
142 $table->setRowOptions(array('valign'=>'top','class'=>'tableHeader'));
143 $table->setData($projects);
144 $table->setKeyField('id');
145 $table->setValue($cl_projects);
146 $table->addColumn(new TableColumn('name', $i18n->get('label.project'), new NameCellRenderer()));
147 $table->addColumn(new TableColumn('p_rate', $i18n->get('form.users.rate'), new RateCellRenderer()));
148 $form->addInputElement($table);
149
150 $form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->get('button.submit')));
151
152 if ($request->isPost()) {
153   // Validate user input.
154   if (!ttValidString($cl_name)) $err->add($i18n->get('error.field'), $i18n->get('label.person_name'));
155   if (!ttValidString($cl_login)) $err->add($i18n->get('error.field'), $i18n->get('label.login'));
156   if (!$auth->isPasswordExternal()) {
157     if (!ttValidString($cl_password1)) $err->add($i18n->get('error.field'), $i18n->get('label.password'));
158     if (!ttValidString($cl_password2)) $err->add($i18n->get('error.field'), $i18n->get('label.confirm_password'));
159     if ($cl_password1 !== $cl_password2)
160       $err->add($i18n->get('error.not_equal'), $i18n->get('label.password'), $i18n->get('label.confirm_password'));
161   }
162   if (!ttValidEmail($cl_email, true)) $err->add($i18n->get('error.field'), $i18n->get('label.email'));
163   // Require selection of a client for a client role.
164   if ($user->isPluginEnabled('cl') && ttRoleHelper::isClientRole($cl_role_id) && !$cl_client_id) $err->add($i18n->get('error.client'));
165   if (!ttValidFloat($cl_rate, true)) $err->add($i18n->get('error.field'), $i18n->get('form.users.default_rate'));
166   if (!ttValidFloat($cl_quota_percent, true)) $err->add($i18n->get('error.field'), $i18n->get('label.quota'));
167   if (!ttUserHelper::canAdd()) $err->add($i18n->get('error.user_count'));
168
169   if ($err->no()) {
170     if (!ttUserHelper::getUserByLogin($cl_login)) {
171       $fields = array(
172         'name' => $cl_name,
173         'login' => $cl_login,
174         'password' => $cl_password1,
175         'rate' => $cl_rate,
176         'quota_percent' => $cl_quota_percent,
177         'group_id' => $user->getGroup(),
178         'org_id' => $user->org_id,
179         'role_id' => $cl_role_id,
180         'client_id' => $cl_client_id,
181         'projects' => $assigned_projects,
182         'email' => $cl_email);
183       $user_id = ttUserHelper::insert($fields);
184       if ($user_id) {
185         if (!$user->exists()) {
186           // We added a user to an empty subgroup. Set new user as on behalf user.
187           // Needed for user-based things to work (such as notifications config).
188           $user->setOnBehalfUser($user_id);
189         }
190         header('Location: users.php');
191         exit();
192       } else
193         $err->add($i18n->get('error.db'));
194     } else
195       $err->add($i18n->get('error.user_exists'));
196   }
197 } // isPost
198
199 $smarty->assign('auth_external', $auth->isPasswordExternal());
200 $smarty->assign('active_roles', $active_roles);
201 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
202 $smarty->assign('onload', 'onLoad="document.userForm.name.focus();handleClientControl();"');
203 $smarty->assign('show_quota', $show_quota);
204 $smarty->assign('show_projects', $show_projects);
205 $smarty->assign('title', $i18n->get('title.add_user'));
206 $smarty->assign('content_page_name', 'user_add.tpl');
207 $smarty->display('index.tpl');