timesheetController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. $changeYearForm = new changeYearForm();
  75. $this->view->changeYearForm = $changeYearForm;
  76. }
  77. public function remove($id)
  78. {
  79. if($id != null)
  80. {
  81. //Confirm entry for user
  82. $timesheet = new timesheetModel();
  83. if($timesheet->exists($id))
  84. {
  85. //Delete Item
  86. if($timesheet->remove($id))
  87. {
  88. $this->view->message = "Entry removed.";
  89. }
  90. else
  91. {
  92. $this->view->message = "ERROR: Could not remove entry.";
  93. }
  94. }
  95. else
  96. {
  97. header("location: ".$this->_link(array('timesheet'))."");
  98. }
  99. }
  100. else
  101. {
  102. header("location: ".$this->_link(array('timesheet'))."");
  103. }
  104. }
  105. public function edit($id = null)
  106. {
  107. if($id != null)
  108. {
  109. $entry = new timeEntryModel($id);
  110. if($entry)
  111. {
  112. $form = new editTimeForm();
  113. $form->setAction($this->_link(array('timesheet','edit',$id)));
  114. //$form->addData();
  115. $this->view->form = $form;
  116. }
  117. else
  118. {
  119. echo "Entry loaded";
  120. //header("location: ".$this->_link(array('timesheet'))."");
  121. }
  122. }
  123. else
  124. {
  125. echo "ERROR: Unable to load entry";
  126. //header("location: ".$this->_link(array('timesheet'))."");
  127. }
  128. }
  129. }
  130. ?>