Reange and optimize

This commit is contained in:
Mathieu Sanchez
2017-12-19 23:42:09 +01:00
parent cbe1959e4f
commit cb635858f1
25 changed files with 7915 additions and 7839 deletions

View File

@ -6,36 +6,35 @@ use Exception;
use PDO;
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
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;
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
];
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());
}
}
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 instance() {
return self::$bdd;
}
public static function lastInsertId() {
return self::$bdd->lastInsertId();
}
public static function lastInsertId() {
return self::$bdd->lastInsertId();
}
}
?>

View File

@ -4,10 +4,10 @@ namespace base\Model;
abstract class BDTables {
// const NOM = 'bd_nom'
// Ex : const ABONNEMENT = 'abonnement';
// const NOM = 'bd_nom'
// Ex : const ABONNEMENT = 'abonnement';
const LOGS = "logs";
const LOGS = "logs";
}
?>

File diff suppressed because it is too large Load Diff

View File

@ -6,191 +6,179 @@ use PDO;
class Logs {
public $id;
public $level;
public $message;
public $file;
public $line;
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'
];
public $id;
public $level;
public $message;
public $file;
public $line;
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|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 $level = null, string $message = null, string $file = null, string $line = null, string $date = null)
{
if ($id === NULL) {
return;
}
$this->id = $id;
$this->level = $level;
$this->message = $message;
$this->file = $file;
$this->line = $line;
$this->date = $date;
}
/**
* Logs constructor.
*
* @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 $level = null, string $message = null, string $file = null, string $line = null, string $date = null ) {
if ( $id === null ) {
return;
}
$this->id = $id;
$this->level = $level;
$this->message = $message;
$this->file = $file;
$this->line = $line;
$this->date = $date;
}
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))
]);
}
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 ) )
] );
}
/**
* 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 *
/**
* 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 *
FROM ' . BDTables::LOGS . '
ORDER BY date DESC
LIMIT :limit');
$req->bindValue('limit', $limit, PDO::PARAM_INT);
$req->execute();
$return = [];
LIMIT :limit' );
$req->bindValue( 'limit', $limit, PDO::PARAM_INT );
$req->execute();
$return = [];
foreach ($req->fetchAll() as $l){
$log = new Logs($l['id'], $l['level'], $l['message'], $l['file'], $l['line'], $l['date']);
$return[] = $log;
}
foreach ( $req->fetchAll() as $l ) {
$log = new Logs( $l['id'], $l['level'], $l['message'], $l['file'], $l['line'], $l['date'] );
$return[] = $log;
}
return $return;
}
return $return;
}
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @return int
*/
public function getId() {
return $this->id;
}
/**
* @param int $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @param int $id
*/
public function setId( $id ) {
$this->id = $id;
}
/**
* @return string
*/
public function getLevel()
{
return $this->level;
}
/**
* @return string
*/
public function getLevel() {
return $this->level;
}
/**
* @param string $level
*/
public function setLevel($level)
{
$this->level = $level;
}
/**
* @param string $level
*/
public function setLevel( $level ) {
$this->level = $level;
}
/**
* @return string
*/
public function getMessage()
{
return htmlspecialchars($this->message);
}
/**
* @return string
*/
public function getMessage() {
return htmlspecialchars( $this->message );
}
/**
* @param string $message
*/
public function setMessage($message)
{
$this->message = $message;
}
/**
* @param string $message
*/
public function setMessage( $message ) {
$this->message = $message;
}
/**
* @return string
*/
public function getFile()
{
return htmlspecialchars($this->file);
}
/**
* @return string
*/
public function getFile() {
return htmlspecialchars( $this->file );
}
/**
* @param string $file
*/
public function setFile($file)
{
$this->file = $file;
}
/**
* @param string $file
*/
public function setFile( $file ) {
$this->file = $file;
}
/**
* @return string
*/
public function getLine()
{
return htmlspecialchars($this->line);
}
/**
* @return string
*/
public function getLine() {
return htmlspecialchars( $this->line );
}
/**
* @param string $line
*/
public function setLine($line)
{
$this->line = $line;
}
/**
* @param string $line
*/
public function setLine( $line ) {
$this->line = $line;
}
/**
* @return string
*/
public function getDate()
{
return $this->date;
}
/**
* @return string
*/
public function getDate() {
return $this->date;
}
/**
* @param string $date
*/
public function setDate($date)
{
$this->date = $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]);
}
/**
* Retourne le type d'erreur en string (label)
* @return string
*/
public function getErrorLabel() {
return htmlspecialchars( self::ERROR_LEVEL[ $this->level ] );
}
}
?>

View File

@ -4,43 +4,47 @@ 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();
}
/**
* 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 );
/**
* 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;
return BDD::lastInsertId();
}
//echo $reqStr; exit();
/**
* 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;
$req = BDD::instance()->prepare($reqStr);
$req->execute($data);
}
//echo $reqStr; exit();
$req = BDD::instance()->prepare( $reqStr );
$req->execute( $data );
}
}
?>