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