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