Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once 'controllers/controller_base.php';
- require_once 'models/model_user.php';
- require_once 'models/model_liste.php';
- require_once 'global/config.php';
- class Controller_Liste extends Controller_Base
- {
- public function __construct()
- {}
- public function index()
- {
- $this->render_view('gestListe');
- }
- public function creerListe()
- {
- if($_SERVER['REQUEST_METHOD']=='POST')
- {
- if(!empty($_POST['liste_nom']))
- {
- try
- {
- $db = new PDO(SQL_DSN, SQL_USERNAME, SQL_PASSWORD);
- }
- catch (Exception $e)
- {
- $c = new Controller_Base();
- http_response_code(500);
- $c->render_view('errors/500');
- exit;
- }
- $l = new Liste($_POST['liste_nom']);
- $idListe=$l->ajoutBase($db,$_SESSION['id']);
- $l->setId($idListe);
- $this->message("success","Votre liste a bien été créée vous pouvez maintenant la modifier");
- $this->render_view('gestListe');
- }
- else
- {
- $this->message("error","Veuillez donner un titre à votre liste");
- $this->render_view('newListe');
- }
- }
- else
- {
- $this->render_view('newListe');
- }
- }
- public function delListe($id)
- {
- try
- {
- $db = new PDO(SQL_DSN, SQL_USERNAME, SQL_PASSWORD);
- }
- catch (Exception $e)
- {
- $c = new Controller_Base();
- http_response_code(500);
- $c->render_view('errors/500');
- exit;
- }
- Liste::delListe($db,$id,$_SESSION['id']);
- $this->message("success","Votre lise a bien été supprimée");
- $this->redirect('liste/gest');
- }
- public function gest()
- {
- $this->render_view('gestListe');
- }
- public function partageListe($idListe)
- {
- $_SESSION['idListePartage']=$idListe;
- try
- {
- $db = new PDO(SQL_DSN, SQL_USERNAME, SQL_PASSWORD);
- }
- catch (Exception $e)
- {
- $c = new Controller_Base();
- http_response_code(500);
- $c->render_view('errors/500');
- exit;
- }
- if($_SERVER['REQUEST_METHOD']=='POST')
- {
- if(!empty($_POST['log_User']))
- {
- $_POST['log_User']=strtolower($_POST['log_User']);
- $u=new User($_POST['log_User'],'mdp');
- if($u->dansBase($db))
- {
- Liste::partageListe($db,$idListe,$_POST['log_User']);
- $this->message("success","Votre liste de course a bien été partagé");
- $this->redirect('liste/gest');
- }
- else
- {
- $this->message("error","Veuillez saisir un utilisateur éxistant");
- $this->render_view('partage');
- }
- }
- else
- {
- $this->message("error","Veuillez saisir un nom d'utilisateur");
- $this->render_view('partage');
- }
- }
- else
- {
- $this->render_view('partage');
- }
- }
- public function modifListe($idListe)
- {
- $_SESSION['idListeModif']=$idListe;
- $this->render_view('modifListe');
- }
- public function modifTitre()
- {
- try
- {
- $db = new PDO(SQL_DSN, SQL_USERNAME, SQL_PASSWORD);
- }
- catch (Exception $e)
- {
- $c = new Controller_Base();
- http_response_code(500);
- $c->render_view('errors/500');
- exit;
- }
- if(!empty($_POST['liste_nom']))
- {
- Liste::modifTitre($db,$_SESSION['idListeModif'],$_POST['liste_nom']);
- $this->message("success","Le titre de votre liste a bien été changé");
- $this->redirect('liste/modifListe/'.$_SESSION['idListeModif']);
- }
- else
- {
- $this->message("error","Veuillez saisir un titre pour la liste");
- $this->redirect('liste/modifListe/'.$_SESSION['idListeModif']);
- }
- }
- }
- ?>
- <?php
- require_once 'controllers/controller_base.php';
- require_once 'models/model_user.php';
- require_once 'models/model_liste.php';
- require_once 'models/model_produit.php';
- require_once 'global/config.php';
- class Controller_Produit extends Controller_Base
- {
- public function __construct()
- {}
- public function index()
- {
- $this->render_view('gestListe');
- }
- public function creerProd()
- {
- try
- {
- $db = new PDO(SQL_DSN, SQL_USERNAME, SQL_PASSWORD);
- }
- catch (Exception $e)
- {
- $c = new Controller_Base();
- http_response_code(500);
- $c->render_view('errors/500');
- exit;
- }
- if(!empty($_POST['prod_nom']))
- {
- Produit::ajoutBase($db,$_SESSION['idListeModif'],$_POST['prod_nom']);
- $this->message("success","Votre produit a bien été ajouté à la liste");
- $this->redirect('liste/modifListe/'.$_SESSION['idListeModif']);
- }
- else
- {
- $this->message("error","Veuillez saisir un nom pour le produit");
- $this->redirect('liste/modifListe/'.$_SESSION['idListeModif']);
- }
- }
- public function delProd($id)
- {
- try
- {
- $db = new PDO(SQL_DSN, SQL_USERNAME, SQL_PASSWORD);
- }
- catch (Exception $e)
- {
- $c = new Controller_Base();
- http_response_code(500);
- $c->render_view('errors/500');
- exit;
- }
- Produit::delProd($db,$id);
- $this->message("success","Votre produit a bien été supprimé de la liste");
- $this->redirect('liste/modifListe/'.$_SESSION['idListeModif']);
- }
- public function modifEtat($id)
- {
- try
- {
- $db = new PDO(SQL_DSN, SQL_USERNAME, SQL_PASSWORD);
- }
- catch (Exception $e)
- {
- $c = new Controller_Base();
- http_response_code(500);
- $c->render_view('errors/500');
- exit;
- }
- Produit::changerEtat($db,$id);
- $this->redirect('liste/modifListe/'.$_SESSION['idListeModif']);
- }
- }
- ?>
- <?php
- require_once 'controllers/controller_base.php';
- require_once 'models/model_user.php';
- require_once 'global/config.php';
- class Controller_User extends Controller_Base
- {
- public function __construct()
- {}
- public function index()
- {
- $this->render_view('home');
- }
- public function login()
- {
- if($_SERVER['REQUEST_METHOD']=='POST')
- {
- if(!empty($_POST['user_id']) && !empty($_POST['user_pswd']))
- {
- $u = new user($_POST['user_id'],$_POST['user_pswd']);
- try {
- $db = new PDO(SQL_DSN, SQL_USERNAME, SQL_PASSWORD);
- }
- catch (Exception $e) {
- $c = new Controller_Base();
- http_response_code(500);
- $c->render_view('errors/500');
- exit;
- }
- $id=$u->connect($db);
- if(isset($id))
- {
- $u->setId($id);
- $_SESSION['user']=$u;
- $_SESSION['login']=$u->getLogin();
- $_SESSION['mdp']=$u->getMdp();
- $_SESSION['id']=$u->getId();
- $this->message("success","Vous êtes connecté ".$_SESSION['user']->getLogin());
- $this->render_view('home');
- }
- else
- {
- $this->message("error","Veuillez vérifier vos identifiants");
- $this->render_view('signin');
- }
- }
- else
- {
- $this->message("error","veuillez remplir les champs identifiant et mot de passe");
- $this->render_view('signin');
- }
- }
- else
- {
- $this->render_view('signin');
- }
- }
- public function logout()
- {
- session_destroy();
- $this->redirect();
- }
- public function compte()
- {
- $this->render_view('compte');
- }
- public function modifMdp()
- {
- try
- {
- $db = new PDO(SQL_DSN, SQL_USERNAME, SQL_PASSWORD);
- }
- catch (Exception $e) {
- $c = new Controller_Base();
- http_response_code(500);
- $c->render_view('errors/500');
- exit;
- }
- $password=$_POST['user_pswd'];
- $passwordConf=$_POST['user_pswd_conf'];
- if($password==$passwordConf)//si les 2 MDP sont ok on crée l'instance user
- {
- if(isset($_SESSION['user']))
- {
- $u= new user($_SESSION['login'],$_SESSION['mdp']);
- $u->modifierMdp($db,$password);
- $this->message("success","Votre mot de passe a bien été modifié");
- $this->compte();
- }
- }
- else
- {
- $this->message("error","Assurez vous de saisir deux fois le même mot de passe");
- $this->compte();
- }
- }
- public function delUser()
- {
- try
- {
- $db = new PDO(SQL_DSN, SQL_USERNAME, SQL_PASSWORD);
- }
- catch (Exception $e) {
- $c = new Controller_Base();
- http_response_code(500);
- $c->render_view('errors/500');
- exit;
- }
- if(isset($_SESSION['user']))
- {
- $u= new user($_SESSION['login'],$_SESSION['mdp']);
- $u->supprimerUser($db);
- $this->logout();
- }
- else {
- $this->message("error","Il y a eu un problème au moment de la suppression de votre compte");
- $this->compte();
- }
- }
- public function ajoutUser()
- {
- try
- {
- $db = new PDO(SQL_DSN, SQL_USERNAME, SQL_PASSWORD);
- }
- catch (Exception $e) {
- $c = new Controller_Base();
- http_response_code(500);
- $c->render_view('errors/500');
- exit;
- }
- if($_SERVER['REQUEST_METHOD']=='POST')
- {
- if( !empty($_POST['user_id']) && !empty($_POST['user_pswd']) && !empty($_POST['user_pswd_conf']))
- {
- $password=$_POST['user_pswd'];
- $passwordConf=$_POST['user_pswd_conf'];
- if($password==$passwordConf)//si les 2 MDP sont ok on crée l'instance user
- {
- $u=new user($_POST['user_id'],$password);
- if(!$u->dansBase($db))
- {
- $id=$u->ajoutBase($db);
- $u->setId($id);
- $_SESSION['user']=$u;
- $_SESSION['id']=$u->getId();
- $_SESSION['login']=$u->getLogin();
- $_SESSION['mdp']=$u->getMdp();
- $this->message("success","Votre compte a été créé avec succès ".$_SESSION['user']->getLogin());
- $this->render_view('home');
- }
- else
- {
- $this->message("error","Identifiant déjà utilisé");
- $this->render_view('signup');
- }
- }
- else
- {
- $this->message("error","Veuillez saisir deux mot de passe identique");
- $this->render_view('signup');
- }
- }
- else
- {
- $this->message("error","veuillez remplir tout les champs");
- $this->render_view('signup');
- }
- }
- else
- {
- $this->render_view('signup');
- }
- }
- }
- ?>
- <?php
- class Liste
- {
- private $_idListe;
- private $_nom;
- public function setId($id)
- {
- $this->_idListe=$id;
- }
- public function setNom($nom)
- {
- $this->_nom=$nom;
- }
- public function __construct($nom)
- {
- $this->setNom($nom);
- }
- public function getId()
- {
- return $this->_idListe;
- }
- public function getNom()
- {
- return $this->_nom;
- }
- public function ajoutBase($db,$idUser)
- {
- $statement=
- $db->prepare('INSERT INTO `akirrmann`.`Listes` (`id_liste`, `id_user`, `nom`) VALUES (NULL, :a, :b);');
- $statement->bindValue(':a', $idUser , PDO::PARAM_STR);
- $statement->bindValue(':b', $this->_nom, PDO::PARAM_STR);
- $statement->execute();
- $statement=
- $db->prepare('Select max(`id_liste`) from `Listes`');
- $statement->execute();
- $res=$statement->fetch();
- return $res[0];
- }
- public static function delListe($db,$idListe,$idUser)
- {
- $statement=
- $db->prepare('DELETE FROM `Listes` WHERE `id_liste`=:a AND `id_user`=:b');
- $statement->bindValue(':a',$idListe,PDO::PARAM_STR);
- $statement->bindValue(':b',$idUser,PDO::PARAM_STR);
- $statement->execute();
- }
- public static function getListesByUser($db,$idUser)//renvoi un tableau contenant les liste de l'user passé en paramètre
- {
- $statement=
- $db->prepare('Select `id_liste`,`nom` FROM `Listes` WHERE `id_user`=:a');
- $statement->bindValue(':a',$idUser,PDO::PARAM_STR);
- $statement->execute();
- while($row=$statement->fetch())
- {
- $li=new Liste($row['nom']);
- $li->setId($row['id_liste']);
- $res[]=$li;
- }
- if(isset($res))
- {
- return $res;
- }
- else
- {
- return null;
- }
- }
- public static function partageListe($db,$idListe,$logUser)//cherhce l'id user le nom de la liste puis ajoute la ligne pour le deuxièem user
- {
- $statement=
- $db->prepare('Select `id` from `Users` WHERE lower(login)=:a');
- $statement->bindvalue(':a',$logUser,PDO::PARAM_STR);
- $statement->execute();
- $idUserPartage=$statement->fetch();
- $statement=
- $db->prepare('Select `nom` from `Listes` WHERE id_liste=:a');
- $statement->bindvalue(':a',$idListe,PDO::PARAM_STR);
- $statement->execute();
- $nomListePartage=$statement->fetch();
- if(isset($idUserPartage['id']) && isset($nomListePartage['nom']))
- {
- $statement=
- $db->prepare('INSERT INTO `akirrmann`.`Listes` (`id_liste`, `id_user`, `nom`) VALUES (:c, :a, :b)');
- $statement->bindValue(':a', $idUserPartage['id'] , PDO::PARAM_STR);
- $statement->bindValue(':b', $nomListePartage['nom'], PDO::PARAM_STR);
- $statement->bindValue(':c', $idListe , PDO::PARAM_STR);
- $statement->execute();
- }
- }
- public static function modifTitre($db,$idListe,$nom)
- {
- $statement=
- $db->prepare('UPDATE `akirrmann`.`Listes` SET `nom` = :a WHERE `Listes`.`id_liste` = :b');
- $statement->bindvalue(':a',$nom,PDO::PARAM_STR);
- $statement->bindvalue(':b',$idListe,PDO::PARAM_STR);
- $statement->execute();
- }
- }
- ?>
- <?php
- class User
- {
- private $_id;
- private $_login;
- private $_mdp;
- public function setId($id)
- {
- $this->_id=$id;
- }
- public function setMdp($mdp)
- {
- $this->_mdp=$mdp;
- }
- public function setLogin($login)
- {
- $this->_login=$login;
- }
- public function __construct($login,$mdp)
- {
- $this->setMdp($mdp);
- $this->setLogin($login);
- }
- public function getId()
- {
- return $this->_id;
- }
- public function getMdp()
- {
- return $this->_mdp;
- }
- public function getLogin()
- {
- return $this->_login;
- }
- public function decrire()
- {
- echo $this->_login." ".$this->_mdp;
- }
- public function Connect($db)
- {
- $statement=
- $db->prepare('Select `id`,`password` from `Users` where lower(login)= :c');
- $statement->bindValue(':c',$this->_login, PDO::PARAM_STR);
- $statement->execute();
- $res=$statement->fetch();
- if($res['password']==$this->_mdp)
- {
- return $res['id'];
- }
- else
- {
- return null;
- }
- }
- public function dansBase($db)
- {
- $statement =
- $db->prepare('Select Count(*) From Users where lower(login)= :c');
- $statement->bindValue(':c', $this->_login, PDO::PARAM_STR);
- $statement->execute();
- $nb = $statement->fetchAll();
- if($nb[0][0]==1)
- {
- return true;
- }
- else return false;
- }
- public function ajoutBase($db)
- {
- $statement=
- $db->prepare('INSERT INTO `akirrmann`.`Users` (`id`, `login`, `password`) VALUES (NULL, :a, :b);');
- $statement->bindValue(':a', $this->_login, PDO::PARAM_STR);
- $statement->bindValue(':b', $this->_mdp, PDO::PARAM_STR);
- $statement->execute();
- $statement=
- $db->prepare('Select `id` from `Users` where lower(login)= :c');
- $statement->bindValue(':c',$this->_login, PDO::PARAM_STR);
- $statement->execute();
- $res=$statement->fetch();
- return $res['id'];
- }
- public function modifierMdp($db,$mdp)
- {
- $statement=
- $db->prepare('UPDATE `Users` SET `password`=:a WHERE lower(login)=:b');
- $statement->bindValue(':a',$mdp, PDO::PARAM_STR);
- $statement->bindValue(':b',$this->_login,PDO::PARAM_STR);
- $statement->execute();
- }
- public function supprimerUser($db)
- {
- $statement=
- $db->prepare('DELETE FROM `Users` WHERE lower(login)=:a');
- $statement->bindValue(':a',$this->_login,PDO::PARAM_STR);
- $statement->execute();
- }
- }
- ?>
- <?php
- class Produit
- {
- private $_id;
- private $_id_liste;
- private $_nom;
- private $_achete;
- public function setId($id)
- {
- $this->_id=$id;
- }
- public function setNom($nom)
- {
- $this->_nom=$nom;
- }
- public function setAchete($achete)
- {
- $this->_achete=$achete;
- }
- public function setIdListe($id)
- {
- $this->_id_liste=$id;
- }
- public function __construct($nom,$achete)
- {
- $this->setNom($nom);
- $this->setAchete($achete);
- }
- public function getId()
- {
- return $this->_id;
- }
- public function getId_liste()
- {
- return $this->_id_liste;
- }
- public function getNom()
- {
- return $this->_nom;
- }
- public function getAchete()
- {
- if($this->_achete=="false")
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- public static function ajoutBase($db,$idListe,$nomProd)
- {
- $statement=
- $db->prepare('INSERT INTO `akirrmann`.`Produits` (`id_prod`, `id_liste`, `nom`, `achete`) VALUES (NULL, :a, :b,:c)');
- $statement->bindValue(':a',$idListe,PDO::PARAM_STR);
- $statement->bindValue(':b',$nomProd,PDO::PARAM_STR);
- $statement->bindValue(':c','false',PDO::PARAM_STR);
- $statement->execute();
- }
- public static function getElementsByListe($db,$idListe)
- {
- $statement=
- $db->prepare('SELECT `id_prod`,`nom`,`achete` FROM `Produits` WHERE `id_liste`=:a');
- $statement->bindValue(':a',$idListe,PDO::PARAM_STR);
- $statement->execute();
- while($row=$statement->fetch())
- {
- $li=new Produit($row['nom'],$row['achete']);
- $li->setId($row['id_prod']);
- $res[]=$li;
- }
- if(isset($res))
- {
- return $res;
- }
- else
- {
- return null;
- }
- }
- public static function delProd($db,$idProd)
- {
- $statement=
- $db->prepare('DELETE FROM `Produits` WHERE `id_prod`=:a');
- $statement->bindValue(':a',$idProd,PDO::PARAM_STR);
- $statement->execute();
- }
- public static function changerEtat($db,$id)
- {
- $statement=
- $db->prepare('SELECT `achete` FROM `Produits` WHERE `id_prod`=:a');
- $statement->bindValue(':a',$id,PDO::PARAM_STR);
- $statement->execute();
- $etat=$statement->fetch();
- if($etat['achete']=='true')
- {
- $statement=
- $db->prepare('UPDATE `akirrmann`.`Produits` SET `achete` = :b WHERE `Produits`.`id_prod` =:a');
- $statement->bindValue(':a',$id,PDO::PARAM_STR);
- $statement->bindValue(':b','false',PDO::PARAM_STR);
- $statement->execute();
- }
- else
- {
- $statement=
- $db->prepare('UPDATE `akirrmann`.`Produits` SET `achete` = :b WHERE `Produits`.`id_prod` =:a');
- $statement->bindValue(':a',$id,PDO::PARAM_STR);
- $statement->bindValue(':b','true',PDO::PARAM_STR);
- $statement->execute();
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement