mirror of
https://github.com/YGGverse/YGGtracker.git
synced 2026-04-01 01:25:39 +00:00
implement torrent locales form #11
This commit is contained in:
parent
6031ce59a2
commit
bf08fa6191
7 changed files with 544 additions and 243 deletions
|
|
@ -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()
|
||||
]
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,4 +20,37 @@ class TorrentLocalesRepository extends ServiceEntityRepository
|
|||
{
|
||||
parent::__construct($registry, TorrentLocales::class);
|
||||
}
|
||||
|
||||
public function getTorrentLocales(int $id): ?TorrentLocales
|
||||
{
|
||||
return $this->createQueryBuilder('tl')
|
||||
->where('tl.id = :id')
|
||||
->setParameter('id', $id)
|
||||
->getQuery()
|
||||
->getOneOrNullResult()
|
||||
;
|
||||
}
|
||||
|
||||
public function findLastTorrentLocales(int $torrentId): ?TorrentLocales
|
||||
{
|
||||
return $this->createQueryBuilder('tl')
|
||||
->where('tl.torrentId = :torrentId')
|
||||
->setParameter('torrentId', $torrentId)
|
||||
->orderBy('tl.id', 'DESC') // same to tl.added
|
||||
->setMaxResults(1)
|
||||
->getQuery()
|
||||
->getOneOrNullResult()
|
||||
;
|
||||
}
|
||||
|
||||
public function findTorrentLocales(int $torrentId): array
|
||||
{
|
||||
return $this->createQueryBuilder('tl')
|
||||
->where('tl.torrentId = :torrentId')
|
||||
->setParameter('torrentId', $torrentId)
|
||||
->orderBy('tl.id', 'DESC') // same to tl.added
|
||||
->getQuery()
|
||||
->getResult()
|
||||
;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -69,7 +69,28 @@ class TorrentService
|
|||
->findOneByIdField($id);
|
||||
}
|
||||
|
||||
public function submit(
|
||||
public function getTorrentLocales(int $id): ?TorrentLocales
|
||||
{
|
||||
return $this->entityManagerInterface
|
||||
->getRepository(TorrentLocales::class)
|
||||
->getTorrentLocales($id);
|
||||
}
|
||||
|
||||
public function findLastTorrentLocales(int $torrentId): ?TorrentLocales
|
||||
{
|
||||
return $this->entityManagerInterface
|
||||
->getRepository(TorrentLocales::class)
|
||||
->findLastTorrentLocales($torrentId);
|
||||
}
|
||||
|
||||
public function findTorrentLocales(int $torrentId): array
|
||||
{
|
||||
return $this->entityManagerInterface
|
||||
->getRepository(TorrentLocales::class)
|
||||
->findTorrentLocales($torrentId);
|
||||
}
|
||||
|
||||
public function add(
|
||||
string $filepath,
|
||||
int $userId,
|
||||
int $added,
|
||||
|
|
@ -78,7 +99,7 @@ class TorrentService
|
|||
bool $approved
|
||||
): ?Torrent
|
||||
{
|
||||
$torrent = $this->saveTorrent(
|
||||
$torrent = $this->addTorrent(
|
||||
$this->getTorrentInfoNameByFilepath($filepath),
|
||||
$this->getTorrentKeywordsByFilepath($filepath)
|
||||
);
|
||||
|
|
@ -93,7 +114,7 @@ class TorrentService
|
|||
|
||||
if (!empty($locales))
|
||||
{
|
||||
$this->saveTorrentLocales(
|
||||
$this->addTorrentLocales(
|
||||
$torrent->getId(),
|
||||
$userId,
|
||||
$added,
|
||||
|
|
@ -102,7 +123,7 @@ class TorrentService
|
|||
);
|
||||
}
|
||||
|
||||
$this->saveTorrentSensitive(
|
||||
$this->addTorrentSensitive(
|
||||
$torrent->getId(),
|
||||
$userId,
|
||||
$added,
|
||||
|
|
@ -113,7 +134,7 @@ class TorrentService
|
|||
return $torrent;
|
||||
}
|
||||
|
||||
public function saveTorrent(
|
||||
public function addTorrent(
|
||||
string $filepath,
|
||||
string $keywords
|
||||
): ?Torrent
|
||||
|
|
@ -129,7 +150,7 @@ class TorrentService
|
|||
return $torrent;
|
||||
}
|
||||
|
||||
public function saveTorrentLocales(
|
||||
public function addTorrentLocales(
|
||||
int $torrentId,
|
||||
int $userId,
|
||||
int $added,
|
||||
|
|
@ -151,7 +172,7 @@ class TorrentService
|
|||
return $torrentLocales;
|
||||
}
|
||||
|
||||
public function saveTorrentSensitive(
|
||||
public function addTorrentSensitive(
|
||||
int $torrentId,
|
||||
int $userId,
|
||||
int $added,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue