timesheetController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. $form = new insertTimeForm();
  38. $this->view->insertTimeForm = $form;
  39. }
  40. else
  41. {
  42. $this->view->message = "ERROR: Unable to save entry.";
  43. $this->view->insertTimeForm = $form;
  44. }
  45. }
  46. else
  47. {
  48. //Send form with error message back.
  49. $form->message = array("<b>'Time In'</b> entry cannot be before <b>'Time Out'</b> entry.");
  50. $this->view->insertTimeForm = $form;
  51. }
  52. }
  53. else
  54. {
  55. $this->view->insertTimeForm = $form;
  56. }
  57. }
  58. else
  59. {
  60. $this->view->insertTimeForm = $form;
  61. }
  62. //Set year and month variables if undefined.
  63. if($year == null)
  64. {
  65. $date = new DateTime();
  66. $year = $date->format('Y');
  67. }
  68. if($month == null)
  69. {
  70. $date = new DateTime();
  71. $month = $date->format('m');
  72. }
  73. //Load timesheet for user.
  74. $timesheet = new timesheetModel($year,$month);
  75. //Pass timesheet object to view
  76. $this->view->timesheet = $timesheet;
  77. $changeYearForm = new changeYearForm();
  78. $this->view->changeYearForm = $changeYearForm;
  79. }
  80. public function remove($id)
  81. {
  82. if($id != null)
  83. {
  84. //Confirm entry for user
  85. $timesheet = new timesheetModel();
  86. if($timesheet->exists($id))
  87. {
  88. //Delete Item
  89. if($timesheet->remove($id))
  90. {
  91. $this->view->message = "Entry removed.";
  92. }
  93. else
  94. {
  95. $this->view->message = "ERROR: Could not remove entry.";
  96. }
  97. }
  98. else
  99. {
  100. header("location: ".$this->_link(array('timesheet'))."");
  101. }
  102. }
  103. else
  104. {
  105. header("location: ".$this->_link(array('timesheet'))."");
  106. }
  107. }
  108. public function edit($id = null)
  109. {
  110. if($id != null)
  111. {
  112. $entry = new timeEntryModel($id);
  113. if($entry)
  114. {
  115. $form = new editTimeForm();
  116. $form->setAction($this->_link(array('timesheet','edit',$id)));
  117. //$form->addData();
  118. $this->view->form = $form;
  119. }
  120. else
  121. {
  122. echo "Entry loaded";
  123. //header("location: ".$this->_link(array('timesheet'))."");
  124. }
  125. }
  126. else
  127. {
  128. echo "ERROR: Unable to load entry";
  129. //header("location: ".$this->_link(array('timesheet'))."");
  130. }
  131. }
  132. public function changeyear()
  133. {
  134. $form = new changeYearForm();
  135. if($form->wasSubmitted())
  136. {
  137. $form->addData($_POST);
  138. if($form->validate())
  139. {
  140. $data = $form->exportFormData();
  141. header("location: ".$this->_link(array('timesheet',$data['year']))."");
  142. }
  143. else
  144. {
  145. header("location: ".$this->_link(array('timesheet'))."");
  146. }
  147. }
  148. else
  149. {
  150. header("location: ".$this->_link(array('timesheet'))."");
  151. }
  152. }
  153. public function validate($year, $month)
  154. {
  155. $timesheet = new timesheetModel($year,$month);
  156. echo $timesheet->getStartDateTimeString();
  157. //Get Current Batch ID
  158. $auth = Staple_Auth::get();
  159. $user = new userModel($auth->getAuthId());
  160. $batchId = $user->getBatchId();
  161. $this->view->timesheet = $timesheet;
  162. }
  163. }
  164. ?>