<?php
$id = $output[1];
unset($output, $output2);
if ($data = $db->where('old_slug', $id)->getOne(TABLE_FILES, 'slug')) {
  Redirect(APP_URL . '/' . $data['slug']);
  exit;
}
$db->join(TABLE_CAT . ' c', 'f.cid=c.id', "INNER");
$db->join('users u','f.user_id=u.id',"LEFT");
$db->where('f.slug', $id);
$db->where('f.status', 1);
$db->where('f.deleted_at',NULL,'IS');

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');
}

$selectData = 'f.*,u.user_name,u.name as user_fullname,c.name as category_name,c.folder,c.thumb as category_thumb,c.slug as category_slug,c.parentid as c_parentid,c.is_cat,c.release_year';
$file = $db->getOne(TABLE_FILES . ' f', $selectData);
if (!$file) {
  if (ERROR_PAGE == true) {
    require __DIR__ . '/ErrorPage.php';
  } else {
    Redirect(APP_URL);
  }
  exit;
}
if ($file['is_cat'] == 1) {
  $catLabel = 'Categorye';
  $catRoute = 'categorie';
} else {
  $catLabel = 'Album/Movie';
  $catRoute = 'album';
}

$releasedate = date('d, M Y', strtotime($file['created_at']));
$parentids = array_filter(explode('/', $file['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');
  foreach ($breadcrumbs as $bread) {
    $bpos++;
    if ($bread['is_cat'] == 1) {
      $bRoute = 'categorie';
      $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>';
  }
}
$cNameYear = ($file['release_year']) ? $file['category_name'] . ' (' . $file['release_year'] . ')' : $file['category_name'];
$bpos++;
$breadcrumb[] =  [
  "@type" => "ListItem",
  "position" => $bpos,
  "item" => [
    "@id" =>  APP_URL . '/' . $catRoute . '/' . $file['category_slug'] . $endRoute,
    "name" => $cNameYear
  ]
];
$bpos++;
$breadcrumb[] =  [
  "@type" => "ListItem",
  "position" => $bpos,
  "item" => [
    "@id" =>  APP_URL . '/' . $file['slug'] . $endRoute,
    "name" =>  $file['name']
  ]
];
$breadhtml .= '<li class="breadcrumb-item"><a href="' . APP_URL . '/' . $catRoute . '/' . $file['category_slug'] . $endRoute  . '">' . $cNameYear . '</a></li>';
$breadhtml .= '</ol></nav>';
$schema = [
  "@context" => "https://schema.org",
  "@type" => "BreadcrumbList",
  "itemListElement" => $breadcrumb
];
$arists = '';
if ($file['artist']) {
  $aristss = explode(',', $file['artist']);
  array_filter($aristss);
  foreach ($aristss as $ar)
    $arists .= '<a href="' . APP_URL . '/artist/' . slug($ar) . $endRoute . '">' . $ar . '</a> ,';
}
$extension = pathinfo($file['dname'], PATHINFO_EXTENSION);
$filename = $extension ? $file['dname'] : $file['dname'] . '.' . $file['ext'];
$media_source = $file['folder'] . $filename;
$downLink = true;
$db->join(TABLE_CAT . ' c', 'f.cid=c.id', "INNER");
$db->orderBy('RAND()');
$db->where('f.cid', $file['cid']);
$db->where('f.id', $file['id'], '!=');
$ReletedFiles = $db->where('f.deleted_at',NULL,'IS')->where('f.approved_at', NULL, 'IS NOT')->where('f.status',1)->get(TABLE_FILES . ' f', RELETED_FILES_LIMIT, 'f.id,f.thumb,f.dname,f.ext,f.duration,f.name,f.slug,f.created_at,f.download,c.folder');

$relids = array($file['id']);
if ($ReletedFiles) {
  $relids = array_merge($relids, array_column($ReletedFiles, 'id'));
}
$sq = $db->subQuery();
$sq->where('parentid', $file['c_parentid']);
$sqIds =  $sq->get(TABLE_CAT, null, 'id');
$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.created_at',$releasedate.'%','LIKE')
  ->where('f.id', $relids, 'NOT IN')
  ->where('f.status', 1)->where('f.cid', $sqIds, 'IN')->get(TABLE_FILES . ' f', 4, 'f.name,f.id,f.slug,f.dname,f.ext,f.thumb,f.duration,f.download,f.created_at,c.folder');
unset($relids);
$ReletedAlbums = $db->where('id', $file['cid'], '!=')->where('parentid', $file['c_parentid'])->where('status', 1)->orderBy('Rand()')->get(TABLE_CAT . ' c', RELETED_FILES_LIMIT, 'c.name,c.slug,c.thumb,c.is_cat,c.release_year,c.totalitem');
$ReletedArists = $db->where('status', 1)->orderBy('RAND()')->get('artist a', 4, 'a.name,a.slug,a.thumb');
$PREVIEW_THUMB = '230x230';
$thumbWH = explode('x', $PREVIEW_THUMB);
if ($file['thumb']) {
  $thumb = APP_URL . '/' . $file['folder'] . $PREVIEW_THUMB . '/' . $file['thumb'];
  if (THUMB_OGIMAGE == 1)
    $metaogimage = APP_URL . '/' . $file['folder'] . $PREVIEW_THUMB . '/' . $file['thumb'];
} else {
  $thumb = APP_URL . '/assets/images/music.svg';
}
$title = $file['meta_title'] ? $file['meta_title'] : str_replace(array('{{FILENAME}}', '{{CATEGORY_NAME}}', '{{RELEASE_DATE}}'), array($file['name'], $cNameYear, $releasedate), $lang['FILE_TITLE']);
$metadescription = $file['meta_des'] ? $file['meta_des'] : str_replace(array('{{FILENAME}}', '{{CATEGORY_NAME}}', '{{RELEASE_DATE}}'), array($file['name'], $cNameYear, $releasedate), $lang['FILE_META_DESCRIPTION']);
$metakeywords = $file['meta_keyw'] ? $file['meta_keyw'] : str_replace(array('{{FILENAME}}', '{{CATEGORY_NAME}}', '{{RELEASE_DATE}}'), array($file['name'], $cNameYear, $releasedate), $lang['FILE_META_KEYWORDS']);
$autotags = str_replace(array('{{FILENAME}}', '{{CATEGORY_NAME}}', '{{RELEASE_DATE}}'), array($file['name'], $cNameYear, $releasedate), $lang['FILE_AUTO_TAG']);

$title = title($title) . '';
$metadescription = title($metadescription);
$metakeywords = title($metakeywords);
$Route = 'FILE';
require_once $Template . '/header.php';
require_once $Template . '/File.php';
require_once $Template . '/footer.php';
