Base d'un site web
This commit is contained in:
41
src/Model/BDD.php
Normal file
41
src/Model/BDD.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace base\Model;
|
||||
|
||||
use Exception;
|
||||
use PDO;
|
||||
|
||||
class BDD
|
||||
{
|
||||
const SQL_SERVER = 'localhost';
|
||||
const SQL_LOGIN = 'root';
|
||||
const SQL_PASSWORD = '';
|
||||
const SQL_DB = 'eldotravo';
|
||||
|
||||
private static $bdd;
|
||||
|
||||
public function __construct() {
|
||||
try {
|
||||
$pdo_options = [
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING,
|
||||
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4',
|
||||
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);
|
||||
}
|
||||
catch(Exception $e) {
|
||||
die('Erreur : '.$e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static function instance() {
|
||||
return self::$bdd;
|
||||
}
|
||||
|
||||
public static function lastInsertId() {
|
||||
return self::$bdd->lastInsertId();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
11
src/Model/BDTables.php
Normal file
11
src/Model/BDTables.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace base\Model;
|
||||
|
||||
abstract class BDTables {
|
||||
|
||||
// const NOM = 'bd_nom'
|
||||
// Ex : const ABONNEMENT = 'abonnement';
|
||||
}
|
||||
|
||||
?>
|
1899
src/Model/FPDF.php
Normal file
1899
src/Model/FPDF.php
Normal file
File diff suppressed because it is too large
Load Diff
211
src/Model/Logs.php
Normal file
211
src/Model/Logs.php
Normal file
@ -0,0 +1,211 @@
|
||||
<?php
|
||||
|
||||
namespace base\Model;
|
||||
|
||||
use PDO;
|
||||
|
||||
class Logs {
|
||||
|
||||
public $id;
|
||||
public $site;
|
||||
public $level;
|
||||
public $message;
|
||||
public $file;
|
||||
public $line;
|
||||
public $user;
|
||||
public $date;
|
||||
|
||||
const ERROR_LEVEL = [
|
||||
1 => 'E_ERROR',
|
||||
2 => 'E_WARNING',
|
||||
4 => 'E_PARSE',
|
||||
8 => 'E_NOTICE',
|
||||
16 => 'E_CORE_ERROR',
|
||||
32 => 'E_CORE_WARNING',
|
||||
64 => 'E_COMPILE_ERROR',
|
||||
128 => 'E_COMPILE_WARNING',
|
||||
256 => 'E_USER_ERROR',
|
||||
512 => 'E_USER_WARNING',
|
||||
1024 => 'E_USER_NOTICE',
|
||||
2048 => 'E_STRICT',
|
||||
4096 => 'E_RECOVERABLE_ERROR',
|
||||
8192 => 'E_DEPRECATED',
|
||||
16384 => 'E_USER_DEPRECATED',
|
||||
32767 => 'E_ALL'
|
||||
];
|
||||
|
||||
/**
|
||||
* Logs constructor.
|
||||
* @param int $id
|
||||
* @param string $site
|
||||
* @param string $level
|
||||
* @param string $message
|
||||
* @param string $file
|
||||
* @param string $line
|
||||
* @param string $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)
|
||||
{
|
||||
if ($id === NULL) {
|
||||
return;
|
||||
}
|
||||
$this->id = $id;
|
||||
$this->site = $site;
|
||||
$this->level = $level;
|
||||
$this->message = $message;
|
||||
$this->file = $file;
|
||||
$this->line = $line;
|
||||
$this->date = $date;
|
||||
}
|
||||
|
||||
|
||||
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]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retourne un tableau des derniers logs (limite en param)
|
||||
* @param int $limit
|
||||
* @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->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']);
|
||||
$return[] = $log;
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
*/
|
||||
public function setId($id)
|
||||
{
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSite()
|
||||
{
|
||||
return htmlspecialchars($this->site);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $site
|
||||
*/
|
||||
public function setSite($site)
|
||||
{
|
||||
$this->site = $site;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getLevel()
|
||||
{
|
||||
return $this->level;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $level
|
||||
*/
|
||||
public function setLevel($level)
|
||||
{
|
||||
$this->level = $level;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getMessage()
|
||||
{
|
||||
return htmlspecialchars($this->message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
*/
|
||||
public function setMessage($message)
|
||||
{
|
||||
$this->message = $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getFile()
|
||||
{
|
||||
return htmlspecialchars($this->file);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $file
|
||||
*/
|
||||
public function setFile($file)
|
||||
{
|
||||
$this->file = $file;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getLine()
|
||||
{
|
||||
return htmlspecialchars($this->line);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $line
|
||||
*/
|
||||
public function setLine($line)
|
||||
{
|
||||
$this->line = $line;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getDate()
|
||||
{
|
||||
return $this->date;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $date
|
||||
*/
|
||||
public function setDate($date)
|
||||
{
|
||||
$this->date = $date;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retourne le type d'erreur en string (label)
|
||||
* @return string
|
||||
*/
|
||||
public function getErrorLabel(){
|
||||
return htmlspecialchars(self::ERROR_LEVEL[$this->level]);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
46
src/Model/Model.php
Normal file
46
src/Model/Model.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace base\Model;
|
||||
|
||||
class Model {
|
||||
|
||||
/**
|
||||
* Crée une reqette d'insertion en base àpartir du nom de la table et d'un tableau associatif et l'exécute
|
||||
* @param string $tableName
|
||||
* @param array $data
|
||||
* @return int lastInsertId
|
||||
*/
|
||||
public static function insert(string $tableName, array $data){
|
||||
$req = BDD::instance()->prepare('INSERT INTO ' . $tableName . ' (' . implode(', ', array_keys($data)) . ')
|
||||
VALUES (' . ':'. implode(', :', array_keys($data)) . ')');
|
||||
$req->execute($data);
|
||||
return BDD::lastInsertId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Met à jour les données d'une ligne d'un table données
|
||||
* @param string $tableName
|
||||
* @param array $data
|
||||
* @param string $idColumn
|
||||
* @param int $idValue
|
||||
*/
|
||||
public static function update(string $tableName, array $data, string $idColumn, int $idValue){
|
||||
$reqStr = 'UPDATE ' . $tableName . ' SET ';
|
||||
$lastKey = endKey($data);
|
||||
foreach ($data as $key => $value){
|
||||
$reqStr .= $key.' = :'.$key;
|
||||
if($key != $lastKey) {
|
||||
$reqStr .= ', ';
|
||||
}
|
||||
}
|
||||
$reqStr .= ' WHERE '.$idColumn.' = :'.$idColumn;
|
||||
$data[$idColumn] = $idValue;
|
||||
|
||||
//echo $reqStr; exit();
|
||||
|
||||
$req = BDD::instance()->prepare($reqStr);
|
||||
$req->execute($data);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Reference in New Issue
Block a user