107 lines
3.6 KiB
PHP
107 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Legacy;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class InterviewController extends Controller
|
|
{
|
|
public function show(Request $request, $id, $slug = null)
|
|
{
|
|
$id = (int) $id;
|
|
|
|
// Handle comment POST
|
|
if ($request->isMethod('post')) {
|
|
$action = $request->input('action');
|
|
if ($action === 'store' && (!empty($_SESSION['web_login']['user_type']) && $_SESSION['web_login']['user_type'] > 1)) {
|
|
$comment = $request->input('comment');
|
|
$tekst = nl2br(htmlspecialchars($comment ?? '', ENT_QUOTES, 'UTF-8'));
|
|
$interviewId = (int) $request->input('interview_id');
|
|
|
|
try {
|
|
DB::connection('legacy')->table('interviews_comment')->insert([
|
|
'nid' => $interviewId,
|
|
'author' => $_SESSION['web_login']['username'] ?? 'Anonymous',
|
|
'datum' => DB::raw('CURRENT_TIMESTAMP'),
|
|
'tekst' => $tekst,
|
|
]);
|
|
|
|
$ar2 = DB::connection('legacy')->table('users')
|
|
->where('uname', $_SESSION['web_login']['username'])
|
|
->first();
|
|
|
|
if (!empty($ar2->user_id)) {
|
|
DB::connection('legacy')->table('users_statistics')
|
|
->where('user_id', $ar2->user_id)
|
|
->increment('newscomment');
|
|
}
|
|
} catch (\Throwable $e) {
|
|
// fail silently
|
|
}
|
|
}
|
|
}
|
|
|
|
try {
|
|
$ar = DB::connection('legacy')->table('interviews')->where('id', $id)->first();
|
|
} catch (\Throwable $e) {
|
|
$ar = null;
|
|
}
|
|
|
|
if (! $ar) {
|
|
return redirect('/interviews');
|
|
}
|
|
|
|
try {
|
|
$artworks = DB::connection('legacy')->table('wallz')
|
|
->where('uname', $ar->username)
|
|
->inRandomOrder()
|
|
->limit(2)
|
|
->get();
|
|
} catch (\Throwable $e) {
|
|
$artworks = collect();
|
|
}
|
|
|
|
try {
|
|
$comments = DB::connection('legacy')->table('interviews_comment as c')
|
|
->leftJoin('users as u', 'u.uname', '=', 'c.author')
|
|
->where('c.nid', $id)
|
|
->select('c.*', 'u.user_id', 'u.user_type', 'u.signature', 'u.icon')
|
|
->orderBy('c.datum')
|
|
->get();
|
|
} catch (\Throwable $e) {
|
|
$comments = collect();
|
|
}
|
|
|
|
// compute total posts per author across interviews_comment
|
|
$authors = $comments->pluck('author')->unique()->values()->all();
|
|
$postCounts = [];
|
|
if (!empty($authors)) {
|
|
try {
|
|
$counts = DB::connection('legacy')->table('interviews_comment')
|
|
->select('author', DB::raw('COUNT(*) as cnt'))
|
|
->whereIn('author', $authors)
|
|
->groupBy('author')
|
|
->get();
|
|
|
|
foreach ($counts as $c) {
|
|
$postCounts[$c->author] = $c->cnt;
|
|
}
|
|
} catch (\Throwable $e) {
|
|
// ignore
|
|
}
|
|
}
|
|
|
|
$page_title = 'Interview with ' . ($ar->username ?? '');
|
|
|
|
return view('legacy.interview', [
|
|
'ar' => $ar,
|
|
'artworks' => $artworks,
|
|
'comments' => $comments,
|
|
'postCounts' => $postCounts,
|
|
'page_title' => $page_title,
|
|
]);
|
|
}
|
|
}
|