Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 16.35 KB | None | 0 0
  1. // file index.php
  2.  
  3. <?php
  4.  
  5. include_once "library/Autoloader.php";
  6. Autoloader::register();
  7.  
  8. $application = new Application();
  9. $application->run();
  10.  
  11.  
  12. // file library/autoloader.php
  13.  
  14. <?php
  15.  
  16.  
  17. class Autoloader
  18. {
  19.  
  20.     static function register()
  21.     {
  22.         spl_autoload_register(array(__CLASS__, 'autoload'));
  23.     }
  24.  
  25.     static function autoload($name)
  26.     {
  27.         //todo: vérifier les fichiers autorisés
  28.         $dir = "model";
  29.         if (strpos($name, "Controller") !== FALSE)
  30.             $dir = "controller";
  31.  
  32.         if (!file_exists($dir . "/" . $name . ".php")) {
  33.             $dir = "library";
  34.         }
  35.         include_once $dir . "/" . $name . ".php";
  36.     }
  37.  
  38. }
  39.  
  40.  
  41. // file library/application.php
  42.  
  43. <?php
  44.  
  45. class Application
  46. {
  47.     public function __construct()
  48.     {
  49.         $this->config();
  50.     }
  51.  
  52.     public function run()
  53.     {
  54.         $this->route();
  55.     }
  56.  
  57.     private function config()
  58.     {
  59.         error_reporting(E_ALL | E_STRICT);
  60.         ini_set('display_errors', true);
  61.     }
  62.  
  63.     public static function getParameters()
  64.     {
  65.         return array_merge($_GET, $_POST);
  66.     }
  67.  
  68.     private function route()
  69.     {
  70.         $parameters = $this->getParameters();
  71.  
  72.         if (isset($parameters["controller"])) {
  73.             $controller = ucfirst($parameters["controller"]) . "Controller";
  74.  
  75.             if (class_exists($controller)) {
  76.                 $c = new $controller();
  77.  
  78.                 if (isset($parameters["action"])) {
  79.                     $action = strtolower($parameters["action"]);
  80.  
  81.                     if (method_exists($c, $action)) {
  82.                         $c->$action();
  83.                     }
  84.  
  85.                 } else {
  86.                     $c->index();
  87.                 }
  88.             }
  89.  
  90.         } else {
  91.             $c = new HomeController();
  92.             $c->index();
  93.         }
  94.     }
  95. }
  96.  
  97. // file library/database.php
  98.  
  99. <?php
  100.  
  101. class DataBase
  102. {
  103.     private static $connexion;
  104.  
  105.     const DB_NAME = "recrutement-mvc";
  106.     const DB_USER = "root";
  107.     const DB_PASSWORD = "";
  108.  
  109.     private static function connect()
  110.     {
  111.         if (self::$connexion == null) {
  112.             try {
  113.                 self::$connexion = new PDO("mysql:host=localhost;dbname=" . self::DB_NAME, self::DB_USER, self::DB_PASSWORD);
  114.                 self::$connexion->exec("SET CHARACTER SET utf8");
  115.             } catch (Exception $e) {
  116.                 echo "Erreur de connexion à la base de données.";
  117.                 exit;
  118.             }
  119.         }
  120.     }
  121.  
  122.     public static function getConnexion()
  123.     {
  124.         self::connect();
  125.         return self::$connexion;
  126.     }
  127.  
  128. }
  129.  
  130. // file controller/AbstractController.php
  131.  
  132. <?php
  133.  
  134. abstract class AbstractController
  135. {
  136.     private $controller;
  137.     private $model;
  138.     private static $data;
  139.  
  140.  
  141.     public function __construct()
  142.     {
  143.         $this->controller = get_class($this);
  144.         $this->model = substr($this->controller, 0, strpos($this->controller, "Controller"));
  145.     }
  146.  
  147.     public function render($view, $d = null)
  148.     {
  149.         self::$data = $d;
  150.  
  151.         $model = strtolower($this->model);
  152.         if ($model == null) {
  153.             $model = 'home';
  154.         }
  155.  
  156.         if(file_exists("view/" . $model . "/" . $view . ".php")){
  157.             include_once "view/header.php";
  158.             include_once "view/" . $model . "/" . $view . ".php";
  159.             include_once "view/footer.php";
  160.         }
  161.  
  162.     }
  163.  
  164.     public function remove()
  165.     {
  166.         try {
  167.             /** @var Model $r */
  168.             $r = new $this->model(Application::getParameters()["id"]);
  169.             $r->delete();
  170.  
  171.         } catch (Exception $e) {
  172.             //(new HomeController())->render("index");
  173.             // $this->render("error");
  174.         }
  175.     }
  176.  
  177.     public function getControllerName()
  178.     {
  179.     }
  180.  
  181.     public function getActionName()
  182.     {
  183.  
  184.     }
  185.  
  186.     public function index()
  187.     {
  188.         $this->render("index");
  189.     }
  190.  
  191.     public static function getData(){
  192.         return self::$data;
  193.     }
  194. }
  195.  
  196. // file controller/UserController.php
  197.  
  198. <?php
  199.  
  200. class UserController extends AbstractController
  201. {
  202.  
  203.     public function index()
  204.     {
  205.         $this->render("index", User::findAll());
  206.     }
  207.  
  208.     public function view()
  209.     {
  210.         try {
  211.             $u = new User(Application::getParameters()["id"]);
  212.  
  213.             $this->render("view", $u);
  214.         } catch (Exception $e) {
  215.             (new HomeController())->render("index");
  216.         }
  217.     }
  218.  
  219.     public function add()
  220.     {
  221.         $this->render("add");
  222.     }
  223.  
  224.     public function edit()
  225.     {
  226.         $this->render("edit");
  227.     }
  228.  
  229.  
  230. }
  231.  
  232.  
  233. // file controller/RoleController.php
  234.  
  235. <?php
  236.  
  237. class RoleController extends AbstractController
  238. {
  239.     public function index()
  240.     {
  241.         $this->render("index", Role::findAll());
  242.     }
  243.  
  244.     public function view()
  245.     {
  246.         try {
  247.             $r = new Role(Application::getParameters()["id"]);
  248.             $this->render("view", $r);
  249.         } catch (Exception $e) {
  250.             (new HomeController())->render("index");
  251.         }
  252.     }
  253.  
  254.     public function add()
  255.     {
  256.         if (isset(Application::getParameters()["add_role"])) {
  257.             if (!empty(Application::getParameters()["name"])) {
  258.                 $role = new Role();
  259.                 $role->name = Application::getParameters()["name"];
  260.  
  261.                 $role->insert();
  262.  
  263.                 header('Location: /?controller=role');
  264.             }
  265.         }
  266.  
  267.         $this->render("add");
  268.     }
  269.  
  270.     public function edit()
  271.     {
  272.         $parameters = Application::getParameters();
  273.  
  274.         if (isset($parameters['id'])) {
  275.             $id = $parameters['id'];
  276.  
  277.             try {
  278.                 $role = new Role($id);
  279.  
  280.                 if (isset($parameters['edit_role'])) {
  281.                     if (!empty($parameters['name'])) {
  282.  
  283.                         try {
  284.                             $role->name = $parameters['name'];
  285.                             header('Location: /?controller=role&action=index');
  286.  
  287.                         } catch (Exception $e) {
  288.                             $this->render("edit", array('role' => $role, 'error' => 'Le role n\'a pas été mis à jour.'));
  289.                         }
  290.  
  291.                     } else {
  292.                         $this->render("edit", array('role' => $role, 'error' => 'Le nom du rôle ne peut être vide.'));
  293.                     }
  294.                 }
  295.  
  296.                 $this->render("edit", array('role' => $role));
  297.  
  298.             } catch (Exception $e) {
  299.                 (new HomeController())->render("index");
  300.             }
  301.         } else {
  302.             (new HomeController())->render("index");
  303.         }
  304.     }
  305.  
  306.  
  307. }
  308.  
  309.  
  310. // file model/Abstract_Model.php
  311.  
  312. <?php
  313.  
  314. abstract class Abstract_Model
  315. {
  316.  
  317.     public function __construct($id = null)
  318.     {
  319.         $class = get_class($this);
  320.         $table = strtolower($class);
  321.  
  322.         if ($id != null) {
  323.             $connexion = dataBase::getConnexion();
  324.             $st = $connexion->prepare("select * from " . $table . " where id" . $table . "=:id");
  325.             $st->bindValue(":id", $id);
  326.             $st->execute();
  327.             if ($st->rowCount() != 1) {
  328.                 throw new Exception("Not in table: " . $table . " id: " . $id);
  329.             } else {
  330.                 $row = $st->fetch(PDO::FETCH_ASSOC);
  331.                 foreach ($row as $field => $value) {
  332.                     if (substr($field, 0, 2) == "id") {
  333.                         $linkedField = substr($field, 2);
  334.                         $linkedClass = ucfirst($linkedField);
  335.                         if ($linkedClass != get_class($this))
  336.                             $this->$linkedField = new $linkedClass($value);
  337.                         else
  338.                             $this->$field = $value;
  339.                     } else
  340.                         $this->$field = $value;
  341.                 }
  342.             }
  343.         }
  344.     }
  345.  
  346.     public static function findAll()
  347.     {
  348.         $class = get_called_class();
  349.         $table = strtolower($class);
  350.  
  351.         $connexion = dataBase::getConnexion();
  352.         $st = $connexion->prepare("select id" . $table . " from " . $table);
  353.         $st->execute();
  354.  
  355.         $list = array();
  356.         while ($row = $st->fetch(PDO::FETCH_ASSOC)) {
  357.             $list[] = new $class($row["id" . $table]);
  358.         }
  359.  
  360.         return $list;
  361.     }
  362.  
  363.     public function __get($fieldName)
  364.     {
  365.         $varName = "_" . $fieldName;
  366.  
  367.         if (strpos($varName, "_id") === 0) {
  368.             $letter = substr($varName, 3, 1);
  369.             $varName = str_replace($letter, strtoupper($letter), $varName);
  370.         }
  371.  
  372.         if (property_exists(get_class($this), $varName))
  373.             return $this->$varName;
  374.         else
  375.             throw new Exception("Unknown variable: " . $fieldName);
  376.     }
  377.  
  378.     public function __set($fieldName, $value)
  379.     {
  380.         $varName = "_" . $fieldName;
  381.         if ($value != null) {
  382.             if (property_exists(get_class($this), $varName)) {
  383.                 $this->$varName = $value;
  384.                 $class = get_class($this);
  385.                 $table = strtolower($class);
  386.                 $id = "_id" . $fieldName;
  387.                 if (isset($value->$id)) {
  388.                     $connexion = dataBase::getConnexion();
  389.                     $st = $connexion->prepare("update " . $table . " set id" . $fieldName . "=:val where id" . $table . "=:id");
  390.                     $id = substr($id, 1);
  391.                     $st->bindValue(":val", $value->$id);
  392.                 } else {
  393.                     $connexion = dataBase::getConnexion();
  394.                     $st = $connexion->prepare("update " . $table . " set " . $fieldName . "=:val where id" . $table . "=:id");
  395.                     $st->bindValue(":val", $value);
  396.                 }
  397.                 $id = "id" . $table;
  398.                 $st->bindValue(":id", $this->$id);
  399.                 $st->execute();
  400.             } else
  401.                 throw new Exception("Unknown variable: " . $fieldName);
  402.         }
  403.     }
  404.  
  405.     public function insert()
  406.     {
  407.         $class = get_class($this);
  408.         $table = strtolower($class);
  409.  
  410.         $fields = "(";
  411.         $values = "(";
  412.  
  413.         foreach ($this as $attribute => $value) {
  414.  
  415.             $fields .= "`" . substr($attribute, 1) . "`, ";
  416.  
  417.             if ($value == null) {
  418.                 $value = 'NULL';
  419.                 $values .= $value . ", ";
  420.             } else {
  421.                 $values .= ":" . substr($attribute, 1) . ", ";
  422.             }
  423.         }
  424.  
  425.         $fields = substr_replace($fields, ")", -2, 2);
  426.         $values = substr_replace($values, ")", -2, 2);
  427.  
  428.         $connexion = dataBase::getConnexion();
  429.         $st = $connexion->prepare("insert into `" . $table . "` " . $fields . " values " . $values . ";");
  430.  
  431.         foreach ($this as $attribute => $value) {
  432.             if ($value != null) {
  433.                 $st->bindValue(":" . substr($attribute, 1), $value);
  434.             }
  435.         }
  436.  
  437.         $st->execute();
  438.     }
  439.  
  440.     public function delete()
  441.     {
  442.         $class = get_class($this);
  443.         $table = strtolower($class);
  444.  
  445.         $id = "_id" . $class;
  446.  
  447.         $connexion = dataBase::getConnexion();
  448.         $st = $connexion->prepare("delete from " . $table . " where id" . $table . "=:id");
  449.         $st->bindValue(':id', $this->$id);
  450.         $st->execute();
  451.  
  452.         header("Location: /?controller=" . $table);
  453.     }
  454.  
  455.     public function getObject($name)
  456.     {
  457.         $name = strtolower($name);
  458.         $class = ucfirst($name);
  459.         $attr = "_" . $name;
  460.  
  461.         $connexion = dataBase::getConnexion();
  462.         $st = $connexion->prepare("select * from " . $name . " where id" . $class . "=:id");
  463.         $st->bindValue(":id", $this->$attr);
  464.         $st->execute();
  465.  
  466.         $object = null;
  467.  
  468.         try {
  469.             $object = $st->fetchObject($name);
  470.  
  471.         } catch (Exception $e) {
  472.             (new HomeController())->render("index");
  473.         }
  474.  
  475.         return $object;
  476.     }
  477.  
  478. }
  479.  
  480. // file model/Role.php
  481.  
  482. <?php
  483.  
  484. class Role extends Abstract_Model
  485. {
  486.     protected $_idRole;
  487.     protected $_name;
  488. }
  489.  
  490. // file model/User.php
  491.  
  492. <?php
  493.  
  494. class User extends Abstract_Model
  495. {
  496.     protected $_idUser;
  497.     protected $_firstName;
  498.     protected $_lastName;
  499.     protected $_age;
  500.     protected $_role;
  501.  
  502.     public function getRole()
  503.     {
  504.  
  505.  
  506.     }
  507.  
  508. }
  509.  
  510. // file view/header.php
  511.  
  512. <!DOCTYPE html>
  513. <html>
  514. <head>
  515.     <meta charset="UTF-8">
  516.     <title>Gestion des utilisateurs</title>
  517.     <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>
  518.     <link rel="stylesheet" type="text/css" href="css/bootstrap-theme.min.css"/>
  519. </head>
  520. <body class="container">
  521.  
  522. <header>
  523.     <h1>Gestion des utilisateurs</h1>
  524.  
  525.     <nav>
  526.         <ul class="list-inline">
  527.             <li><a href="?controller=user">Utilisateurs</a></li>
  528.             <li><a href="?controller=role">Roles</a></li>
  529.         </ul>
  530.     </nav>
  531. </header>
  532.  
  533. <main>
  534.  
  535.  
  536. //file view/footer.php
  537.  
  538. </main>
  539. <footer></footer>
  540. </body>
  541. </html>
  542.  
  543.  
  544. // file view/user/index.php
  545.  
  546. <table class="table table-striped">
  547.     <thead>
  548.     <tr>
  549.         <th>Prénom</th>
  550.         <th>Nom</th>
  551.         <th>Role</th>
  552.         <th>Age</th>
  553.         <th></th>
  554.     </tr>
  555.     </thead>
  556.     <tbody>
  557.     <?php foreach (UserController::getData() as $user): ?>
  558.     <tr>
  559.         <td><?php echo $user->firstName; ?></td>
  560.         <td><?php echo $user->lastName; ?></td>
  561.         <td><?php echo $user->getObject("role")->name; ?></td>
  562.         <td><?php echo $user->age; ?></td>
  563.         <td>
  564.             <div class="pull-right">
  565.                 <a class="btn btn-default"
  566.                    href="?controller=user&action=view&id=<?php echo $user->idUser; ?>">Voir cet utilisateur</a>
  567.                 <a class="glyphicon glyphicon-pencil btn btn-default"
  568.                    href="?controller=user&action=edit&id=<?php echo $user->idUser; ?>"></a>
  569.                 <a class="glyphicon glyphicon-remove btn btn-default btn-danger"
  570.                    href="?controller=user&action=remove&id=<?php echo $user->idUser; ?>"></a>
  571.             </div>
  572.         </td>
  573.     </tr>
  574. <?php endforeach; ?>
  575. </tbody>
  576. </table>
  577.  
  578. <a class="btn btn-primary" href="?controller=user&action=add" role="button">Ajouter un utilisateur</a>
  579.  
  580. // file view/user/view.php
  581.  
  582. <h3><?php echo UserController::getData()->firstName; ?></h3>
  583.  
  584. // file view/role/index.php
  585.  
  586. <table class="table table-striped">
  587.     <thead>
  588.     <tr>
  589.         <th>Nom</th>
  590.         <th></th>
  591.     </tr>
  592.     </thead>
  593.     <tbody>
  594.     <?php foreach (RoleController::getData() as $role): ?>
  595.         <tr>
  596.             <td><?php echo $role->name; ?></td>
  597.             <td>
  598.                 <div class="pull-right">
  599.                     <a class="glyphicon glyphicon-pencil btn btn-default"
  600.                        href="?controller=role&action=edit&id=<?php echo $role->idRole; ?>"></a>
  601.                     <a class="glyphicon glyphicon-remove btn btn-default btn-danger"
  602.                        href="?controller=role&action=remove&id=<?php echo $role->idRole; ?>"></a>
  603.                 </div>
  604.             </td>
  605.         </tr>
  606.     <?php endforeach; ?>
  607.     </tbody>
  608. </table>
  609.  
  610. <a class="btn btn-primary" href="/?controller=role&action=add" role="button">Ajouter un role</a>
  611.  
  612.  
  613. // file view/role/add.php
  614.  
  615. <h3>Ajout d'un role : </h3>
  616.  
  617. <form action="/?controller=role&action=add" method="post" class="form-inline">
  618.     <div class="form-group">
  619.         <label for="name">Nom : </label>
  620.         <input type="text" id="name" name="name" class="form-control">
  621.     </div>
  622.  
  623.     <input type="submit" name="add_role" class="btn btn-default">
  624. </form>
  625.  
  626. // file view/role/edit.php
  627.  
  628. <?php $data = RoleController::getData(); ?>
  629. <h3>Modification d'un role : </h3>
  630.  
  631. <form action="/?controller=role&action=edit&id=<?php echo $data['role']->idRole; ?>" method="post" class="form-inline">
  632.     <div class="form-group">
  633.         <label for="name">Nom : </label>
  634.         <input type="text" id="name" name="name" class="form-control" value="<?php echo $data['role']->name; ?>">
  635.     </div>
  636.  
  637.     <input type="submit" name="edit_role" class="btn btn-default">
  638. </form>
  639.  
  640. <?php if(isset($data['error'])): ?>
  641.     <p class="alert alert-danger"><?php echo $data['error']; ?></p>
  642. <?php endif; ?>
  643.  
  644.  
  645.  
  646. // file model/.htaccess
  647. // file view/.htaccess
  648. // file controller/.htaccess
  649. // file library/.htaccess
  650.  
  651. Deny from all
  652.  
  653.  
  654. // css : bootstrap
  655.  
  656. // file .gitignore
  657.  
  658. .idea/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement