Added twig template, templates and search functionality.

This commit is contained in:
Krzysztof Płaczek
2024-03-28 16:07:19 +01:00
parent 5d0a44946f
commit d2abfba355
6 changed files with 261 additions and 65 deletions

View File

@@ -3,78 +3,32 @@
include_once 'vendor/autoload.php';
use Illuminate\Database\Capsule\Manager as Capsule;
use Krzysiej\RyobiCrawler\Models\Price;
use Krzysiej\RyobiCrawler\Models\Product;
use Twig\{Environment, Loader\FilesystemLoader};
$capsule = new Capsule;
$capsule->addConnection([
'driver' => 'sqlite',
'database' => __DIR__ . '/database.sqlite',
'prefix' => '',
]);
$capsule->addConnection(['driver' => 'sqlite', 'database' => __DIR__ . '/database.sqlite']);
$capsule->setAsGlobal();
$capsule->bootEloquent();
echo '<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous"></head>';
$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']);
echo "<a href='/browser.php'>back</a><table class='table table-hover'>";
echo "<tr>
<td><img src='$product->image&width=150' class='img-fluid' alt='$product->name'/></td>
<td><a href='?product_id=$product->id'>$product->name</a></td>
<td>$product->subTitle</td>
<td><a href='https://pl.ryobitools.eu/{$product->url}'>link</a></td></tr>";
echo "</table>";
echo "<table class='table table-hover'>
<thead>
<tr>
<th>price</th>
<th>lowest product price in 30 days</th>
<th colspan='2'>standard price</th>
</tr>
</thead>";
/** @var Price $price */
foreach ($product->price as $price) {
echo "<tr>
<td>$price->price</td>
<td>$price->lowestProductPrice30Days</td>
<td>$price->productStandardPrice</td>
<td>$price->created_at</td>
</tr>";
}
echo "</table>";
}
if (isset($_GET['category'])) {
echo "<a href='/browser.php'>back</a><table class='table table-hover'>";
$template = 'productList.html.twig';
$products = Product::with('price')->selectRaw('products.*')->fromRaw('products, json_each(products.categories)')->whereRaw('json_each.value = ?', [$_GET['category']])->get();
echo "<table class='table table-hover'>";
foreach ($products as $product) {
echo "<tr>
<td><img src='$product->image&width=70' class='img-fluid' alt='$product->name' /></td>
<td><a href='?product_id=$product->id'>$product->name</a></td>
<td>$product->subTitle</td><td><ul class='nav'>";
foreach ($product->categories as $category) {
echo '<li class="nav-item"><a class="nav-link" href="?category=' . $category . '">' . $category . '</a></li>';
}
echo "</td><td><a href='https://pl.ryobitools.eu/{$product->url}'>link</a></td>
<td>{$product->price->last()->price}</td>
</tr>";
}
echo "</table>";
}
if (!isset($_GET['product_id']) && !isset($_GET['category'])) {
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();
echo "<table class='table table-hover'>";
foreach ($products as $product) {
echo "<tr>
<td><img src='$product->image&width=70' class='img-fluid' alt='$product->name' /></td>
<td><a href='?product_id=$product->id'>$product->name</a></td>
<td>$product->subTitle</td><td><ul class='nav'>";
foreach ($product->categories as $category) {
echo '<li class="nav-item"><a class="nav-link" href="?category=' . $category . '">' . $category . '</a></li>';
}
echo "</td><td><a href='https://pl.ryobitools.eu/{$product->url}'>link</a></td>
<td>{$product->price->last()->price}</td>
</tr>";
}
echo "</table>";
}
$twig->display($template, ['products' => $products, 'product' => $product, 'search' => $_GET['search']]);