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