/controleurs/Controller.class.php

https://bitbucket.org/iwantc00kies/yaps · PHP · 182 lines · 70 code · 49 blank · 63 comment · 6 complexity · 43767ac08e5ed43815b0492c3b087453 MD5 · raw file

  1. <?php
  2. class ControlAbstract {
  3. var $connexion;
  4. public function __construct() {
  5. require_once('../conf/connexion.php');
  6. include('../conf/connexion.php');
  7. }
  8. }
  9. /**
  10. * Auto-chargement des classes du controlleur
  11. * (ex. /controller/Test.class.php)
  12. */
  13. function chargerClasse($classe)
  14. {
  15. require_once realpath($_SERVER["DOCUMENT_ROOT"]) .'/controller/' . ucfirst($classe) . '.class.php';
  16. //$classPath = realpath($_SERVER["DOCUMENT_ROOT"]) .'/controller/' . $classe . '.class.php' ;
  17. // if(!class_exists('AClass')){
  18. // require_once $classPath;
  19. // }
  20. //echo realpath($_SERVER["DOCUMENT_ROOT"]) .'/controler/' . $classe . '.class.php </br>';
  21. //require_once realpath($_SERVER["DOCUMENT_ROOT"]) .'/controller/' . $classe . '.class.php';
  22. //require_once $root . $classe . '.class.php';
  23. //var_dump( spl_autoload_functions( ) );
  24. }
  25. /**
  26. * Classe controlleur principale de l'application
  27. */
  28. class Controller extends ControlAbstract {
  29. /**
  30. * constructeur du controlleur
  31. */
  32. public function __construct()
  33. {
  34. parent::__construct();
  35. echo "<br/> nouveau controlleur <br/>";
  36. // auto-chargement
  37. spl_autoload_extensions(".php");
  38. if(!spl_autoload_functions()) {
  39. spl_autoload_register('chargerClasse');
  40. }
  41. }
  42. /**
  43. * Hydratation des données
  44. */
  45. public function hydrate(array $donnees)
  46. {
  47. foreach ($donnees as $key => $value)
  48. {
  49. $method = 'set'.ucfirst($key);
  50. if (method_exists($this, $method))
  51. {
  52. $this->$method($value);
  53. }
  54. }
  55. }
  56. /**
  57. * Méthode magique appelee lorsqu'un set inexistant est appelé
  58. * @param unknown_type $nom
  59. * @param unknown_type $valeur
  60. */
  61. // public function __set($nom, $valeur)
  62. // {
  63. // echo 'Ah, on a tenté d\'assigner à l\'attribut <strong>', $nom, '</strong> la valeur <strong>', $valeur, '</strong> mais c\'est pas possible !<br />';
  64. // }
  65. // public function __call($nom, $arguments)
  66. // {
  67. // echo 'La méthode <strong>', $nom, '</strong> a été appelée alors qu\'elle n\'existe pas ! Ses arguments étaient les suivants : <strong>', implode($arguments, '</strong>, <strong>'), '</strong>';
  68. // }
  69. /**
  70. * returns the root path of the project
  71. */
  72. public static function root()
  73. {
  74. return realpath($_SERVER["DOCUMENT_ROOT"]);
  75. }
  76. /*******************
  77. * Methodes metier
  78. * *****************/
  79. /*******************
  80. * LOGIN
  81. * *****************/
  82. /**
  83. *
  84. * @return true si l'utilisateur est loggé, false sinon
  85. */
  86. public static function isRegistered()
  87. {
  88. if (isset($_SESSION['myusername'])) {
  89. $registeredUser=true;
  90. }
  91. else {
  92. $registeredUser=false;
  93. }
  94. return $registeredUser;
  95. }
  96. /**
  97. * Renvoie le contenu du panier
  98. * (Tableau associatif Reference => Quantite )
  99. */
  100. function contenuPanier() {
  101. return $_SESSION['cart'] ;
  102. }
  103. /*******************
  104. * GESTION DU PANIER
  105. * Fonctions d'affichage
  106. * *****************/
  107. /**
  108. * @return returns the total number of items in the shopping cart
  109. */
  110. function itemNumber() {
  111. $cart = $_SESSION['cart'];
  112. if (!$cart) {
  113. return 0;
  114. }
  115. else {
  116. $contents = array();
  117. $count = 0;
  118. // count item number
  119. foreach ($cart as $key => $value) {
  120. $count += intval($value) ;
  121. }
  122. return $count ;
  123. }
  124. }
  125. /**
  126. * @return item or items or nothing depending on the number of item(s)
  127. */
  128. function itemOrItems() {
  129. $s =(itemNumber() > 1) ? 's':'';
  130. return $count.' item'.$s ;
  131. }
  132. }
  133. ?>