Base d'un site web v2

This commit is contained in:
Sanchez
2017-04-17 23:05:15 +02:00
committed by mathieu
parent e761ff7c5a
commit ec9eb79038
21 changed files with 143 additions and 103 deletions

View File

@ -5,12 +5,11 @@ namespace base\Model;
use Exception;
use PDO;
class BDD
{
const SQL_SERVER = 'localhost';
const SQL_LOGIN = 'root';
const SQL_PASSWORD = '';
const SQL_DB = 'eldotravo';
class BDD {
const SQL_SERVER = 'localhost'; // BDD Server
const SQL_LOGIN = 'root'; // BDD Login
const SQL_PASSWORD = ''; // BDD Password
const SQL_DB = 'base'; // BDD Name
private static $bdd;
@ -22,10 +21,11 @@ class BDD
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
];
self::$bdd = new PDO('mysql:host='.self::SQL_SERVER.';dbname='.self::SQL_DB.';charset=utf8', self::SQL_LOGIN, self::SQL_PASSWORD, $pdo_options);
self::$bdd = new PDO('mysql:host=' . self::SQL_SERVER . ';dbname=' . self::SQL_DB.';charset=utf8',
self::SQL_LOGIN, self::SQL_PASSWORD, $pdo_options);
}
catch(Exception $e) {
die('Erreur : '.$e->getMessage());
catch (Exception $e) {
die('Erreur : ' . $e->getMessage());
}
}

View File

@ -6,6 +6,8 @@ abstract class BDTables {
// const NOM = 'bd_nom'
// Ex : const ABONNEMENT = 'abonnement';
const LOGS = "logs";
}
?>

View File

@ -8,7 +8,7 @@
*******************************************************************************/
namespace base\Model;
define('FPDF_VERSION','1.81');
define('FPDF_VERSION', '1.81');
class FPDF
{

View File

@ -7,12 +7,10 @@ use PDO;
class Logs {
public $id;
public $site;
public $level;
public $message;
public $file;
public $line;
public $user;
public $date;
const ERROR_LEVEL = [
@ -36,22 +34,19 @@ class Logs {
/**
* Logs constructor.
* @param int $id
* @param string $site
* @param string $level
* @param string $message
* @param string $file
* @param string $line
* @param string $date
* @param int|null $id
* @param string|null $level
* @param string|null $message
* @param string|null $file
* @param string|null $line
* @param string|null $date
*/
public function __construct(int $id = null, string $site = null, string $level = null, string $message = null,
string $file = null, string $line = null, string $date = null)
public function __construct(int $id = null, string $level = null, string $message = null, string $file = null, string $line = null, string $date = null)
{
if ($id === NULL) {
return;
}
$this->id = $id;
$this->site = $site;
$this->level = $level;
$this->message = $message;
$this->file = $file;
@ -60,10 +55,14 @@ class Logs {
}
public static function insert($site, $level, $message, $file, $line, $date) {
$user = NULL;
$req = BDD::instance()->prepare('INSERT INTO '.BDTables::LOGS.'(site, level, message, date, file, line) VALUES(:site, :level, :message, :date, :file, :line)');
$req->execute(['site' => $site, 'level' => $level, 'message' => $message, 'date' => $date, 'file' => $file, 'line' => $line]);
public static function insert($level, $message, $file, $line, $date) {
Model::insert(BDTables::LOGS, [
'level' => $level,
'message' => $message,
'file' => $file,
'line' => $line,
'date' => date("Y-m-d H:i:s", strtotime($date))
]);
}
/**
@ -72,15 +71,16 @@ class Logs {
* @return array
*/
public static function getLastLogs(int $limit){
$req = BDD::instance()->prepare('SELECT l.*
FROM '.BDTables::LOGS.' l
ORDER BY date DESC LIMIT :limit');
$req = BDD::instance()->prepare('SELECT *
FROM ' . BDTables::LOGS . '
ORDER BY date DESC
LIMIT :limit');
$req->bindValue('limit', $limit, PDO::PARAM_INT);
$req->execute();
$return = [];
foreach ($req->fetchAll() as $l){
$log = new Logs($l['id'], $l['site'], $l['level'], $l['message'], $l['file'], $l['line'], $l['date']);
$log = new Logs($l['id'], $l['level'], $l['message'], $l['file'], $l['line'], $l['date']);
$return[] = $log;
}
@ -103,22 +103,6 @@ class Logs {
$this->id = $id;
}
/**
* @return string
*/
public function getSite()
{
return htmlspecialchars($this->site);
}
/**
* @param string $site
*/
public function setSite($site)
{
$this->site = $site;
}
/**
* @return string
*/
@ -203,7 +187,8 @@ class Logs {
* Retourne le type d'erreur en string (label)
* @return string
*/
public function getErrorLabel(){
public function getErrorLabel()
{
return htmlspecialchars(self::ERROR_LEVEL[$this->level]);
}
}

View File

@ -12,7 +12,7 @@ class Model {
*/
public static function insert(string $tableName, array $data){
$req = BDD::instance()->prepare('INSERT INTO ' . $tableName . ' (' . implode(', ', array_keys($data)) . ')
VALUES (' . ':'. implode(', :', array_keys($data)) . ')');
VALUES (' . ':' . implode(', :', array_keys($data)) . ')');
$req->execute($data);
return BDD::lastInsertId();
}
@ -28,12 +28,12 @@ class Model {
$reqStr = 'UPDATE ' . $tableName . ' SET ';
$lastKey = endKey($data);
foreach ($data as $key => $value){
$reqStr .= $key.' = :'.$key;
$reqStr .= $key . ' = :' . $key;
if($key != $lastKey) {
$reqStr .= ', ';
}
}
$reqStr .= ' WHERE '.$idColumn.' = :'.$idColumn;
$reqStr .= ' WHERE ' . $idColumn . ' = :' . $idColumn;
$data[$idColumn] = $idValue;
//echo $reqStr; exit();