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.
11 // | There are only two ways to violate the license:
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).
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).
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
24 // +----------------------------------------------------------------------+
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
29 require_once('initialize.php');
30 require_once('plugins/MonthlyQuota.class.php');
32 import('ttTeamHelper');
33 import('ttTimeHelper');
36 if (!ttAccessAllowed('manage_advanced_settings')) {
37 header('Location: access_denied.php');
40 if (!$user->isPluginEnabled('mq')) {
41 header('Location: feature_disabled.php');
45 // Start and end fallback values for the Year dropdown.
49 // If values are defined in config - use them.
50 if (defined('MONTHLY_QUOTA_YEAR_START')){
51 $yearStart = (int)MONTHLY_QUOTA_YEAR_START;
53 if (defined('MONTHLY_QUOTA_YEAR_END')){
54 $yearEnd = (int)MONTHLY_QUOTA_YEAR_END;
57 // Create values for the Year dropdown.
59 for ($i = $yearStart; $i <= $yearEnd; $i++) {
60 array_push($years, array('id'=>$i,'name'=>$i));
63 // Get selected year from url parameter.
64 $selectedYear = $request->getParameter('year');
65 if (!$selectedYear or !ttValidInteger($selectedYear)){
66 $selectedYear = date('Y');
68 $selectedYear = (int) $selectedYear;
71 // Months are zero indexed.
72 $months = $i18n->monthNames;
74 $quota = new MonthlyQuota();
76 if ($request->isPost()){
77 // Validate user input.
78 if (false === ttTimeHelper::postedDurationToMinutes($request->getParameter('workdayHours')))
79 $err->add($i18n->get('error.field'), $i18n->get('form.quota.workday_hours'));
81 for ($i = 0; $i < count($months); $i++){
82 $val = $request->getParameter($months[$i]);
83 if (false === ttTimeHelper::postedDurationToMinutes($val, 44640/*24*60*31*/))
84 $err->add($i18n->get('error.field'), $months[$i]);
86 // Finished validating user input.
90 // Handle workday hours.
91 $workday_minutes = ttTimeHelper::postedDurationToMinutes($request->getParameter('workdayHours'));
92 if ($workday_minutes != $user->workday_minutes) {
93 if (!$user->updateGroup(array('workday_minutes'=>$workday_minutes)))
94 $err->add($i18n->get('error.db'));
97 // Handle monthly quotas for a selected year.
98 $selectedYear = (int) $request->getParameter('year');
99 for ($i = 0; $i < count($months); $i++){
100 $quota_in_minutes = ttTimeHelper::postedDurationToMinutes($request->getParameter($months[$i]), 44640/*24*60*31*/);
101 if (!$quota->update($selectedYear, $i+1, $quota_in_minutes))
102 $err->add($i18n->get('error.db'));
106 // Redisplay the form.
107 header('Location: quotas.php?year='.$selectedYear);
113 // Get monthly quotas for the entire year.
114 $monthsData = $quota->get($selectedYear);
115 $workdayHours = ttTimeHelper::toAbsDuration($user->workday_minutes, true);
117 $form = new Form('monthlyQuotasForm');
118 $form->addInput(array('type'=>'text', 'name'=>'workdayHours', 'value'=>$workdayHours, 'style'=>'width:60px'));
119 $form->addInput(array('type'=>'combobox','name'=>'year','data'=>$years,'datakeys'=>array('id','name'),'value'=>$selectedYear,'onchange'=>'yearChange(this.value);'));
120 for ($i=0; $i < count($months); $i++) {
122 if (array_key_exists($i+1, $monthsData)){
123 $value = $monthsData[$i+1];
124 $value = ttTimeHelper::toAbsDuration($value, true);
127 $form->addInput(array('type'=>'text','name'=>$name,'maxlength'=>6,'value'=> $value,'style'=>'width:70px'));
130 $smarty->assign('months', $months);
131 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
132 $smarty->assign('title', $i18n->get('title.monthly_quotas'));
133 $smarty->assign('content_page_name', 'quotas.tpl');
134 $smarty->display('index.tpl');