Add a lot

This commit is contained in:
2018-11-27 13:59:21 +09:00
parent d9e043c468
commit 561bfbf475
37 changed files with 8589 additions and 7952 deletions

View File

@ -6,35 +6,43 @@ use Exception;
use PDO;
class BDD {
const SQL_SERVER = 'sql.sanchez-mathieu.fr'; // BDD Server
const SQL_LOGIN = 'why7n0_contact'; // BDD Login
const SQL_PASSWORD = 'fC3c87Gy'; // BDD Password
const SQL_DB = 'why7n0_contact'; // BDD Name
private static $bdd;
// Server BDD
// const SQL_SERVER = 'sql.sanchez-mathieu.fr'; // BDD Server
// const SQL_LOGIN = 'why7n0_contact'; // BDD Login
// const SQL_PASSWORD = 'fC3c87Gy'; // BDD Password
// const SQL_DB = 'why7n0_contact'; // BDD Name
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
];
// Local BDD
const SQL_SERVER = 'localhost'; // BDD Server
const SQL_LOGIN = 'root'; // BDD Login
const SQL_PASSWORD = ''; // BDD Password
const SQL_DB = 'contact'; // BDD Name
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() );
}
}
private static $bdd;
public static function instance() {
return self::$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 static function lastInsertId() {
return self::$bdd->lastInsertId();
}
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();
}
}
?>

View File

@ -4,10 +4,11 @@ namespace CAUProject3Contact\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";
const CONTACT = "contact";
}
?>

220
src/Model/Contact.php Normal file
View File

@ -0,0 +1,220 @@
<?php
namespace CAUProject3Contact\Model;
class Contact {
public $id;
public $firstName;
public $lastName;
public $surname;
public $email;
public $address;
public $phoneNumber;
public $birthday;
// Constructors
public function __construct( int $id = null, string $firstName = null, string $lastName = null,
string $surname = null, string $email = null, string $address = null,
string $phoneNumber = null, string $birthday = null ) {
if ( $id === null || $firstName === null || $lastName === null ) {
return;
}
$this->id = $id;
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->surname = $surname;
$this->email = $email;
$this->address = $address;
$this->phoneNumber = $phoneNumber;
$this->birthday = $birthday;
}
public static function getById( int $id ) {
$req = BDD::instance()->prepare( "SELECT * FROM " . BDTables::CONTACT .
" WHERE `id` = :id" );
$req->execute( [ "id" => $id ] );
$d = $req->fetch();
return new Contact( $d[ "id" ], $d[ "first_name" ], $d[ "last_name" ], $d[ "surname" ],
$d[ "email" ], $d[ "address" ], $d[ "phone_number" ], $d[ "birthday" ] );
}
// Getters
public static function insertNewContact( string $firstName, string $lastName, string $surname = null,
string $email = null,
string $address = null, string $phoneNumber = null,
string $birthday = null ) {
$data = [
"first_name" => $firstName,
"last_name" => $lastName,
];
if ( $surname !== null && $surname !== "" ) {
$data[ "surname" ] = $surname;
}
if ( $email !== null && $email !== "" ) {
$data[ "email" ] = $email;
}
if ( $address !== null && $address !== "" ) {
$data[ "address" ] = $address;
}
if ( $phoneNumber !== null && $phoneNumber !== "" ) {
$data[ "phone_number" ] = $phoneNumber;
}
if ( $birthday !== null && $birthday !== "" ) {
$data[ "birthday" ] = date( "Y-m-d", strtotime( $birthday ) );
}
return Model::insert( BDTables::CONTACT, $data );
}
public static function deleteContact( int $id ) {
Model::delete( BDTables::CONTACT, [ "id" => $id ] );
}
public static function getAllContact(): array {
$contacts = [];
$req = BDD::instance()->prepare( "SELECT * FROM " . BDTables::CONTACT );
$req->execute();
foreach ( $req->fetchAll() as $c ) {
$contacts[] = new Contact( $c[ "id" ], $c[ "first_name" ], $c[ "last_name" ], $c[ "surname" ],
$c[ "email" ], $c[ "address" ], $c[ "phone_number" ], $c[ "birthday" ] );
}
return ( count( $contacts ) > 0 ? $contacts : null );
}
public static function search( string $query ) {
$result = [];
$words = explode( " ", cleanString( $query ) );
$q1 = $q2 = $q3 = $q4 = "SELECT * FROM `" . BDTables::CONTACT . "` WHERE ";
$lastKey = endKey( $words );
foreach ( $words as $key => $word ) {
$normal = self::getQuerySearch( $word, [ "first_name", "last_name", "surname" ] );;
$hard = self::getQuerySearch( $word, [ "email", "address", "phone_number" ] );
$q1 .= $normal;
$q2 .= $normal;
$q3 .= $hard;
$q4 .= $hard;
if ( $key != $lastKey ) {
$q1 .= " AND ";
$q2 .= " OR ";
$q3 .= " AND ";
$q4 .= " OR ";
}
}
$req1 = BDD::instance()->prepare( $q1 );
$req2 = BDD::instance()->prepare( $q2 );
$req3 = BDD::instance()->prepare( $q3 );
$req4 = BDD::instance()->prepare( $q4 );
$req1->execute();
$req2->execute();
$req3->execute();
$req4->execute();
$tmp1 = $req1->fetchAll();
$tmp2 = filterArrays( $tmp1, $req2->fetchAll() );
$tmp3 = filterArrays( $tmp1, filterArrays( $tmp2, $req3->fetchAll() ) );
$tmp4 = filterArrays( $tmp1, filterArrays( $tmp2, filterArrays( $tmp3, $req4->fetchAll() ) ) );
if ( count( $tmp1 ) > 0 || count( $tmp2 ) > 0 || count( $tmp3 ) > 0 || count( $tmp4 ) > 0 ) {
$result[ "1" ] = ( count( $tmp1 ) > 0 ? $tmp1 : null );
$result[ "2" ] = ( count( $tmp2 ) > 0 ? $tmp2 : null );
$result[ "3" ] = ( count( $tmp3 ) > 0 ? $tmp3 : null );
$result[ "4" ] = ( count( $tmp4 ) > 0 ? $tmp4 : null );
return $result;
}
return null;
}
private static function getQuerySearch( string $word, array $fields ): string {
$str = '';
$i = 0;
foreach ( $fields as $field ) {
if ( $i === 0 ) {
$str .= "(";
} else {
$str .= " OR ";
}
$str .= "`" . $field . "` LIKE '%" . $word . "%'";
$i++;
}
$str .= ')';
return $str;
}
/**
* @return string
*/
public function getFirstName(): string {
return $this->firstName;
}
/**
* @return string
*/
public function getLastName(): string {
return $this->lastName;
}
/**
* @return string
*/
public function getSurname() {
return $this->surname;
}
// Method
/**
* @return string
*/
public function getEmail() {
return $this->email;
}
// Static functions
/**
* @return string
*/
public function getAddress() {
return $this->address;
}
/**
* @return string
*/
public function getPhoneNumber() {
return $this->phoneNumber;
}
/**
* @return string
*/
public function getBirthday() {
return $this->birthday;
}
public function updateContact( array $data ) {
foreach ( $data as $key => $value ) {
$this->{$key} = $value;
}
Model::update( BDTables::CONTACT, $data, "id", $this->getId() );
}
/**
* @return int
*/
public function getId(): int {
return $this->id;
}
}
?>

File diff suppressed because it is too large Load Diff

View File

@ -6,179 +6,180 @@ use PDO;
class Logs {
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'
];
public $id;
public $level;
public $message;
public $file;
public $line;
public $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;
}
/**
* 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 = [];
$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,47 +4,63 @@ namespace CAUProject3Contact\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 ) ) . ')
/**
* 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 );
$req->execute( $data );
return BDD::lastInsertId();
}
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;
/**
* 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();
//echo $reqStr; exit();
$req = BDD::instance()->prepare( $reqStr );
$req->execute( $data );
}
public static function delete( string $tableName, array $conditions ) {
$reqStr = 'DELETE FROM ' . $tableName . ' WHERE ';
$lastKey = endKey( $conditions );
foreach ( $conditions as $key => $value ) {
$reqStr .= $key . ' = :' . $key;
if ( $key != $lastKey ) {
$reqStr .= ' AND ';
}
}
$req = BDD::instance()->prepare( $reqStr );
$req->execute( $conditions );
}
$req = BDD::instance()->prepare( $reqStr );
$req->execute( $data );
}
}
?>