aboutsummaryrefslogtreecommitdiff
path: root/class/class.app.php
diff options
context:
space:
mode:
authorvincent-peugnet <v.peugnet@free.fr>2018-03-17 22:15:58 +0100
committervincent-peugnet <v.peugnet@free.fr>2018-03-17 22:15:58 +0100
commitbd20965314da58dd81a19ed20e9b539b7045257a (patch)
tree4fb453918810e24037fe58c097f10c7d36d3d9fb /class/class.app.php
parenta8d40a4ac2f73da9509bde6c8337c13042cc6992 (diff)
downloadwcms-bd20965314da58dd81a19ed20e9b539b7045257a.tar.gz
wcms-bd20965314da58dd81a19ed20e9b539b7045257a.zip
class stables
Diffstat (limited to 'class/class.app.php')
-rw-r--r--class/class.app.php108
1 files changed, 59 insertions, 49 deletions
diff --git a/class/class.app.php b/class/class.app.php
index 056e65b..d9867d3 100644
--- a/class/class.app.php
+++ b/class/class.app.php
@@ -4,69 +4,79 @@ class App
{
private $bdd;
- public function __construct()
+ public function __construct($config)
{
- // try {
- // $this->bdd = new PDO('mysql:host=' . $config['host'] . ';dbname=' . $config['dbname'] . ';charset=utf8', $config['user'], $config['password']);
- // } catch (Exeption $e) {
- // die('Erreur : ' . $e->getMessage());
- // }
-
try {
- $this->bdd = new PDO('mysql:host=localhost;dbname=wcms;charset=utf8', 'root', '');
+ $this->bdd = new PDO('mysql:host=' . $config['host'] . ';dbname=' . $config['dbname'] . ';charset=utf8', $config['user'], $config['password']);
} catch (Exeption $e) {
die('Erreur : ' . $e->getMessage());
}
+
+ // try {
+ // $this->bdd = new PDO('mysql:host=localhost;dbname=wcms;charset=utf8', 'root', '');
+ // } catch (Exeption $e) {
+ // die('Erreur : ' . $e->getMessage());
+ // }
}
public function add(Art $art)
{
-// tester l'existence de l'id
- $now = new DateTimeImmutable(null, timezone_open("Europe/Paris"));
-
- $q = $this->bdd->prepare('INSERT INTO art(id, titre, soustitre, intro, tag, datecreation, datemodif, css, html, secure, couleurtext, couleurbkg, couleurlien) VALUES(:id, :titre, :soustitre, :intro, :tag, :datecreation, :datemodif, :css, :html, :secure, :couleurtext, :couleurbkg, :couleurlien)');
- $q->bindValue(':id', $art->id());
- $q->bindValue(':titre', $art->titre());
- $q->bindValue(':soustitre', $art->soustitre());
- $q->bindValue(':intro', $art->intro());
- $q->bindValue(':tag', $art->tag());
- $q->bindValue(':datecreation', $now->format('Y-m-d H:i:s'));
- $q->bindValue(':datemodif', $now->format('Y-m-d H:i:s'));
- $q->bindValue(':css', $art->css());
- $q->bindValue(':html', $art->html());
- $q->bindValue(':secure', $art->secure());
- $q->bindValue(':couleurtext', $art->couleurtext());
- $q->bindValue(':couleurbkg', $art->couleurbkg());
- $q->bindValue(':couleurlien', $art->couleurlien());
-
- $q->execute();
+ if ($this->exist($art->id())) {
+ echo '<p>cet id existe deja</p>';
+ } else {
+
+ $now = new DateTimeImmutable(null, timezone_open("Europe/Paris"));
+
+ $q = $this->bdd->prepare('INSERT INTO art(id, titre, soustitre, intro, tag, datecreation, datemodif, css, html, secure, couleurtext, couleurbkg, couleurlien) VALUES(:id, :titre, :soustitre, :intro, :tag, :datecreation, :datemodif, :css, :html, :secure, :couleurtext, :couleurbkg, :couleurlien)');
+
+ $q->bindValue(':id', $art->id());
+ $q->bindValue(':titre', $art->titre());
+ $q->bindValue(':soustitre', $art->soustitre());
+ $q->bindValue(':intro', $art->intro());
+ $q->bindValue(':tag', $art->tag());
+ $q->bindValue(':datecreation', $now->format('Y-m-d H:i:s'));
+ $q->bindValue(':datemodif', $now->format('Y-m-d H:i:s'));
+ $q->bindValue(':css', $art->css());
+ $q->bindValue(':html', $art->html());
+ $q->bindValue(':secure', $art->secure());
+ $q->bindValue(':couleurtext', $art->couleurtext());
+ $q->bindValue(':couleurbkg', $art->couleurbkg());
+ $q->bindValue(':couleurlien', $art->couleurlien());
+
+ $q->execute();
+ }
}
public function delete(Art $art)
{
- $this->bdd->exec('DELETE FROM art WHERE id = ' . $art->id());
+ $req = $this->bdd->prepare('DELETE FROM art WHERE id = :id ');
+ $req->execute(array('id' => $art->id()));
+ $req->closeCursor();
}
public function get($id)
{
- $q = $this->bdd->query('SELECT * FROM art WHERE id = ' . $id);
- $donnees = $q->fetch(PDO::FETCH_ASSOC);
+ $req = $this->bdd->prepare('SELECT * FROM art WHERE id = :id ');
+ $req->execute(array('id' => $id));
+ $donnees = $req->fetch(PDO::FETCH_ASSOC);
return new Art($donnees);
+
+ $req->closeCursor();
+
}
public function getlist()
{
- $listart = [];
+ $list = [];
- $q = $this->bdd->query('SELECT titre, soustitre, intro FROM art ORDER BY titre');
-
- while ($donnees = $q->fetch(PDO::FETCH_ASSOC)) {
- $listart[] = new art($donnees);
+ $req = $this->bdd->query('SELECT * FROM art ORDER BY id');
+ while ($donnees = $req->fetch(PDO::FETCH_ASSOC))
+ {
+ $list[] = new Art($donnees);
}
-
- return $listart;
+ return $list;
}
public function count()
@@ -74,33 +84,33 @@ class App
return $this->bdd->query('SELECT COUNT(*) FROM art')->fetchColumn();
}
- // public function exist($id)
- // {
- // $r = $this->bdd->query('SELECT COUNT(*) AS art FROM art WHERE id = '.$id);
- // return $donnees = $r->fetch(PDO::FETCH_ASSOC);
- // }
-
public function exist($id)
{
- return $this->bdd->query('SELECT COUNT(*) FROM art WHERE id = ' . $id)->fetchColumn();
+ $req = $this->bdd->prepare('SELECT COUNT(*) FROM art WHERE id = :id ');
+ $req->execute(array('id' => $id));
+ $donnees = $req->fetch(PDO::FETCH_ASSOC);
+
+ return (bool)$donnees['COUNT(*)'];
}
public function update(Art $art)
{
- $q = $this->bdd->prepare('UPDATE art SET titre = :titre, soustitre = :soustitre, intro = :intro, tag = :tag, datecreation = :datecreation, datemodif = :datemodif, cass = :css, html = :html, secure = :secure, couleurtext = :couleurtext, couleurbkg = :couleurbkg, couleurlien = :couleurlien WHERE id = :id');
+ $now = new DateTimeImmutable(null, timezone_open("Europe/Paris"));
+
+ $q = $this->bdd->prepare('UPDATE art SET titre = :titre, soustitre = :soustitre, intro = :intro, tag = :tag, datecreation = :datecreation, datemodif = :datemodif, css = :css, html = :html, secure = :secure, couleurtext = :couleurtext, couleurbkg = :couleurbkg, couleurlien = :couleurlien WHERE id = :id');
$q->bindValue(':id', $art->id());
$q->bindValue(':titre', $art->titre());
$q->bindValue(':soustitre', $art->soustitre());
$q->bindValue(':intro', $art->intro());
$q->bindValue(':tag', $art->tag());
- $q->bindValue(':datecreation', $art->datecreation());
- $q->bindValue(':datemodif', $art->datemodif());
+ $q->bindValue(':datecreation', $art->datecreation()->format('Y-m-d H:i:s'));
+ $q->bindValue(':datemodif', $now->format('Y-m-d H:i:s'));
$q->bindValue(':css', $art->css());
$q->bindValue(':html', $art->html());
$q->bindValue(':secure', $art->secure(), PDO::PARAM_INT);
- $q->bindValue(':couleurtext', $art->tag());
- $q->bindValue(':couleurbkgt', $art->couleurbkgt());
+ $q->bindValue(':couleurtext', $art->couleurtext());
+ $q->bindValue(':couleurbkg', $art->couleurbkg());
$q->bindValue(':couleurlien', $art->couleurlien());
$q->execute();