Bladeren bron

Отображение поста, комментарии, 404 ошибка

Alexander Musikhin 2 jaren geleden
bovenliggende
commit
221a1f1ea1

+ 11 - 0
app/controllers/controller_404.php

@@ -0,0 +1,11 @@
+<?php
+
+class controller_404 extends Controller
+{
+
+    function index()
+    {
+        $this->view->generate('404.php');
+    }
+
+}

+ 14 - 10
app/controllers/controller_main.php

@@ -8,35 +8,39 @@ class controller_main extends Controller
             header('Location: /user/login');
         }
         $this->model = new Model_main();
-
     }
 
     function index()
     {
-
-
         $data['users'] = $this->get_users();
-
         $data['posts'] = $this->model->get_posts();
-
-
         $this->view->generate('main_view.php', $data);
-
     }
 
     public function createpost(){
         if(isset($_POST['caption'], $_POST['post'])){
-            $this->model->create_post($_POST['caption'], $_POST['post']);
+            $this->model->create_post(htmlentities($_POST['caption']), htmlentities($_POST['post']));
         }
         header('Location: /');
     }
 
-    public function post($id){
+    public function post($id)
+    {
         if(!$id) Route::ErrorPage404();
         $data['post'] = $this->model->get_post($id);
+        if(!$data['post']) Route::ErrorPage404();
         $data['users'] = $this->get_users();
-        var_dump($data);
+        $data['comments'] = $this->model->get_comments($id);
+        $this->view->generate('post.php', $data);
+    }
 
+    public function add_comment($post_id)
+    {
+        if(isset($_POST['comment'])){
+            $user_id = $_SESSION['user']['id'];
+            $this->model->add_comment($post_id, $user_id, htmlentities($_POST['comment']));
+        }
+        header('Location: /main/post/' . $post_id);
     }
 
     private function get_users(): array

+ 0 - 5
app/core/model.php

@@ -9,9 +9,4 @@ class model
         $this->db = new db();
     }
 
-    public function get_data()
-    {
-
-
-    }
 }

+ 2 - 2
app/core/route.php

@@ -89,9 +89,9 @@ class Route
 
     public  static function ErrorPage404()
     {
-        $host = 'http://'.$_SERVER['HTTP_HOST'].'/';
+        $host = '/';
         header('HTTP/1.1 404 Not Found');
         header("Status: 404 Not Found");
-        header('Location:'.$host.'404');
+        header('Location: /404');
     }
 }

+ 15 - 1
app/models/model_main.php

@@ -9,7 +9,9 @@ class Model_main extends Model
 
     public function get_post($id){
         $q = "SELECT * FROM `posts` WHERE `id` = " . $this->db->escape($id);
-        return $this->db->query($q);
+        $ret = $this->db->query($q);
+        if(!empty($ret[0])) return $ret[0];
+        return false;
     }
 
     public function create_post($caption, $post){
@@ -17,7 +19,19 @@ class Model_main extends Model
             ", `post` = " . $this->db->escape($post) .
             ", `user_id` = " . $this->db->escape($_SESSION['user']['id']);
         $this->db->query($q);
+    }
+
+    public function get_comments($post_id){
+        $q = "SELECT * FROM `comments` WHERE `post_id` = " . $this->db->escape($post_id) . " ORDER BY `created_at` DESC";
+        return $this->db->query($q);
+    }
 
+    public function add_comment($post_id, $user_id, $comment){
+        $q = "INSERT INTO `comments` SET ".
+            " `post_id` = " . $this->db->escape($post_id) .
+            ", `user_id` = " . $this->db->escape($user_id) .
+            ", `comment` = " . $this->db->escape($comment);
+        $this->db->query($q);
     }
 
 }

+ 10 - 0
app/views/404.php

@@ -0,0 +1,10 @@
+<?php
+include 'app/views/header.php';
+?>
+
+
+<div class="row mt-5">
+    <div class="col-12 mt-5 fs-1 text-center">
+        404 - страница не найдена
+    </div>
+</div>

+ 9 - 0
app/views/header.php

@@ -0,0 +1,9 @@
+<div class="row mt-3">
+    <div class="col-6">
+        <h1><a href="/">Блог</a></h1>
+    </div>
+    <div class="col-6 text-end">
+        <?=$_SESSION['user']['login']; ?>
+        <a class="ms-5" href="/user/logout">Выход</a>
+    </div>
+</div>

+ 8 - 12
app/views/main_view.php

@@ -1,12 +1,6 @@
-<div class="row mt-3">
-    <div class="col-6">
-        <h1>Блог</h1>
-    </div>
-    <div class="col-6 text-end">
-        <?=$_SESSION['user']['login']; ?>
-        <a href="/user/logout">Выход</a>
-    </div>
-</div>
+<?php
+include 'app/views/header.php';
+?>
 
 <form action="/main/createpost" method="post">
     <div class="row my-3">
@@ -31,8 +25,10 @@
 <div class="row my-3">
     <div class="col-12 mb-2">
         <span class="me-3">Автор: <strong><?=$users[$post['user_id']]; ?></strong></span>
-        <span class="me-3">Создан <? echo date('d.m.Y', strtotime($post['created_at'])); ?></span>
-        <span class="me-3">Изменён <? echo date('d.m.Y', strtotime($post['updated_at'])); ?></span>
+        <span class="me-3">Создан <? echo date('d.m.Y', strtotime($post['created_at'])) . ' ' .
+                date('H:i', strtotime($post['created_at'])); ?></span>
+        <span class="me-3">Изменён <? echo date('d.m.Y', strtotime($post['updated_at'])) . ' ' .
+                date('H:i', strtotime($post['updated_at'])); ?></span>
     </div>
     <div class="col-12 mb-5 pb-2 border-bottom">
         <h5><a href="/main/post/<?=$post['id']; ?>"><? echo $post['caption']; ?></a></h5>
@@ -44,5 +40,5 @@
 
 <? endforeach; ?>
 <?php
-var_dump($users);
+//var_dump($users);
 ?>

+ 50 - 0
app/views/post.php

@@ -0,0 +1,50 @@
+<?php
+include 'app/views/header.php';
+?>
+<div class="col-12">
+    <h2><?=$post['caption']; ?></h2>
+    <div class="col-12 mb-2">
+        <span class="me-3">Автор: <strong><?=$users[$post['user_id']]; ?></strong></span>
+        <span class="me-3">Создан <? echo date('d.m.Y', strtotime($post['created_at'])) . ' ' .
+                date('H:i', strtotime($post['created_at'])); ?></span>
+        <span class="me-3">Изменён <? echo date('d.m.Y', strtotime($post['updated_at'])) . ' ' .
+                date('H:i', strtotime($post['updated_at'])); ?></span>
+    </div>
+</div>
+<div class="row mt-4">
+    <div class="col-12">
+        <? echo nl2br($post['post']); ?>
+    </div>
+</div>
+
+<div class="row mt-4">
+    <div class="col-12">
+        <h5>Комментарии</h5>
+    </div>
+    <? if (empty($comments)): ?>
+    <div class="col-12 fs-3 my-5 text-center">
+        Комментариев пока нет...
+    </div>
+    <? else:
+        foreach ($comments as $comment): ?>
+        <div class="col-12 my-3 border-bottom">
+            <span class="me-3">Автор: <strong><?=$users[$comment['user_id']]; ?></strong></span>
+            <span class="me-3">Создан <? echo date('d.m.Y', strtotime($comment['created_at'])) . ' ' .
+                    date('H:i', strtotime($comment['created_at'])); ?></span><br>
+            <p><? echo nl2br($comment['comment']); ?></p>
+        </div>
+
+    <?
+        endforeach;
+    endif; ?>
+    <form action="/main/add_comment/<? echo $post['id']; ?>" method="post">
+        <div class="row">
+        <div class="col-10">
+            <textarea required class="form-control" name="comment" placeholder="оставьте свой комментарий к посту"></textarea>
+        </div>
+        <div class="col-2  mt-2">
+            <button class="btn btn-primary">Сохранить</button>
+        </div>
+        </div>
+    </form>
+</div>