Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/service centre #35

Merged
merged 3 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions controllers/ProductController.php
Original file line number Diff line number Diff line change
@@ -1 +1,123 @@
<?php

namespace app\controllers;
use app\core\Application;
use app\core\Controller;
use app\core\Request;
use app\core\Response;
use app\models\Product;

class ProductController extends Controller
{
public function create(Request $request)
{
$product = new Product();
$ser_cen_id = Application::$app->session->get('serviceCenter');

if (!$ser_cen_id) {
Application::$app->session->setFlash('error', 'Please log in to create a product.');
Application::$app->response->redirect('/service-centre-login');
return;
}
$product->ser_cen_id = $ser_cen_id;

if ($request->isPost()) {
$product->loadData($request->getBody());

if (!empty($_FILES['media']['name'])) {
$uploadDir = 'assets/uploads/';
$product->media = $_FILES['media']['name'];
$targetFile = $uploadDir . basename($product->media);

if (!move_uploaded_file($_FILES['media']['tmp_name'], $targetFile)) {
Application::$app->session->setFlash('error', 'Failed to upload file.');
return $this->render('service-centre/create-product', [
'model' => $product,
'products' => [] // Ensure products is passed even if empty
]);
}
}

if ($product->validate() && $product->save()) {
Application::$app->session->setFlash('success', 'Product created successfully.');
Application::$app->response->redirect('/service-center-create-product');
return;
}
}

// Fetch products for the logged-in service center
// Output the dump in the HTML

// $products = $product->getProductByServiceCenter($ser_cen_id);

return $this->render('/service-centre/create-product', [

'model' => $product,
// 'products' => $products
// Pass the products to the view
]);
}

public function filterProductsById()
{
$ser_cen_id = Application::$app->session->get('serviceCenter');
$products = (new Product)->getProductByServiceCenter($ser_cen_id);
$this->setLayout('auth');
return $this->render('service-centre/create-product', [
'products' => $products
]);
}

public function index()
{
$products = (new Product)->getAllProducts(); // Fetch all products from the database
$this->setLayout('auth'); // Set layout if needed
return $this->render('/service-centre/market-place-home', [
'products' => $products // Pass products to the view
]);
}

public function update(Request $request)
{
$product = new Product();
$ser_cen_id = Application::$app->session->get('serviceCenter');

if (!$ser_cen_id) {
Application::$app->session->setFlash('error', 'Please log in to create a product.');
Application::$app->response->redirect('/service-centre-login');
}

if ($request->isPost()) {
$product->loadData($request->getBody());
$product->ser_cen_id = $ser_cen_id;

if (!empty($_FILES['media']['name'])) {
$uploadDir = 'assets/uploads/';
$fileName = uniqid() . '_' . basename($_FILES['media']['name']);
$targetFile = $uploadDir . $fileName;

if (!move_uploaded_file($_FILES['media']['tmp_name'], $targetFile)) {
Application::$app->session->setFlash('error', 'Failed to upload file.');
Application::$app->response->redirect('/service-center-create-product');
return;
}

$product->media = $fileName;
}
if ($product->editProduct()){
Application::$app->session->setFlash('success', 'Product updated successfully.');
Application::$app->response->redirect('/service-center-create-product');
}
else {
Application::$app->session->setFlash('error', 'Failed to update product.');
}
Application::$app->response->redirect('/service-center-create-product');
}
}


}



?>
14 changes: 13 additions & 1 deletion controllers/ServiceCentreController.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,19 @@ public function serviceCenterCommunity()
public function marketPlaceHome()
{
$this->setLayout('auth');
return $this->render('service-centre/market-place/market-place-home');
return $this->render('service-centre/market-place-home');
}

public function serviceCenterCreateProduct()
{
$this->setLayout('auth');
return $this->render('service-centre/create-product');
}

public function update()
{
$this->setLayout('auth');
return $this->render('service-centre/update-product');
}

public function serviceCenterMessages()
Expand Down
72 changes: 60 additions & 12 deletions models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,25 @@ public function primaryKey(): string
return 'product_id';
}

// public function loadData($data)
// {
// parent::loadData($data); // TODO: Change the autogenerated stub
// }

public function save()
{
// if (!empty($_FILES['media']['name'])) {
// $uploadDir = 'assets/uploads/';
// $fileName = uniqid() . '_' . basename($_FILES['media']['name']); // Generate unique name
// $targetFile = $uploadDir . $fileName;
//
// // Validate file type and size (example: images only, max 5MB)
// $allowedTypes = ['image/jpeg', 'image/png', 'image/gif'];
// if (in_array($_FILES['media']['type'], $allowedTypes) && $_FILES['media']['size'] <= 5 * 1024 * 1024) {
// if (!move_uploaded_file($_FILES['media']['tmp_name'], $targetFile)) {
// throw new \Exception('File upload failed.');
// }
// $this->media = $fileName;
// } else {
// throw new \Exception('Invalid file type or size.');
// }
// }
$this->media = $_FILES['media']['name'];
move_uploaded_file($_FILES['media']['tmp_name'], 'assets/uploads/' . $this->media);
return parent::save();
Expand All @@ -49,21 +61,43 @@ public static function getAllProducts(): array
$stmt->execute();
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
}

public function getProductByServiceCenter(int $ser_cen_id): array
{
try {
$sql = 'SELECT * FROM product WHERE ser_cen_id = :ser_cen_id ORDER BY created_at DESC';
$stmt = self::prepare($sql);
$stmt->bindValue(':ser_cen_id', $ser_cen_id);
$stmt->execute();

$products = $stmt->fetchAll(\PDO::FETCH_ASSOC);

// Debugging: Check the fetched data
if (!$products) {
throw new \Exception("No products found for service center ID: $ser_cen_id");
}

return $products;
} catch (\Exception $e) {
// Log the error (optional) and return an empty array
error_log($e->getMessage());
return [];
}
}

public function editProduct(): bool
{
$tableName = self::tableName();
$stmt = self::prepare("
UPDATE $tableName
SET description = :description, price = :price, media = :media, updated_at = NOW()
WHERE product_id = :product_id AND ser_cen_id = :ser_cen_id
");
$sql = "UPDATE product SET description = :description, price = :price, media = :media, updated_at = NOW() WHERE product_id = :product_id AND ser_cen_id = :ser_cen_id";
$stmt = self::prepare($sql);
$stmt->bindValue(':description', $this->description);
$stmt->bindValue(':price', $this->price);
$stmt->bindValue(':media', $this->media);
$stmt->bindValue(':product_id', $this->product_id);
$stmt->bindValue(':ser_cen_id', $this->ser_cen_id);
$stmt->bindValue("product_id", $this->product_id);
$stmt->bindValue("ser_cen_id", $this->ser_cen_id);

return $stmt->execute();
}

public function deleteProduct(int $product_id, int $ser_cen_id): bool
{
$tableName = self::tableName();
Expand All @@ -84,6 +118,20 @@ public function productRules(): array
'price' => [self::RULE_REQUIRED]
];
}

public function rules(): array
{
return [

];
}

public function updateRules(): array
{
return [

];
}
}

?>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/assets/uploads/Screenshot (11).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/assets/uploads/download (1).jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/assets/uploads/download (2).jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/assets/uploads/download (3).jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/assets/uploads/download (4).jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/assets/uploads/download (5).jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/assets/uploads/download (6).jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/assets/uploads/download (7).jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/assets/uploads/download.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
76 changes: 76 additions & 0 deletions public/css/service-center/add-products.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* General Form Styling */
form {
max-width: 600px;
margin: 2rem auto;
padding: 1.5rem;
background-color: #f9f9f9;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
font-family: Arial, sans-serif;
}

/* Label Styling */
form label {
display: block;
margin-bottom: 0.5rem;
font-weight: bold;
color: #333;
}

/* Input and Textarea Styling */
form input[type="number"],
form input[type="file"],
form textarea {
width: 100%;
padding: 0.75rem;
margin-bottom: 1rem;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 1rem;
font-family: inherit;
box-sizing: border-box;
}

/* Input and Textarea Focus Effects */
form input:focus,
form textarea:focus {
outline: none;
border-color: #007bff;
box-shadow: 0 0 5px rgba(0, 123, 255, 0.5);
}

/* Button Styling */
form button {
display: inline-block;
padding: 0.75rem 1.5rem;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
font-size: 1rem;
font-weight: bold;
cursor: pointer;
text-align: center;
transition: background-color 0.3s ease;
}

/* Button Hover Effect */
form button:hover {
background-color: #0056b3;
}

/* File Input Styling */
form input[type="file"] {
padding: 0.5rem;
}

/* Responsive Design */
@media (max-width: 768px) {
form {
padding: 1rem;
}

form button {
width: 100%;
}
}
Loading