timesheetController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. class timesheetController extends Staple_Controller
  3. {
  4. public function _start()
  5. {
  6. }
  7. public function index($year = null, $month = 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. //Set year and month variables if undefined.
  60. if($year == null)
  61. {
  62. $date = new DateTime();
  63. $year = $date->format('Y');
  64. }
  65. if($month == null)
  66. {
  67. $date = new DateTime();
  68. $month = $date->format('m');
  69. }
  70. //Load timesheet for user.
  71. $timesheet = new timesheetModel($year,$month);
  72. //Pass timesheet object to view
  73. $this->view->timesheet = $timesheet;
  74. }
  75. public function remove($id)
  76. {
  77. if($id != null)
  78. {
  79. //Confirm entry for user
  80. $timesheet = new timesheetModel();
  81. if($timesheet->exists($id))
  82. {
  83. //Delete Item
  84. if($timesheet->remove($id))
  85. {
  86. $this->view->message = "Entry removed.";
  87. }
  88. else
  89. {
  90. $this->view->message = "ERROR: Could not remove entry.";
  91. }
  92. }
  93. else
  94. {
  95. header("location: ".$this->_link(array('timesheet'))."");
  96. }
  97. }
  98. else
  99. {
  100. header("location: ".$this->_link(array('timesheet'))."");
  101. }
  102. }
  103. public function edit($id = null)
  104. {
  105. if($id != null)
  106. {
  107. $entry = new timeEntryModel($id);
  108. if($entry)
  109. {
  110. $form = new editTimeForm();
  111. $form->setAction($this->_link(array('timesheet','edit',$id)));
  112. //$form->addData();
  113. $this->view->form = $form;
  114. }
  115. else
  116. {
  117. echo "Entry loaded";
  118. //header("location: ".$this->_link(array('timesheet'))."");
  119. }
  120. }
  121. else
  122. {
  123. echo "ERROR: Unable to load entry";
  124. //header("location: ".$this->_link(array('timesheet'))."");
  125. }
  126. }
  127. }
  128. ?>