blog handler optimized, blog user handler created
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
note
|
||||
description: "Request handler related to /blogs."
|
||||
author: "Dario B<>sch <daboesch@student.ethz.ch>"
|
||||
date: "$Date: 2015-05-18 13:49:99 +0100 (lun., 18 mai 2015) $"
|
||||
revision: "$966167$"
|
||||
date: "$Date: 2015-05-18 13:49:00 +0100 (lun., 18 mai 2015) $"
|
||||
revision: "$9661667$"
|
||||
|
||||
class
|
||||
BLOG_HANDLER
|
||||
@@ -64,114 +64,128 @@ feature -- HTTP Methods
|
||||
-- <Precursor>
|
||||
local
|
||||
l_page: CMS_RESPONSE
|
||||
s: STRING
|
||||
n: CMS_NODE
|
||||
lnk: CMS_LOCAL_LINK
|
||||
page_number:NATURAL_16
|
||||
page_number:NATURAL_32
|
||||
do
|
||||
-- At the moment the template is hardcoded, but we can
|
||||
-- get them from the configuration file and load them into
|
||||
-- the setup class.
|
||||
|
||||
--Check if a page is given, if not start with page 1
|
||||
if req.path_info.ends_with_general ("/blogs") then
|
||||
-- No page number given, set to 0
|
||||
page_number := 1
|
||||
else
|
||||
-- Read page number from get variable
|
||||
page_number := page_number_path_parameter (req)
|
||||
end
|
||||
|
||||
-- Ensure that page is never 0 (happens if /blogs/ is routed and then "" interpreted as 0)
|
||||
if page_number = 0 then page_number := 1 end
|
||||
-- Read the page number from get variable
|
||||
page_number := page_number_path_parameter (req)
|
||||
|
||||
create {GENERIC_VIEW_CMS_RESPONSE} l_page.make (req, res, api)
|
||||
l_page.add_variable (node_api.nodes, "nodes")
|
||||
|
||||
|
||||
-- Output the title. If more than one page, also output the current page number
|
||||
create s.make_from_string ("<h2>Blog")
|
||||
if more_than_one_page then
|
||||
s.append (" (Page " + page_number.out + " of " + pages.out + ")")
|
||||
-- Get the posts from the current page (limited by entries per page)
|
||||
end
|
||||
s.append ("</h2>")
|
||||
|
||||
-- Get the posts from the current page (given by page number and entries per page)
|
||||
if attached node_api.blogs_order_created_desc_limited (entries_per_page, (page_number-1) * entries_per_page) as lst then
|
||||
|
||||
-- List all posts of the blog
|
||||
s.append ("<ul class=%"cms_blog_nodes%">%N")
|
||||
across
|
||||
lst as ic
|
||||
loop
|
||||
n := ic.item
|
||||
lnk := node_api.node_link (n)
|
||||
s.append ("<li class=%"cms_type_"+ n.content_type +"%">")
|
||||
|
||||
-- Output the creation date
|
||||
s.append (creation_date_html(n))
|
||||
|
||||
-- Output the author of the post
|
||||
s.append (author_html(n))
|
||||
|
||||
-- Output the title of the post as a link (to the detail page)
|
||||
s.append (title_html(n, l_page))
|
||||
|
||||
-- Output the summary of the post and a more link to the detail page
|
||||
s.append (summary_html(n, l_page))
|
||||
|
||||
s.append ("</li>%N")
|
||||
end
|
||||
|
||||
-- End of post list
|
||||
s.append ("</ul>%N")
|
||||
|
||||
-- Pagination
|
||||
s.append (pagination_html(page_number))
|
||||
|
||||
--end
|
||||
end
|
||||
|
||||
l_page.set_main_content (s)
|
||||
l_page.set_main_content (main_content_html(page_number, l_page))
|
||||
l_page.add_block (create {CMS_CONTENT_BLOCK}.make ("nodes_warning", Void, "/blogs/ is not yet fully implemented<br/>", Void), "highlighted")
|
||||
l_page.execute
|
||||
end
|
||||
|
||||
feature {NONE} -- Query
|
||||
feature -- Query
|
||||
|
||||
|
||||
posts (page_number: NATURAL_32) : LIST[CMS_NODE]
|
||||
-- The posts to list on the given page
|
||||
do
|
||||
Result := node_api.blogs_order_created_desc_limited (entries_per_page, (page_number-1) * entries_per_page)
|
||||
end
|
||||
more_than_one_page : BOOLEAN
|
||||
-- Checks if all posts fit on one page (FALSE) or if more than one page is needed (TRUE)
|
||||
do
|
||||
Result := entries_per_page < node_api.blogs_count
|
||||
Result := entries_per_page < total_entries
|
||||
end
|
||||
|
||||
|
||||
pages : NATURAL_16
|
||||
pages : NATURAL_32
|
||||
-- Returns the number of pages needed to display all posts
|
||||
require
|
||||
entries_per_page > 0
|
||||
local
|
||||
tmp: REAL_32
|
||||
do
|
||||
tmp := node_api.blogs_count.to_real / entries_per_page.to_real;
|
||||
Result := tmp.ceiling.to_natural_16
|
||||
tmp := total_entries.to_real_32 / entries_per_page.to_real_32;
|
||||
Result := tmp.ceiling.to_natural_32
|
||||
end
|
||||
|
||||
page_number_path_parameter (req: WSF_REQUEST): NATURAL_16
|
||||
-- Returns the page number from the path /blogs/{page}. It's an unsigned integere since negative pages are not allowed
|
||||
page_number_path_parameter (req: WSF_REQUEST): NATURAL_32
|
||||
-- Returns the page number from the path /blogs/{page}. It's an unsigned integer since negative pages are not allowed
|
||||
local
|
||||
s: STRING
|
||||
do
|
||||
Result := 1 -- default if not get variable is set
|
||||
if attached {WSF_STRING} req.path_parameter ("page") as p_page then
|
||||
s := p_page.value
|
||||
if s.is_natural_16 then
|
||||
Result := s.to_natural_16
|
||||
if s.is_natural_32 then
|
||||
if s.to_natural_32 > 1 then
|
||||
Result := s.to_natural_32
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
feature {NONE} -- HTML Output
|
||||
total_entries : NATURAL_32
|
||||
-- Returns the number of total entries/posts
|
||||
do
|
||||
Result := node_api.blogs_count.to_natural_32
|
||||
end
|
||||
|
||||
feature -- HTML Output
|
||||
|
||||
main_content_html (page_number: NATURAL_32; page: CMS_RESPONSE) : STRING
|
||||
-- Return the content of the page as a html string
|
||||
local
|
||||
n: CMS_NODE
|
||||
lnk: CMS_LOCAL_LINK
|
||||
do
|
||||
-- Output the title. If more than one page, also output the current page number
|
||||
create Result.make_from_string (page_title_html(page_number))
|
||||
|
||||
-- Get the posts from the current page (given by page number and entries per page)
|
||||
if attached posts(page_number) as lst then
|
||||
|
||||
-- List all posts of the blog
|
||||
Result.append ("<ul class=%"cms_blog_nodes%">%N")
|
||||
across
|
||||
lst as ic
|
||||
loop
|
||||
n := ic.item
|
||||
lnk := node_api.node_link (n)
|
||||
Result.append ("<li class=%"cms_type_"+ n.content_type +"%">")
|
||||
|
||||
-- Output the creation date
|
||||
Result.append (creation_date_html(n))
|
||||
|
||||
-- Output the author of the post
|
||||
Result.append (author_html(n))
|
||||
|
||||
-- Output the title of the post as a link (to the detail page)
|
||||
Result.append (title_html(n, page))
|
||||
|
||||
-- Output the summary of the post and a more link to the detail page
|
||||
Result.append (summary_html(n, page))
|
||||
|
||||
Result.append ("</li>%N")
|
||||
end
|
||||
|
||||
-- End of post list
|
||||
Result.append ("</ul>%N")
|
||||
|
||||
-- Pagination
|
||||
Result.append (pagination_html(page_number))
|
||||
|
||||
--end
|
||||
end
|
||||
end
|
||||
|
||||
page_title_html (page_number: NATURAL_32) : STRING
|
||||
-- Returns the title of the page as a html string. It shows the current page number
|
||||
do
|
||||
create Result.make_from_string ("<h2>Blog")
|
||||
if more_than_one_page then
|
||||
Result.append (" (Page " + page_number.out + " of " + pages.out + ")")
|
||||
-- Get the posts from the current page (limited by entries per page)
|
||||
end
|
||||
Result.append ("</h2>")
|
||||
end
|
||||
|
||||
creation_date_html (n: CMS_NODE) : STRING
|
||||
-- returns the creation date. At the moment hard coded as html
|
||||
@@ -227,10 +241,10 @@ feature {NONE} -- HTML Output
|
||||
end
|
||||
end
|
||||
|
||||
pagination_html (page_number: NATURAL_16) : STRING
|
||||
pagination_html (page_number: NATURAL_32) : STRING
|
||||
-- returns a html string with the pagination links (if necessary)
|
||||
local
|
||||
tmp: NATURAL_16
|
||||
tmp: NATURAL_32
|
||||
do
|
||||
Result := ""
|
||||
if more_than_one_page then
|
||||
|
||||
Reference in New Issue
Block a user