timeEntryModel.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. class timeEntryModel extends Staple_Model
  3. {
  4. private $db;
  5. private $id;
  6. private $date;
  7. private $inTime;
  8. private $outTime;
  9. private $lessTime;
  10. private $codeId;
  11. /**
  12. * @return mixed
  13. */
  14. public function getId()
  15. {
  16. return $this->id;
  17. }
  18. /**
  19. * @param mixed $id
  20. */
  21. public function setId($id)
  22. {
  23. $this->id = $id;
  24. }
  25. /**
  26. * @return mixed
  27. */
  28. public function getDate()
  29. {
  30. return $this->date;
  31. }
  32. /**
  33. * @param mixed $date
  34. */
  35. public function setDate($date)
  36. {
  37. $this->date = $date;
  38. }
  39. /**
  40. * @return mixed
  41. */
  42. public function getInTime()
  43. {
  44. return $this->inTime;
  45. }
  46. /**
  47. * @param mixed $inTime
  48. */
  49. public function setInTime($inTime)
  50. {
  51. $this->inTime = $inTime;
  52. }
  53. /**
  54. * @return mixed
  55. */
  56. public function getOutTime()
  57. {
  58. return $this->outTime;
  59. }
  60. /**
  61. * @param mixed $outTime
  62. */
  63. public function setOutTime($outTime)
  64. {
  65. $this->outTime = $outTime;
  66. }
  67. /**
  68. * @return mixed
  69. */
  70. public function getLessTime()
  71. {
  72. return $this->lessTime;
  73. }
  74. /**
  75. * @param mixed $lessTime
  76. */
  77. public function setLessTime($lessTime)
  78. {
  79. $this->lessTime = $lessTime;
  80. }
  81. /**
  82. * @return mixed
  83. */
  84. public function getCodeId()
  85. {
  86. return $this->codeId;
  87. }
  88. /**
  89. * @param mixed $codeId
  90. */
  91. public function setCodeId($codeId)
  92. {
  93. $this->codeId = $codeId;
  94. }
  95. function __construct($id = null)
  96. {
  97. $this->db = Staple_DB::get();
  98. if($id !== null)
  99. {
  100. $sql = "SELECT * FROM timeEntries WHERE id = '".$this->db->real_escape_string($id)."'";
  101. if($this->db->query($sql)->fetch_row() > 0)
  102. {
  103. $query = $this->db->query($sql);
  104. $result = $query->fetch_assoc();
  105. $this->setDate(date("m/d/Y",$result['inTime']));
  106. $this->setInTime(date("h:i A",$result['inTime']));
  107. $this->setOutTime(date("h:i A",$result['outTime']));
  108. $this->setLessTime($result['lessTime']);
  109. $this->setCodeId($result['codeId']);
  110. }
  111. }
  112. }
  113. function remove($id)
  114. {
  115. $this->db = Staple_DB::get();
  116. if($id !== null)
  117. {
  118. $auth = Staple_Auth::get();
  119. $user = new userModel($auth->getAuthId());
  120. $userId = $user->getId();
  121. $sql = "DELETE FROM timeEntries WHERE id = '".$this->db->real_escape_string($id)."' AND userId = '".$this->db->real_escape_string($userId)."'";
  122. if($this->db->query($sql))
  123. {
  124. return true;
  125. }
  126. }
  127. }
  128. function save()
  129. {
  130. $this->db = Staple_DB::get();
  131. $auth = Staple_Auth::get();
  132. $user = new userModel($auth->getAuthId());
  133. $userId = $user->getId();
  134. $inTime = strtotime($this->getDate()." ".$this->getInTime());
  135. $outTime = strtotime($this->getDate()." ".$this->getOutTime());
  136. if($this->getId() == NULL)
  137. {
  138. //Insert new item
  139. $sql = "INSERT INTO timeEntries (userId, inTime, outTime, lessTime, codeId)
  140. VALUES (
  141. '".$this->db->real_escape_string($userId)."',
  142. '".$this->db->real_escape_string($inTime)."',
  143. '".$this->db->real_escape_string($outTime)."',
  144. '".$this->db->real_escape_string($this->getLessTime())."',
  145. '".$this->db->real_escape_string($this->getCodeId())."'
  146. )";
  147. }
  148. else
  149. {
  150. //Update item
  151. $sql = "UPDATE timeEntries SET
  152. userId='".$this->db->real_escape_string($userId)."',
  153. inTime='".$this->db->real_escape_string($inTime)."',
  154. outTime='".$this->db->real_escape_string($outTime)."',
  155. lessTime='".$this->db->real_escape_string($this->getLessTime())."',
  156. codeId='".$this->db->real_escape_string($this->getCodeId())."'
  157. WHERE id='".$this->db->real_escape_string($this->getId())."'
  158. ";
  159. }
  160. $query = $this->db->query($sql);
  161. if($query === true)
  162. {
  163. return true;
  164. }
  165. }
  166. }
  167. ?>