Adjusted monthly quotas plugin to use configurable holidays.
[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 // There are 4 distinct situations:
37 //   1) Editing home group in get or post.
38 //   2) Editing a subgroup in get or post.
39 // We'll check access separately as it is about different right checks.
40 if ($request->isGet()) {
41   $group_id = $request->getParameter('id') ? $request->getParameter('id') : $user->getGroup();
42 } else {
43   $group_id = $request->getParameter('group') ? $request->getParameter('group') : $user->getGroup();
44 }
45 $home_group = $user->group_id == $group_id;
46 if ($home_group) {
47   // Editing home group.
48   if (!(ttAccessAllowed('manage_basic_settings') || ttAccessAllowed('manage_advanced_settings'))) {
49     header('Location: access_denied.php'); // Not allowed to edit home group settings.
50     exit();
51   }
52 } else {
53   // Editing a subgroup.
54   if (!ttAccessAllowed('manage_subgroups')) {
55     header('Location: access_denied.php'); // No right to manage subgroups.
56     exit();
57   }
58   if (!$user->isSubgroupValid($group_id)) {
59     header('Location: access_denied.php'); // Wrong subgroup.
60     exit();
61   }
62 }
63 // End of access checks.
64
65 // Set on behalf group accordingly.
66 $groupChanged = $request->getParameter('group_changed');
67 if ($request->isPost() && $groupChanged) {
68  $user->setOnBehalfGroup($group_id);
69 }
70
71 $groups = $user->getGroupsForDropdown();
72 $group = ttGroupHelper::getGroupAttrs($group_id);
73 $config = new ttConfigHelper($group['config']);
74
75 $advanced_settings = $home_group ? $user->can('manage_advanced_settings') : true;
76 if (!defined('CURRENCY_DEFAULT')) define('CURRENCY_DEFAULT', '$');
77
78 if ($request->isPost() && !$groupChanged) {
79   $cl_group = trim($request->getParameter('group_name'));
80   $cl_description = trim($request->getParameter('description'));
81   $cl_currency = trim($request->getParameter('currency'));
82   if (!$cl_currency) $cl_currency = CURRENCY_DEFAULT;
83   $cl_lang = $request->getParameter('lang');
84   $cl_decimal_mark = $request->getParameter('decimal_mark');
85   $cl_date_format = $request->getParameter('date_format');
86   $cl_time_format = $request->getParameter('time_format');
87   $cl_start_week = $request->getParameter('start_week');
88   $cl_show_holidays = $request->getParameter('show_holidays');
89   $cl_holidays = trim($request->getParameter('holidays'));
90   $cl_tracking_mode = $request->getParameter('tracking_mode');
91   $cl_project_required = $request->getParameter('project_required');
92   $cl_task_required = $request->getParameter('task_required');
93   $cl_record_type = $request->getParameter('record_type');
94   $cl_punch_mode = $request->getParameter('punch_mode');
95   $cl_allow_overlap = $request->getParameter('allow_overlap');
96   $cl_future_entries = $request->getParameter('future_entries');
97   $cl_uncompleted_indicators = $request->getParameter('uncompleted_indicators');
98   $cl_confirm_save = $request->getParameter('confirm_save');
99   $cl_bcc_email = trim($request->getParameter('bcc_email'));
100   $cl_allow_ip = trim($request->getParameter('allow_ip'));
101 } else {
102   $cl_group = $group['name'];
103   $cl_description = $group['description'];
104   $cl_currency = ($group['currency'] == ''? CURRENCY_DEFAULT : $group['currency']);
105   $cl_lang = $group['lang'];
106   $cl_decimal_mark = $group['decimal_mark'];
107   $cl_date_format = $group['date_format'];
108   $cl_time_format = $group['time_format'];
109   $cl_start_week = $group['week_start'];
110   $cl_show_holidays = $config->getDefinedValue('show_holidays');
111   $cl_holidays = $group['holidays'];
112   $cl_tracking_mode = $group['tracking_mode'];
113   $cl_project_required = $group['project_required'];
114   $cl_task_required = $group['task_required'];
115   $cl_record_type = $group['record_type'];
116   $cl_punch_mode = $config->getDefinedValue('punch_mode');
117   $cl_allow_overlap = $config->getDefinedValue('allow_overlap');
118   $cl_future_entries = $config->getDefinedValue('future_entries');
119   $cl_uncompleted_indicators = $config->getDefinedValue('uncompleted_indicators');
120   $cl_confirm_save = $config->getDefinedValue('confirm_save');
121   $cl_bcc_email = $group['bcc_email'];
122   $cl_allow_ip = $group['allow_ip'];
123 }
124
125 $form = new Form('groupForm');
126 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$group_id));
127 if (count($groups) > 1) {
128   $form->addInput(array('type'=>'combobox',
129     'onchange'=>'document.groupForm.group_changed.value=1;document.groupForm.submit();',
130     'name'=>'group',
131     'style'=>'width: 250px;',
132     'value'=>$group_id,
133     'data'=>$groups,
134     'datakeys'=>array('id','name')));
135   $form->addInput(array('type'=>'hidden','name'=>'group_changed'));
136   $smarty->assign('group_dropdown', 1);
137 }
138 $form->addInput(array('type'=>'text','maxlength'=>'200','name'=>'group_name','value'=>$cl_group,'enable'=>$advanced_settings));
139 $form->addInput(array('type'=>'textarea','name'=>'description','style'=>'width: 250px; height: 40px;','value'=>$cl_description));
140 $form->addInput(array('type'=>'text','maxlength'=>'7','name'=>'currency','value'=>$cl_currency));
141
142 // Prepare an array of available languages.
143 $lang_files = I18n::getLangFileList();
144 foreach ($lang_files as $lfile) {
145   $content = file(RESOURCE_DIR."/".$lfile);
146   $lname = '';
147   foreach ($content as $line) {
148     if (strstr($line, 'i18n_language')) {
149       $a = explode('=', $line);
150       $lname = trim(str_replace(';','',str_replace("'","",$a[1])));
151       break;
152     }
153   }
154   unset($content);
155   $longname_lang[] = array('id'=>I18n::getLangFromFilename($lfile),'name'=>$lname);
156 }
157 $longname_lang = mu_sort($longname_lang, 'name');
158 $form->addInput(array('type'=>'combobox','name'=>'lang','style'=>'width: 200px','data'=>$longname_lang,'datakeys'=>array('id','name'),'value'=>$cl_lang));
159
160 $DECIMAL_MARK_OPTIONS = array(array('id'=>'.','name'=>'.'),array('id'=>',','name'=>','));
161 $form->addInput(array('type'=>'combobox','name'=>'decimal_mark','style'=>'width: 150px','data'=>$DECIMAL_MARK_OPTIONS,'datakeys'=>array('id','name'),'value'=>$cl_decimal_mark,
162   'onchange'=>'adjustDecimalPreview()'));
163
164 $DATE_FORMAT_OPTIONS = array(
165   array('id'=>'%Y-%m-%d','name'=>'Y-m-d'),
166   array('id'=>'%m/%d/%Y','name'=>'m/d/Y'),
167   array('id'=>'%d.%m.%Y','name'=>'d.m.Y'),
168   array('id'=>'%d.%m.%Y %a','name'=>'d.m.Y a'));
169 $form->addInput(array('type'=>'combobox','name'=>'date_format','style'=>'width: 150px;','data'=>$DATE_FORMAT_OPTIONS,'datakeys'=>array('id','name'),'value'=>$cl_date_format,
170   'onchange'=>'MakeFormatPreview(&quot;date_format_preview&quot;, this);'));
171 $TIME_FORMAT_OPTIONS = array(
172   array('id'=>'%H:%M','name'=>$i18n->get('form.group_edit.24_hours')),
173   array('id'=>'%I:%M %p','name'=>$i18n->get('form.group_edit.12_hours')));
174 $form->addInput(array('type'=>'combobox','name'=>'time_format','style'=>'width: 150px;','data'=>$TIME_FORMAT_OPTIONS,'datakeys'=>array('id','name'),'value'=>$cl_time_format,
175   'onchange'=>'MakeFormatPreview(&quot;time_format_preview&quot;, this);'));
176
177 // Prepare week start choices.
178 $week_start_options = array();
179 foreach ($i18n->weekdayNames as $id => $week_dn) {
180   $week_start_options[] = array('id' => $id, 'name' => $week_dn);
181 }
182 $form->addInput(array('type'=>'combobox','name'=>'start_week','style'=>'width: 150px;','data'=>$week_start_options,'datakeys'=>array('id','name'),'value'=>$cl_start_week));
183
184 // Show holidays checkbox.
185 $form->addInput(array('type'=>'checkbox','name'=>'show_holidays','value'=>$cl_show_holidays));
186 $form->addInput(array('type'=>'text','name'=>'holidays','value'=>$cl_holidays));
187
188 // Prepare tracking mode choices.
189 $tracking_mode_options = array();
190 $tracking_mode_options[MODE_TIME] = $i18n->get('form.group_edit.mode_time');
191 $tracking_mode_options[MODE_PROJECTS] = $i18n->get('form.group_edit.mode_projects');
192 $tracking_mode_options[MODE_PROJECTS_AND_TASKS] = $i18n->get('form.group_edit.mode_projects_and_tasks');
193 $form->addInput(array('type'=>'combobox','name'=>'tracking_mode','style'=>'width: 150px;','data'=>$tracking_mode_options,'value'=>$cl_tracking_mode,'onchange'=>'handleTaskRequiredCheckbox()'));
194 $form->addInput(array('type'=>'checkbox','name'=>'project_required','value'=>$cl_project_required));
195 $form->addInput(array('type'=>'checkbox','name'=>'task_required','value'=>$cl_task_required));
196
197 // Prepare record type choices.
198 $record_type_options = array();
199 $record_type_options[TYPE_ALL] = $i18n->get('form.group_edit.type_all');
200 $record_type_options[TYPE_START_FINISH] = $i18n->get('form.group_edit.type_start_finish');
201 $record_type_options[TYPE_DURATION] = $i18n->get('form.group_edit.type_duration');
202 $form->addInput(array('type'=>'combobox','name'=>'record_type','style'=>'width: 150px;','data'=>$record_type_options,'value'=>$cl_record_type));
203
204 // Punch mode checkbox.
205 $form->addInput(array('type'=>'checkbox','name'=>'punch_mode','value'=>$cl_punch_mode));
206
207 // Allow overlap checkbox.
208 $form->addInput(array('type'=>'checkbox','name'=>'allow_overlap','value'=>$cl_allow_overlap));
209
210 // Future entries checkbox.
211 $form->addInput(array('type'=>'checkbox','name'=>'future_entries','value'=>$cl_future_entries));
212
213 // Uncompleted indicators checkbox.
214 $form->addInput(array('type'=>'checkbox','name'=>'uncompleted_indicators','value'=>$cl_uncompleted_indicators));
215
216 // Confirm save checkbox.
217 $form->addInput(array('type'=>'checkbox','name'=>'confirm_save','value'=>$cl_confirm_save));
218
219 // Add bcc email control.
220 if ($advanced_settings) {
221   $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'bcc_email','value'=>$cl_bcc_email));
222   $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'allow_ip','value'=>$cl_allow_ip));
223 }
224
225 $form->addInput(array('type'=>'submit','name'=>'btn_save','value'=>$i18n->get('button.save')));
226 if ($user->can('delete_group')) $form->addInput(array('type'=>'submit','name'=>'btn_delete','value'=>$i18n->get('button.delete')));
227
228 $form->setValueByElement('group_changed','');
229
230 if ($request->isPost()) {
231
232   if ($request->getParameter('btn_delete')) {
233     // Delete button pressed, redirect.
234     header('Location: group_delete.php?id='.$group_id);
235     exit();
236   }
237
238   if ($request->getParameter('btn_save')) {
239     // Validate user input.
240     if (!ttValidString($cl_group)) $err->add($i18n->get('error.field'), $i18n->get('label.group_name'));
241     if (!ttValidString($cl_description, true)) $err->add($i18n->get('error.field'), $i18n->get('label.description'));
242     if (!ttValidString($cl_currency, true)) $err->add($i18n->get('error.field'), $i18n->get('label.currency'));
243     if (!ttValidHolidays($cl_holidays)) $err->add($i18n->get('error.field'), $i18n->get('form.group_edit.holidays'));
244     if ($advanced_settings) {
245       if (!ttValidEmail($cl_bcc_email, true)) $err->add($i18n->get('error.field'), $i18n->get('label.bcc'));
246       if (!ttValidIP($cl_allow_ip, true)) $err->add($i18n->get('error.field'), $i18n->get('form.group_edit.allow_ip'));
247     }
248     // Finished validating user input.
249
250     if ($err->no()) {
251       // Update config.
252       $config->setDefinedValue('show_holidays', $cl_show_holidays);
253       $config->setDefinedValue('punch_mode', $cl_punch_mode);
254       $config->setDefinedValue('allow_overlap', $cl_allow_overlap);
255       $config->setDefinedValue('future_entries', $cl_future_entries);
256       $config->setDefinedValue('uncompleted_indicators', $cl_uncompleted_indicators);
257       $config->setDefinedValue('confirm_save', $cl_confirm_save);
258
259       if ($user->updateGroup(array(
260         'group_id' => $group_id,
261         'name' => $cl_group,
262         'description' => $cl_description,
263         'currency' => $cl_currency,
264         'lang' => $cl_lang,
265         'decimal_mark' => $cl_decimal_mark,
266         'date_format' => $cl_date_format,
267         'time_format' => $cl_time_format,
268         'week_start' => $cl_start_week,
269         'holidays' => $cl_holidays,
270         'tracking_mode' => $cl_tracking_mode,
271         'project_required' => $cl_project_required,
272         'task_required' => $cl_task_required,
273         'record_type' => $cl_record_type,
274         'uncompleted_indicators' => $cl_uncompleted_indicators,
275         'bcc_email' => $cl_bcc_email,
276         'allow_ip' => $cl_allow_ip,
277         'config' => $config->getConfig()))) {
278         header('Location: success.php');
279         exit();
280       } else
281         $err->add($i18n->get('error.db'));
282     }
283   }
284 } // isPost
285
286 $smarty->assign('auth_external', $auth->isPasswordExternal());
287 $smarty->assign('group_id', $group_id);
288 $smarty->assign('group_dropdown', count($groups) > 1);
289 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
290 $smarty->assign('onload', 'onLoad="handleTaskRequiredCheckbox(); handlePluginCheckboxes();"');
291 $smarty->assign('title', $i18n->get('title.edit_group'));
292 $smarty->assign('content_page_name', 'group_edit.tpl');
293 $smarty->display('index.tpl');