Skip to content

Commit

Permalink
migrate playist editor and view playlist to twig
Browse files Browse the repository at this point in the history
  • Loading branch information
RocketMan committed Sep 23, 2024
1 parent 450455e commit 2ed78a7
Show file tree
Hide file tree
Showing 11 changed files with 365 additions and 344 deletions.
6 changes: 2 additions & 4 deletions api/Playlists.php
Original file line number Diff line number Diff line change
Expand Up @@ -680,15 +680,13 @@ public function deleteResource(RequestInterface $request): ResponseInterface {
}

private function injectMetadata($api, $rqMeta, $rsMeta, $listId, $size, $entry) {
ob_start();
$action = $rqMeta->getOptional("action", "");
PlaylistBuilder::newInstance([
$fragment = PlaylistBuilder::newInstance([
"action" => $action,
"editMode" => true,
"authUser" => true
])->observe($entry);
$rsMeta->set("html", ob_get_contents());
ob_end_clean();
$rsMeta->set("html", $fragment);

// seq is one of:
// -1 client playlist is out of sync with the service
Expand Down
103 changes: 22 additions & 81 deletions ui/PlaylistBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,7 @@ class PlaylistBuilder extends PlaylistObserver {
private const PARAMS = [ "action", "editMode", "authUser" ];

protected $params;
protected $break;

protected static function timestampToLocale($timestamp) {
// colon is included in 24hr format for symmetry with fxtime
$timeSpec = UI::isUsLocale() ? 'h:i a' : 'H:i';
return $timestamp ? date($timeSpec, $timestamp) : '';
}
protected $template;

public static function newInstance(array $params) {
// validate all parameters are present
Expand All @@ -54,88 +48,35 @@ public static function newInstance(array $params) {
return new PlaylistBuilder($params);
}

protected function makeAlbumLink($entry, $includeLabel) {
$albumName = $entry->getAlbum();
$labelName = $entry->getLabel();

if(empty($albumName) && empty($labelName))
return "";

$albumTitle = $entry->getTag() ?
"<a href='?s=byAlbumKey&amp;n=" . htmlentities($entry->getTag()) .
"&amp;action=search' class='nav'>" . htmlentities($albumName) . "</a>" :
UI::smartURL($albumName);

if($includeLabel)
$albumTitle .= "<span class='songLabel'> / " . UI::smartURL($labelName) . "</span>";

return $albumTitle;
}

protected function makeEditDiv($entry) {
$href = "?id=" . $entry->getId() . "&amp;subaction=" . $this->params["action"] . "&amp;seq=editTrack";
$editLink = "<a class='songEdit nav' href='$href'><span class='fas fa-edit'></span></a>";
$dnd = "<div class='grab fas fa-grip-vertical' data-id='" . $entry->getId() . "'></div>";
return "<div class='songManager'>" . $dnd . $editLink . "</div>";
protected function renderBlock($block, $entry) {
return $this->template->renderBlock($block, [
"params" => $this->params,
"entry" => $entry
]);
}

protected function __construct(array $params) {
$templateFact = new TemplateFactoryUI();
$this->template = $templateFact->load('list/block-event.html');
$this->params = $params;
$this->params['break'] = false;
$this->params['usLocale'] = UI::isUsLocale();
$this->on('comment', function($entry) {
$editCell = $this->params["editMode"] ? "<td>" .
$this->makeEditDiv($entry) . "</td>" : "";
$created = $entry->getCreatedTimestamp();
$timeplayed = self::timestampToLocale($created);
echo "<tr class='commentRow".($this->params["editMode"]?"Edit":"")."'>" . $editCell .
"<td class='time' data-utc='$created'>$timeplayed</td>" .
"<td colspan=4>".UI::markdown($entry->getComment()).
"</td></tr>\n";
$this->break = false;
$fragment = $this->renderBlock('comment', $entry);
$this->params['break'] = false;
return $fragment;
})->on('logEvent', function($entry) {
$created = $entry->getCreatedTimestamp();
$timeplayed = self::timestampToLocale($created);
if($this->params["authUser"]) {
// display log entries only for authenticated users
$editCell = $this->params["editMode"] ? "<td>" .
$this->makeEditDiv($entry) . "</td>" : "";
echo "<tr class='logEntry".($this->params["editMode"]?"Edit":"")."'>" . $editCell .
"<td class='time' data-utc='$created'>$timeplayed</td>" .
"<td>".$entry->getLogEventType()."</td>" .
"<td colspan=3>".$entry->getLogEventCode()."</td>" .
"</tr>\n";
$this->break = false;
} else if(!$this->break) {
echo "<tr class='songDivider'>" .
"<td class='time' data-utc='$created'>$timeplayed</td><td colspan=4><hr></td></tr>\n";
$this->break = true;
}
$fragment = $this->renderBlock('logEvent', $entry);
$this->params['break'] = !$this->params['authUser'];
return $fragment;
})->on('setSeparator', function($entry) {
if($this->params["editMode"] || !$this->break) {
$editCell = $this->params["editMode"] ? "<td>" .
$this->makeEditDiv($entry) . "</td>" : "";
$created = $entry->getCreatedTimestamp();
$timeplayed = self::timestampToLocale($created);
echo "<tr class='songDivider'>" . $editCell .
"<td class='time' data-utc='$created'>$timeplayed</td><td colspan=4><hr></td></tr>\n";
$this->break = true;
}
$fragment = $this->renderBlock('setSeparator', $entry);
$this->params['break'] = true;
return $fragment;
})->on('spin', function($entry) {
$editCell = $this->params["editMode"] ? "<td>" .
$this->makeEditDiv($entry) . "</td>" : "";
$created = $entry->getCreatedTimestamp();
$timeplayed = self::timestampToLocale($created);
$reviewCell = $entry->getReviewed() ? "<div class='albumReview'></div>" : "";
$artistName = $entry->getTag() ? PlaylistEntry::swapNames($entry->getArtist()) : $entry->getArtist();

$albumLink = $this->makeAlbumLink($entry, true);
echo "<tr class='songRow'>" . $editCell .
"<td class='time' data-utc='$created'>$timeplayed</td>" .
"<td>" . UI::smartURL($artistName) . "</td>" .
"<td>" . UI::smartURL($entry->getTrack()) . "</td>" .
"<td style='width: 15px'>$reviewCell</td>" .
"<td>$albumLink</td>" .
"</tr>\n";
$this->break = false;
$fragment = $this->renderBlock('spin', $entry);
$this->params['break'] = false;
return $fragment;
});
}
}
Loading

0 comments on commit 2ed78a7

Please sign in to comment.