mirror of
https://github.com/ZeJMaN/LBCAlerte_ynh.git
synced 2025-07-06 20:00:48 +02:00
Initial commit
Functional, without SSO
This commit is contained in:
141
sources/app/models/Storage/Db/Alert.php
Normal file
141
sources/app/models/Storage/Db/Alert.php
Normal file
@ -0,0 +1,141 @@
|
||||
<?php
|
||||
|
||||
namespace App\Storage\Db;
|
||||
|
||||
class Alert implements \App\Storage\Alert
|
||||
{
|
||||
/**
|
||||
* @var \mysqli
|
||||
*/
|
||||
protected $_connection;
|
||||
|
||||
protected $_table = "LBC_Alert";
|
||||
|
||||
/**
|
||||
* @var \App\User\User
|
||||
*/
|
||||
protected $_user;
|
||||
|
||||
public function __construct(\mysqli $connection, \App\User\User $user)
|
||||
{
|
||||
$this->_connection = $connection;
|
||||
$this->_user = $user;
|
||||
}
|
||||
|
||||
public function fetchAll()
|
||||
{
|
||||
$alerts = array();
|
||||
$alertsDb = $this->_connection->query("SELECT * FROM ".$this->_table
|
||||
." WHERE user_id = ".$this->_user->getId());
|
||||
while ($alertDb = $alertsDb->fetch_assoc()) {
|
||||
$alert = new \App\Mail\Alert();
|
||||
if (isset($alertDb["last_id"]) && !is_numeric($alertDb["last_id"])) {
|
||||
$alertDb["last_id"] = json_decode($alertDb["last_id"], true);
|
||||
if (!is_array($alertDb["last_id"])) {
|
||||
$alertDb["last_id"] = array();
|
||||
}
|
||||
}
|
||||
$alert->fromArray($alertDb);
|
||||
$alert->id = $alertDb["idstr"];
|
||||
$alerts[] = $alert;
|
||||
}
|
||||
return $alerts;
|
||||
}
|
||||
|
||||
public function fetchById($id)
|
||||
{
|
||||
$alert = null;
|
||||
$alertDb = $this->_connection->query(
|
||||
"SELECT * FROM ".$this->_table." WHERE user_id = ".$this->_user->getId()."
|
||||
AND idstr = '".$this->_connection->real_escape_string($id)."'")
|
||||
->fetch_assoc();
|
||||
if ($alertDb) {
|
||||
$alert = new \App\Mail\Alert();
|
||||
if (isset($alertDb["last_id"]) && !is_numeric($alertDb["last_id"])) {
|
||||
$alertDb["last_id"] = json_decode($alertDb["last_id"], true);
|
||||
if (!is_array($alertDb["last_id"])) {
|
||||
$alertDb["last_id"] = array();
|
||||
}
|
||||
}
|
||||
$alert->fromArray($alertDb);
|
||||
$alert->id = $alertDb["idstr"];
|
||||
}
|
||||
return $alert;
|
||||
}
|
||||
|
||||
public function save(\App\Mail\Alert $alert, $forceInsert = false)
|
||||
{
|
||||
$options = $alert->toArray();
|
||||
|
||||
if (is_array($options["last_id"])) {
|
||||
$options["last_id"] = json_encode($options["last_id"]);
|
||||
}
|
||||
|
||||
if (!$alert->id || $forceInsert) {
|
||||
$options["user_id"] = $this->_user->getId();
|
||||
if (!$alert->id) {
|
||||
$id = sha1(uniqid());
|
||||
$alert->id = $id;
|
||||
}
|
||||
$options["idstr"] = $alert->id;
|
||||
unset($options["id"]);
|
||||
$sqlOptions = array();
|
||||
foreach ($options AS $name => $value) {
|
||||
if ($value === null) {
|
||||
$value = "NULL";
|
||||
} elseif (is_bool($value)) {
|
||||
$value = (int) $value;
|
||||
} elseif (!is_numeric($value)) {
|
||||
$value = "'".$this->_connection->real_escape_string($value)."'";
|
||||
}
|
||||
$sqlOptions[$name] = $value;
|
||||
}
|
||||
$this->_connection->query("INSERT INTO ".$this->_table.
|
||||
" (`".implode("`, `", array_keys($options)).
|
||||
"`, `date_created`) VALUES (".implode(", ", $sqlOptions).", NOW())");
|
||||
} else {
|
||||
$idStr = $options["id"];
|
||||
$sqlOptions = array();
|
||||
unset($options["id"]);
|
||||
foreach ($options AS $name => $value) {
|
||||
if ($value === null) {
|
||||
$value = "NULL";
|
||||
} elseif (is_bool($value)) {
|
||||
$value = (int) $value;
|
||||
} elseif (!is_numeric($value)) {
|
||||
$value = "'".$this->_connection->real_escape_string($value)."'";
|
||||
}
|
||||
$sqlOptions[] = "`".$name."` = ".$value;
|
||||
}
|
||||
$this->_connection->query("UPDATE ".$this->_table." SET
|
||||
".implode(",", $sqlOptions).
|
||||
" WHERE idstr = '".$this->_connection->real_escape_string($idStr)."'");
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function delete(\App\Mail\Alert $alert)
|
||||
{
|
||||
$this->_connection->query("DELETE FROM ".$this->_table."
|
||||
WHERE idstr = '".$this->_connection->real_escape_string($alert->id)."'");
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \mysqli $dbConnection
|
||||
* @return \App\Storage\Db\User
|
||||
*/
|
||||
public function setDbConnection($dbConnection)
|
||||
{
|
||||
$this->_connection = $dbConnection;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \mysqli
|
||||
*/
|
||||
public function getDbConnection()
|
||||
{
|
||||
return $this->_connection;
|
||||
}
|
||||
}
|
102
sources/app/models/Storage/Db/User.php
Normal file
102
sources/app/models/Storage/Db/User.php
Normal file
@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
namespace App\Storage\Db;
|
||||
|
||||
class User implements \App\Storage\User
|
||||
{
|
||||
/**
|
||||
* @var \mysqli
|
||||
*/
|
||||
protected $_connection;
|
||||
|
||||
protected $_table = "LBC_User";
|
||||
|
||||
public function __construct(\mysqli $connection)
|
||||
{
|
||||
$this->_connection = $connection;
|
||||
}
|
||||
|
||||
public function fetchAll()
|
||||
{
|
||||
$users = array();
|
||||
$usersDb = $this->_connection->query("SELECT * FROM ".$this->_table);
|
||||
while ($userDb = $usersDb->fetch_object()) {
|
||||
$user = new \App\User\User();
|
||||
$user->setId($userDb->id)
|
||||
->setPassword($userDb->password)
|
||||
->setUsername($userDb->username);
|
||||
if (!empty($userDb->options)) {
|
||||
$options = json_decode($userDb->options, true);
|
||||
if (is_array($options)) {
|
||||
$user->setOptions($options);
|
||||
}
|
||||
}
|
||||
$users[] = $user;
|
||||
}
|
||||
return $users;
|
||||
}
|
||||
|
||||
public function fetchByUsername($username)
|
||||
{
|
||||
$user = null;
|
||||
$userDb = $this->_connection->query(
|
||||
"SELECT * FROM ".$this->_table." WHERE username = '".
|
||||
$this->_connection->real_escape_string($username)."'")
|
||||
->fetch_object();
|
||||
if ($userDb) {
|
||||
$user = new \App\User\User();
|
||||
$user->setId($userDb->id)
|
||||
->setPassword($userDb->password)
|
||||
->setUsername($userDb->username);
|
||||
if (!empty($userDb->options)) {
|
||||
$options = json_decode($userDb->options, true);
|
||||
if (is_array($options)) {
|
||||
$user->setOptions($options);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $user;
|
||||
}
|
||||
|
||||
public function save(\App\User\User $user)
|
||||
{
|
||||
if (!$this->fetchByUsername($user->getUsername())) {
|
||||
$this->_connection->query("INSERT INTO `".$this->_table.
|
||||
"` (`username`, `password`, `options`) VALUES (
|
||||
'".$this->_connection->real_escape_string($user->getUsername())."',
|
||||
'".$this->_connection->real_escape_string($user->getPassword())."',
|
||||
'".$this->_connection->real_escape_string(json_encode($user->getOptions()))."'
|
||||
)");
|
||||
} else {
|
||||
$this->_connection->query("UPDATE `".$this->_table."` SET
|
||||
`password` = '".$this->_connection->real_escape_string($user->getPassword())."',
|
||||
`options` = '".$this->_connection->real_escape_string(json_encode($user->getOptions()))."'
|
||||
WHERE id = ".$user->getId());
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function delete(\App\User\User $user)
|
||||
{
|
||||
$this->_connection->query("DELETE FROM ".$this->_table." WHERE id = ".$user->getId());
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \mysqli $dbConnection
|
||||
* @return \App\Storage\Db\User
|
||||
*/
|
||||
public function setDbConnection($dbConnection)
|
||||
{
|
||||
$this->_connection = $dbConnection;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \mysqli
|
||||
*/
|
||||
public function getDbConnection()
|
||||
{
|
||||
return $this->_connection;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user