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 (!ttAccessCheck(right_manage_team) || !$user->isPluginEnabled('mq')) {
 
  37   header('Location: access_denied.php');
 
  41 // Start and end fallback values for the Year dropdown.
 
  45 // If values are defined in config - use them.
 
  46 if (defined('MONTHLY_QUOTA_YEAR_START')){
 
  47   $yearStart = (int)MONTHLY_QUOTA_YEAR_START;
 
  49 if (defined('MONTHLY_QUOTA_YEAR_END')){
 
  50   $yearEnd = (int)MONTHLY_QUOTA_YEAR_END;
 
  53 // Create values for the Year dropdown.
 
  55 for ($i = $yearStart; $i <= $yearEnd; $i++) {
 
  56   array_push($years, array('id'=>$i,'name'=>$i));
 
  59 // Get selected year from url parameter.
 
  60 $selectedYear = $request->getParameter('year');
 
  61 if (!$selectedYear or !ttValidInteger($selectedYear)){
 
  62   $selectedYear = date('Y');
 
  64   $selectedYear = (int) $selectedYear;
 
  67 // Months are zero indexed.
 
  68 $months = $i18n->monthNames;
 
  70 $quota = new MonthlyQuota();
 
  72 if ($request->isPost()){
 
  73   // Validate user input.
 
  74   for ($i = 0; $i < count($months); $i++){
 
  75     $val = $request->getParameter($months[$i]);
 
  76     if (!ttTimeHelper::isValidQuota($val))
 
  77       $err->add($i18n->getKey('error.field'), $months[$i]);
 
  79   // Finished validating user input.
 
  83     // Handle workday hours.
 
  84     $hours = (int)$request->getParameter('workdayHours');
 
  85     if ($hours != $user->workday_hours) {
 
  86       if (!ttTeamHelper::update($user->team_id, array('name'=>$user->team,'workday_hours'=>$hours)))
 
  87         $err->add($i18n->getKey('error.db'));
 
  90     // Handle monthly quotas for a selected year.
 
  91     $selectedYear = (int) $request->getParameter('year');
 
  92     for ($i = 0; $i < count($months); $i++){
 
  93       if (!$quota->update($selectedYear, $i+1, $request->getParameter($months[$i])))
 
  94         $err->add($i18n->getKey('error.db'));
 
  98       // Redisplay the form.
 
  99       header('Location: quotas.php?year='.$selectedYear);
 
 105 // Get monthly quotas for the entire year.
 
 106 $monthsData = $quota->get($selectedYear);
 
 108 $form = new Form('monthlyQuotasForm');
 
 109 $form->addInput(array('type'=>'text', 'name'=>'workdayHours', 'value'=>$user->workday_hours, 'style'=>'width:50px'));
 
 110 $form->addInput(array('type'=>'combobox','name'=>'year','data'=>$years,'datakeys'=>array('id','name'),'value'=>$selectedYear,'onchange'=>'yearChange(this.value);'));
 
 111 for ($i=0; $i < count($months); $i++) { 
 
 113   if (array_key_exists($i+1, $monthsData)){
 
 114     $value = $monthsData[$i+1];
 
 115     $value = ttTimeHelper::toAbsDuration($value * 60, true);
 
 118   $form->addInput(array('type'=>'text','name'=>$name,'maxlength'=>6,'value'=> $value,'style'=>'width:70px'));
 
 121 $smarty->assign('months', $months);
 
 122 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
 
 123 $smarty->assign('title', $i18n->getKey('title.monthly_quotas'));
 
 124 $smarty->assign('content_page_name', 'quotas.tpl');
 
 125 $smarty->display('index.tpl');