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