web-base/core/Api/User/Fetch.class.php

96 lines
2.5 KiB
PHP
Raw Normal View History

2020-04-03 18:09:01 +02:00
<?php
namespace Api\User;
2020-04-04 01:15:59 +02:00
use Api\Parameter\Parameter;
2020-04-03 18:09:01 +02:00
use \Api\Request;
class Fetch extends Request {
2020-06-15 20:07:43 +02:00
const SELECT_SIZE = 10;
2020-04-04 01:15:59 +02:00
private int $userCount;
2020-04-03 18:09:01 +02:00
public function __construct($user, $externalCall = false) {
2020-04-04 01:15:59 +02:00
parent::__construct($user, $externalCall, array(
'page' => new Parameter('page', Parameter::TYPE_INT, true, 1)
));
2020-04-03 18:09:01 +02:00
$this->loginRequired = true;
2020-04-03 22:10:21 +02:00
$this->requiredGroup = USER_GROUP_ADMIN;
2020-04-04 01:15:59 +02:00
$this->userCount = 0;
2020-06-14 19:39:52 +02:00
$this->csrfTokenRequired = true;
2020-04-04 01:15:59 +02:00
}
private function getUserCount() {
$sql = $this->user->getSQL();
$res = $sql->select($sql->count())->from("User")->execute();
$this->success = ($res !== FALSE);
$this->lastError = $sql->getLastError();
if ($this->success) {
$this->userCount = $res[0]["count"];
}
return $this->success;
2020-04-03 18:09:01 +02:00
}
public function execute($values = array()) {
if(!parent::execute($values)) {
return false;
}
2020-04-04 01:15:59 +02:00
$page = $this->getParam("page");
if($page < 1) {
return $this->createError("Invalid page count");
}
if (!$this->getUserCount()) {
return false;
}
2020-04-03 18:09:01 +02:00
$sql = $this->user->getSQL();
2020-04-04 15:11:38 +02:00
$res = $sql->select("User.uid as userId", "User.name", "User.email", "User.registered_at",
2020-04-04 01:15:59 +02:00
"Group.uid as groupId", "Group.name as groupName")
2020-04-03 18:09:01 +02:00
->from("User")
->leftJoin("UserGroup", "User.uid", "UserGroup.user_id")
->leftJoin("Group", "Group.uid", "UserGroup.group_id")
2020-04-04 01:15:59 +02:00
->orderBy("User.uid")
->ascending()
->limit(Fetch::SELECT_SIZE)
->offset(($page - 1) * Fetch::SELECT_SIZE)
2020-04-03 18:09:01 +02:00
->execute();
$this->success = ($res !== FALSE);
$this->lastError = $sql->getLastError();
if($this->success) {
$this->result["users"] = array();
foreach($res as $row) {
2020-04-04 01:15:59 +02:00
$userId = intval($row["userId"]);
$groupId = intval($row["groupId"]);
2020-04-03 18:09:01 +02:00
$groupName = $row["groupName"];
if (!isset($this->result["users"][$userId])) {
2020-06-15 20:07:43 +02:00
$this->result["users"][$userId] = array(
2020-04-03 18:09:01 +02:00
"uid" => $userId,
"name" => $row["name"],
"email" => $row["email"],
2020-04-04 15:11:38 +02:00
"registered_at" => $row["registered_at"],
2020-04-03 18:09:01 +02:00
"groups" => array(),
);
}
if(!is_null($groupId)) {
$this->result["users"][$userId]["groups"][$groupId] = $groupName;
}
}
2020-06-15 20:07:43 +02:00
$this->result["pageCount"] = intval(ceil($this->userCount / Fetch::SELECT_SIZE));
2020-06-15 23:10:14 +02:00
$this->result["totalCount"] = $this->userCount;
2020-04-03 18:09:01 +02:00
}
return $this->success;
}
2020-04-04 15:11:38 +02:00
}