Location: PHPKode > projects > pkZoos Website > board.php
<?php
	if(!defined('PK_MAGIC'))
		die('hack or what?');





	/* */

	$show_board = 1;





	/* */

    if(isset($_GET['action']) && $_GET['action'] == 'mark_read')
    {
		if(!$user['logged_in'])
		{
			error_add('not this for guests.', '');
		}

        if($errors == 0)
        {
            $q = "
                SELECT cat_id
                FROM " . CATS_TABLE . "
            ";

            $cat_res = $db->query($q);

            while($cat_row = $db->fetch_row($cat_res))
            {
                $q = "
                    SELECT topic_id
                    FROM " . TOPICS_TABLE . "
                    WHERE
                    	cat_id           = " . $cat_row['cat_id'] . " AND
                    	topic_is_archive = 0
                ";

                $topic_res = $db->query($q);

                while($topic_row = $db->fetch_row($topic_res))
                {
                    $q = "
                        UPDATE " . TOPICS_TRACK_TABLE . "
                        SET last_visit_time = " . CURRENT_TIME . "
                        WHERE
                            topic_id = " . $topic_row['topic_id'] . " AND
                            cat_id   = " . $cat_row['cat_id'] . " AND
                            user_id  = " . $user['id'] . "
                    ";

                    $track_res = $db->query($q);

                    if(!$db->affected_rows($track_res))
                    {
                        $q = "
                            INSERT INTO " . TOPICS_TRACK_TABLE . "
                            SET
                                last_visit_time = " . CURRENT_TIME . ",
                                topic_id        = " . $topic_row['topic_id'] . ",
                                cat_id          = " . $cat_row['cat_id'] . ",
                                user_id         = " . $user['id'] . "
                        ";

                        $db->query($q);
                    }
                }
            }

            redirect(make_url('board', array()));
        }
    }





	/* */

    if($user['logged_in'])
    {
        $tpl->add_var('URL_MARK_READ', html_escape(make_url('board', array('action' => 'mark_read'))));
    }
    else
    {
        $tpl->add_var('URL_MARK_READ', '');
    }





	/* */

    $q = "
        SELECT
        	head_id,
        	head_title
        FROM " . HEADS_TABLE . "
        ORDER BY head_pos ASC
    ";

    $head_res = $db->query($q);
    $head_count = $db->num_rows($head_res);

    $tpl->add_var('HEAD_COUNT', $head_count);

    for($head_i = 0; $head_i < $head_count; $head_i++)
    {
        $head_row = $db->fetch_row($head_res);

        $tpl->add_block_vars
        (
        	'heads',
        	array
        	(
        		'TITLE' => html_escape($head_row['head_title'])
        	)
        );





		/* */

        $q = "
            SELECT
            	cat_id,
                cat_title,
                cat_topics,
                cat_posts,
                last_post_time,
                last_poster_id,
                last_poster_name,
                cat_is_readonly,
                cat_is_private,
                cat_password
            FROM " . CATS_TABLE . "
            WHERE
                head_id        = " . $head_row['head_id'] . " AND
                cat_is_deleted = 0
            ORDER BY cat_pos ASC
        ";

        $cat_res = $db->query($q);
        $cat_count = $db->num_rows($cat_res);

        $tpl->data['heads'][$head_i]['CAT_COUNT'] = $cat_count;

        for($cat_i = 0; $cat_i < $cat_count; $cat_i++)
        {
            $cat_row = $db->fetch_row($cat_res);





			/* */

			$access = 1;

			if($cat_row['cat_is_private'])
			{
				$access = 0;

				if($user['is_admin'])
				{
					$access = 1;
				}
				else if($user['logged_in'])
				{
                    $q = "
                        SELECT mod_id
                        FROM " . MODS_TABLE . "
                        WHERE
                            cat_id          = " . $cat_row['cat_id'] . " AND
                            user_id         = " . $user['id'] . " AND
                            mod_is_disabled = 0
                    ";

                    $mod_res = $db->query($q);

                    if(!$db->fetch_row($mod_res))
                    {
                        $q = "
                            SELECT cat_password
                            FROM " . CATS_PASSWORDS_TABLE . "
                            WHERE
                                cat_id  = " . $cat_row['cat_id'] . " AND
                                user_id = " . $user['id'] . "
                        ";

                        $password_res = $db->query($q);
                        $password_row = $db->fetch_row($password_res);

                        if($password_row && $password_row['cat_password'] == $cat_row['cat_password'])
                        {
                            $access = 1;
                        }
                    }
                    else
                    {
                        $access = 1;
                    }
                }
			}





			/* */

            $cat_row['cat_is_new'] = 0;

            if($user['logged_in'] && $access)
            {
                $q = "
                    SELECT COUNT(topic_id) as count
                    FROM " . TOPICS_TABLE .  "
                    WHERE
                    	cat_id           = " . $cat_row['cat_id'] . " AND
                    	topic_is_archive = 0
                ";

                $topic_res = $db->query($q);
                $topic_row = $db->fetch_row($topic_res);

                $q = "
                    SELECT COUNT(topic_id) as count
                    FROM " . TOPICS_TRACK_TABLE .  "
                    WHERE
                        user_id          = " . $user['id'] . " AND
                        cat_id           = " . $cat_row['cat_id'] . " AND
                    	topic_is_archive = 0
                ";

                $track_res = $db->query($q);
                $track_row = $db->fetch_row($track_res);

                if($topic_row['count'] <= $track_row['count'])
                {
                    $q = "
                        SELECT topic_id
                        FROM " . TOPICS_TABLE . "
                        WHERE
                        	cat_id           = " . $cat_row['cat_id'] . " AND
                        	topic_is_archive = 0
                        ORDER BY last_post_time DESC
                    ";

                    $topic_res = $db->query($q);

                    while($topic_row = $db->fetch_row($topic_res))
                    {
                        $q = "
                            SELECT last_visit_time
                            FROM " . TOPICS_TRACK_TABLE .  "
                            WHERE
                                user_id  = " . $user['id'] . " AND
                                topic_id = " . $topic_row['topic_id'] . "
                        ";

                        $track_res = $db->query($q);

                        if($track_row = $db->fetch_row($track_res))
                        {
                            $q = "
                                SELECT COUNT(post_id) as count
                                FROM " . POSTS_TABLE . "
                                WHERE
                                    topic_id      = " . $topic_row['topic_id'] . " AND
                                    post_posttime > " . $track_row['last_visit_time'] . "
                                ORDER BY post_posttime ASC
                                LIMIT 1
                            ";

                            $post_res = $db->query($q);
                            $post_row = $db->fetch_row($post_res);

                            if($post_row['count'] > 0)
                            {
                                $cat_row['cat_is_new'] = 1;
                                break;
                            }
                        }
                        else
                        {
                            $cat_row['cat_is_new'] = 1;
                            break;
                        }
                    }
                }
                else
                {
                    $cat_row['cat_is_new'] = 1;
                }
            }





			/* */

            $tpl->add_block_vars
            (
            	'heads.cats',
            	array
            	(
					'TITLE' => html_escape($cat_row['cat_title']),
					'TOPICS' => ($access ? $cat_row['cat_topics'] : ''),
					'REPLIES' => ($access ? $cat_row['cat_posts'] : ''),
					'IS_READONLY' => $cat_row['cat_is_readonly'],
					'IS_PRIVATE' => $cat_row['cat_is_private'],
					'IS_NEW' => $cat_row['cat_is_new'],
					'URL' => html_escape(make_url('cat', array('cat_id' => $cat_row['cat_id']))),
					'LAST_POST_DATE' => ($access ? html_escape(make_date($cat_row['last_post_time'])) : ''),
					'LAST_POSTER_NAME' => ($access ? html_escape($cat_row['last_poster_name']) : ''),
					'LAST_POSTER_URL' => ($access ? html_escape(make_url('user', array('user_id' => $cat_row['last_poster_id']))) : '')
            	)
			);





			/* */

            $q = "
                SELECT
                    user_id,
                    user_name
                FROM " . MODS_TABLE . "
                WHERE
                    cat_id          = " . $cat_row['cat_id'] . " AND
                    mod_is_disabled = 0
                ORDER BY user_name ASC
            ";

            $mod_res = $db->query($q);
            $mod_count = $db->num_rows($mod_res);

            for($mod_i = 0; $mod_i < $mod_count; $mod_i++)
            {
                $mod_row = $db->fetch_row($mod_res);

                $tpl->add_block_vars
                (
                    'heads.cats.mods',
                    array
                    (
                        'NAME' => html_escape($mod_row['user_name']),
                        'URL' => html_escape(make_url('user', array('user_id' => $mod_row['user_id'])))
                    )
                );
            }





			/* */

			$tpl->data['heads'][$head_i]['cats'][$cat_i]['MOD_COUNT'] = $mod_count;
        }
    }





    /* */

	$tpl->add_var('SHOW_BOARD', $show_board);





    /* */

	$tpl->add_var('PAGE_TITLE', html_escape($l['title_board']));
?>
Return current item: pkZoos Website