2016-09-02 20:37:12 +00:00
|
|
|
<?php
|
2018-08-04 22:05:14 +00:00
|
|
|
namespace App\Controller\Api;
|
2016-09-02 20:37:12 +00:00
|
|
|
|
2018-08-21 00:48:03 +00:00
|
|
|
use App\Doctrine\Paginator;
|
2017-05-20 00:40:36 +00:00
|
|
|
use App\Utilities;
|
2018-08-04 22:05:14 +00:00
|
|
|
use App\ApiUtilities;
|
2018-02-01 11:49:40 +00:00
|
|
|
use Doctrine\ORM\EntityManager;
|
2018-08-04 22:05:14 +00:00
|
|
|
use App\Entity;
|
2018-02-01 11:49:40 +00:00
|
|
|
use App\Http\Request;
|
|
|
|
use App\Http\Response;
|
2016-09-02 20:37:12 +00:00
|
|
|
|
2018-02-01 11:49:40 +00:00
|
|
|
class RequestsController
|
2016-09-02 20:37:12 +00:00
|
|
|
{
|
2018-02-01 11:49:40 +00:00
|
|
|
/** @var EntityManager */
|
|
|
|
protected $em;
|
2017-12-09 17:29:01 +00:00
|
|
|
|
2018-04-17 22:51:43 +00:00
|
|
|
/** @var ApiUtilities */
|
|
|
|
protected $api_utils;
|
2018-02-01 11:49:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param EntityManager $em
|
2018-08-21 00:48:03 +00:00
|
|
|
* @param ApiUtilities $api_utils
|
2018-08-27 07:14:05 +00:00
|
|
|
* @see \App\Provider\ApiProvider
|
2018-02-01 11:49:40 +00:00
|
|
|
*/
|
2018-08-25 10:56:43 +00:00
|
|
|
public function __construct(EntityManager $em, ApiUtilities $api_utils)
|
2018-02-01 11:49:40 +00:00
|
|
|
{
|
|
|
|
$this->em = $em;
|
2018-04-17 22:51:43 +00:00
|
|
|
$this->api_utils = $api_utils;
|
2017-12-09 17:29:01 +00:00
|
|
|
}
|
|
|
|
|
2017-06-17 00:19:42 +00:00
|
|
|
/**
|
|
|
|
* @SWG\Get(path="/station/{station_id}/requests",
|
2017-06-17 19:39:21 +00:00
|
|
|
* tags={"Stations: Song Requests"},
|
2017-06-17 00:19:42 +00:00
|
|
|
* description="Return a list of requestable songs.",
|
|
|
|
* @SWG\Parameter(ref="#/parameters/station_id_required"),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Success",
|
|
|
|
* @SWG\Schema(
|
|
|
|
* type="array",
|
|
|
|
* @SWG\Items(ref="#/definitions/StationRequest")
|
|
|
|
* )
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(response=404, description="Station not found"),
|
|
|
|
* @SWG\Response(response=403, description="Station does not support requests")
|
|
|
|
* )
|
|
|
|
*/
|
2018-02-01 11:49:40 +00:00
|
|
|
public function listAction(Request $request, Response $response, $station_id): Response
|
2016-09-02 20:37:12 +00:00
|
|
|
{
|
2018-08-19 10:40:05 +00:00
|
|
|
$station = $request->getStation();
|
2016-09-02 20:37:12 +00:00
|
|
|
|
2018-04-12 22:43:58 +00:00
|
|
|
// Verify that the station supports requests.
|
2018-08-19 10:40:05 +00:00
|
|
|
$ba = $request->getStationBackend();
|
2018-04-12 22:43:58 +00:00
|
|
|
if (!$ba->supportsRequests() || !$station->getEnableRequests()) {
|
|
|
|
return $response->withJson('This station does not accept requests currently.', 403);
|
2017-01-24 00:35:16 +00:00
|
|
|
}
|
2016-11-21 23:01:42 +00:00
|
|
|
|
2018-08-21 00:48:03 +00:00
|
|
|
$qb = $this->em->createQueryBuilder();
|
|
|
|
|
|
|
|
$qb->select('sm, s, spm, sp')
|
|
|
|
->from(Entity\StationMedia::class, 'sm')
|
|
|
|
->join('sm.song', 's')
|
|
|
|
->leftJoin('sm.playlist_items', 'spm')
|
|
|
|
->leftJoin('spm.playlist', 'sp')
|
|
|
|
->where('sm.station_id = :station_id')
|
|
|
|
->andWhere('sp.id IS NOT NULL')
|
|
|
|
->andWhere('sp.is_enabled = 1')
|
|
|
|
->andWhere('sp.include_in_requests = 1')
|
|
|
|
->setParameter('station_id', $station_id);
|
|
|
|
|
|
|
|
if ($request->hasParam('sort')) {
|
|
|
|
$sort_fields = [
|
|
|
|
'song_title' => 'sm.title',
|
|
|
|
'song_artist' => 'sm.artist',
|
|
|
|
'song_album' => 'sm.album',
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach($request->getParam('sort') as $sort_key => $sort_direction)
|
|
|
|
{
|
|
|
|
if (isset($sort_fields[$sort_key])) {
|
|
|
|
$qb->addOrderBy($sort_fields[$sort_key], $sort_direction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$qb->orderBy('sm.artist', 'ASC')
|
|
|
|
->addOrderBy('sm.title', 'ASC');
|
|
|
|
}
|
|
|
|
|
|
|
|
$search_phrase = trim($request->getParam('searchPhrase'));
|
|
|
|
if (!empty($search_phrase)) {
|
|
|
|
$qb->andWhere('(sm.title LIKE :query OR sm.artist LIKE :query OR sm.album LIKE :query)')
|
|
|
|
->setParameter('query', '%'.$search_phrase.'%');
|
|
|
|
}
|
|
|
|
|
|
|
|
$paginator = new Paginator($qb);
|
|
|
|
$paginator->setFromRequest($request);
|
|
|
|
|
|
|
|
$is_bootgrid = $paginator->isFromBootgrid();
|
2018-08-25 10:56:43 +00:00
|
|
|
$router = $request->getRouter();
|
2018-08-21 00:48:03 +00:00
|
|
|
|
2018-08-25 10:56:43 +00:00
|
|
|
$paginator->setPostprocessor(function($media_row) use ($station_id, $is_bootgrid, $router) {
|
2018-04-17 22:51:43 +00:00
|
|
|
/** @var Entity\StationMedia $media_row */
|
2018-02-03 12:31:05 +00:00
|
|
|
$row = new Entity\Api\StationRequest;
|
2018-04-17 22:51:43 +00:00
|
|
|
$row->song = $media_row->api($this->api_utils);
|
|
|
|
$row->request_id = (int)$media_row->getId();
|
2018-08-25 10:56:43 +00:00
|
|
|
$row->request_url = (string)$router->named('api:requests:submit', [
|
2018-02-01 11:49:40 +00:00
|
|
|
'station' => $station_id,
|
2018-04-17 22:51:43 +00:00
|
|
|
'media_id' => $media_row->getUniqueId(),
|
2017-06-17 00:19:42 +00:00
|
|
|
]);
|
|
|
|
|
2018-08-21 00:48:03 +00:00
|
|
|
if ($is_bootgrid) {
|
2018-08-25 10:56:43 +00:00
|
|
|
return Utilities::flatten_array($row, '_');
|
2016-11-08 07:46:17 +00:00
|
|
|
}
|
|
|
|
|
2018-08-21 00:48:03 +00:00
|
|
|
return $row;
|
|
|
|
});
|
2016-11-08 07:46:17 +00:00
|
|
|
|
2018-08-21 00:48:03 +00:00
|
|
|
return $paginator->write($response);
|
2016-09-02 20:37:12 +00:00
|
|
|
}
|
|
|
|
|
2017-06-17 00:19:42 +00:00
|
|
|
/**
|
|
|
|
* @SWG\Post(path="/station/{station_id}/request/{request_id}",
|
2017-06-17 19:39:21 +00:00
|
|
|
* tags={"Stations: Song Requests"},
|
2017-06-17 00:19:42 +00:00
|
|
|
* description="Submit a song request.",
|
|
|
|
* @SWG\Parameter(ref="#/parameters/station_id_required"),
|
|
|
|
* @SWG\Parameter(
|
|
|
|
* name="request_id",
|
|
|
|
* description="The requestable song ID",
|
|
|
|
* type="integer",
|
|
|
|
* format="int64",
|
|
|
|
* in="path",
|
|
|
|
* required=true
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(response=200, description="Success"),
|
|
|
|
* @SWG\Response(response=404, description="Station not found"),
|
|
|
|
* @SWG\Response(response=403, description="Station does not support requests")
|
|
|
|
* )
|
|
|
|
*/
|
2018-02-01 11:49:40 +00:00
|
|
|
public function submitAction(Request $request, Response $response, $station_id, $media_id): Response
|
2016-09-02 20:37:12 +00:00
|
|
|
{
|
2018-08-19 10:40:05 +00:00
|
|
|
$station = $request->getStation();
|
2016-09-02 20:37:12 +00:00
|
|
|
|
2018-04-12 22:43:58 +00:00
|
|
|
// Verify that the station supports requests.
|
2018-08-19 10:40:05 +00:00
|
|
|
$ba = $request->getStationBackend();
|
2018-04-12 22:43:58 +00:00
|
|
|
if (!$ba->supportsRequests() || !$station->getEnableRequests()) {
|
|
|
|
return $response->withJson('This station does not accept requests currently.', 403);
|
2017-01-24 00:35:16 +00:00
|
|
|
}
|
2016-11-21 23:01:42 +00:00
|
|
|
|
2017-01-24 00:35:16 +00:00
|
|
|
try {
|
2018-02-01 11:49:40 +00:00
|
|
|
/** @var Entity\Repository\StationRequestRepository $request_repo */
|
|
|
|
$request_repo = $this->em->getRepository(Entity\StationRequest::class);
|
|
|
|
$request_repo->submit($station, $media_id);
|
2016-09-02 20:37:12 +00:00
|
|
|
|
2018-02-01 11:49:40 +00:00
|
|
|
return $response->withJson('Request submitted successfully.');
|
2017-01-24 00:35:16 +00:00
|
|
|
} catch (\App\Exception $e) {
|
2018-02-01 11:49:40 +00:00
|
|
|
return $response->withJson($e->getMessage(), 400);
|
2016-09-02 20:37:12 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-04 22:05:14 +00:00
|
|
|
}
|