<?php
if ($output2) {
    $id = $output2[3];
    $page = $output2[2];
    $sort = 'default';
    $curType = $output2[1];
} else {
    $id = $output[2];
    $page = 1;
    $sort = 'default';
    $curType = $output[1];
}
unset($output, $output2);
if ($data = $db->where('old_slug', $id)->getOne(TABLE_CAT, 'slug')) {
    Redirect(APP_URL . '/categorie/' . $data['slug']);
    exit;
}
/*browse category code*/
if($id!="list") {
$category = $db->where('slug', $id)->getOne(TABLE_CAT);
}
else {
    $category = [
        'id' => 0,
        'name' => 'Categorii Tonuri de Apel',
        'thumb' => '',
        'release_year' => '',
        'slug' => 'list',
        'folder' => 'upload_file',
        'subcate' => 1000,
        'parentid' => 0,
        'is_cat' => 1,
        'meta_title' => '',
        'meta_des' => '',
        'meta_keyw'=> ''
    ];
}
/*end browse category code*/
$catThumb = '';
if (!$category) {
    if (ERROR_PAGE == true) {
        require __DIR__ . '/ErrorPage.php';
    } else {
        Redirect(APP_URL);
    }
    exit;
}
$ParentCat = array();
$categories = array();
$files = array();
$sort = "category";
switch ($sort) {
    case "download":
        $orderby = array('download', 'DESC');
        break;
    case "az":
        $orderby = array('name', 'ASC');
        break;
    case "new":
        $orderby = array('id', 'DESC');
        break;
    default:
        $orderby = array('pos', 'ASC');
}
$YouMayFiles = array();
$YouMayMovies = array();
if ($category['subcate'] == 0) {

    $pagelimit = FILES_PER_PAGE;
    $db->pageLimit = $pagelimit;
    //
    $db->join(TABLE_CAT . ' c', 'f.cid=c.id', "INNER");
    $db->orderBy('f.' . $orderby[0], $orderby[1]);
    $db->orderBy('f.id');
    $db->where('f.cid', $category['id']);
    $db->where('f.status', 1);
    $files = $db->where('f.deleted_at',NULL,'IS')->where('f.approved_at', NULL, 'IS NOT')->paginate(TABLE_FILES . ' f', $page,  'f.slug,f.id,f.dname,f.ext,f.thumb,f.name,f.duration,f.download,c.name as category_name,c.folder,c.thumb as category_thumb');
    $totalPage = $db->totalPages;
    $sq = $db->subQuery();
    $sq->where('parentid', $category['parentid']);
    $sqIds =  $sq->get(TABLE_CAT, null, 'id');
    $YouMayFiles = $db
        ->join(TABLE_CAT . ' c', 'f.cid=c.id', "INNER")
        ->orderBy('RAND()')
        ->where('f.deleted_at',NULL,'IS')
        ->where('f.approved_at', NULL, 'IS NOT')
        ->where('f.status', 1)->where('f.cid', $sqIds, 'IN')->get(TABLE_FILES . ' f', 4, 'f.name,f.slug,f.id,f.dname,f.ext,f.thumb,f.duration,f.download,c.folder');

    if (!$YouMayFiles) {
        $YouMayFiles = $db
            ->join(TABLE_CAT . ' c', 'f.cid=c.id', "INNER")
            ->where('f.deleted_at',NULL,'IS')
            ->orderBy('RAND()')
             ->where('f.approved_at', NULL, 'IS NOT')
            ->where('f.status', 1)->get(TABLE_FILES . ' f', 8, 'f.name,f.slug,f.id,f.thumb,f.duration,f.download,c.folder');
    }
} else {
    $pagelimit = CATEGORY_PER_PAGE;
    $db->pageLimit = $pagelimit;
    $db->where('status', 1);
    $db->where('parentid', $category['id']);
    $db->orderBy($orderby[0], $orderby[1]);
    $categories  = $db->paginate(TABLE_CAT . ' c', $page, 'c.id,c.slug,c.name,c.folder,c.totalitem,c.thumb,c.is_cat,c.release_year');
    $totalPage = $db->totalPages;
    $relids = array($category['id']);
    if ($categories) {
        $relids = array_merge($relids, array_column($categories, 'id'));
    }
    $YouMayMovies =  $db->where('id', $relids, 'NOT IN')->where('parentid', $category['id'])->where('status', 1)->get(TABLE_CAT, 10, 'name,slug,thumb,totalitem,is_cat,release_year');
}

if($category['is_cat']==1){
    $title = $category['meta_title'] ? $category['meta_title'] : str_replace(array('{{CATEGORY_NAME}}'), array($category['name']), $lang['CATLIST_TITLE']);
    $metadescription = $category['meta_des'] ? $category['meta_des'] : str_replace(array('{{CATEGORY_NAME}}'), array($category['name']), $lang['CATLIST_META_DESCRIPTION']);
    $metakeywords = $category['meta_keyw'] ? $category['meta_keyw'] : str_replace(array('{{CATEGORY_NAME}}'), array($category['name']), $lang['CATLIST_META_KEYWORDS']);
    $autotags = str_replace(array('{{CATEGORY_NAME}}'), array($category['name']), $lang['CATLIST_AUTO_TAG']);
    $heading = title($category['name']);
} else {
    $title = $category['meta_title'] ? $category['meta_title'] : str_replace(array('{{CATEGORY_NAME}}', '{{RELEASE_YEAR}}'), array($category['name'], $category['release_year']), $lang['FILELIST_TITLE']);
    $metadescription = $category['meta_des'] ? $category['meta_des'] : str_replace(array('{{CATEGORY_NAME}}', '{{RELEASE_YEAR}}'), array($category['name'], $category['release_year']), $lang['FILELIST_META_DESCRIPTION']);
    $metakeywords = $category['meta_keyw'] ? $category['meta_keyw'] : str_replace(array('{{CATEGORY_NAME}}', '{{RELEASE_YEAR}}'), array($category['name'], $category['release_year']), $lang['FILELIST_META_KEYWORDS']);
    $autotags = str_replace(array('{{CATEGORY_NAME}}', '{{RELEASE_YEAR}}'), array($category['name'], $category['release_year']), $lang['FILELIST_AUTO_TAG']);
    $heading = title($category['name']);
    $heading .= ($category['release_year']) ? ' (' . $category['release_year'] . ') Mp3 Songs' : '';
}
$title = title($title) . ' 2022';
$metadescription = title($metadescription);
$metakeywords = title($metakeywords);
$pageLink = APP_URL . '/' . $curType . '/{{PAGE}}/' . $category['slug'] . $endRoute;
$parentids = array_filter(explode('/', $category['folder']));
array_shift($parentids);
array_pop($parentids);
$bpos = 1;
$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>';
if ($parentids) {
        $breadcrumbs = $db->where('id', $parentids, 'IN')->orderBy('id', 'ASC', $parentids)->get(TABLE_CAT, null, 'name,slug,is_cat,release_year');
    $bcnt = $db->count;
    foreach ($breadcrumbs as $bread) {
        $bpos++;
        if($bread['is_cat'] == 1){
            $bRoute = 'category';
            $bname =  $bread['name'];
        } else {
            $bRoute = 'album';
            $bname =  ($bread['release_year']) ? $bread['name'] . ' (' . $bread['release_year'] . ')' : $bread['name'];
        }
        $breadcrumb[] =  [
            "@type" => "ListItem",
            "position" => $bpos,
            "item" => [
                "@id" => APP_URL . '/' . $bRoute . '/' . $bread['slug'] . $endRoute,
                "name" => $bname
            ]
        ];
        $breadhtml .= '<li class="breadcrumb-item"><a href="' . APP_URL . '/' . $bRoute . '/' . $bread['slug'] . $endRoute . '">' . $bname . '</a></li>';
   
        if(($bpos-1)==$bcnt){
            $ParentCat = ['slug'=> $bRoute . '/' . $bread['slug'],'name'=>$bname];
        }
    }
}
$cNameYear = ($category['release_year']) ? $category ['name'].' (' . $category['release_year'] . ')' : $category['name'];
$breadcrumb[] =  [
    "@type" => "ListItem",
    "position" => $bpos + 1,
    "item" => [
        "@id" =>  $AppProtocol . $request_uri,
        "name" =>  $cNameYear
    ]
];
// $breadhtml .= '<li class="breadcrumb-item"><a href="' . $AppProtocol . $request_uri  . '">' . $cNameYear . '</a></li>';
$breadhtml .= '</ol></nav>';
$schema = [
    "@context" => "https://schema.org",
    "@type" => "BreadcrumbList",
    "itemListElement" => $breadcrumb
];

if ($category['thumb']) {
    $catThumb = APP_URL . '/upload_file/folderthumb/' . PREVIEW_THUMB . '/' . $category['thumb'];
    if (THUMB_OGIMAGE == 1)
        $metaogimage = APP_URL . '/upload_file/folderthumb/' . META_OG_THUMB . '/' . $category['thumb'];
}

$thumbWH = explode('x', PREVIEW_THUMB);
require_once $Template . '/header.php';
require_once $Template . '/Category.php';
require_once $Template . '/footer.php';
