<?php

/*!
     * ifsoft.co.uk v1.1
     *
     * http://ifsoft.com.ua, http://ifsoft.co.uk
     * qascript@mail.ru
     *
     * Copyright 2012-2017 Demyanchuk Dmitry (https://vk.com/dmitry.demyanchuk)
     */

    if (!$auth->authorize(auth::getCurrentUserId(), auth::getAccessToken())) {

        header('Location: /');
    }

    $profile = new profile($dbo, auth::getCurrentUserId());

    if (isset($_GET['action'])) {

        $notifications = new notify($dbo);
        $notifications->setRequestFrom(auth::getCurrentUserId());

        $notifications_count = $notifications->getNewCount($profile->getLastNotifyView());

        echo $notifications_count;
        exit;
    }

    $profile->setLastNotifyView();

    $notifications = new notify($dbo);
    $notifications->setRequestFrom(auth::getCurrentUserId());

    $notifications_all = $notifications->getAllCount();
    $notifications_loaded = 0;

    if (!empty($_POST)) {

        $notifyId = isset($_POST['notifyId']) ? $_POST['notifyId'] : 0;
        $loaded = isset($_POST['loaded']) ? $_POST['loaded'] : 0;

        $notifyId = helper::clearInt($notifyId);
        $loaded = helper::clearInt($loaded);

        $result = $notifications->getAll($notifyId);

        $notifications_loaded = count($result['notifications']);

        $result['notifications_loaded'] = $notifications_loaded + $loaded;
        $result['answers_all'] = $notifications_all;

        if ($notifications_loaded != 0) {

            ob_start();

            foreach ($result['notifications'] as $key => $value) {

                draw($value, $LANG, $helper);
            }

            $result['html'] = ob_get_clean();

            if ($result['notifications_loaded'] < $notifications_all) {

                ob_start();

                ?>

                <header class="top-banner loading-banner">

                    <div class="prompt">
                        <button onclick="Notifications.moreAll('<?php echo $result['notifyId']; ?>'); return false;" class="button green loading-button"><?php echo $LANG['action-more']; ?></button>
                    </div>

                </header>

                <?php

                $result['banner'] = ob_get_clean();
            }
        }

        echo json_encode($result);
        exit;
    }

    $page_id = "notifications";

    $css_files = array("main.css", "my.css");
    $page_title = $LANG['page-notifications-likes']." | ".APP_TITLE;

    include_once("../html/common/header.inc.php");

?>

<body class="job-listings">


    <?php
        include_once("../html/common/topbar.inc.php");
    ?>


    <div class="wrap content-page">

        <div class="main-column">

            <div class="main-content">

                <div class="job-listings-page content-list-page">

                    <header class="top-banner">

                        <div class="info">
                            <h1><?php echo $LANG['page-notifications-likes']; ?></h1>
                            <p><?php echo $LANG['label-notifications-sub-title']; ?></p>
                        </div>

                    </header>

                    <?php

                    $result = $notifications->getAll(0);

                    $notifications_loaded = count($result['notifications']);

                    if ($notifications_loaded != 0) {

                        ?>

                            <ul class="items-list content-list">

                                <?php

                                    foreach ($result['notifications'] as $key => $value) {

                                        draw($value, $LANG, $helper);
                                    }
                                ?>

                            </ul>

                        <?php

                    } else {

                        ?>

                        <header class="top-banner info-banner">

                            <div class="info">
                                <h1><?php echo $LANG['label-empty-list']; ?></h1>
                            </div>

                        </header>

                        <?php
                    }
                    ?>

                    <?php

                        if ($notifications_all > 20) {

                            ?>

                            <header class="top-banner loading-banner">

                                <div class="prompt">
                                    <button onclick="Notifications.moreAll('<?php echo $result['notifyId']; ?>'); return false;" class="button green loading-button"><?php echo $LANG['action-more']; ?></button>
                                </div>

                            </header>

                            <?php
                        }
                    ?>


                </div>

            </div>
        </div>

        <?php

            include_once("../html/common/sidebar.inc.php");
        ?>

    </div>

    <?php

        include_once("../html/common/footer.inc.php");
    ?>

    <script type="text/javascript" src="/js/jquery.tipsy.js"></script>
    <script type="text/javascript" src="/js/friends.js"></script>

    <script type="text/javascript">

        var notifications_all = <?php echo $notifications_all; ?>;
        var notifications_loaded = <?php echo $notifications_loaded; ?>;

    </script>


</body
</html>

<?php

    function draw($notify, $LANG, $helper)
    {
        $time = new language(NULL, $LANG['lang-code']);
        $profilePhotoUrl = "/img/profile_default_photo.png";

        if (strlen($notify['fromUserPhotoUrl']) != 0) {

            $profilePhotoUrl = $notify['fromUserPhotoUrl'];
        }

        switch ($notify['type']) {

            case NOTIFY_TYPE_LIKE: {

                $post = new post(NULL);
                $post->setRequestFrom(auth::getCurrentUserId());

                $post = $post->info($notify['postId']);

                ?>

                <li class="custom-list-item" data-id="<?php echo $notify['id']; ?>">

                    <a href="/<?php echo $notify['fromUserUsername']; ?>" class="item-logo" style="background-image:url(<?php echo $profilePhotoUrl; ?>)"></a>

                    <a href="/<?php echo $notify['fromUserUsername']; ?>" class="custom-item-link"><?php echo  $notify['fromUserFullname']; ?>
                        <span><?php echo $LANG['label-likes-your-post'] ?></span>
                    </a>

                    <div class="item-meta">

                        <span class="post-date"><?php echo $time->timeAgo($notify['createAt']); ?></span>
                        <span class="post-date"><a href="/<?php echo $post['fromUserUsername']; ?>/post/<?php echo $notify['postId']; ?>"><?php echo $LANG['action-go-to-post']; ?> »</a></span>

                    </div

                </li>

                <?php

                break;
            }

            case NOTIFY_TYPE_FOLLOWER: {

                ?>

                <li class="custom-list-item" data-id="<?php echo $notify['id']; ?>">

                    <a href="/<?php echo $notify['fromUserUsername']; ?>" class="item-logo" style="background-image:url(<?php echo $profilePhotoUrl; ?>)"></a>

                    <a href="/<?php echo $notify['fromUserUsername']; ?>" class="custom-item-link"><?php echo  $notify['fromUserFullname']; ?>
                        <span><?php echo $LANG['label-notify-request-to-friends']; ?>.</span>
                    </a>

                    <div class="item-meta">

                        <span class="post-date"><?php echo $time->timeAgo($notify['createAt']); ?></span>

                        <span class="post-date">
                            <a href="javascript:void(0)" onclick="Friends.rejectRequest('<?php echo $notify['id']; ?>', '<?php echo $notify['fromUserId']; ?>', '<?php echo auth::getAccessToken(); ?>'); return false;"><?php echo $LANG['action-reject']; ?></a>
                        </span>

                        <span class="post-date">
                            <a href="javascript:void(0)" onclick="Friends.acceptRequest('<?php echo $notify['id']; ?>', '<?php echo $notify['fromUserId']; ?>', '<?php echo auth::getAccessToken(); ?>'); return false;"><?php echo $LANG['action-accept']; ?></a>
                        </span>
                    </div

                </li>

                <?php

                break;
            }

            case NOTIFY_TYPE_COMMENT: {

                $post = new post(NULL);
                $post->setRequestFrom(auth::getCurrentUserId());

                $post = $post->info($notify['postId']);

                ?>

                <li class="custom-list-item" data-id="<?php echo $notify['id']; ?>">

                    <a href="/<?php echo $notify['fromUserUsername']; ?>" class="item-logo" style="background-image:url(<?php echo $profilePhotoUrl; ?>)"></a>

                    <a href="/<?php echo $notify['fromUserUsername']; ?>" class="custom-item-link"><?php echo  $notify['fromUserFullname']; ?>
                        <span><?php echo $LANG['label-new-comment'] ?></span>
                    </a>

                    <div class="item-meta">

                        <span class="post-date"><?php echo $time->timeAgo($notify['createAt']); ?></span>
                        <span class="post-date"><a href="/<?php echo $post['fromUserUsername']; ?>/post/<?php echo $notify['postId']; ?>"><?php echo $LANG['action-go-to-post']; ?> »</a></span>

                    </div

                </li>

                <?php

                break;
            }

            case NOTIFY_TYPE_COMMENT_REPLY: {

                $post = new post(NULL);
                $post->setRequestFrom(auth::getCurrentUserId());

                $post = $post->info($notify['postId']);

                ?>

                <li class="custom-list-item" data-id="<?php echo $notify['id']; ?>">

                    <a href="/<?php echo $notify['fromUserUsername']; ?>" class="item-logo" style="background-image:url(<?php echo $profilePhotoUrl; ?>)"></a>

                    <a href="/<?php echo $notify['fromUserUsername']; ?>" class="custom-item-link"><?php echo  $notify['fromUserFullname']; ?>
                        <span><?php echo $LANG['label-new-reply-to-comment']; ?></span>
                    </a>

                    <div class="item-meta">

                        <span class="post-date"><?php echo $time->timeAgo($notify['createAt']); ?></span>
                        <span class="post-date"><a href="/<?php echo $post['fromUserUsername']; ?>/post/<?php echo $notify['postId']; ?>"><?php echo $LANG['action-go-to-post']; ?> »</a></span>

                    </div

                </li>

                <?php

                break;
            }

            case NOTIFY_TYPE_GIFT: {

                ?>

                <li class="custom-list-item" data-id="<?php echo $notify['id']; ?>">

                    <a href="/<?php echo $notify['fromUserUsername']; ?>" class="item-logo" style="background-image:url(<?php echo $profilePhotoUrl; ?>)"></a>

                    <a href="/<?php echo $notify['fromUserUsername']; ?>" class="custom-item-link"><?php echo  $notify['fromUserFullname']; ?>
                        <span><?php echo $LANG['label-new-gift']; ?></span>
                    </a>

                    <div class="item-meta">

                        <span class="post-date"><?php echo $time->timeAgo($notify['createAt']); ?></span>
                        <span class="post-date"><a href="/<?php echo auth::getCurrentUserLogin(); ?>/gifts"><?php echo $LANG['action-view']; ?> »</a></span>

                    </div

                </li>

                <?php

                break;
            }

            case NOTIFY_TYPE_IMAGE_COMMENT: {

                $photos = new photos(NULL);
                $photos->setRequestFrom(auth::getCurrentUserId());

                $photoInfo = $photos->info($notify['postId']);

                ?>

                <li class="custom-list-item" data-id="<?php echo $notify['id']; ?>">

                    <a href="/<?php echo $notify['fromUserUsername']; ?>" class="item-logo" style="background-image:url(<?php echo $profilePhotoUrl; ?>)"></a>

                    <a href="/<?php echo $notify['fromUserUsername']; ?>" class="custom-item-link"><?php echo  $notify['fromUserFullname']; ?>
                        <span><?php echo $LANG['label-new-comment']; ?></span>
                    </a>

                    <div class="item-meta">

                        <span class="post-date"><?php echo $time->timeAgo($notify['createAt']); ?></span>
                        <span class="post-date"><a href="/<?php echo $photoInfo['fromUserUsername']; ?>/image/<?php echo $notify['postId']; ?>"><?php echo $LANG['action-go-to-photo']; ?> »</a></span>

                    </div

                </li>

                <?php

                break;
            }

            case NOTIFY_TYPE_IMAGE_COMMENT_REPLY: {

                $photos = new photos(NULL);
                $photos->setRequestFrom(auth::getCurrentUserId());

                $photoInfo = $photos->info($notify['postId']);

                ?>

                <li class="custom-list-item" data-id="<?php echo $notify['id']; ?>">

                    <a href="/<?php echo $notify['fromUserUsername']; ?>" class="item-logo" style="background-image:url(<?php echo $profilePhotoUrl; ?>)"></a>

                    <a href="/<?php echo $notify['fromUserUsername']; ?>" class="custom-item-link"><?php echo  $notify['fromUserFullname']; ?>
                        <span><?php echo $LANG['label-new-reply-to-comment']; ?></span>
                    </a>

                    <div class="item-meta">

                        <span class="post-date"><?php echo $time->timeAgo($notify['createAt']); ?></span>
                        <span class="post-date"><a href="/<?php echo $photoInfo['fromUserUsername']; ?>/image/<?php echo $notify['postId']; ?>"><?php echo $LANG['action-go-to-photo']; ?> »</a></span>

                    </div

                </li>

                <?php

                break;
            }

            case NOTIFY_TYPE_IMAGE_LIKE: {

                $photos = new photos(NULL);
                $photos->setRequestFrom(auth::getCurrentUserId());

                $photoInfo = $photos->info($notify['postId']);

                ?>

                <li class="custom-list-item" data-id="<?php echo $notify['id']; ?>">

                    <a href="/<?php echo $notify['fromUserUsername']; ?>" class="item-logo" style="background-image:url(<?php echo $profilePhotoUrl; ?>)"></a>

                    <a href="/<?php echo $notify['fromUserUsername']; ?>" class="custom-item-link"><?php echo  $notify['fromUserFullname']; ?>
                        <span><?php echo $LANG['label-likes-your-photo']; ?></span>
                    </a>

                    <div class="item-meta">

                        <span class="post-date"><?php echo $time->timeAgo($notify['createAt']); ?></span>
                        <span class="post-date"><a href="/<?php echo $photoInfo['fromUserUsername']; ?>/image/<?php echo $notify['postId']; ?>"><?php echo $LANG['action-go-to-photo']; ?> »</a></span>

                    </div

                </li>

                <?php

                break;
            }
            
            default: {


                break;
            }
        }
    }

?>