<?php

/* DCMS Special
 * Дата последнего редактирования 29.09.2016
 * Модифицировал densnet
 */

foreach (array('start', 'compress', 'sess', 'settings', 'db_connect', 'ipua', 'fnc', 'user') as $inc) {
    require_once "../../sys/inc/$inc.php";
}

if (isset($_GET['id'])) {
    $public['id'] = intval($_GET['id']);
}
$public = $db->get_array($db->query("SELECT * FROM `public` WHERE `id` = '" . $public['id'] . ")'"));
if ($public['id'] == intval($_GET['id'])) {
    $set['title'] = lang('Музыка') . " - " . $public['name'];
    require_once H . 'sys/inc/thead.php';

    if (isset($user) && $user['id'] == $public['id_user']) {
        if (isset($_GET['del']) && $_GET['del'] == 'del' && intval($_GET['post'])) {
            $pubic = $db->get_array($db->query("SELECT * FROM `public_audio` WHERE `id` = '" . intval($_GET['post']) . "' LIMIT 1"));
            $db->query("DELETE FROM `public_audio` WHERE `id` = '" . intval($_GET['post']) . "'");
            @unlink(H . "sys/public/music/" . $pubic['id'] . ".mp3");
            $_SESSION['message'] = lang('Аудиозапись успешно удалена');
            header("Location: audio.php?id=" . $public['id'] . "");
            exit();
        }
    }

    aut();
    err();

    #Навигация
    echo "<div class='list-group-item-null list-group-item-grey'><small>";
    echo "<a href='/' class='hint--right' data-hint='" . lang('На главную') . "'><i class='fa fa-home fa-lg'></i></a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo "<a href='/public.php'>" . lang('Сообщества') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo "<a href='index.php?id=$public[id]'>" . toOutput($public['name']) . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo lang('Аудиозаписи');
    echo "</small></div><br />";

    if (isset($user) && $user['id'] == $public['id_user']) {
        if (isset($_GET['del']) && $_GET['del'] == 'delete' && intval($_GET['row'])) {
            $publ = $db->get_array($db->query("SELECT * FROM `public_audio` WHERE `id` = '" . intval($_GET['row']) . "' LIMIT 1"));
            echo "<div class='list-group-item-null list-group-item-warning'>" . lang('Подтвердите удаление') . " " . $publ['name'] . "<br />";
            echo "<a class='btn btn-success btn-sm' href='audio.php?id=" . $public['id'] . "&amp;del=del&amp;post=" . $publ['id'] . "'><i class='fa fa-trash-o fa-fw'></i> " . lang('Удалить') . "</a> ";
            echo "<a class='btn btn-secondary btn-sm' href='audio.php?id=" . $public['id'] . "'>" . lang('Отмена') . "</a>";
            echo "</div>";
        }
    }

    $ank = $db->get_array($db->query("SELECT * FROM `public_coint` WHERE `id_public` = '" . $public['id'] . "' AND `id_user` = '" . $user['id'] . "' LIMIT 1"));
    $no = $db->get_array($db->query("SELECT * FROM `public_coint` WHERE `id_public` = '" . $public['id'] . "' AND `id_user` = '" . $user['id'] . "'"), 0);

    if ($user['id'] == $no['id_user']) {
        if (isset($user) && $user['id'] == $public['id_user'] || $public['a'] == '0') {
            echo "<div class='list-group-item-null'>";
            echo "<div class='btn btn-secondary btn-sm'><a href='edit.php?id=" . $public['id'] . "&amp;audio=add'><i class='fa fa-music fa-fw'></i> " . lang('Добавить аудиозапись') . "</a></div>";
            echo "</div>";
        }
    }

    $audio = $db->super_query("SELECT COUNT(*) as count FROM `public_audio` WHERE `id_public` = '$public[id]'");
    $audio = $audio['count'];
    echo "<div class='list-group-item-null list-group-item-grey'>";
    echo "<i class='fa fa-music fa-fw'></i> " . lang('Аудиозаписи') . " <small style='color: grey;'>$audio</small>";
    echo "</div>";

    $k_post = $db->super_query("SELECT COUNT(*) as count FROM `public_audio` WHERE `id_public` = '$public[id]'");
    $k_post = $k_post['count'];
    $k_page = k_page($k_post, $set['p_str']);
    $page = page($k_page);
    $start = $set['p_str'] * $page - $set['p_str'];

    if ($k_post == 0) {
        $doc->NoResult();
    }

    $q = $db->query("SELECT id,name FROM `public_audio` WHERE `id_public` = '" . $public['id'] . "' ORDER BY id DESC LIMIT $start, " . $set['p_str'] . "");
    while ($music = $db->get_array($q)) {
        echo "<div class='list-group-item-null'>";
        echo "<span style='float: right;'>";
        if (isset($user) && $user['id'] == $public['id_user']) {
            echo "<a href='edit.php?id=" . $public['id'] . "&amp;audio=audio&amp;row=" . $music['id'] . "' class='hint--left' data-hint='" . lang('Редактировать') . "'><i class='fa fa-edit fa-fw'></i></a> ";
            echo " <a href='audio.php?id=" . $public['id'] . "&amp;del=delete&amp;row=" . $music['id'] . "' class='hint--left' data-hint='" . lang('Удалить') . "'><i class='fa fa-trash-o fa-fw'></i></a>";
        }
        echo "</span>";
        echo "<i class='fa fa-music fa-fw'></i> " . toOutput($music['name']) . "<br />";
        echo '<center>
     <audio controls="controls" style="width:100%">
    <source src="/sys/public/music/' . $music['id'] . '.mp3" type="audio/mpeg">
    Тег audio не поддерживается вашим браузером. 
  </audio>
  </center>';
        echo "</div>";
        echo "<div class='hr'></div>";
    }

    if ($k_page > 1) {
        str("audio.php?id=" . $public['id'] . "&amp;", $k_page, $page);
    }
}
require_once H . 'sys/inc/tfoot.php';
