timesheetController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. $form = new insertTimeForm();
  37. $form->successMessage = array("<i class=\"fa fa-check\"></i> Entry saved for ".$data['date']."");
  38. $this->view->insertTimeForm = $form;
  39. }
  40. else
  41. {
  42. $message = $entry->save()->getMessage();
  43. $form->errorMessage = array($message);
  44. $this->view->insertTimeForm = $form;
  45. }
  46. }
  47. else
  48. {
  49. //Send form with error message back.
  50. $form->message = array("<b>'Time In'</b> entry cannot be before <b>'Time Out'</b> entry.");
  51. $this->view->insertTimeForm = $form;
  52. }
  53. }
  54. else
  55. {
  56. $this->view->insertTimeForm = $form;
  57. }
  58. }
  59. else
  60. {
  61. $this->view->insertTimeForm = $form;
  62. }
  63. //Set year and month variables if undefined.
  64. if($year == null)
  65. {
  66. $date = new DateTime();
  67. $year = $date->format('Y');
  68. }
  69. if($month == null)
  70. {
  71. $date = new DateTime();
  72. $month = $date->format('m');
  73. }
  74. //Load timesheet for user.
  75. $timesheet = new timesheetModel($year,$month);
  76. //Pass timesheet object to view
  77. $this->view->timesheet = $timesheet;
  78. //Check for unvalidated entries
  79. $i = 0;
  80. foreach($timesheet->getEntries() as $entry)
  81. {
  82. if($entry->batchId == $timesheet->getBatch())
  83. {
  84. $i++;
  85. }
  86. }
  87. if($i > 0)
  88. {
  89. $this->view->needsValidation = true;
  90. }
  91. else
  92. {
  93. $this->view->needsValidation = false;
  94. }
  95. $changeYearForm = new changeYearForm();
  96. $this->view->changeYearForm = $changeYearForm;
  97. }
  98. public function remove($id)
  99. {
  100. if($id != null)
  101. {
  102. //Confirm entry for user
  103. $timesheet = new timesheetModel();
  104. if($timesheet->exists($id))
  105. {
  106. //Delete Item
  107. if($timesheet->remove($id))
  108. {
  109. $this->view->message = "Entry removed.";
  110. }
  111. else
  112. {
  113. $this->view->message = "ERROR: Could not remove entry.";
  114. }
  115. }
  116. else
  117. {
  118. header("location: ".$this->_link(array('timesheet'))."");
  119. }
  120. }
  121. else
  122. {
  123. header("location: ".$this->_link(array('timesheet'))."");
  124. }
  125. }
  126. public function edit($id = null)
  127. {
  128. if($id != null)
  129. {
  130. $entry = new timeEntryModel($id);
  131. if($entry)
  132. {
  133. $form = new editTimeForm();
  134. $form->setAction($this->_link(array('timesheet','edit',$id)));
  135. //$form->addData();
  136. $this->view->form = $form;
  137. }
  138. else
  139. {
  140. echo "Entry loaded";
  141. //header("location: ".$this->_link(array('timesheet'))."");
  142. }
  143. }
  144. else
  145. {
  146. echo "ERROR: Unable to load entry";
  147. //header("location: ".$this->_link(array('timesheet'))."");
  148. }
  149. }
  150. public function changeyear()
  151. {
  152. $form = new changeYearForm();
  153. if($form->wasSubmitted())
  154. {
  155. $form->addData($_POST);
  156. if($form->validate())
  157. {
  158. $data = $form->exportFormData();
  159. header("location: ".$this->_link(array('timesheet',$data['year']))."");
  160. }
  161. else
  162. {
  163. header("location: ".$this->_link(array('timesheet'))."");
  164. }
  165. }
  166. else
  167. {
  168. header("location: ".$this->_link(array('timesheet'))."");
  169. }
  170. }
  171. public function validate($year, $month)
  172. {
  173. $timesheet = new timesheetModel($year,$month);
  174. //Get Current Batch ID
  175. $auth = Staple_Auth::get();
  176. $user = new userModel($auth->getAuthId());
  177. $batchId = $user->getBatchId();
  178. //Check for unvalidated entries
  179. $i = 0;
  180. foreach($timesheet->getEntries() as $entry)
  181. {
  182. if($entry->batchId == $timesheet->getBatch())
  183. {
  184. $i++;
  185. }
  186. }
  187. if($i > 0)
  188. {
  189. $this->view->timesheet = $timesheet;
  190. $form = new validateTimeSheetForm();
  191. $form->setAction($this->_link(array('timesheet','validate',$timesheet->getCurrentYear(),$timesheet->getCurrentMonth())));
  192. if($form->wasSubmitted())
  193. {
  194. $timesheet->validate($batchId);
  195. $this->view->needsValidation = true;
  196. }
  197. else
  198. {
  199. $this->view->form = $form;
  200. $this->view->needsValidation = false;
  201. }
  202. }
  203. else
  204. {
  205. $this->view->needsValidation = false;
  206. $this->view->timesheet = array();
  207. }
  208. }
  209. }
  210. ?>