connect new frontend to backend
This commit is contained in:
@@ -8,9 +8,11 @@ use Symfony\Component\Routing\Annotation\Route;
|
||||
|
||||
class IndexController extends AbstractController
|
||||
{
|
||||
#[Route(path: '/', name: 'homepage')]
|
||||
public function index() : Response
|
||||
#[Route(path: '/{hash}', name: 'homepage', defaults: ['hash'=>null])]
|
||||
public function index($hash) : Response
|
||||
{
|
||||
return $this->render("index/index.html.twig");
|
||||
return $this->render("index/index.html.twig",[
|
||||
'currentHash'=>$hash,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use _PHPStan_5a70c2d68\Nette\Utils\Json;
|
||||
use App\Enum\MarketNames;
|
||||
use App\Helper\Currency;
|
||||
use App\Helper\DataNeed\NeedFileName;
|
||||
@@ -13,6 +14,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Form\Extension\Core\Type\FileType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
@@ -62,24 +64,8 @@ class TaxesController extends AbstractController
|
||||
$form->handleRequest($request);
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$files = $form->get('uploads')->getData();
|
||||
$hash = substr(md5(random_int(0, mt_getrandmax())),0,16);
|
||||
if ($files) {
|
||||
$i=1;
|
||||
/** @var UploadedFile $file */
|
||||
foreach ($files as $file) {
|
||||
$newName = $i;
|
||||
$checkName = NeedFileName::check($file->getClientOriginalName());
|
||||
if ($checkName !== null) {
|
||||
$newName .= "-".$checkName;
|
||||
}
|
||||
$file->move(
|
||||
$this->getParameter("uploads_path")."/".$hash,
|
||||
$newName.".".$file->getClientOriginalExtension()
|
||||
);
|
||||
$i++;
|
||||
}
|
||||
return $this->redirectToRoute("app_taxes_{$type}_show",["hash" => $hash]);
|
||||
}
|
||||
$hash = $this->saveFiles($files);
|
||||
return $this->redirectToRoute("app_taxes_{$type}_show",["hash" => $hash]);
|
||||
}
|
||||
|
||||
return $this->renderForm('taxes/index.html.twig', [
|
||||
@@ -89,6 +75,59 @@ class TaxesController extends AbstractController
|
||||
]);
|
||||
}
|
||||
|
||||
private function saveFiles(array $files) : string
|
||||
{
|
||||
if (!count($files)) {
|
||||
return "";
|
||||
}
|
||||
$hash = substr(md5(random_int(0, mt_getrandmax())), 0, 16);
|
||||
$i = 1;
|
||||
/** @var UploadedFile $file */
|
||||
foreach ($files as $file) {
|
||||
$newName = $i;
|
||||
$checkName = NeedFileName::check($file->getClientOriginalName());
|
||||
if ($checkName !== null) {
|
||||
$newName .= "-" . $checkName;
|
||||
}
|
||||
$file->move(
|
||||
$this->getParameter("uploads_path") . "/" . $hash,
|
||||
$newName . "." . $file->getClientOriginalExtension()
|
||||
);
|
||||
$i++;
|
||||
}
|
||||
|
||||
return $hash;
|
||||
}
|
||||
|
||||
#[Route(path: '/taxes/summary/{type}', name: 'app_taxes_summary', methods: ['POST'])]
|
||||
public function takeSummary(string $type, Request $request, DividendsService $tax, ?MarketNames $market): Response
|
||||
{
|
||||
$files = $request->files->all()['pliki'];
|
||||
$hash = $this->saveFiles($files);
|
||||
if ($hash) {
|
||||
$tax->setHash($hash);
|
||||
if ($tax->readFiles($market)) {
|
||||
$gen = $tax->generate();
|
||||
// dd($gen);
|
||||
return $this->render("taxes/show.html.twig", [
|
||||
"taxes" => $gen->getSummaryByCountry(),
|
||||
"errorDividend" => $gen->getErrorDividend(),
|
||||
"errorString" => $gen->getErrorString(),
|
||||
"rawCount" => $gen->takeCountRawData(),
|
||||
"markets" => $gen->getMarkets(),
|
||||
"markets_all" => MarketNames::cases(),
|
||||
"hash_param" => $tax->takeHash(),
|
||||
"current_market" => $market,
|
||||
]);
|
||||
|
||||
}
|
||||
}
|
||||
return $this->json([
|
||||
"type" => $type,
|
||||
"request" => $files,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route(path: '/taxes/dividend/{hash}/{market}', name: 'app_taxes_dividend_show', defaults: ['market' => null])]
|
||||
public function showDividend(DividendsService $tax, ?MarketNames $market) : Response
|
||||
{
|
||||
@@ -105,9 +144,8 @@ class TaxesController extends AbstractController
|
||||
"hash_param" => $tax->takeHash(),
|
||||
"current_market" => $market,
|
||||
]);
|
||||
|
||||
}
|
||||
return new Response("error");
|
||||
return new Response("Brak danych do wyświetlenia.");
|
||||
}
|
||||
#[Route(path: '/taxes/crypto/{hash}', name: 'app_taxes_crypto_show')]
|
||||
public function showCrypto(CryptoService $tax) : Response
|
||||
|
||||
@@ -10,7 +10,7 @@ class MasterServices
|
||||
protected string $uploadPath;
|
||||
protected array $allFiles;
|
||||
|
||||
public function __construct(protected EntityManager $manager, protected string $hashParam)
|
||||
public function __construct(protected EntityManager $manager, protected ?string $hashParam)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -23,6 +23,9 @@ class MasterServices
|
||||
{
|
||||
$fileNames = [];
|
||||
$dir=$this->uploadPath . "/" . $this->hashParam;
|
||||
if (!is_dir($dir)) {
|
||||
return false;
|
||||
}
|
||||
$handle = opendir($dir);
|
||||
while (($inDir = readdir($handle)) !== false) {
|
||||
if ($inDir !== "." && $inDir !== "..") {
|
||||
@@ -46,4 +49,9 @@ class MasterServices
|
||||
{
|
||||
return $this->hashParam;
|
||||
}
|
||||
|
||||
public function setHash(string $hash): void
|
||||
{
|
||||
$this->hashParam = $hash;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user