Guest User

Untitled

a guest
Dec 29th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.73 KB | None | 0 0
  1. config
  2. |_____config.php
  3.  
  4. controllers
  5. |_____NotaController.php
  6.  
  7. libs
  8. |_____app.php
  9. |_____Database.php
  10.  
  11. models
  12. |_____Nota.php
  13.  
  14. public
  15. |_____css
  16. |_____styles.css
  17. sql
  18. |_____crearBBDD.sql
  19.  
  20. views
  21. |_____layout
  22. |_____footer.php
  23. |_____header.php
  24. Nota
  25. |_____crearNota.php
  26. |_____editarNota.php
  27. |_____todas.php
  28.  
  29. .htaccess
  30. index.php
  31.  
  32. // constantes globales
  33. define('URL', 'http://localhost/mvcnotes/');
  34. define('DEFAULT_CONTROLLER', 'Nota');
  35. define('DEFAULT_ACTION', 'index');
  36.  
  37. <?php
  38. require_once 'models/Nota.php';
  39.  
  40. class NotaController {
  41.  
  42. private $nota;
  43.  
  44. public function __construct() {
  45. //echo 'Nota Controller Creado.';
  46. $this->nota = new Nota();
  47. }
  48.  
  49. public function index() { // En este caso index va a mostrar todas mis notas
  50. //echo 'Hola, soy index()';
  51. $notas = $this->nota->getAll();
  52. require_once 'views/Nota/todas.php';
  53. }
  54.  
  55. public function crearNota() {
  56. require_once 'views/Nota/crearNota.php';
  57. }
  58.  
  59. public function guardarNota() {
  60. $id = 'null';
  61. $fecha = 'SYSDATE()';
  62. if (isset($_POST)) {
  63. $titulo = $_POST['titulo'];
  64. $color = $_POST['color'];
  65. $contenido = $_POST['contenido'];
  66.  
  67. $this->nota->setId($id);
  68. $this->nota->setTitulo($titulo);
  69. $this->nota->setContenido($contenido);
  70. $this->nota->setColor($color);
  71. $this->nota->setFecha($fecha);
  72.  
  73. if ($this->nota->save($this->nota)) {
  74. echo 'Nota guardada correctamente';
  75. } else {
  76. echo 'No se pudo guardar la nota';
  77. }
  78. }
  79. }
  80.  
  81. public function eliminar() {
  82. if (isset($_GET)) {
  83. $parametros = explode('/', $_GET['url']);
  84. $id = $parametros[2];
  85.  
  86. if($this->nota->delete($id)) {
  87. echo 'La nota se eliminó correctamente.';
  88. } else {
  89. echo 'La nota no se pudo eliminar';
  90. }
  91. }
  92. }
  93.  
  94. public function editar() {
  95. if (isset($_GET)) {
  96. $parametros = explode('/', $_GET['url']);
  97. $id = $parametros[2];
  98.  
  99. if($nota = $this->nota->getById($id)) {
  100. require_once 'views/Nota/editarNota.php';
  101. }
  102. }
  103. }
  104.  
  105. public function actualizar() {
  106. $fecha = 'SYSDATE()';
  107. if (isset($_POST)) {
  108. $id = $_POST['id'];
  109. $titulo = $_POST['titulo'];
  110. $color = $_POST['color'];
  111. $contenido = $_POST['contenido'];
  112.  
  113. $this->nota->setId($id);
  114. $this->nota->setTitulo($titulo);
  115. $this->nota->setContenido($contenido);
  116. $this->nota->setColor($color);
  117. $this->nota->setFecha($fecha);
  118.  
  119. if ($this->nota->update($this->nota)) {
  120. echo 'Nota editada correctamente';
  121. } else {
  122. echo 'No se pudo editar la nota';
  123. }
  124. }
  125. }
  126. }
  127.  
  128. <?php
  129. require_once 'config/config.php';
  130.  
  131. class App {
  132.  
  133. public $controller;
  134. public $controllerObj;
  135. public $action;
  136.  
  137. public function __construct(){
  138.  
  139. // Cargo el header para el HTML
  140. require_once 'views/layout/header.php';
  141.  
  142. // Compruebo la URL
  143. $this->checkURL();
  144. //echo '<p>El controlador actual es: <b>' . $this->controller . '</b>, y la acción: <b>' . $this->action . '</b></p>';
  145.  
  146. // Compruebo el Controlador
  147. $this->comprobarControlador();
  148.  
  149. // Compruebo la accion
  150. if (!empty($this->action)) {
  151. $this->comprobarAccion();
  152. } else {
  153. $this->action = 'index';
  154. $this->comprobarAccion();
  155. }
  156.  
  157. // Cargo el footer para el HTML
  158. require_once 'views/layout/footer.php';
  159. }
  160.  
  161. public function checkURL() {
  162. // Si recibo parámetros
  163. if (isset($_GET) && count($_GET) >= 1) {
  164. // Particiono la URL en parámetros
  165. $camposURL = explode('/', $_GET['url']);
  166.  
  167. // Cuento los parámetros recibidos
  168. if (count($camposURL) == 3 && $camposURL[0] == 'nota' && is_numeric($camposURL[2])) {
  169. // Caso especial para editar/elimar nota, que me envian el id por GET como 3er parametro
  170. $this->controller = $camposURL[0];
  171. $this->action = $camposURL[1];
  172.  
  173. } else if (count($camposURL) == 2) {
  174. $this->controller = $camposURL[0];
  175. $this->action = $camposURL[1];
  176.  
  177. } else if (count($camposURL) == 1) {
  178. $this->controller = $camposURL[0];
  179. $this->action = DEFAULT_ACTION;
  180.  
  181. } else {
  182. echo '<h4 style="color: red;">La url contenía más de 2 parámetros</h4>';
  183. die();
  184. }
  185.  
  186. // Si no recibo parámetros
  187. } else {
  188. $this->controller = DEFAULT_CONTROLLER;
  189. $this->action = DEFAULT_ACTION;
  190. }
  191.  
  192. // Le añado la terminacion Controller al controlador -> Nota -> NotaController
  193. $this->controller = $this->controller . 'Controller';
  194. }
  195.  
  196. public function comprobarControlador() {
  197. // Si el controlador es correcto.
  198. if (file_exists('controllers/' . $this->controller . '.php')) {
  199. //echo '<p>El controlador ' . $this->controller . ' existe.</p>';
  200.  
  201. // Cargo el controlador
  202. require_once 'controllers/' . $this->controller . '.php';
  203.  
  204. // Creo el Objeto controller
  205. $this->controllerObj = new $this->controller;
  206.  
  207.  
  208. } else {
  209. echo '<h4 style="color: red;">El controlador ' . $this->controller . ' no se reconoció.</h4>';
  210. die();
  211. }
  212. }
  213.  
  214. public function comprobarAccion() {
  215. // Si la accion existe como método en la clase del controller
  216. if (method_exists($this->controllerObj, $this->action)) {
  217. $accion = $this->action;
  218. // Ejecuto esa accion
  219. $this->controllerObj->$accion();
  220.  
  221. } else {
  222. echo '<h4 style="color: red;">La accion ' . $this->action . ' no se reconoció.</h4>';
  223. die();
  224. }
  225. }
  226. }
  227.  
  228. <?php
  229.  
  230. class Database {
  231.  
  232. private $host;
  233. private $user;
  234. private $password;
  235. private $dbname;
  236.  
  237. public function __construct() {
  238. $this->host = 'localhost';
  239. $this->user = 'root';
  240. $this->password = '';
  241. $this->dbname = 'mvcnotes';
  242. }
  243.  
  244. public function connect() {
  245. try {
  246. $conexion = new mysqli($this->host, $this->user, $this->password, $this->dbname);
  247. $conexion->query("SET NAMES 'utf8'");
  248. return $conexion;
  249.  
  250. } catch (Exception $e) {
  251. echo 'Error en Database.php -> El erro fue: ' . $e->getMessage();
  252. die();
  253. }
  254. }
  255. }
  256.  
  257. <?php
  258. require_once 'libs/Database.php';
  259.  
  260. class Nota {
  261.  
  262. private $db;
  263.  
  264. private $id;
  265. private $titulo;
  266. private $contenido;
  267. private $color;
  268. private $fecha;
  269.  
  270. public function __construct() {
  271. $database = new Database();
  272. $this->db = $database->connect();
  273. }
  274.  
  275. // METODOS PARA BBDD
  276. public function getAll() {
  277. $notas = false;
  278. $resultado = $this->db->query("SELECT * FROM notas ORDER BY id DESC");
  279. while ($row = $resultado->fetch_object('Nota')) {
  280. $notas[] = $row;
  281. }
  282.  
  283. return $notas;
  284. }
  285.  
  286. public function getById($id) {
  287. $nota = false;
  288. $resultado = $this->db->query("SELECT * FROM notas WHERE id = $id");
  289. if ($row = $resultado->fetch_object('Nota')) {
  290. $nota = $row;
  291. }
  292. return $nota;
  293. }
  294.  
  295. public function save($nota) {
  296. $resultado = $this->db->query("INSERT INTO notas(id, titulo, contenido, color, fecha) VALUES($nota->id, '$nota->titulo', '$nota->contenido', '$nota->color', $nota->fecha)");
  297. return $resultado;
  298. }
  299.  
  300. public function update($nota) {
  301. $resultado = $this->db->query("UPDATE notas SET titulo = '$nota->titulo', contenido = '$nota->contenido', color = '$nota->color', fecha = $nota->fecha WHERE id = $nota->id");
  302. return $resultado;
  303. }
  304.  
  305. public function delete($id) {
  306. $resultado = $this->db->query("DELETE FROM notas WHERE id = $id");
  307. return $resultado;
  308. }
  309.  
  310. // GETTERS
  311. public function getId() {
  312. return $this->id;
  313. }
  314.  
  315. public function getTitulo() {
  316. return $this->titulo;
  317. }
  318.  
  319. public function getContenido() {
  320. return $this->contenido;
  321. }
  322.  
  323. public function getColor() {
  324. return $this->color;
  325. }
  326.  
  327. public function getFecha() {
  328. return $this->fecha;
  329. }
  330.  
  331. // SETTERS
  332. public function setId($id) {
  333. $this->id = $id;
  334. }
  335.  
  336. public function setTitulo($titulo) {
  337. $this->titulo = $titulo;
  338. }
  339.  
  340. public function setContenido($contenido) {
  341. $this->contenido = $contenido;
  342. }
  343.  
  344. public function setColor($color) {
  345. $this->color = $color;
  346. }
  347.  
  348. public function setFecha($fecha) {
  349. $this->fecha = $fecha;
  350. }
  351.  
  352. }
  353.  
  354. * {
  355. margin: 0;
  356. padding: 0;
  357. }
  358.  
  359. body {
  360. width: 80%;
  361. background-color: blanchedalmond;
  362. margin: 0 auto;
  363. }
  364.  
  365. body, header, nav, main, section, article, aside, footer {
  366. box-sizing: border-box;
  367. }
  368.  
  369. /* CABECERA */
  370. header {
  371. width: 100%;
  372. height: 100px;
  373. background-color: cadetblue;
  374. display: flex;
  375. justify-content: center;
  376. align-items: center;
  377. }
  378.  
  379. /* MENU */
  380. nav {
  381. width: 100%;
  382. height: 50px;
  383. background: linear-gradient(lightseagreen, darkslategray);
  384. border-bottom: 2px solid black;
  385. display: flex;
  386. justify-content: space-around;
  387. align-items: center;
  388. overflow: hidden;
  389. }
  390. nav a {
  391. box-sizing: border-box;
  392. text-decoration: none;
  393. color: black;
  394. width: 50%;
  395. height: 50px;
  396. text-align: center;
  397. line-height: 50px;
  398. border: 1px solid black;
  399. }
  400.  
  401. nav a:hover {
  402. background-color:darkslategray;
  403. color: cadetblue;
  404. }
  405.  
  406. /* PRINCIPAL / MAIN */
  407. main {
  408. width: 100%;
  409. min-height: 300px;
  410. background-color: white;
  411. display: flex;
  412. flex-direction: column;
  413. justify-content: space-around;
  414. align-items: center;
  415. }
  416.  
  417. main > section {
  418. margin: 10px;
  419. width: 90%;
  420. min-height: 150px;
  421. padding: 20px;
  422. }
  423.  
  424. main > section span {
  425. color: darkslategrey;
  426. font-family: Consolas;
  427. }
  428.  
  429. main > section a:nth-child(2) {
  430. padding-left: 20px;
  431. }
  432.  
  433. .green {
  434. background-color: yellowgreen;
  435. }
  436.  
  437. .yellow {
  438. background-color: goldenrod;
  439. }
  440.  
  441. .blue {
  442. background-color: dodgerblue;
  443. }
  444.  
  445. form > p {
  446. padding: 20px;
  447. }
  448.  
  449. form textarea {
  450. width: 100%;
  451. height: 400px;
  452. }
  453.  
  454. /* PIE */
  455. footer {
  456. width: 100%;
  457. height: 100px;
  458. background-color: cadetblue;
  459. display: flex;
  460. justify-content: center;
  461. align-items: center;
  462. border-top: 2px solid darkslategray;
  463. }
  464.  
  465. footer span {
  466. font-weight: bolder;
  467. }
  468.  
  469. CREATE DATABASE mvcnotes;
  470.  
  471. USE mvcnotes;
  472.  
  473. CREATE TABLE notas (
  474. id int not null auto_increment,
  475. titulo varchar(250) not null,
  476. contenido text not null,
  477. color varchar(50) not null,
  478. fecha datetime not null,
  479. CONSTRAINT pk_notas PRIMARY KEY (id)
  480. );
  481.  
  482. </main>
  483.  
  484. <!-- FOOTER / PIE -->
  485. <footer>
  486. <p>Web, aplicación y modelo MVC creado por <span>Perico los Palotes xD</span> &copy; <?php echo date('Y');?></p>
  487. </footer>
  488. </body>
  489. </html>
  490.  
  491. <!DOCTYPE html>
  492. <html lang="es">
  493. <head>
  494. <meta charset="UTF-8">
  495. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  496. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  497. <title>MVC Notes</title>
  498. <link rel="stylesheet" type="text/css" href="<?=URL?>public/css/styles.css">
  499. </head>
  500. <body>
  501. <!-- HEADER / CABECERA -->
  502. <header>
  503. <h1> MVC Notes - Aplicación Web</h1>
  504. </header>
  505.  
  506. <!-- NAV / MENU -->
  507. <nav>
  508. <a href="<?=URL?>nota/index">Inicio</a>
  509. <a href="<?=URL?>nota/crearNota">Crear Nota</a>
  510. </nav>
  511.  
  512. <!-- MAIN / PRINCIPAL -->
  513. <main>
  514.  
  515. <section>
  516. <h3>Crear Nota</h3>
  517. <form action="<?=URL?>nota/guardarNota" method="POST">
  518. <p>
  519. <label for="titulo">Titulo: </label>
  520. <input type="text" name="titulo" required>
  521. </p>
  522. <p>
  523. <label for="color">Color: </label>
  524. <select name="color">
  525. <option value="yellow" selected>Amarillo</option>
  526. <option value="blue">Azul</option>
  527. <option value="green">Verde</option>
  528. </select>
  529. </p>
  530. <p>
  531. <label for="contenido">Contenido: </label>
  532. <textarea name="contenido" required></textarea>
  533. </p>
  534. <p>
  535. <input type="submit" name="submit" value="Crear Nota">
  536. <input type="reset" value="Reiniciar Datos">
  537. </p>
  538. </form>
  539. </section>
  540.  
  541. <section>
  542. <h3>Editar Nota</h3>
  543. <form action="<?=URL?>nota/actualizar" method="POST">
  544. <p>
  545. <label for="titulo">Titulo: </label>
  546. <input type="text" name="titulo" required value="<?= $nota->getTitulo() ?>">
  547. </p>
  548. <p>
  549. <label for="color">Color: </label>
  550. <select name="color">
  551. <option value="yellow" <?php if ($nota->getColor() == 'yellow') { echo 'selected'; } ?>>Amarillo</option>
  552. <option value="blue" <?php if ($nota->getColor() == 'blue') { echo 'selected'; } ?>>Azul</option>
  553. <option value="green" <?php if ($nota->getColor() == 'green') { echo 'selected'; } ?>>Verde</option>
  554. </select>
  555. </p>
  556. <p>
  557. <label for="contenido">Contenido: </label>
  558. <textarea name="contenido" required><?= $nota->getContenido() ?></textarea>
  559. </p>
  560. <p>
  561. <input type="submit" name="submit" value="Editar Nota">
  562. <input type="reset" value="Reiniciar Datos">
  563. </p>
  564. <input type="hidden" name="id" value="<?= $nota->getId() ?>">
  565. </form>
  566. </section>
  567.  
  568. <?php if ($notas) : ?>
  569. <?php foreach($notas as $nota): ?>
  570. <section class="seccion <?= $nota->getColor() ?>">
  571. <article>
  572. <h3><?= $nota->getTitulo(); ?></h3>
  573. <span><?= $nota->getFecha() ?></span>
  574. <p><?= $nota->getContenido(); ?></p>
  575. </article>
  576. <div id="botones" class="botones">
  577. <a href="<?=URL?>nota/editar/<?= $nota->getId() ?>">Editar</a>
  578. <a href="<?=URL?>nota/eliminar/<?= $nota->getId() ?>">Eliminar</a>
  579. </div>
  580. </section>
  581. <?php endforeach; ?>
  582. <?php else: ?>
  583. <h3>Wops! No se encontraron notas en la BBDD. Está vacia.</h3>
  584. <?php endif; ?>
  585.  
  586. <IfModule mod_rewrite.c>
  587. RewriteEngine On
  588.  
  589. RewriteCond %{REQUEST_FILENAME} !-f
  590. RewriteCond %{REQUEST_FILENAME} !-d
  591. RewriteCond %{REQUEST_FILENAME} !-l
  592.  
  593. RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
  594. </IfModule>
  595.  
  596. <?php
  597. require_once 'libs/app.php';
  598.  
  599. $app = new App();
Add Comment
Please, Sign In to add comment