web-base/core/Api/GroupsAPI.class.php

184 lines
5.1 KiB
PHP
Raw Normal View History

2020-06-20 20:13:51 +02:00
<?php
namespace Api {
2020-06-24 01:09:08 +02:00
use Driver\SQL\Condition\Compare;
2022-06-20 19:52:31 +02:00
use Objects\Context;
2020-06-24 01:09:08 +02:00
2020-07-01 21:10:25 +02:00
abstract class GroupsAPI extends Request {
2020-06-20 20:13:51 +02:00
2022-06-20 19:52:31 +02:00
public function __construct(Context $context, bool $externalCall = false, array $params = array()) {
parent::__construct($context, $externalCall, $params);
}
protected function groupExists($name): bool {
$sql = $this->context->getSQL();
2020-06-24 01:09:08 +02:00
$res = $sql->select($sql->count())
->from("Group")
->where(new Compare("name", $name))
->execute();
$this->success = ($res !== FALSE);
$this->lastError = $sql->getLastError();
return $this->success && $res[0]["count"] > 0;
}
2020-06-20 20:13:51 +02:00
}
}
namespace Api\Groups {
use Api\GroupsAPI;
use Api\Parameter\Parameter;
2020-06-24 01:09:08 +02:00
use Api\Parameter\StringType;
2022-06-20 19:52:31 +02:00
use Objects\Context;
use Objects\DatabaseEntity\Group;
2020-06-20 20:13:51 +02:00
class Fetch extends GroupsAPI {
private int $groupCount;
2022-06-20 19:52:31 +02:00
public function __construct(Context $context, $externalCall = false) {
parent::__construct($context, $externalCall, array(
'page' => new Parameter('page', Parameter::TYPE_INT, true, 1),
'count' => new Parameter('count', Parameter::TYPE_INT, true, 20)
2020-06-20 20:13:51 +02:00
));
$this->groupCount = 0;
}
2022-06-20 19:52:31 +02:00
private function fetchGroupCount(): bool {
2020-06-20 20:13:51 +02:00
2022-06-20 19:52:31 +02:00
$sql = $this->context->getSQL();
2020-06-20 20:13:51 +02:00
$res = $sql->select($sql->count())->from("Group")->execute();
$this->success = ($res !== FALSE);
$this->lastError = $sql->getLastError();
if ($this->success) {
$this->groupCount = $res[0]["count"];
}
return $this->success;
}
2022-02-21 13:01:03 +01:00
public function _execute(): bool {
2020-06-20 20:13:51 +02:00
$page = $this->getParam("page");
if($page < 1) {
return $this->createError("Invalid page count");
}
$count = $this->getParam("count");
if($count < 1 || $count > 50) {
return $this->createError("Invalid fetch count");
}
2022-06-20 19:52:31 +02:00
if (!$this->fetchGroupCount()) {
2020-06-20 20:13:51 +02:00
return false;
}
2022-06-20 19:52:31 +02:00
$sql = $this->context->getSQL();
$res = $sql->select("Group.id as groupId", "Group.name as groupName", "Group.color as groupColor", $sql->count("UserGroup.user_id"))
2020-06-20 20:13:51 +02:00
->from("Group")
2022-06-20 19:52:31 +02:00
->leftJoin("UserGroup", "UserGroup.group_id", "Group.id")
->groupBy("Group.id")
->orderBy("Group.id")
2020-06-20 20:13:51 +02:00
->ascending()
->limit($count)
->offset(($page - 1) * $count)
2020-06-20 20:13:51 +02:00
->execute();
$this->success = ($res !== FALSE);
$this->lastError = $sql->getLastError();
if($this->success) {
$this->result["groups"] = array();
foreach($res as $row) {
$groupId = intval($row["groupId"]);
$groupName = $row["groupName"];
$groupColor = $row["groupColor"];
2020-06-20 20:13:51 +02:00
$memberCount = $row["usergroup_user_id_count"];
$this->result["groups"][$groupId] = array(
"name" => $groupName,
"memberCount" => $memberCount,
"color" => $groupColor,
2020-06-20 20:13:51 +02:00
);
}
$this->result["pageCount"] = intval(ceil($this->groupCount / $count));
2020-06-20 20:13:51 +02:00
$this->result["totalCount"] = $this->groupCount;
}
return $this->success;
}
}
2020-06-24 01:09:08 +02:00
class Create extends GroupsAPI {
2022-06-20 19:52:31 +02:00
public function __construct(Context $context, $externalCall = false) {
parent::__construct($context, $externalCall, array(
2020-06-24 01:09:08 +02:00
'name' => new StringType('name', 32),
'color' => new StringType('color', 10),
));
}
2022-02-21 13:01:03 +01:00
public function _execute(): bool {
2020-06-24 01:09:08 +02:00
$name = $this->getParam("name");
if (preg_match("/^[a-zA-Z][a-zA-Z0-9_-]*$/", $name) !== 1) {
return $this->createError("Invalid name");
}
$color = $this->getParam("color");
if (preg_match("/^#[a-fA-F0-9]{3,6}$/", $color) !== 1) {
return $this->createError("Invalid color");
}
$exists = $this->groupExists($name);
if (!$this->success) {
return false;
} else if ($exists) {
return $this->createError("A group with this name already exists");
}
2022-06-20 19:52:31 +02:00
$sql = $this->context->getSQL();
2020-06-24 01:09:08 +02:00
2022-06-20 19:52:31 +02:00
$group = new Group();
$group->name = $name;
$group->color = $color;
$this->success = ($group->save($sql) !== FALSE);
2020-06-24 01:09:08 +02:00
$this->lastError = $sql->getLastError();
if ($this->success) {
2022-06-20 19:52:31 +02:00
$this->result["id"] = $group->getId();
2020-06-24 01:09:08 +02:00
}
return $this->success;
}
}
2020-06-24 01:23:37 +02:00
class Delete extends GroupsAPI {
2022-06-20 19:52:31 +02:00
public function __construct(Context $context, $externalCall = false) {
parent::__construct($context, $externalCall, array(
'id' => new Parameter('id', Parameter::TYPE_INT)
2020-06-24 01:23:37 +02:00
));
}
2022-02-21 13:01:03 +01:00
public function _execute(): bool {
2022-06-20 19:52:31 +02:00
$id = $this->getParam("id");
2020-06-24 16:09:04 +02:00
if (in_array($id, DEFAULT_GROUPS)) {
return $this->createError("You cannot delete a default group.");
}
2020-06-24 01:23:37 +02:00
2022-06-20 19:52:31 +02:00
$sql = $this->context->getSQL();
$group = Group::find($sql, $id);
2020-06-24 01:23:37 +02:00
2022-06-20 19:52:31 +02:00
$this->success = ($group !== FALSE);
2020-06-24 01:23:37 +02:00
$this->lastError = $sql->getLastError();
2022-06-20 19:52:31 +02:00
if ($this->success && $group === null) {
2020-06-24 01:23:37 +02:00
return $this->createError("This group does not exist.");
}
2022-06-20 19:52:31 +02:00
$this->success = ($group->delete($sql) !== FALSE);
2020-06-24 01:23:37 +02:00
$this->lastError = $sql->getLastError();
return $this->success;
}
}
2020-06-20 20:13:51 +02:00
}