# -*- tcl-*-
# $Id$
::xowiki::Object new -title "Weblog" -text {
  #
  # A sample Weblog object.
  #
  my initialize -parameter {
    {-page_size:integer 10}
    {-page_number:integer 1}
    {-summary:boolean 0}
    {-category_id ""}
    {-date ""}
    {-tag ""}
    {-ptag ""}
    {-entries_of ""}
  }

  #
  # The following definition is the default rendering per 
  # weblog entry. This is executed in the context of every displayed page.
  #
  Class create EntryRenderer -instproc render {} {
    my instvar package_id name title creator creation_user pretty_date description 
    my log "--W entry [self] [my name] package_id $package_id"
    [my set __parent] instvar weblog_obj

    # We get the instance_attributes, if these are available. For the
    # time being, we have these only in full mode (no summary)
    set link [my detail_link]
    set show_more [expr {[$weblog_obj summary] && [my exists text] && [my text] ne ""}]
    set more [expr {$show_more ? 
                    " <span class='more'> \[<a href='$link'>#xowiki.weblog-more#</a>\]</span>" : ""}]
    append more "<p></p>"
    set my_footer [my htmlFooter]

    append content "<DIV class='post' style='clear: both; background: #f8f8f8;'>" \
        "<h2><a href='$link'>$title</a></h2>" \
        "<p class='auth'>Created by $creator, " \
        "last modified by [::xo::get_user_name $creation_user] " \
        "<span class='date'>$pretty_date</span></p>" \
        $description $more $my_footer \n\
        "</DIV>"
    #my log "--W entry done [self] [my info class] subst=[my do_substitutions] [my name]"
    return $content
  }

  #
  # The following definition is the renderer for the full weblog.
  # This is executed in the context of the whole weblog object
  #
  Class create WeblogRenderer -instproc render {} {
    my instvar filter_msg link name prev_page_link next_page_link 

    set filter ""
    set prev ""
    set next ""
    
    if {[info exists filter_msg]} {
      set filter  "<div class='filter'>$filter_msg</div>"
    } 
    if {[info exists prev_page_link]} {
      set prev "<a href='$prev_page_link'>\
        <img border=0 src='/resources/acs-subsite/left.gif' \
             alt='Previous Page' style='float: left;  top: 0px;'></a>"
    }
    if {[info exists next_page_link]} {
      set next "<a href='$next_page_link'>\
        <img border=0 src='/resources/acs-subsite/right.gif' \
             alt='Next Page' style='float: left;  top: 0px;'></a>"
    }
    return "<div class='weblog'> $filter [next] $prev $next </div>"
  }
  
  my proc content {} {
    my get_parameters
    my log "--get_parameters returned package_id=$package_id"
    set page [my info parent]

    if {[$page exists __including_page]} {
      set i [$page set __including_page]
      #my log "--W including page $i"
      set exclude_item_ids [$i item_id]
      $i set render_adp 0  ;# no double adp substitutions
      $i do_substitutions 0;# no double substitutions in parent
    } else {
      #my log "--W NO including page"
      set exclude_item_ids [$page item_id]
      $page set __no_footer 1
    }
    $page do_substitutions 0; # no double substitutions in page

    # on the weblog-portlet page, an edit-new should not create an object
    ::xo::cc set_parameter object_type ::xowiki::Page

    # the default renderer
    #set renderer       ::xowiki::Weblog::WeblogRenderer
    #set entry_renderer ::xowiki::Weblog::EntryRenderer

    # the above custom renderers
    set renderer       [self]::WeblogRenderer
    set entry_renderer [self]::EntryRenderer 
    
    set w [::xowiki::Weblog new -destroy_on_cleanup \
               -package_id $package_id \
               -page_size $page_size \
               -page_number $page_number \
               -summary $summary \
               -date $date \
               -category_id $category_id \
               -tag $tag \
               -ptag $ptag \
               -entries_of $entries_of \
               -exclude_item_ids $exclude_item_ids \
               -entry_renderer $entry_renderer \
              ]

    $w mixin add $renderer
    set html [$w render]
    $page do_substitutions 1; # reset to default
    return $html
  }
  
}
