v1.0.0.0
StocketBase
- <?php
-
- declare(strict_types=1);
-
- require_once __DIR__ . '/includes/config.php';
- require_once __DIR__ . '/includes/database.php';
- require_once __DIR__ . '/includes/user-auth.php';
- require_once __DIR__ . '/includes/language.php';
- require_once __DIR__ . '/includes/site-front.php';
- require_once __DIR__ . '/includes/pagination.php';
- require_once __DIR__ . '/includes/product-card.php';
- require_once __DIR__ . '/includes/product-search.php';
- require_once __DIR__ . '/includes/tags.php';
-
- UserAuth::boot();
-
- $db = Database::site();
- $settings = SiteFront::settings();
-
- if ($settings['search_enabled'] !== '1') {
- http_response_code(404);
- $pageTitle = 'Not found - ' . $settings['site_name'];
- require __DIR__ . '/includes/front-header.php';
- echo '<p>' . htmlspecialchars(Language::get('search_not_available', 'Search is not available.')) . '</p>';
- require __DIR__ . '/includes/front-footer.php';
- exit;
- }
-
- $query = trim((string) ($_GET['q'] ?? ''));
- $categoryId = (int) ($_GET['category'] ?? 0);
- $tagId = (int) ($_GET['tag'] ?? 0);
- $sort = ProductSearch::sanitizeSort((string) ($_GET['sort'] ?? 'newest'));
- $tagsEnabled = ProductTags::isEnabled();
- if (!$tagsEnabled) {
- $tagId = 0;
- }
-
- $categories = $db->query('SELECT id, name FROM categories ORDER BY name ASC')->fetchAll();
- $tags = $tagsEnabled ? $db->query('SELECT id, name FROM tags ORDER BY name ASC')->fetchAll() : [];
-
- $hasFilters = $categoryId > 0 || $tagId > 0 || $sort !== 'newest';
-
- $products = [];
- $totalProducts = 0;
- $totalPages = 0;
- $currentPage = Pagination::currentPage();
- $perPage = max(1, (int) $settings['products_per_page_home']);
-
- if ($query !== '' || $hasFilters) {
- $result = ProductSearch::run(
- $settings,
- ['q' => $query, 'category' => $categoryId, 'tag' => $tagId, 'sort' => $sort],
- $currentPage,
- $perPage
- );
- $products = $result['products'];
- $totalProducts = $result['total'];
- $totalPages = $result['totalPages'];
- $currentPage = $result['page'];
- $perPage = $result['perPage'];
- }
-
- $isStaticProductLoading = ($settings['product_loading_mode'] ?? 'scroll') === 'static';
-
- $paginationExtraParams = ['q' => $query];
- if ($categoryId > 0) {
- $paginationExtraParams['category'] = $categoryId;
- }
- if ($tagId > 0) {
- $paginationExtraParams['tag'] = $tagId;
- }
- if ($sort !== 'newest') {
- $paginationExtraParams['sort'] = $sort;
- }
-
- $pageTitle = Language::get('nav_search', 'Search') . ' - ' . $settings['site_name'];
- $pageDescription = '';
-
- require __DIR__ . '/includes/front-header.php';
-
- ?>
- <h1 class="category-title"><?= htmlspecialchars(Language::get('nav_search', 'Search')) ?></h1>
-
- <form action="search.php" method="get" class="site-search-form site-search-form-page" role="search">
- <span class="site-search-form-icon">
- <svg class="icon icon-sm" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
- <circle cx="11" cy="11" r="7"></circle>
- <line x1="21" y1="21" x2="16.65" y2="16.65"></line>
- </svg>
- </span>
- <input type="text" name="q" class="site-search-input" value="<?= htmlspecialchars($query) ?>" placeholder="<?= htmlspecialchars(Language::get('search_placeholder', 'Search')) ?>" aria-label="<?= htmlspecialchars(Language::get('search_placeholder', 'Search')) ?>" autofocus>
- <button type="submit" class="site-search-submit"><?= htmlspecialchars(Language::get('search_button', 'Search')) ?></button>
- </form>
-
- <form action="search.php" method="get" class="search-filters">
- <input type="hidden" name="q" value="<?= htmlspecialchars($query, ENT_QUOTES) ?>">
-
- <label class="search-filter-field">
- <span><?= htmlspecialchars(Language::get('search_filter_category', 'Category')) ?></span>
- <select name="category" onchange="this.form.submit()">
- <option value="0"><?= htmlspecialchars(Language::get('search_filter_all_categories', 'All categories')) ?></option>
- <?php foreach ($categories as $categoryOption): ?>
- <option value="<?= (int) $categoryOption['id'] ?>" <?= $categoryId === (int) $categoryOption['id'] ? 'selected' : '' ?>><?= htmlspecialchars($categoryOption['name']) ?></option>
- <?php endforeach; ?>
- </select>
- </label>
-
- <?php if ($tagsEnabled && !empty($tags)): ?>
- <label class="search-filter-field">
- <span><?= htmlspecialchars(Language::get('search_filter_tag', 'Tag')) ?></span>
- <select name="tag" onchange="this.form.submit()">
- <option value="0"><?= htmlspecialchars(Language::get('search_filter_all_tags', 'All tags')) ?></option>
- <?php foreach ($tags as $tagOption): ?>
- <option value="<?= (int) $tagOption['id'] ?>" <?= $tagId === (int) $tagOption['id'] ? 'selected' : '' ?>><?= htmlspecialchars($tagOption['name']) ?></option>
- <?php endforeach; ?>
- </select>
- </label>
- <?php endif; ?>
-
- <label class="search-filter-field">
- <span><?= htmlspecialchars(Language::get('search_filter_sort', 'Sort by')) ?></span>
- <select name="sort" onchange="this.form.submit()">
- <option value="newest" <?= $sort === 'newest' ? 'selected' : '' ?>><?= htmlspecialchars(Language::get('search_sort_newest', 'Publish date (newest)')) ?></option>
- <option value="oldest" <?= $sort === 'oldest' ? 'selected' : '' ?>><?= htmlspecialchars(Language::get('search_sort_oldest', 'Publish date (oldest)')) ?></option>
- <option value="price_asc" <?= $sort === 'price_asc' ? 'selected' : '' ?>><?= htmlspecialchars(Language::get('search_sort_price_asc', 'Price: low to high')) ?></option>
- <option value="price_desc" <?= $sort === 'price_desc' ? 'selected' : '' ?>><?= htmlspecialchars(Language::get('search_sort_price_desc', 'Price: high to low')) ?></option>
- <option value="rating" <?= $sort === 'rating' ? 'selected' : '' ?>><?= htmlspecialchars(Language::get('search_sort_rating', 'Highest rated')) ?></option>
- </select>
- </label>
-
- <noscript><button type="submit" class="dash-btn-small"><?= htmlspecialchars(Language::get('search_filter_apply', 'Apply')) ?></button></noscript>
- </form>
-
- <?php if ($query === '' && !$hasFilters): ?>
- <p class="search-hint"><?= htmlspecialchars(Language::get('search_hint_empty', 'Type something above to search products.')) ?></p>
- <?php else: ?>
- <p class="search-hint">
- <?php if ($query !== ''): ?>
- <?php
- $resultsTemplate = $totalProducts === 1
- ? Language::get('search_results_count_one', '%d result for "%s"')
- : Language::get('search_results_count_other', '%d results for "%s"');
- echo htmlspecialchars(sprintf($resultsTemplate, $totalProducts, $query));
- ?>
- <?php else: ?>
- <?php
- $resultsTemplate = $totalProducts === 1
- ? Language::get('search_results_count_one_nolabel', '%d result')
- : Language::get('search_results_count_other_nolabel', '%d results');
- echo htmlspecialchars(sprintf($resultsTemplate, $totalProducts));
- ?>
- <?php endif; ?>
- </p>
-
- <div class="article-grid" id="article-list" data-pagination-source="search" data-search-query="<?= htmlspecialchars($query, ENT_QUOTES) ?>" data-search-category="<?= $categoryId ?>" data-search-tag="<?= $tagId ?>" data-search-sort="<?= htmlspecialchars($sort, ENT_QUOTES) ?>">
- <?php foreach ($products as $product): ?>
- <?= renderProductCard($product) ?>
- <?php endforeach; ?>
- <?php if (empty($products)): ?>
- <p><?= htmlspecialchars(Language::get('search_no_results', 'No products matched your search.')) ?></p>
- <?php endif; ?>
- </div>
-
- <div id="pagination-container">
- <?= $isStaticProductLoading ? '' : Pagination::render($settings['pagination_style'], 'search.php', $currentPage, $totalPages, $paginationExtraParams, 'article-list') ?>
- </div>
- <?php endif; ?>
-
- <?php require __DIR__ . '/includes/front-footer.php'; ?>
-