241 lines
8.8 KiB
PHP
241 lines
8.8 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Frontend;
|
|
|
|
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\VariationOptionsGroups;
|
|
use App\Models\VariationTags;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use CodeIgniter\RESTful\ResourceController;
|
|
use CodeIgniter\Database\Config;
|
|
use App\Models\OutletMenus;
|
|
use App\Controllers\BaseController;
|
|
use App\Models\Options;
|
|
use App\Models\OptionsGroups;
|
|
use CodeIgniter\API\ResponseTrait;
|
|
|
|
|
|
class MenuItemController extends BaseController
|
|
{
|
|
use ResponseTrait;
|
|
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 $variation_options_groups;
|
|
private $variation_tags;
|
|
private $regular_item_availability;
|
|
private $seasonal_item_availability;
|
|
private $menu_images;
|
|
private $outlet_menus;
|
|
private $options;
|
|
private $option_groups;
|
|
|
|
|
|
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->options = new Options();
|
|
$this->option_groups = new OptionsGroups();
|
|
$this->db = Config::connect();
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$groups = $this->option_groups->findAll();
|
|
$data = [];
|
|
|
|
foreach ($groups as $group) {
|
|
$group['options'] = $this->options
|
|
->where('option_group_id', $group['id'])
|
|
->where('deleted_at', null)
|
|
->findAll();
|
|
$data[] = $group;
|
|
}
|
|
|
|
return $this->respond(['status' => 200, 'data' => $data]);
|
|
}
|
|
|
|
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'],
|
|
'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' => getVariationRelation($menuItem['id'])
|
|
];
|
|
}
|
|
return $this->respond(["status" => 200, "message" => "Successfully retrive data!", "data" => $menuItemData]);
|
|
}
|
|
|
|
public function showOption($id = null)
|
|
{
|
|
$group = $this->option_groups->find($id);
|
|
|
|
if (!$group) {
|
|
return $this->failNotFound("Option group not found.");
|
|
}
|
|
|
|
$group['options'] = $this->options
|
|
->where('option_group_id', $group['id'])
|
|
->where('deleted_at', null)
|
|
->findAll();
|
|
|
|
return $this->respond(['status' => 200, 'data' => $group]);
|
|
}
|
|
|
|
public function getAllMenus($outlet_id = null)
|
|
{
|
|
$db = \Config\Database::connect();
|
|
|
|
// Get all active categories first
|
|
$menu_categories = $db->table('menu_categories')
|
|
->select('id, title, image_url, status, compressed_image_url, order_index')
|
|
->where('status', 'active')
|
|
->where('deleted_at', null)
|
|
->orderBy('order_index', 'ASC')
|
|
->get()
|
|
->getResult();
|
|
|
|
// Get all tags for mapping
|
|
$all_tags = $db->table('tags')->select('id, title, icon_url')->get()->getResult();
|
|
$tagMap = [];
|
|
foreach ($all_tags as $tag) {
|
|
$tagMap[$tag->id] = [
|
|
'id' => $tag->id,
|
|
'title' => $tag->title,
|
|
'icon_url' => $tag->icon_url
|
|
];
|
|
}
|
|
|
|
$menu_items = [];
|
|
|
|
// Loop through each category to get corresponding items
|
|
foreach ($menu_categories as $category) {
|
|
// Get menu items for this category
|
|
$category_items = $db->table('menu_items')
|
|
->select('menu_items.id, menu_items.title, menu_items.short_description, menu_items.price, menu_items.availability_type, menu_items.order_index, menu_items.status, menu_items.membership_tier, menu_items.pwp')
|
|
->join('menu_item_categories', 'menu_item_categories.menu_item_id = menu_items.id')
|
|
->where('menu_item_categories.category_id', $category->id)
|
|
->where('menu_items.status', 'active')
|
|
->where('menu_items.deleted_at', null)
|
|
->orderBy('menu_items.order_index', 'ASC')
|
|
->get()
|
|
->getResult();
|
|
|
|
// Process each item in this category
|
|
foreach ($category_items as $item) {
|
|
// Get item images
|
|
$images = $db->table('menu_images')
|
|
->select('image_url, image_url_compressed')
|
|
->where('menu_item_id', $item->id)
|
|
->get()
|
|
->getResult();
|
|
|
|
// Get item tags
|
|
$menu_item_tags_links = $db->table('menu_item_tags')
|
|
->select('tag_id')
|
|
->where('menu_item_id', $item->id)
|
|
->get()
|
|
->getResult();
|
|
|
|
$tag_list = [];
|
|
foreach ($menu_item_tags_links as $tag_link) {
|
|
if (isset($tagMap[$tag_link->tag_id])) {
|
|
$tag_list[] = $tagMap[$tag_link->tag_id];
|
|
}
|
|
}
|
|
|
|
// Check if item is available in outlet
|
|
$is_available = $this->outlet_menus
|
|
->where('outlet_id', $outlet_id)
|
|
->where('menu_item_id', $item->id)
|
|
->first();
|
|
|
|
// Get category IDs for this item (should include current category)
|
|
$item_categories = $db->table('menu_item_categories')
|
|
->select('category_id')
|
|
->where('menu_item_id', $item->id)
|
|
->where('deleted_at', null)
|
|
->get()
|
|
->getResult();
|
|
|
|
$category_ids = [];
|
|
foreach ($item_categories as $cat_link) {
|
|
$category_ids[] = $cat_link->category_id;
|
|
}
|
|
|
|
$membership_tier = $item->membership_tier;
|
|
|
|
$menu_items[] = [
|
|
'id' => $item->id,
|
|
'title' => $item->title,
|
|
'short_description' => $item->short_description,
|
|
'price' => $item->price,
|
|
'availability_type' => $item->availability_type,
|
|
'order_index' => $item->order_index,
|
|
'status' => $item->status,
|
|
'images' => $images,
|
|
'category_ids' => $category_ids,
|
|
'tags' => $tag_list,
|
|
'is_available' => $is_available ? true : false,
|
|
'membership_tier' => $membership_tier,
|
|
'pwp' => $item->pwp,
|
|
];
|
|
}
|
|
}
|
|
|
|
return $this->response->setJSON([
|
|
'status' => 'success',
|
|
'response' => '200',
|
|
'Categories' => $menu_categories,
|
|
'Items' => $menu_items
|
|
]);
|
|
}
|
|
|
|
}
|