timesheetController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. class timesheetController extends Staple_Controller
  3. {
  4. public function _start()
  5. {
  6. }
  7. public function index($month = null, $year = null)
  8. {
  9. //Typecast variables
  10. $month = (int) $month;
  11. $year = (int) $year;
  12. //Build new insert form
  13. $form = new insertTimeForm();
  14. //Check for form submission
  15. if($form->wasSubmitted())
  16. {
  17. //Add submitted data to the form
  18. $form->addData($_POST);
  19. //Check form validation
  20. if($form->validate())
  21. {
  22. //Export form data into an array
  23. $data = $form->exportFormData();
  24. //Compare in Times and out Times.
  25. if(strtotime($data['inTime']) < strtotime($data['outTime']))
  26. {
  27. //Create a new entry object
  28. $entry = new timeEntryModel();
  29. $entry->setDate($data['date']);
  30. $entry->setInTime($data['inTime']);
  31. $entry->setOutTime($data['outTime']);
  32. $entry->setLessTime($data['lessTime']);
  33. $entry->setCodeId($data['code']);
  34. if($entry->save())
  35. {
  36. $this->view->message = "Entry saved.";
  37. }
  38. else
  39. {
  40. $this->view->message = "ERROR: Unable to save entry.";
  41. }
  42. }
  43. else
  44. {
  45. //Send form with error message back.
  46. $form->message = array("<b>'Time In'</b> entry cannot be before <b>'Time Out'</b> entry.");
  47. $this->view->insertTimeForm = $form;
  48. }
  49. }
  50. else
  51. {
  52. $this->view->insertTimeForm = $form;
  53. }
  54. }
  55. else
  56. {
  57. $this->view->insertTimeForm = $form;
  58. }
  59. //Load timesheet for user.
  60. $timesheet = new timesheetModel($month,$year);
  61. echo $timesheet->getStartDate()."<br>";
  62. echo $timesheet->getEndDate();
  63. //View
  64. $this->view->year = $timesheet->getYear();
  65. $this->view->nextYear = $timesheet->getNextYear();
  66. $this->view->previousYear = $timesheet->getPreviousYear();
  67. $this->view->month = $timesheet->getMonth();
  68. $this->view->previousMonth = $timesheet->getPreviousMonth();
  69. $this->view->nextMonth = $timesheet->getNextMonth();
  70. }
  71. public function remove($id)
  72. {
  73. if($id != null)
  74. {
  75. //Confirm entry for user
  76. $timesheet = new timesheetModel();
  77. if($timesheet->exists($id))
  78. {
  79. //Delete Item
  80. if($timesheet->remove($id))
  81. {
  82. $this->view->message = "Entry removed.";
  83. }
  84. else
  85. {
  86. $this->view->message = "ERROR: Could not remove entry.";
  87. }
  88. }
  89. else
  90. {
  91. header("location: ".$this->_link(array('timesheet'))."");
  92. }
  93. }
  94. else
  95. {
  96. header("location: ".$this->_link(array('timesheet'))."");
  97. }
  98. }
  99. public function edit($id = null)
  100. {
  101. if($id != null)
  102. {
  103. $entry = new timeEntryModel();
  104. if($entry->load($id))
  105. {
  106. $form = new editTimeForm();
  107. $form->setAction($this->_link(array('timesheet','edit',$id)));
  108. }
  109. else
  110. {
  111. echo "Entry loaded";
  112. //header("location: ".$this->_link(array('timesheet'))."");
  113. }
  114. }
  115. else
  116. {
  117. echo "ERROR: Unable to load entry";
  118. //header("location: ".$this->_link(array('timesheet'))."");
  119. }
  120. }
  121. }
  122. ?>