Browse Source

Added viewing of data

AItEKS 1 week ago
parent
commit
7fea24c5db

+ 32 - 6
form-app/app/Http/Controllers/FormController.php

@@ -4,9 +4,17 @@ namespace App\Http\Controllers;
 
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\File;
+use Carbon\Carbon;
 
 class FormController extends Controller
 {
+    private $path;
+
+    public function __construct()
+    {
+        $this->path = storage_path('app/forms/contacts.json');
+    }
+    
     public function index() 
     {
         return view('form');
@@ -20,21 +28,39 @@ class FormController extends Controller
             'message' => 'required|string|min:10|max:100'
         ]);
 
-        $path = storage_path('app/forms/contacts.json');
-
-        File::ensureDirectoryExists(dirname($path));
+        File::ensureDirectoryExists(dirname($this->path));
 
         $data = [];
-        if (File::exists($path)) {
-            $content = File::get($path);
+        if (File::exists($this->path)) {
+            $content = File::get($this->path);
             $data = json_decode($content, true) ?: [];
         }
 
         $validated['created_at'] = now()->toDateTimeString();
         $data[] = $validated;
 
-        File::put($path, json_encode($data, JSON_UNESCAPED_UNICODE));
+        File::put($this->path, json_encode($data, JSON_UNESCAPED_UNICODE));
 
         return redirect()->back()->with('success', 'Данные сохранены!');
     }
+
+    public function showData() 
+    {
+        $data = [];
+        if (File::exists($this->path)) {
+            $content = File::get($this->path);
+            $data = json_decode($content, true) ?: [];
+        }
+
+        $submissions = collect($data)->map(function ($item) {
+            return (object) [
+                'name' => $item['name'],
+                'email' => $item['email'],
+                'message' => $item['message'],
+                'created_at' => Carbon::parse($item['created_at'])
+            ];
+        }) -> sortBy('createdAt');
+
+        return view('admin', compact('submissions'));
+    }
 }

+ 214 - 0
form-app/resources/views/admin.blade.php

@@ -0,0 +1,214 @@
+<!DOCTYPE html>
+<html lang="ru">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Все отправленные данные</title>
+    <style>
+        * { margin: 0; padding: 0; box-sizing: border-box; }
+        body {
+            font-family: Arial, sans-serif;
+            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
+            min-height: 100vh;
+            padding: 40px 20px;
+        }
+        .container {
+            background: white;
+            padding: 40px;
+            border-radius: 10px;
+            box-shadow: 0 10px 40px rgba(0,0,0,0.2);
+            max-width: 1200px;
+            margin: 0 auto;
+        }
+        h1 {
+            color: #333;
+            margin-bottom: 10px;
+            text-align: center;
+        }
+        .subtitle {
+            color: #666;
+            text-align: center;
+            margin-bottom: 30px;
+            font-size: 14px;
+        }
+        .header-actions {
+            display: flex;
+            justify-content: space-between;
+            align-items: center;
+            margin-bottom: 20px;
+            flex-wrap: wrap;
+            gap: 10px;
+        }
+        .btn {
+            padding: 10px 20px;
+            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
+            color: white;
+            border: none;
+            border-radius: 5px;
+            font-size: 14px;
+            font-weight: bold;
+            cursor: pointer;
+            text-decoration: none;
+            display: inline-block;
+            transition: transform 0.2s;
+        }
+        .btn:hover {
+            transform: translateY(-2px);
+        }
+        .btn-secondary {
+            background: linear-gradient(135deg, #a8a8a8 0%, #7f7f7f 100%);
+        }
+        .count-badge {
+            background: #667eea;
+            color: white;
+            padding: 5px 15px;
+            border-radius: 20px;
+            font-size: 14px;
+            font-weight: bold;
+        }
+        .table-wrapper {
+            overflow-x: auto;
+            margin-top: 20px;
+        }
+        table {
+            width: 100%;
+            border-collapse: collapse;
+            background: white;
+        }
+        thead {
+            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
+        }
+        thead th {
+            color: white;
+            padding: 15px;
+            text-align: left;
+            font-weight: bold;
+            font-size: 14px;
+        }
+        tbody tr {
+            border-bottom: 1px solid #e1e1e1;
+            transition: background-color 0.2s;
+        }
+        tbody tr:hover {
+            background-color: #f8f9fa;
+        }
+        tbody tr:last-child {
+            border-bottom: none;
+        }
+        td {
+            padding: 15px;
+            color: #333;
+            font-size: 14px;
+        }
+        .message-cell {
+            max-width: 300px;
+            word-wrap: break-word;
+            white-space: pre-wrap;
+        }
+        .date-cell {
+            white-space: nowrap;
+            color: #666;
+            font-size: 13px;
+        }
+        .email-cell {
+            color: #667eea;
+            font-weight: 500;
+        }
+        .empty-state {
+            text-align: center;
+            padding: 60px 20px;
+            color: #999;
+        }
+        .empty-state-icon {
+            font-size: 64px;
+            margin-bottom: 20px;
+        }
+        .alert {
+            padding: 15px;
+            border-radius: 5px;
+            margin-bottom: 20px;
+        }
+        .alert-success {
+            background: #d4edda;
+            color: #155724;
+            border: 1px solid #c3e6cb;
+        }
+        
+        @media (max-width: 768px) {
+            .container {
+                padding: 20px;
+            }
+            table {
+                font-size: 12px;
+            }
+            thead th, td {
+                padding: 10px 8px;
+            }
+            .message-cell {
+                max-width: 150px;
+            }
+        }
+    </style>
+</head>
+<body>
+    <div class="container">
+        <h1>📋 Все отправленные данные</h1>
+        <p class="subtitle">Laravel {{ app()->version() }} | Данные из формы</p>
+
+        @if(session('success'))
+            <div class="alert alert-success">
+                {{ session('success') }}
+            </div>
+        @endif
+
+        <div class="header-actions">
+            <div class="count-badge">
+                Всего записей: {{ $submissions->count() }}
+            </div>
+            <a href="{{ route('form') }}" class="btn btn-secondary">
+                ← Вернуться к форме
+            </a>
+        </div>
+
+        <div class="table-wrapper">
+            @if($submissions->count() > 0)
+                <table>
+                    <thead>
+                        <tr>
+                            <th>#</th>
+                            <th>Имя</th>
+                            <th>Email</th>
+                            <th>Сообщение</th>
+                            <th>Дата отправки</th>
+                        </tr>
+                    </thead>
+                    <tbody>
+                        @foreach($submissions as $index => $submission)
+                            <tr>
+                                <td>{{ $index + 1 }}</td>
+                                <td>{{ $submission->name }}</td>
+                                <td class="email-cell">{{ $submission->email }}</td>
+                                <td class="message-cell">{{ $submission->message }}</td>
+                                <td class="date-cell">
+                                    {{ $submission->created_at->format('d.m.Y H:i') }}
+                                </td>
+                            </tr>
+                        @endforeach
+                    </tbody>
+                </table>
+            @else
+                <div class="empty-state">
+                    <div class="empty-state-icon">📭</div>
+                    <h3>Данных пока нет</h3>
+                    <p style="margin-top: 10px; color: #999;">
+                        Отправьте форму, чтобы увидеть данные здесь
+                    </p>
+                    <a href="{{ route('form.index') }}" class="btn" style="margin-top: 20px;">
+                        Перейти к форме
+                    </a>
+                </div>
+            @endif
+        </div>
+    </div>
+</body>
+</html>

+ 5 - 0
form-app/resources/views/form.blade.php

@@ -99,6 +99,11 @@
 <body>
     <div class="container">
         <h1>Laravel Форма</h1>
+        <div style="text-align: center; margin-bottom: 20px;">
+            <a href="{{ route('admin.index') }}" style="color: #667eea; text-decoration: none; font-size: 14px;">
+                📋 Посмотреть все отправленные данные
+            </a>
+        </div>
         <p class="subtitle">Laravel {{ app()->version() }} | PHP {{ phpversion() }}</p>
 
         @if(session('success'))

+ 4 - 1
form-app/routes/web.php

@@ -2,6 +2,9 @@
 
 use Illuminate\Support\Facades\Route;
 use App\Http\Controllers\FormController;
+use App\Http\Controllers\AdminController;
 
 Route::get('/', [FormController::class, 'index'])->name('form');
-Route::post('/', [FormController::class, 'submit'])->name('form.submit'); 
+Route::post('/', [FormController::class, 'submit'])->name('form.submit'); 
+
+Route::get('/admin', [FormController::class, 'showData'])->name('admin.index');