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.
 
  11 // | There are only two ways to violate the license:
 
  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).
 
  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).
 
  21 // | This license applies to this document only, not any other software
 
  22 // | that it may be combined with.
 
  24 // +----------------------------------------------------------------------+
 
  26 // | https://www.anuko.com/time_tracker/credits.htm
 
  27 // +----------------------------------------------------------------------+
 
  29 require_once('initialize.php');
 
  31 import('ttUserHelper');
 
  32 import('ttRoleHelper');
 
  33 import('ttConfigHelper');
 
  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();
 
  43   $group_id = $request->getParameter('group') ? $request->getParameter('group') : $user->getGroup();
 
  45 $home_group = $user->group_id == $group_id;
 
  47   // Editing home group.
 
  48   if (!ttAccessAllowed('manage_basic_settings')) {
 
  49     header('Location: access_denied.php'); // Not allowed to edit home group settings.
 
  53   // Editing a subgroup.
 
  54   if (!ttAccessAllowed('manage_subgroups')) {
 
  55     header('Location: access_denied.php'); // No right to manage subgroups.
 
  58   if (!$user->isSubgroupValid($group_id)) {
 
  59     header('Location: access_denied.php'); // Wrong subgroup.
 
  63 // End of access checks.
 
  65 // Set on behalf group accordingly.
 
  66 $groupChanged = $request->getParameter('group_changed');
 
  67 if ($request->isPost() && $groupChanged) {
 
  68  $user->setOnBehalfGroup($group_id);
 
  71 $groups = $user->getGroupsForDropdown();
 
  72 $group = ttGroupHelper::getGroupAttrs($group_id);
 
  73 $config = $user->getConfigHelper();
 
  75 if (!defined('CURRENCY_DEFAULT')) define('CURRENCY_DEFAULT', '$');
 
  77 if ($request->isPost() && !$groupChanged) {
 
  78   $cl_currency = trim($request->getParameter('currency'));
 
  79   if (!$cl_currency) $cl_currency = CURRENCY_DEFAULT;
 
  80   $cl_lang = $request->getParameter('lang');
 
  81   $cl_decimal_mark = $request->getParameter('decimal_mark');
 
  82   $cl_date_format = $request->getParameter('date_format');
 
  83   $cl_time_format = $request->getParameter('time_format');
 
  84   $cl_start_week = $request->getParameter('start_week');
 
  85   $cl_holidays = trim($request->getParameter('holidays'));
 
  86   $cl_tracking_mode = $request->getParameter('tracking_mode');
 
  87   $cl_project_required = $request->getParameter('project_required');
 
  88   $cl_task_required = $request->getParameter('task_required');
 
  89   $cl_record_type = $request->getParameter('record_type');
 
  90   $cl_punch_mode = $request->getParameter('punch_mode');
 
  91   $cl_allow_overlap = $request->getParameter('allow_overlap');
 
  92   $cl_future_entries = $request->getParameter('future_entries');
 
  93   $cl_uncompleted_indicators = $request->getParameter('uncompleted_indicators');
 
  94   $cl_confirm_save = $request->getParameter('confirm_save');
 
  96   $cl_currency = ($group['currency'] == ''? CURRENCY_DEFAULT : $group['currency']);
 
  97   $cl_lang = $group['lang'];
 
  98   $cl_decimal_mark = $group['decimal_mark'];
 
  99   $cl_date_format = $group['date_format'];
 
 100   $cl_time_format = $group['time_format'];
 
 101   $cl_start_week = $group['week_start'];
 
 102   $cl_holidays = $group['holidays'];
 
 103   $cl_tracking_mode = $group['tracking_mode'];
 
 104   $cl_project_required = $group['project_required'];
 
 105   $cl_task_required = $group['task_required'];
 
 106   $cl_record_type = $group['record_type'];
 
 107   $cl_punch_mode = $config->getDefinedValue('punch_mode');
 
 108   $cl_allow_overlap = $config->getDefinedValue('allow_overlap');
 
 109   $cl_future_entries = $config->getDefinedValue('future_entries');
 
 110   $cl_uncompleted_indicators = $config->getDefinedValue('uncompleted_indicators');
 
 111   $cl_confirm_save = $config->getDefinedValue('confirm_save');
 
 114 $form = new Form('groupForm');
 
 115 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$group_id));
 
 116 if (count($groups) > 1) {
 
 117   $form->addInput(array('type'=>'combobox',
 
 118     'onchange'=>'document.groupForm.group_changed.value=1;document.groupForm.submit();',
 
 120     'style'=>'width: 250px;',
 
 123     'datakeys'=>array('id','name')));
 
 124   $form->addInput(array('type'=>'hidden','name'=>'group_changed'));
 
 125   $smarty->assign('group_dropdown', 1);
 
 127 $form->addInput(array('type'=>'text','maxlength'=>'7','name'=>'currency','value'=>$cl_currency));
 
 129 // Prepare an array of available languages.
 
 130 $lang_files = I18n::getLangFileList();
 
 131 foreach ($lang_files as $lfile) {
 
 132   $content = file(RESOURCE_DIR."/".$lfile);
 
 134   foreach ($content as $line) {
 
 135     if (strstr($line, 'i18n_language')) {
 
 136       $a = explode('=', $line);
 
 137       $lname = trim(str_replace(';','',str_replace("'","",$a[1])));
 
 142   $longname_lang[] = array('id'=>I18n::getLangFromFilename($lfile),'name'=>$lname);
 
 144 $longname_lang = mu_sort($longname_lang, 'name');
 
 145 $form->addInput(array('type'=>'combobox','name'=>'lang','style'=>'width: 200px','data'=>$longname_lang,'datakeys'=>array('id','name'),'value'=>$cl_lang));
 
 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()'));
 
 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("date_format_preview", this);'));
 
 158 $TIME_FORMAT_OPTIONS = array(
 
 159   array('id'=>'%H:%M','name'=>$i18n->get('form.group_edit.24_hours')),
 
 160   array('id'=>'%I:%M %p','name'=>$i18n->get('form.group_edit.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("time_format_preview", this);'));
 
 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);
 
 169 $form->addInput(array('type'=>'combobox','name'=>'start_week','style'=>'width: 150px;','data'=>$week_start_options,'datakeys'=>array('id','name'),'value'=>$cl_start_week));
 
 171 // Show holidays control.
 
 172 $form->addInput(array('type'=>'text','name'=>'holidays','value'=>$cl_holidays));
 
 174 // Prepare tracking mode choices.
 
 175 $tracking_mode_options = array();
 
 176 $tracking_mode_options[MODE_TIME] = $i18n->get('form.group_edit.mode_time');
 
 177 $tracking_mode_options[MODE_PROJECTS] = $i18n->get('form.group_edit.mode_projects');
 
 178 $tracking_mode_options[MODE_PROJECTS_AND_TASKS] = $i18n->get('form.group_edit.mode_projects_and_tasks');
 
 179 $form->addInput(array('type'=>'combobox','name'=>'tracking_mode','style'=>'width: 150px;','data'=>$tracking_mode_options,'value'=>$cl_tracking_mode,'onchange'=>'handleTaskRequiredCheckbox()'));
 
 180 $form->addInput(array('type'=>'checkbox','name'=>'project_required','value'=>$cl_project_required));
 
 181 $form->addInput(array('type'=>'checkbox','name'=>'task_required','value'=>$cl_task_required));
 
 183 // Prepare record type choices.
 
 184 $record_type_options = array();
 
 185 $record_type_options[TYPE_ALL] = $i18n->get('form.group_edit.type_all');
 
 186 $record_type_options[TYPE_START_FINISH] = $i18n->get('form.group_edit.type_start_finish');
 
 187 $record_type_options[TYPE_DURATION] = $i18n->get('form.group_edit.type_duration');
 
 188 $form->addInput(array('type'=>'combobox','name'=>'record_type','style'=>'width: 150px;','data'=>$record_type_options,'value'=>$cl_record_type));
 
 190 // Punch mode checkbox.
 
 191 $form->addInput(array('type'=>'checkbox','name'=>'punch_mode','value'=>$cl_punch_mode));
 
 193 // Allow overlap checkbox.
 
 194 $form->addInput(array('type'=>'checkbox','name'=>'allow_overlap','value'=>$cl_allow_overlap));
 
 196 // Future entries checkbox.
 
 197 $form->addInput(array('type'=>'checkbox','name'=>'future_entries','value'=>$cl_future_entries));
 
 199 // Uncompleted indicators checkbox.
 
 200 $form->addInput(array('type'=>'checkbox','name'=>'uncompleted_indicators','value'=>$cl_uncompleted_indicators));
 
 202 // Confirm save checkbox.
 
 203 $form->addInput(array('type'=>'checkbox','name'=>'confirm_save','value'=>$cl_confirm_save));
 
 205 $form->addInput(array('type'=>'submit','name'=>'btn_save','value'=>$i18n->get('button.save')));
 
 206 if ($user->can('delete_group')) $form->addInput(array('type'=>'submit','name'=>'btn_delete','value'=>$i18n->get('button.delete')));
 
 208 $form->setValueByElement('group_changed','');
 
 210 if ($request->isPost()) {
 
 212   if ($request->getParameter('btn_delete')) {
 
 213     // Delete button pressed, redirect.
 
 214     header('Location: group_delete.php?id='.$group_id);
 
 218   if ($request->getParameter('btn_save')) {
 
 219     // Validate user input.
 
 220     if (!ttValidString($cl_currency, true)) $err->add($i18n->get('error.field'), $i18n->get('label.currency'));
 
 221     if (!ttValidHolidays($cl_holidays)) $err->add($i18n->get('error.field'), $i18n->get('form.group_edit.holidays'));
 
 222     // Finished validating user input.
 
 226       $config->setDefinedValue('punch_mode', $cl_punch_mode);
 
 227       $config->setDefinedValue('allow_overlap', $cl_allow_overlap);
 
 228       $config->setDefinedValue('future_entries', $cl_future_entries);
 
 229       $config->setDefinedValue('uncompleted_indicators', $cl_uncompleted_indicators);
 
 230       $config->setDefinedValue('confirm_save', $cl_confirm_save);
 
 232       if ($user->updateGroup(array(
 
 233         'group_id' => $group_id,
 
 234         'currency' => $cl_currency,
 
 236         'decimal_mark' => $cl_decimal_mark,
 
 237         'date_format' => $cl_date_format,
 
 238         'time_format' => $cl_time_format,
 
 239         'week_start' => $cl_start_week,
 
 240         'holidays' => $cl_holidays,
 
 241         'tracking_mode' => $cl_tracking_mode,
 
 242         'project_required' => $cl_project_required,
 
 243         'task_required' => $cl_task_required,
 
 244         'record_type' => $cl_record_type,
 
 245         'uncompleted_indicators' => $cl_uncompleted_indicators,
 
 246         'config' => $config->getConfig()))) {
 
 247         header('Location: success.php');
 
 250         $err->add($i18n->get('error.db'));
 
 255 $smarty->assign('group_dropdown', count($groups) > 1);
 
 256 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
 
 257 $smarty->assign('onload', 'onLoad="handleTaskRequiredCheckbox(); handlePluginCheckboxes();"');
 
 258 $smarty->assign('title', $i18n->get('title.edit_group'));
 
 259 $smarty->assign('content_page_name', 'group_edit.tpl');
 
 260 $smarty->display('index.tpl');