blue-twilight/app/Services/AlbumService.php

42 lines
1.1 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Services;
use App\Album;
use App\Helpers\DbHelper;
class AlbumService
{
/**
* Get a list of all albums in a flattened tree-structure.
* @return mixed
*/
public function getFlattenedAlbumTree()
{
$allAlbums = DbHelper::getAlbumsForCurrentUser_NonPaged()->get();
$result = [];
$this->buildAlbumTree($result, $allAlbums);
return $result;
}
private function buildAlbumTree(array &$result, $allAlbums, Album $parent = null, $level = 0)
{
$parentAlbums = [];
foreach ($allAlbums as $album)
{
if ((is_null($parent) && is_null($album->parent_album_id)) || (!is_null($parent) && $album->parent_album_id == $parent->id))
{
$parentAlbums[] = $album;
}
}
foreach ($parentAlbums as $album)
{
$album->display_name = trim(sprintf('%s %s', str_repeat('-', $level), $album->name));
$result[$album->id] = $album;
$this->buildAlbumTree($result, $allAlbums, $album, $level + 1);
}
}
}