<?php
/**
 * /subs/parts/userarea/blog.php
 * @version 1.3
 * @desc Userarea: blog
 * @author Fándly Gergő Zoltán (gergo@systemtest.tk, systemtest.tk)
 * @copy 2018 Fándly Gergő Zoltán
 * License:
    Systemtest.tk website's.
    Copyright (C) 2018  Fándly Gergő Zoltán

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 **/
?>

<div id="postlist">
    <table class="footable" style="text-align: left">
        <thead>
            <tr>
                <th><?php echo $lang['title'] ?></th>
                <th data-breakpoints="xs sm"><?php echo $lang['tags'] ?></th>
                <th data-breakpoints="xs sm"><?php echo $lang['owner'] ?></th>
                <th data-breakpoints="xs sm"><?php echo $lang['date'] ?></th>
                <th><?php echo $lang['published'] ?></th>
                <th data-breakpoints="xs sm md"><?php echo $lang['operations'] ?></th>
            </tr>
        </thead>
        <tbody>
            <?php
            if($_SESSION['accesslevel']<3){
                $sql=$db->prepare("SELECT b.id, b.title, u.fullname AS owner, b.date, b.published, GROUP_CONCAT(bt.tag SEPARATOR ';') FROM blog AS b INNER JOIN users AS u ON (u.id=b.owner) LEFT JOIN blog_tags AS bt ON (bt.blogentry=b.id) WHERE b.owner=:uid GROUP BY b.id ORDER BY date DESC");
                $sql->execute(array(":uid"=>$_SESSION['id']));
            }
            else{
                $sql=$db->prepare("SELECT b.id, b.title, u.fullname AS owner, b.date, b.published, GROUP_CONCAT(bt.tag SEPARATOR ';') AS tags FROM blog AS b INNER JOIN users AS u ON (u.id=b.owner) LEFT JOIN blog_tags AS bt ON (bt.blogentry=b.id) GROUP BY b.id ORDER BY date DESC");
                $sql->execute();
            }
            
            while($row=$sql->fetch(PDO::FETCH_ASSOC)){
                echo "
                <tr>
                    <td>".$row['title']."</td>
                    <td>".$row['tags']."</td>
                    <td>".$row['owner']."</td>
                    <td>".$row['date']."</td>
                    <td>".($row['published']==1?$lang['tyes']:$lang['tno'])."</td>
                    <td>
                        <button type=\"button\" onclick=\"blogEdit(".$row['id'].")\">".$lang['edit']."</button>
                        <button type=\"button\" onclick=\"blogDelete(".$row['id'].", this)\">".$lang['delete']."</button>
                    </td>
                </tr>
                ";
            }
            ?>
        </tbody>
    </table>
    <hr class="placeholder">
    <button type="button" onclick="newBlog()"><i class="fa fa-plus-circle"></i> <?php echo $lang['new'] ?></button>
</div>
<div id="postEdit" style="display: none">
    <hr class="placeholder">
    <form method="POST" action="" class="ajaxform">
        <input type="hidden" name="blog_id" id="blog_id">
        <fieldset style="width: 95%">
            <legend><?php echo $lang['editor'] ?></legend>
            <table>
                <tr>
                    <td><?php echo $lang['name'].": " ?></td>
                    <td><input type="text" name="blog_title" placeholder="<?php echo $lang['name']."..." ?>" required style="width: 95%"></td>
                </tr>
                <tr>
                    <td><?php echo $lang['tags'].": " ?></td>
                    <td><input type="text" name="blog_tags" placeholder="<?php echo $lang['tags']."..." ?>" style="widht: 95%"></td>
                </tr>
                <tr>
                    <td><?php echo $lang['published'].": " ?></td>
                    <td>
                        <div class="checkbox">
                            <input type="checkbox" name="blog_published" id="blog_published" hidden>
                            <label for="blog_published"></label>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td><?php echo $lang['autosave'].": " ?></td>
                    <td>
                        <div class="checkbox">
                            <input type="checkbox" id="autosave" onclick="blogAutoSave()" hidden>
                            <label for="autosave"></label>
                        </div>
                    </td>
                </tr>
            </table>
            <br>
            <div id="editorContainer" style="background: rgb(255, 255, 255)">
                <div id="editor"></div>
            </div>
            <p style="text-align: right"><i><?php echo $lang['last_saved'].": " ?><span id="lastSaved"></span></i></p>
            <br>
            <br>
            <button type="button" class="green" onclick="blogSave()"><i class="fa fa-save"></i> <?php echo $lang['save'] ?></button>
            <button type="button" class="red" onclick="blogDiscard()"><i class="fa fa-trash"></i> <?php echo $lang['discard'] ?></button>
        </fieldset>
    </form>
</div>