mirror of
https://github.com/ZeJMaN/LBCAlerte_ynh.git
synced 2025-07-26 13:17:34 +02:00
Upgrade LBCAlerte to version 3.3
Add upgrade script
This commit is contained in:
154
sources/app/models/Storage/Db/Ad.php
Normal file
154
sources/app/models/Storage/Db/Ad.php
Normal file
@ -0,0 +1,154 @@
|
||||
<?php
|
||||
|
||||
namespace App\Storage\Db;
|
||||
|
||||
use App\Ad\Ad as AdItem;
|
||||
|
||||
class Ad implements \App\Storage\Ad
|
||||
{
|
||||
/**
|
||||
* @var \mysqli
|
||||
*/
|
||||
protected $_connection;
|
||||
|
||||
protected $_table = "LBC_BackupAd";
|
||||
|
||||
/**
|
||||
* @var \App\User\User
|
||||
*/
|
||||
protected $_user;
|
||||
|
||||
public function __construct(\mysqli $connection, \App\User\User $user)
|
||||
{
|
||||
$this->_connection = $connection;
|
||||
$this->_user = $user;
|
||||
}
|
||||
|
||||
public function fetchAll($order = null)
|
||||
{
|
||||
$ads = array();
|
||||
$query = "SELECT * FROM ".$this->_table
|
||||
." WHERE user_id = ".$this->_user->getId();
|
||||
if ($order) {
|
||||
if (is_string($order)) {
|
||||
$query .= " ORDER By ".$order;
|
||||
} elseif (is_array($order)) {
|
||||
$query .= implode(", ", $order);
|
||||
}
|
||||
}
|
||||
$adsDb = $this->_connection->query($query);
|
||||
while ($adDb = $adsDb->fetch_assoc()) {
|
||||
foreach (array("photos", "properties") AS $key) {
|
||||
$adDb[$key] = json_decode($adDb[$key], true);
|
||||
if (!$adDb[$key]) {
|
||||
$adDb[$key] = array();
|
||||
}
|
||||
}
|
||||
$ad = new AdItem();
|
||||
$ad->setFromArray($adDb);
|
||||
$ads[] = $ad;
|
||||
}
|
||||
return $ads;
|
||||
}
|
||||
|
||||
public function fetchById($id)
|
||||
{
|
||||
$ad = null;
|
||||
$adDb = $this->_connection->query(
|
||||
"SELECT * FROM ".$this->_table." WHERE user_id = ".$this->_user->getId()."
|
||||
AND id = '".$this->_connection->real_escape_string($id)."'")
|
||||
->fetch_assoc();
|
||||
if ($adDb) {
|
||||
foreach (array("photos", "properties") AS $key) {
|
||||
$adDb[$key] = json_decode($adDb[$key], true);
|
||||
if (!$adDb[$key]) {
|
||||
$adDb[$key] = array();
|
||||
}
|
||||
}
|
||||
$ad = new AdItem();
|
||||
$ad->setFromArray($adDb);
|
||||
}
|
||||
return $ad;
|
||||
}
|
||||
|
||||
public function save(AdItem $ad)
|
||||
{
|
||||
$options = $ad->toArray();
|
||||
$options["photos"] = json_encode($options["photos"]);
|
||||
$options["properties"] = json_encode($options["properties"]);
|
||||
|
||||
if (!$ad->getAid()) {
|
||||
$options["user_id"] = $this->_user->getId();
|
||||
unset($options["aid"]);
|
||||
if (empty($options["date_created"])) {
|
||||
$options["date_created"] = date("Y-m-d H:i:s");
|
||||
}
|
||||
$sqlOptions = array();
|
||||
foreach ($options AS $name => $value) {
|
||||
if ($value === null) {
|
||||
$value = "NULL";
|
||||
} elseif (is_bool($value)) {
|
||||
$value = (int) $value;
|
||||
} else {
|
||||
$value = "'".$this->_connection->real_escape_string($value)."'";
|
||||
}
|
||||
$sqlOptions[$name] = $value;
|
||||
}
|
||||
$this->_connection->query("INSERT INTO ".$this->_table.
|
||||
" (`".implode("`, `", array_keys($options)).
|
||||
"`) VALUES (".implode(", ", $sqlOptions).")");
|
||||
if ($this->_connection->error) {
|
||||
var_dump($this->_connection->error);
|
||||
exit;
|
||||
}
|
||||
} else {
|
||||
$sqlOptions = array();
|
||||
unset($options["aid"]);
|
||||
foreach ($options AS $name => $value) {
|
||||
if ($value === null) {
|
||||
$value = "NULL";
|
||||
} elseif (is_bool($value)) {
|
||||
$value = (int) $value;
|
||||
} else {
|
||||
$value = "'".$this->_connection->real_escape_string($value)."'";
|
||||
}
|
||||
$sqlOptions[] = "`".$name."` = ".$value;
|
||||
}
|
||||
$this->_connection->query("UPDATE ".$this->_table." SET
|
||||
".implode(",", $sqlOptions).
|
||||
" WHERE `aid` = ".$ad->getAid());
|
||||
if ($this->_connection->error) {
|
||||
var_dump($this->_connection->error);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function delete(AdItem $ad)
|
||||
{
|
||||
$this->_connection->query("DELETE FROM ".$this->_table."
|
||||
WHERE `aid` = ".$ad->getAid());
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \mysqli $dbConnection
|
||||
* @return \App\Storage\Db\Ad
|
||||
*/
|
||||
public function setDbConnection($dbConnection)
|
||||
{
|
||||
$this->_connection = $dbConnection;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \mysqli
|
||||
*/
|
||||
public function getDbConnection()
|
||||
{
|
||||
return $this->_connection;
|
||||
}
|
||||
}
|
@ -24,13 +24,9 @@ class User implements \App\Storage\User
|
||||
$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);
|
||||
}
|
||||
}
|
||||
->setUsername($userDb->username)
|
||||
->setApiKey($userDb->api_key);
|
||||
$this->_loadUserOptions($user, $userDb->options);
|
||||
$users[] = $user;
|
||||
}
|
||||
return $users;
|
||||
@ -47,29 +43,56 @@ class User implements \App\Storage\User
|
||||
$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);
|
||||
}
|
||||
}
|
||||
->setUsername($userDb->username)
|
||||
->setApiKey($userDb->api_key);
|
||||
$this->_loadUserOptions($user, $userDb->options);
|
||||
}
|
||||
return $user;
|
||||
}
|
||||
|
||||
protected function _loadUserOptions(\App\User\User $user, $options)
|
||||
{
|
||||
if (empty($options)) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$options = json_decode($options, true);
|
||||
if (!is_array($options)) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
if (!empty($options["notification"]) && is_array($options["notification"])) {
|
||||
foreach ($options["notification"] AS $key => $params) {
|
||||
if ($params && !isset($params["active"])) {
|
||||
$options["notification"][$key]["active"] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$user->setOptions($options);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function save(\App\User\User $user)
|
||||
{
|
||||
if (!$api_key = $user->getApiKey()) {
|
||||
$api_key = "NULL";
|
||||
} else {
|
||||
$api_key = "'".$this->_connection->real_escape_string($api_key)."'";
|
||||
}
|
||||
if (!$this->fetchByUsername($user->getUsername())) {
|
||||
$this->_connection->query("INSERT INTO `".$this->_table.
|
||||
"` (`username`, `password`, `options`) VALUES (
|
||||
"` (`username`, `password`, `api_key`, `options`) VALUES (
|
||||
'".$this->_connection->real_escape_string($user->getUsername())."',
|
||||
'".$this->_connection->real_escape_string($user->getPassword())."',
|
||||
".$api_key.",
|
||||
'".$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())."',
|
||||
`api_key` = ".$api_key.",
|
||||
`options` = '".$this->_connection->real_escape_string(json_encode($user->getOptions()))."'
|
||||
WHERE id = ".$user->getId());
|
||||
}
|
||||
|
Reference in New Issue
Block a user