timesheetModel.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. class timesheetModel extends Staple_Model
  3. {
  4. private $db;
  5. private $id;
  6. private $userId;
  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 getUserId()
  29. {
  30. return $this->userId;
  31. }
  32. /**
  33. * @param mixed $userId
  34. */
  35. public function setUserId($userId)
  36. {
  37. $this->userId = $userId;
  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()
  96. {
  97. $this->db = Staple_DB::get();
  98. }
  99. function load()
  100. {
  101. $authId = Staple_Auth::get()->getAuthId();
  102. //Get User ID.
  103. if(isset($authId))
  104. {
  105. $sql = "SELECT id FROM accounts WHERE username = '".$this->db->real_escape_string($authId)."'";
  106. if($this->db->query($sql)->fetch_row() > 0)
  107. {
  108. $query = $this->db->query($sql);
  109. $result = $query->fetch_assoc();
  110. $this->setUserId($result['id']);
  111. }
  112. if(isset($this->userId))
  113. {
  114. $sql = "SELECT * FROM timeEntries WHERE userId = '" . $this->db->real_escape_string($this->userId) . "' ORDER BY inTime ASC";
  115. if ($this->db->query($sql)->fetch_row() > 0)
  116. {
  117. $query = $this->db->query($sql);
  118. $data = array();
  119. while ($row = $query->fetch_assoc())
  120. {
  121. $data[] = $row;
  122. }
  123. foreach($data as $entry)
  124. {
  125. $code = new codeModel();
  126. $code->load($entry['codeId']);
  127. $codeName = $code->getName();
  128. $data2['id'] = $entry['id'];
  129. //Set Date Object
  130. $date = new DateTime();
  131. $date->setTimestamp($entry['inTime']);
  132. //Date
  133. $data2['date']['ymd'] = $date->format("Y-m-d");
  134. //Formatted Date
  135. $data2['date']['formatted'] = $date->format("F jS, Y");
  136. //Day
  137. $data2['date']['dayOfWeek'] = $date->format("l");
  138. //Day Abbreviated
  139. $data2['date']['dayShort'] = $date->format("D.");
  140. //MonthYear
  141. $data2['date']['my'] = $date->format("m/y");
  142. //In Time
  143. $data2['rawInTime'] = $date->format("g:i A");
  144. $data2['roundedInTime'] = $this->nearestQuarterHour($date->format("g:i A"));
  145. //Out Time
  146. $date->setTimestamp($entry['outTime']);
  147. $data2['rawOutTime'] = $date->format("g:i A");
  148. $data2['roundedOutTime'] = $this->nearestQuarterHour($date->format("g:i A"));
  149. //Less Time
  150. $data2['lessTime'] = $entry['lessTime'];
  151. //Total Worked Time
  152. $dateTime1 = new DateTime($data2['roundedInTime']);
  153. $dateTime2 = new DateTime($data2['roundedOutTime']);
  154. $interval = $dateTime1->diff($dateTime2);
  155. $data2['timeWorked'] = $interval->h.":".$interval->i;
  156. $data2['timeWorkedDec'] = $this->time_to_decimal($interval->h.":".$interval->i);
  157. $data2['code'] = $codeName;
  158. $data3[] = $data2;
  159. }
  160. return $data3;
  161. }
  162. else
  163. {
  164. return array();
  165. }
  166. }
  167. }
  168. }
  169. function nearestQuarterHour($time)
  170. {
  171. $time = strtotime($time);
  172. $round = 15*60;
  173. $rounded = round($time/$round)*$round;
  174. return date("g:i A", $rounded);
  175. }
  176. function time_to_decimal($time)
  177. {
  178. $timeArr = explode(':', $time);
  179. $hours = $timeArr[0]*1;
  180. $minutes = $timeArr[1]/60;
  181. $dec = $hours + $minutes;
  182. return round($dec,2);
  183. }
  184. function save()
  185. {
  186. $id = $this->getId();
  187. if($id == NULL)
  188. {
  189. //Insert new item
  190. $sql = "INSERT INTO posts (title, content, expires, post)
  191. VALUES (
  192. '".$this->db->real_escape_string($title)."',
  193. '".$this->db->real_escape_string($content)."',
  194. '".$this->db->real_escape_string($expire)."',
  195. '".$this->db->real_escape_string($post)."'
  196. )";
  197. }
  198. else
  199. {
  200. //Update item
  201. $sql = "UPDATE posts SET
  202. title='".$this->db->real_escape_string($title)."',
  203. content='".$this->db->real_escape_string($content)."',
  204. expires='".$this->db->real_escape_string($expire)."',
  205. post='".$this->db->real_escape_string($post)."' WHERE id='".$this->db->real_escape_string($id)."'
  206. ";
  207. }
  208. $query = $this->db->query($sql);
  209. if($query === true)
  210. {
  211. return true;
  212. }
  213. else
  214. {
  215. return false;
  216. }
  217. }
  218. }
  219. ?>