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