query($query); if (!$result) { echo "MySQL Error: " . $conn->error . "\n"; } $data = []; while ($row = $result->fetch_assoc()) { $data[] = $row; } appresponse($data); } function handleAppCategory($conn, $identifier, $offset, $rowsPerPage, $fields, $lang) { if (!$identifier) { response(400, "Identifier Not Found"); return; } // Fetch the category based on the identifier and language $query = "SELECT cat.ID, COALESCE(category_translations.translated_name, cat.Name) AS Name FROM ncategories AS cat LEFT JOIN category_translations ON category_translations.category_id = cat.ID AND category_translations.language_code = ? WHERE cat.ID = ?"; $stmt = $conn->prepare($query); if (!$stmt) { echo "MySQL Prepare Error: " . $conn->error . "\n"; return; } $stmt->bind_param("si", $lang, $identifier); $stmt->execute(); if ($stmt->errno) { echo "MySQL Execute Error: " . $stmt->error . "\n"; } $result = $stmt->get_result(); if (!$result) { echo "MySQL Error: " . $conn->error . "\n"; } if ($row = $result->fetch_assoc()) { $data['id'] = $row['ID']; $data['title'] = $row['Name']; $data['headerDesc'] = $row['sData']; // Fetch articles based on the category ID (mCat) and language $data['articles'] = fetchNews($conn, $identifier, $offset, $rowsPerPage, $fields, $lang); $categoriesQuery = " SELECT c.ID, COALESCE(ct.translated_name, c.Name) as Name, c.link FROM ncategories c LEFT JOIN category_translations ct ON c.ID = ct.category_id AND ct.language_code = ? WHERE c.cmsfilter = 1 ORDER BY corder ASC "; $categoriesStmt = $conn->prepare($categoriesQuery); if (!$categoriesStmt) { echo "MySQL Prepare Error for categories: " . $conn->error . "\n"; return; } $categoriesStmt->bind_param("s", $lang); $categoriesStmt->execute(); if ($categoriesStmt->errno) { echo "MySQL Execute Error for categories: " . $categoriesStmt->error . "\n"; return; } $categoriesResult = $categoriesStmt->get_result(); $categories = []; while ($row = $categoriesResult->fetch_assoc()) { if ($row['link'] == 1) { // Fetch the single article ID for this category in the specified language $articleQuery = " SELECT ID FROM news WHERE mCat = ? AND lang = ? LIMIT 1 "; $articleStmt = $conn->prepare($articleQuery); if (!$articleStmt) { echo "MySQL Prepare Error for article: " . $conn->error . "\n"; return; } $articleStmt->bind_param("is", $row['ID'], $lang); $articleStmt->execute(); if ($articleStmt->errno) { echo "MySQL Execute Error for article: " . $articleStmt->error . "\n"; return; } $articleResult = $articleStmt->get_result(); if ($articleRow = $articleResult->fetch_assoc()) { // Add the article ID to the category row $row['articleid'] = $articleRow['ID']; } $articleStmt->close(); } $categories[] = $row; } $data['categories']= $categories; appresponse($data); } else { response(400, "Category Not Found"); } } function handleAppVideo($conn, $offset, $rowsPerPage, $fields, $lang) { $query = "SELECT " . implode(", ", $fields) . " FROM news LEFT JOIN ncategories ON ncategories.ID = news.mCat LEFT JOIN category_translations ON category_translations.category_id = news.mCat AND category_translations.language_code = ? WHERE news.mStatus=1 AND news.mobilevideo != '' AND news.lang = ? ORDER BY news.ID DESC LIMIT ?, ?"; $stmt = $conn->prepare($query); if (!$stmt) { echo "MySQL Prepare Error: " . $conn->error . "\n"; return; } $stmt->bind_param("ssii", $lang, $lang, $offset, $rowsPerPage); $stmt->execute(); if ($stmt->errno) { echo "MySQL Execute Error: " . $stmt->error . "\n"; } $result = $stmt->get_result(); if (!$result) { echo "MySQL Error: " . $conn->error . "\n"; } $data['videos'] = []; while ($row = $result->fetch_assoc()) { $row['picture'] = $row['picPath'] . $row['picture']; if ($row['mobilevideo']) { $row['mobilevideo'] = "https://cdn.alarab.com/h264/" . $row['mobilevideo'] . "_,48,24,0p.mp4.urlset/playlist.m3u8"; } $data['videos'][] = $row; } appresponse($data); } function handleAppArticle($conn, $identifier, $fields, $lang) { // Fetch the main article first to get its mCat $query = "SELECT news.ID, news.pdate, news.title, news.fData,news.mStatus, news.mCat, news.picture, news.userInserted, news.udate, news.mobilevideo, category_translations.translated_name AS category FROM news LEFT JOIN ncategories ON ncategories.ID = news.mCat LEFT JOIN category_translations ON category_translations.category_id = news.mCat AND category_translations.language_code = ? WHERE news.ID = ?"; $stmt = $conn->prepare($query); if (!$stmt) { echo "MySQL Prepare Error: " . $conn->error . "\n"; return; } // Corrected to bind the two parameters (lang and identifier) $stmt->bind_param("si", $lang, $identifier); $stmt->execute(); if ($stmt->errno) { echo "MySQL Execute Error: " . $stmt->error . "\n"; return; } $result = $stmt->get_result(); if (!$result) { echo "MySQL Error: " . $conn->error . "\n"; return; } if ($row = $result->fetch_assoc()) { $mCat = $row['mCat']; // Capture the mCat of the selected article $row['picture'] = "http://comark-light.localhost/". $row['picture']; $data['article'] = $row; // Fetch related images $data['article']['gallery'] = fetchGallery($conn, $identifier); // Fetch related news using the mCat of the current article $data['article']['related'] = fetchRelatedNews($conn, $mCat, $identifier, $lang); $categoriesQuery = " SELECT c.ID, COALESCE(ct.translated_name, c.Name) as Name, c.link FROM ncategories c LEFT JOIN category_translations ct ON c.ID = ct.category_id AND ct.language_code = ? WHERE c.cmsfilter = 1 ORDER BY corder ASC "; $categoriesStmt = $conn->prepare($categoriesQuery); if (!$categoriesStmt) { echo "MySQL Prepare Error for categories: " . $conn->error . "\n"; return; } $categoriesStmt->bind_param("s", $lang); $categoriesStmt->execute(); if ($categoriesStmt->errno) { echo "MySQL Execute Error for categories: " . $categoriesStmt->error . "\n"; return; } $categoriesResult = $categoriesStmt->get_result(); $categories = []; while ($row = $categoriesResult->fetch_assoc()) { if ($row['link'] == 1) { // Fetch the single article ID for this category in the specified language $articleQuery = " SELECT ID FROM news WHERE mCat = ? AND lang = ? LIMIT 1 "; $articleStmt = $conn->prepare($articleQuery); if (!$articleStmt) { echo "MySQL Prepare Error for article: " . $conn->error . "\n"; return; } $articleStmt->bind_param("is", $row['ID'], $lang); $articleStmt->execute(); if ($articleStmt->errno) { echo "MySQL Execute Error for article: " . $articleStmt->error . "\n"; return; } $articleResult = $articleStmt->get_result(); if ($articleRow = $articleResult->fetch_assoc()) { // Add the article ID to the category row $row['articleid'] = $articleRow['ID']; } $articleStmt->close(); } $categories[] = $row; } $data['categories']= $categories; appresponse($data); } else { response(400, "Article Not Found"); } } // Adjust fetchRelatedNews to use mCat instead of smallcat function fetchRelatedNews($conn, $mCat, $newsId, $lang) { $query = "SELECT news.ID, news.title, news.picture FROM news LEFT JOIN ncategories ON ncategories.ID = news.mCat LEFT JOIN category_translations ON category_translations.category_id = news.mCat AND category_translations.language_code = ? WHERE news.ID < ? AND news.mCat = ? AND news.lang = ? ORDER BY ID DESC LIMIT 5"; $stmt = $conn->prepare($query); if (!$stmt) { echo "MySQL Prepare Error: " . $conn->error . "\n"; return; } $stmt->bind_param("siii", $lang, $newsId, $mCat, $lang); $stmt->execute(); if ($stmt->errno) { echo "MySQL Execute Error: " . $stmt->error . "\n"; } $result = $stmt->get_result(); if (!$result) { echo "MySQL Error: " . $conn->error . "\n"; } $related = []; while ($row = $result->fetch_assoc()) { $row['picture'] = "http://comark-light.localhost/". $row['picture'] . "?aspect_ratio=1:1&width=500"; $related[] = $row; } return $related; } function handleAppCatBig($conn, $identifier, $fields, $lang) { if (!$identifier) { response(400, "Identifier Not Found"); return; } $query = "SELECT " . implode(", ", $fields) . " FROM news LEFT JOIN ncategories ON ncategories.ID = news.mCat LEFT JOIN category_translations ON category_translations.category_id = news.mCat AND category_translations.language_code = ? WHERE news.mStatus=1 AND news.mCat = ? AND news.lang = ? ORDER BY news.ID DESC LIMIT ?, ?"; $stmt = $conn->prepare($query); if (!$stmt) { echo "MySQL Prepare Error: " . $conn->error . "\n"; return; } $stmt->bind_param("sissi", $lang, $identifier, $lang, $offset, $rowsPerPage); $stmt->execute(); if ($stmt->errno) { echo "MySQL Execute Error: " . $stmt->error . "\n"; } $result = $stmt->get_result(); if (!$result) { echo "MySQL Error: " . $conn->error . "\n"; } $data['mainnews'] = []; while ($row = $result->fetch_assoc()) { $row['picture'] = $row['picPath'] . $row['picture']. "?aspect_ratio=1:1&width=500"; $data['mainnews'][] = $row; } appresponse($data); } function handleAppCityList($conn) { $query = "SELECT id, erea FROM `erea`"; $result = $conn->query($query); if (!$result) { echo "MySQL Error: " . $conn->error . "\n"; } $data['citylist'] = []; while ($row = $result->fetch_assoc()) { $data['citylist'][] = $row; } appresponse($data); } function handleAppNavigation($conn) { $query = "SELECT * FROM navigation ORDER BY display_order ASC"; $result = $conn->query($query); if (!$result) { echo "MySQL Error: " . $conn->error . "\n"; } $data['Menu'] = []; while ($row = $result->fetch_assoc()) { $data['Menu'][] = $row; } appresponse($data); } function handleAppHome($conn, $identifier, $fields, $lang) { // Fetch categories for the specific language $categoriesQuery = " SELECT c.ID, COALESCE(ct.translated_name, c.Name) as Name, c.link FROM ncategories c LEFT JOIN category_translations ct ON c.ID = ct.category_id AND ct.language_code = ? WHERE c.cmsfilter = 1 ORDER BY corder ASC "; $categoriesStmt = $conn->prepare($categoriesQuery); if (!$categoriesStmt) { echo "MySQL Prepare Error for categories: " . $conn->error . "\n"; return; } $categoriesStmt->bind_param("s", $lang); $categoriesStmt->execute(); if ($categoriesStmt->errno) { echo "MySQL Execute Error for categories: " . $categoriesStmt->error . "\n"; return; } $categoriesResult = $categoriesStmt->get_result(); $categories = []; while ($row = $categoriesResult->fetch_assoc()) { if ($row['link'] == 1) { // Fetch the single article ID for this category in the specified language $articleQuery = " SELECT ID, fData FROM news WHERE mCat = ? AND lang = ? LIMIT 1 "; $articleStmt = $conn->prepare($articleQuery); if (!$articleStmt) { echo "MySQL Prepare Error for article: " . $conn->error . "\n"; return; } $articleStmt->bind_param("is", $row['ID'], $lang); $articleStmt->execute(); if ($articleStmt->errno) { echo "MySQL Execute Error for article: " . $articleStmt->error . "\n"; return; } $articleResult = $articleStmt->get_result(); if ($articleRow = $articleResult->fetch_assoc()) { // Add the article ID to the category row $row['articleid'] = $articleRow['ID']; if($row['ID'] == 46 ){ $aboutustext= $articleRow['fData']; $data['aboutus'] = $aboutustext; } } $articleStmt->close(); } $categories[] = $row; } $categoriesStmt->close(); // Fetch the last 3 articles for the specific language $articlesQuery = " SELECT ID, title, pdate, picture FROM news WHERE lang = ? AND mCat=45 ORDER BY ID DESC "; $articlesStmt = $conn->prepare($articlesQuery); if (!$articlesStmt) { echo "MySQL Prepare Error for articles: " . $conn->error . "\n"; return; } $articlesStmt->bind_param("s", $lang); $articlesStmt->execute(); if ($articlesStmt->errno) { echo "MySQL Execute Error for articles: " . $articlesStmt->error . "\n"; return; } $articlesResult = $articlesStmt->get_result(); $articles = []; while ($row = $articlesResult->fetch_assoc()) { $row['picture'] = "https://www.bayt.org.il/". $row['picture']. "?class=thumb"; $articles[] = $row; } $articlesStmt->close(); // Prepare the response $response = [ 'categories' => $categories, 'articles' => $articles, 'aboutus' => $aboutustext ]; // Send the response appresponse($response); } // Fetch news from the database function fetchNews($conn, $mCat, $offset, $rowsPerPage, $fields, $lang) { $query = "SELECT " . implode(", ", $fields) . " FROM news LEFT JOIN ncategories ON ncategories.ID = news.mCat LEFT JOIN category_translations ON category_translations.category_id = ncategories.ID AND category_translations.language_code = ? WHERE news.mCat = ? AND news.lang = ? ORDER BY news.ID ASC LIMIT ?, ?"; $stmt = $conn->prepare($query); if (!$stmt) { echo "MySQL Prepare Error: " . $conn->error . "\n"; return []; } // Bind all five parameters $stmt->bind_param("sisii", $lang, $mCat, $lang, $offset, $rowsPerPage); $stmt->execute(); if ($stmt->errno) { echo "MySQL Execute Error: " . $stmt->error . "\n"; return []; } $result = $stmt->get_result(); if (!$result) { echo "MySQL Error: " . $conn->error . "\n"; return []; } $articles = []; while ($row = $result->fetch_assoc()) { $row['picture'] = $row['picPath'] . $row['picture']; $articles[] = $row; } return $articles; } // Fetch categories from the database function fetchCategories($conn, $lang) { $query = "SELECT ncategories.ID, category_translations.translated_name AS Name FROM ncategories LEFT JOIN category_translations ON category_translations.category_id = ncategories.ID AND category_translations.language_code = ? WHERE ncategories.cmsfilter=1 ORDER BY ncategories.ID ASC"; $stmt = $conn->prepare($query); if (!$stmt) { echo "MySQL Prepare Error: " . $conn->error . "\n"; return; } $stmt->bind_param("s", $lang); $stmt->execute(); if ($stmt->errno) { echo "MySQL Execute Error: " . $stmt->error . "\n"; } $result = $stmt->get_result(); if (!$result) { echo "MySQL Error: " . $conn->error . "\n"; } $categories = []; while ($row = $result->fetch_assoc()) { $categories[] = $row; } return $categories; } // Fetch gallery images from the database function fetchGallery($conn, $newsId) { $query = "SELECT * FROM images WHERE newsid = ?"; $stmt = $conn->prepare($query); if (!$stmt) { echo "MySQL Prepare Error: " . $conn->error . "\n"; return; } $stmt->bind_param("i", $newsId); $stmt->execute(); if ($stmt->errno) { echo "MySQL Execute Error: " . $stmt->error . "\n"; } $result = $stmt->get_result(); if (!$result) { echo "MySQL Error: " . $conn->error . "\n"; } $gallery = []; while ($pic = $result->fetch_assoc()) { $gallery[] = [ "src" => "https://images.alarab.com/" . $pic['file_name'], "width" => $pic['width'], "height" => $pic['height'], "title" => $pic['title'] ]; } return $gallery; } // JSON response functions function response($status, $message, $data = null) { echo json_encode([ 'status' => $status, 'message' => $message, 'data' => $data ], JSON_UNESCAPED_UNICODE); exit; } function appresponse($data) { response(200, "Success", $data); } // Handle the request based on the action if (!empty($action)) { handleRequest($conn, $action, $identifier, $offset, $rowsPerPage, $defaultFields, $lang); } else { response(400, "Action Not Found"); } mysqli_close($conn);