<?php
namespace App\Controller;
use Pimcore\Log\ApplicationLogger;
use App\Model\GenericWebsiteClassModel;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
use Pimcore\Model\DataObject;
use App\Service\PublicUserPermissionService;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* Website controller is responsible for handling various actions related to website.
*
* @Route("/api/ncm/public-portal", name="api-ncm-public-portal-")
*/
class PublicPortalPagesController extends AbstractController
{
private $genericWebsiteClassModel;
public function __construct(protected TranslatorInterface $translator, private ApplicationLogger $logger, private PublicUserPermissionService $publicUserPermissionService)
{
$this->genericWebsiteClassModel = new GenericWebsiteClassModel();
$this->publicUserPermissionService = $publicUserPermissionService;
}
/**
* @Route("/home-page", name="home-page", methods={"POST"})
*/
public function homePage(Request $request)
{
try {
$response = $this->publicUserPermissionService->isAuthorized($request, $this->translator);
if ($response['success'] !== true) {
return $this->json($response);
}
$response = [];
$homePage = new DataObject\HomePage\Listing();
$classDefinition = $homePage->current()->getClass()->getFieldDefinitions();
$homePageObj = $homePage->current();
foreach ($classDefinition as $value) {
$response = array_merge($response, $this->genericWebsiteClassModel->getSwitchCaseCondition($homePageObj, $value));
}
return $this->json(['success' => true, 'data' => $response]);
} catch (\Exception $ex) {
$this->logger->error($ex->getMessage());
return $this->json(['success' => false, 'message' => $ex->getMessage()]);
}
}
/**
* @Route("/services-page", name="services-page", methods={"POST"})
*/
public function servicesPage(Request $request)
{
try {
$response = $this->publicUserPermissionService->isAuthorized($request, $this->translator);
if ($response['success'] !== true) {
return $this->json($response);
}
$response = [];
$servicePage = new DataObject\ServicesPage\Listing();
$servicePage->current();
$classDefinition = $servicePage->current()->getClass()->getFieldDefinitions();
$servicesPageObj = $servicePage->current();
foreach ($classDefinition as $value) {
$response = array_merge($response, $this->genericWebsiteClassModel->getSwitchCaseCondition($servicesPageObj, $value));
}
return $this->json(['success' => true, 'data' => $response]);
} catch (\Exception $ex) {
$this->logger->error($ex->getMessage());
return $this->json(['success' => false, 'message' => $ex->getMessage()]);
}
}
/**
* @Route("/services-detail", name="services-detail", methods={"POST"})
*/
public function servicesDetail(Request $request)
{
try {
$response = $this->publicUserPermissionService->isAuthorized($request, $this->translator);
if ($response['success'] !== true) {
return $this->json($response);
}
$response = [];
$param = json_decode($request->getContent(), true);
$viewService = \Pimcore\Model\DataObject\ServicesListing::getById($param['id'], true);
if (!empty($viewService)) {
$classDefinition = $viewService->getClass()->getFieldDefinitions();
foreach ($classDefinition as $value) {
$response = array_merge($response, $this->genericWebsiteClassModel->getSwitchCaseCondition($viewService, $value));
}
} else {
throw new \Exception('Service not found.');
}
// return $this->json(['success' => true, 'data' => $response]);
return $this->json(['success' => true, 'data' => $response]);
} catch (\Exception $ex) {
$this->logger->error($ex->getMessage());
return $this->json(['success' => false, 'message' => $ex->getMessage()]);
}
}
/**
* @Route("/about-us-page", name="about-us-page", methods={"POST"})
*/
public function aboutUsPage(Request $request)
{
try {
$response = $this->publicUserPermissionService->isAuthorized($request, $this->translator);
if ($response['success'] !== true) {
return $this->json($response);
}
$response = [];
$aboutPage = new DataObject\AboutUsPage\Listing();
$aboutPage->current();
$classDefinition = $aboutPage->current()->getClass()->getFieldDefinitions();
$aboutPageObj = $aboutPage->current();
foreach ($classDefinition as $value) {
$response = array_merge($response, $this->genericWebsiteClassModel->getSwitchCaseCondition($aboutPageObj, $value));
}
return $this->json(['success' => true, 'data' => $response]);
} catch (\Exception $ex) {
$this->logger->error($ex->getMessage());
return $this->json(['success' => false, 'message' => $ex->getMessage()]);
}
}
/**
* @Route("/contact-us-page", name="contact-us-page", methods={"POST"})
*/
public function contactUsPage(Request $request)
{
try {
$response = $this->publicUserPermissionService->isAuthorized($request, $this->translator);
if ($response['success'] !== true) {
return $this->json($response);
}
$response = [];
$contactUsPage = new DataObject\ContactUsPage\Listing();
$contactUsPage->current();
$classDefinition = $contactUsPage->current()->getClass()->getFieldDefinitions();
$contactPageObj = $contactUsPage->current();
foreach ($classDefinition as $value) {
$response = array_merge($response, $this->genericWebsiteClassModel->getSwitchCaseCondition($contactPageObj, $value));
}
return $this->json(['success' => true, 'data' => $response]);
} catch (\Throwable $th) {
//throw $th;
}
}
/**
* @Route("/news-page", name="news-page", methods={"POST"})
*/
public function newsPage(Request $request)
{
try {
$response = $this->publicUserPermissionService->isAuthorized($request, $this->translator);
if ($response['success'] !== true) {
return $this->json($response);
}
$response = [];
$newsPage = new DataObject\NewsPage\Listing();
$newsPage->current();
$classDefinition = $newsPage->current()->getClass()->getFieldDefinitions();
$newsPageObj = $newsPage->current();
foreach ($classDefinition as $value) {
$response = array_merge($response, $this->genericWebsiteClassModel->getSwitchCaseCondition($newsPageObj, $value));
}
return $this->json(['success' => true, 'data' => $response]);
} catch (\Exception $ex) {
$this->logger->error($ex->getMessage());
return $this->json(['success' => false, 'message' => $ex->getMessage()]);
}
}
/**
* @Route("/today-weather-page", name="today-weather-page", methods={"POST"})
*/
public function todayWeatherPage(Request $request)
{
try {
$response = $this->publicUserPermissionService->isAuthorized($request, $this->translator);
if ($response['success'] !== true) {
return $this->json($response);
}
$response = [];
$tWeatherPage = new DataObject\TodaysWeather\Listing();
$tWeatherPage->current();
$classDefinition = $tWeatherPage->current()->getClass()->getFieldDefinitions();
$tWeatherPageObj = $tWeatherPage->current();
foreach ($classDefinition as $value) {
$response = array_merge($response, $this->genericWebsiteClassModel->getSwitchCaseCondition($tWeatherPageObj, $value));
}
return $this->json(['success' => true, 'data' => $response]);
} catch (\Exception $ex) {
$this->logger->error($ex->getMessage());
return $this->json(['success' => false, 'message' => $ex->getMessage()]);
}
}
/**
* @Route("/local-weather-page", name="local-weather-page", methods={"POST"})
*/
public function localWeatherPage(Request $request)
{
try {
$response = $this->publicUserPermissionService->isAuthorized($request, $this->translator);
if ($response['success'] !== true) {
return $this->json($response);
}
$response = [];
$localWeatherPage = new DataObject\LocalWeatherPage\Listing();
$localWeatherPage->current();
$classDefinition = $localWeatherPage->current()->getClass()->getFieldDefinitions();
$localWeatherPageObj = $localWeatherPage->current();
foreach ($classDefinition as $value) {
$response = array_merge($response, $this->genericWebsiteClassModel->getSwitchCaseCondition($localWeatherPageObj, $value));
}
return $this->json(['success' => true, 'data' => $response]);
} catch (\Exception $ex) {
$this->logger->error($ex->getMessage());
return $this->json(['success' => false, 'message' => $ex->getMessage()]);
}
}
/**
* @Route("/holy-sites-page", name="holy-sites-page", methods={"POST"})
*/
public function holySitesPage(Request $request)
{
try {
$response = $this->publicUserPermissionService->isAuthorized($request, $this->translator);
if ($response['success'] !== true) {
return $this->json($response);
}
$response = [];
$holySitesPage = new DataObject\HolySitesPage\Listing();
$holySitesPage->current();
$classDefinition = $holySitesPage->current()->getClass()->getFieldDefinitions();
$holySitesPageObj = $holySitesPage->current();
foreach ($classDefinition as $value) {
$response = array_merge($response, $this->genericWebsiteClassModel->getSwitchCaseCondition($holySitesPageObj, $value));
}
return $this->json(['success' => true, 'data' => $response]);
} catch (\Exception $ex) {
$this->logger->error($ex->getMessage());
return $this->json(['success' => false, 'message' => $ex->getMessage()]);
}
}
/**
* @Route("/marine-outlook-page", name="marine-outlook-page", methods={"POST"})
*/
public function marineOutlookPage(Request $request)
{
try {
$response = $this->publicUserPermissionService->isAuthorized($request, $this->translator);
if ($response['success'] !== true) {
return $this->json($response);
}
$response = [];
$marineOutlookPage = new DataObject\MarineOutlook\Listing();
$marineOutlookPage->current();
$classDefinition = $marineOutlookPage->current()->getClass()->getFieldDefinitions();
$marineOutlookPageObj = $marineOutlookPage->current();
foreach ($classDefinition as $value) {
$response = array_merge($response, $this->genericWebsiteClassModel->getSwitchCaseCondition($marineOutlookPageObj, $value));
}
return $this->json(['success' => true, 'data' => $response]);
} catch (\Exception $ex) {
$this->logger->error($ex->getMessage());
return $this->json(['success' => false, 'message' => $ex->getMessage()]);
}
}
/**
* @Route("/weather-warning-page", name="weather-warning-page", methods={"POST"})
*/
public function weatherWarningPage(Request $request)
{
try {
$response = $this->publicUserPermissionService->isAuthorized($request, $this->translator);
if ($response['success'] !== true) {
return $this->json($response);
}
$response = [];
$weatherWarningPage = new DataObject\WeatherWarningPage\Listing();
$weatherWarningPage->current();
$classDefinition = $weatherWarningPage->current()->getClass()->getFieldDefinitions();
$weatherWarningPageObj = $weatherWarningPage->current();
foreach ($classDefinition as $value) {
$response = array_merge($response, $this->genericWebsiteClassModel->getSwitchCaseCondition($weatherWarningPageObj, $value));
}
return $this->json(['success' => true, 'data' => $response]);
} catch (\Exception $ex) {
$this->logger->error($ex->getMessage());
return $this->json(['success' => false, 'message' => $ex->getMessage()]);
}
}
/**
* @Route("/climate-kingdom-page", name="climate-kingdom-page", methods={"POST"})
*/
public function climateKingdomPage(Request $request)
{
try {
$response = $this->publicUserPermissionService->isAuthorized($request, $this->translator);
if ($response['success'] !== true) {
return $this->json($response);
}
$response = [];
$climateKingdomPage = new DataObject\ClimateKingdomPage\Listing();
$climateKingdomPage->current();
$classDefinition = $climateKingdomPage->current()->getClass()->getFieldDefinitions();
$climateKingdomPageObj = $climateKingdomPage->current();
foreach ($classDefinition as $value) {
$response = array_merge($response, $this->genericWebsiteClassModel->getSwitchCaseCondition($climateKingdomPageObj, $value));
}
return $this->json(['success' => true, 'data' => $response]);
} catch (\Exception $ex) {
$this->logger->error($ex->getMessage());
return $this->json(['success' => false, 'message' => $ex->getMessage()]);
}
}
/**
* @Route("/translations", name="translations-all", methods={"GET"})
*/
public function allTranslations(Request $request)
{
try {
$locales = ['en', 'ar']; // Define the locales you want to fetch
$response = [];
$db = \Pimcore\Db::get();
foreach ($locales as $lang) {
$selectedLocalities = $db->fetchAllAssociative("
SELECT * FROM translations_messages
WHERE `key` LIKE 'frontend_%'
AND `language` = '$lang'
ORDER BY `creationDate` ASC
");
$localeResponse = [];
foreach ($selectedLocalities as $value) {
$keyParts = explode('_', $value['key'], 3);
$category = $keyParts[1];
$key = $keyParts[2];
if (!isset($localeResponse[$category])) {
$localeResponse[$category] = [];
}
$localeResponse[$category][$key] = $value['text'];
}
// Add the locale's response to the main response array
$response[$lang] = $localeResponse;
}
return $this->json($response);
} catch (\Exception $ex) {
$this->logger->error($ex->getMessage());
return $this->json(['success' => false, 'message' => $ex->getMessage()]);
}
}
/**
* @Route("/translations/{locale}", name="translations", methods={"GET"})
*/
public function frontendDashboardTranslation(Request $request)
{
try {
$lang = $request->get('locale', 'en');
$response = [];
$db = \Pimcore\Db::get();
$selectedLocalities = $db->fetchAllAssociative("
SELECT * FROM translations_messages
WHERE `key` LIKE 'frontend_%'
AND `language` = '$lang'
ORDER BY `creationDate` ASC
");
foreach ($selectedLocalities as $value) {
$keyParts = explode('_', $value['key'], 3);
$category = $keyParts[1];
$key = $keyParts[2];
if (!isset($response[$category])) {
$response[$category] = [];
}
$response[$category][$key] = $value['text'];
}
return $this->json($response);
} catch (\Exception $ex) {
$this->logger->error($ex->getMessage());
return $this->json(['success' => false, 'message' => $ex->getMessage()]);
}
}
}