<?php
/*
 * pages/news.php
 * PHP logic: INTACT - foloseste $paginate (creat de include/functions/news.php)
 * HTML: design Nostalgia2
 * 
 * Variabile disponibile:
 *   $paginate - instanta clasei din include/classes/news.php
 *   $database - USER class (are runQuerySqlite pentru SQLite)
 *   $web_admin, $jsondataPrivileges['news']
 *   $lang[], $site_url, $offline
 */

if(!$offline && $database->is_loggedin())
    if($web_admin >= $jsondataPrivileges['news'])
        include 'include/functions/add-news.php';

if(version_compare(phpversion(), '5.6.0', '<')) { ?>
<div class="alert alert-danger alert-dismissible fade show m-3" role="alert">
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    Metin2CMS needs PHP >= 5.6.0. Currently: <?php echo phpversion(); ?>
</div>
<?php } ?>

<!-- Header bara titlu pagina -->
<nav class="navbar navbar-dark bg-news-header mb-2">
    <div class="container-fluid">
        <h4 class="h4 small m-0 text-uppercase">
            <?php echo $site_title; ?> - <?php echo strtoupper($lang['news']); ?>
        </h4>
        <?php if($offline) echo '<small><strong class="text-danger">'.$lang['server-offline'].'</strong></small>'; ?>
    </div>
</nav>

<?php
// ---- LOGICA PHP INTACT ----
$query = "SELECT * FROM news ORDER BY id DESC";
$records_per_page = intval(getJsonSettings("news"));
$newquery = $paginate->paging($query, $records_per_page);

// Fetch cu runQuerySqlite (db din news/paginate e privat, dar USER are aceasta metoda)
$stmt = $database->runQuerySqlite($newquery);
$stmt->execute();
$news_rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

if(empty($news_rows)) {
    echo '<p class="text-muted text-center p-4">No news yet.</p>';
} else {
    foreach($news_rows as $row) {
        $string = strip_tags($row['content']);
        $image  = [];
        preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $row['content'], $image);
        $big = strlen($string) > 500;
        if($big) {
            $cut = substr($string, 0, 500);
            $preview = substr($cut, 0, strrpos($cut, ' ')).'...';
        } else {
            $preview = $row['content'];
        }
?>

<!-- Bara titlu stire -->
<div class="col-11 p-4 bg-news-title small mx-auto">
    <div class="row">
        <p class="col text-white w-100 small m-0 news-title-text">
            <?php echo htmlspecialchars($row['title']); ?>
        </p>
        <p class="col text-right w-100 m-0 d-none d-lg-block small text-bluevioletmutted">
            <span class="text-new px-2"><?php echo $row['time']; ?></span>
        </p>
    </div>
</div>

<!-- Continut stire -->
<div class="row mx-auto">
    <div class="col-12 background-white py-3 p-4 mx-auto">
        <?php if(isset($image['src']) && $big) { ?>
        <div class="text-center mb-2">
            <img src="<?php echo $image['src']; ?>" class="img-fluid" alt="">
        </div>
        <?php } ?>
        <div class="clearfix">
            <p class="col-12 small text-muted text-justify news__content">
                <?php echo $preview; ?>
            </p>
        </div>
        <div class="d-flex justify-content-between align-items-center px-2 mt-2">
            <!-- Butoane admin -->
            <div>
                <?php if(!$offline && $database->is_loggedin() && $web_admin >= $jsondataPrivileges['news']) { ?>
                <a href="<?php echo $site_url; ?>read/<?php echo $row['id']; ?>" class="text-warning me-2">
                    <i class="fa fa-pencil"></i>
                </a>
                <a href="<?php echo $site_url; ?>?delete=<?php echo $row['id']; ?>"
                   onclick="return confirm('<?php echo addslashes($lang['sure?']); ?>');"
                   class="text-danger">
                    <i class="fa fa-trash-o"></i>
                </a>
                <?php } ?>
            </div>
            <!-- Read more -->
            <div class="d-flex read-more justify-content-center">
                <hr>
                <a href="<?php echo $site_url; ?>read/<?php echo $row['id']; ?>">
                    <button class="btn btn-read"><?php echo $lang['read-more']; ?></button>
                </a>
            </div>
        </div>
    </div>
</div>

<?php } } // end foreach ?>

<!-- Paginare -->
<div class="w-100 p-4 text-center mx-auto">
    <?php $paginate->paginglink($query, $records_per_page, $lang['first-page'], $lang['last-page'], $site_url); ?>
</div>
