Location: PHPKode > projects > Gnew > Gnew-2013.1/news/index.php
<?php
// -----------------------------------------------------------------------------
// $Id: index.php 676 2013-03-27 13:38:34Z raoul $
//
// Copyright (C) 2013 Raoul Proença
// License: GNU GPL version 3 (see copying.txt file)
// Website: http://www.gnew.fr/
// -----------------------------------------------------------------------------
// 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 <http://www.gnu.org/licenses/>.
// -----------------------------------------------------------------------------

include('./../includes/common.php');

page_header($lang['NEWS_INDEX']);

// Query string - ?news_id=&category_id=&news_month=&news_year=&page=&list=
if (!empty($_GET['news_id']) && string_is_int($_GET['news_id']))
{
    $clause = 'WHERE t1.news_id = \'' . $_GET['news_id'] . '\'';
    $query = 'news_id=' . $_GET['news_id'];
}
if (!empty($_GET['category_id']) && string_is_int($_GET['category_id']))
{
    if (!empty($_GET['news_id']) && string_is_int($_GET['news_id']))
    {
        $clause .= ' AND t1.category_id = \'' . $_GET['category_id'] . '\'';
        $query .= '&amp;category_id=' . $_GET['category_id'];
    }
    else
    {
        $clause = 'WHERE t1.category_id = \'' . $_GET['category_id'] . '\'';
        $query = 'category_id=' . $_GET['category_id'];
    }
}
if (!empty($_GET['news_month']) && string_is_int($_GET['news_month']))
{
    if (!empty($_GET['news_id']) && string_is_int($_GET['news_id']))
    {
        $clause .= ' AND t1.news_month = \'' . $_GET['news_month'] . '\'';
        $query .= '&amp;news_month=' . $_GET['news_month'];
    }
    elseif (!empty($_GET['category_id']) && string_is_int($_GET['category_id']))
    {
        $clause .= ' AND t1.news_month = \'' . $_GET['news_month'] . '\'';
        $query .= '&amp;news_month=' . $_GET['news_month'];
    }
    else
    {
        $clause = 'WHERE t1.news_month = \'' . $_GET['news_month'] . '\'';
        $query = 'news_month=' . $_GET['news_month'];
    }
}
if (!empty($_GET['news_year']) && string_is_int($_GET['news_year']))
{
    if (!empty($_GET['news_id']) && string_is_int($_GET['news_id']))
    {
        $clause .= ' AND t1.news_year = \'' . $_GET['news_year'] . '\'';
        $query .= '&amp;news_year=' . $_GET['news_year'];
    }
    elseif (!empty($_GET['category_id']) && string_is_int($_GET['category_id']))
    {
        $clause .= ' AND t1.news_year = \'' . $_GET['news_year'] . '\'';
        $query .= '&amp;news_year=' . $_GET['news_year'];
    }
    elseif (!empty($_GET['news_month']) && string_is_int($_GET['news_month']))
    {
        $clause .= ' AND t1.news_year = \'' . $_GET['news_year'] . '\'';
        $query .= '&amp;news_year=' . $_GET['news_year'];
    }
    else
    {
        $clause = 'WHERE t1.news_year = \'' . $_GET['news_year'] . '\'';
        $query = 'news_year=' . $_GET['news_year'];
    }
}
if ((empty($_GET['news_id']) || !string_is_int($_GET['news_id'])) &&
    (empty($_GET['category_id']) || !string_is_int($_GET['category_id'])) &&
    (empty($_GET['news_month']) || !string_is_int($_GET['news_month'])) &&
    (empty($_GET['news_year']) || !string_is_int($_GET['news_year'])))
{
    $clause = 'WHERE';
    $query = '';
}
else
{
    $clause .= ' AND';
    $query .= '&amp;';
}

// Pages list creation - start
if (empty($_GET['page']) || !string_is_int($_GET['page']))
{
    $_GET['page'] = 1;
}
$news_per_page = $settings['news_per_page'];
$news_offset = ($_GET['page'] - 1) * $news_per_page;

$template->set_file('index', 'news/index.htpl');
$template->set_block('index', 'NEWS_BLOCK', 'news');
$sql->query('SELECT t1.news_id, t1.category_id, t1.user_id, t1.news_comments, t1.news_source, t1.news_subject, t1.news_text, t1.news_creation, t1.news_edition, t1.edition_id, t1.edition_name, t2.category_image, t2.category_name, t3.user_name
             FROM ' . TABLE_NEWS . ' AS t1
             INNER JOIN ' . TABLE_CATEGORIES . ' AS t2
                         ON (t2.category_id = t1.category_id AND t2.category_level IN (\'2\', \'4\'))
             INNER JOIN ' . TABLE_USERS . ' AS t3
                         ON (t3.user_id = t1.user_id)
             ' . $clause . ' t1.news_active = \'1\'
             ORDER BY ' . $settings['news_sorting'] . ' ' . $settings['news_order'] . '
             LIMIT ' . $news_per_page . ' OFFSET ' . $news_offset);
while ($table_news = $sql->fetch())
{
    if ($table_news['news_edition'])
    {
        $news_edited = sprintf($lang['EDITED_BY'], $table_news['edition_id'], $table_news['edition_name'], format_date($table_news['news_edition']));
    }
    else
    {
        $news_edited = '';
    }
    if ($table_news['news_source'])
    {
        $news_source = undo_escape_sequences($table_news['news_source']);
        $news_source = sprintf($lang['SOURCE'], $news_source);
    }
    else
    {
        $news_source = '<!-- empty -->';
    }
    $news_text = undo_escape_sequences($table_news['news_text']);
    // User logged?
    if ($_SESSION['user_id'] > 0)
    {
        // New news since last visit
        if ($table_news['news_creation'] > $users['user_last_visit'])
        {
            $subject_class = 'new';
        }
        else
        {
            $subject_class = 'old';
        }
    }
    else
    {
        $subject_class = 'old';
    }
    $news_text = cut_text($news_text, './../news/index.php?news_id=' . $table_news['news_id'], 'news_id');
    $template->set_var(array('CATEGORY_ID' => $table_news['category_id'],
                             'CATEGORY_IMAGE' => $table_news['category_image'],
                             'CATEGORY_NAME' => $table_news['category_name'],
                             'NEWS_COMMENTS' => $table_news['news_comments'],
                             'NEWS_EDITED' => $news_edited,
                             'NEWS_ID' => $table_news['news_id'],
                             'NEWS_POSTED' => sprintf($lang['POSTED_BY'], $table_news['user_id'], $table_news['user_name'], format_date($table_news['news_creation'])),
                             'NEWS_SOURCE' => $news_source,
                             'NEWS_SUBJECT' => $table_news['news_subject'],
                             'NEWS_TEXT' => $news_text,
                             'SUBJECT_CLASS' => $subject_class));
    $template->parse('NEWS_BLOCK', 'news', true);
    $news_exist = true;
}
if (isset($news_exist))
{
    // Pages list creation - end
    $sql->query('SELECT t1.news_id
                 FROM ' . TABLE_NEWS . ' AS t1
                 INNER JOIN ' . TABLE_CATEGORIES . ' AS t2
                             ON (t2.category_id = t1.category_id AND t2.category_level IN (\'2\', \'4\'))
                 ' . $clause . ' t1.news_active = \'1\'');
    $num_news = $sql->num_rows();
    $num_pages = ceil($num_news / $news_per_page);
    $pages_list = get_pages_list('./../news/index.php?' . $query, $num_pages);

    $date_format = get_date_format();
    $date_offset = get_date_offset();
    $current_time = date($date_format, (time() + $date_offset));
    $template->set_var(array('CURRENT_TIME' => sprintf($lang['CURRENT_TIME'], $current_time),
                             'PAGES' => sprintf($lang['PAGES'], $pages_list),
                             'WELCOME' => sprintf($lang['WELCOME'], $settings['site_name'])));
    $template->parse('index', null, false, array(TABLE_CATEGORIES, TABLE_NEWS, TABLE_USERS));
}
else
{
    error_template($lang['ERROR_NEWS_SORTING']);
}

page_footer();

?>
Return current item: Gnew