-
Notifications
You must be signed in to change notification settings - Fork 0
/
recipe.php
148 lines (138 loc) · 4.97 KB
/
recipe.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
session_start();
require_once('includes/db.php');
require_once('includes/functions.php');
require_once('classes/Recipe.php');
// Check if recipe slug is provided
if (!isset($_GET['slug'])) {
header("Location: index.php");
exit();
}
// Instantiate Recipe object
$recipe = new Recipe($conn);
// Fetch recipe details by slug
$slug = $_GET['slug'];
$recipe_details = $recipe->getRecipeBySlug($slug);
// Check if recipe exists
if (!$recipe_details) {
// Recipe not found
$title = "Recipe Not Found";
$description = "The recipe you are looking for does not exist.";
$ingredients = "";
$instructions = "";
$category = "";
$meta_title = $title . " | Cook Book";
$meta_description = $description;
include 'inc/header.php';
?>
<div class="container mt-5">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header"><?php echo $title; ?></div>
<div class="card-body">
<p><?php echo $description; ?></p>
<a href="index.php" class="btn btn-primary">Back to Home</a>
</div>
</div>
</div>
</div>
</div>
<?php
include 'inc/footer.php';
exit();
}
// Display recipe details
$title = $recipe_details['title'];
$description = $recipe_details['description'];
$ingredients = $recipe_details['ingredients'];
$instructions = str_replace(array('\r\n', '\n\r', '\n', '\r'), '<br>', $recipe_details['instructions']);
$category = $recipe_details['category'];
$publish_date = $recipe_details['created_at'];
// Display author information
$user_id = $recipe_details['user_id'];
$user_query = "SELECT username, email FROM users WHERE id = ?";
$user_statement = $conn->prepare($user_query);
$user_statement->bind_param("i", $user_id); // "i" indicates integer type for user_id
$user_statement->execute();
$user_result = $user_statement->get_result();
$user_details = $user_result->fetch_assoc();
$author_name = $user_details['username'];
$author_email = $user_details['email'];
$user_statement->close();
// Original image URL from the database
$image_url = $recipe_details['image_url'];
// Transformation parameters
$transformation = 'ar_16:9,c_crop'; // Landscape ratio and automatic subject detection
// Add transformation to the image URL if it's not empty
$transformed_url = '';
if (!empty($image_url)) {
// Add transformation to the image URL
$transformed_url = preg_replace('/(upload\/)/', '$1' . $transformation . '/', $image_url);
}
?>
<?php
$meta_title = $title . " | Cook Book";
$meta_description = $description;
;?>
<?php include 'inc/header.php';?>
<section class="section bg-light">
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card shadow recipe-details">
<div class="card-body">
<h1><?php echo $title; ?></h1>
<ul class="meta">
<li>
<img src="https://www.gravatar.com/avatar/<?php echo md5(strtolower(trim($author_email))); ?>?s=40"
alt="<?php echo $author_name; ?>'s Avatar" class="rounded-circle"> <?php echo $author_name; ?>
</li>
<li>
<?php echo date('F j, Y', strtotime($publish_date)); ?>
</li>
<li>
<?php echo $category; ?>
</li>
</ul>
</div>
<!-- Display image -->
<?php if (!empty($recipe_details['image_url'])) : ?>
<img src="<?php echo $transformed_url; ?>" class="img-fluid w-100 mb-3"
alt="<?php echo $recipe_details['title']; ?>">
<?php endif; ?>
<div class="card-body">
<h5>Description:</h5>
<p><?php echo $description; ?></p>
<h5>Ingredients:</h5>
<ul>
<?php
$ingredient_list = explode("|", $ingredients);
foreach ($ingredient_list as $ingredient) {
echo "<li>$ingredient</li>";
}
?>
</ul>
<h5>Instructions:</h5>
<p><?php echo $instructions; ?></p>
<?php if (isset($_SESSION['user_id']) && isset($recipe_details['user_id']) && $_SESSION['user_id'] == $recipe_details['user_id']) { ?>
<div class="row mt-3">
<div class="col-md-6">
<a href="edit_recipe.php?slug=<?php echo $slug; ?>" class="btn btn-primary d-block w-100">Edit
Recipe</a>
</div>
<div class="col-md-6">
<form action="actions/delete_recipe.php?id=<?php echo $recipe_details['id']; ?>" method="POST"
onsubmit="return confirm('Are you sure you want to delete this recipe?');">
<button type="submit" class="btn btn-danger d-block w-100">Delete Recipe</button>
</form>
</div>
</div>
<?php } ?>
</div>
</div>
</div>
</div>
</div>
</section>
<?php include 'inc/footer.php';?>