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