Another try to fix tt_client_project_binds.
[timetracker.git] / group_edit.php
index 9c4cf36..44c5d7f 100644 (file)
@@ -37,15 +37,30 @@ if (!(ttAccessAllowed('manage_basic_settings') || ttAccessAllowed('manage_advanc
   header('Location: access_denied.php');
   exit();
 }
+$group_id = (int)$request->getParameter('id');
+if ($group_id && !$user->isGroupValid($group_id)) {
+  header('Location: access_denied.php');
+  exit();
+}
 // End of access checks.
 
-$config = new ttConfigHelper($user->config);
+if ($group_id) {
+  // We are passed a valid group_id.
+  // Set on behalf group accordingly.
+  $user->setOnBehalfGroup($group_id);
+}
+
+if (!$group_id) $group_id = $user->getGroup();
+$groups = $user->getGroupsForDropdown();
+$group = ttGroupHelper::getGroupAttrs($group_id);
+$config = new ttConfigHelper($group['config']);
 
 $advanced_settings = $user->can('manage_advanced_settings');
 if (!defined('CURRENCY_DEFAULT')) define('CURRENCY_DEFAULT', '$');
 
 if ($request->isPost()) {
   $cl_group = trim($request->getParameter('group_name'));
+  $cl_description = trim($request->getParameter('description'));
   $cl_currency = trim($request->getParameter('currency'));
   if (!$cl_currency) $cl_currency = CURRENCY_DEFAULT;
   $cl_lang = $request->getParameter('lang');
@@ -62,31 +77,47 @@ if ($request->isPost()) {
   $cl_allow_overlap = $request->getParameter('allow_overlap');
   $cl_future_entries = $request->getParameter('future_entries');
   $cl_uncompleted_indicators = $request->getParameter('uncompleted_indicators');
+  $cl_confirm_save = $request->getParameter('confirm_save');
   $cl_bcc_email = trim($request->getParameter('bcc_email'));
   $cl_allow_ip = trim($request->getParameter('allow_ip'));
 } else {
-  $cl_group = $user->group;
-  $cl_currency = ($user->currency == ''? CURRENCY_DEFAULT : $user->currency);
-  $cl_lang = $user->lang;
-  $cl_decimal_mark = $user->decimal_mark;
-  $cl_date_format = $user->date_format;
-  $cl_time_format = $user->time_format;
-  $cl_start_week = $user->week_start;
-  $cl_show_holidays = $user->show_holidays;
-  $cl_tracking_mode = $user->tracking_mode;
-  $cl_project_required = $user->project_required;
-  $cl_task_required = $user->task_required;
-  $cl_record_type = $user->record_type;
-  $cl_punch_mode = $user->punch_mode;
-  $cl_allow_overlap = $user->allow_overlap;
-  $cl_future_entries = $user->future_entries;
-  $cl_uncompleted_indicators = $user->uncompleted_indicators;
-  $cl_bcc_email = $user->bcc_email;
-  $cl_allow_ip = $user->allow_ip;
+  $cl_group = $group['name'];
+  $cl_description = $group['description'];
+  $cl_currency = ($group['currency'] == ''? CURRENCY_DEFAULT : $group['currency']);
+  $cl_lang = $group['lang'];
+  $cl_decimal_mark = $group['decimal_mark'];
+  $cl_date_format = $group['date_format'];
+  $cl_time_format = $group['time_format'];
+  $cl_start_week = $group['week_start'];
+  $cl_show_holidays = $config->getDefinedValue('show_holidays');
+  $cl_tracking_mode = $group['tracking_mode'];
+  $cl_project_required = $group['project_required'];
+  $cl_task_required = $group['task_required'];
+  $cl_record_type = $group['record_type'];
+  $cl_punch_mode = $config->getDefinedValue('punch_mode');
+  $cl_allow_overlap = $config->getDefinedValue('allow_overlap');
+  $cl_future_entries = $config->getDefinedValue('future_entries');
+  $cl_uncompleted_indicators = $config->getDefinedValue('uncompleted_indicators');
+  $cl_confirm_save = $config->getDefinedValue('confirm_save');
+  $cl_bcc_email = $group['bcc_email'];
+  $cl_allow_ip = $group['allow_ip'];
 }
 
 $form = new Form('groupForm');
+$form->addInput(array('type'=>'hidden','name'=>'id','value'=>$group_id));
+if (count($groups) > 1) {
+  $form->addInput(array('type'=>'combobox',
+    'onchange'=>'document.groupForm.group_changed.value=1;document.groupForm.submit();',
+    'name'=>'group',
+    'style'=>'width: 250px;',
+    'value'=>$group_id,
+    'data'=>$groups,
+    'datakeys'=>array('id','name')));
+  $form->addInput(array('type'=>'hidden','name'=>'group_changed'));
+  $smarty->assign('group_dropdown', 1);
+}
 $form->addInput(array('type'=>'text','maxlength'=>'200','name'=>'group_name','value'=>$cl_group,'enable'=>$advanced_settings));
+$form->addInput(array('type'=>'textarea','name'=>'description','style'=>'width: 250px; height: 40px;','value'=>$cl_description));
 $form->addInput(array('type'=>'text','maxlength'=>'7','name'=>'currency','value'=>$cl_currency));
 
 // Prepare an array of available languages.
@@ -162,6 +193,9 @@ $form->addInput(array('type'=>'checkbox','name'=>'future_entries','value'=>$cl_f
 // Uncompleted indicators checkbox.
 $form->addInput(array('type'=>'checkbox','name'=>'uncompleted_indicators','value'=>$cl_uncompleted_indicators));
 
+// Confirm save checkbox.
+$form->addInput(array('type'=>'checkbox','name'=>'confirm_save','value'=>$cl_confirm_save));
+
 // Add bcc email control.
 if ($advanced_settings) {
   $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'bcc_email','value'=>$cl_bcc_email));
@@ -171,16 +205,26 @@ if ($advanced_settings) {
 $form->addInput(array('type'=>'submit','name'=>'btn_save','value'=>$i18n->get('button.save')));
 if ($user->can('delete_group')) $form->addInput(array('type'=>'submit','name'=>'btn_delete','value'=>$i18n->get('button.delete')));
 
+$form->setValueByElement('group_changed','');
+
 if ($request->isPost()) {
+  if ($request->getParameter('group_changed')) {
+    // User changed the group in dropdown.
+    $new_group_id = $request->getParameter('group');
+    // Redirect to self.
+    header('Location: group_edit.php?id='.$new_group_id);
+    exit();
+  }
 
   if ($request->getParameter('btn_delete')) {
     // Delete button pressed, redirect.
-    header('Location: group_delete.php?id='.$user->group_id);
+    header('Location: group_delete.php?id='.$group_id);
     exit();
   }
 
   // Validate user input.
-  if (!ttValidString($cl_group, true)) $err->add($i18n->get('error.field'), $i18n->get('label.group_name'));
+  if (!ttValidString($cl_group)) $err->add($i18n->get('error.field'), $i18n->get('label.group_name'));
+  if (!ttValidString($cl_description, true)) $err->add($i18n->get('error.field'), $i18n->get('label.description'));
   if (!ttValidString($cl_currency, true)) $err->add($i18n->get('error.field'), $i18n->get('label.currency'));
   if ($advanced_settings) {
     if (!ttValidEmail($cl_bcc_email, true)) $err->add($i18n->get('error.field'), $i18n->get('label.bcc'));
@@ -195,9 +239,12 @@ if ($request->isPost()) {
     $config->setDefinedValue('allow_overlap', $cl_allow_overlap);
     $config->setDefinedValue('future_entries', $cl_future_entries);
     $config->setDefinedValue('uncompleted_indicators', $cl_uncompleted_indicators);
+    $config->setDefinedValue('confirm_save', $cl_confirm_save);
 
     if ($user->updateGroup(array(
+      'group_id' => $group_id,
       'name' => $cl_group,
+      'description' => $cl_description,
       'currency' => $cl_currency,
       'lang' => $cl_lang,
       'decimal_mark' => $cl_decimal_mark,
@@ -212,7 +259,7 @@ if ($request->isPost()) {
       'bcc_email' => $cl_bcc_email,
       'allow_ip' => $cl_allow_ip,
       'config' => $config->getConfig()))) {
-      header('Location: time.php');
+      header('Location: success.php');
       exit();
     } else
       $err->add($i18n->get('error.db'));
@@ -220,8 +267,10 @@ if ($request->isPost()) {
 } // isPost
 
 $smarty->assign('auth_external', $auth->isPasswordExternal());
+$smarty->assign('group_id', $group_id);
+$smarty->assign('group_dropdown', count($groups) > 1);
 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
 $smarty->assign('onload', 'onLoad="handleTaskRequiredCheckbox(); handlePluginCheckboxes();"');
-$smarty->assign('title', $i18n->get('title.group'));
+$smarty->assign('title', $i18n->get('title.edit_group'));
 $smarty->assign('content_page_name', 'group_edit.tpl');
 $smarty->display('index.tpl');