posaune
[timetracker.git] / quotas.php
index 06fdbbe..b6d63fa 100644 (file)
 require_once('initialize.php');
 require_once('plugins/MonthlyQuota.class.php');
 import('form.Form');
-import('ttTeamHelper');
 import('ttTimeHelper');
 
-// Access check.
-if (!ttAccessAllowed('manage_advanced_settings') || !$user->isPluginEnabled('mq')) {
+// Access checks.
+if (!ttAccessAllowed('manage_advanced_settings')) {
   header('Location: access_denied.php');
   exit();
 }
+if (!$user->isPluginEnabled('mq')) {
+  header('Location: feature_disabled.php');
+  exit();
+}
 
 // Start and end fallback values for the Year dropdown.
 $yearStart = 2015;
@@ -71,13 +74,15 @@ $quota = new MonthlyQuota();
 
 if ($request->isPost()){
   // Validate user input.
-  if (false === ttTimeHelper::postedDurationToMinutes($request->getParameter('workdayHours')))
-    $err->add($i18n->getKey('error.field'), $i18n->getKey('form.quota.workday_hours'));
+  $workdayMinutes = ttTimeHelper::postedDurationToMinutes($request->getParameter('workdayHours'));
+  if (false === $workdayMinutes || $workdayMinutes <= 0 )
+    $err->add($i18n->get('error.field'), $i18n->get('form.quota.workday_hours'));
 
   for ($i = 0; $i < count($months); $i++){
     $val = $request->getParameter($months[$i]);
-    if (false === ttTimeHelper::postedDurationToMinutes($val, 44640/*24*60*31*/))
-      $err->add($i18n->getKey('error.field'), $months[$i]);
+    $monthMinutes = ttTimeHelper::postedDurationToMinutes($val, 44640/*24*60*31*/);
+    if (false === $monthMinutes || $monthMinutes < 0)
+      $err->add($i18n->get('error.field'), $months[$i]);
   }
   // Finished validating user input.
 
@@ -85,9 +90,9 @@ if ($request->isPost()){
 
     // Handle workday hours.
     $workday_minutes = ttTimeHelper::postedDurationToMinutes($request->getParameter('workdayHours'));
-    if ($workday_minutes != $user->workday_minutes) {
-      if (!ttTeamHelper::update($user->team_id, array('name'=>$user->team,'workday_minutes'=>$workday_minutes)))
-        $err->add($i18n->getKey('error.db'));
+    if ($workday_minutes != $user->getWorkdayMinutes()) {
+      if (!$user->updateGroup(array('workday_minutes'=>$workday_minutes)))
+        $err->add($i18n->get('error.db'));
     }
 
     // Handle monthly quotas for a selected year.
@@ -95,7 +100,7 @@ if ($request->isPost()){
     for ($i = 0; $i < count($months); $i++){
       $quota_in_minutes = ttTimeHelper::postedDurationToMinutes($request->getParameter($months[$i]), 44640/*24*60*31*/);
       if (!$quota->update($selectedYear, $i+1, $quota_in_minutes))
-        $err->add($i18n->getKey('error.db'));
+        $err->add($i18n->get('error.db'));
     }
 
     if ($err->no()) {
@@ -108,7 +113,7 @@ if ($request->isPost()){
 
 // Get monthly quotas for the entire year.
 $monthsData = $quota->get($selectedYear);
-$workdayHours = ttTimeHelper::toAbsDuration($user->workday_minutes, true);
+$workdayHours = ttTimeHelper::toAbsDuration($user->getWorkdayMinutes(), true);
 
 $form = new Form('monthlyQuotasForm');
 $form->addInput(array('type'=>'text', 'name'=>'workdayHours', 'value'=>$workdayHours, 'style'=>'width:60px'));
@@ -125,6 +130,6 @@ for ($i=0; $i < count($months); $i++) {
 
 $smarty->assign('months', $months);
 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
-$smarty->assign('title', $i18n->getKey('title.monthly_quotas'));
+$smarty->assign('title', $i18n->get('title.monthly_quotas'));
 $smarty->assign('content_page_name', 'quotas.tpl');
 $smarty->display('index.tpl');