ad_page_contract {
    @author Richard Hamilton <richard.hamilton@webteamworks.com>
    @creation-date 2005-09-27
} {
    message_id:integer,notnull
    {send_message_p:boolean 0}
    {confirm_send_p:boolean 0}
} -properties {
    title:onevalue
    context:onevalue
    sender:onevalue
    subject:onevalue
    body:onevalue
}

# Check permissions on the package_id
ad_require_permission [ad_conn package_id] read

# Obtain the user_id of the requesting user
set user_id [ad_verify_and_get_user_id]


# Now perform checks to ensure that the requesting user is supposed to be looking at this message!

# Is this a 'sent' message or one in the process of being created?
db_1row message_sent_p {
        select im_message__sent_p(:message_id) as message_sent_p
}

# Is the requesting user the author?
db_1row message_author_p {
        select im_message__author_p(:message_id, :user_id) as message_author_p
}

# Is the requesting user an addressee?
db_1row message_addressee_p {
        select im_message__addressee_p(:message_id, :user_id) as message_addressee_p
}

# If the message has not been sent yet then the only person who should see it is the
# author, so if the user is not the author, abort the page.
# If the message has been sent, the only people who should see it are addressees.
if { [string is false $message_sent_p] } {
    
    if { [string is false $message_author_p] } { ad_script_abort }

} elseif { [string is false $message_addressee_p] } {

    ad_script_abort
}

set context [list "Display Message"]

db_1row message-display {
        select sender_user_id, party_name as sender, date_trunc('second', sent_timestamptz) as sent_date, subject, body, priority_p
        from im_message m, party_names p
        where m.sender_user_id = p.party_id
        and m.message_id = :message_id
}

db_multirow to_addressees to_addressees {
        select party_name as addressee_name, addressee_id as party_id
        from im_addressees a, party_names p
        where a.addressee_id = p.party_id
        and address_mode_id = 1
        and exists (
                select * from im_message m
                where m.message_id = :message_id
                and a.message_id = m.message_id)
}

db_multirow cc_addressees cc_addressees {
        select party_name as addressee_name, addressee_id as party_id
        from im_addressees a, party_names p
        where a.addressee_id = p.party_id
        and address_mode_id = 2
        and exists (
                select * from im_message m
                where m.message_id = :message_id
                and a.message_id = m.message_id)
}

# Is the requesting user the sender? If so user should see all bcc addressees. Have to run query
# anyway because multiple rowcount is referenced in the adp.
db_multirow bcc_addressees bcc_addressees {
        select party_name as addressee_name, addressee_id as party_id
        from im_addressees a, party_names p
        where a.addressee_id = p.party_id
        and address_mode_id = 3
        and exists (
                select * from im_message m
                where m.message_id = :message_id
                and a.message_id = m.message_id)
}

# Prepare the blank value.
set bcc_user_name ""

if { [string is false $message_author_p] } {

    # Is the requesting user a bcc addressee? If so user should see self in bcc addressees field.
    db_1row user_bcc_p {
        select im_message__user_bcc_p(:message_id, :user_id) as user_bcc_p
    }
    
    if { [string is true $user_bcc_p] } {
        # Lookup the user's name for the ui display.
        db_1row bcc_user_name {
            select party_name as bcc_user_name
            from party_names
            where party_id = :user_id
        }
    }

}

set title "Message from $sender dated: $sent_date"

set sender [ad_text_to_html $sender]
set subject [ad_text_to_html $subject]
set body [ad_text_to_html $body]

# check to see if this user has viewed the message, if not set read_p to true for that user.
db_1row mark_read_if_appropriate {
    select im_mark_read_p (:message_id, :user_id)
}

# This file displays a message when received, and both prior to edit and also prior to sending. Checks
# here are for requirement to send and confirmation to send.

if { [string is true $message_author_p] && [string is false $message_sent_p] && [string is true $send_message_p] && [string is false $confirm_send_p] } {

  # Check permissions on the package_id
  ad_require_permission [ad_conn package_id] write
  set title "Send Message"
  set context [list $title]
  set yes_url "./message-display?[export_vars { {message_id $message_id} {send_message_p 1} { confirm_send_p 1 } }]"
  set no_url "./message-display?[export_vars { {message_id $message_id} {send_message_p 0} } ]"

  return
}

if { [string is true $message_author_p] && [string is false $message_sent_p] && [string is true $send_message_p] && [string is true $confirm_send_p] } {

    # Check permissions on the package_id
    ad_require_permission [ad_conn package_id] write

    db_1row send_message {
        select im_message__send (:message_id, :user_id) as message_send_successful_p
    }

}

db_1row message_sent_p {
        select im_message__sent_p(:message_id) as message_sent_p
}

ad_return_template

