206 lines
6.5 KiB
PHP
206 lines
6.5 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Backend;
|
|
|
|
use App\Controllers\BaseController;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use App\Models\PwpModel;
|
|
use CodeIgniter\RESTful\ResourceController;
|
|
|
|
|
|
class PwpController extends ResourceController
|
|
{
|
|
protected $pwpModel;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->pwpModel = new PwpModel();
|
|
$this->db = db_connect();
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$pwps = $this->pwpModel->findAll();
|
|
foreach ($pwps as &$pwp) {
|
|
$selectedIds = array_filter(array_map('trim', explode(',', $pwp['selected_item'])));
|
|
$pwp['selected_item_details'] = [];
|
|
|
|
if (!empty($selectedIds)) {
|
|
$pwp['selected_item_details'] = $this->db->table('menu_items')
|
|
->select('id, title, price, status')
|
|
->whereIn('id', $selectedIds)
|
|
->where('deleted_at', null)
|
|
->orderBy("created_at", "ASC")
|
|
->get()
|
|
->getResultArray();
|
|
}
|
|
|
|
$pwpIds = array_filter(array_map('trim', explode(',', $pwp['pwp_item_id'])));
|
|
$pwp['pwp_item_details'] = [];
|
|
|
|
if (!empty($pwpIds)) {
|
|
$pwp['pwp_item_details'] = $this->db->table('menu_items')
|
|
->select('id, title, price, status')
|
|
->whereIn('id', $pwpIds)
|
|
->where('deleted_at', null)
|
|
->orderBy("created_at", "DESC")
|
|
->get()
|
|
->getResultArray();
|
|
}
|
|
}
|
|
|
|
return $this->respond([
|
|
'status' => 200,
|
|
'data' => $pwps
|
|
]);
|
|
}
|
|
|
|
|
|
public function create()
|
|
{
|
|
$json = $this->request->getJSON(true);
|
|
if (empty($json['mode']) || empty($json['order_index']) || empty($json['amount']) || empty($json['amount_type'])) {
|
|
return $this->failValidationErrors('mode, order_index, amount and amount_type are required');
|
|
}
|
|
$pwpItems = is_array($json['pwp_item_id']) ? implode(',', $json['pwp_item_id']) : $json['pwp_item_id'];
|
|
$selectedIds = is_array($json['selected_item']) ? $json['selected_item'] : explode(',', $json['selected_item']);
|
|
$selectedItems = implode(',', $selectedIds);
|
|
|
|
$data = [
|
|
'mode' => $json['mode'],
|
|
'order_index' => $json['order_index'],
|
|
'pwp_item_id' => $pwpItems,
|
|
'amount' => $json['amount'],
|
|
'amount_type' => $json['amount_type'],
|
|
'selected_item' => $selectedItems,
|
|
];
|
|
|
|
if (!$this->pwpModel->insert($data)) {
|
|
return $this->fail($this->pwpModel->errors());
|
|
}
|
|
|
|
$pwpItemDetails = [];
|
|
if (!empty($pwpItems)) {
|
|
$pwpItemArray = explode(',', $pwpItems);
|
|
$pwpItemDetails = $this->db->table('menu_items')
|
|
->select('id, title, price')
|
|
->whereIn('id', $pwpItemArray)
|
|
->where('status', 'active')
|
|
->get()
|
|
->getResultArray();
|
|
}
|
|
|
|
$selectedItemDetails = [];
|
|
if (!empty($selectedIds)) {
|
|
$selectedItemDetails = $this->db->table('menu_items')
|
|
->select('id, title, price')
|
|
->whereIn('id', $selectedIds)
|
|
->where('status', 'active')
|
|
->get()
|
|
->getResultArray();
|
|
}
|
|
|
|
return $this->respondCreated([
|
|
'message' => 'PWP promo created successfully',
|
|
'data' => array_merge($data, [
|
|
'pwp_item_id' => $pwpItems,
|
|
'pwp_item_details' => $pwpItemDetails,
|
|
'selected_item' => $selectedIds,
|
|
'selected_item_details' => $selectedItemDetails
|
|
])
|
|
]);
|
|
}
|
|
|
|
public function show($id = null)
|
|
{
|
|
$pwp = $this->pwpModel->find($id);
|
|
|
|
if (!$pwp) {
|
|
return $this->failNotFound('PWP not found');
|
|
}
|
|
|
|
$selectedIds = explode(',', $pwp['selected_item']);
|
|
$pwp['selected_item_details'] = [];
|
|
|
|
if (!empty($selectedIds[0])) {
|
|
$pwp['selected_item_details'] = $this->db->table('menu_items')
|
|
->select('id, title, price, status')
|
|
->whereIn('id', $selectedIds)
|
|
->where('status', 'active')
|
|
->get()
|
|
->getResultArray();
|
|
}
|
|
|
|
return $this->respond($pwp);
|
|
}
|
|
|
|
public function update($id = null)
|
|
{
|
|
$json = $this->request->getJSON(true);
|
|
|
|
if (empty($json['mode']) || empty($json['order_index']) ) {
|
|
return $this->failValidationErrors('mode, order_index are required');
|
|
}
|
|
|
|
$pwpItems = is_array($json['pwp_item_id']) ? implode(',', $json['pwp_item_id']) : $json['pwp_item_id'];
|
|
$selectedIds = is_array($json['selected_item']) ? $json['selected_item'] : explode(',', $json['selected_item']);
|
|
$selectedItems = implode(',', $selectedIds);
|
|
|
|
$data = [
|
|
'mode' => $json['mode'],
|
|
'order_index' => $json['order_index'],
|
|
'pwp_item_id' => $pwpItems,
|
|
'amount' => $json['amount'] ?? 0.00,
|
|
'amount_type' => $json['amount_type'] ?? 'amount',
|
|
'selected_item' => $selectedItems,
|
|
];
|
|
|
|
if (!$this->pwpModel->update($id, $data)) {
|
|
return $this->fail($this->pwpModel->errors());
|
|
}
|
|
|
|
$selectedItemDetails = [];
|
|
if (!empty($selectedIds)) {
|
|
$selectedItemDetails = $this->db->table('menu_items')
|
|
->select('id, title, price')
|
|
->whereIn('id', $selectedIds)
|
|
->where('status', 'active')
|
|
->get()
|
|
->getResultArray();
|
|
}
|
|
|
|
return $this->respond([
|
|
'status' => 200,
|
|
'message' => 'PWP promo updated successfully',
|
|
'data' => array_merge($data, [
|
|
'selected_item' => $selectedIds,
|
|
'selected_item_details' => $selectedItemDetails
|
|
])
|
|
]);
|
|
}
|
|
|
|
public function delete($id = null)
|
|
{
|
|
if (!$this->pwpModel->find($id)) {
|
|
return $this->respond([
|
|
'status' => 404,
|
|
'message' => 'PWP not found'
|
|
], 404);
|
|
}
|
|
|
|
if (!$this->pwpModel->delete($id)) {
|
|
return $this->respond([
|
|
'status' => 500,
|
|
'message' => 'Failed to delete PWP'
|
|
], 500);
|
|
}
|
|
|
|
return $this->respond([
|
|
'status' => 200,
|
|
'message' => 'PWP promo deleted successfully'
|
|
], 200);
|
|
}
|
|
|
|
|
|
}
|