# Copyright (C) 2003-2005 Kouichirou Eto, All rights reserved.

$LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")

module Qwik
  class Action
    def plg_comment
      msg = @req.query["message"]
      if msg.nil? || msg == ""
	return comment_form
      end

      user = MailAddress.new(@req.user).obfuscate
      ymdx = Time.now.ymdx
      content = ":#{user} (#{ymdx}):#{msg}\n"
      page = @site[@req.base]
      page.add(content)
      c_make_log("comment") # COMMENT
      c_monitor("comment") # COMMENT
      url = @req.base+".html"
      return [
	[:h2, _("Message is added.")],
	[:p, _("Go back"), " : ", [:a, {:href=>url}, url]]
      ]
    end

    def comment_form
      pagename = @req.base
      user = plg_login_user
      [:form, {:action=>pagename+".html"},
	[:dl,
	  [:dt, _("User")],
	  [:dd, [:em, user]],
	  [:dt, _("Message")],
	  [:dd, [:textarea, {:rows=>"7", :name=>"message", :cols=>"40"}, ""]],
	  [:dd, [:input, {:value=>"POST", :type=>"submit"}]]]]
    end
  end
end

if $0 == __FILE__
  require "t/test"
  $debug = true
end

if $debug
  class TC_ActComment < Test::Unit::TestCase
    include TestSession

    def test_comment
      t_add_user
      page = @site["FrontPage"]
      page.store("{{comment}}")

      session("/test/")
      #pw("//html")
      #pw("//form")
      assert_xpath([:textarea, {:cols=>"40", :rows=>"7", :name=>"message"}, ""],
		   "//textarea")

      session("/test/?message=Hi")
      assert_text("Message is added.", "h2")

      session("/test/")
      assert_xpath([:dd, "Hi"], "//div[@class='section']/dl[2]/dd")

      page = @site["_SiteChanged"]
      assert_match(/^,[.0-9]+,user@e.com,comment,FrontPage$/, page.load)
    end
  end
end
