auditModel.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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($page,$items)
  106. {
  107. $pager = new Staple_Pager();
  108. //Get total rows
  109. $sql = "SELECT COUNT(id) as count FROM audit";
  110. $result = $this->db->query($sql)->fetch_assoc();
  111. $total = $result['count'];
  112. $pager->setTotal($total);
  113. $pager->setItemsPerPage($items);
  114. $pager->setPage($page);
  115. $sql = "
  116. SELECT * FROM audit WHERE 1 ORDER BY timestamp ASC LIMIT ".$pager->getStartingItem().", ".$pager->getItemsPerPage()."
  117. ";
  118. $this->pager = $pager;
  119. if($this->db->query($sql)->num_rows > 0)
  120. {
  121. $query = $this->db->query($sql);
  122. $data = array();
  123. $i = 0;
  124. while($result = $query->fetch_assoc())
  125. {
  126. $data[$i]['timestamp'] = $result['timestamp'];
  127. $account = new userModel();
  128. $data[$i]['account'] = $account->userInfo($result['userId']);
  129. $data[$i]['action'] = $result['action'];
  130. $data[$i]['item'] = $result['item'];
  131. $i++;
  132. }
  133. return $data;
  134. }
  135. else
  136. {
  137. return array();
  138. }
  139. }
  140. }
  141. ?>