Merge pull request 'TIST-26: Added export recipe to pdf endpoint' (#14) from TIST-26 into develop
Reviewed-on: #14 Reviewed-by: greendavid004 <davidkatrinka1995@gmail.com>
This commit is contained in:
commit
dba1d5c8ae
46
apps/Recipes/Controllers/ExportPdfController.php
Normal file
46
apps/Recipes/Controllers/ExportPdfController.php
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Lycoreco\Apps\Recipes\Controllers;
|
||||||
|
|
||||||
|
use FPDF;
|
||||||
|
use Lycoreco\Apps\Recipes\Models\RecipeModel;
|
||||||
|
use Lycoreco\Apps\Recipes\Utils\RecipePDF;
|
||||||
|
use Lycoreco\Includes\BaseController;
|
||||||
|
use Lycoreco\Includes\Routing\HttpExceptions;
|
||||||
|
|
||||||
|
class ExportPdfController extends BaseController
|
||||||
|
{
|
||||||
|
protected $template_name = APPS_PATH . '/Recipes/Templates/export-pdf.php';
|
||||||
|
|
||||||
|
protected function get_model()
|
||||||
|
{
|
||||||
|
if (isset($this->__model))
|
||||||
|
return $this->__model;
|
||||||
|
|
||||||
|
$this->__model = RecipeModel::get(
|
||||||
|
array(
|
||||||
|
[
|
||||||
|
'name' => 'obj.id',
|
||||||
|
'type' => '=',
|
||||||
|
'value' => $this->url_context['url_1']
|
||||||
|
]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
if (empty($this->__model))
|
||||||
|
throw new HttpExceptions\NotFound404('Recipe not found');
|
||||||
|
|
||||||
|
return $this->__model;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get_context_data()
|
||||||
|
{
|
||||||
|
$context = parent::get_context_data();
|
||||||
|
$recipe = $this->get_model();
|
||||||
|
|
||||||
|
$fpdf = new RecipePDF($recipe);
|
||||||
|
$fpdf->PrintRecipe();
|
||||||
|
|
||||||
|
$context['fpdf'] = $fpdf;
|
||||||
|
return $context;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -27,7 +27,7 @@ class RecipeModel extends BaseModel
|
|||||||
'field' => [
|
'field' => [
|
||||||
'tb1.name AS category_name'
|
'tb1.name AS category_name'
|
||||||
],
|
],
|
||||||
'join_table' => 'ingredients tb1 ON tb1.id = obj.category_id'
|
'join_table' => 'categories tb1 ON tb1.id = obj.category_id'
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
'field' => [
|
'field' => [
|
||||||
|
|||||||
1
apps/Recipes/Templates/export-pdf.php
Normal file
1
apps/Recipes/Templates/export-pdf.php
Normal file
@ -0,0 +1 @@
|
|||||||
|
<?php $context['fpdf']->Output('I', 'recipe.pdf') ?>
|
||||||
111
apps/Recipes/Utils/RecipePDF.php
Normal file
111
apps/Recipes/Utils/RecipePDF.php
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Lycoreco\Apps\Recipes\Utils;
|
||||||
|
|
||||||
|
use FPDF;
|
||||||
|
use Lycoreco\Apps\Recipes\Models\IngredientInRecipeModel;
|
||||||
|
use Lycoreco\Apps\Recipes\Models\RecipeModel;
|
||||||
|
|
||||||
|
class RecipePDF extends FPDF
|
||||||
|
{
|
||||||
|
public RecipeModel $recipe;
|
||||||
|
|
||||||
|
public function __construct(RecipeModel $recipe)
|
||||||
|
{
|
||||||
|
parent::__construct('P', 'mm', 'A4');
|
||||||
|
$this->recipe = $recipe;
|
||||||
|
}
|
||||||
|
function Header()
|
||||||
|
{
|
||||||
|
$recipe = $this->recipe;
|
||||||
|
|
||||||
|
// Background
|
||||||
|
$this->SetTextColor(0, 139, 112);
|
||||||
|
|
||||||
|
$this->SetFont('Arial', 'B', 16);
|
||||||
|
$this->Image(BASE_PATH . '/assets/images/fridgeLogo.png', 10, 6, 20);
|
||||||
|
$this->Cell(80);
|
||||||
|
$this->Cell(30, 10, $recipe->field_title, 0, 0, 'C');
|
||||||
|
$this->Ln(8);
|
||||||
|
|
||||||
|
$this->SetFont('Arial', '', 12);
|
||||||
|
$this->SetTextColor(0, 0, 0);
|
||||||
|
$this->Cell(80);
|
||||||
|
$this->Cell(30, 10, $recipe->category_name, 0, 0, 'C');
|
||||||
|
$this->Ln(20);
|
||||||
|
}
|
||||||
|
function Footer()
|
||||||
|
{
|
||||||
|
// Position at 1.5 cm from bottom
|
||||||
|
$this->SetY(-15);
|
||||||
|
// Arial italic 8
|
||||||
|
$this->SetFont('Arial', 'I', 8);
|
||||||
|
// Text color in gray
|
||||||
|
$this->SetTextColor(128);
|
||||||
|
// Page number
|
||||||
|
$this->Cell(0, 3, 'Page ' . $this->PageNo(), 0, 0, 'C');
|
||||||
|
$this->Ln();
|
||||||
|
$this->Cell(0, 10, 'LycoReco', 0, 0, 'C');
|
||||||
|
}
|
||||||
|
|
||||||
|
function RecipeTable()
|
||||||
|
{
|
||||||
|
$header = array('Ingredient', 'Count');
|
||||||
|
|
||||||
|
$ingredients = IngredientInRecipeModel::filter(array(
|
||||||
|
[
|
||||||
|
'name' => 'obj.recipe_id',
|
||||||
|
'type' => '=',
|
||||||
|
'value' => $this->recipe->get_id()
|
||||||
|
]
|
||||||
|
));
|
||||||
|
|
||||||
|
// Colors, line width and bold font
|
||||||
|
$this->SetFillColor(0, 139, 122);
|
||||||
|
$this->SetTextColor(255);
|
||||||
|
$this->SetDrawColor(179, 179, 179);
|
||||||
|
$this->SetLineWidth(.3);
|
||||||
|
$this->SetFont('', 'B');
|
||||||
|
// Header
|
||||||
|
$w = array(80, 35);
|
||||||
|
$this->Cell(35);
|
||||||
|
for ($i = 0; $i < count($header); $i++)
|
||||||
|
$this->Cell($w[$i], 7, $header[$i], 1, 0, 'C', true);
|
||||||
|
$this->Ln();
|
||||||
|
|
||||||
|
// Body
|
||||||
|
$this->SetFillColor(224,235,255);
|
||||||
|
$this->SetTextColor(0);
|
||||||
|
$this->SetFont('');
|
||||||
|
foreach ($ingredients as $ing) {
|
||||||
|
$this->Cell(35);
|
||||||
|
$this->Cell($w[0], 6, $ing->ingredient_name, 'LR', 0, 'L', false);
|
||||||
|
$this->Cell($w[1], 6, $ing->get_count(), 'LR', 0, 'L', false);
|
||||||
|
$this->Ln();
|
||||||
|
}
|
||||||
|
$this->Cell(35);
|
||||||
|
$this->Cell(array_sum($w),0,'','T');
|
||||||
|
$this->Ln(10);
|
||||||
|
}
|
||||||
|
|
||||||
|
function RecipeContent()
|
||||||
|
{
|
||||||
|
$recipe = $this->recipe;
|
||||||
|
|
||||||
|
$this->SetFont('Times', '', 14);
|
||||||
|
$this->SetTextColor(0);
|
||||||
|
$this->MultiCell(0, 5, txt: $recipe->field_instruction);
|
||||||
|
$this->Ln();
|
||||||
|
$this->SetFont('', 'I');
|
||||||
|
$this->Cell(0, 5, '(Bon appetit!)');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function PrintRecipe()
|
||||||
|
{
|
||||||
|
$this->SetTitle($this->recipe->field_title);
|
||||||
|
$this->AddPage();
|
||||||
|
$this->RecipeTable();
|
||||||
|
$this->RecipeContent();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -5,5 +5,6 @@ use Lycoreco\Includes\Routing\Path;
|
|||||||
|
|
||||||
$recipes_urls = [
|
$recipes_urls = [
|
||||||
new Path('/recipe/[:int]', new Controllers\SingleRecipeController(), 'single'),
|
new Path('/recipe/[:int]', new Controllers\SingleRecipeController(), 'single'),
|
||||||
|
new Path('/recipe/[:int]/export-pdf', new Controllers\ExportPdfController(), 'export-pdf'),
|
||||||
new Path('/catalog', new Controllers\CatalogController(), 'catalog'),
|
new Path('/catalog', new Controllers\CatalogController(), 'catalog'),
|
||||||
];
|
];
|
||||||
|
|||||||
@ -9,6 +9,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
"phpmailer/phpmailer": "^6.10"
|
"phpmailer/phpmailer": "^6.10",
|
||||||
|
"setasign/fpdf": "^1.8"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
48
composer.lock
generated
48
composer.lock
generated
@ -4,7 +4,7 @@
|
|||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"content-hash": "a475eab76e37559e0cc7396ccf30c49e",
|
"content-hash": "a6db4a4cd2450cf3fbd46a573661b495",
|
||||||
"packages": [
|
"packages": [
|
||||||
{
|
{
|
||||||
"name": "phpmailer/phpmailer",
|
"name": "phpmailer/phpmailer",
|
||||||
@ -86,6 +86,52 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"time": "2025-04-24T15:19:31+00:00"
|
"time": "2025-04-24T15:19:31+00:00"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "setasign/fpdf",
|
||||||
|
"version": "1.8.6",
|
||||||
|
"source": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/Setasign/FPDF.git",
|
||||||
|
"reference": "0838e0ee4925716fcbbc50ad9e1799b5edfae0a0"
|
||||||
|
},
|
||||||
|
"dist": {
|
||||||
|
"type": "zip",
|
||||||
|
"url": "https://api.github.com/repos/Setasign/FPDF/zipball/0838e0ee4925716fcbbc50ad9e1799b5edfae0a0",
|
||||||
|
"reference": "0838e0ee4925716fcbbc50ad9e1799b5edfae0a0",
|
||||||
|
"shasum": ""
|
||||||
|
},
|
||||||
|
"require": {
|
||||||
|
"ext-gd": "*",
|
||||||
|
"ext-zlib": "*"
|
||||||
|
},
|
||||||
|
"type": "library",
|
||||||
|
"autoload": {
|
||||||
|
"classmap": [
|
||||||
|
"fpdf.php"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"notification-url": "https://packagist.org/downloads/",
|
||||||
|
"license": [
|
||||||
|
"MIT"
|
||||||
|
],
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Olivier Plathey",
|
||||||
|
"email": "oliver@fpdf.org",
|
||||||
|
"homepage": "http://fpdf.org/"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": "FPDF is a PHP class which allows to generate PDF files with pure PHP. F from FPDF stands for Free: you may use it for any kind of usage and modify it to suit your needs.",
|
||||||
|
"homepage": "http://www.fpdf.org",
|
||||||
|
"keywords": [
|
||||||
|
"fpdf",
|
||||||
|
"pdf"
|
||||||
|
],
|
||||||
|
"support": {
|
||||||
|
"source": "https://github.com/Setasign/FPDF/tree/1.8.6"
|
||||||
|
},
|
||||||
|
"time": "2023-06-26T14:44:25+00:00"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"packages-dev": [],
|
"packages-dev": [],
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user