Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // file index.php
- <?php
- include_once "library/Autoloader.php";
- Autoloader::register();
- $application = new Application();
- $application->run();
- // file library/autoloader.php
- <?php
- class Autoloader
- {
- static function register()
- {
- spl_autoload_register(array(__CLASS__, 'autoload'));
- }
- static function autoload($name)
- {
- //todo: vérifier les fichiers autorisés
- $dir = "model";
- if (strpos($name, "Controller") !== FALSE)
- $dir = "controller";
- if (!file_exists($dir . "/" . $name . ".php")) {
- $dir = "library";
- }
- include_once $dir . "/" . $name . ".php";
- }
- }
- // file library/application.php
- <?php
- class Application
- {
- public function __construct()
- {
- $this->config();
- }
- public function run()
- {
- $this->route();
- }
- private function config()
- {
- error_reporting(E_ALL | E_STRICT);
- ini_set('display_errors', true);
- }
- public static function getParameters()
- {
- return array_merge($_GET, $_POST);
- }
- private function route()
- {
- $parameters = $this->getParameters();
- if (isset($parameters["controller"])) {
- $controller = ucfirst($parameters["controller"]) . "Controller";
- if (class_exists($controller)) {
- $c = new $controller();
- if (isset($parameters["action"])) {
- $action = strtolower($parameters["action"]);
- if (method_exists($c, $action)) {
- $c->$action();
- }
- } else {
- $c->index();
- }
- }
- } else {
- $c = new HomeController();
- $c->index();
- }
- }
- }
- // file library/database.php
- <?php
- class DataBase
- {
- private static $connexion;
- const DB_NAME = "recrutement-mvc";
- const DB_USER = "root";
- const DB_PASSWORD = "";
- private static function connect()
- {
- if (self::$connexion == null) {
- try {
- self::$connexion = new PDO("mysql:host=localhost;dbname=" . self::DB_NAME, self::DB_USER, self::DB_PASSWORD);
- self::$connexion->exec("SET CHARACTER SET utf8");
- } catch (Exception $e) {
- echo "Erreur de connexion à la base de données.";
- exit;
- }
- }
- }
- public static function getConnexion()
- {
- self::connect();
- return self::$connexion;
- }
- }
- // file controller/AbstractController.php
- <?php
- abstract class AbstractController
- {
- private $controller;
- private $model;
- private static $data;
- public function __construct()
- {
- $this->controller = get_class($this);
- $this->model = substr($this->controller, 0, strpos($this->controller, "Controller"));
- }
- public function render($view, $d = null)
- {
- self::$data = $d;
- $model = strtolower($this->model);
- if ($model == null) {
- $model = 'home';
- }
- if(file_exists("view/" . $model . "/" . $view . ".php")){
- include_once "view/header.php";
- include_once "view/" . $model . "/" . $view . ".php";
- include_once "view/footer.php";
- }
- }
- public function remove()
- {
- try {
- /** @var Model $r */
- $r = new $this->model(Application::getParameters()["id"]);
- $r->delete();
- } catch (Exception $e) {
- //(new HomeController())->render("index");
- // $this->render("error");
- }
- }
- public function getControllerName()
- {
- }
- public function getActionName()
- {
- }
- public function index()
- {
- $this->render("index");
- }
- public static function getData(){
- return self::$data;
- }
- }
- // file controller/UserController.php
- <?php
- class UserController extends AbstractController
- {
- public function index()
- {
- $this->render("index", User::findAll());
- }
- public function view()
- {
- try {
- $u = new User(Application::getParameters()["id"]);
- $this->render("view", $u);
- } catch (Exception $e) {
- (new HomeController())->render("index");
- }
- }
- public function add()
- {
- $this->render("add");
- }
- public function edit()
- {
- $this->render("edit");
- }
- }
- // file controller/RoleController.php
- <?php
- class RoleController extends AbstractController
- {
- public function index()
- {
- $this->render("index", Role::findAll());
- }
- public function view()
- {
- try {
- $r = new Role(Application::getParameters()["id"]);
- $this->render("view", $r);
- } catch (Exception $e) {
- (new HomeController())->render("index");
- }
- }
- public function add()
- {
- if (isset(Application::getParameters()["add_role"])) {
- if (!empty(Application::getParameters()["name"])) {
- $role = new Role();
- $role->name = Application::getParameters()["name"];
- $role->insert();
- header('Location: /?controller=role');
- }
- }
- $this->render("add");
- }
- public function edit()
- {
- $parameters = Application::getParameters();
- if (isset($parameters['id'])) {
- $id = $parameters['id'];
- try {
- $role = new Role($id);
- if (isset($parameters['edit_role'])) {
- if (!empty($parameters['name'])) {
- try {
- $role->name = $parameters['name'];
- header('Location: /?controller=role&action=index');
- } catch (Exception $e) {
- $this->render("edit", array('role' => $role, 'error' => 'Le role n\'a pas été mis à jour.'));
- }
- } else {
- $this->render("edit", array('role' => $role, 'error' => 'Le nom du rôle ne peut être vide.'));
- }
- }
- $this->render("edit", array('role' => $role));
- } catch (Exception $e) {
- (new HomeController())->render("index");
- }
- } else {
- (new HomeController())->render("index");
- }
- }
- }
- // file model/Abstract_Model.php
- <?php
- abstract class Abstract_Model
- {
- public function __construct($id = null)
- {
- $class = get_class($this);
- $table = strtolower($class);
- if ($id != null) {
- $connexion = dataBase::getConnexion();
- $st = $connexion->prepare("select * from " . $table . " where id" . $table . "=:id");
- $st->bindValue(":id", $id);
- $st->execute();
- if ($st->rowCount() != 1) {
- throw new Exception("Not in table: " . $table . " id: " . $id);
- } else {
- $row = $st->fetch(PDO::FETCH_ASSOC);
- foreach ($row as $field => $value) {
- if (substr($field, 0, 2) == "id") {
- $linkedField = substr($field, 2);
- $linkedClass = ucfirst($linkedField);
- if ($linkedClass != get_class($this))
- $this->$linkedField = new $linkedClass($value);
- else
- $this->$field = $value;
- } else
- $this->$field = $value;
- }
- }
- }
- }
- public static function findAll()
- {
- $class = get_called_class();
- $table = strtolower($class);
- $connexion = dataBase::getConnexion();
- $st = $connexion->prepare("select id" . $table . " from " . $table);
- $st->execute();
- $list = array();
- while ($row = $st->fetch(PDO::FETCH_ASSOC)) {
- $list[] = new $class($row["id" . $table]);
- }
- return $list;
- }
- public function __get($fieldName)
- {
- $varName = "_" . $fieldName;
- if (strpos($varName, "_id") === 0) {
- $letter = substr($varName, 3, 1);
- $varName = str_replace($letter, strtoupper($letter), $varName);
- }
- if (property_exists(get_class($this), $varName))
- return $this->$varName;
- else
- throw new Exception("Unknown variable: " . $fieldName);
- }
- public function __set($fieldName, $value)
- {
- $varName = "_" . $fieldName;
- if ($value != null) {
- if (property_exists(get_class($this), $varName)) {
- $this->$varName = $value;
- $class = get_class($this);
- $table = strtolower($class);
- $id = "_id" . $fieldName;
- if (isset($value->$id)) {
- $connexion = dataBase::getConnexion();
- $st = $connexion->prepare("update " . $table . " set id" . $fieldName . "=:val where id" . $table . "=:id");
- $id = substr($id, 1);
- $st->bindValue(":val", $value->$id);
- } else {
- $connexion = dataBase::getConnexion();
- $st = $connexion->prepare("update " . $table . " set " . $fieldName . "=:val where id" . $table . "=:id");
- $st->bindValue(":val", $value);
- }
- $id = "id" . $table;
- $st->bindValue(":id", $this->$id);
- $st->execute();
- } else
- throw new Exception("Unknown variable: " . $fieldName);
- }
- }
- public function insert()
- {
- $class = get_class($this);
- $table = strtolower($class);
- $fields = "(";
- $values = "(";
- foreach ($this as $attribute => $value) {
- $fields .= "`" . substr($attribute, 1) . "`, ";
- if ($value == null) {
- $value = 'NULL';
- $values .= $value . ", ";
- } else {
- $values .= ":" . substr($attribute, 1) . ", ";
- }
- }
- $fields = substr_replace($fields, ")", -2, 2);
- $values = substr_replace($values, ")", -2, 2);
- $connexion = dataBase::getConnexion();
- $st = $connexion->prepare("insert into `" . $table . "` " . $fields . " values " . $values . ";");
- foreach ($this as $attribute => $value) {
- if ($value != null) {
- $st->bindValue(":" . substr($attribute, 1), $value);
- }
- }
- $st->execute();
- }
- public function delete()
- {
- $class = get_class($this);
- $table = strtolower($class);
- $id = "_id" . $class;
- $connexion = dataBase::getConnexion();
- $st = $connexion->prepare("delete from " . $table . " where id" . $table . "=:id");
- $st->bindValue(':id', $this->$id);
- $st->execute();
- header("Location: /?controller=" . $table);
- }
- public function getObject($name)
- {
- $name = strtolower($name);
- $class = ucfirst($name);
- $attr = "_" . $name;
- $connexion = dataBase::getConnexion();
- $st = $connexion->prepare("select * from " . $name . " where id" . $class . "=:id");
- $st->bindValue(":id", $this->$attr);
- $st->execute();
- $object = null;
- try {
- $object = $st->fetchObject($name);
- } catch (Exception $e) {
- (new HomeController())->render("index");
- }
- return $object;
- }
- }
- // file model/Role.php
- <?php
- class Role extends Abstract_Model
- {
- protected $_idRole;
- protected $_name;
- }
- // file model/User.php
- <?php
- class User extends Abstract_Model
- {
- protected $_idUser;
- protected $_firstName;
- protected $_lastName;
- protected $_age;
- protected $_role;
- public function getRole()
- {
- }
- }
- // file view/header.php
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>Gestion des utilisateurs</title>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap-theme.min.css"/>
- </head>
- <body class="container">
- <header>
- <h1>Gestion des utilisateurs</h1>
- <nav>
- <ul class="list-inline">
- <li><a href="?controller=user">Utilisateurs</a></li>
- <li><a href="?controller=role">Roles</a></li>
- </ul>
- </nav>
- </header>
- <main>
- //file view/footer.php
- </main>
- <footer></footer>
- </body>
- </html>
- // file view/user/index.php
- <table class="table table-striped">
- <thead>
- <tr>
- <th>Prénom</th>
- <th>Nom</th>
- <th>Role</th>
- <th>Age</th>
- <th></th>
- </tr>
- </thead>
- <tbody>
- <?php foreach (UserController::getData() as $user): ?>
- <tr>
- <td><?php echo $user->firstName; ?></td>
- <td><?php echo $user->lastName; ?></td>
- <td><?php echo $user->getObject("role")->name; ?></td>
- <td><?php echo $user->age; ?></td>
- <td>
- <div class="pull-right">
- <a class="btn btn-default"
- href="?controller=user&action=view&id=<?php echo $user->idUser; ?>">Voir cet utilisateur</a>
- <a class="glyphicon glyphicon-pencil btn btn-default"
- href="?controller=user&action=edit&id=<?php echo $user->idUser; ?>"></a>
- <a class="glyphicon glyphicon-remove btn btn-default btn-danger"
- href="?controller=user&action=remove&id=<?php echo $user->idUser; ?>"></a>
- </div>
- </td>
- </tr>
- <?php endforeach; ?>
- </tbody>
- </table>
- <a class="btn btn-primary" href="?controller=user&action=add" role="button">Ajouter un utilisateur</a>
- // file view/user/view.php
- <h3><?php echo UserController::getData()->firstName; ?></h3>
- // file view/role/index.php
- <table class="table table-striped">
- <thead>
- <tr>
- <th>Nom</th>
- <th></th>
- </tr>
- </thead>
- <tbody>
- <?php foreach (RoleController::getData() as $role): ?>
- <tr>
- <td><?php echo $role->name; ?></td>
- <td>
- <div class="pull-right">
- <a class="glyphicon glyphicon-pencil btn btn-default"
- href="?controller=role&action=edit&id=<?php echo $role->idRole; ?>"></a>
- <a class="glyphicon glyphicon-remove btn btn-default btn-danger"
- href="?controller=role&action=remove&id=<?php echo $role->idRole; ?>"></a>
- </div>
- </td>
- </tr>
- <?php endforeach; ?>
- </tbody>
- </table>
- <a class="btn btn-primary" href="/?controller=role&action=add" role="button">Ajouter un role</a>
- // file view/role/add.php
- <h3>Ajout d'un role : </h3>
- <form action="/?controller=role&action=add" method="post" class="form-inline">
- <div class="form-group">
- <label for="name">Nom : </label>
- <input type="text" id="name" name="name" class="form-control">
- </div>
- <input type="submit" name="add_role" class="btn btn-default">
- </form>
- // file view/role/edit.php
- <?php $data = RoleController::getData(); ?>
- <h3>Modification d'un role : </h3>
- <form action="/?controller=role&action=edit&id=<?php echo $data['role']->idRole; ?>" method="post" class="form-inline">
- <div class="form-group">
- <label for="name">Nom : </label>
- <input type="text" id="name" name="name" class="form-control" value="<?php echo $data['role']->name; ?>">
- </div>
- <input type="submit" name="edit_role" class="btn btn-default">
- </form>
- <?php if(isset($data['error'])): ?>
- <p class="alert alert-danger"><?php echo $data['error']; ?></p>
- <?php endif; ?>
- // file model/.htaccess
- // file view/.htaccess
- // file controller/.htaccess
- // file library/.htaccess
- Deny from all
- // css : bootstrap
- // file .gitignore
- .idea/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement