auditModel.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. class auditModel extends Staple_Model
  3. {
  4. private $db;
  5. private $timestamp;
  6. private $action;
  7. private $userId;
  8. private $group;
  9. private $item;
  10. private $pager;
  11. /**
  12. * @return mixed
  13. */
  14. public function getTimestamp()
  15. {
  16. return $this->timestamp;
  17. }
  18. /**
  19. * @return mixed
  20. */
  21. public function getAction()
  22. {
  23. return $this->action;
  24. }
  25. /**
  26. * @param mixed $action
  27. */
  28. public function setAction($action)
  29. {
  30. $this->action = $action;
  31. }
  32. /**
  33. * @return mixed
  34. */
  35. public function getUserId()
  36. {
  37. return $this->userId;
  38. }
  39. /**
  40. * @param mixed $userId
  41. */
  42. public function setUserId($userId)
  43. {
  44. $this->userId = $userId;
  45. }
  46. /**
  47. * @return mixed
  48. */
  49. public function getGroup()
  50. {
  51. return $this->group;
  52. }
  53. /**
  54. * @param mixed $group
  55. */
  56. public function setGroup($group)
  57. {
  58. $this->group = $group;
  59. }
  60. /**
  61. * @return mixed
  62. */
  63. public function getItem()
  64. {
  65. return $this->item;
  66. }
  67. /**
  68. * @param mixed $item
  69. */
  70. public function setItem($item)
  71. {
  72. $this->item = $item;
  73. }
  74. /**
  75. * @return mixed
  76. */
  77. public function getPager()
  78. {
  79. return $this->pager;
  80. }
  81. /**
  82. * @param mixed $pager
  83. */
  84. public function setPager($pager)
  85. {
  86. $this->pager = $pager;
  87. }
  88. function __construct()
  89. {
  90. $this->db = Staple_DB::get();
  91. }
  92. function save()
  93. {
  94. if(isset($this->userId) && isset($this->action) && isset($this->item))
  95. {
  96. $sql = "
  97. INSERT INTO audit (action, userId, item) VALUES ('".$this->db->real_escape_string($this->getAction())."','".$this->db->real_escape_string($this->getUserId())."','".$this->db->real_escape_string($this->getItem())."');
  98. ";
  99. if($this->db->query($sql))
  100. {
  101. return true;
  102. }
  103. }
  104. }
  105. function getAll($uid = null,$page,$items)
  106. {
  107. $pager = new Staple_Pager();
  108. //Get total rows
  109. if($uid == null)
  110. {
  111. $sql = "SELECT COUNT(id) as count FROM audit";
  112. }
  113. else
  114. {
  115. $sql = "SELECT COUNT(id) as count FROM audit WHERE userId = '".$this->db->real_escape_string($uid)."'";
  116. }
  117. $result = $this->db->query($sql)->fetch_assoc();
  118. $total = $result['count'];
  119. $pager->setTotal($total);
  120. $pager->setItemsPerPage($items);
  121. $pager->setPage($page);
  122. if($uid == null)
  123. {
  124. $sql = "
  125. SELECT * FROM audit ORDER BY timestamp ASC LIMIT ".$pager->getStartingItem().", ".$pager->getItemsPerPage()."
  126. ";
  127. }
  128. else
  129. {
  130. $sql = "
  131. SELECT * FROM audit WHERE userId = '".$this->db->real_escape_string($uid)."' ORDER BY timestamp ASC LIMIT ".$pager->getStartingItem().", ".$pager->getItemsPerPage()."
  132. ";
  133. }
  134. $this->pager = $pager;
  135. if($this->db->query($sql)->num_rows > 0)
  136. {
  137. $query = $this->db->query($sql);
  138. $data = array();
  139. $i = 0;
  140. while($result = $query->fetch_assoc())
  141. {
  142. $data[$i]['timestamp'] = $result['timestamp'];
  143. $account = new userModel();
  144. $data[$i]['account'] = $account->userInfo($result['userId']);
  145. $data[$i]['action'] = $result['action'];
  146. $data[$i]['item'] = $result['item'];
  147. $i++;
  148. }
  149. return $data;
  150. }
  151. else
  152. {
  153. return array();
  154. }
  155. }
  156. }
  157. ?>