vlw.se/public/search.php

72 lines
No EOL
2.3 KiB
PHP

<?php
use VLW\Database\Models\Search\Search;
use const VLW\{ICONS_DIR, DEFAULT_BUTTON_ICON};
use VLW\Database\Tables\Search\{SearchTable, SearchCategoryEnum};
require_once VV::root("src/Consts.php");
require_once VV::root("src/Database/Tables/Search/Search.php");
require_once VV::root("src/Database/Models/Search/Search.php");
$search = new class extends Search {
public function __construct() {}
public static function get_query(): ?string {
return $_GET[SearchTable::QUERY->value] ?? null;
}
public static function get_category(): ?SearchCategoryEnum {
return SearchCategoryEnum::tryFromName($_GET[SearchTable::CATEGORY->value] ?? "");
}
public function search(): array {
return parent::all([SearchTable::QUERY->value => self::get_query()]);
}
}
?>
<style><?= VV::css("public/assets/css/pages/search") ?></style>
<section class="search">
<form>
<input name="<?= SearchTable::QUERY->value ?>" type="search" placeholder="search vlw.se..." value="<?= $search::get_query() ?>">
<select name="<?= SearchTable::CATEGORY->value ?>">
<option value="null">All</option>
<optgroup label="Categories">
<?php foreach (SearchCategoryEnum::names() as $category): ?>
<?php $category = SearchCategoryEnum::fromName($category); ?>
<option value="<?= $category->name ?>" <?= $search::get_category() === $category ? "selected" : "" ?>><?= ucfirst(strtolower($category->name)) ?></option>
<?php endforeach; ?>
</optgroup>
</select>
<button type="submit" class="inline solid"><?= VV::embed(ICONS_DIR . "search.svg") ?></button>
</form>
</section>
<?php if (array_key_exists(SearchTable::QUERY->value, $_GET)): ?>
<?php foreach ($search->search() as $result): ?>
<section class="result">
<?php if ($result->title()): ?>
<h2><?= $result->title() ?></h2>
<?php endif; ?>
<p><?= $result->summary() ?></p>
<?php if ($result->href()): ?>
<a href="<?= $result->href() ?>"><button class="inline">
<p>read more</p>
<?= VV::embed(DEFAULT_BUTTON_ICON) ?>
</button></a>
<?php endif; ?>
</section>
<?php endforeach; ?>
<?php else: ?>
<section class="start-search">
<?= VV::embed(ICONS_DIR . "search.svg") ?>
<p>Start typing to search</p>
</section>
<?php endif; ?>
<script><?= VV::js("public/assets/js/pages/search") ?></script>