14d0f3fbcc03f84cea86c549e4ffdba5a15fdd7e
[timetracker.git] / cf_monthly_quota.php
1 <?php
2
3 require_once('initialize.php');
4 require_once('plugins/MonthlyQuota.class.php');
5 import('form.Form');
6
7 // Access check.
8 if (!ttAccessCheck(right_manage_team)) {
9   header('Location: access_denied.php');
10   exit();
11 }
12
13 $form = new Form('monthlyQuotaForm');
14 // months are zero indexed
15 $months = $i18n->monthNames;
16 $years = array();
17 for ($i=1990; $i < 2040; $i++) { 
18   array_push($years, array('id'=>$i, 'name'=>$i));
19 }
20
21 $year = $request->getParameter("year");
22 if (!$year or !ttValidInteger($year)){
23   $year = date("Y");
24 }else {
25   $year = intval($year);
26 }
27
28 $quota = new MonthlyQuota();
29
30 if ($request->isPost()){
31   $postedYear = $request->getParameter("years");
32   $year = intval($postedYear);
33   $res = false;
34   for ($i=0; $i < count($months); $i++){
35     $res = $quota->update($postedYear, $i+1, $request->getParameter($months[$i]));
36   }
37   if ($res){
38     header('Location: profile_edit.php');
39     exit();
40   } else
41       $err->add($i18n->getKey('error.db'));
42 }
43
44 // returns months where January is month 1, not 0
45 $monthsData = $quota->get($year);
46
47 $form->addInput(array('type'=>'combobox', 'name'=>'years', 'data'=>$years, 'datakeys'=>array('id', 'name'), 'value'=>$year, 'onchange'=>'yearChange(this.value);'));
48 for ($i=0; $i < count($months); $i++) { 
49   $value = "";
50   if (array_key_exists($i+1, $monthsData)){
51     $value = $monthsData[$i+1];
52   }
53   $name = $months[$i];
54   $form->addInput(array('type'=>'text', 'name'=>$name, 'maxlength'=>3, 'value'=> $value, 'style'=>'width:50px'));
55 }
56 $smarty->assign('months', $months);
57 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
58 $smarty->assign('content_page_name', 'cf_monthly_quota.tpl');
59 $smarty->display('index.tpl');