159 lines
4.4 KiB
PHP
159 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Web;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Response;
|
|
|
|
class SiteController extends Controller
|
|
{
|
|
/** @var array<string,string> */
|
|
private const CATEGORY_TO_SLUG = [
|
|
'Smileys & Emotion' => 'smileys',
|
|
'People & Body' => 'people',
|
|
'Animals & Nature' => 'animals',
|
|
'Food & Drink' => 'food',
|
|
'Travel & Places' => 'travel',
|
|
'Activities' => 'activities',
|
|
'Objects' => 'objects',
|
|
'Symbols' => 'symbols',
|
|
'Flags' => 'flags',
|
|
];
|
|
|
|
public function home(Request $request): View
|
|
{
|
|
return view('site.home', [
|
|
'initialQuery' => trim((string) $request->query('q', '')),
|
|
'initialCategory' => trim((string) $request->query('category', '')),
|
|
'initialSubcategory' => trim((string) $request->query('subcategory', '')),
|
|
'canonicalPath' => '/',
|
|
]);
|
|
}
|
|
|
|
public function browse(Request $request): RedirectResponse|View
|
|
{
|
|
$cat = strtolower(trim((string) $request->query('cat', 'all')));
|
|
if ($cat !== '' && $cat !== 'all' && array_key_exists($cat, $this->categorySlugMap())) {
|
|
return redirect('/'.$cat, 301);
|
|
}
|
|
|
|
return view('site.home', [
|
|
'initialQuery' => trim((string) $request->query('q', '')),
|
|
'initialCategory' => trim((string) $request->query('category', '')),
|
|
'initialSubcategory' => trim((string) $request->query('subcategory', '')),
|
|
'canonicalPath' => '/browse',
|
|
]);
|
|
}
|
|
|
|
public function category(string $categorySlug): View
|
|
{
|
|
if ($categorySlug === 'all') {
|
|
return view('site.home', [
|
|
'initialQuery' => '',
|
|
'initialCategory' => '',
|
|
'initialSubcategory' => '',
|
|
'canonicalPath' => '/',
|
|
]);
|
|
}
|
|
|
|
$categoryLabel = $this->categorySlugMap()[$categorySlug] ?? '';
|
|
abort_if($categoryLabel === '', 404);
|
|
|
|
return view('site.home', [
|
|
'initialQuery' => '',
|
|
'initialCategory' => $categoryLabel,
|
|
'initialSubcategory' => '',
|
|
'canonicalPath' => '/'.$categorySlug,
|
|
]);
|
|
}
|
|
|
|
public function categorySubcategory(string $categorySlug, string $subcategorySlug): View
|
|
{
|
|
if ($categorySlug === 'all') {
|
|
abort(404);
|
|
}
|
|
|
|
$categoryLabel = $this->categorySlugMap()[$categorySlug] ?? '';
|
|
abort_if($categoryLabel === '', 404);
|
|
|
|
return view('site.home', [
|
|
'initialQuery' => '',
|
|
'initialCategory' => $categoryLabel,
|
|
'initialSubcategory' => $subcategorySlug,
|
|
'canonicalPath' => '/'.$categorySlug.'/'.$subcategorySlug,
|
|
]);
|
|
}
|
|
|
|
public function apiDocs(): View
|
|
{
|
|
return view('site.api-docs');
|
|
}
|
|
|
|
public function pricing(): View
|
|
{
|
|
return view('site.pricing');
|
|
}
|
|
|
|
public function privacy(): View
|
|
{
|
|
return view('site.privacy');
|
|
}
|
|
|
|
public function terms(): View
|
|
{
|
|
return view('site.terms');
|
|
}
|
|
|
|
public function emojiDetail(string $slug): View|Response
|
|
{
|
|
$dataPath = (string) config('dewemoji.data_path');
|
|
if (!is_file($dataPath)) {
|
|
abort(500, 'Emoji dataset file not found.');
|
|
}
|
|
|
|
$raw = file_get_contents($dataPath);
|
|
if ($raw === false) {
|
|
abort(500, 'Emoji dataset file could not be read.');
|
|
}
|
|
|
|
$decoded = json_decode($raw, true);
|
|
if (!is_array($decoded)) {
|
|
abort(500, 'Emoji dataset JSON is invalid.');
|
|
}
|
|
|
|
$items = $decoded['emojis'] ?? [];
|
|
$match = null;
|
|
foreach ($items as $item) {
|
|
if (($item['slug'] ?? '') === $slug) {
|
|
$match = $item;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!$match) {
|
|
abort(404);
|
|
}
|
|
|
|
return view('site.emoji-detail', [
|
|
'emoji' => $match,
|
|
'canonicalPath' => '/emoji/'.$slug,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @return array<string,string>
|
|
*/
|
|
private function categorySlugMap(): array
|
|
{
|
|
$out = [];
|
|
foreach (self::CATEGORY_TO_SLUG as $label => $slug) {
|
|
$out[$slug] = $label;
|
|
}
|
|
|
|
return $out;
|
|
}
|
|
}
|