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');
 
  31 import('ttTimesheetHelper');
 
  34 if (!(ttAccessAllowed('track_own_time') || ttAccessAllowed('track_time'))) {
 
  35   header('Location: access_denied.php');
 
  38 if (!$user->isPluginEnabled('ts')) {
 
  39   header('Location: feature_disabled.php');
 
  42 // End of access checks.
 
  44 if ($request->isPost()) {
 
  45   $cl_name = trim($request->getParameter('timesheet_name'));
 
  46   $cl_client = $request->getParameter('client');
 
  47   $cl_project = $request->getParameter('project');
 
  48   $cl_start = $request->getParameter('start');
 
  49   $cl_finish = $request->getParameter('finish');
 
  50   $cl_comment = trim($request->getParameter('comment'));
 
  53 $user_id = $user->getUser();
 
  55 $form = new Form('timesheetForm');
 
  56 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'timesheet_name','style'=>'width: 250px;','value'=>$cl_name));
 
  58 // Dropdown for clients if the clients plugin is enabled.
 
  59 $showClient = $user->isPluginEnabled('cl');
 
  61   $clients = ttGroupHelper::getActiveClients();
 
  62   $form->addInput(array('type'=>'combobox','name'=>'client','style'=>'width: 250px;','data'=>$clients,'datakeys'=>array('id','name'),'value'=>$cl_client,'empty'=>array(''=>$i18n->get('dropdown.select'))));
 
  64 // Dropdown for projects.
 
  65 $showProject = MODE_PROJECTS == $user->getTrackingMode() || MODE_PROJECTS_AND_TASKS == $user->getTrackingMode();
 
  67   $projects = $user->getAssignedProjects();
 
  68   $form->addInput(array('type'=>'combobox','name'=>'project','style'=>'width: 250px;','data'=>$projects,'datakeys'=>array('id','name'),'value'=>$cl_project,'empty'=>array(''=>$i18n->get('dropdown.all'))));
 
  70 $form->addInput(array('type'=>'datefield','maxlength'=>'20','name'=>'start','value'=>$cl_start));
 
  71 $form->addInput(array('type'=>'datefield','maxlength'=>'20','name'=>'finish','value'=>$cl_finish));
 
  72 $form->addInput(array('type'=>'textarea','name'=>'comment','style'=>'width: 250px; height: 40px;','value'=>$cl_comment));
 
  73 $form->addInput(array('type'=>'submit','name'=>'btn_add','value'=>$i18n->get('button.add')));
 
  75 if ($request->isPost()) {
 
  76   // Validate user input.
 
  77   if (!ttValidString($cl_name)) $err->add($i18n->get('error.field'), $i18n->get('label.thing_name'));
 
  78   if (!ttValidDate($cl_start)) $err->add($i18n->get('error.field'), $i18n->get('label.start_date'));
 
  79   if (!ttValidDate($cl_finish)) $err->add($i18n->get('error.field'), $i18n->get('label.end_date'));
 
  80   if (!ttValidString($cl_comment, true)) $err->add($i18n->get('error.field'), $i18n->get('label.comment'));
 
  81   if ($err->no() && ttTimesheetHelper::getTimesheetByName($cl_name)) $err->add($i18n->get('error.object_exists'));
 
  82   $fields = array('user_id' => $user_id,
 
  84     'client_id' => $cl_client,
 
  85     'project_id' => $cl_project,
 
  86     'start_date' => $cl_start,
 
  87     'end_date' => $cl_finish,
 
  88     'comment' => $cl_comment);
 
  89   if ($err->no() && !ttTimesheetHelper::timesheetItemsExist($fields)) $err->add($i18n->get('error.no_records'));
 
  90   if ($err->no() && ttTimesheetHelper::overlaps($fields)) $err->add($i18n->get('error.overlap'));
 
  91   // Finished validating user input.
 
  94     if (ttTimesheetHelper::createTimesheet($fields)) {
 
  95       header('Location: timesheets.php');
 
  98       $err->add($i18n->get('error.db'));
 
 102 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
 
 103 $smarty->assign('onload', 'onLoad="document.timesheetForm.timesheet_name.focus()"');
 
 104 $smarty->assign('show_client', $showClient);
 
 105 $smarty->assign('show_project', $showProject);
 
 106 $smarty->assign('title', $i18n->get('title.add_timesheet'));
 
 107 $smarty->assign('content_page_name', 'timesheet_add.tpl');
 
 108 $smarty->display('index.tpl');