1190 lines
50 KiB
PHP
1190 lines
50 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Backend;
|
|
|
|
use App\Models\MenuCategories;
|
|
use App\Models\MenuImages;
|
|
use App\Models\MenuItemCategories;
|
|
use App\Models\MenuItemOptionsGroups;
|
|
use App\Models\MenuItems;
|
|
use App\Models\MenuItemTags;
|
|
use App\Models\MenuItemVariations;
|
|
use App\Models\RegularItemAvailability;
|
|
use App\Models\SeasonalItemAvailability;
|
|
use App\Models\OptionsGroups;
|
|
use App\Models\Options;
|
|
use App\Models\VariationOptionsGroups;
|
|
use App\Models\VariationTags;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use CodeIgniter\RESTful\ResourceController;
|
|
use CodeIgniter\Database\Config;
|
|
use App\Models\OutletMenus;
|
|
|
|
|
|
helper('image');
|
|
|
|
|
|
class MenuController extends ResourceController
|
|
{
|
|
|
|
private $db;
|
|
private $menu_categories;
|
|
private $menu_items;
|
|
private $menu_items_categories;
|
|
private $menu_item_tags;
|
|
private $menu_item_variations;
|
|
private $menu_item_options_groups;
|
|
private $option_groups;
|
|
private $options;
|
|
private $variation_options_groups;
|
|
private $variation_tags;
|
|
private $regular_item_availability;
|
|
private $seasonal_item_availability;
|
|
private $menu_images;
|
|
private $outlet_menus;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->menu_categories = new MenuCategories();
|
|
$this->menu_items = new MenuItems();
|
|
$this->menu_items_categories = new MenuItemCategories();
|
|
$this->menu_item_tags = new MenuItemTags();
|
|
$this->menu_item_variations = new MenuItemVariations();
|
|
$this->menu_item_options_groups = new MenuItemOptionsGroups();
|
|
$this->variation_options_groups = new VariationOptionsGroups();
|
|
$this->variation_tags = new VariationTags();
|
|
$this->regular_item_availability = new RegularItemAvailability();
|
|
$this->seasonal_item_availability = new SeasonalItemAvailability();
|
|
$this->menu_images = new MenuImages();
|
|
$this->outlet_menus = new OutletMenus();
|
|
$this->option_groups = new OptionsGroups();
|
|
$this->options = new Options();
|
|
$this->db = Config::connect();
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$menuItems = $this->menu_items->where('status!=', 'trash')->where('deleted_at', null)->findAll();
|
|
|
|
if (empty($menuItems)) {
|
|
return $this->fail("No menu item found.", 400);
|
|
}
|
|
|
|
$menuItemData = [];
|
|
|
|
foreach ($menuItems as $menuItem) {
|
|
$menu_tag = $this->menu_item_tags->where('menu_item_id', $menuItem['id'])->findAll();
|
|
$menu_option_group = $this->menu_item_options_groups->where('menu_item_id', $menuItem['id'])->findAll();
|
|
$menuItemData[] = [
|
|
'id' => $menuItem['id'],
|
|
'title' => $menuItem['title'],
|
|
'category' => getMenuCategory($menuItem['id']),
|
|
'short_description' => $menuItem['short_description'],
|
|
'long_description' => $menuItem['long_description'],
|
|
'price' => $menuItem['price'],
|
|
'packaging_price' => $menuItem['packaging_price'],
|
|
'pwp_price' => $menuItem['pwp_price'],
|
|
'order_index' => $menuItem['order_index'],
|
|
'availability_type' => $menuItem['availability_type'],
|
|
'availability' => getAvailability($menuItem['id'], $menuItem['availability_type']),
|
|
'status' => $menuItem['status'],
|
|
'image' => getMenuImage($menuItem['id']),
|
|
'created_at' => $menuItem['created_at'],
|
|
'menu_tag' => getMenuTag($menuItem['id']),
|
|
'menu_option_group' => getMenuOptionGroup($menuItem['id']),
|
|
'variation_group' => getVariationRelation($menuItem['id'])
|
|
];
|
|
}
|
|
return $this->respond(["status" => 200, "message" => "Successfully retrive data!", "data" => $menuItemData]);
|
|
}
|
|
|
|
public function show($id = null)
|
|
{
|
|
$menuItem = $this->menu_items->find($id);
|
|
|
|
if (empty($menuItem)) {
|
|
return $this->fail("No menu item found.", 400);
|
|
}
|
|
|
|
$menuItemData = [];
|
|
|
|
if ($menuItem) {
|
|
$menu_tag = $this->menu_item_tags->where('menu_item_id', $menuItem['id'])->findAll();
|
|
$menu_option_group = $this->menu_item_options_groups->where('menu_item_id', $menuItem['id'])->findAll();
|
|
$menuItemData[] = [
|
|
'id' => $menuItem['id'],
|
|
'title' => $menuItem['title'],
|
|
'category' => getMenuCategory($menuItem['id']),
|
|
'short_description' => $menuItem['short_description'],
|
|
'long_description' => $menuItem['long_description'],
|
|
'price' => $menuItem['price'],
|
|
'packaging_price' => $menuItem['packaging_price'],
|
|
'pwp_price' => $menuItem['pwp_price'],
|
|
'order_index' => $menuItem['order_index'],
|
|
'availability_type' => $menuItem['availability_type'],
|
|
'availability' => getAvailability($menuItem['id'], $menuItem['availability_type']),
|
|
'membership_tier' => $menuItem['membership_tier'],
|
|
'status' => $menuItem['status'],
|
|
'image' => getMenuImage($menuItem['id']),
|
|
'created_at' => $menuItem['created_at'],
|
|
'menu_tag' => getMenuTag($menuItem['id']),
|
|
'menu_option_group' => getMenuOptionGroup($menuItem['id']),
|
|
'variation' => getVariationRelation($menuItem['id'])
|
|
];
|
|
}
|
|
return $this->respond(["status" => 200, "message" => "Successfully retrive data!", "data" => $menuItemData]);
|
|
}
|
|
|
|
|
|
public function new()
|
|
{
|
|
$validation = $this->validate([
|
|
'title' => 'required',
|
|
'price' => 'required',
|
|
'membership_tier' => 'permit_empty'
|
|
]);
|
|
|
|
if (!$validation) {
|
|
return $this->failValidationErrors($this->validator->getErrors());
|
|
}
|
|
|
|
$title = $this->request->getVar('title');
|
|
$price = $this->request->getVar('price');
|
|
$membership_tier = $this->request->getVar("membership_tier");
|
|
|
|
$item = [
|
|
'title' => $title,
|
|
'price' => $price,
|
|
'membership_tier' => $membership_tier,
|
|
];
|
|
|
|
$id = $this->menu_items->insert($item);
|
|
|
|
if ($id) {
|
|
return $this->respond(["status" => 200, "message" => "Menu Item Successfully Created!"]);
|
|
}
|
|
|
|
return $this->respond(['status' => 400, 'message' => 'Fail to create menu item!']);
|
|
}
|
|
|
|
|
|
public function create()
|
|
{
|
|
|
|
// echo(123123);exit;
|
|
|
|
$validation = $this->validate([
|
|
'title' => 'required',
|
|
'price' => 'required',
|
|
'status' => 'required',
|
|
'availability' => 'required',
|
|
'membership_tier' => 'permit_empty',
|
|
'pwp' => 'permit_empty',
|
|
]);
|
|
|
|
if (!$validation) {
|
|
return $this->failValidationErrors($this->validator->getErrors());
|
|
}
|
|
|
|
|
|
$title = $this->request->getVar('title');
|
|
$short_description = $this->request->getVar('short_description');
|
|
$long_description = $this->request->getVar('long_description');
|
|
$price = $this->request->getVar('price');
|
|
$packaging_price = $this->request->getVar('packaging_price');
|
|
$pwp_price = $this->request->getVar('pwp_price');
|
|
$status = $this->request->getVar('status');
|
|
$order_index = $this->request->getVar('order_index');
|
|
$category = $this->request->getVar("category");
|
|
$availability = $this->request->getVar("availability");
|
|
$membership_tier = $this->request->getVar("membership_tier");
|
|
$images = $this->request->getFiles();
|
|
$variation = $this->request->getVar("variation");
|
|
$menu_tag = $this->request->getVar("menu_tag");
|
|
$menu_option_group = $this->request->getVar("menu_option_group");
|
|
$menu = $this->request->getVar("menu");
|
|
$pwp_price = $this->request->getVar('pwp_price');
|
|
|
|
|
|
if (!empty($images['image'])) {
|
|
$images = $images['image'];
|
|
}
|
|
|
|
$menuItemData = [
|
|
'title' => $title,
|
|
'short_description' => $short_description,
|
|
'long_description' => $long_description,
|
|
'price' => $price,
|
|
'packaging_price' => $packaging_price,
|
|
'pwp_price' => $pwp_price,
|
|
'status' => $status,
|
|
'order_index' => $order_index,
|
|
'availability_type' => $availability['type'],
|
|
'membership_tier' => $membership_tier,
|
|
'pwp_price' => $pwp_price,
|
|
];
|
|
// print_r($menuItemData);exit;
|
|
|
|
$this->db->transStart();
|
|
|
|
try {
|
|
|
|
$menu_item_id = $this->menu_items->insert($menuItemData);
|
|
if (!$menu_item_id) {
|
|
throw new \Exception('Failed to create menu item!');
|
|
}
|
|
|
|
if (!empty($category)) {
|
|
foreach ($category as $value) {
|
|
$item_category = [
|
|
'menu_item_id' => $menu_item_id,
|
|
'category_id' => $value
|
|
];
|
|
|
|
if (!$this->menu_items_categories->insert($item_category)) {
|
|
throw new \Exception('Failed to insert category!');
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!empty($menu_tag)) {
|
|
foreach ($menu_tag as $value) {
|
|
$item_tag = [
|
|
'menu_item_id' => $menu_item_id,
|
|
'tag_id' => $value
|
|
];
|
|
|
|
if (!$this->menu_item_tags->insert($item_tag)) {
|
|
throw new \Exception('Failed to insert menu tag!');
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!empty($menu_option_group)) {
|
|
foreach ($menu_option_group as $i => $value) {
|
|
$item_option_group_data = [
|
|
'menu_item_id' => $menu_item_id,
|
|
'option_group_id' => $value,
|
|
'order_index' => $i // set order_index as the array index
|
|
];
|
|
|
|
if (!$this->menu_item_options_groups->insert($item_option_group_data)) {
|
|
throw new \Exception('Failed to insert menu options group!');
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!empty($availability)) {
|
|
if ($availability['type'] == 'seasonal') {
|
|
$item_seasonal = [
|
|
'start_date' => $availability['seasonal']['start_date'],
|
|
'end_date' => $availability['seasonal']['end_date'],
|
|
'start_time' => $availability['seasonal']['start_time'],
|
|
'end_time' => $availability['seasonal']['end_time'],
|
|
'menu_item_id' => $menu_item_id
|
|
];
|
|
|
|
if (!$this->seasonal_item_availability->insert($item_seasonal)) {
|
|
throw new \Exception('Failed to insert seasonal availability!');
|
|
}
|
|
} else if ($availability['type'] == 'regular') {
|
|
if (!empty($availability['regular'])) {
|
|
foreach ($availability['regular'] as $value) {
|
|
$item_regular = [
|
|
'day_of_week' => $value['day_of_week'],
|
|
'is_enabled' => $value['is_enabled'],
|
|
'start_time' => $value['start_time'],
|
|
'end_time' => $value['end_time'],
|
|
'menu_item_id' => $menu_item_id
|
|
];
|
|
|
|
if (!$this->regular_item_availability->insert($item_regular)) {
|
|
throw new \Exception('Failed to insert regular availability!');
|
|
}
|
|
}
|
|
} else {
|
|
for ($int = 0; $int < 7; $int++) {
|
|
$item_regular = [
|
|
'day_of_week' => $int,
|
|
'is_enabled' => 1,
|
|
'start_time' => '00:00:00',
|
|
'end_time' => '23:59:59',
|
|
'menu_item_id' => $menu_item_id
|
|
];
|
|
if (!$this->regular_item_availability->insert($item_regular)) {
|
|
throw new \Exception('Failed to insert regular availability!');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!empty($variation)) {
|
|
foreach ($variation as $i => $value) {
|
|
$variationImage = $this->request->getFile("variation_image$i");
|
|
|
|
if (!$variationImage || !$variationImage->isValid() || $variationImage->hasMoved()) {
|
|
$this->db->transRollback();
|
|
return $this->failValidationErrors(['variation_image' => 'Please upload an image for the variation.']);
|
|
}
|
|
$result = save_image_with_compression(
|
|
$variationImage,
|
|
FCPATH . 'backend/uploads/variation_images/',
|
|
FCPATH . 'backend/uploads/variation_images_compressed/',
|
|
900,
|
|
80
|
|
);
|
|
|
|
$imageUrl = base_url('backend/uploads/variation_images/' . basename($result['original']));
|
|
$compressedUrl = base_url('backend/uploads/variation_images_compressed/' . basename($result['compressed']));
|
|
|
|
$variation_data = [
|
|
'title' => $value['title'],
|
|
'price' => $value['price'],
|
|
'order_index' => $value['order_index'] ? $value['order_index'] : 0,
|
|
'menu_item_id' => $menu_item_id,
|
|
'images' => $imageUrl,
|
|
'images_compressed' => $compressedUrl,
|
|
|
|
];
|
|
|
|
$variation_id = $this->menu_item_variations->insert($variation_data);
|
|
if (!$variation_id) {
|
|
throw new \Exception('Failed to insert variation!');
|
|
}
|
|
|
|
if (!empty($value['tag'])) {
|
|
foreach ($value['tag'] as $value1) {
|
|
$variation_tag = [
|
|
'tag_id' => $value1,
|
|
'variation_id' => $variation_id
|
|
];
|
|
|
|
if (!$this->variation_tags->insert($variation_tag)) {
|
|
throw new \Exception('Failed to insert variation tag!');
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!empty($value['option_group'])) {
|
|
foreach ($value['option_group'] as $i => $value2) {
|
|
$variation_option_group = [
|
|
'option_group_id' => $value2,
|
|
'variation_id' => $variation_id,
|
|
'order_index' => $i // set order_index as the array index
|
|
];
|
|
|
|
if (!$this->variation_options_groups->insert($variation_option_group)) {
|
|
throw new \Exception('Failed to insert variation option group!');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!empty($menu)) {
|
|
foreach ($menu as $menuData) {
|
|
$outletMenuRow = [
|
|
'menu_item_id' => $menu_item_id,
|
|
'outlet_id' => $menuData['outlet_id'],
|
|
// 'menu_type' => $menuData['menu_type'],
|
|
];
|
|
if (!$this->outlet_menus->insert($outletMenuRow)) {
|
|
throw new \Exception('Failed to insert outlet_menu!');
|
|
}
|
|
}
|
|
}
|
|
$image = $this->request->getFile('image');
|
|
if ($image) {
|
|
if (!$image->isValid()) {
|
|
log_message('error', 'Image is not valid: ' . $image->getErrorString());
|
|
} else if ($image->hasMoved()) {
|
|
log_message('error', 'Image already moved');
|
|
} else {
|
|
$originalPath = $_SERVER['DOCUMENT_ROOT'] . '/backend/uploads/menu_images/';
|
|
$compressedPath = $_SERVER['DOCUMENT_ROOT'] . '/backend/uploads/menu_images_compressed/';
|
|
$result = save_image_with_compression($image, $originalPath, $compressedPath, 500, 60);
|
|
|
|
$originalName = basename($result['original']);
|
|
$compressedName = basename($result['compressed']);
|
|
|
|
if (
|
|
!$this->menu_images->insert([
|
|
'menu_item_id' => $menu_item_id,
|
|
'image_url' => $originalName,
|
|
'image_url_compressed' => $compressedName,
|
|
])
|
|
) {
|
|
throw new \Exception('Failed to insert image!');
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
$this->db->transCommit();
|
|
|
|
return $this->respond([
|
|
"status" => 200,
|
|
"message" => "Menu Item created successfully",
|
|
"data" => array_merge($menuItemData, [
|
|
"id" => $menu_item_id
|
|
])
|
|
]);
|
|
} catch (\Exception $e) {
|
|
$this->db->transRollback();
|
|
|
|
return $this->fail($e->getMessage(), 400);
|
|
}
|
|
}
|
|
|
|
public function update($id = null)
|
|
{
|
|
// echo(123);exit;
|
|
$menu_item = $this->menu_items->find($id);
|
|
if ($menu_item) {
|
|
$validation = $this->validate([
|
|
'title' => 'required',
|
|
'price' => 'required',
|
|
'status' => 'required',
|
|
]);
|
|
|
|
if (!$validation) {
|
|
return $this->failValidationErrors($this->validator->getErrors());
|
|
}
|
|
|
|
$title = $this->request->getVar('title');
|
|
$short_description = $this->request->getVar('short_description');
|
|
$long_description = $this->request->getVar('long_description');
|
|
$price = $this->request->getVar('price');
|
|
$packaging_price = $this->request->getVar('packaging_price');
|
|
$pwp_price = $this->request->getVar('pwp_price');
|
|
$status = $this->request->getVar('status');
|
|
$order_index = $this->request->getVar('order_index');
|
|
$category_group = $this->request->getVar("category");
|
|
$availability = $this->request->getVar("availability");
|
|
$membership_tier = $this->request->getVar("membership_tier");
|
|
$images = $this->request->getFiles();
|
|
$variation_group = $this->request->getVar("variation");
|
|
$menu_tag_group = $this->request->getVar("menu_tag");
|
|
$menu_option_group = $this->request->getVar("menu_option_group");
|
|
$existing_image = $this->request->getVar("existing_image");
|
|
$menu = $this->request->getVar('menu');
|
|
$pwp_price = $this->request->getVar('pwp_price');
|
|
|
|
|
|
if (!empty($images['image'])) {
|
|
$images = $images['image'];
|
|
}
|
|
|
|
$menuItemData = [
|
|
'title' => $title,
|
|
'short_description' => $short_description,
|
|
'long_description' => $long_description,
|
|
'price' => $price,
|
|
'packaging_price' => $packaging_price,
|
|
'pwp_price' => $pwp_price,
|
|
'status' => $status,
|
|
'order_index' => $order_index,
|
|
'availability_type' => $availability['type'],
|
|
'membership_tier' => $membership_tier,
|
|
'pwp_price' => $pwp_price
|
|
];
|
|
$this->db->transStart();
|
|
|
|
try {
|
|
$updateMenuItem = $this->menu_items->update($id, $menuItemData);
|
|
if (!$updateMenuItem) {
|
|
throw new \Exception('Failed to update menu item!');
|
|
}
|
|
|
|
if (!empty($category_group)) {
|
|
$existing_menu_category = $this->menu_items_categories->where('menu_item_id', $id)->findAll();
|
|
$existing_menu_category_group = array_column($existing_menu_category, 'category_id', 'id');
|
|
|
|
$non_update_category = [];
|
|
|
|
foreach ($category_group as $category) {
|
|
|
|
if (!in_array($category, $existing_menu_category_group)) {
|
|
$item_category = [
|
|
'menu_item_id' => $id,
|
|
'category_id' => $category
|
|
];
|
|
|
|
if (!$this->menu_items_categories->insert($item_category)) {
|
|
throw new \Exception('Failed to update category!');
|
|
}
|
|
} else {
|
|
$non_update_category[] = $category;
|
|
}
|
|
}
|
|
|
|
foreach ($existing_menu_category_group as $exist_menu_category_id => $exist_menu_category) {
|
|
if (!in_array($exist_menu_category, $non_update_category)) {
|
|
$this->menu_items_categories->delete($exist_menu_category_id);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!empty($menu_tag_group)) {
|
|
$existing_menu_tag = $this->menu_item_tags->where('menu_item_id', $id)->findAll();
|
|
|
|
$existing_menu_tag_group = array_column($existing_menu_tag, 'tag_id', 'id');
|
|
|
|
$non_update_category = [];
|
|
|
|
foreach ($menu_tag_group as $menu_tag) {
|
|
|
|
if (!in_array($menu_tag, $existing_menu_tag_group)) {
|
|
|
|
$item_tag = [
|
|
'menu_item_id' => $id,
|
|
'tag_id' => $menu_tag
|
|
];
|
|
|
|
if (!$this->menu_item_tags->insert($item_tag)) {
|
|
throw new \Exception('Failed to update menu tag!');
|
|
}
|
|
} else {
|
|
$non_update_category[] = $menu_tag;
|
|
}
|
|
}
|
|
|
|
foreach ($existing_menu_tag_group as $exist_menu_tag_id => $exist_menu_tag) {
|
|
if (!in_array($exist_menu_tag, $non_update_category)) {
|
|
$this->menu_item_tags->delete($exist_menu_tag_id);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!empty($menu_option_group)) {
|
|
$existing_menu_option = $this->menu_item_options_groups->where('menu_item_id', $id)->findAll();
|
|
$existing_menu_option_group = array_column($existing_menu_option, 'option_group_id', 'id');
|
|
$non_update_category = [];
|
|
|
|
foreach ($menu_option_group as $i => $option) {
|
|
|
|
if (!in_array($option, $existing_menu_option_group)) {
|
|
$item_option_group_data = [
|
|
'menu_item_id' => $id,
|
|
'option_group_id' => $option,
|
|
'order_index' => $i // set order_index as the array index
|
|
];
|
|
|
|
if (!$this->menu_item_options_groups->insert($item_option_group_data)) {
|
|
throw new \Exception('Failed to update menu options group!');
|
|
}
|
|
} else {
|
|
$non_update_category[] = $option;
|
|
}
|
|
}
|
|
|
|
foreach ($existing_menu_option_group as $option_group_id => $exist_option) {
|
|
if (!in_array($exist_option, $non_update_category)) {
|
|
$this->menu_item_options_groups->delete($option_group_id);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!empty($availability)) {
|
|
|
|
if ($availability['type'] == 'seasonal') {
|
|
$exist_season = $this->seasonal_item_availability->where('menu_item_id', $id)->first();
|
|
$item_seasonal = [
|
|
'start_date' => $availability['seasonal']['start_date'],
|
|
'end_date' => $availability['seasonal']['end_date'],
|
|
'start_time' => $availability['seasonal']['start_time'],
|
|
'end_time' => $availability['seasonal']['end_time'],
|
|
];
|
|
|
|
if ($exist_season) {
|
|
if (!$this->seasonal_item_availability->update($exist_season['id'], $item_seasonal)) {
|
|
throw new \Exception('Failed to update seasonal availability!');
|
|
}
|
|
} else {
|
|
$item_seasonal['menu_item_id'] = $id;
|
|
if (!$this->seasonal_item_availability->insert($item_seasonal)) {
|
|
throw new \Exception('Failed to update seasonal availability!');
|
|
}
|
|
}
|
|
} else if ($availability['type'] == 'regular') {
|
|
if (!empty($availability['regular'])) {
|
|
foreach ($availability['regular'] as $value) {
|
|
$exist_regular = $this->regular_item_availability->where('menu_item_id', $id)
|
|
->where('day_of_week', $value['day_of_week'])
|
|
->first();
|
|
$item_regular = [
|
|
'day_of_week' => $value['day_of_week'],
|
|
'is_enabled' => $value['is_enabled'],
|
|
'start_time' => $value['start_time'],
|
|
'end_time' => $value['end_time'],
|
|
];
|
|
if ($exist_regular) {
|
|
if (!$this->regular_item_availability->update($exist_regular['id'], $item_regular)) {
|
|
throw new \Exception('Failed to update regular availability!');
|
|
}
|
|
} else {
|
|
|
|
$item_regular['menu_item_id'] = $id;
|
|
if (!$this->regular_item_availability->insert($item_regular)) {
|
|
throw new \Exception('Failed to update regular availability!');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$exist_variation = $this->menu_item_variations->where('menu_item_id', $id)->findAll();
|
|
$exist_variation_group = array_column($exist_variation, 'id');
|
|
|
|
$update_category = [];
|
|
|
|
if (!empty($variation_group)) {
|
|
foreach ($variation_group as $i => $variation) {
|
|
$variation_data = [
|
|
'title' => $variation['title'],
|
|
'price' => $variation['price'],
|
|
'order_index' => $variation['order_index'] ?? 0,
|
|
'images' => $variation['images'] ?? null,
|
|
'images_compressed' => $variation['images_compressed'] ?? null,
|
|
];
|
|
|
|
// ✅ Handle variation image upload
|
|
$variationImage = $this->request->getFile("variation_image$i");
|
|
if ($variationImage && $variationImage->isValid() && !$variationImage->hasMoved()) {
|
|
$result = save_image_with_compression(
|
|
$variationImage,
|
|
FCPATH . 'backend/uploads/variation_images/',
|
|
FCPATH . 'backend/uploads/variation_images_compressed/',
|
|
900,
|
|
80
|
|
);
|
|
|
|
$imageUrl = base_url('backend/uploads/variation_images/' . basename($result['original']));
|
|
$compressedUrl = base_url('backend/uploads/variation_images_compressed/' . basename($result['compressed']));
|
|
|
|
$variation_data['images'] = $imageUrl;
|
|
$variation_data['images_compressed'] = $compressedUrl;
|
|
}
|
|
|
|
// ✅ Update existing variation
|
|
if (!empty($variation['id']) && in_array($variation['id'], $exist_variation_group)) {
|
|
$variation_id = $variation['id'];
|
|
$this->menu_item_variations->update($variation_id, $variation_data);
|
|
|
|
// === Update tags ===
|
|
$exist_variation_tag = $this->variation_tags->where('variation_id', $variation_id)->findAll();
|
|
$exist_variation_tag_group = array_column($exist_variation_tag, 'tag_id', 'id');
|
|
$non_update_tags = [];
|
|
|
|
if (!empty($variation['tag'])) {
|
|
foreach ($variation['tag'] as $tagId) {
|
|
if (!in_array($tagId, $exist_variation_tag_group)) {
|
|
$this->variation_tags->insert([
|
|
'tag_id' => $tagId,
|
|
'variation_id' => $variation_id
|
|
]);
|
|
} else {
|
|
$non_update_tags[] = $tagId;
|
|
}
|
|
}
|
|
|
|
foreach ($exist_variation_tag_group as $tagRowId => $tagId) {
|
|
if (!in_array($tagId, $non_update_tags)) {
|
|
$this->variation_tags->delete($tagRowId);
|
|
}
|
|
}
|
|
}
|
|
|
|
// === Update option groups ===
|
|
$exist_option_groups = $this->variation_options_groups->where('variation_id', $variation_id)->findAll();
|
|
$exist_option_map = array_column($exist_option_groups, 'option_group_id', 'id');
|
|
$non_update_groups = [];
|
|
|
|
if (!empty($variation['option_group'])) {
|
|
foreach ($variation['option_group'] as $oi => $groupId) {
|
|
if (!in_array($groupId, $exist_option_map)) {
|
|
$this->variation_options_groups->insert([
|
|
'option_group_id' => $groupId,
|
|
'variation_id' => $variation_id,
|
|
'order_index' => $oi
|
|
]);
|
|
} else {
|
|
$non_update_groups[] = $groupId;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Delete any option groups not in the updated list
|
|
foreach ($exist_option_map as $rowId => $groupId) {
|
|
if (!in_array($groupId, $non_update_groups)) {
|
|
$this->variation_options_groups->delete($rowId);
|
|
}
|
|
}
|
|
}
|
|
// ✅ Insert new variation
|
|
else {
|
|
$variation_data['menu_item_id'] = $id;
|
|
$variation_id = $this->menu_item_variations->insert($variation_data);
|
|
|
|
if (!empty($variation['tag'])) {
|
|
foreach ($variation['tag'] as $tagId) {
|
|
$this->variation_tags->insert([
|
|
'tag_id' => $tagId,
|
|
'variation_id' => $variation_id
|
|
]);
|
|
}
|
|
}
|
|
|
|
if (!empty($variation['option_group'])) {
|
|
foreach ($variation['option_group'] as $oi => $groupId) {
|
|
$this->variation_options_groups->insert([
|
|
'option_group_id' => $groupId,
|
|
'variation_id' => $variation_id,
|
|
'order_index' => $oi
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
|
|
$update_category[] = $variation_id;
|
|
}
|
|
}
|
|
|
|
// ✅ Always delete variations not sent by frontend
|
|
foreach ($exist_variation_group as $exist_variation_value) {
|
|
if (!in_array($exist_variation_value, $update_category)) {
|
|
$this->menu_item_variations->delete($exist_variation_value);
|
|
$this->variation_tags->where('variation_id', $exist_variation_value)->delete();
|
|
$this->variation_options_groups->where('variation_id', $exist_variation_value)->delete();
|
|
}
|
|
}
|
|
|
|
if (!empty($images)) {
|
|
|
|
foreach ($images as $image) {
|
|
$image_file = $image;
|
|
if ($image_file && $image_file->isValid() && !$image_file->hasMoved()) {
|
|
$uploadPath = $_SERVER['DOCUMENT_ROOT'] . '/backend/uploads/menu_images/';
|
|
if (!is_dir($uploadPath)) {
|
|
mkdir($uploadPath, 0755, true);
|
|
}
|
|
|
|
$filename = $image_file->getClientName();
|
|
$extension = pathinfo($filename, PATHINFO_EXTENSION);
|
|
$timestamp = time();
|
|
$newImageName = $id . "_" . $timestamp . "." . $extension;
|
|
|
|
$image_file->move($uploadPath, $newImageName);
|
|
|
|
if (
|
|
!$this->menu_images->insert([
|
|
'menu_item_id' => $id,
|
|
'image_url' => $newImageName
|
|
])
|
|
) {
|
|
throw new \Exception('Failed to update image!');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!empty($menu)) {
|
|
$this->outlet_menus->where('menu_item_id', $id)->delete();
|
|
|
|
foreach ($menu as $menuData) {
|
|
$outletMenuRow = [
|
|
'menu_item_id' => $id,
|
|
'outlet_id' => $menuData['outlet_id'],
|
|
];
|
|
if (!$this->outlet_menus->insert($outletMenuRow)) {
|
|
throw new \Exception('Failed to update outlet_menu!');
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!empty($existing_image)) {
|
|
$exist_images = $this->menu_images->where('menu_item_id', $id)->findAll();
|
|
$exist_images_group = array_column($exist_images, 'id');
|
|
foreach ($exist_images_group as $image_id) {
|
|
if (!in_array($image_id, $existing_image)) {
|
|
$this->menu_images->delete($image_id);
|
|
}
|
|
}
|
|
}
|
|
|
|
$this->db->transCommit();
|
|
|
|
return $this->respond([
|
|
"status" => 200,
|
|
"message" => "Menu Item updated successfully",
|
|
"data" => $menuItemData
|
|
]);
|
|
} catch (\Exception $e) {
|
|
$this->db->transRollback();
|
|
|
|
return $this->fail($e->getMessage(), 400);
|
|
}
|
|
} else {
|
|
return $this->fail("No menu item found!", 400);
|
|
}
|
|
}
|
|
|
|
public function updateMenuOrderIndex()
|
|
{
|
|
$menu_order = $this->request->getVar('menu_order');
|
|
|
|
if (!empty($menu_order)) {
|
|
foreach ($menu_order as $key => $value) {
|
|
$this->menu_items->update($key, ['order_index' => $value]);
|
|
}
|
|
}
|
|
|
|
return $this->respond(['status' => 200, 'message' => 'Menu order index updated successfully!']);
|
|
}
|
|
|
|
public function delete($id = null)
|
|
{
|
|
$menu_item = $this->menu_items->find($id);
|
|
|
|
if ($menu_item) {
|
|
$this->menu_items->update($id, ['status' => 'trash']);
|
|
$this->menu_items->delete($id);
|
|
$this->menu_images->where('menu_item_id', $id)->delete();
|
|
$this->menu_item_variations->deleteWithRelations($id);
|
|
$this->menu_item_tags->where('menu_item_id', $id)->delete();
|
|
$this->menu_item_options_groups->where('menu_item_id', $id)->delete();
|
|
$this->regular_item_availability->where('menu_item_id', $id)->delete();
|
|
$this->seasonal_item_availability->where('menu_item_id', $id)->delete();
|
|
|
|
return $this->respond(['status' => 200, 'message' => 'Menu Item successfully deleted!']);
|
|
} else {
|
|
return $this->fail('No menu item found!', 400);
|
|
}
|
|
}
|
|
|
|
public function getMenuCategoryList()
|
|
{
|
|
$menuCategories = $this->menu_categories->where('deleted_at', null)->where('status', 'active')->findAll();
|
|
|
|
if (empty($menuCategories)) {
|
|
return $this->fail("No category found.", 400);
|
|
}
|
|
|
|
$allCategoryData = [];
|
|
|
|
foreach ($menuCategories as $category) {
|
|
$allCategoryData[] = [
|
|
'id' => $category['id'],
|
|
'title' => $category['title'],
|
|
'description' => $category['description'],
|
|
'status' => $category['status'],
|
|
'image_url' => getImagePath('menu_categories', $category['image_url']),
|
|
'order_index' => $category['order_index'],
|
|
'compressed_image_url' => getImagePath('menu_categories_compressed', $category['compressed_image_url']),
|
|
'created_at' => $category['created_at'],
|
|
];
|
|
}
|
|
return $this->respond(["status" => 200, "message" => "Category Found.", "data" => $allCategoryData]);
|
|
}
|
|
|
|
public function showMenuCategory($id = null)
|
|
{
|
|
$menuCategories = $this->menu_categories->find($id);
|
|
|
|
if (empty($menuCategories)) {
|
|
return $this->fail("No category found.", 400);
|
|
}
|
|
|
|
$allCategoryData = [];
|
|
|
|
if ($menuCategories) {
|
|
$allCategoryData[] = [
|
|
'id' => $menuCategories['id'],
|
|
'title' => $menuCategories['title'],
|
|
'description' => $menuCategories['description'],
|
|
'status' => $menuCategories['status'],
|
|
'image' => getImagePath('menu_categories', $menuCategories['image_url']),
|
|
'order_index' => $menuCategories['order_index'],
|
|
'created_at' => $menuCategories['created_at'],
|
|
];
|
|
}
|
|
return $this->respond(["status" => 200, "message" => "Category Found.", "data" => $allCategoryData]);
|
|
}
|
|
|
|
public function createMenuCategory()
|
|
{
|
|
$validation = $this->validate([
|
|
'title' => 'required',
|
|
'description' => 'permit_empty',
|
|
'status' => 'required',
|
|
'image' => 'permit_empty|is_image[image]',
|
|
'order_index' => 'permit_empty'
|
|
]);
|
|
|
|
if (!$validation) {
|
|
return $this->failValidationErrors($this->validator->getErrors());
|
|
}
|
|
|
|
$title = $this->request->getVar('title');
|
|
$description = $this->request->getVar('description');
|
|
$status = $this->request->getVar('status');
|
|
$image = $this->request->getFile('image');
|
|
$order_index = $this->request->getVar('order_index') ? $this->request->getVar('order_index') : 0;
|
|
|
|
$imagePath = '';
|
|
|
|
$category = [
|
|
'title' => $title,
|
|
'description' => $description,
|
|
'status' => $status,
|
|
'order_index' => $order_index
|
|
];
|
|
|
|
$id = $this->menu_categories->insert($category);
|
|
|
|
if (!$id) {
|
|
return $this->fail('Fail to create category!', 400);
|
|
}
|
|
|
|
$imagePath = '';
|
|
$compressedImagePath = '';
|
|
|
|
// if ($image && $image->isValid() && !$image->hasMoved()) {
|
|
|
|
// $uploadPath = $_SERVER['DOCUMENT_ROOT'] . '/backend/uploads/menu_categories/';
|
|
|
|
// if (!is_dir($uploadPath)) {
|
|
// mkdir($uploadPath, 0755, true);
|
|
// }
|
|
|
|
// $filename = $image->getClientName();
|
|
// $extension = pathinfo($filename, PATHINFO_EXTENSION);
|
|
// $timestamp = time();
|
|
// $newImageName = $id . "_" . $timestamp . "." . $extension;
|
|
|
|
// $image->move($uploadPath, $newImageName);
|
|
|
|
// $imagePath = $newImageName;
|
|
// }
|
|
if ($image && $image->isValid() && !$image->hasMoved()) {
|
|
$result = save_image_with_compression(
|
|
$image,
|
|
FCPATH . 'backend/uploads/menu_categories',
|
|
FCPATH . 'backend/uploads/menu_categories_compressed',
|
|
900, // width
|
|
80 // quality
|
|
);
|
|
$imagePath = basename($result['original']);
|
|
$compressedImagePath = basename($result['compressed']);
|
|
}
|
|
|
|
$this->menu_categories->update($id, ['image_url' => $imagePath, 'compressed_image_url' => $compressedImagePath,]);
|
|
|
|
return $this->respond(["status" => 200, "message" => "Category Successfully Created!"]);
|
|
}
|
|
|
|
public function createQuickMenuCategory()
|
|
{
|
|
$validation = $this->validate([
|
|
'title' => 'required',
|
|
]);
|
|
|
|
if (!$validation) {
|
|
return $this->failValidationErrors($this->validator->getErrors());
|
|
}
|
|
|
|
$title = $this->request->getVar('title');
|
|
|
|
$category = [
|
|
'title' => $title,
|
|
];
|
|
|
|
$id = $this->menu_categories->insert($category);
|
|
|
|
if ($id) {
|
|
return $this->respond(["status" => 200, "message" => "Category Successfully Created!"]);
|
|
}
|
|
|
|
return $this->fail('Fail to create category!', 400);
|
|
}
|
|
|
|
public function updateMenuCategory($id = null)
|
|
{
|
|
$category = $this->menu_categories->find($id);
|
|
|
|
if ($category) {
|
|
$validation = $this->validate([
|
|
'title' => 'required',
|
|
'description' => 'permit_empty',
|
|
'status' => 'required',
|
|
'image' => 'permit_empty|is_image[image]',
|
|
'order_index' => 'permit_empty'
|
|
]);
|
|
|
|
if (!$validation) {
|
|
return $this->failValidationErrors($this->validator->getErrors());
|
|
}
|
|
|
|
$title = $this->request->getVar('title');
|
|
$description = $this->request->getVar('description');
|
|
$status = $this->request->getVar('status');
|
|
$image = $this->request->getFile('image');
|
|
$order_index = $this->request->getVar('order_index') ? $this->request->getVar('order_index') : 0;
|
|
|
|
$updatedCategoryData = [
|
|
'title' => $title,
|
|
'description' => $description,
|
|
'status' => $status,
|
|
'order_index' => $order_index
|
|
];
|
|
|
|
$imagePath = $category['image_url'];
|
|
$compressedImagePath = $category['compressed_image_url'] ?? '';
|
|
|
|
// if ($image && $image->isValid() && !$image->hasMoved()) {
|
|
// $uploadPath = $_SERVER['DOCUMENT_ROOT'] . '/backend/uploads/menu_categories/';
|
|
|
|
// if (!is_dir($uploadPath)) {
|
|
// mkdir($uploadPath, 0755, true);
|
|
// }
|
|
|
|
// $filename = $image->getClientName();
|
|
// $extension = pathinfo($filename, PATHINFO_EXTENSION);
|
|
// $timestamp = time();
|
|
// $newImageName = $id . "_" . $timestamp . "." . $extension;
|
|
|
|
// $image->move($uploadPath, $newImageName);
|
|
|
|
// $imagePath = $newImageName;
|
|
// }
|
|
if ($image && $image->isValid() && !$image->hasMoved()) {
|
|
$result = save_image_with_compression(
|
|
$image,
|
|
FCPATH . 'backend/uploads/menu_categories',
|
|
FCPATH . 'backend/uploads/menu_categories_compressed',
|
|
900,
|
|
80
|
|
);
|
|
$imagePath = basename($result['original']);
|
|
$compressedImagePath = basename($result['compressed']);
|
|
}
|
|
|
|
$updatedCategoryData['image_url'] = $imagePath;
|
|
$updatedCategoryData['compressed_image_url'] = $compressedImagePath;
|
|
|
|
|
|
$this->menu_categories->update($id, $updatedCategoryData);
|
|
|
|
return $this->respond(["status" => 200, "message" => "Category successfully updated!"]);
|
|
} else {
|
|
return $this->fail('No category found!', 400);
|
|
}
|
|
}
|
|
|
|
public function updateMenuCategoryOrderIndex()
|
|
{
|
|
$category_order = $this->request->getVar('category_order');
|
|
if (!empty($category_order)) {
|
|
foreach ($category_order as $key => $value) {
|
|
$this->menu_categories->update($key, ['order_index' => $value]);
|
|
}
|
|
}
|
|
|
|
return $this->respond(['status' => 200, 'message' => 'Category order index updated successfully!']);
|
|
}
|
|
|
|
public function updateOptionGroupOrderIndex()
|
|
{
|
|
$option_group_order = $this->request->getVar('option_group_order');
|
|
if (!empty($option_group_order)) {
|
|
foreach ($option_group_order as $key => $value) {
|
|
$this->option_groups->update($key, ['order_index' => $value]);
|
|
}
|
|
}
|
|
|
|
return $this->respond(['status' => 200, 'message' => 'Option group order index updated successfully!']);
|
|
}
|
|
|
|
public function updateMenuItemOptionGroupOrderIndex($menu_item_id = null)
|
|
{
|
|
$data = $this->request->getJSON(true); // expects an array of objects
|
|
|
|
if (!$menu_item_id || empty($data)) {
|
|
return $this->fail('Missing menu_item_id or data.', 400);
|
|
}
|
|
|
|
foreach ($data as $item) {
|
|
if (isset($item['option_group_id'], $item['order_index'])) {
|
|
// Find the row with this menu_item_id and option_group_id
|
|
$row = $this->menu_item_options_groups
|
|
->where('menu_item_id', $menu_item_id)
|
|
->where('option_group_id', $item['option_group_id'])
|
|
->first();
|
|
|
|
if ($row) {
|
|
$this->menu_item_options_groups->update($row['id'], [
|
|
'order_index' => $item['order_index']
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
|
|
return $this->respond(['status' => 200, 'message' => 'Menu item option group order index updated successfully!']);
|
|
}
|
|
|
|
public function updateMenuItemVariationOptionGroupOrderIndex($variation_id = null)
|
|
{
|
|
$data = $this->request->getJSON(true); // expects an array of objects
|
|
|
|
if (!$variation_id || empty($data)) {
|
|
return $this->fail('Missing variation_id or data.', 400);
|
|
}
|
|
|
|
foreach ($data as $item) {
|
|
if (isset($item['option_group_id'], $item['order_index'])) {
|
|
// Find the row with this variation_id and option_group_id
|
|
$row = $this->variation_options_groups
|
|
->where('variation_id', $variation_id)
|
|
->where('option_group_id', $item['option_group_id'])
|
|
->first();
|
|
|
|
if ($row) {
|
|
$this->variation_options_groups->update($row['id'], [
|
|
'order_index' => $item['order_index']
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
|
|
return $this->respond(['status' => 200, 'message' => 'Variation option group order index updated successfully!']);
|
|
}
|
|
|
|
public function updateOptionOrderIndex()
|
|
{
|
|
$option_order = $this->request->getVar('option_order');
|
|
if (!empty($option_order)) {
|
|
foreach ($option_order as $key => $value) {
|
|
$this->options->update($key, ['order_index' => $value]);
|
|
}
|
|
}
|
|
|
|
return $this->respond(['status' => 200, 'message' => 'Option group order index updated successfully!']);
|
|
}
|
|
|
|
function deleteMenuCategory($id = null)
|
|
{
|
|
$category = $this->menu_categories->find($id);
|
|
|
|
if ($category) {
|
|
$this->menu_categories->update($id, ['status' => 'trash']);
|
|
$this->menu_categories->delete($id);
|
|
|
|
return $this->respond(['status' => 200, 'message' => 'Category successfully deleted!']);
|
|
} else {
|
|
return $this->fail('No category found!', 400);
|
|
}
|
|
}
|
|
}
|