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