unlockModel.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. class unlockModel extends Staple_Model
  3. {
  4. private $db;
  5. private $username;
  6. private $id;
  7. private $startTime;
  8. private $endTime;
  9. private $userId;
  10. private $rangeDates;
  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 getStartTime()
  29. {
  30. $date = new DateTime();
  31. $date->setTimestamp($this->startTime);
  32. $startTime = $date->format('m/d/Y');
  33. return $startTime;
  34. }
  35. /**
  36. * @param mixed $startTime
  37. */
  38. public function setStartTime($startTime)
  39. {
  40. $this->startTime = strtotime($startTime);
  41. }
  42. /**
  43. * @return mixed
  44. */
  45. public function getEndTime()
  46. {
  47. $date = new DateTime();
  48. $date->setTimestamp($this->endTime);
  49. $endTime = $date->format('m/d/Y');
  50. return $endTime;
  51. }
  52. /**
  53. * @param mixed $endTime
  54. */
  55. public function setEndTime($endTime)
  56. {
  57. $this->endTime = strtotime($endTime);
  58. }
  59. /**
  60. * @return mixed
  61. */
  62. public function getUserId()
  63. {
  64. return $this->userId;
  65. }
  66. /**
  67. * @param mixed $userId
  68. */
  69. public function setUserId($userId)
  70. {
  71. $this->userId = $userId;
  72. }
  73. /**
  74. * @return mixed
  75. */
  76. public function getRangeDates()
  77. {
  78. return $this->rangeDates;
  79. }
  80. /**
  81. * @param mixed $rangeDates
  82. */
  83. public function setRangeDates($rangeDates)
  84. {
  85. $this->rangeDates = $rangeDates;
  86. }
  87. function __construct()
  88. {
  89. $this->db = Staple_DB::get();
  90. $auth = Staple_Auth::get();
  91. $this->username = $auth->getAuthId();
  92. }
  93. function load($uid)
  94. {
  95. $sql = "SELECT * type FROM overrideDates WHERE username = '".$this->db->real_escape_string($uid)."'";
  96. if($this->db->query($sql)->fetch_row() > 0)
  97. {
  98. $query = $this->db->query($sql);
  99. $result = $query->fetch_assoc();
  100. $this->setId($result['id']);
  101. $this->setStartTime($result['startTime']);
  102. $this->setEndTime($result['startTime']);
  103. }
  104. }
  105. function save()
  106. {
  107. if(isset($this->startTime) && !isset($this->id))
  108. {
  109. $sql = "
  110. INSERT INTO overrideDates (startTime, endTime, userId) VALUES ('".$this->db->real_escape_string($this->startTime)."','".$this->db->real_escape_string($this->endTime)."','".$this->db->real_escape_string($this->userId)."')
  111. ";
  112. if($this->db->query($sql))
  113. {
  114. $audit = new auditModel();
  115. $audit->setUserId($this->userId);
  116. $audit->setAction('Range unlock');
  117. $audit->setItem($this->username." unlocked dates from ".$this->getStartTime()." to ".$this->getEndTime());
  118. $audit->save();
  119. return True;
  120. }
  121. }
  122. }
  123. function unlock($id)
  124. {
  125. //get userid
  126. $sql = "
  127. SELECT userId FROM timeEntries WHERE id = '".$this->db->real_escape_string($id)."';
  128. ";
  129. if($this->db->query($sql)->num_rows > 0)
  130. {
  131. $query = $this->db->query($sql);
  132. $result = $query->fetch_assoc();
  133. $userId = $result['userId'];
  134. $user = new userModel();
  135. $user = $user->userInfo($userId);
  136. $userId = $user['id'];
  137. $batchId = $user['batchId'];
  138. //Check if it's for the same user.
  139. $currentUser = new userModel();
  140. if($currentUser->getId() != $userId)
  141. {
  142. $sql = "
  143. UPDATE timeEntries SET batchId = '".$this->db->real_escape_string($batchId)."' WHERE id = '".$this->db->real_escape_string($id)."'
  144. ";
  145. if($this->db->query($sql))
  146. {
  147. $audit = new auditModel();
  148. $audit->setUserId($userId);
  149. $audit->setAction('Single unlock');
  150. $audit->setItem($this->username." unlocked time entry ". $id);
  151. $audit->save();
  152. return true;
  153. }
  154. }
  155. }
  156. }
  157. function rangeDates($uid)
  158. {
  159. $sql = "
  160. SELECT * FROM overrideDates WHERE userId = '".$this->db->real_escape_string($uid)."'
  161. ";
  162. if($this->db->query($sql)->num_rows > 0)
  163. {
  164. $query = $this->db->query($sql);
  165. $rangeDays = array();
  166. $groups = array();
  167. $i=0;
  168. while($result = $query->fetch_assoc())
  169. {
  170. $date = new DateTime();
  171. $date->setTimestamp($result['startTime']);
  172. $date2 = new DateTime();
  173. $date2->setTimestamp($result['endTime']);
  174. $interval = $date->diff($date2);
  175. $days = $interval->days;
  176. $groups[$i]['days'] = $days;
  177. $groups[$i]['startTime'] = $result['startTime'];
  178. $groups[$i]['endTime'] = $result['endTime'];
  179. $i++;
  180. }
  181. $total=0;
  182. foreach($groups as $group)
  183. {
  184. $total += $group['days'];
  185. }
  186. foreach($groups as $group)
  187. {
  188. for($i=1;$i<=$total;$i++)
  189. {
  190. $rangeDays[$i]['startTime'] = $group['startTime'] + (86400 * $i);
  191. $rangeDays[$i]['endTime'] = $group['startTime'] + (86400 * $i) + 86400;
  192. $rangeDays[$i]['formattedStart'] = date('Y-m-d D', $group['startTime'] + (86400 * $i));
  193. $rangeDays[$i]['formattedEnd'] = date('Y-m-d D', $group['startTime'] + (86400 * $i) + 86400);
  194. }
  195. }
  196. return $rangeDays;
  197. }
  198. }
  199. }
  200. ?>