<?php

/* DCMS Special
 * Дата последнего редактирования 24.01.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 = mysql_fetch_array(mysql_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($_GET['del']) && $_GET['del'] == 'del' && intval($_GET['foto'])) {
        if ($user['id'] == $public['id_user']) {
            $f = mysql_fetch_array(mysql_query("SELECT * FROM `public_foto` WHERE `id` = '" . intval($_GET['foto']) . ")'"));
            mysql_query("DELETE FROM `public_foto` WHERE `id` = '" . $f['id'] . "'");
            @unlink(H . "sys/public/foto/600/" . $f['id'] . ".jpg");
            @unlink(H . "sys/public/foto/foto/" . $f['id'] . ".jpg");
            $_SESSION['message'] = lang('Фотография успешно удалена');
            header("Location: foto.php?id=" . $public['id'] . "");
        } else {
            $err = lang('Вы не имеете права удалять фотографии');
        }
    }


    if (isset($_GET['like']) && $_GET['like'] == 'dislike' && intval($_GET['foto'])) {
        mysql_query("DELETE FROM `public_like` WHERE `id_user` = '" . $user['id'] . "' AND `pub_foto` = '" . intval($_GET['foto']) . "'");
        mysql_query("OPTIMIZE TABLE `public_like`");
        msg('Лайк отменен');
        header("Location: foto.php?id=" . $public['id'] . "");
    } elseif (isset($_GET['like']) && $_GET['like'] == 'like' && intval($_GET['foto'])) {
        mysql_query("INSERT INTO `public_like` (`id_user`, `pub_foto`, `like`) values('" . $user['id'] . "', '" . intval($_GET['foto']) . "', '+1')", $db);
        mysql_query("INSERT INTO `public_wall` (`id_wall`, `link`, `ras`, `name`, `id_public`) values('" . $user['id'] . "', '/sys/public/foto/600/" . intval($_GET['foto']) . ".jpg', 'jpg', '" . $foto['name'] . "', '" . $public['id'] . "')");
        mysql_query("OPTIMIZE TABLE `public_like`");
        mysql_query("OPTIMIZE TABLE `public_wall`");
        msg('Вам понравилось');
        header("Location: foto.php?id=" . $public['id'] . "");
    }

    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 />";

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


    #Кнопка добавления фотографий, если разрешено
    if ($public['p'] == 0 || $user['id'] == $public['id_user']) {
        if ($user['id'] == $no['id_user']) {
            echo "<div class='list-group-item-null'>";
            echo "<div class='btn btn-secondary btn-sm'><a href='edit.php?id=" . $public['id'] . "&amp;foto=add'><i class='fa fa-camera fa-fw'></i> " . lang('Добавить фотографию') . "</a></div>";
            echo "</div>";
        }
    }


    $fotki = mysql_result(mysql_query("SELECT COUNT(*) FROM `public_foto` WHERE `id_public` = '" . $public['id'] . "' AND `foto` = '2'"), 0);
    echo "<div class='list-group-item-null list-group-item-grey'>";
    echo "<i class='fa fa-camera-retro fa-fw'></i> " . lang('Фотографии') . " <small style='color: grey;'>$fotki</small>";
    echo "</div>";
    ?>
    <script src="/style/js/jquery.js"></script>
    <script>
        $(document).ready(function () {
            $('span#like').click(function () {
                setVote('like', $(this));
            });
        });

        function setVote(type, element) {
            var go = $('#go').val();
            var us = $('#us').val();
            var id_post = element.parent().find('#id_post').val();

            $.ajax({
                type: "POST",
                url: "/modules/public/like.php?pub_foto=pub_foto",
                data: {
                    'go': go,
                    'id_post': id_post,
                    'us': us
                },
                dataType: "json",
                success: function (data) {
                    if (data.result == 'like') {
                        var count = parseInt(element.find('b').html());
                        element.find('b').html(count + 1);
                    } else {
                        var count = parseInt(element.find('b').html());
                        element.find('b').html(count - 1);
                    }
                }
            });
        }
    </script>
    <?php

    $k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `public_foto` WHERE `id_public` = '" . $public['id'] . "'"), 0);
    $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();
    }

    $f = mysql_query("SELECT id,ras FROM `public_foto` WHERE `id_public` = '" . $public['id'] . "' AND `foto` = '2' LIMIT $start, " . $set['p_str'] . "");
    while ($foto = mysql_fetch_array($f)) {
        echo "<table class='list-group-item-null'><tr><td class='icon14'>";

        echo "<img src='/sys/public/foto/600/" . $foto['id'] . ".jpg' style='width: 60%;' />";

        echo "<br /><br />";

        $us = $user['id'];
        $go = 'go';

        $like_f = mysql_result(mysql_query("SELECT COUNT(*) FROM `public_like` WHERE `pub_foto` = '" . $foto['id'] . "'"), 0);
        if (mysql_result(mysql_query("SELECT COUNT(*) FROM `public_like` WHERE `id_user` = '" . $user['id'] . "' AND `pub_foto` = '" . $foto['id'] . "'"), 0) == 0) {
            if (!IS_WEB) {
                echo "<a href='foto.php?id=" . $public['id'] . "&amp;like=like&amp;foto=" . $foto['id'] . "'><i class='fa fa-heart-o fa-fw'></i> " . lang('Мне нравится') . " " . (($like_f > 0) ? "<b>$like_f</b>" : "<b>$like_f</b>") . "</a> &#183; ";
            } else {
                echo "<span id='like'><a class='cursor'><i class='fa fa-heart-o fa-fw'></i> " . lang('Мне нравится') . " " . (($like_f > 0) ? "<b>$like_f</b>" : "<b>$like_f</b>") . "</a></span> &#183; ";
                echo '<input type="hidden" id="id_post" value="' . $foto['id'] . '" />';
                echo '<input type="hidden" id="go" value="' . $go . '" />';
                echo '<input type="hidden" id="us" value="' . $user['id'] . '" />';
            }
        } else {
            if (!IS_WEB) {
                echo "<a href='foto.php?id=" . $public['id'] . "&amp;like=dislike&amp;foto=" . $foto['id'] . "'><i class='fa fa-heart fa-fw'></i> " . lang('Мне нравится') . " " . (($like_f > 0) ? "<b>$like_f</b>" : "<b>$like_f</b>") . "</a> &#183; ";
            } else {
                echo "<span id='like'><a class='cursor'><i class='fa fa-heart fa-fw'></i> " . lang('Мне нравится') . " " . (($like_f > 0) ? "<b>$like_f</b>" : "<b>$like_f</b>") . "</a></span> &#183; ";
                echo '<input type="hidden" id="id_post" value="' . $foto['id'] . '" />';
                echo '<input type="hidden" id="go" value="' . $go . '" />';
                echo '<input type="hidden" id="us" value="' . $user['id'] . '" />';
            }
        }

        echo "<a href='/sys/public/foto/foto/" . $foto['id'] . ".jpg'><i class='fa fa-arrows-alt fa-fw'></i> " . lang('Открыть') . "</a>";
        if ($user['id'] == $public['id_user']) {
            echo " &#183; <a href='foto.php?id=" . $public['id'] . "&amp;del=del&amp;foto=" . $foto['id'] . "'><i class='fa fa-trash-o fa-fw'></i> " . lang('Удалить') . "</a>";
        }
        echo "</td></tr></table><div class='hr'></div>";
    }

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