Files
ryobi-crawler/browser.php
2024-03-28 16:07:19 +01:00

35 lines
1.3 KiB
PHP

<?php
include_once 'vendor/autoload.php';
use Illuminate\Database\Capsule\Manager as Capsule;
use Krzysiej\RyobiCrawler\Models\Product;
use Twig\{Environment, Loader\FilesystemLoader};
$capsule = new Capsule;
$capsule->addConnection(['driver' => 'sqlite', 'database' => __DIR__ . '/database.sqlite']);
$capsule->setAsGlobal();
$capsule->bootEloquent();
$loader = new FilesystemLoader(__DIR__ . '/src/templates');
$twig = new Environment($loader);
if (isset($_GET['product_id'])) {
$template = 'product.html.twig';
$product = Product::with('price')->find($_GET['product_id']);
}
if (isset($_GET['category'])) {
$template = 'productList.html.twig';
$products = Product::with('price')->selectRaw('products.*')->fromRaw('products, json_each(products.categories)')->whereRaw('json_each.value = ?', [$_GET['category']])->get();
}
if (isset($_GET['search'])) {
$template = 'productList.html.twig';
$products = Product::with('price')
->orWhere([['name', 'like', "%{$_GET['search']}%"]])
->orWhere([['subTitle', 'like', "%{$_GET['search']}%"]])->get();
}
if (empty($_GET)) {
$template = 'productList.html.twig';
$products = Product::with('price')->get();
}
$twig->display($template, ['products' => $products, 'product' => $product, 'search' => $_GET['search']]);