Add PageListRenderer: Allow rendering pagelist by creationdate and A-Z as before
此提交包含在:
		
							
								
								
									
										66
									
								
								pagelistrenderer.cpp
									
									
									
									
									
										一般檔案
									
								
							
							
						
						
									
										66
									
								
								pagelistrenderer.cpp
									
									
									
									
									
										一般檔案
									
								
							@@ -0,0 +1,66 @@
 | 
			
		||||
#include "pagelistrenderer.h"
 | 
			
		||||
#include "grouper.h"
 | 
			
		||||
 | 
			
		||||
PageListRenderer::PageListRenderer(Template &templ, UrlProvider &provider, Database &database)
 | 
			
		||||
{
 | 
			
		||||
	this->templ = &templ;
 | 
			
		||||
	this->urlProvider = &provider;
 | 
			
		||||
	this->database = &database;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::string PageListRenderer::render(const std::vector<Page> &pagelist, std::string type) const
 | 
			
		||||
{
 | 
			
		||||
	TemplatePage pagelistrendergroup = this->templ->loadResolvedPart("pagelistrender_group");
 | 
			
		||||
	TemplatePage pagelistlink = this->templ->loadResolvedPart("pagelistrender_link");
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	std::function<bool(const std::string &, const std::string &)> lesser = [](const std::string &a, const std::string &b) -> bool {
 | 
			
		||||
		return a < b;
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	std::function<bool(const std::string &, const std::string &)> greater = [](const std::string &a, const std::string &b) -> bool {
 | 
			
		||||
		return a > b;
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	using Grouper = Grouper<std::string, Page, std::function<bool(const std::string &,const std::string &)>>;
 | 
			
		||||
 | 
			
		||||
	Grouper grouper = (type == "letter") ? Grouper(lesser) : Grouper(greater);
 | 
			
		||||
	if(type == "letter")
 | 
			
		||||
	{
 | 
			
		||||
		auto az = [&](const Page &p) -> std::string {
 | 
			
		||||
			int upper = toupper(p.title[0]); // TODO: this is not unicode safe.
 | 
			
		||||
			return { (char)upper };
 | 
			
		||||
		};
 | 
			
		||||
		grouper.group(az, pagelist);
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		auto revisionDao = this->database->createRevisionDao();
 | 
			
		||||
		auto creationdate = [&revisionDao](const Page &p) -> std::string {
 | 
			
		||||
			return utils::formatLocalDate(revisionDao->getRevisionForPage(p.name, 1).value().timestamp, "%Y-%m");
 | 
			
		||||
		};
 | 
			
		||||
		grouper.group(creationdate, pagelist);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	std::stringstream stream;
 | 
			
		||||
	std::string lastGroup = "";
 | 
			
		||||
 | 
			
		||||
	for(auto &entry : grouper.getResults())
 | 
			
		||||
	{
 | 
			
		||||
		std::string groupname = entry.first;
 | 
			
		||||
		const std::vector<const Page*> &pages = entry.second;
 | 
			
		||||
		if(lastGroup != groupname)
 | 
			
		||||
		{
 | 
			
		||||
			lastGroup = groupname;
 | 
			
		||||
			pagelistrendergroup.setVar("groupname", groupname);
 | 
			
		||||
			stream << pagelistrendergroup.render();
 | 
			
		||||
		}
 | 
			
		||||
		for(const Page *p : pages)
 | 
			
		||||
		{
 | 
			
		||||
			pagelistlink.setVar("inner", p->title);
 | 
			
		||||
			pagelistlink.setVar("href", this->urlProvider->page(p->name));
 | 
			
		||||
			stream << pagelistlink.render();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return stream.str();
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										27
									
								
								pagelistrenderer.h
									
									
									
									
									
										一般檔案
									
								
							
							
						
						
									
										27
									
								
								pagelistrenderer.h
									
									
									
									
									
										一般檔案
									
								
							@@ -0,0 +1,27 @@
 | 
			
		||||
#ifndef PAGELISTRENDERER_H
 | 
			
		||||
#define PAGELISTRENDERER_H
 | 
			
		||||
#include "utils.h"
 | 
			
		||||
#include "page.h"
 | 
			
		||||
#include "template.h"
 | 
			
		||||
#include "htmllink.h"
 | 
			
		||||
#include "urlprovider.h"
 | 
			
		||||
#include "database/database.h"
 | 
			
		||||
 | 
			
		||||
class PageListRenderer
 | 
			
		||||
{
 | 
			
		||||
private:
 | 
			
		||||
	Template *templ;
 | 
			
		||||
	UrlProvider *urlProvider;
 | 
			
		||||
	Database *database;
 | 
			
		||||
 | 
			
		||||
public:
 | 
			
		||||
	PageListRenderer(Template &templ, UrlProvider &provider, Database &database);
 | 
			
		||||
 | 
			
		||||
	std::string render(const std::vector<Page> &pages, std::string type) const;
 | 
			
		||||
 | 
			
		||||
	inline static const std::string RENDER_GROUP_BY_LETTER { "letter" };
 | 
			
		||||
	inline static const std::string RENDER_GROUP_BY_CREATIONDATE { "creationdate" };
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
};
 | 
			
		||||
#endif
 | 
			
		||||
@@ -0,0 +1,3 @@
 | 
			
		||||
{qswiki:include:pagelistrender_header}
 | 
			
		||||
{qswiki:var:pagelistcontent}
 | 
			
		||||
{qswiki:include:pagelistrender_footer}
 | 
			
		||||
@@ -0,0 +1 @@
 | 
			
		||||
<div class="letter_search_result">{qswiki:var:groupname}</div>
 | 
			
		||||
@@ -0,0 +1 @@
 | 
			
		||||
Sort by: <a href="{qswiki:var:pagelistletterlink}">A-Z</a> - <a href="{qswiki:var:pagelistcreationdatelink}">Creation date</a>
 | 
			
		||||
@@ -0,0 +1 @@
 | 
			
		||||
<a href="{qswiki:var:href}">{qswiki:var:inner}</a><br>
 | 
			
		||||
		新增問題並參考
	
	封鎖使用者