223 lines
5.5 KiB
PHP
223 lines
5.5 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Servicio de Gestión de Menús
|
|
* Maneja la obtención y navegación de menús
|
|
*/
|
|
|
|
class MenuService
|
|
{
|
|
private $db;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->db = Database::getInstance();
|
|
}
|
|
|
|
/**
|
|
* Obtiene un menú por su ID
|
|
* @param int $menuId
|
|
* @return array|null
|
|
*/
|
|
public function getMenu(int $menuId): ?array
|
|
{
|
|
$stmt = $this->db->prepare("
|
|
SELECT * FROM menus
|
|
WHERE id = ?
|
|
AND status = 'active'
|
|
LIMIT 1
|
|
");
|
|
$stmt->execute([$menuId]);
|
|
$menu = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$menu) {
|
|
return null;
|
|
}
|
|
|
|
// Obtener opciones del menú
|
|
$menu['options'] = $this->getMenuOptions($menuId);
|
|
|
|
return $menu;
|
|
}
|
|
|
|
/**
|
|
* Obtiene un menú por su clave
|
|
* @param string $menuKey
|
|
* @return array|null
|
|
*/
|
|
public function getMenuByKey(string $menuKey): ?array
|
|
{
|
|
$stmt = $this->db->prepare("
|
|
SELECT * FROM menus
|
|
WHERE menu_key = ?
|
|
AND status = 'active'
|
|
LIMIT 1
|
|
");
|
|
$stmt->execute([$menuKey]);
|
|
$menu = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$menu) {
|
|
return null;
|
|
}
|
|
|
|
$menu['options'] = $this->getMenuOptions($menu['id']);
|
|
|
|
return $menu;
|
|
}
|
|
|
|
/**
|
|
* Obtiene las opciones de un menú
|
|
* @param int $menuId
|
|
* @return array
|
|
*/
|
|
public function getMenuOptions(int $menuId): array
|
|
{
|
|
$stmt = $this->db->prepare("
|
|
SELECT * FROM menu_options
|
|
WHERE menu_id = ?
|
|
AND is_active = 1
|
|
ORDER BY option_order ASC
|
|
");
|
|
$stmt->execute([$menuId]);
|
|
|
|
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
/**
|
|
* Obtiene el ID del menú principal
|
|
* @return int|null
|
|
*/
|
|
public function getMainMenuId(): ?int
|
|
{
|
|
$stmt = $this->db->prepare("
|
|
SELECT id FROM menus
|
|
WHERE menu_key = 'lab_menu_principal'
|
|
AND status = 'active'
|
|
LIMIT 1
|
|
");
|
|
$stmt->execute();
|
|
$result = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
return $result ? (int)$result['id'] : null;
|
|
}
|
|
|
|
/**
|
|
* Procesa una opción seleccionada del menú
|
|
* @param int $menuId
|
|
* @param string $optionKey
|
|
* @return array|null
|
|
*/
|
|
public function processMenuOption(int $menuId, string $optionKey): ?array
|
|
{
|
|
$stmt = $this->db->prepare("
|
|
SELECT * FROM menu_options
|
|
WHERE menu_id = ?
|
|
AND option_key = ?
|
|
AND is_active = 1
|
|
LIMIT 1
|
|
");
|
|
$stmt->execute([$menuId, $optionKey]);
|
|
$option = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$option) {
|
|
return null;
|
|
}
|
|
|
|
return [
|
|
'type' => $option['response_type'],
|
|
'text' => $option['response_text'],
|
|
'next_menu_id' => $option['next_menu_id']
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Formatea un menú con su welcome_message y opciones
|
|
* @param int $menuId
|
|
* @return string
|
|
*/
|
|
public function formatMenuMessage(int $menuId): string
|
|
{
|
|
$menu = $this->getMenu($menuId);
|
|
|
|
if (!$menu) {
|
|
return "❌ Menú no disponible.";
|
|
}
|
|
|
|
$message = $menu['welcome_message'];
|
|
|
|
// Si el welcome_message no incluye las opciones, agregarlas
|
|
if (!empty($menu['options']) && strpos($message, '1️⃣') === false) {
|
|
$message .= "\n\n";
|
|
foreach ($menu['options'] as $option) {
|
|
$emoji = $this->getNumberEmoji($option['option_key']);
|
|
$message .= "$emoji {$option['option_text']}\n";
|
|
}
|
|
}
|
|
|
|
return $message;
|
|
}
|
|
|
|
/**
|
|
* Obtiene el emoji de número para una opción
|
|
* @param string $number
|
|
* @return string
|
|
*/
|
|
private function getNumberEmoji(string $number): string
|
|
{
|
|
$emojis = [
|
|
'0' => '0️⃣',
|
|
'1' => '1️⃣',
|
|
'2' => '2️⃣',
|
|
'3' => '3️⃣',
|
|
'4' => '4️⃣',
|
|
'5' => '5️⃣',
|
|
'6' => '6️⃣',
|
|
'7' => '7️⃣',
|
|
'8' => '8️⃣',
|
|
'9' => '9️⃣'
|
|
];
|
|
|
|
return $emojis[$number] ?? $number;
|
|
}
|
|
|
|
/**
|
|
* Obtiene todos los menús activos
|
|
* @return array
|
|
*/
|
|
public function getAllMenus(): array
|
|
{
|
|
$stmt = $this->db->prepare("
|
|
SELECT m.*,
|
|
(SELECT COUNT(*) FROM menu_options WHERE menu_id = m.id AND is_active = 1) as options_count
|
|
FROM menus m
|
|
WHERE m.status = 'active'
|
|
ORDER BY m.created_at ASC
|
|
");
|
|
$stmt->execute();
|
|
|
|
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
/**
|
|
* Obtiene el menú padre de un menú
|
|
* @param int $menuId
|
|
* @return array|null
|
|
*/
|
|
public function getParentMenu(int $menuId): ?array
|
|
{
|
|
$stmt = $this->db->prepare("
|
|
SELECT parent_menu_id FROM menus
|
|
WHERE id = ?
|
|
LIMIT 1
|
|
");
|
|
$stmt->execute([$menuId]);
|
|
$result = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$result || !$result['parent_menu_id']) {
|
|
return null;
|
|
}
|
|
|
|
return $this->getMenu($result['parent_menu_id']);
|
|
}
|
|
}
|