implement torrent locales form #11

This commit is contained in:
ghost 2023-10-08 01:03:27 +03:00
parent 6031ce59a2
commit bf08fa6191
7 changed files with 544 additions and 243 deletions

View file

@ -11,16 +11,15 @@ use Symfony\Component\HttpFoundation\Request;
use App\Service\UserService;
use App\Service\TorrentService;
use App\Service\TimeService;
class TorrentController extends AbstractController
{
#[Route(
'/{_locale}/torrent/{id}',
'/{_locale}/torrent/{torrentId}',
name: 'torrent_info',
requirements:
[
'id' => '\d+'
'torrentId' => '\d+'
],
methods:
[
@ -32,7 +31,6 @@ class TorrentController extends AbstractController
TranslatorInterface $translator,
UserService $userService,
TorrentService $torrentService,
TimeService $timeService
): Response
{
// Init user
@ -41,7 +39,7 @@ class TorrentController extends AbstractController
);
// Init torrent
if (!$torrent = $torrentService->getTorrent($request->get('id')))
if (!$torrent = $torrentService->getTorrent($request->get('torrentId')))
{
throw $this->createNotFoundException();
}
@ -62,18 +60,18 @@ class TorrentController extends AbstractController
}
/*
if (!$torrent = $torrentService->getTorrentLocales($request->get('id')))
if (!$torrent = $torrentService->getTorrentLocales($request->get('torrentId')))
{
throw $this->createNotFoundException();
}
*/
//print_r($file->getFileTree());exit;
return $this->render('default/torrent/info.html.twig', [
'torrent' =>
[
'id' => $torrent->getId(),
'added' => 0, // @TODO
'locales' => [], //$torrent->getLocales(),
'locales' => $torrentService->findLastTorrentLocales($torrent->getId()),
'pages' => []
],
'file' =>
@ -101,6 +99,184 @@ class TorrentController extends AbstractController
]);
}
#[Route(
'/{_locale}/torrent/{torrentId}/edit/locales/{torrentLocalesId}',
name: 'torrent_edit_locales',
requirements:
[
'torrentId' => '\d+',
'torrentLocalesId' => '\d+',
],
defaults:
[
'torrentLocalesId' => null,
],
methods:
[
'GET',
'POST'
]
)]
public function editLocales(
Request $request,
TranslatorInterface $translator,
UserService $userService,
TorrentService $torrentService
): Response
{
// Init user
$user = $userService->init(
$request->getClientIp()
);
if (!$user->isStatus())
{
// @TODO
throw new \Exception(
$translator->trans('Access denied')
);
}
// Init torrent
if (!$torrent = $torrentService->getTorrent($request->get('torrentId')))
{
throw $this->createNotFoundException();
}
// Init torrent locales
$torrentLocalesValue = [];
// Get from edition version requested
if ($request->get('torrentLocalesId'))
{
if ($torrentLocales = $torrentService->getTorrentLocales($request->get('torrentLocalesId')))
{
foreach ($torrentLocales->getValue() as $value)
{
$torrentLocalesValue[] = $value;
}
}
else
{
throw $this->createNotFoundException();
}
}
// Otherwise, get latest available
else
{
if ($torrentLocales = $torrentService->findLastTorrentLocales($torrent->getId()))
{
foreach ($torrentLocales->getValue() as $value)
{
$torrentLocalesValue[] = $value;
}
// Update active locale
$request->attributes->set('torrentLocalesId', $torrentLocales->getId());
}
else
{
$torrentLocalesValue[] = $request->get('_locale');
}
}
// Init edition history
$editions = [];
foreach ($torrentService->findTorrentLocales($torrent->getId()) as $torrentLocales)
{
$editions[] =
[
'id' => $torrentLocales->getId(),
'added' => $torrentLocales->getAdded(),
'approved' => $torrentLocales->isApproved(),
'active' => $torrentLocales->getId() == $request->get('torrentLocalesId'),
'user' =>
[
'id' => $torrentLocales->getUserId(),
'identicon' => $userService->identicon(
$userService->get(
$torrentLocales->getUserId()
)->getAddress()
),
]
];
}
// Init form
$form =
[
'locales' =>
[
'error' => [],
'attribute' =>
[
'value' => $request->get('locales') ? $request->get('locales') : $torrentLocalesValue,
'placeholder' => $translator->trans('Content language')
]
]
];
// Process request
if ($request->isMethod('post'))
{
/// Locales
$locales = [];
if ($request->get('locales'))
{
foreach ((array) $request->get('locales') as $locale)
{
if (in_array($locale, explode('|', $this->getParameter('app.locales'))))
{
$locales[] = $locale;
}
}
}
//// At least one valid locale required
if (!$locales)
{
$form['locales']['error'][] = $translator->trans('At least one locale required');
}
// Request is valid
if (empty($form['locales']['error']))
{
// Save data
$torrentService->addTorrentLocales(
$torrent->getId(),
$user->getId(),
time(),
$locales,
$user->isApproved()
);
// Redirect to info page created
return $this->redirectToRoute(
'torrent_info',
[
'_locale' => $request->get('_locale'),
'torrentId' => $torrent->getId()
]
);
}
}
// Render form template
return $this->render(
'default/torrent/edit/locales.html.twig',
[
'torrentId' => $torrent->getId(),
'locales' => explode('|', $this->getParameter('app.locales')),
'editions' => $editions,
'form' => $form,
]
);
}
#[Route(
'/{_locale}/submit/torrent',
name: 'torrent_submit',
@ -208,7 +384,7 @@ class TorrentController extends AbstractController
if (empty($form['torrent']['error']) && empty($form['locales']['error']))
{
// Save data
$torrent = $torrentService->submit(
$torrent = $torrentService->add(
$file->getPathName(),
$user->getId(),
time(),
@ -221,8 +397,8 @@ class TorrentController extends AbstractController
return $this->redirectToRoute(
'torrent_info',
[
'_locale' => $request->get('_locale'),
'id' => $torrent->getId()
'_locale' => $request->get('_locale'),
'torrentId' => $torrent->getId()
]
);
}