ad_page_contract {

    one forum view

    @author Ben Adida (ben@openforce.net)
    @creation-date 2002-05-24
    @cvs-id $Id: threads-chunk.tcl,v 1.2.2.8 2005/04/08 18:41:57 jader Exp $

}

# Get forum data
forum::get -forum_id $forum_id -array forum

if { $moderate_p } {
    set replies reply_count
} else {
    set replies approved_reply_count
}

set actions [list]

# new postings are allowed if

# 1. Users can create new threads AND the posting policy is open or moderated
# 2. User is a moderator or adminsitrator

if {([forum::new_questions_allowed_p -forum_id $forum_id] && ($forum(posting_policy) == "open" || $forum(posting_policy) == "moderated")) ||  [template::util::is_true $permissions(admin_p)] ||  [template::util::is_true $permissions(moderate_p)]  } {
    lappend actions [_ forums.Post_a_New_Message] [export_vars -base "message-post" { forum_id }] {}
}

if { [template::util::is_true $permissions(admin_p)] } {
    lappend actions [_ forums.Administer] [export_vars -base "admin/forum-edit" { forum_id {return_url [ad_return_url]}}] {}
    lappend actions [_ forums.Subscribe_others] [export_vars -base "admin/subscribe-others" { forum_id {return_url [ad_return_url]}}] {}
}

if { [template::util::is_true $permissions(moderate_p)] } {
    lappend actions [_ forums.ManageModerate] [export_vars -base "moderate/forum" { forum_id }] {}
}

template::list::create \
    -name messages \
    -multirow messages \
    -page_size 30 \
    -page_query_name messages_select_paginate \
    -pass_properties { moderate_p } \
    -actions $actions \
    -elements {
        subject {
            label "#forums.Subject#"
            link_url_col message_url
            display_template {
                <if @messages.new_p@><b>@messages.subject@</b></if>
                <else>@messages.subject@</else>
            }
        }
        state_pretty {
            label "\#forums.Moderate\#"
            hide_p {[ad_decode $moderate_p 1 0 1]}
        }
        user_name {
            label "#forums.Author#"
            link_url_col user_url
        }
        n_messages {
            label "#forums.Replies#"
            display_col n_messages_pretty
            html { align right }
        }
        posting_date {
            label "#forums.First_Post#"
            display_col posting_date_pretty
        }
        last_child_post {
            label "#forums.Last_Post#"
            display_col last_child_post_pretty
        }
    } -orderby {
        posting_date {
            label "#forums.First_Post#"
            orderby posting_date
            default_direction desc
        }
        last_child_post {
            label "#forums.Last_Post#"
            orderby last_child_post
            default_direction desc
        }
        subject {
            label "#forums.Subject#"
            orderby upper(subject)
        }
        user_name {
            label "#forums.Author#"
            orderby_asc_name "orderby_user_name_asc"
            orderby_desc_name "orderby_user_name_desc"
        }
        n_messages {
            label "#forums.Replies#"
            orderby_asc {n_messages asc, posting_date desc}
            orderby_desc {n_messages desc, posting_date desc}
        }
    } -filters {
        forum_id {}
    }

db_multirow -extend { 
    last_child_post_pretty
    posting_date_pretty
    message_url
    user_url
    n_messages_pretty
    state_pretty
} messages messages_select {} {
    set last_child_post_ansi [lc_time_system_to_conn $last_child_post_ansi]
    set last_child_post_pretty [lc_time_fmt $last_child_post_ansi "%x %X"]

    set posting_date_ansi [lc_time_system_to_conn $posting_date_ansi]
    set posting_date_pretty [lc_time_fmt $posting_date_ansi "%x %X"]

    set message_url [export_vars -base "message-view" { message_id }]
    set user_url [export_vars -base "user-history" { user_id }]
    set n_messages_pretty [lc_numeric $n_messages]

    switch $state {
        pending {
            set state_pretty [_ forums.Pending]
        }
        rejected {
            set state_pretty [_ forums.Rejected]
        }
        default {
            set state_pretty {}
        }
    }
}

if {[exists_and_not_null alt_template]} {
  ad_return_template $alt_template
}

