Merged some functions to help keeping things compact.
[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('ttTimeHelper');
33
34 // Access checks.
35 if (!ttAccessAllowed('manage_advanced_settings')) {
36   header('Location: access_denied.php');
37   exit();
38 }
39 if (!$user->isPluginEnabled('mq')) {
40   header('Location: feature_disabled.php');
41   exit();
42 }
43
44 // Start and end fallback values for the Year dropdown.
45 $yearStart = 2015;
46 $yearEnd = 2030;
47
48 // If values are defined in config - use them.
49 if (defined('MONTHLY_QUOTA_YEAR_START')){
50   $yearStart = (int)MONTHLY_QUOTA_YEAR_START;
51 }
52 if (defined('MONTHLY_QUOTA_YEAR_END')){
53   $yearEnd = (int)MONTHLY_QUOTA_YEAR_END;
54 }
55
56 // Create values for the Year dropdown.
57 $years = array();
58 for ($i = $yearStart; $i <= $yearEnd; $i++) {
59   array_push($years, array('id'=>$i,'name'=>$i));
60 }
61
62 // Get selected year from url parameter.
63 $selectedYear = $request->getParameter('year');
64 if (!$selectedYear or !ttValidInteger($selectedYear)){
65   $selectedYear = date('Y');
66 } else {
67   $selectedYear = (int) $selectedYear;
68 }
69
70 // Months are zero indexed.
71 $months = $i18n->monthNames;
72
73 $quota = new MonthlyQuota();
74
75 if ($request->isPost()){
76   // Validate user input.
77   if (false === ttTimeHelper::postedDurationToMinutes($request->getParameter('workdayHours')))
78     $err->add($i18n->get('error.field'), $i18n->get('form.quota.workday_hours'));
79
80   for ($i = 0; $i < count($months); $i++){
81     $val = $request->getParameter($months[$i]);
82     if (false === ttTimeHelper::postedDurationToMinutes($val, 44640/*24*60*31*/))
83       $err->add($i18n->get('error.field'), $months[$i]);
84   }
85   // Finished validating user input.
86
87   if ($err->no()) {
88
89     // Handle workday hours.
90     $workday_minutes = ttTimeHelper::postedDurationToMinutes($request->getParameter('workdayHours'));
91     if ($workday_minutes != $user->getWorkdayMinutes()) {
92       if (!$user->updateGroup(array('workday_minutes'=>$workday_minutes)))
93         $err->add($i18n->get('error.db'));
94     }
95
96     // Handle monthly quotas for a selected year.
97     $selectedYear = (int) $request->getParameter('year');
98     for ($i = 0; $i < count($months); $i++){
99       $quota_in_minutes = ttTimeHelper::postedDurationToMinutes($request->getParameter($months[$i]), 44640/*24*60*31*/);
100       if (!$quota->update($selectedYear, $i+1, $quota_in_minutes))
101         $err->add($i18n->get('error.db'));
102     }
103
104     if ($err->no()) {
105       // Redisplay the form.
106       header('Location: quotas.php?year='.$selectedYear);
107       exit();
108     }
109   }
110 }
111
112 // Get monthly quotas for the entire year.
113 $monthsData = $quota->get($selectedYear);
114 $workdayHours = ttTimeHelper::toAbsDuration($user->getWorkdayMinutes(), true);
115
116 $form = new Form('monthlyQuotasForm');
117 $form->addInput(array('type'=>'text', 'name'=>'workdayHours', 'value'=>$workdayHours, 'style'=>'width:60px'));
118 $form->addInput(array('type'=>'combobox','name'=>'year','data'=>$years,'datakeys'=>array('id','name'),'value'=>$selectedYear,'onchange'=>'yearChange(this.value);'));
119 for ($i=0; $i < count($months); $i++) { 
120   $value = "";
121   if (array_key_exists($i+1, $monthsData)){
122     $value = $monthsData[$i+1];
123     $value = ttTimeHelper::toAbsDuration($value, true);
124   }
125   $name = $months[$i];
126   $form->addInput(array('type'=>'text','name'=>$name,'maxlength'=>6,'value'=> $value,'style'=>'width:70px'));
127 }
128
129 $smarty->assign('months', $months);
130 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
131 $smarty->assign('title', $i18n->get('title.monthly_quotas'));
132 $smarty->assign('content_page_name', 'quotas.tpl');
133 $smarty->display('index.tpl');