From d2abfba35552f637b437c64aa2511ba18609bd6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20P=C5=82aczek?= Date: Thu, 28 Mar 2024 16:07:19 +0100 Subject: [PATCH] Added twig template, templates and search functionality. --- browser.php | 78 +++----------- composer.json | 3 +- composer.lock | 157 +++++++++++++++++++++++++++- src/templates/product.html.twig | 42 ++++++++ src/templates/productList.html.twig | 22 ++++ src/templates/template.html.twig | 24 +++++ 6 files changed, 261 insertions(+), 65 deletions(-) create mode 100644 src/templates/product.html.twig create mode 100644 src/templates/productList.html.twig create mode 100644 src/templates/template.html.twig diff --git a/browser.php b/browser.php index d2d3897..e2fba90 100644 --- a/browser.php +++ b/browser.php @@ -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 ''; +$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 "back"; - echo " - - - - "; - echo "
$product->name$product->name$product->subTitlelink
"; - echo " - - - - - - - "; - /** @var Price $price */ - foreach ($product->price as $price) { - echo " - - - - -"; - } - echo "
pricelowest product price in 30 daysstandard price
$price->price$price->lowestProductPrice30Days$price->productStandardPrice$price->created_at
"; } if (isset($_GET['category'])) { - echo "back"; + $template = 'productList.html.twig'; $products = Product::with('price')->selectRaw('products.*')->fromRaw('products, json_each(products.categories)')->whereRaw('json_each.value = ?', [$_GET['category']])->get(); - echo "
"; - foreach ($products as $product) { - echo " - - - - -"; - } - echo "
$product->name$product->name$product->subTitlelink{$product->price->last()->price}
"; } -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 ""; - foreach ($products as $product) { - echo " - - - - -"; - } - echo "
$product->name$product->name$product->subTitlelink{$product->price->last()->price}
"; } + +$twig->display($template, ['products' => $products, 'product' => $product, 'search' => $_GET['search']]); diff --git a/composer.json b/composer.json index 5245765..8d57d8e 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,8 @@ "guzzlehttp/guzzle": "^7.0", "symfony/var-dumper": "^7.0", "illuminate/database": "^11.0", - "ext-json": "*" + "ext-json": "*", + "twig/twig": "^3.0" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index 447239b..d241cb5 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "2de5a90e6e774457aaf4cab180a00f16", + "content-hash": "ce7d9a1457a9f208ddadf1630309fecc", "packages": [ { "name": "brick/math", @@ -1542,6 +1542,85 @@ ], "time": "2023-05-23T14:45:45+00:00" }, + { + "name": "symfony/polyfill-ctype", + "version": "v1.29.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-ctype.git", + "reference": "ef4d7e442ca910c4764bce785146269b30cb5fc4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/ef4d7e442ca910c4764bce785146269b30cb5fc4", + "reference": "ef4d7e442ca910c4764bce785146269b30cb5fc4", + "shasum": "" + }, + "require": { + "php": ">=7.1" + }, + "provide": { + "ext-ctype": "*" + }, + "suggest": { + "ext-ctype": "For best performance" + }, + "type": "library", + "extra": { + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ], + "psr-4": { + "Symfony\\Polyfill\\Ctype\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Gert de Pagter", + "email": "BackEndTea@gmail.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for ctype functions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "ctype", + "polyfill", + "portable" + ], + "support": { + "source": "https://github.com/symfony/polyfill-ctype/tree/v1.29.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2024-01-29T20:11:03+00:00" + }, { "name": "symfony/polyfill-mbstring", "version": "v1.29.0", @@ -2034,6 +2113,78 @@ ], "time": "2024-02-15T11:33:06+00:00" }, + { + "name": "twig/twig", + "version": "v3.8.0", + "source": { + "type": "git", + "url": "https://github.com/twigphp/Twig.git", + "reference": "9d15f0ac07f44dc4217883ec6ae02fd555c6f71d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/twigphp/Twig/zipball/9d15f0ac07f44dc4217883ec6ae02fd555c6f71d", + "reference": "9d15f0ac07f44dc4217883ec6ae02fd555c6f71d", + "shasum": "" + }, + "require": { + "php": ">=7.2.5", + "symfony/polyfill-ctype": "^1.8", + "symfony/polyfill-mbstring": "^1.3", + "symfony/polyfill-php80": "^1.22" + }, + "require-dev": { + "psr/container": "^1.0|^2.0", + "symfony/phpunit-bridge": "^5.4.9|^6.3|^7.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Twig\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com", + "homepage": "http://fabien.potencier.org", + "role": "Lead Developer" + }, + { + "name": "Twig Team", + "role": "Contributors" + }, + { + "name": "Armin Ronacher", + "email": "armin.ronacher@active-4.com", + "role": "Project Founder" + } + ], + "description": "Twig, the flexible, fast, and secure template language for PHP", + "homepage": "https://twig.symfony.com", + "keywords": [ + "templating" + ], + "support": { + "issues": "https://github.com/twigphp/Twig/issues", + "source": "https://github.com/twigphp/Twig/tree/v3.8.0" + }, + "funding": [ + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/twig/twig", + "type": "tidelift" + } + ], + "time": "2023-11-21T18:54:41+00:00" + }, { "name": "voku/portable-ascii", "version": "2.0.1", @@ -2115,7 +2266,9 @@ "stability-flags": [], "prefer-stable": false, "prefer-lowest": false, - "platform": [], + "platform": { + "ext-json": "*" + }, "platform-dev": [], "plugin-api-version": "2.3.0" } diff --git a/src/templates/product.html.twig b/src/templates/product.html.twig new file mode 100644 index 0000000..23c3ce1 --- /dev/null +++ b/src/templates/product.html.twig @@ -0,0 +1,42 @@ +{% extends "template.html.twig" %} + +{% block content %} + + + + + + + + + + + +
{{ product.name }}{{ product.name }}{{ product.subTitle }} + + link
+ + + + + + + + + + {% for price in product.price %} + + + + + + + {% endfor %} +
pricelowest product price in 30 daysstandard price
{{ price.price }}{{ price.lowestProductPrice30Days }}{{ price.productStandardPrice }}{{ price.created_at }}
+
+{% endblock %} + diff --git a/src/templates/productList.html.twig b/src/templates/productList.html.twig new file mode 100644 index 0000000..fea02b6 --- /dev/null +++ b/src/templates/productList.html.twig @@ -0,0 +1,22 @@ +{% extends "template.html.twig" %} + +{% block content %} + + {% for product in products %} + + + + + + + + + {% endfor %} +
{{ product.name }}{{ product.name }}{{ product.subTitle }} + + link{{ product.price.last.price }}
+{% endblock %} \ No newline at end of file diff --git a/src/templates/template.html.twig b/src/templates/template.html.twig new file mode 100644 index 0000000..484a50f --- /dev/null +++ b/src/templates/template.html.twig @@ -0,0 +1,24 @@ + + + + + + + Ryobi crawler + + + + +{% block content %}{% endblock %} + + +