wasSubmitted())
{
//Add submitted data to the form
$form->addData($_POST);
//Check form validation
if($form->validate())
{
//Export form data into an array
$data = $form->exportFormData();
//Check if dates are within the current pay period.
$startMonth = date('m',strtotime('last month'));
if($startMonth == 1)
{
$startYear = date('Y',strtotime('last year'));
}
else
{
$startYear = date('Y');
}
$endMonth = date('m');
$endYear = date('Y');
$startDate= strtotime($startMonth.'/26/'.$startYear);
$endDate = strtotime($endMonth.'/25/'.$endYear);
$userDate = strtotime($data['date']);
//Date is within pay period
if($userDate >= $startDate && $userDate <= $endDate)
{
//Compare in Times and out Times.
if(strtotime($data['inTime']) < strtotime($data['outTime']))
{
//Create a new entry object and set properties
$entry = new timeEntryModel();
$entry->setDate($data['date']);
$entry->setInTime($data['inTime']);
$entry->setOutTime($data['outTime']);
$entry->setLessTime($data['lessTime']);
$entry->setCodeId($data['code']);
//Save entry data to table.
if($entry->save())
{
//Return a new time form with success message
$form = new insertTimeForm();
$form->successMessage = array(" Entry saved for ".$data['date']."");
$this->view->insertTimeForm = $form;
}
else
{
//Return the same form with a warning message
$message = " Cannot insert overlapping time entries. Please add a new entry or edit an already existing one.";
$form->errorMessage = array($message);
$this->view->insertTimeForm = $form;
}
}
else
{
//Return the same form with error message.
$form->errorMessage = array("'Time In' entry cannot be before 'Time Out' entry.");
$this->view->insertTimeForm = $form;
}
}
else
{
//Return the same form with error message.
$form->errorMessage = array(" You may only submit time for the current date period.");
$this->view->insertTimeForm = $form;
}
}
else
{
//Return form with invalid data.
$this->view->insertTimeForm = $form;
}
}
else
{
//Return form
$this->view->insertTimeForm = $form;
}
//Set year and month variables if undefined.
if($year == null)
{
$date = new DateTime();
$year = $date->format('Y');
}
if($month == null)
{
$date = new DateTime();
$month = $date->format('m');
}
//Load timesheet for user.
$timesheet = new timesheetModel($year,$month);
//Pass timesheet object to view
$this->view->timesheet = $timesheet;
//Check for unvalidated entries
$i = 0;
foreach($timesheet->getEntries() as $entry)
{
if($entry->batchId == $timesheet->getBatch())
{
$i++;
}
}
if($i > 0)
{
$this->view->needsValidation = true;
}
else
{
$this->view->needsValidation = false;
}
$changeYearForm = new changeYearForm();
$this->view->changeYearForm = $changeYearForm;
}
public function remove($id)
{
if($id != null)
{
//Confirm entry for user
$timesheet = new timesheetModel();
if($timesheet->exists($id))
{
//Delete Item
if($timesheet->remove($id))
{
$this->view->message = "Entry removed.";
}
else
{
$this->view->message = "ERROR: Could not remove entry.";
}
}
else
{
header("location: ".$this->_link(array('timesheet'))."");
}
}
else
{
header("location: ".$this->_link(array('timesheet'))."");
}
}
public function edit($id = null)
{
if($id != null)
{
$entry = new timeEntryModel($id);
print_r($entry);
}
else
{
echo "ERROR: Unable to load entry";
//header("location: ".$this->_link(array('timesheet'))."");
}
}
public function changeyear()
{
$form = new changeYearForm();
if($form->wasSubmitted())
{
$form->addData($_POST);
if($form->validate())
{
$data = $form->exportFormData();
header("location: ".$this->_link(array('timesheet',$data['year']))."");
}
else
{
header("location: ".$this->_link(array('timesheet'))."");
}
}
else
{
header("location: ".$this->_link(array('timesheet'))."");
}
}
public function validate($year, $month)
{
$timesheet = new timesheetModel($year,$month);
//Get Current Batch ID
$auth = Staple_Auth::get();
$user = new userModel($auth->getAuthId());
$batchId = $user->getBatchId();
//Check for unvalidated entries
$i = 0;
foreach($timesheet->getEntries() as $entry)
{
if($entry->batchId == $timesheet->getBatch())
{
$i++;
}
}
if($i > 0)
{
$this->view->timesheet = $timesheet;
$form = new validateTimeSheetForm();
$form->setAction($this->_link(array('timesheet','validate',$timesheet->getCurrentYear(),$timesheet->getCurrentMonth())));
if($form->wasSubmitted())
{
$timesheet->validate($batchId);
header("location:".$this->_link(array('timesheet'))."");
}
else
{
$this->view->form = $form;
$this->view->needsValidation = false;
}
}
else
{
$this->view->needsValidation = false;
$this->view->timesheet = array();
}
}
}
?>