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 #39

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
3 changes: 0 additions & 3 deletions .env
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
DB_DSN = "mysql:host=localhost;dbname=fixmedb"
DB_USER = root
DB_PASSWORD =
91 changes: 55 additions & 36 deletions controllers/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,34 @@ class ProductController extends Controller
{
public function create(Request $request)
{
$product = new Product();
$productModel = 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;
$productModel->ser_cen_id = $ser_cen_id;

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

if (!empty($_FILES['media']['name'])) {
$uploadDir = 'assets/uploads/';
$product->media = $_FILES['media']['name'];
$targetFile = $uploadDir . basename($product->media);
$productModel->media = $_FILES['media']['name'];
$targetFile = $uploadDir . basename($productModel->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,
'model' => $productModel,
'products' => [] // Ensure products is passed even if empty
]);
}
}

if ($product->validate() && $product->save()) {
if ($productModel->validate() && $productModel->save()) {
Application::$app->session->setFlash('success', 'Product created successfully.');
Application::$app->response->redirect('/service-center-create-product');
return;
Expand All @@ -48,71 +48,90 @@ public function create(Request $request)
// Fetch products for the logged-in service center
// Output the dump in the HTML

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

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

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

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


public function index()
{
$products = (new Product)->getAllProducts(); // Fetch all products from the database
$productModels = (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
'products' => $productModels // Pass products to the view
]);
}

public function update(Request $request)
{
$product = new Product();
$productModel = 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->session->setFlash('error', 'Please log in to update a product.');
Application::$app->response->redirect('/service-centre-login');
}
if ($request->isGet()){
$product_id = $request->getBody()['product_id'] ?? null;

if ($product_id) {
$product = $productModel->getProductByIdAndServiceCenter($ser_cen_id, $product_id);
if ($product) {
$this->setLayout('auth');
return $this->render('service-centre/update-product', [
'product' => $product
]);
}
}
else {
Application::$app->session->setFlash('error', 'invalid product id.');
Application::$app->response->redirect('/service-centre-login');
}

}

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

$productModel->loadData($request->getBody());
$productModel->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;
$productModel->media = $_FILES['media']['name'];
move_uploaded_file($_FILES['media']['tmp_name'], 'assets/uploads/' . $productModel->media);
}
if ($product->editProduct()){
if ($productModel->editProduct()){
Application::$app->session->setFlash('success', 'Product updated successfully.');
Application::$app->response->redirect('/service-center-create-product');
return;
}
else {
Application::$app->session->setFlash('error', 'Failed to update product.');
}
Application::$app->response->redirect('/service-center-create-product');
}

}

public function delete(Request $request){
$product_id = $request->getBody()['product_id'];
$ser_cen_id = Application::$app->session->get('serviceCenter');

if ((new Product())->deleteProduct($product_id, $ser_cen_id)){
Application::$app->session->setFlash('success', 'Product deleted successfully.');
}
else {
Application::$app->session->setFlash('error', 'Failed to delete product.');
}
Application::$app->response->redirect('/service-center-create-product');
}


Expand Down
59 changes: 45 additions & 14 deletions models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
class Product extends DbModel
{
public int $ser_cen_id;
public int $product_id;
public string $description = '';
public float $price;
public string $media;
public string $media = '';
public ?string $created_at = null;
public ?string $updated_at = null;

Expand Down Expand Up @@ -85,29 +86,59 @@ public function getProductByServiceCenter(int $ser_cen_id): array
}
}

public function getProductByIdAndServiceCenter(int $ser_cen_id, int $product_id): ?array
{
$sql = 'SELECT * FROM product WHERE product_id = :product_id AND ser_cen_id = :ser_cen_id';
$stmt = self::prepare($sql);
$stmt->bindValue(':product_id', $product_id);
$stmt->bindValue(':ser_cen_id', $ser_cen_id);
$stmt->execute();

return $stmt->fetch(\PDO::FETCH_ASSOC) ?: null;
}

public function editProduct(): bool
{
$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";
$sql = "
UPDATE product
SET description = :description, price = :price, media = CASE WHEN :media = '' THEN media ELSE :media END, 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);

return $stmt->execute();
$stmt->bindValue(':product_id', $this->product_id);
$stmt->bindValue(':ser_cen_id', $this->ser_cen_id);

// Debugging: Check SQL and parameters
// var_dump([
// 'SQL' => $sql,
// 'description' => $this->description,
// 'price' => $this->price,
// 'media' => $this->media,
// 'product_id' => $this->product_id,
// 'ser_cen_id' => $this->ser_cen_id
// ]);

$result = $stmt->execute();

// Debugging: Check execution result
// var_dump($result);
// die();

return $result;
}


public function deleteProduct(int $product_id, int $ser_cen_id): bool
{
$tableName = self::tableName();
$statement = self::prepare("
DELETE FROM $tableName
WHERE product_id = :product_id AND ser_cen_id = :ser_cen_id
");
$statement->bindValue(':product_id', $product_id);
$statement->bindValue(':seller_id', $ser_cen_id);
return $statement->execute();
$sql = 'DELETE FROM product WHERE product_id = :product_id AND ser_cen_id = :ser_cen_id';
$stmt = self::prepare($sql);
$stmt->bindValue(':product_id', $product_id);
$stmt->bindValue(':ser_cen_id', $ser_cen_id);
return $stmt->execute();
}
public function productRules(): array
{
Expand Down
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/67415f67d582e_download.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/Gby5MnmXsAUvyrd.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,11 @@
$app->router->post('/service-center-create-product', [ProductController::class, 'create']);
$app->router->get('/market-place-home', [ProductController::class, 'index']);
$app->router->get('/service-center-create-product', [ProductController::class, 'filterProductsById']);
$app->router->get('/service-center-update-product', [ProductController::class,'update']);
$app->router->get('/service-center-update-product', [ServiceCentreController::class, 'update']);
$app->router->post('/service-center-update-product', [ProductController::class, 'update']);
$app->router->post('/service-center-delete-product', [ProductController::class, 'delete']);



/** Admin Routes */
Expand Down
2 changes: 1 addition & 1 deletion views/service-centre/components/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

<h6 class="user-name">
<?php
$username = strtoupper(Application::$app->service_center->{'name'});
$username = strtoupper(Application::$app->serviceCenter->{'name'});
echo $username;
?>
</h6>
Expand Down
48 changes: 32 additions & 16 deletions views/service-centre/update-product.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,42 @@
include_once 'components/header.php';
?>

<form action="/service-center-update-product" method="post" enctype="multipart/form-data">
<input type="hidden" name="product_id" value="<?php echo htmlspecialchars($product['product_id']); ?>">

<div>
<label>Description:</label>
<input type="text" name="description" value="<?php echo htmlspecialchars($product['description']); ?>" required>
<?php if (Application::$app->session->getFlash('success')): ?>
<div class="alert alert-success">
<?php echo Application::$app->session->getFlash('success') ?>
</div>
<?php endif; ?>

<div>
<label>Price:</label>
<input type="number" step="0.01" name="price" value="<?php echo htmlspecialchars($product['price']); ?>" required>
</div>
<form action="/service-center-update-product" name="" method="post" enctype="multipart/form-data">

<div>
<label>Media:</label>
<input type="file" name="media">
<p>Current File: <?php echo htmlspecialchars($product['media']); ?></p>
</div>
<?php if (!empty($product)): ?>
<!-- --><?php //foreach ($products as $product): ?>

<input type="hidden" name="product_id" value="<?php echo htmlspecialchars($product['product_id']); ?>">

<div>
<label>Description:</label>
<input type="text" name="description" value="<?php echo htmlspecialchars($product['description']); ?>">
</div>

<div>
<label>Price:</label>
<input type="number" step="0.01" name="price" value="<?php echo htmlspecialchars($product['price']); ?>">

</div>
<div>
<label>Media:</label>
<input type="file" name="media">
<p>Current File: <?php echo htmlspecialchars($product['media']); ?></p>
</div>
<button type="submit">Update Product</button>

<!-- --><?php //endforeach; ?>

<?php else: ?>
<p>No products have been added yet.</p>
<?php endif; ?>

<button type="submit">Update Product</button>
</form>


Expand Down