Added export of monthly quotas data.
[timetracker.git] / quotas.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 require_once('plugins/MonthlyQuota.class.php');
31 import('form.Form');
32 import('ttTeamHelper');
33
34 // Access check.
35 if (!ttAccessCheck(right_manage_team)) {
36   header('Location: access_denied.php');
37   exit();
38 }
39
40 // Start and end fallback values for the Year dropdown.
41 $yearStart = 2015;
42 $yearEnd = 2030;
43
44 // If values are defined in config - use them.
45 if (defined('MONTHLY_QUOTA_YEAR_START')){
46   $yearStart = (int)MONTHLY_QUOTA_YEAR_START;
47 }
48 if (defined('MONTHLY_QUOTA_YEAR_END')){
49   $yearEnd = (int)MONTHLY_QUOTA_YEAR_END;
50 }
51
52 // Create values for the Year dropdown.
53 $years = array();
54 for ($i = $yearStart; $i <= $yearEnd; $i++) {
55   array_push($years, array('id'=>$i,'name'=>$i));
56 }
57
58 // Get selected year from url parameter.
59 $selectedYear = $request->getParameter('year');
60 if (!$selectedYear or !ttValidInteger($selectedYear)){
61   $selectedYear = date('Y');
62 } else {
63   $selectedYear = intval($selectedYear);
64 }
65
66 // Months are zero indexed.
67 $months = $i18n->monthNames;
68
69 $quota = new MonthlyQuota();
70
71 if ($request->isPost()){
72   // TODO: Add parameter validation.
73   $res = false;
74   if ($_POST['btn_hours']){
75
76     // User changed workday hours for team.
77     $hours = (int)$request->getParameter('workdayHours');
78     $res = ttTeamHelper::update($user->team_id, array('name'=>$user->team,'workday_hours'=>$hours));
79   }
80   if ($_POST['btn_submit']){
81     // User pressed the Save button under monthly quotas table.
82     $postedYear = $request->getParameter('year');
83     $selectedYear = intval($postedYear);
84     for ($i = 0; $i < count($months); $i++){
85       $res = $quota->update($postedYear, $i+1, $request->getParameter($months[$i]));
86     }
87   }
88   if ($res) {
89     header('Location: profile_edit.php');
90     exit();
91   } else {
92     $err->add($i18n->getKey('error.db'));
93   }
94 }
95
96 // Returns monthly quotas where January is month 1, not 0.
97 $monthsData = $quota->get($selectedYear);
98
99 $form = new Form('monthlyQuotasForm');
100 $form->addInput(array('type'=>'text', 'name'=>'workdayHours', 'value'=>$user->workday_hours, 'style'=>'width:50px'));
101 $form->addInput(array('type'=>'combobox','name'=>'year','data'=>$years,'datakeys'=>array('id','name'),'value'=>$selectedYear,'onchange'=>'yearChange(this.value);'));
102 for ($i=0; $i < count($months); $i++) { 
103   $value = "";
104   if (array_key_exists($i+1, $monthsData)){
105     $value = $monthsData[$i+1];
106   }
107   $name = $months[$i];
108   $form->addInput(array('type'=>'text','name'=>$name,'maxlength'=>3,'value'=> $value,'style'=>'width:50px'));
109 }
110
111 $smarty->assign('months', $months);
112 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
113 $smarty->assign('title', $i18n->getKey('title.monthly_quotas'));
114 $smarty->assign('content_page_name', 'quotas.tpl');
115 $smarty->display('index.tpl');