timesheetController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. //Check for unvalidated entries
  78. $i = 0;
  79. foreach($timesheet->getEntries() as $entry)
  80. {
  81. if($entry->batchId == $timesheet->getBatch())
  82. {
  83. $i++;
  84. }
  85. }
  86. if($i > 0)
  87. {
  88. $this->view->needsValidation = true;
  89. }
  90. else
  91. {
  92. $this->view->needsValidation = false;
  93. }
  94. $changeYearForm = new changeYearForm();
  95. $this->view->changeYearForm = $changeYearForm;
  96. }
  97. public function remove($id)
  98. {
  99. if($id != null)
  100. {
  101. //Confirm entry for user
  102. $timesheet = new timesheetModel();
  103. if($timesheet->exists($id))
  104. {
  105. //Delete Item
  106. if($timesheet->remove($id))
  107. {
  108. $this->view->message = "Entry removed.";
  109. }
  110. else
  111. {
  112. $this->view->message = "ERROR: Could not remove entry.";
  113. }
  114. }
  115. else
  116. {
  117. header("location: ".$this->_link(array('timesheet'))."");
  118. }
  119. }
  120. else
  121. {
  122. header("location: ".$this->_link(array('timesheet'))."");
  123. }
  124. }
  125. public function edit($id = null)
  126. {
  127. if($id != null)
  128. {
  129. $entry = new timeEntryModel($id);
  130. if($entry)
  131. {
  132. $form = new editTimeForm();
  133. $form->setAction($this->_link(array('timesheet','edit',$id)));
  134. //$form->addData();
  135. $this->view->form = $form;
  136. }
  137. else
  138. {
  139. echo "Entry loaded";
  140. //header("location: ".$this->_link(array('timesheet'))."");
  141. }
  142. }
  143. else
  144. {
  145. echo "ERROR: Unable to load entry";
  146. //header("location: ".$this->_link(array('timesheet'))."");
  147. }
  148. }
  149. public function changeyear()
  150. {
  151. $form = new changeYearForm();
  152. if($form->wasSubmitted())
  153. {
  154. $form->addData($_POST);
  155. if($form->validate())
  156. {
  157. $data = $form->exportFormData();
  158. header("location: ".$this->_link(array('timesheet',$data['year']))."");
  159. }
  160. else
  161. {
  162. header("location: ".$this->_link(array('timesheet'))."");
  163. }
  164. }
  165. else
  166. {
  167. header("location: ".$this->_link(array('timesheet'))."");
  168. }
  169. }
  170. public function validate($year, $month)
  171. {
  172. $timesheet = new timesheetModel($year,$month);
  173. //Get Current Batch ID
  174. $auth = Staple_Auth::get();
  175. $user = new userModel($auth->getAuthId());
  176. $batchId = $user->getBatchId();
  177. //Check for unvalidated entries
  178. $i = 0;
  179. foreach($timesheet->getEntries() as $entry)
  180. {
  181. if($entry->batchId == $timesheet->getBatch())
  182. {
  183. $i++;
  184. }
  185. }
  186. if($i > 0)
  187. {
  188. $this->view->timesheet = $timesheet;
  189. $form = new validateTimeSheetForm();
  190. $form->setAction($this->_link(array('timesheet','validate',$timesheet->getCurrentYear(),$timesheet->getCurrentMonth())));
  191. if($form->wasSubmitted())
  192. {
  193. $timesheet->validate($batchId);
  194. $this->view->needsValidation = true;
  195. }
  196. else
  197. {
  198. $this->view->form = $form;
  199. $this->view->needsValidation = false;
  200. }
  201. }
  202. else
  203. {
  204. $this->view->needsValidation = false;
  205. $this->view->timesheet = array();
  206. }
  207. }
  208. }
  209. ?>