aboutsummaryrefslogtreecommitdiff
path: root/app/Models/GuestbookEntry.php
blob: f5e2de2169e47d1d9c650c2755b8241f41c04c38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php

namespace App\Models;

use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class GuestbookEntry extends Model
{
    use HasFactory;
    protected $table = "guestbook__entries";
    protected $fillable = ['name', 'message'];

    /**
     * Creates a new guestbook entry.
     *
     * @param Request $request The HTTP POST request
     * @return void
     */
    public static function insertGuestbookEntry(Request $request) {
        $newEntry = new GuestbookEntry;
        $newEntry->name = htmlspecialchars($request->get('name'));
        $newEntry->message = htmlspecialchars($request->get('message'));
        $newEntry->ip = $request->ip();
        $newEntry->agent = $request->userAgent();
        $newEntry->admin = auth()->check();
        $newEntry->save();
    }

    public static function selectEntries() {
        $entries = GuestbookEntry::orderBy('created_at', 'desc')->get();
        return $entries;
    }

    public static function importGuestbookEntry(array $data) {
        foreach ($data as $entry) {
            $dt = new \DateTime('@' . $entry['timestamp']);
            $newEntry = new GuestbookEntry;
            $newEntry->name = $entry['name'];
            $newEntry->ip = $entry['ip_address'];
            $newEntry->agent = $entry['agent'];
            $newEntry->admin = $entry['site_owner'];
            $newEntry->message = $entry['message'];
            $newEntry->created_at = $dt;
            $newEntry->updated_at = $dt;
            $newEntry->save();
        }
    }
}