Work in progress on roles.
[timetracker.git] / profile_edit.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('ttUserHelper');
32 import('ttRoleHelper');
33
34 // Access check.
35 if (!ttAccessCheck(right_data_entry|right_view_reports)) {
36   header('Location: access_denied.php');
37   exit();
38 }
39
40 if (!defined('CURRENCY_DEFAULT')) define('CURRENCY_DEFAULT', '$');
41 $can_change_login = $user->canManageTeam();
42
43 if ($request->isPost()) {
44   $cl_name = trim($request->getParameter('name'));
45   $cl_login = trim($request->getParameter('login'));
46   if (!$auth->isPasswordExternal()) {
47     $cl_password1 = $request->getParameter('password1');
48     $cl_password2 = $request->getParameter('password2');
49   }
50   $cl_email = trim($request->getParameter('email'));
51
52   if ($user->canManageTeam()) {
53     $cl_team = trim($request->getParameter('team_name'));
54     $cl_currency = trim($request->getParameter('currency'));
55     if (!$cl_currency) $cl_currency = CURRENCY_DEFAULT;
56     $cl_roles = $request->getParameter('roles');
57     $cl_lang = $request->getParameter('lang');
58     $cl_decimal_mark = $request->getParameter('decimal_mark');
59     $cl_date_format = $request->getParameter('date_format');
60     $cl_time_format = $request->getParameter('time_format');
61     $cl_start_week = $request->getParameter('start_week');
62     $cl_show_holidays = $request->getParameter('show_holidays');
63     $cl_tracking_mode = $request->getParameter('tracking_mode');
64     $cl_project_required = $request->getParameter('project_required');
65     $cl_task_required = $request->getParameter('task_required');
66     $cl_record_type = $request->getParameter('record_type');
67     $cl_punch_mode = $request->getParameter('punch_mode');
68     $cl_allow_overlap = $request->getParameter('allow_overlap');
69     $cl_future_entries = $request->getParameter('future_entries');
70     $cl_uncompleted_indicators = $request->getParameter('uncompleted_indicators');
71     $cl_bcc_email = trim($request->getParameter('bcc_email'));
72
73     // Plugin checkboxes.
74     $cl_charts = $request->getParameter('charts');
75     $cl_clients = $request->getParameter('clients');
76     $cl_client_required = $request->getParameter('client_required');
77     $cl_invoices = $request->getParameter('invoices');
78     $cl_paid_status = $request->getParameter('paid_status');
79     $cl_custom_fields = $request->getParameter('custom_fields');
80     $cl_expenses = $request->getParameter('expenses');
81     $cl_tax_expenses = $request->getParameter('tax_expenses');
82     $cl_notifications = $request->getParameter('notifications');
83     $cl_locking = $request->getParameter('locking');
84     $cl_quotas = $request->getParameter('quotas');
85     $cl_week_view = $request->getParameter('week_view');
86   }
87 } else {
88   $cl_name = $user->name;
89   $cl_login = $user->login;
90   $cl_email = $user->email;
91   if ($user->canManageTeam()) {
92     $cl_team = $user->team;
93     $cl_currency = ($user->currency == ''? CURRENCY_DEFAULT : $user->currency);
94     $cl_roles = $user->roles;
95     $cl_lang = $user->lang;
96     $cl_decimal_mark = $user->decimal_mark;
97     $cl_date_format = $user->date_format;
98     $cl_time_format = $user->time_format;
99     $cl_start_week = $user->week_start;
100     $cl_show_holidays = $user->show_holidays;
101     $cl_tracking_mode = $user->tracking_mode;
102     $cl_project_required = $user->project_required;
103     $cl_task_required = $user->task_required;
104     $cl_record_type = $user->record_type;
105     $cl_punch_mode = $user->punch_mode;
106     $cl_allow_overlap = $user->allow_overlap;
107     $cl_future_entries = $user->future_entries;
108     $cl_uncompleted_indicators = $user->uncompleted_indicators;
109     $cl_bcc_email = $user->bcc_email;
110
111     // Which plugins do we have enabled?
112     $plugins = explode(',', $user->plugins);
113     $cl_charts = in_array('ch', $plugins);
114     $cl_clients = in_array('cl', $plugins);
115     $cl_client_required = in_array('cm', $plugins);
116     $cl_invoices = in_array('iv', $plugins);
117     $cl_paid_status = in_array('ps', $plugins);
118     $cl_custom_fields = in_array('cf', $plugins);    
119     $cl_expenses = in_array('ex', $plugins);
120     $cl_tax_expenses = in_array('et', $plugins);
121     $cl_notifications = in_array('no', $plugins);
122     $cl_locking = in_array('lk', $plugins);
123     $cl_quotas = in_array('mq', $plugins);
124     $cl_week_view = in_array('wv', $plugins);
125   }
126 }
127
128 $form = new Form('profileForm');
129 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'name','value'=>$cl_name));
130 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'login','value'=>$cl_login,'enable'=>$can_change_login));
131 if (!$auth->isPasswordExternal()) {
132   $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'password1','value'=>$cl_password1));
133   $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'password2','value'=>$cl_password2));
134 }
135 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'email','value'=>$cl_email,'enable'=>$can_change_login));
136 if ($user->canManageTeam()) {
137   $form->addInput(array('type'=>'text','maxlength'=>'200','name'=>'team_name','value'=>$cl_team));
138   $form->addInput(array('type'=>'text','maxlength'=>'7','name'=>'currency','value'=>$cl_currency));
139   // Roles checkbox.
140   $form->addInput(array('type'=>'checkbox','name'=>'roles','value'=>$cl_roles,'onchange'=>'handleRolesCheckbox()'));
141
142   // Prepare an array of available languages.
143   $lang_files = I18n::getLangFileList();
144   foreach ($lang_files as $lfile) {
145     $content = file(RESOURCE_DIR."/".$lfile);
146     $lname = '';
147     foreach ($content as $line) {
148       if (strstr($line, 'i18n_language')) {
149         $a = explode('=', $line);
150         $lname = trim(str_replace(';','',str_replace("'","",$a[1])));
151         break;
152       }
153     }
154     unset($content);
155     $longname_lang[] = array('id'=>I18n::getLangFromFilename($lfile),'name'=>$lname);
156   }
157   $longname_lang = mu_sort($longname_lang, 'name');
158   $form->addInput(array('type'=>'combobox','name'=>'lang','style'=>'width: 150px','data'=>$longname_lang,'datakeys'=>array('id','name'),'value'=>$cl_lang));
159
160   $DECIMAL_MARK_OPTIONS = array(array('id'=>'.','name'=>'.'),array('id'=>',','name'=>','));
161   $form->addInput(array('type'=>'combobox','name'=>'decimal_mark','style'=>'width: 150px','data'=>$DECIMAL_MARK_OPTIONS,'datakeys'=>array('id','name'),'value'=>$cl_decimal_mark,
162     'onchange'=>'adjustDecimalPreview()'));
163
164   $DATE_FORMAT_OPTIONS = array(
165     array('id'=>'%Y-%m-%d','name'=>'Y-m-d'),
166     array('id'=>'%m/%d/%Y','name'=>'m/d/Y'),
167     array('id'=>'%d.%m.%Y','name'=>'d.m.Y'),
168     array('id'=>'%d.%m.%Y %a','name'=>'d.m.Y a'));
169   $form->addInput(array('type'=>'combobox','name'=>'date_format','style'=>'width: 150px;','data'=>$DATE_FORMAT_OPTIONS,'datakeys'=>array('id','name'),'value'=>$cl_date_format,
170     'onchange'=>'MakeFormatPreview(&quot;date_format_preview&quot;, this);'));
171   $TIME_FORMAT_OPTIONS = array(
172     array('id'=>'%H:%M','name'=>$i18n->getKey('form.profile.24_hours')),
173     array('id'=>'%I:%M %p','name'=>$i18n->getKey('form.profile.12_hours')));
174   $form->addInput(array('type'=>'combobox','name'=>'time_format','style'=>'width: 150px;','data'=>$TIME_FORMAT_OPTIONS,'datakeys'=>array('id','name'),'value'=>$cl_time_format,
175     'onchange'=>'MakeFormatPreview(&quot;time_format_preview&quot;, this);'));
176
177   // Prepare week start choices.
178   $week_start_options = array();
179   foreach ($i18n->weekdayNames as $id => $week_dn) {
180     $week_start_options[] = array('id' => $id, 'name' => $week_dn);
181   }
182   $form->addInput(array('type'=>'combobox','name'=>'start_week','style'=>'width: 150px;','data'=>$week_start_options,'datakeys'=>array('id','name'),'value'=>$cl_start_week));
183
184   // Show holidays checkbox.
185   $form->addInput(array('type'=>'checkbox','name'=>'show_holidays','value'=>$cl_show_holidays));
186
187   // Prepare tracking mode choices.
188   $tracking_mode_options = array();
189   $tracking_mode_options[MODE_TIME] = $i18n->getKey('form.profile.mode_time');
190   $tracking_mode_options[MODE_PROJECTS] = $i18n->getKey('form.profile.mode_projects');
191   $tracking_mode_options[MODE_PROJECTS_AND_TASKS] = $i18n->getKey('form.profile.mode_projects_and_tasks');
192   $form->addInput(array('type'=>'combobox','name'=>'tracking_mode','style'=>'width: 150px;','data'=>$tracking_mode_options,'value'=>$cl_tracking_mode,'onchange'=>'handleTaskRequiredCheckbox()'));
193   $form->addInput(array('type'=>'checkbox','name'=>'project_required','value'=>$cl_project_required));
194   $form->addInput(array('type'=>'checkbox','name'=>'task_required','value'=>$cl_task_required));
195
196   // Prepare record type choices.
197   $record_type_options = array();
198   $record_type_options[TYPE_ALL] = $i18n->getKey('form.profile.type_all');
199   $record_type_options[TYPE_START_FINISH] = $i18n->getKey('form.profile.type_start_finish');
200   $record_type_options[TYPE_DURATION] = $i18n->getKey('form.profile.type_duration');
201   $form->addInput(array('type'=>'combobox','name'=>'record_type','style'=>'width: 150px;','data'=>$record_type_options,'value'=>$cl_record_type));
202
203   // Punch mode checkbox.
204   $form->addInput(array('type'=>'checkbox','name'=>'punch_mode','value'=>$cl_punch_mode));
205
206   // Allow overlap checkbox.
207   $form->addInput(array('type'=>'checkbox','name'=>'allow_overlap','value'=>$cl_allow_overlap));
208
209   // Future entries checkbox.
210   $form->addInput(array('type'=>'checkbox','name'=>'future_entries','value'=>$cl_future_entries));
211
212   // Uncompleted indicators checkbox.
213   $form->addInput(array('type'=>'checkbox','name'=>'uncompleted_indicators','value'=>$cl_uncompleted_indicators));
214
215   // Add bcc email control, for manager only.
216   if ($user->isManager()) {
217     $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'bcc_email','value'=>$cl_bcc_email));
218   }
219
220   // Plugin checkboxes.
221   $form->addInput(array('type'=>'checkbox','name'=>'charts','value'=>$cl_charts));
222   $form->addInput(array('type'=>'checkbox','name'=>'clients','value'=>$cl_clients,'onchange'=>'handlePluginCheckboxes()'));
223   $form->addInput(array('type'=>'checkbox','name'=>'client_required','value'=>$cl_client_required));
224   $form->addInput(array('type'=>'checkbox','name'=>'invoices','value'=>$cl_invoices));
225   $form->addInput(array('type'=>'checkbox','name'=>'paid_status','value'=>$cl_paid_status));
226   $form->addInput(array('type'=>'checkbox','name'=>'custom_fields','value'=>$cl_custom_fields,'onchange'=>'handlePluginCheckboxes()'));
227   $form->addInput(array('type'=>'checkbox','name'=>'expenses','value'=>$cl_expenses,'onchange'=>'handlePluginCheckboxes()'));
228   $form->addInput(array('type'=>'checkbox','name'=>'tax_expenses','value'=>$cl_tax_expenses));
229   $form->addInput(array('type'=>'checkbox','name'=>'notifications','value'=>$cl_notifications,'onchange'=>'handlePluginCheckboxes()'));
230   $form->addInput(array('type'=>'checkbox','name'=>'locking','value'=>$cl_locking,'onchange'=>'handlePluginCheckboxes()'));
231   $form->addInput(array('type'=>'checkbox','name'=>'quotas','value'=>$cl_quotas,'onchange'=>'handlePluginCheckboxes()'));
232   $form->addInput(array('type'=>'checkbox','name'=>'week_view','value'=>$cl_week_view,'onchange'=>'handlePluginCheckboxes()'));
233 }
234 $form->addInput(array('type'=>'submit','name'=>'btn_save','value'=>$i18n->getKey('button.save')));
235
236 if ($request->isPost()) {
237   // Validate user input.
238   if (!ttValidString($cl_name)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.person_name'));
239   if ($can_change_login) {
240     if (!ttValidString($cl_login)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.login'));
241
242     // New login must be unique.
243     if ($cl_login != $user->login && ttUserHelper::getUserByLogin($cl_login))
244       $err->add($i18n->getKey('error.user_exists'));
245   }
246   if (!$auth->isPasswordExternal() && ($cl_password1 || $cl_password2)) {
247     if (!ttValidString($cl_password1)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.password'));
248     if (!ttValidString($cl_password2)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.confirm_password'));
249     if ($cl_password1 !== $cl_password2)
250       $err->add($i18n->getKey('error.not_equal'), $i18n->getKey('label.password'), $i18n->getKey('label.confirm_password'));
251   }
252   if (!ttValidEmail($cl_email, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.email'));
253   if ($user->canManageTeam()) {
254     if (!ttValidString($cl_team, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.team_name'));
255     if (!ttValidString($cl_currency, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.currency'));
256     if ($user->isManager()) {
257       if (!ttValidEmail($cl_bcc_email, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.bcc'));
258     }
259   }
260   // Finished validating user input.
261
262   if ($err->no()) {
263     $update_result = true;
264     if ($user->canManageTeam()) {
265
266       // Prepare plugins string.
267       if ($cl_charts)
268         $plugins .= ',ch';
269       if ($cl_clients)
270         $plugins .= ',cl';
271       if ($cl_client_required)
272         $plugins .= ',cm';
273       if ($cl_invoices)
274         $plugins .= ',iv';
275       if ($cl_paid_status)
276         $plugins .= ',ps';
277       if ($cl_custom_fields)
278         $plugins .= ',cf';
279       if ($cl_expenses)
280         $plugins .= ',ex';
281       if ($cl_tax_expenses)
282         $plugins .= ',et';
283       if ($cl_notifications)
284         $plugins .= ',no';
285       if ($cl_locking)
286         $plugins .= ',lk';
287       if ($cl_quotas)
288         $plugins .= ',mq';
289       if ($cl_week_view)
290         $plugins .= ',wv';
291
292       // Recycle week view plugin options as they are not configured on this page.
293       $existing_plugins = explode(',', $user->plugins);
294       if (in_array('wvn', $existing_plugins))
295         $plugins .= ',wvn';
296       if (in_array('wvl', $existing_plugins))
297         $plugins .= ',wvl';
298       if (in_array('wvns', $existing_plugins))
299         $plugins .= ',wvns';
300
301       $plugins = trim($plugins, ',');
302
303       // If we use roles... No, we'll do it differently.
304       /*
305       if ($cl_roles && !ttRoleHelper::rolesExist()) {
306          ttRoleHelper::createDefaultRoles();
307       }*/
308
309       // Prepare config string.
310       if ($cl_roles)
311         $config .= ',roles';
312       if ($cl_show_holidays)
313         $config .= ',show_holidays';
314       if ($cl_punch_mode)
315         $config .= ',punch_mode';
316       if ($cl_allow_overlap)
317         $config .= ',allow_overlap';
318       if ($cl_future_entries)
319         $config .= ',future_entries';
320       if ($cl_uncompleted_indicators)
321         $config .= ',uncompleted_indicators';
322       $config = trim($config, ',');
323
324       $update_result = ttTeamHelper::update($user->team_id, array(
325         'name' => $cl_team,
326         'currency' => $cl_currency,
327         'lang' => $cl_lang,
328         'decimal_mark' => $cl_decimal_mark,
329         'date_format' => $cl_date_format,
330         'time_format' => $cl_time_format,
331         'week_start' => $cl_start_week,
332         'tracking_mode' => $cl_tracking_mode,
333         'project_required' => $cl_project_required,
334         'task_required' => $cl_task_required,
335         'record_type' => $cl_record_type,
336         'uncompleted_indicators' => $cl_uncompleted_indicators,
337         'bcc_email' => $cl_bcc_email,
338         'plugins' => $plugins,
339         'config' => $config));
340     }
341     if ($update_result) {
342       $update_result = ttUserHelper::update($user->id, array(
343         'name' => $cl_name,
344         'login' => $cl_login,
345         'password' => $cl_password1,
346         'email' => $cl_email,
347         'status' => ACTIVE));
348     }
349     if ($update_result) {
350       header('Location: time.php');
351       exit();
352     } else
353       $err->add($i18n->getKey('error.db'));
354   }
355 } // isPost
356
357 $smarty->assign('auth_external', $auth->isPasswordExternal());
358 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
359 $smarty->assign('onload', 'onLoad="handleRolesCheckbox(); handleTaskRequiredCheckbox(); handlePluginCheckboxes();"');
360 $smarty->assign('title', $i18n->getKey('title.profile'));
361 $smarty->assign('content_page_name', 'profile_edit.tpl');
362 $smarty->display('index.tpl');