<?php
$get  = $output[2];
$page = $output[1];
$atoz = false;
$pagelimit = FILES_PER_PAGE;
$db->pageLimit = $pagelimit;
$files = array();
$categories = array();
if ($get == "latest-songs") {
    $name = "Latest";
    $title = "Tonuri de Apel Noi - Sonerii Telefon 2022 " . APP_NAME;
    $heading = "Tonuri De Apel Recente ";
    $pageLink = APP_URL . '/musics/{{PAGE}}/latest-songs' . $endRoute;
    $db->join(TABLE_CAT . ' c', 'f.cid=c.id', "INNER");
    $files = $db->where('f.deleted_at',NULL,'IS')->where('f.approved_at', NULL, 'IS NOT')->where('f.status', 1)->where('newtag', 1)->orderBy('f.id')->paginate(TABLE_FILES . ' f', $page, 'f.name,f.dname,f.id,f.ext,f.slug,f.thumb,f.duration,f.download,c.folder');
    $totalPage = $db->totalPages;
} elseif ($get == "top-songs") {
    $name = "Top Ringtones";
    $title = "Top Ringtones - " . APP_NAME;
    $heading = "All $name Ringtones";
    $pageLink = APP_URL . '/musics/{{PAGE}}/top-songs' . $endRoute;
    $db->join(TABLE_CAT . ' c', 'f.cid=c.id', "INNER");
    $files = $db->where('f.deleted_at',NULL,'IS')->where('f.approved_at', NULL, 'IS NOT')->where('f.status', 1)->orderBy('f.download')->paginate(TABLE_FILES . ' f', $page, 'f.name,f.id,f.slug,f.thumb,f.dname,f.ext,f.duration,f.download,c.folder');
    $totalPage = $db->totalPages;
} elseif ($get == "trending") {
    $name = "Trending";
    $heading = "All $name Ringtones";
    $title = "Trending Ringtones - " . APP_NAME;
    $pageLink = APP_URL . '/musics/{{PAGE}}/trending' . $endRoute;

    $db->join(TABLE_CAT . ' c', 'f.cid=c.id', "INNER");
    $files = $db->where('f.deleted_at',NULL,'IS')->where('f.approved_at', NULL, 'IS NOT')->where('f.status', 1)->where('trend', 1)->orderBy('f.id')->paginate(TABLE_FILES . ' f', $page, 'f.name,f.id,f.slug,f.dname,f.ext,f.thumb,f.duration,f.download,c.folder');
    $totalPage = $db->totalPages;
} else if ($get == "latest-albums") {
    $name = "Latest Albums and Movies";
    $heading = "All $name Ringtones";
    $title = "Latest Albums and Ringtones - " . APP_NAME;
    $pageLink = APP_URL . '/musics/{{PAGE}}/latest-albums' . $endRoute;
    $categories = $db->where('newitemtag', 1)->where('status', 1)->where('is_cat', false)->orderBy('id')->paginate(TABLE_CAT . ' c', $page, 'c.name,c.slug,c.thumb,c.is_cat,c.release_year,c.totalitem');
    $totalPage = $db->totalPages;
} else if ($get == "my-uploads" && $isLogged) {
    $name = "My Uploads";
    $heading = "My Uploads - " . $AuthUser['name'];
    $title = "My Uploads - " . $AuthUser['name'] . " - " . APP_NAME;
    $pageLink = APP_URL . '/musics/{{PAGE}}/my-uploads' . $endRoute;

    $db->join(TABLE_CAT . ' c', 'f.cid=c.id', "INNER");
    $files = $db->where('f.deleted_at',NULL,'IS')->where('(f.approved_at IS NOT NULL OR f.user_id="' . $AuthUser['id'] . '")')->where('f.status', 1)->where('user_id', $AuthUser['id'])->orderBy('f.id')->paginate(TABLE_FILES . ' f', $page, 'f.name,f.id,f.slug,f.thumb,f.dname,f.ext,f.duration,f.download,c.folder');
    $totalPage = $db->totalPages;
} else if ($get == "my-favourites" && $isLogged) {

    $name = "My Favourites";
    $heading = "My Favourites - " . $AuthUser['name'];
    $title = "My Favourites - " . $AuthUser['name'] . " - " . APP_NAME;
    $pageLink = APP_URL . '/musics/{{PAGE}}/my-favourites' . $endRoute;
    $db->join(TABLE_CAT . ' c', 'f.cid=c.id', "INNER");
    $db->join('favourite fav', 'f.id=fav.file_id', "INNER");

    if ($isLogged) {
        $db->where('(f.approved_at IS NOT NULL OR f.user_id="' . $AuthUser['id'] . '")');
      } else {
        $db->where('f.approved_at', NULL, 'IS NOT');
      }
    $files = $db->where('f.deleted_at',NULL,'IS')->where('fav.user_id', $AuthUser['id'])->where('f.status', 1)->orderBy('fav.id')->paginate(TABLE_FILES . ' f', $page, 'f.*,c.name as category_name,c.thumb as category_thumb,c.folder');
    $totalPage = $db->totalPages;
} else {
    $UserInfo = $db->where('user_name', $get)->getOne('users');
    if (!$UserInfo) {
        if (ERROR_PAGE == true) {
            require __DIR__ . '/ErrorPage.php';
        } else {
            Redirect(APP_URL);
        }
        exit;
    }

    $name = $UserInfo['name'];
    $heading = $UserInfo['name'] . " - Uploads";
    $title =  $UserInfo['name'] . " - Uploaded All Ringtones - " . APP_NAME;
    $pageLink = APP_URL . '/musics/{{PAGE}}/' . $get . $endRoute;
    $db->join(TABLE_CAT . ' c', 'f.cid=c.id', "INNER");

    if ($isLogged) {
        if ($UserInfo['id'] == $AuthUser['id']) {
            $db->where('(f.approved_at IS NOT NULL OR f.user_id="' . $AuthUser['id'] . '")');
        } else {
            $db->where('f.approved_at', NULL, 'IS NOT');
        }
    } else {
        $db->where('f.approved_at', NULL, 'IS NOT');
    }

    $files = $db->where('f.status', 1)->where('f.deleted_at',NULL,'IS')->where('user_id', $UserInfo['id'])->orderBy('f.id')->paginate(TABLE_FILES . ' f', $page, 'f.name,f.dname,f.id,f.ext,f.slug,f.thumb,f.duration,f.download,c.folder');
    $totalPage = $db->totalPages;
}
$YouMayFiles = $db
    ->join(TABLE_CAT . ' c', 'f.cid=c.id', "INNER")
    ->orderBy('RAND()')
    ->where('f.approved_at', NULL, 'IS NOT')
    ->where('f.deleted_at',NULL,'IS')
    ->where('(f.trend="1" OR f.newtag="1")')->where('f.status', 1)->get(TABLE_FILES . ' f', 4, 'f.name,f.id,f.slug,f.thumb,f.duration,f.dname,f.ext,c.folder,f.download');
$breadhtml = '<nav aria-label="breadcrumb"><ol class="breadcrumb mt-2"><li class="breadcrumb-item"><a href="' . APP_URL . '"><i class="fa fa-home" aria-hidden="true"></i> Home</a></li>';
$breadcrumb[] =  [
    "@type" => "ListItem",
    "position" => 2,
    "item" => [
        "@id" => $AppProtocol . $request_uri,
        "name" => $name
    ]
];
$breadhtml .= '<li class="breadcrumb-item"><a href="' . $AppProtocol . $request_uri . '">' . $name . '</a></li>';
$breadhtml .= '</ol></nav>';
$schema = [
    "@context" => "https://schema.org",
    "@type" => "BreadcrumbList",
    "itemListElement" => $breadcrumb
];

$metakeywords = '';
require_once $Template . '/header.php';
require_once $Template . '/AllSongs.php';
require_once $Template . '/footer.php';
