Skip to content

Commit

Permalink
Further dev work on new playlist manager.
Browse files Browse the repository at this point in the history
  • Loading branch information
BusterNeece committed Nov 4, 2019
1 parent 9768ab9 commit a44f9fe
Show file tree
Hide file tree
Showing 18 changed files with 289 additions and 307 deletions.
2 changes: 1 addition & 1 deletion config/assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ function (Request $request) {

'station_playlists' => [
'order' => 10,
'require' => ['vue', 'vue-translations', 'bootstrap-vue'],
'require' => ['vue', 'vue-translations', 'bootstrap-vue', 'moment_timezone'],
'files' => [
'js' => [
[
Expand Down
47 changes: 19 additions & 28 deletions config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,22 @@
->add(Middleware\Module\StationFiles::class)
->add(new Middleware\Permissions(Acl::STATION_MEDIA, true));

$group->get('/playlists/schedule', Controller\Api\Stations\PlaylistsController::class . ':scheduleAction')
->setName('api:stations:playlists:schedule')
->add(new Middleware\Permissions(Acl::STATION_MEDIA, true));

$group->group('/playlist/{id}', function (RouteCollectorProxy $group) {

$group->put('/toggle', Controller\Api\Stations\PlaylistsController::class . ':toggleAction')
->setName('api:station:playlist:toggle');

$group->get('/order', Controller\Api\Stations\PlaylistsController::class . ':getOrderAction')
->setName('api:station:playlist:order');

$group->put('/order', Controller\Api\Stations\PlaylistsController::class . ':putOrderAction');

})->add(new Middleware\Permissions(Acl::STATION_MEDIA, true));

$group->get('/status', Controller\Api\Stations\ServicesController::class . ':statusAction')
->setName('api:stations:status')
->add(new Middleware\Permissions(Acl::STATION_VIEW, true));
Expand Down Expand Up @@ -514,34 +530,9 @@

})->add(new Middleware\Permissions(Acl::STATION_LOGS, true));

$group->group('/playlists', function (RouteCollectorProxy $group) {

$group->get('', Controller\Stations\PlaylistsController::class . ':indexAction')
->setName('stations:playlists:index');

$group->get('/schedule', Controller\Stations\PlaylistsController::class . ':scheduleAction')
->setName('stations:playlists:schedule');

$group->map(['GET', 'POST'], '/edit/{id}', Controller\Stations\PlaylistsController::class . ':editAction')
->setName('stations:playlists:edit');

$group->map(['GET', 'POST'], '/add', Controller\Stations\PlaylistsController::class . ':editAction')
->setName('stations:playlists:add');

$group->get('/delete/{id}/{csrf}', Controller\Stations\PlaylistsController::class . ':deleteAction')
->setName('stations:playlists:delete');

$group->map(['GET', 'POST'], '/reorder/{id}',
Controller\Stations\PlaylistsController::class . ':reorderAction')
->setName('stations:playlists:reorder');

$group->get('/toggle/{id}', Controller\Stations\PlaylistsController::class . ':toggleAction')
->setName('stations:playlists:toggle');

$group->get('/export/{id}[/{format}]', Controller\Stations\PlaylistsController::class . ':exportAction')
->setName('stations:playlists:export');

})->add(new Middleware\Permissions(Acl::STATION_MEDIA, true));
$group->get('/playlists', Controller\Stations\PlaylistsController::class)
->setName('stations:playlists:index')
->add(new Middleware\Permissions(Acl::STATION_MEDIA, true));

$group->group('/mounts', function (RouteCollectorProxy $group) {

Expand Down
11 changes: 7 additions & 4 deletions src/Controller/Api/Stations/PlaylistsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public function listAction(ServerRequest $request, Response $response): Response
->from(Entity\StationPlaylist::class, 'sp')
->leftJoin('sp.schedule_items', 'spc')
->where('sp.station = :station')
->orderBy('sp.name', 'ASC')
->setParameter('station', $station);

$searchPhrase = trim($request->getParam('searchPhrase', ''));
Expand Down Expand Up @@ -179,8 +180,8 @@ public function scheduleAction(ServerRequest $request, Response $response): Resp

if ($scheduleItem->shouldPlayOnCurrentDate($i)
&& $scheduleItem->isScheduledToPlayToday($dayOfWeek)) {
$playlistStart = Entity\StationPlaylist::getDateTime($scheduleItem->getScheduleStartTime(), $i);
$playlistEnd = Entity\StationPlaylist::getDateTime($scheduleItem->getScheduleEndTime(), $i);
$playlistStart = Entity\StationPlaylist::getDateTime($scheduleItem->getStartTime(), $i);
$playlistEnd = Entity\StationPlaylist::getDateTime($scheduleItem->getEndTime(), $i);

// Handle overnight playlists
if ($playlistEnd < $playlistStart) {
Expand All @@ -193,7 +194,7 @@ public function scheduleAction(ServerRequest $request, Response $response): Resp
'start' => $playlistStart->toIso8601String(),
'end' => $playlistEnd->toIso8601String(),
'url' => (string)$request->getRouter()->named(
'stations:playlists:edit',
'api:stations:playlist',
['station_id' => $station->getId(), 'id' => $playlist->getId()]
),
];
Expand Down Expand Up @@ -319,7 +320,7 @@ public function toggleAction(ServerRequest $request, Response $response, $id): R

protected function _getRecord(Entity\Station $station, $id)
{
return $this->em->createQuery(/** @lang DQL */ 'SELECT DISTINCT sp, spc FROM Entity\StationPlaylist sp JOIN sp.schedule_items spc WHERE sp.id = :id AND sp.station = :station')
return $this->em->createQuery(/** @lang DQL */ 'SELECT DISTINCT sp, spc FROM App\Entity\StationPlaylist sp JOIN sp.schedule_items spc WHERE sp.id = :id AND sp.station = :station')
->setParameter('id', $id)
->setParameter('station', $station)
->getSingleResult();
Expand All @@ -345,6 +346,8 @@ protected function _viewRecord($record, RouterInterface $router)
$return['total_length'] = (int)$song_totals[0]['total_length'];

$return['links'] = [
'toggle' => (string)$router->fromHere('api:station:playlist:toggle', ['id' => $record->getId()], [], true),
'order' => (string)$router->fromHere('api:station:playlist:order', ['id' => $record->getId()], [], true),
'self' => (string)$router->fromHere($this->resourceRouteName, ['id' => $record->getId()], [], true),
];
return $return;
Expand Down
110 changes: 4 additions & 106 deletions src/Controller/Stations/PlaylistsController.php
Original file line number Diff line number Diff line change
@@ -1,46 +1,20 @@
<?php
namespace App\Controller\Stations;

use App\Entity;
use App\Form\StationPlaylistForm;
use App\Http\Response;
use App\Http\ServerRequest;
use Azura\Exception;
use Azura\Session\Flash;
use Cake\Chronos\Chronos;
use DateTimeZone;
use Psr\Http\Message\ResponseInterface;

class PlaylistsController extends AbstractStationCrudController
class PlaylistsController
{
/** @var Entity\Repository\StationPlaylistRepository */
protected $playlist_repo;

/** @var Entity\Repository\StationPlaylistMediaRepository */
protected $playlist_media_repo;

/**
* @param StationPlaylistForm $form
*/
public function __construct(
StationPlaylistForm $form,
Entity\Repository\StationPlaylistRepository $playlistRepo,
Entity\Repository\StationPlaylistMediaRepository $spmRepo
) {
parent::__construct($form);

$this->csrf_namespace = 'stations_playlists';
$this->playlist_repo = $playlistRepo;
$this->playlist_media_repo = $spmRepo;
}

/**
* @param ServerRequest $request
* @param Response $response
*
* @return ResponseInterface
*/
public function indexAction(ServerRequest $request, Response $response): ResponseInterface
public function __invoke(ServerRequest $request, Response $response): ResponseInterface
{
$station = $request->getStation();

Expand All @@ -49,84 +23,8 @@ public function indexAction(ServerRequest $request, Response $response): Respons
throw new Exception(__('This feature is not currently supported on this station.'));
}

/** @var Entity\StationPlaylist[] $all_playlists */
$all_playlists = $station->getPlaylists();

$total_weights = 0;
foreach ($all_playlists as $playlist) {
if ($playlist->getIsEnabled() && $playlist->getType() === 'default') {
$total_weights += $playlist->getWeight();
}
}

$station_tz = $station->getTimezone();
$now = Chronos::now(new DateTimeZone($station_tz));

$playlists = [];

$songs_query = $this->em->createQuery(/** @lang DQL */ '
SELECT count(sm.id) AS num_songs, sum(sm.length) AS total_length
FROM App\Entity\StationMedia sm
JOIN sm.playlists spm
WHERE spm.playlist = :playlist');

foreach ($all_playlists as $playlist) {
$playlist_row = $this->playlist_repo->toArray($playlist);

if ($playlist->getIsEnabled()) {
if (Entity\StationPlaylist::TYPE_DEFAULT === $playlist->getType()) {
$playlist_row['probability'] = round(($playlist->getWeight() / $total_weights) * 100, 1) . '%';
} elseif (Entity\StationPlaylist::TYPE_SCHEDULED === $playlist->getType()) {
$schedule_start = Entity\StationPlaylist::getDateTime($playlist->getScheduleStartTime(), $now);
$schedule_end = Entity\StationPlaylist::getDateTime($playlist->getScheduleEndTime(), $now);

$playlist_row['schedule_start'] = $schedule_start->toIso8601String();
$playlist_row['schedule_end'] = $schedule_end->toIso8601String();
}
}

$song_totals = $songs_query->setParameter('playlist', $playlist)
->getArrayResult();

$playlist_row['num_songs'] = (int)$song_totals[0]['num_songs'];
$playlist_row['total_length'] = (int)$song_totals[0]['total_length'];
$playlists[$playlist->getId()] = $playlist_row;
}

return $request->getView()->renderToResponse($response, 'stations/playlists/index', [
'playlists' => $playlists,
'csrf' => $request->getCsrf()->generate($this->csrf_namespace),
'station_tz' => $station_tz,
'station_now' => $now->toIso8601String(),
'schedule_url' => $request->getRouter()->named('stations:playlists:schedule',
['station_id' => $station->getId()]),
]);
}


public function editAction(ServerRequest $request, Response $response, $id = null): ResponseInterface
{
if (false !== $this->_doEdit($request, $id)) {
$request->getFlash()->addMessage('<b>' . ($id ? __('Playlist updated.') : __('Playlist added.')) . '</b>',
Flash::SUCCESS);
return $response->withRedirect($request->getRouter()->fromHere('stations:playlists:index'));
}

return $request->getView()->renderToResponse($response, 'stations/playlists/edit', [
'form' => $this->form,
'title' => $id ? __('Edit Playlist') : __('Add Playlist'),
'station_tz' => $station->getTimezone(),
]);
}

public function deleteAction(
ServerRequest $request,
Response $response,
$id,
$csrf
): ResponseInterface {
$this->_doDelete($request, $id, $csrf);

$request->getFlash()->addMessage('<b>' . __('Playlist deleted.') . '</b>', Flash::SUCCESS);
return $response->withRedirect($request->getRouter()->fromHere('stations:playlists:index'));
}
}
}
6 changes: 3 additions & 3 deletions templates/stations/playlists/index.js.phtml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
$props = [
'listUrl' => $router->fromHere('api:stations:playlists:list'),
'calendarUrl' => $router->fromHere('api:stations:playlists:calendar'),
'listUrl' => $router->fromHere('api:stations:playlists'),
'scheduleUrl' => $router->fromHere('api:stations:playlists:schedule'),
'locale' => substr($customization->getLocale(), 0, 2),
'filesUrl' => $router->fromHere('stations:files:index'),
'stationTimeZone' => $station_tz,
Expand All @@ -13,7 +13,7 @@ $(function () {
station_playlists = new Vue({
el: '#station-playlist',
render: function (createElement) {
return createElement(StationMedia.default, {
return createElement(StationPlaylist.default, {
props: <?=json_encode($props) ?>
});
}
Expand Down
2 changes: 1 addition & 1 deletion templates/stations/playlists/index.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ $assets->load('station_playlists')
]));
?>

<div class="station-playlist"></div>
<div id="station-playlist"></div>
4 changes: 2 additions & 2 deletions web/static/assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
"dist/light.css": "dist/light-97511efaa3.css",
"dist/material.js": "dist/material-8a0f31e5be.js",
"dist/radio_player.js": "dist/radio_player-b8df57d8b4.js",
"dist/station_media.js": "dist/station_media-f55d41e17e.js",
"dist/station_playlists.js": "dist/station_playlists-c5a13ac09c.js",
"dist/station_media.js": "dist/station_media-7bf9066b1f.js",
"dist/station_playlists.js": "dist/station_playlists-c8e3690282.js",
"dist/vue_gettext.js": "dist/vue_gettext-3680f650b9.js",
"dist/webcaster.js": "dist/webcaster-137f49b033.js",
"dist/zxcvbn.js": "dist/zxcvbn-f4433cd930.js"
Expand Down

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion web/static/dist/station_playlists-c5a13ac09c.js

This file was deleted.

1 change: 1 addition & 0 deletions web/static/dist/station_playlists-c8e3690282.js

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions web/static/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions web/static/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"@flowjs/flow.js": "^2.13.1",
"@fullcalendar/core": "^4.3.1",
"@fullcalendar/daygrid": "^4.3.0",
"@fullcalendar/moment": "^4.3.0",
"@fullcalendar/moment-timezone": "^4.3.0",
"@fullcalendar/timegrid": "^4.3.0",
"@fullcalendar/vue": "^4.3.1",
"autosize": "^4.0.2",
"axios": "^0.18.1",
Expand Down
Loading

0 comments on commit a44f9fe

Please sign in to comment.