Location: PHPKode > projects > Gnew > Gnew-2013.1/posts/list.php
<?php
// -----------------------------------------------------------------------------
// $Id: list.php 690 2013-03-29 09:31: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['POSTS_LIST']);

// Check query
if (!empty($_GET['category_id']) && string_is_int($_GET['category_id']))
{
    $sql->query('SELECT category_level, category_name
                 FROM ' . TABLE_CATEGORIES . '
                 WHERE category_level IN (\'0\', \'3\', \'4\')
                 AND category_id = \'' . $_GET['category_id'] . '\'');
    $table_categories = $sql->fetch();
    // Forum category?
    if ($table_categories['category_name'])
    {
        // Private category - administrators only
        if ($table_categories['category_level'] == 0 && $users['user_level'] < 4)
        {
            error_template($lang['ERROR_PRIVATE_CATEGORY']);
        }
        else
        {
            // Create pages list
            if (empty($_GET['page']) || !string_is_int($_GET['page']))
            {
                $_GET['page'] = 1;
            }
            $threads_per_page = $settings['threads_per_page'];
            $threads_offset = ($_GET['page'] - 1) * $threads_per_page;
            $sql->query('SELECT DISTINCT thread_id
                         FROM ' . TABLE_POSTS . '
                         WHERE category_id = \'' . $_GET['category_id'] . '\'');
            $num_threads = $sql->num_rows();
            $num_pages = ceil($num_threads / $threads_per_page);
            $pages_list = get_pages_list('./../posts/list.php?category_id=' . $_GET['category_id'] . '&amp;', $num_pages);

            $first_creation = $first_id = $first_name = array();
            $sql->query('SELECT t1.thread_id, t1.user_id, t1.post_creation, t3.user_name
                         FROM ' . TABLE_POSTS . ' AS t1
                         INNER JOIN (SELECT thread_id, MIN(post_creation) AS first_post
                                     FROM ' . TABLE_POSTS . '
                                     WHERE category_id = \'' . $_GET['category_id'] . '\'
                                     GROUP BY thread_id, post_sticky
                                     ORDER BY post_sticky DESC, first_post DESC
                                     LIMIT ' . $threads_per_page . ' OFFSET ' . $threads_offset . ') AS t2
                                     ON (t2.thread_id = t1.thread_id AND t2.first_post = t1.post_creation)
                         INNER JOIN ' . TABLE_USERS . ' AS t3
                                     ON (t3.user_id = t1.user_id)');
            while ($table_posts = $sql->fetch())
            {
                // First ids, names and creations are allocated to threads
                $first_creation[$table_posts['thread_id']] = $table_posts['post_creation'];
                $first_id[$table_posts['thread_id']] = $table_posts['user_id'];
                $first_name[$table_posts['thread_id']] = $table_posts['user_name'];
            }

            $folder_image = $last_creation = $last_id = $last_name = array();
            $sql->query('SELECT t1.thread_id, t1.user_id, t1.post_active, t1.post_sticky, t1.post_creation, t3.user_name
                         FROM ' . TABLE_POSTS . ' AS t1
                         INNER JOIN (SELECT thread_id, MAX(post_creation) AS last_post
                                     FROM ' . TABLE_POSTS . '
                                     WHERE category_id = \'' . $_GET['category_id'] . '\'
                                     GROUP BY thread_id, post_sticky
                                     ORDER BY post_sticky DESC, last_post DESC
                                     LIMIT ' . $threads_per_page . ' OFFSET ' . $threads_offset . ') AS t2
                                     ON (t2.thread_id = t1.thread_id AND t2.last_post = t1.post_creation)
                         INNER JOIN ' . TABLE_USERS . ' AS t3
                                     ON (t3.user_id = t1.user_id)');
            while ($table_posts = $sql->fetch())
            {
                // Last ids, names and creations are allocated to threads
                $last_creation[$table_posts['thread_id']] = $table_posts['post_creation'];
                $last_id[$table_posts['thread_id']] = $table_posts['user_id'];
                $last_name[$table_posts['thread_id']] = $table_posts['user_name'];

                // Images are allocated to threads
                if ($table_posts['post_active'])
                {
                    if ($table_posts['post_sticky'])
                    {
                        if ($_SESSION['user_id'] > 0 && ($table_posts['post_creation'] > $users['user_last_visit']))
                        {
                            $folder_image[$table_posts['thread_id']] = './../images/posts/sticky_new.png';
                        }
                        else
                        {
                            $folder_image[$table_posts['thread_id']] = './../images/posts/sticky.png';
                        }
                    }
                    else
                    {
                        if ($_SESSION['user_id'] > 0 && ($table_posts['post_creation'] > $users['user_last_visit']))
                        {
                            $folder_image[$table_posts['thread_id']] = './../images/posts/folder_new.png';
                        }
                        else
                        {
                            $folder_image[$table_posts['thread_id']] = './../images/posts/folder.png';
                        }
                    }
                }
                else
                {
                    if ($table_posts['post_sticky'])
                    {
                        if ($_SESSION['user_id'] > 0 && ($table_posts['post_creation'] > $users['user_last_visit']))
                        {
                            $folder_image[$table_posts['thread_id']] = './../images/posts/sticky_new_closed.png';
                        }
                        else
                        {
                            $folder_image[$table_posts['thread_id']] = './../images/posts/sticky_closed.png';
                        }
                    }
                    else
                    {
                        if ($_SESSION['user_id'] > 0 && ($table_posts['post_creation'] > $users['user_last_visit']))
                        {
                            $folder_image[$table_posts['thread_id']] = './../images/posts/folder_new_closed.png';
                        }
                        else
                        {
                            $folder_image[$table_posts['thread_id']] = './../images/posts/folder_closed.png';
                        }
                    }
                }
            }

            if ($settings['allow_html'])
            {
                $html_support = $lang['HTML_ENABLED'];
            }
            else
            {
                $html_support = $lang['HTML_DISABLED'];
            }
            $template->set_file('list', 'posts/list.htpl');
            $template->set_block('list', 'THREADS_BLOCK', 'threads');
            $sql->query('SELECT t1.thread_id, t1.post_subject, t2.posts_per_thread
                         FROM ' . TABLE_POSTS . ' AS t1
                         INNER JOIN (SELECT thread_id, MAX(post_creation) AS last_post, COUNT(*) AS posts_per_thread
                                     FROM ' . TABLE_POSTS . '
                                     WHERE category_id = \'' . $_GET['category_id'] . '\'
                                     GROUP BY thread_id, post_sticky
                                     ORDER BY post_sticky DESC, last_post DESC
                                     LIMIT ' . $threads_per_page . ' OFFSET ' . $threads_offset . ') AS t2
                                     ON (t2.thread_id = t1.thread_id AND t2.last_post = t1.post_creation)
                         ORDER BY t1.post_sticky DESC, t1.post_creation DESC');
            while ($table_posts = $sql->fetch())
            {
                $template->set_var(array('FIRST_CREATION' => format_date($first_creation[$table_posts['thread_id']]),
                                         'FIRST_ID' => $first_id[$table_posts['thread_id']],
                                         'FIRST_NAME' => $first_name[$table_posts['thread_id']],
                                         'FOLDER_IMAGE' => $folder_image[$table_posts['thread_id']],
                                         'LAST_CREATION' => format_date($last_creation[$table_posts['thread_id']]),
                                         'LAST_ID' => $last_id[$table_posts['thread_id']],
                                         'LAST_LIST' => ceil(ceil($table_posts['posts_per_thread'] / $settings['posts_per_page']) / $settings['pages_limit']),
                                         'LAST_NAME' => $last_name[$table_posts['thread_id']],
                                         'LAST_PAGE' => ceil($table_posts['posts_per_thread'] / $settings['posts_per_page']),
                                         'NUM_REPLIES' => $table_posts['posts_per_thread'] - 1,
                                         'POST_SUBJECT' => $table_posts['post_subject'],
                                         'THREAD_ID' => $table_posts['thread_id']));
                $template->parse('THREADS_BLOCK', 'threads', true);
                $threads_exist = true;
            }
            $template->set_var(array('CATEGORY_ID' => $_GET['category_id'],
                                     'CATEGORY_NAME' => $table_categories['category_name'],
                                     'EMOTICONS_LIST' => get_emoticons_list(0),
                                     'HTML_SUPPORT' => $html_support));

            if (isset($threads_exist))
            {
                $template->set_var('FOOTER', sprintf($lang['PAGES'], $pages_list));
            }
            else
            {
                $template->set_var('FOOTER', $lang['ERROR_NO_THREAD']);
            }
            $template->parse('list', null, false, array(TABLE_CATEGORIES, TABLE_EMOTICONS, TABLE_POSTS, TABLE_USERS));
        }
    }
    else
    {
        error_template($lang['ERROR_CATEGORY_NO_POST']);
    }
}
else
{
    error_template($lang['ERROR_NO_DATA']);
}

page_footer();

?>
Return current item: Gnew