2022-02-01 20:38:11 +01:00
|
|
|
<?php declare(strict_types=1);
|
2022-01-31 21:05:14 +01:00
|
|
|
|
|
|
|
namespace App\Repository;
|
|
|
|
|
|
|
|
use App\Controller\DatabaseConnection;
|
2022-09-17 15:40:44 +02:00
|
|
|
use App\Entity\Domain;
|
2022-01-31 21:05:14 +01:00
|
|
|
use App\Entity\Panel;
|
|
|
|
use PDO;
|
|
|
|
use PDOException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class PanelRepository
|
|
|
|
{
|
2022-09-17 15:40:44 +02:00
|
|
|
public function __construct(private readonly DatabaseConnection $databaseConnection)
|
2022-01-31 21:05:14 +01:00
|
|
|
{}
|
|
|
|
|
2022-09-17 15:40:44 +02:00
|
|
|
public function findSelf(): array
|
|
|
|
{
|
|
|
|
$sql = "
|
|
|
|
SELECT id, name, a, aaaa, apikey, self
|
|
|
|
FROM " . DatabaseConnection::TABLE_PANELS . "
|
|
|
|
WHERE self = 1";
|
|
|
|
|
|
|
|
$panels = [];
|
|
|
|
try {
|
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
|
|
|
$statement->execute();
|
|
|
|
while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
|
|
|
|
$panel = new Panel(name: $result['name'], id: $result['id'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], self: $result['self']);
|
|
|
|
$panels[] = $panel;
|
|
|
|
}
|
|
|
|
return $panels;
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
exit($e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2022-01-31 21:05:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function findAll(): array
|
|
|
|
{
|
|
|
|
$panels = [];
|
|
|
|
$sql = "
|
2022-09-17 15:40:44 +02:00
|
|
|
SELECT id, name, a, aaaa, apikey, self
|
2022-01-31 21:05:14 +01:00
|
|
|
FROM " . DatabaseConnection::TABLE_PANELS . "
|
|
|
|
ORDER BY name";
|
|
|
|
|
|
|
|
try {
|
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
|
|
|
$statement->execute();
|
|
|
|
while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
|
2022-09-17 15:40:44 +02:00
|
|
|
$panel = new Panel(name: $result['name'], id: $result['id'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], self: $result['self']);
|
2022-01-31 21:05:14 +01:00
|
|
|
$panels[] = $panel;
|
|
|
|
}
|
|
|
|
return $panels;
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
exit($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $id
|
|
|
|
*
|
2022-09-17 15:40:44 +02:00
|
|
|
* @return null|\App\Entity\Panel
|
2022-01-31 21:05:14 +01:00
|
|
|
*/
|
2022-09-17 15:40:44 +02:00
|
|
|
public function findByID(int $id): ?Panel
|
2022-01-31 21:05:14 +01:00
|
|
|
{
|
|
|
|
$sql = "
|
2022-09-17 15:40:44 +02:00
|
|
|
SELECT id, name, a, aaaa, apikey, self
|
2022-01-31 21:05:14 +01:00
|
|
|
FROM . " . DatabaseConnection::TABLE_PANELS . "
|
|
|
|
WHERE id = :id";
|
|
|
|
|
|
|
|
try {
|
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
|
|
|
$statement->bindParam(param: ':id', var: $id);
|
|
|
|
$statement->execute();
|
2022-09-17 15:40:44 +02:00
|
|
|
if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
|
|
|
|
return new Panel(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], self: $result['self']);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2022-01-31 21:05:14 +01:00
|
|
|
} catch (PDOException $e) {
|
|
|
|
exit($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String $name
|
|
|
|
*
|
|
|
|
* @return \App\Entity\Panel|bool
|
|
|
|
*/
|
|
|
|
public function findByName(string $name): Panel|bool
|
|
|
|
{
|
|
|
|
$sql = "
|
2022-09-17 15:40:44 +02:00
|
|
|
SELECT id, name, a, aaaa, apikey, self
|
2022-01-31 21:05:14 +01:00
|
|
|
FROM " . DatabaseConnection::TABLE_PANELS . "
|
|
|
|
WHERE name = :name";
|
|
|
|
|
|
|
|
try {
|
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
|
|
|
$statement->bindParam(param: ':name', var: $name);
|
|
|
|
$statement->execute();
|
|
|
|
if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
|
2022-09-17 15:40:44 +02:00
|
|
|
return new Panel(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], self: $result['self']);
|
2022-01-31 21:05:14 +01:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
exit($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String $name
|
|
|
|
* @param String $a
|
|
|
|
* @param String $aaaa
|
|
|
|
* @param String $apikey
|
|
|
|
*
|
|
|
|
* @return string|false
|
|
|
|
*/
|
2022-09-17 15:40:44 +02:00
|
|
|
public function insert(string $name, string $a, string $aaaa, String $apikey, int $self): bool|string
|
2022-01-31 21:05:14 +01:00
|
|
|
{
|
|
|
|
$sql = "
|
2022-09-17 15:40:44 +02:00
|
|
|
INSERT INTO " . DatabaseConnection::TABLE_PANELS . " (name, a, aaaa, apikey, self)
|
|
|
|
VALUES (:name, :a, :aaaa, :apikey, :self)";
|
2022-01-31 21:05:14 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
|
|
|
$statement->bindParam(param: ':name', var: $name);
|
|
|
|
$statement->bindParam(param: ':a', var: $a);
|
|
|
|
$statement->bindParam(param: ':aaaa', var: $aaaa);
|
|
|
|
$statement->bindParam(param: ':apikey', var: $apikey);
|
2022-09-17 15:40:44 +02:00
|
|
|
$statement->bindParam(param: ':self', var: $self);
|
2022-01-31 21:05:14 +01:00
|
|
|
$statement->execute();
|
|
|
|
|
|
|
|
return $this->databaseConnection->getConnection()->lastInsertId();
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
exit($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Int $id
|
|
|
|
* @param String $name
|
|
|
|
* @param String $a
|
|
|
|
* @param String $aaaa
|
|
|
|
* @param String $apikey
|
|
|
|
*
|
|
|
|
* @return false|int
|
|
|
|
*/
|
2022-09-17 15:40:44 +02:00
|
|
|
public function update(int $id, string $name, string $a, string $aaaa, String $apikey, int $self): bool|int
|
2022-01-31 21:05:14 +01:00
|
|
|
{
|
|
|
|
$current = $this->findByID(id: $id);
|
|
|
|
|
|
|
|
if (empty($name)) {
|
|
|
|
$name = $current->getName();
|
|
|
|
}
|
|
|
|
if (empty($a)) {
|
|
|
|
$a = $current->getA();
|
|
|
|
}
|
|
|
|
if (empty($aaaa)) {
|
|
|
|
$aaaa = $current->getAaaa();
|
|
|
|
}
|
|
|
|
if (empty($apikey)) {
|
|
|
|
$apikey = $current->getApikey();
|
|
|
|
}
|
|
|
|
|
2022-09-17 15:40:44 +02:00
|
|
|
if (empty($self)) {
|
|
|
|
echo "self is empty";
|
|
|
|
$self = $current->getSelf();
|
|
|
|
} else {
|
|
|
|
if ($self == -1) {
|
|
|
|
$self = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-31 21:05:14 +01:00
|
|
|
$sql = "
|
|
|
|
UPDATE " . DatabaseConnection::TABLE_PANELS . " SET
|
|
|
|
name = :name,
|
|
|
|
a = :a,
|
|
|
|
aaaa = :aaaa,
|
2022-09-17 15:40:44 +02:00
|
|
|
apikey = :apikey,
|
|
|
|
self = :self
|
2022-01-31 21:05:14 +01:00
|
|
|
WHERE id = :id";
|
|
|
|
|
|
|
|
try {
|
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
|
|
|
$statement->bindParam(param: 'id', var: $id);
|
|
|
|
$statement->bindParam(param: 'name', var: $name);
|
|
|
|
$statement->bindParam(param: 'a', var: $a);
|
|
|
|
$statement->bindParam(param: 'aaaa', var: $aaaa);
|
|
|
|
$statement->bindParam(param: 'apikey', var: $apikey);
|
2022-09-17 15:40:44 +02:00
|
|
|
$statement->bindParam(param: 'self', var: $self);
|
2022-01-31 21:05:14 +01:00
|
|
|
$statement->execute();
|
|
|
|
|
|
|
|
return $statement->rowCount();
|
|
|
|
} catch (PDOException $e) {
|
2022-03-01 18:14:33 +01:00
|
|
|
echo $e->getMessage();
|
2022-01-31 21:05:14 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $id
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function delete($id): int
|
|
|
|
{
|
|
|
|
$sql = "
|
|
|
|
DELETE FROM " . DatabaseConnection::TABLE_PANELS . "
|
|
|
|
WHERE id = :id";
|
|
|
|
|
|
|
|
try {
|
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
|
|
|
$statement->bindParam(param: 'id', var: $id);
|
|
|
|
$statement->execute();
|
|
|
|
|
|
|
|
return $statement->rowCount();
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
exit($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String $field
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getLongestEntry(String $field): int
|
|
|
|
{
|
|
|
|
$sql = "
|
|
|
|
SELECT MAX(LENGTH(" . $field . ")) as length FROM " . DatabaseConnection::TABLE_PANELS;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
|
|
|
$statement->execute();
|
|
|
|
$result = $statement->fetch();
|
|
|
|
return $result['length'];
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
exit($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
2022-09-17 15:40:44 +02:00
|
|
|
}
|