Import Export Grocery List
And misc improvements
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/../includes/api_bootstrap.php';
|
||||
require_once __DIR__ . '/../includes/meal_helpers.php';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
|
||||
sendJson(['success' => false, 'error' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
$people = normalizePeopleList(readJsonFile('people.json'));
|
||||
requireActivePerson($people);
|
||||
|
||||
$mealId = isset($_GET['mealId']) ? trim((string) $_GET['mealId']) : '';
|
||||
if ($mealId === '') {
|
||||
sendJson(['success' => false, 'error' => 'mealId is required'], 400);
|
||||
}
|
||||
|
||||
$meals = migrateLegacyMealsList(normalizeMealsList(readJsonFile('meals.json')));
|
||||
$meal = findMealById($meals, $mealId);
|
||||
if ($meal === null) {
|
||||
sendJson(['success' => false, 'error' => 'Meal not found'], 404);
|
||||
}
|
||||
|
||||
$meal = normalizeMealRow($meal);
|
||||
$plan = normalizeMealPlan(readJsonFile('meal_plans.json'));
|
||||
$weekStart = $plan['weekStart'];
|
||||
$slots = $plan['slots'] ?? [];
|
||||
$pruned = mealDefaultEmptySlots();
|
||||
for ($i = 0; $i < 7; $i++) {
|
||||
$key = (string) $i;
|
||||
$day = $slots[$key] ?? [];
|
||||
if (!is_array($day)) {
|
||||
continue;
|
||||
}
|
||||
foreach (mealSlotTypes() as $mt) {
|
||||
$v = $day[$mt] ?? null;
|
||||
$pruned[$key][$mt] = (is_string($v) && $v === $mealId) ? $mealId : null;
|
||||
}
|
||||
}
|
||||
|
||||
sendJson([
|
||||
'success' => true,
|
||||
'version' => '1',
|
||||
'meals' => [$meal],
|
||||
'meal_plans' => [
|
||||
'weekStart' => $weekStart,
|
||||
'slots' => $pruned,
|
||||
],
|
||||
]);
|
||||
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/../includes/api_bootstrap.php';
|
||||
require_once __DIR__ . '/../includes/meal_helpers.php';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
sendJson(['success' => false, 'error' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
$people = normalizePeopleList(readJsonFile('people.json'));
|
||||
$actor = requireActivePerson($people);
|
||||
if (($actor['role'] ?? '') !== ROLE_HEAD || !isHohVerified()) {
|
||||
sendJson(['success' => false, 'error' => 'Only a verified Head of household can import recipes'], 403);
|
||||
}
|
||||
|
||||
$body = readJsonBody();
|
||||
$data = $body;
|
||||
if (isset($body['package']) && is_array($body['package'])) {
|
||||
$data = $body['package'];
|
||||
}
|
||||
|
||||
$incomingMeals = $data['meals'] ?? null;
|
||||
if (!is_array($incomingMeals) || !array_is_list($incomingMeals) || count($incomingMeals) === 0) {
|
||||
sendJson(['success' => false, 'error' => 'Invalid package: meals array required'], 400);
|
||||
}
|
||||
|
||||
$mealsUpserted = 0;
|
||||
$importedIds = [];
|
||||
$normalizedRows = [];
|
||||
|
||||
foreach ($incomingMeals as $row) {
|
||||
if (!is_array($row) || empty($row['id']) || !is_string($row['id'])) {
|
||||
sendJson(['success' => false, 'error' => 'Invalid meal row'], 400);
|
||||
}
|
||||
$norm = normalizeMealRow($row);
|
||||
if (trim((string) ($norm['title'] ?? '')) === '') {
|
||||
sendJson(['success' => false, 'error' => 'Each meal must have a title'], 400);
|
||||
}
|
||||
$mid = (string) ($norm['id'] ?? '');
|
||||
if ($mid === '') {
|
||||
sendJson(['success' => false, 'error' => 'Invalid meal id'], 400);
|
||||
}
|
||||
$importedIds[$mid] = true;
|
||||
$normalizedRows[] = $norm;
|
||||
}
|
||||
|
||||
$meals = migrateLegacyMealsList(normalizeMealsList(readJsonFile('meals.json')));
|
||||
|
||||
foreach ($normalizedRows as $norm) {
|
||||
$mid = (string) ($norm['id'] ?? '');
|
||||
$idx = findMealIndexById($meals, $mid);
|
||||
if ($idx !== null) {
|
||||
$meals[$idx] = $norm;
|
||||
} else {
|
||||
$meals[] = $norm;
|
||||
}
|
||||
$mealsUpserted++;
|
||||
}
|
||||
|
||||
if (!writeJsonFile('meals.json', $meals)) {
|
||||
sendJson(['success' => false, 'error' => 'Failed to save meals'], 500);
|
||||
}
|
||||
|
||||
$plan = normalizeMealPlan(readJsonFile('meal_plans.json'));
|
||||
$slotsApplied = 0;
|
||||
|
||||
$impPlan = $data['meal_plans'] ?? [];
|
||||
$impSlots = is_array($impPlan) && isset($impPlan['slots']) && is_array($impPlan['slots'])
|
||||
? $impPlan['slots']
|
||||
: [];
|
||||
|
||||
for ($i = 0; $i < 7; $i++) {
|
||||
$key = (string) $i;
|
||||
$day = $impSlots[$key] ?? $impSlots[$i] ?? [];
|
||||
if (!is_array($day)) {
|
||||
continue;
|
||||
}
|
||||
foreach (mealSlotTypes() as $mt) {
|
||||
$mid = $day[$mt] ?? null;
|
||||
if ($mid === null || $mid === '') {
|
||||
continue;
|
||||
}
|
||||
if (!is_string($mid)) {
|
||||
continue;
|
||||
}
|
||||
if (!isset($importedIds[$mid])) {
|
||||
continue;
|
||||
}
|
||||
$plan['slots'][$key][$mt] = $mid;
|
||||
$slotsApplied++;
|
||||
}
|
||||
}
|
||||
|
||||
if (!writeJsonFile('meal_plans.json', $plan)) {
|
||||
sendJson(['success' => false, 'error' => 'Failed to save meal plan'], 500);
|
||||
}
|
||||
|
||||
sendJson([
|
||||
'success' => true,
|
||||
'mealsUpserted' => $mealsUpserted,
|
||||
'slotsApplied' => $slotsApplied,
|
||||
]);
|
||||
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/../includes/api_bootstrap.php';
|
||||
require_once __DIR__ . '/../includes/meal_helpers.php';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
sendJson(['success' => false, 'error' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
$people = normalizePeopleList(readJsonFile('people.json'));
|
||||
requireActivePerson($people);
|
||||
|
||||
migrateLegacyGroceriesIfNeeded();
|
||||
ensureDefaultGroceryStore();
|
||||
$stores = normalizeStoresList(readJsonFile('stores.json'));
|
||||
|
||||
$body = readJsonBody();
|
||||
$mealId = isset($body['mealId']) ? trim((string) $body['mealId']) : '';
|
||||
$itemIndex = isset($body['itemIndex']) ? (int) $body['itemIndex'] : -1;
|
||||
$storeId = isset($body['storeId']) ? trim((string) $body['storeId']) : '';
|
||||
|
||||
if ($mealId === '') {
|
||||
sendJson(['success' => false, 'error' => 'mealId is required'], 400);
|
||||
}
|
||||
if ($itemIndex < 0) {
|
||||
sendJson(['success' => false, 'error' => 'itemIndex is required'], 400);
|
||||
}
|
||||
if (count($stores) === 0) {
|
||||
sendJson(['success' => false, 'error' => 'Add a grocery store first'], 400);
|
||||
}
|
||||
|
||||
$meals = migrateLegacyMealsList(normalizeMealsList(readJsonFile('meals.json')));
|
||||
$meal = findMealById($meals, $mealId);
|
||||
if ($meal === null) {
|
||||
sendJson(['success' => false, 'error' => 'Meal not found'], 404);
|
||||
}
|
||||
|
||||
$meal = normalizeMealRow($meal);
|
||||
$items = $meal['items'] ?? [];
|
||||
if (!is_array($items) || $itemIndex >= count($items)) {
|
||||
sendJson(['success' => false, 'error' => 'Invalid shopping item'], 400);
|
||||
}
|
||||
|
||||
$row = $items[$itemIndex];
|
||||
if (!is_array($row)) {
|
||||
sendJson(['success' => false, 'error' => 'Invalid shopping item'], 400);
|
||||
}
|
||||
|
||||
$name = trim((string) ($row['name'] ?? ''));
|
||||
if ($name === '') {
|
||||
sendJson(['success' => false, 'error' => 'Item has no name'], 400);
|
||||
}
|
||||
|
||||
$mealTitle = (string) ($meal['title'] ?? '');
|
||||
$desc = trim((string) ($row['description'] ?? ''));
|
||||
if ($desc === '') {
|
||||
$desc = $mealTitle !== '' ? 'From meal: ' . $mealTitle : 'From meal shopping list';
|
||||
}
|
||||
|
||||
if ($storeId === '' || findStoreById($stores, $storeId) === null) {
|
||||
$rowStore = trim((string) ($row['storeId'] ?? ''));
|
||||
if ($rowStore !== '' && findStoreById($stores, $rowStore) !== null) {
|
||||
$storeId = $rowStore;
|
||||
} else {
|
||||
$storeId = groceryFirstStoreId($stores);
|
||||
}
|
||||
}
|
||||
if ($storeId === '') {
|
||||
sendJson(['success' => false, 'error' => 'No valid store'], 400);
|
||||
}
|
||||
|
||||
$res = groceryAppendShoppingLine(
|
||||
$stores,
|
||||
$storeId,
|
||||
$name,
|
||||
$desc,
|
||||
trim((string) ($row['size'] ?? '')),
|
||||
trim((string) ($row['quantity'] ?? '1')) ?: '1',
|
||||
trim((string) ($row['price'] ?? '')),
|
||||
trim((string) ($row['image'] ?? '')),
|
||||
'meal_plan',
|
||||
0,
|
||||
$mealId,
|
||||
$mealTitle !== '' ? $mealTitle : null
|
||||
);
|
||||
|
||||
if (!$res['ok']) {
|
||||
sendJson(['success' => false, 'error' => $res['error'] ?? 'Failed'], 400);
|
||||
}
|
||||
|
||||
sendJson(['success' => true, 'item' => $res['item']]);
|
||||
Reference in New Issue
Block a user