Split profile page into two for account and group settings to reduce complexity.
[timetracker.git] / group_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_basic_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
43 if ($request->isPost()) {
44   $cl_team = trim($request->getParameter('team_name'));
45   $cl_currency = trim($request->getParameter('currency'));
46   if (!$cl_currency) $cl_currency = CURRENCY_DEFAULT;
47   $cl_lang = $request->getParameter('lang');
48   $cl_decimal_mark = $request->getParameter('decimal_mark');
49   $cl_date_format = $request->getParameter('date_format');
50   $cl_time_format = $request->getParameter('time_format');
51   $cl_start_week = $request->getParameter('start_week');
52   $cl_show_holidays = $request->getParameter('show_holidays');
53   $cl_tracking_mode = $request->getParameter('tracking_mode');
54   $cl_project_required = $request->getParameter('project_required');
55   $cl_task_required = $request->getParameter('task_required');
56   $cl_record_type = $request->getParameter('record_type');
57   $cl_punch_mode = $request->getParameter('punch_mode');
58   $cl_allow_overlap = $request->getParameter('allow_overlap');
59   $cl_future_entries = $request->getParameter('future_entries');
60   $cl_uncompleted_indicators = $request->getParameter('uncompleted_indicators');
61   $cl_bcc_email = trim($request->getParameter('bcc_email'));
62
63   // Plugin checkboxes.
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_paid_status = $request->getParameter('paid_status');
69   $cl_custom_fields = $request->getParameter('custom_fields');
70   $cl_expenses = $request->getParameter('expenses');
71   $cl_tax_expenses = $request->getParameter('tax_expenses');
72   $cl_notifications = $request->getParameter('notifications');
73   $cl_locking = $request->getParameter('locking');
74   $cl_quotas = $request->getParameter('quotas');
75   $cl_week_view = $request->getParameter('week_view');
76 } else {
77   $cl_team = $user->team;
78   $cl_currency = ($user->currency == ''? CURRENCY_DEFAULT : $user->currency);
79   $cl_lang = $user->lang;
80   $cl_decimal_mark = $user->decimal_mark;
81   $cl_date_format = $user->date_format;
82   $cl_time_format = $user->time_format;
83   $cl_start_week = $user->week_start;
84   $cl_show_holidays = $user->show_holidays;
85   $cl_tracking_mode = $user->tracking_mode;
86   $cl_project_required = $user->project_required;
87   $cl_task_required = $user->task_required;
88   $cl_record_type = $user->record_type;
89   $cl_punch_mode = $user->punch_mode;
90   $cl_allow_overlap = $user->allow_overlap;
91   $cl_future_entries = $user->future_entries;
92   $cl_uncompleted_indicators = $user->uncompleted_indicators;
93   $cl_bcc_email = $user->bcc_email;
94
95   // Which plugins do we have enabled?
96   $plugins = explode(',', $user->plugins);
97   $cl_charts = in_array('ch', $plugins);
98   $cl_clients = in_array('cl', $plugins);
99   $cl_client_required = in_array('cm', $plugins);
100   $cl_invoices = in_array('iv', $plugins);
101   $cl_paid_status = in_array('ps', $plugins);
102   $cl_custom_fields = in_array('cf', $plugins);
103   $cl_expenses = in_array('ex', $plugins);
104   $cl_tax_expenses = in_array('et', $plugins);
105   $cl_notifications = in_array('no', $plugins);
106   $cl_locking = in_array('lk', $plugins);
107   $cl_quotas = in_array('mq', $plugins);
108   $cl_week_view = in_array('wv', $plugins);
109 }
110
111 $form = new Form('groupForm');
112 $form->addInput(array('type'=>'text','maxlength'=>'200','name'=>'team_name','value'=>$cl_team));
113 $form->addInput(array('type'=>'text','maxlength'=>'7','name'=>'currency','value'=>$cl_currency));
114
115 // Prepare an array of available languages.
116 $lang_files = I18n::getLangFileList();
117 foreach ($lang_files as $lfile) {
118   $content = file(RESOURCE_DIR."/".$lfile);
119   $lname = '';
120   foreach ($content as $line) {
121     if (strstr($line, 'i18n_language')) {
122       $a = explode('=', $line);
123       $lname = trim(str_replace(';','',str_replace("'","",$a[1])));
124       break;
125     }
126   }
127   unset($content);
128   $longname_lang[] = array('id'=>I18n::getLangFromFilename($lfile),'name'=>$lname);
129 }
130 $longname_lang = mu_sort($longname_lang, 'name');
131 $form->addInput(array('type'=>'combobox','name'=>'lang','style'=>'width: 200px','data'=>$longname_lang,'datakeys'=>array('id','name'),'value'=>$cl_lang));
132
133 $DECIMAL_MARK_OPTIONS = array(array('id'=>'.','name'=>'.'),array('id'=>',','name'=>','));
134 $form->addInput(array('type'=>'combobox','name'=>'decimal_mark','style'=>'width: 150px','data'=>$DECIMAL_MARK_OPTIONS,'datakeys'=>array('id','name'),'value'=>$cl_decimal_mark,
135   'onchange'=>'adjustDecimalPreview()'));
136
137 $DATE_FORMAT_OPTIONS = array(
138   array('id'=>'%Y-%m-%d','name'=>'Y-m-d'),
139   array('id'=>'%m/%d/%Y','name'=>'m/d/Y'),
140   array('id'=>'%d.%m.%Y','name'=>'d.m.Y'),
141   array('id'=>'%d.%m.%Y %a','name'=>'d.m.Y a'));
142 $form->addInput(array('type'=>'combobox','name'=>'date_format','style'=>'width: 150px;','data'=>$DATE_FORMAT_OPTIONS,'datakeys'=>array('id','name'),'value'=>$cl_date_format,
143   'onchange'=>'MakeFormatPreview(&quot;date_format_preview&quot;, this);'));
144 $TIME_FORMAT_OPTIONS = array(
145   array('id'=>'%H:%M','name'=>$i18n->get('form.profile.24_hours')),
146   array('id'=>'%I:%M %p','name'=>$i18n->get('form.profile.12_hours')));
147 $form->addInput(array('type'=>'combobox','name'=>'time_format','style'=>'width: 150px;','data'=>$TIME_FORMAT_OPTIONS,'datakeys'=>array('id','name'),'value'=>$cl_time_format,
148   'onchange'=>'MakeFormatPreview(&quot;time_format_preview&quot;, this);'));
149
150 // Prepare week start choices.
151 $week_start_options = array();
152 foreach ($i18n->weekdayNames as $id => $week_dn) {
153   $week_start_options[] = array('id' => $id, 'name' => $week_dn);
154 }
155 $form->addInput(array('type'=>'combobox','name'=>'start_week','style'=>'width: 150px;','data'=>$week_start_options,'datakeys'=>array('id','name'),'value'=>$cl_start_week));
156
157 // Show holidays checkbox.
158 $form->addInput(array('type'=>'checkbox','name'=>'show_holidays','value'=>$cl_show_holidays));
159
160 // Prepare tracking mode choices.
161 $tracking_mode_options = array();
162 $tracking_mode_options[MODE_TIME] = $i18n->get('form.profile.mode_time');
163 $tracking_mode_options[MODE_PROJECTS] = $i18n->get('form.profile.mode_projects');
164 $tracking_mode_options[MODE_PROJECTS_AND_TASKS] = $i18n->get('form.profile.mode_projects_and_tasks');
165 $form->addInput(array('type'=>'combobox','name'=>'tracking_mode','style'=>'width: 150px;','data'=>$tracking_mode_options,'value'=>$cl_tracking_mode,'onchange'=>'handleTaskRequiredCheckbox()'));
166 $form->addInput(array('type'=>'checkbox','name'=>'project_required','value'=>$cl_project_required));
167 $form->addInput(array('type'=>'checkbox','name'=>'task_required','value'=>$cl_task_required));
168
169 // Prepare record type choices.
170 $record_type_options = array();
171 $record_type_options[TYPE_ALL] = $i18n->get('form.profile.type_all');
172 $record_type_options[TYPE_START_FINISH] = $i18n->get('form.profile.type_start_finish');
173 $record_type_options[TYPE_DURATION] = $i18n->get('form.profile.type_duration');
174 $form->addInput(array('type'=>'combobox','name'=>'record_type','style'=>'width: 150px;','data'=>$record_type_options,'value'=>$cl_record_type));
175
176 // Punch mode checkbox.
177 $form->addInput(array('type'=>'checkbox','name'=>'punch_mode','value'=>$cl_punch_mode));
178
179 // Allow overlap checkbox.
180 $form->addInput(array('type'=>'checkbox','name'=>'allow_overlap','value'=>$cl_allow_overlap));
181
182 // Future entries checkbox.
183 $form->addInput(array('type'=>'checkbox','name'=>'future_entries','value'=>$cl_future_entries));
184
185 // Uncompleted indicators checkbox.
186 $form->addInput(array('type'=>'checkbox','name'=>'uncompleted_indicators','value'=>$cl_uncompleted_indicators));
187
188 // Add bcc email control, for manager only.
189 if ($user->can('manage_advanced_settings')) {
190   $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'bcc_email','value'=>$cl_bcc_email));
191 }
192
193 // Plugin checkboxes.
194 $form->addInput(array('type'=>'checkbox','name'=>'charts','value'=>$cl_charts));
195 $form->addInput(array('type'=>'checkbox','name'=>'clients','value'=>$cl_clients,'onchange'=>'handlePluginCheckboxes()'));
196 $form->addInput(array('type'=>'checkbox','name'=>'client_required','value'=>$cl_client_required));
197 $form->addInput(array('type'=>'checkbox','name'=>'invoices','value'=>$cl_invoices));
198 $form->addInput(array('type'=>'checkbox','name'=>'paid_status','value'=>$cl_paid_status));
199 $form->addInput(array('type'=>'checkbox','name'=>'custom_fields','value'=>$cl_custom_fields,'onchange'=>'handlePluginCheckboxes()'));
200 $form->addInput(array('type'=>'checkbox','name'=>'expenses','value'=>$cl_expenses,'onchange'=>'handlePluginCheckboxes()'));
201 $form->addInput(array('type'=>'checkbox','name'=>'tax_expenses','value'=>$cl_tax_expenses));
202 $form->addInput(array('type'=>'checkbox','name'=>'notifications','value'=>$cl_notifications,'onchange'=>'handlePluginCheckboxes()'));
203 $form->addInput(array('type'=>'checkbox','name'=>'locking','value'=>$cl_locking,'onchange'=>'handlePluginCheckboxes()'));
204 $form->addInput(array('type'=>'checkbox','name'=>'quotas','value'=>$cl_quotas,'onchange'=>'handlePluginCheckboxes()'));
205 $form->addInput(array('type'=>'checkbox','name'=>'week_view','value'=>$cl_week_view,'onchange'=>'handlePluginCheckboxes()'));
206 $form->addInput(array('type'=>'submit','name'=>'btn_save','value'=>$i18n->get('button.save')));
207
208 if ($request->isPost()) {
209   // Validate user input.
210   if (!ttValidString($cl_team, true)) $err->add($i18n->get('error.field'), $i18n->get('label.team_name'));
211   if (!ttValidString($cl_currency, true)) $err->add($i18n->get('error.field'), $i18n->get('label.currency'));
212   if ($user->can('manage_advanced_settings')) {
213     if (!ttValidEmail($cl_bcc_email, true)) $err->add($i18n->get('error.field'), $i18n->get('label.bcc'));
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_paid_status)
231         $plugins .= ',ps';
232       if ($cl_custom_fields)
233         $plugins .= ',cf';
234       if ($cl_expenses)
235         $plugins .= ',ex';
236       if ($cl_tax_expenses)
237         $plugins .= ',et';
238       if ($cl_notifications)
239         $plugins .= ',no';
240       if ($cl_locking)
241         $plugins .= ',lk';
242       if ($cl_quotas)
243         $plugins .= ',mq';
244       if ($cl_week_view)
245         $plugins .= ',wv';
246
247       // Recycle week view plugin options as they are not configured on this page.
248       $existing_plugins = explode(',', $user->plugins);
249       if (in_array('wvn', $existing_plugins))
250         $plugins .= ',wvn';
251       if (in_array('wvl', $existing_plugins))
252         $plugins .= ',wvl';
253       if (in_array('wvns', $existing_plugins))
254         $plugins .= ',wvns';
255
256       $plugins = trim($plugins, ',');
257
258       // Prepare config string.
259       if ($cl_show_holidays)
260         $config .= ',show_holidays';
261       if ($cl_punch_mode)
262         $config .= ',punch_mode';
263       if ($cl_allow_overlap)
264         $config .= ',allow_overlap';
265       if ($cl_future_entries)
266         $config .= ',future_entries';
267       if ($cl_uncompleted_indicators)
268         $config .= ',uncompleted_indicators';
269       $config = trim($config, ',');
270
271       $update_result = ttTeamHelper::update($user->team_id, array(
272         'name' => $cl_team,
273         'currency' => $cl_currency,
274         'lang' => $cl_lang,
275         'decimal_mark' => $cl_decimal_mark,
276         'date_format' => $cl_date_format,
277         'time_format' => $cl_time_format,
278         'week_start' => $cl_start_week,
279         'tracking_mode' => $cl_tracking_mode,
280         'project_required' => $cl_project_required,
281         'task_required' => $cl_task_required,
282         'record_type' => $cl_record_type,
283         'uncompleted_indicators' => $cl_uncompleted_indicators,
284         'bcc_email' => $cl_bcc_email,
285         'plugins' => $plugins,
286         'config' => $config));
287     }
288     if ($update_result) {
289       header('Location: time.php');
290       exit();
291     } else
292       $err->add($i18n->get('error.db'));
293   }
294 } // isPost
295
296 $smarty->assign('auth_external', $auth->isPasswordExternal());
297 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
298 $smarty->assign('onload', 'onLoad="handleTaskRequiredCheckbox(); handlePluginCheckboxes();"');
299 $smarty->assign('title', $i18n->get('title.group'));
300 $smarty->assign('content_page_name', 'group_edit.tpl');
301 $smarty->display('index.tpl');