Permalink
Browse files

Simplified LyricsController

  • Loading branch information...
Thoulah committed Feb 10, 2019
1 parent 23cf2b2 commit 374d6a2d76944b14a00b9991b5b0434b7fa05567
@@ -2,15 +2,12 @@
namespace app\controllers;
use Yii;
use app\models\lyrics\{Lyrics1Artists, Lyrics2Albums, Lyrics3Tracks};
use yii\helpers\{ArrayHelper, Url};
use yii\helpers\ArrayHelper;
use yii\web\{NotFoundHttpException, Response};
class LyricsController extends \yii\web\Controller {
const PAGE_INDEX = 0;
const PAGE_ARTIST = 1;
const PAGE_ALBUM = 2;
public $page;
public $data;
public $artist;
public $year;
public $album;
@@ -24,13 +21,14 @@ public function init(): void {
$this->size = Yii::$app->request->get('size');
if ($this->artist && $this->year && $this->album) :
$this->page = self::PAGE_ALBUM;
list($this->page, $this->data) = $this->getAlbum();
$this->lastModified = Lyrics3Tracks::getLastModified($this->artist, $this->year, $this->album);
elseif ($this->artist) :
$this->page = self::PAGE_ARTIST;
list($this->page, $this->data) = $this->getArtist();
$this->lastModified = Lyrics2Albums::getLastModified($this->artist);
else :
$this->page = self::PAGE_INDEX;
$this->page = '1_index';
$this->data = Lyrics1Artists::artistsList();
$this->lastModified = Lyrics1Artists::getLastModified();
endif;
@@ -50,77 +48,46 @@ public function behaviors(): array {
}
public function actionIndex(): string {
switch ($this->page) :
case self::PAGE_INDEX: list($page, $data) = ['1_index', (new Lyrics1Artists)->artistsList()]; break;
case self::PAGE_ARTIST: list($page, $data) = $this->pageArtist(); break;
case self::PAGE_ALBUM: list($page, $data) = $this->pageAlbum(); break;
endswitch;
Yii::$app->view->registerMetaTag(['name' => 'google', 'content' => 'notranslate']);
return $this->render($page, [
'data' => $data,
return $this->render($this->page, [
'data' => $this->data,
]);
}
public function actionAlbumpdf(): Response {
$tracks = Lyrics3Tracks::tracksList($this->artist, $this->year, $this->album);
if (!ArrayHelper::keyExists(0, $tracks) || !$tracks[0]->album->active) :
throw new NotFoundHttpException('Album not found.');
endif;
$this->redirectIfNotUrl('albumpdf', $tracks);
$fileName = Lyrics2Albums::buildPdf($tracks[0]->album, $this->renderPartial('albumPdf', ['tracks' => $tracks]));
return Yii::$app->response->sendFile($fileName, implode(' - ', [$tracks[0]->artist->url, $tracks[0]->album->year, $tracks[0]->album->url]).'.pdf');
$fileName = Lyrics2Albums::buildPdf($this->data[0]->album, $this->renderPartial('albumPdf', ['tracks' => $this->data]));
return Yii::$app->response->sendFile($fileName, implode(' - ', [$this->data[0]->artist->url, $this->data[0]->album->year, $this->data[0]->album->url]).'.pdf');
}
public function actionAlbumcover(): Response {
$tracks = Lyrics3Tracks::tracksList($this->artist, $this->year, $this->album);
if (!ArrayHelper::keyExists(0, $tracks) || !ArrayHelper::isIn($this->size, [100, 500, 800])) :
if (!ArrayHelper::isIn($this->size, [100, 500, 800]))
throw new NotFoundHttpException('Cover not found.');
endif;
$this->redirectIfNotUrl('albumcover', $tracks);
list($fileName, $image) = Lyrics2Albums::getCover($this->size, $tracks);
list($fileName, $image) = Lyrics2Albums::getCover($this->size, $this->data);
return Yii::$app->response->sendContentAsFile($image, $fileName, ['mimeType' => 'image/jpeg', 'inline' => true]);
}
private function pageArtist(): array {
private function getArtist(): array {
$albums = Lyrics2Albums::albumsList($this->artist);
if (count($albums) === 0) :
if (count($albums) === 0)
throw new NotFoundHttpException('Artist not found.');
endif;
if ($albums[0]->artist->url !== $this->artist) :
$this->redirect(['index', 'artist' => $albums[0]->artist->url], 301)->send();
endif;
if ($albums[0]->artist->url !== $this->artist)
$this->redirect(["/{$this->module->requestedRoute}", 'artist' => $albums[0]->artist->url], 301)->send();
return ['2_artist', $albums];
}
private function pageAlbum(): array {
private function getAlbum(): array {
$tracks = Lyrics3Tracks::tracksList($this->artist, $this->year, $this->album);
if (!ArrayHelper::keyExists(0, $tracks) || (php_sapi_name() !== 'cli' && (!Yii::$app->user->isGuest && !Yii::$app->user->identity->isAdmin) && !$tracks[0]->album->active)) :
if (!ArrayHelper::keyExists(0, $tracks) || (php_sapi_name() !== 'cli' && (!Yii::$app->user->isGuest && !Yii::$app->user->identity->isAdmin) && !$tracks[0]->album->active))
throw new NotFoundHttpException('Album not found.');
endif;
$this->redirectIfNotUrl('index', $tracks);
Yii::$app->view->registerLinkTag(['rel' => 'alternate', 'href' => Url::to(['albumpdf', 'artist' => $tracks[0]->artist->url, 'year' => $tracks[0]->album->year, 'album' => $tracks[0]->album->url], true), 'type' => 'application/pdf', 'title' => 'PDF']);
if ($tracks[0]->album->image) :
Yii::$app->view->registerMetaTag(['property' => 'og:image', 'content' => Url::to(['cover', 'artist' => $tracks[0]->artist->url, 'year' => $tracks[0]->album->year, 'album' => $tracks[0]->album->url, 'size' => 'cover'], true)]);
endif;
Yii::$app->view->registerMetaTag(['property' => 'og:type', 'content' => 'music.album']);
if ($tracks[0]->artist->url !== $this->artist || $tracks[0]->album->url !== $this->album)
$this->redirect(["/{$this->module->requestedRoute}", 'artist' => $tracks[0]->artist->url, 'year' => $tracks[0]->album->year, 'album' => $tracks[0]->album->url, 'size' => $this->size], 301)->send();
return ['3_album', $tracks];
}
private function redirectIfNotUrl(string $page, array $data): bool {
if ($data[0]->artist->url !== $this->artist || $data[0]->album->url !== $this->album) :
$this->redirect([$page, 'artist' => $data[0]->artist->url, 'year' => $data[0]->album->year, 'album' => $data[0]->album->url], 301)->send();
endif;
return true;
}
}
@@ -15,7 +15,7 @@ public function afterFind(): void {
$this->active = (bool) $this->active;
}
public function artistsList(): array {
public static function artistsList(): array {
return self::find()
->orderBy('name')
->all();
@@ -65,7 +65,7 @@ public static function getLastModified(string $artist): int {
->where(['or', Lyrics1Artists::tableName().'.`name`=:artist', Lyrics1Artists::tableName().'.`url`=:artist'])
->addParams([':artist' => $artist])
->max(self::tableName().'.updated');
return Yii::$app->formatter->asTimestamp($data);
return (int) Yii::$app->formatter->asTimestamp($data);
}
public function getArtist(): LyricsQuery {
@@ -1,12 +1,18 @@
<?php
use yii\bootstrap4\Html;
use yii\helpers\Url;
$this->title = implode(' - ', [$data[0]->artist->name, $data[0]->album->name, 'Lyrics']);
$this->params['breadcrumbs'][] = Yii::t('mr42', 'Music');
$this->params['breadcrumbs'][] = ['label' => Yii::t('mr42', 'Lyrics'), 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $data[0]->artist->name, 'url' => ['index', 'artist' => $data[0]->artist->url]];
$this->params['breadcrumbs'][] = $data[0]->album->name;
if ($data[0]->album->image)
$this->registerMetaTag(['property' => 'og:image', 'content' => Url::to(['albumcover', 'artist' => $data[0]->artist->url, 'year' => $data[0]->album->year, 'album' => $data[0]->album->url, 'size' => 800], true)]);
$this->registerMetaTag(['property' => 'og:type', 'content' => 'music.album']);
$this->registerLinkTag(['rel' => 'alternate', 'href' => Url::to(['albumpdf', 'artist' => $data[0]->artist->url, 'year' => $data[0]->album->year, 'album' => $data[0]->album->url], true), 'type' => 'application/pdf', 'title' => 'PDF']);
if ($data[0]->album->image_color)
$this->params['themeColor'] = $data[0]->album->image_color;

0 comments on commit 374d6a2

Please sign in to comment.