user.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace Controller\Api;
  3. class User extends \Controller\Api {
  4. protected function user_array(\Model\User $user) {
  5. $group_id = $user->id;
  6. if($user->role == 'group') {
  7. $group = new \Model\Custom("user_group");
  8. $man = $group->find(array("group_id = ? AND manager = 1", $user->id));
  9. $man = array_filter($man);
  10. if(!empty($man) && $man[0]->user_id > 0) {
  11. $group_id = $man[0]->user_id;
  12. }
  13. }
  14. $result = array(
  15. "id" =>$group_id,
  16. "name" => $user->name,
  17. "username" => $user->username,
  18. "email" => $user->email
  19. );
  20. return ($result);
  21. }
  22. public function single_get($f3, $params) {
  23. $user = new \Model\User();
  24. $user->load(array("username = ?", $params["username"]));
  25. if($user->id) {
  26. $this->_printJson($this->user_array($user));
  27. } else {
  28. $f3->error(404);
  29. }
  30. }
  31. public function single_email($f3, $params) {
  32. $user = new \Model\User();
  33. $user->load(array("email = ? AND deleted_date IS NULL", $params["email"]));
  34. if($user->id) {
  35. $this->_printJson($this->user_array($user));
  36. } else {
  37. $f3->error(404);
  38. }
  39. }
  40. // Gets a List of uers
  41. public function get($f3) {
  42. $pagLimit = $f3->get("GET.limit") ?: 30;
  43. if($pagLimit == -1) {
  44. $pagLimit = 100000;
  45. } elseif ($pagLimit < 0) {
  46. $pagLimit = 30;
  47. }
  48. $user = new \Model\User;
  49. $result = $user->paginate(
  50. $f3->get("GET.offset") / $pagLimit,
  51. $pagLimit,
  52. "deleted_date IS NULL AND role != 'group'"
  53. );
  54. $users = array();
  55. foreach ($result["subset"] as $user) {
  56. $users[] = $this->user_array($user);
  57. }
  58. $this->_printJson(array(
  59. "total_count" => $result["total"],
  60. "limit" => $result["limit"],
  61. "users" => $users,
  62. "offset" => $result["pos"] * $result["limit"]
  63. ));
  64. }
  65. // Gets a list of Uers
  66. public function get_group($f3) {
  67. $pagLimit = $f3->get("GET.limit") ?: 30;
  68. if($pagLimit == -1) {
  69. $pagLimit = 100000;
  70. } elseif ($pagLimit < 0) {
  71. $pagLimit = 30;
  72. }
  73. $user = new \Model\User;
  74. $result = $user->paginate(
  75. $f3->get("GET.offset") / $pagLimit,
  76. $pagLimit,
  77. "deleted_date IS NULL AND role = 'group'"
  78. );
  79. $groups = array();
  80. foreach ($result["subset"] as $user) {
  81. $groups[] = $this->user_array($user);
  82. }
  83. $this->_printJson(array(
  84. "total_count" => $result["total"],
  85. "limit" => $result["limit"],
  86. "groups" => $groups,
  87. "offset" => $result["pos"] * $result["limit"]
  88. ));
  89. }
  90. }