Advertisement
Guest User

kdo guiksss

a guest
Oct 17th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.85 KB | None | 0 0
  1. <?php
  2. require_once 'controllers/controller_base.php';
  3. require_once 'models/model_user.php';
  4. require_once 'models/model_liste.php';
  5. require_once 'global/config.php';
  6.  
  7. class Controller_Liste extends Controller_Base
  8. {
  9. public function __construct()
  10. {}
  11.  
  12. public function index()
  13. {
  14. $this->render_view('gestListe');
  15. }
  16. public function creerListe()
  17. {
  18. if($_SERVER['REQUEST_METHOD']=='POST')
  19. {
  20. if(!empty($_POST['liste_nom']))
  21. {
  22. try
  23. {
  24. $db = new PDO(SQL_DSN, SQL_USERNAME, SQL_PASSWORD);
  25. }
  26. catch (Exception $e)
  27. {
  28. $c = new Controller_Base();
  29. http_response_code(500);
  30. $c->render_view('errors/500');
  31. exit;
  32. }
  33.  
  34. $l = new Liste($_POST['liste_nom']);
  35. $idListe=$l->ajoutBase($db,$_SESSION['id']);
  36. $l->setId($idListe);
  37.  
  38. $this->message("success","Votre liste a bien été créée vous pouvez maintenant la modifier");
  39. $this->render_view('gestListe');
  40. }
  41. else
  42. {
  43. $this->message("error","Veuillez donner un titre à votre liste");
  44. $this->render_view('newListe');
  45. }
  46. }
  47. else
  48. {
  49. $this->render_view('newListe');
  50. }
  51. }
  52.  
  53. public function delListe($id)
  54. {
  55. try
  56. {
  57. $db = new PDO(SQL_DSN, SQL_USERNAME, SQL_PASSWORD);
  58. }
  59. catch (Exception $e)
  60. {
  61. $c = new Controller_Base();
  62. http_response_code(500);
  63. $c->render_view('errors/500');
  64. exit;
  65. }
  66. Liste::delListe($db,$id,$_SESSION['id']);
  67. $this->message("success","Votre lise a bien été supprimée");
  68. $this->redirect('liste/gest');
  69. }
  70.  
  71. public function gest()
  72. {
  73. $this->render_view('gestListe');
  74. }
  75.  
  76. public function partageListe($idListe)
  77. {
  78. $_SESSION['idListePartage']=$idListe;
  79.  
  80. try
  81. {
  82. $db = new PDO(SQL_DSN, SQL_USERNAME, SQL_PASSWORD);
  83. }
  84. catch (Exception $e)
  85. {
  86. $c = new Controller_Base();
  87. http_response_code(500);
  88. $c->render_view('errors/500');
  89. exit;
  90. }
  91.  
  92. if($_SERVER['REQUEST_METHOD']=='POST')
  93. {
  94. if(!empty($_POST['log_User']))
  95. {
  96. $_POST['log_User']=strtolower($_POST['log_User']);
  97. $u=new User($_POST['log_User'],'mdp');
  98.  
  99. if($u->dansBase($db))
  100. {
  101. Liste::partageListe($db,$idListe,$_POST['log_User']);
  102. $this->message("success","Votre liste de course a bien été partagé");
  103. $this->redirect('liste/gest');
  104. }
  105. else
  106. {
  107. $this->message("error","Veuillez saisir un utilisateur éxistant");
  108. $this->render_view('partage');
  109. }
  110. }
  111. else
  112. {
  113. $this->message("error","Veuillez saisir un nom d'utilisateur");
  114. $this->render_view('partage');
  115. }
  116. }
  117. else
  118. {
  119. $this->render_view('partage');
  120. }
  121. }
  122.  
  123. public function modifListe($idListe)
  124. {
  125. $_SESSION['idListeModif']=$idListe;
  126.  
  127. $this->render_view('modifListe');
  128.  
  129. }
  130.  
  131. public function modifTitre()
  132. {
  133. try
  134. {
  135. $db = new PDO(SQL_DSN, SQL_USERNAME, SQL_PASSWORD);
  136. }
  137. catch (Exception $e)
  138. {
  139. $c = new Controller_Base();
  140. http_response_code(500);
  141. $c->render_view('errors/500');
  142. exit;
  143. }
  144.  
  145. if(!empty($_POST['liste_nom']))
  146. {
  147. Liste::modifTitre($db,$_SESSION['idListeModif'],$_POST['liste_nom']);
  148. $this->message("success","Le titre de votre liste a bien été changé");
  149. $this->redirect('liste/modifListe/'.$_SESSION['idListeModif']);
  150. }
  151. else
  152. {
  153. $this->message("error","Veuillez saisir un titre pour la liste");
  154. $this->redirect('liste/modifListe/'.$_SESSION['idListeModif']);
  155. }
  156.  
  157. }
  158.  
  159. }
  160. ?>
  161.  
  162. <?php
  163. require_once 'controllers/controller_base.php';
  164. require_once 'models/model_user.php';
  165. require_once 'models/model_liste.php';
  166. require_once 'models/model_produit.php';
  167. require_once 'global/config.php';
  168.  
  169. class Controller_Produit extends Controller_Base
  170. {
  171. public function __construct()
  172. {}
  173.  
  174. public function index()
  175. {
  176. $this->render_view('gestListe');
  177. }
  178.  
  179.  
  180. public function creerProd()
  181. {
  182. try
  183. {
  184. $db = new PDO(SQL_DSN, SQL_USERNAME, SQL_PASSWORD);
  185. }
  186. catch (Exception $e)
  187. {
  188. $c = new Controller_Base();
  189. http_response_code(500);
  190. $c->render_view('errors/500');
  191. exit;
  192. }
  193.  
  194.  
  195. if(!empty($_POST['prod_nom']))
  196. {
  197. Produit::ajoutBase($db,$_SESSION['idListeModif'],$_POST['prod_nom']);
  198. $this->message("success","Votre produit a bien été ajouté à la liste");
  199. $this->redirect('liste/modifListe/'.$_SESSION['idListeModif']);
  200. }
  201. else
  202. {
  203. $this->message("error","Veuillez saisir un nom pour le produit");
  204. $this->redirect('liste/modifListe/'.$_SESSION['idListeModif']);
  205. }
  206. }
  207.  
  208. public function delProd($id)
  209. {
  210. try
  211. {
  212. $db = new PDO(SQL_DSN, SQL_USERNAME, SQL_PASSWORD);
  213. }
  214. catch (Exception $e)
  215. {
  216. $c = new Controller_Base();
  217. http_response_code(500);
  218. $c->render_view('errors/500');
  219. exit;
  220. }
  221.  
  222. Produit::delProd($db,$id);
  223. $this->message("success","Votre produit a bien été supprimé de la liste");
  224. $this->redirect('liste/modifListe/'.$_SESSION['idListeModif']);
  225. }
  226.  
  227. public function modifEtat($id)
  228. {
  229. try
  230. {
  231. $db = new PDO(SQL_DSN, SQL_USERNAME, SQL_PASSWORD);
  232. }
  233. catch (Exception $e)
  234. {
  235. $c = new Controller_Base();
  236. http_response_code(500);
  237. $c->render_view('errors/500');
  238. exit;
  239. }
  240.  
  241. Produit::changerEtat($db,$id);
  242. $this->redirect('liste/modifListe/'.$_SESSION['idListeModif']);
  243. }
  244.  
  245. }
  246. ?>
  247.  
  248. <?php
  249. require_once 'controllers/controller_base.php';
  250. require_once 'models/model_user.php';
  251. require_once 'global/config.php';
  252.  
  253. class Controller_User extends Controller_Base
  254. {
  255. public function __construct()
  256. {}
  257.  
  258. public function index()
  259. {
  260. $this->render_view('home');
  261. }
  262.  
  263.  
  264.  
  265. public function login()
  266. {
  267. if($_SERVER['REQUEST_METHOD']=='POST')
  268. {
  269. if(!empty($_POST['user_id']) && !empty($_POST['user_pswd']))
  270. {
  271. $u = new user($_POST['user_id'],$_POST['user_pswd']);
  272. try {
  273. $db = new PDO(SQL_DSN, SQL_USERNAME, SQL_PASSWORD);
  274. }
  275. catch (Exception $e) {
  276. $c = new Controller_Base();
  277. http_response_code(500);
  278. $c->render_view('errors/500');
  279. exit;
  280. }
  281. $id=$u->connect($db);
  282. if(isset($id))
  283. {
  284. $u->setId($id);
  285. $_SESSION['user']=$u;
  286. $_SESSION['login']=$u->getLogin();
  287. $_SESSION['mdp']=$u->getMdp();
  288. $_SESSION['id']=$u->getId();
  289. $this->message("success","Vous êtes connecté ".$_SESSION['user']->getLogin());
  290. $this->render_view('home');
  291.  
  292. }
  293. else
  294. {
  295. $this->message("error","Veuillez vérifier vos identifiants");
  296. $this->render_view('signin');
  297. }
  298. }
  299. else
  300. {
  301. $this->message("error","veuillez remplir les champs identifiant et mot de passe");
  302. $this->render_view('signin');
  303. }
  304. }
  305. else
  306. {
  307. $this->render_view('signin');
  308. }
  309.  
  310. }
  311.  
  312. public function logout()
  313. {
  314. session_destroy();
  315. $this->redirect();
  316. }
  317.  
  318. public function compte()
  319. {
  320. $this->render_view('compte');
  321. }
  322.  
  323. public function modifMdp()
  324. {
  325. try
  326. {
  327. $db = new PDO(SQL_DSN, SQL_USERNAME, SQL_PASSWORD);
  328. }
  329. catch (Exception $e) {
  330. $c = new Controller_Base();
  331. http_response_code(500);
  332. $c->render_view('errors/500');
  333. exit;
  334. }
  335. $password=$_POST['user_pswd'];
  336. $passwordConf=$_POST['user_pswd_conf'];
  337. if($password==$passwordConf)//si les 2 MDP sont ok on crée l'instance user
  338. {
  339. if(isset($_SESSION['user']))
  340. {
  341. $u= new user($_SESSION['login'],$_SESSION['mdp']);
  342. $u->modifierMdp($db,$password);
  343. $this->message("success","Votre mot de passe a bien été modifié");
  344. $this->compte();
  345. }
  346. }
  347. else
  348. {
  349. $this->message("error","Assurez vous de saisir deux fois le même mot de passe");
  350. $this->compte();
  351. }
  352. }
  353.  
  354. public function delUser()
  355. {
  356. try
  357. {
  358. $db = new PDO(SQL_DSN, SQL_USERNAME, SQL_PASSWORD);
  359. }
  360. catch (Exception $e) {
  361. $c = new Controller_Base();
  362. http_response_code(500);
  363. $c->render_view('errors/500');
  364. exit;
  365. }
  366. if(isset($_SESSION['user']))
  367. {
  368. $u= new user($_SESSION['login'],$_SESSION['mdp']);
  369. $u->supprimerUser($db);
  370. $this->logout();
  371. }
  372. else {
  373. $this->message("error","Il y a eu un problème au moment de la suppression de votre compte");
  374. $this->compte();
  375. }
  376. }
  377.  
  378. public function ajoutUser()
  379. {
  380. try
  381. {
  382. $db = new PDO(SQL_DSN, SQL_USERNAME, SQL_PASSWORD);
  383. }
  384. catch (Exception $e) {
  385. $c = new Controller_Base();
  386. http_response_code(500);
  387. $c->render_view('errors/500');
  388. exit;
  389. }
  390. if($_SERVER['REQUEST_METHOD']=='POST')
  391. {
  392. if( !empty($_POST['user_id']) && !empty($_POST['user_pswd']) && !empty($_POST['user_pswd_conf']))
  393. {
  394. $password=$_POST['user_pswd'];
  395. $passwordConf=$_POST['user_pswd_conf'];
  396. if($password==$passwordConf)//si les 2 MDP sont ok on crée l'instance user
  397. {
  398. $u=new user($_POST['user_id'],$password);
  399. if(!$u->dansBase($db))
  400. {
  401. $id=$u->ajoutBase($db);
  402. $u->setId($id);
  403. $_SESSION['user']=$u;
  404. $_SESSION['id']=$u->getId();
  405. $_SESSION['login']=$u->getLogin();
  406. $_SESSION['mdp']=$u->getMdp();
  407. $this->message("success","Votre compte a été créé avec succès ".$_SESSION['user']->getLogin());
  408. $this->render_view('home');
  409. }
  410. else
  411. {
  412. $this->message("error","Identifiant déjà utilisé");
  413. $this->render_view('signup');
  414. }
  415. }
  416. else
  417. {
  418. $this->message("error","Veuillez saisir deux mot de passe identique");
  419. $this->render_view('signup');
  420. }
  421. }
  422. else
  423. {
  424. $this->message("error","veuillez remplir tout les champs");
  425. $this->render_view('signup');
  426. }
  427. }
  428. else
  429. {
  430. $this->render_view('signup');
  431. }
  432. }
  433.  
  434. }
  435. ?>
  436.  
  437. <?php
  438.  
  439. class Liste
  440. {
  441. private $_idListe;
  442. private $_nom;
  443.  
  444.  
  445. public function setId($id)
  446. {
  447. $this->_idListe=$id;
  448. }
  449.  
  450. public function setNom($nom)
  451. {
  452. $this->_nom=$nom;
  453. }
  454.  
  455. public function __construct($nom)
  456. {
  457. $this->setNom($nom);
  458. }
  459.  
  460. public function getId()
  461. {
  462. return $this->_idListe;
  463. }
  464.  
  465. public function getNom()
  466. {
  467. return $this->_nom;
  468. }
  469.  
  470. public function ajoutBase($db,$idUser)
  471. {
  472. $statement=
  473. $db->prepare('INSERT INTO `akirrmann`.`Listes` (`id_liste`, `id_user`, `nom`) VALUES (NULL, :a, :b);');
  474. $statement->bindValue(':a', $idUser , PDO::PARAM_STR);
  475. $statement->bindValue(':b', $this->_nom, PDO::PARAM_STR);
  476. $statement->execute();
  477.  
  478. $statement=
  479. $db->prepare('Select max(`id_liste`) from `Listes`');
  480. $statement->execute();
  481. $res=$statement->fetch();
  482. return $res[0];
  483. }
  484.  
  485. public static function delListe($db,$idListe,$idUser)
  486. {
  487. $statement=
  488. $db->prepare('DELETE FROM `Listes` WHERE `id_liste`=:a AND `id_user`=:b');
  489. $statement->bindValue(':a',$idListe,PDO::PARAM_STR);
  490. $statement->bindValue(':b',$idUser,PDO::PARAM_STR);
  491. $statement->execute();
  492. }
  493.  
  494. public static function getListesByUser($db,$idUser)//renvoi un tableau contenant les liste de l'user passé en paramètre
  495. {
  496. $statement=
  497. $db->prepare('Select `id_liste`,`nom` FROM `Listes` WHERE `id_user`=:a');
  498. $statement->bindValue(':a',$idUser,PDO::PARAM_STR);
  499. $statement->execute();
  500. while($row=$statement->fetch())
  501. {
  502. $li=new Liste($row['nom']);
  503. $li->setId($row['id_liste']);
  504. $res[]=$li;
  505. }
  506. if(isset($res))
  507. {
  508. return $res;
  509. }
  510. else
  511. {
  512. return null;
  513. }
  514. }
  515.  
  516. 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
  517. {
  518. $statement=
  519. $db->prepare('Select `id` from `Users` WHERE lower(login)=:a');
  520. $statement->bindvalue(':a',$logUser,PDO::PARAM_STR);
  521. $statement->execute();
  522. $idUserPartage=$statement->fetch();
  523.  
  524. $statement=
  525. $db->prepare('Select `nom` from `Listes` WHERE id_liste=:a');
  526. $statement->bindvalue(':a',$idListe,PDO::PARAM_STR);
  527. $statement->execute();
  528. $nomListePartage=$statement->fetch();
  529.  
  530. if(isset($idUserPartage['id']) && isset($nomListePartage['nom']))
  531. {
  532. $statement=
  533. $db->prepare('INSERT INTO `akirrmann`.`Listes` (`id_liste`, `id_user`, `nom`) VALUES (:c, :a, :b)');
  534. $statement->bindValue(':a', $idUserPartage['id'] , PDO::PARAM_STR);
  535. $statement->bindValue(':b', $nomListePartage['nom'], PDO::PARAM_STR);
  536. $statement->bindValue(':c', $idListe , PDO::PARAM_STR);
  537. $statement->execute();
  538. }
  539. }
  540.  
  541. public static function modifTitre($db,$idListe,$nom)
  542. {
  543. $statement=
  544. $db->prepare('UPDATE `akirrmann`.`Listes` SET `nom` = :a WHERE `Listes`.`id_liste` = :b');
  545. $statement->bindvalue(':a',$nom,PDO::PARAM_STR);
  546. $statement->bindvalue(':b',$idListe,PDO::PARAM_STR);
  547. $statement->execute();
  548. }
  549.  
  550. }
  551.  
  552. ?>
  553. <?php
  554.  
  555. class User
  556. {
  557. private $_id;
  558. private $_login;
  559. private $_mdp;
  560.  
  561. public function setId($id)
  562. {
  563. $this->_id=$id;
  564. }
  565. public function setMdp($mdp)
  566. {
  567. $this->_mdp=$mdp;
  568. }
  569. public function setLogin($login)
  570. {
  571. $this->_login=$login;
  572. }
  573. public function __construct($login,$mdp)
  574. {
  575. $this->setMdp($mdp);
  576. $this->setLogin($login);
  577. }
  578. public function getId()
  579. {
  580. return $this->_id;
  581. }
  582. public function getMdp()
  583. {
  584. return $this->_mdp;
  585. }
  586. public function getLogin()
  587. {
  588. return $this->_login;
  589. }
  590. public function decrire()
  591. {
  592. echo $this->_login." ".$this->_mdp;
  593. }
  594. public function Connect($db)
  595. {
  596. $statement=
  597. $db->prepare('Select `id`,`password` from `Users` where lower(login)= :c');
  598. $statement->bindValue(':c',$this->_login, PDO::PARAM_STR);
  599. $statement->execute();
  600. $res=$statement->fetch();
  601. if($res['password']==$this->_mdp)
  602. {
  603. return $res['id'];
  604. }
  605. else
  606. {
  607. return null;
  608. }
  609. }
  610.  
  611. public function dansBase($db)
  612. {
  613. $statement =
  614. $db->prepare('Select Count(*) From Users where lower(login)= :c');
  615. $statement->bindValue(':c', $this->_login, PDO::PARAM_STR);
  616. $statement->execute();
  617. $nb = $statement->fetchAll();
  618. if($nb[0][0]==1)
  619. {
  620. return true;
  621. }
  622. else return false;
  623. }
  624.  
  625. public function ajoutBase($db)
  626. {
  627. $statement=
  628. $db->prepare('INSERT INTO `akirrmann`.`Users` (`id`, `login`, `password`) VALUES (NULL, :a, :b);');
  629. $statement->bindValue(':a', $this->_login, PDO::PARAM_STR);
  630. $statement->bindValue(':b', $this->_mdp, PDO::PARAM_STR);
  631. $statement->execute();
  632.  
  633. $statement=
  634. $db->prepare('Select `id` from `Users` where lower(login)= :c');
  635. $statement->bindValue(':c',$this->_login, PDO::PARAM_STR);
  636. $statement->execute();
  637. $res=$statement->fetch();
  638. return $res['id'];
  639. }
  640.  
  641. public function modifierMdp($db,$mdp)
  642. {
  643. $statement=
  644. $db->prepare('UPDATE `Users` SET `password`=:a WHERE lower(login)=:b');
  645. $statement->bindValue(':a',$mdp, PDO::PARAM_STR);
  646. $statement->bindValue(':b',$this->_login,PDO::PARAM_STR);
  647. $statement->execute();
  648. }
  649.  
  650. public function supprimerUser($db)
  651. {
  652. $statement=
  653. $db->prepare('DELETE FROM `Users` WHERE lower(login)=:a');
  654. $statement->bindValue(':a',$this->_login,PDO::PARAM_STR);
  655. $statement->execute();
  656. }
  657. }
  658.  
  659.  
  660. ?>
  661.  
  662. <?php
  663.  
  664. class Produit
  665. {
  666.  
  667. private $_id;
  668. private $_id_liste;
  669. private $_nom;
  670. private $_achete;
  671.  
  672. public function setId($id)
  673. {
  674. $this->_id=$id;
  675. }
  676. public function setNom($nom)
  677. {
  678. $this->_nom=$nom;
  679. }
  680. public function setAchete($achete)
  681. {
  682. $this->_achete=$achete;
  683. }
  684. public function setIdListe($id)
  685. {
  686. $this->_id_liste=$id;
  687. }
  688. public function __construct($nom,$achete)
  689. {
  690. $this->setNom($nom);
  691. $this->setAchete($achete);
  692. }
  693. public function getId()
  694. {
  695. return $this->_id;
  696. }
  697. public function getId_liste()
  698. {
  699. return $this->_id_liste;
  700. }
  701. public function getNom()
  702. {
  703. return $this->_nom;
  704. }
  705. public function getAchete()
  706. {
  707. if($this->_achete=="false")
  708. {
  709. return false;
  710. }
  711. else
  712. {
  713. return true;
  714. }
  715. }
  716.  
  717. public static function ajoutBase($db,$idListe,$nomProd)
  718. {
  719. $statement=
  720. $db->prepare('INSERT INTO `akirrmann`.`Produits` (`id_prod`, `id_liste`, `nom`, `achete`) VALUES (NULL, :a, :b,:c)');
  721. $statement->bindValue(':a',$idListe,PDO::PARAM_STR);
  722. $statement->bindValue(':b',$nomProd,PDO::PARAM_STR);
  723. $statement->bindValue(':c','false',PDO::PARAM_STR);
  724. $statement->execute();
  725. }
  726.  
  727. public static function getElementsByListe($db,$idListe)
  728. {
  729. $statement=
  730. $db->prepare('SELECT `id_prod`,`nom`,`achete` FROM `Produits` WHERE `id_liste`=:a');
  731. $statement->bindValue(':a',$idListe,PDO::PARAM_STR);
  732. $statement->execute();
  733. while($row=$statement->fetch())
  734. {
  735. $li=new Produit($row['nom'],$row['achete']);
  736. $li->setId($row['id_prod']);
  737. $res[]=$li;
  738. }
  739. if(isset($res))
  740. {
  741. return $res;
  742. }
  743. else
  744. {
  745. return null;
  746. }
  747. }
  748.  
  749. public static function delProd($db,$idProd)
  750. {
  751. $statement=
  752. $db->prepare('DELETE FROM `Produits` WHERE `id_prod`=:a');
  753. $statement->bindValue(':a',$idProd,PDO::PARAM_STR);
  754. $statement->execute();
  755. }
  756.  
  757. public static function changerEtat($db,$id)
  758. {
  759. $statement=
  760. $db->prepare('SELECT `achete` FROM `Produits` WHERE `id_prod`=:a');
  761. $statement->bindValue(':a',$id,PDO::PARAM_STR);
  762. $statement->execute();
  763. $etat=$statement->fetch();
  764. if($etat['achete']=='true')
  765. {
  766. $statement=
  767. $db->prepare('UPDATE `akirrmann`.`Produits` SET `achete` = :b WHERE `Produits`.`id_prod` =:a');
  768. $statement->bindValue(':a',$id,PDO::PARAM_STR);
  769. $statement->bindValue(':b','false',PDO::PARAM_STR);
  770. $statement->execute();
  771. }
  772. else
  773. {
  774. $statement=
  775. $db->prepare('UPDATE `akirrmann`.`Produits` SET `achete` = :b WHERE `Produits`.`id_prod` =:a');
  776. $statement->bindValue(':a',$id,PDO::PARAM_STR);
  777. $statement->bindValue(':b','true',PDO::PARAM_STR);
  778. $statement->execute();
  779. }
  780. }
  781. }
  782. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement