Base Directory:
/home/ecedu/public_html/new/api
'error', 'message' => 'Invalid JSON input: ' . json_last_error_msg()]);
exit;
}
$deviceId = $input['deviceId'] ?? '';
$authorization = $input['authorization'] ?? '';
$lang = $input['lang'] ?? 'ar';
$action = $input['action'] ?? 'read';
$auth = new DeviceAuth();
if (!$auth->verifyDevice($deviceId, $authorization)) {
http_response_code(401);
echo json_encode(['status' => 'error', 'message' => 'Unauthorized']);
exit;
}
$news = new NewsModel();
try {
if ($action === 'read') {
$limit = 5;
$newsByType = [];
$mostReadNews = [];
$newsTypes = $news->getAllTableNames();
foreach ($newsTypes as $typeId => $tableName) {
$newsByType["newsType$typeId"] = $news->getNewsByType($typeId, $limit);
}
$newsMap = [];
foreach ($newsByType as $typeKey => $newsList) {
foreach ($newsList as $newsItem) {
$key = $newsItem['news_id'] . '-' . str_replace('newsType', '', $typeKey);
$newsMap[$key] = $newsItem;
}
}
$readView = $news->getViewCount($limit);
foreach ($readView as $row) {
$key = trim((string)$row['news_id']) . '-' . trim((string)$row['table_id']);
if (array_key_exists($key, $newsMap)) {
$item = $newsMap[$key];
$item['num_view'] = $row['total_views'] ?? $row['num_view'] ?? 0;
$item['table_id'] = $row['table_id'];
$mostReadNews[] = $item;
}
}
$data = [
'sliderNews' => array_slice($newsByType['newsType2'] ?? [], 0, $limit),
'lastNews' => array_slice($newsByType['newsType2'] ?? [], 0, $limit),
'mostReadNews' => $mostReadNews,
'editorialData' => $news->getBasic('dbs_editorial_board'),
'contactusData' => $news->getBasic('dbs_contact_us'),
'socialData' => $news->listSocials(),
'imageData' => $news->listImages(),
'adsData' => $news->listAds(),
];
for ($n = 1; $n <= 18; $n++) {
$data["newsType$n"] = $newsByType["newsType$n"] ?? [];
}
echo json_encode(['status' => 'ok', 'data' => $data], JSON_UNESCAPED_UNICODE);
exit;
} else {
throw new Exception('Unsupported action');
}
} catch (Exception $e) {
http_response_code(500);
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
exit;
}