Refactoring in progress of quotas.php file.
[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 // Fallback values for start and end year.
41 $yearStart = 2015;
42 $yearEnd = 2030;
43
44 // If values are defined in config - get 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 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   $res = false;
73   if ($_POST["quotas"]){
74     // User pressed the Save button under monthly quotas table.
75     $postedYear = $request->getParameter('years');
76     $selectedYear = intval($postedYear);
77     for ($i = 0; $i < count($months); $i++){
78       $res = $quota->update($postedYear, $i+1, $request->getParameter($months[$i]));
79     }
80   }
81   // if user saved required working hours for a day
82   if ($_POST["dailyHours"]){
83     $hours = $request->getParameter("dailyWorkingHours");
84     $teamDetails = ttTeamHelper::getTeamDetails($quota->usersTeamId);
85     $res = ttTeamHelper::update($quota->usersTeamId, array('name'=>$teamDetails['team_name'], 
86                                                            'workday_hours'=>$hours));
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 months where January is month 1, not 0
97 $monthsData = $quota->get($selectedYear);
98
99 $form = new Form('monthlyQuotaForm');
100
101 $form->addInput(array('type'=>'combobox', 'name'=>'years', '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 $form->addInput(array('type'=>'text', 'name'=>'dailyWorkingHours', 'value'=>$quota->getDailyWorkingHours(), 'style'=>'width:50px'));
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');