Commit 1a107221 authored by Fedrico Otaran's avatar Fedrico Otaran

Muchos cambios

parent 78dca1b7
<?php
class UserController
{
public function __construct()
{
$this->userRepository = new UserRepository;
}
public function index()
{
$users = $this->userRepository->all();
$view = new UserView();
return $view->index(Array('users' => $users));
}
public function home()
{
$view = new HomeView();
return $view->show();
}
}
<?php
class UserController {
public function __construct()
{
$this->resourceRepository = new ResourceRepository;
}
public function listResources()
{
$resources = $this->resourceRepository->listAll();
$view = new SimpleResourceList();
$view->show($resources);
}
public function home()
{
$view = new Home();
$view->show();
}
}
......@@ -3,17 +3,17 @@ ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
require_once('controller/UserController.php');
require_once('model/PDORepository.php');
require_once('model/ResourceRepository.php');
require_once('model/Resource.php');
require_once('view/TwigView.php');
require_once('view/SimpleResourceList.php');
require_once('view/Home.php');
require_once('controller/UserController.class.php');
require_once('model/BaseRepository.class.php');
require_once('model/UserRepository.class.php');
require_once('model/User.class.php');
require_once('view/TwigView.class.php');
require_once('view/UserView.class.php');
require_once('view/HomeView.class.php');
if (isset($_GET["action"]) && $_GET["action"] == 'users'){
(new UserController)->listResources();
if (isset($_GET["action"]) && $_GET["action"] == 'users') {
(new UserController)->index();
} else {
(new UserController)->home();
}
<?php
abstract class PDORepository {
abstract class BaseRepository
{
const USERNAME = "user";
const PASSWORD = "pass";
const HOST = "db";
const DB = "base";
private function conn()
{
$u = self::USERNAME;
$p = self::PASSWORD;
$username = self::USERNAME;
$password = self::PASSWORD;
$db = self::DB;
$host = self::HOST;
$connection = new PDO("mysql:dbname=$db;host=$host", $u, $p);
$connection = new PDO("mysql:dbname=$db;host=$host", $username, $password);
return $connection;
}
......@@ -23,6 +23,7 @@ abstract class PDORepository {
$connection = $this->conn();
$stmt = $connection->prepare($sql);
$stmt->execute($args); // Acá se podría mirar si la consulta falla o no.
return $stmt;
}
}
<?php
class Resource {
private $name;
private $url;
public function __construct($name, $url) {
$this->name = $name;
$this->url = $url;
}
public function getName() {
return $this->name;
}
public function getUrl() {
return $this->url;
}
}
<?php
class User
{
private $id;
private $first_name;
private $last_name;
public function __construct($id, $first_name, $last_name)
{
$this->id = $id;
$this->first_name = $first_name;
$this->last_name = $last_name;
}
public function getId()
{
return $this->id;
}
public function getFirstName() {
return $this->first_name;
}
public function getLastName()
{
return $this->last_name;
}
public function fullName()
{
return $this->getLastName() . ", ". $this->getFirstName();
}
}
<?php
class ResourceRepository extends PDORepository {
public function listAll()
class UserRepository extends BaseRepository
{
public function all()
{
$data = $this->execute("select * from usuarios where estado = ?;", ['Confirmado']);
$users = [];
foreach ($data->fetchAll() as &$element) {
$users[] = new Resource($element['apellido'].", ".$element['nombre'], $element['id']);
foreach ($data->fetchAll() as $user) {
$users[] = new User($user['id'], $user['nombre'], $user['apellido']);
}
return $users;
}
}
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Home</h1>
<ul>
<li><a href="./?action=users">Usuarios</a></li>
</ul>
</body>
</html>
{% extends "layout/base.html.twig" %}
{% block title %}Home{% endblock %}
{% block head %}
{{ parent() }}
<style type="text/css">
h1 { color: #336699; }
</style>
{% endblock %}
{% block content %}
<h1>Home</h1>
<ul>
<li><a href="./?action=users">Usuarios</a></li>
</ul>
{% endblock %}
<!DOCTYPE html>
<html>
<head>
{% block head %}
<link rel="stylesheet" href="style.css" />
<title>{% block title %}{% endblock %}</title>
{% endblock %}
</head>
<body>
<div id="content">{% block content %}{% endblock %}</div>
<div id="footer">
{% block footer %}
Proyecto de Software
{% endblock %}
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Recursos</title>
</head>
<body>
<h1>Recursos</h1>
<ul>
{% for item in resources %}
<li><a href="{{ item.url }}">{{ item.name }}</a></li>
{% endfor %}
</ul>
</body>
</html>
\ No newline at end of file
{% extends "layout/base.html.twig" %}
{% block title %} Usuarios {% endblock %}
{% block head %}
{{ parent() }}
<style type="text/css">
ul { list-style: none; }
</style>
{% endblock %}
{% block content %}
<h1>Usuarios</h1>
<ul>
{% for user in users %}
<li><a href="{{ user.id }}">{{ user.fullName }}</a></li>
{% endfor %}
</ul>
{% endblock %}
<?php
class Home extends TwigView {
class HomeView extends TwigView
{
public function show()
{
echo self::getTwig()->render('home.html.twig');
$this->display('home.html.twig');
}
}
<?php
class SimpleResourceList extends TwigView {
public function show($resourceArray)
{
echo self::getTwig()
->render('listResources.html.twig', array('resources' => $resourceArray));
}
}
......@@ -2,11 +2,11 @@
require_once './lib/twig/twig/lib/Twig/Autoloader.php';
abstract class TwigView {
abstract class TwigView
{
private static $twig;
public static function getTwig()
public static function twig()
{
if (!isset(self::$twig)) {
......@@ -19,4 +19,14 @@ abstract class TwigView {
return self::$twig;
}
public function display($template, $options = [])
{
return self::twig()->display($template, $options);
}
public function render($template, $options = [])
{
return self::twig()->render($template, $options);
}
}
<?php
class UserView extends TwigView
{
public function index($options)
{
$this->display('user/index.html.twig', $options);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment