<?php
namespace App\Controller;
use App\Constant\StripeSubscription;
use App\Entity\ConfigModel;
use App\Entity\ConfigUi;
use App\Entity\Configurator3D;
use App\Entity\ConfiguratorUI;
use App\Entity\ConfigViewer;
use App\Services\Formatting;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/api")
*/
class ApiController extends AbstractController
{
private $doctrine;
public function __construct(ManagerRegistry $doctrine) {
$this->doctrine = $doctrine;
}
/**
* @Route("/model-viewer/{modelConfigUuid}/{viewerConfigUuid}/{uiConfigUuid}", name="model_viewer_show")
*/
public function apiModelViewerShow($modelConfigUuid, $viewerConfigUuid, $uiConfigUuid,
StripeSubscription $stripeSubscription,
Formatting $formatting)
{
$modelConfig = $this->doctrine->getRepository(ConfigModel::class)->findOneBy(['uuid' => $modelConfigUuid]);
if (!$modelConfig){
return $this->render('api/model-viewer.html.twig', [
'modelConfig' => [],
'viewerConfig' => [],
'uiConfig' => [],
'domains' => [],
'hideLogo' => false,
'expired' => false,
'error' => "Model not found",
'title' => "Arty 3D Viewer - Model not found"
]);
}
$viewerConfigOutput = [];
if (strtolower($viewerConfigUuid) != 'default'){
$viewerConfig = $this->doctrine->getRepository(ConfigViewer::class)->findOneBy(['uuid' => $viewerConfigUuid]);
if ($viewerConfig) {
$viewerConfigDecoded = json_decode($viewerConfig->getConfig(), true);
$viewerConfigOutput = $viewerConfigDecoded['config'];
}
}
$uiConfigOutput = [];
if (strtolower($uiConfigUuid) != 'default'){
$uiConfig = $this->doctrine->getRepository(ConfigUi::class)->findOneBy(['uuid' => $uiConfigUuid]);
if ($uiConfig) {
$uiConfigDecoded = json_decode($uiConfig->getConfig(), true);
$uiConfigOutput = $uiConfigDecoded['config'];
}
}
$domains = $modelConfig->getProject()->getAccount()->getDomains();
if (!empty($domains)){
$domains = $formatting->formatDomains($domains);
}
$expired = false;
$subscription = $modelConfig->getProject()->getAccount()->getSubscription();
if ($subscription->getService()->getCode() != 'FREE'){
$expiryDate = $subscription->getExpiryDate();
$pendingExpirationDays = $stripeSubscription->getSubscriptionExpirationPendingDays();
$dateNow = (new \DateTime("now"))->modify('-'.$pendingExpirationDays.' day');
$expired = ($dateNow >= $expiryDate);
}
$config = json_decode($modelConfig->getConfig(), true);
$modelThumb = $config['config']['modelScreenshot'];
$modelName = $config['config']['name'];
return $this->render('api/model-viewer.html.twig', [
'title' => 'Arty 3D Viewer | ' . ucwords($modelName),
'modelConfig' => json_encode($config['config']),
'modelThumb' => $modelThumb,
'viewerConfig' => json_encode($viewerConfigOutput),
'uiConfig' => json_encode($uiConfigOutput),
'domains' => json_encode($domains),
'hideLogo' => $modelConfig->getProject()->getAccount()->getSubscription()->getService()->isHideLogo(),
'expired' => $expired
]);
}
/**
* @Route("/3d-configurator/{configUuid}/{presetUuid}", name="3d_configurator_show")
*/
public function apiConfiguratorShow($configUuid, $presetUuid)
{
$configurator = $this->doctrine->getRepository(Configurator3D::class)->findOneBy(['uuid' => $configUuid]);
$preset = $this->doctrine->getRepository(ConfiguratorUI::class)->findOneBy(['uuid' => $presetUuid]);
if (!$configurator) return new Response('Configurator not found');
$preview = false;
if (!$preset){
$preview = true;
$configObject = [];
$uiObject = [];
} else {
$configObject = $preset->getConfigObject();
$uiObject = $preset->getUiObject();
}
$modelConfig = $configurator->getModelConfig();
$description = $configurator->getDescription();
return $this->render('api/3d-configurator.html.twig', [
'preview' => $preview,
'modelConfig' => $modelConfig,
'configObject' => json_encode($configObject),
'uiObject' => json_encode($uiObject),
'description' => json_encode($description),
'configuratorName' => $configurator->getName(),
'configurationUuid' => $configurator->getUuid()
]);
}
}