Add PageListRenderer: Allow rendering pagelist by creationdate and A-Z as before
This commit is contained in:
bovenliggende
e217218a3f
commit
b5b2a42839
66
pagelistrenderer.cpp
Normal file
66
pagelistrenderer.cpp
Normal file
@ -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
Normal file
27
pagelistrenderer.h
Normal file
@ -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
|
3
template/quitesimple/pagelistrender
Normal file
3
template/quitesimple/pagelistrender
Normal file
@ -0,0 +1,3 @@
|
||||
{qswiki:include:pagelistrender_header}
|
||||
{qswiki:var:pagelistcontent}
|
||||
{qswiki:include:pagelistrender_footer}
|
0
template/quitesimple/pagelistrender_footer
Normal file
0
template/quitesimple/pagelistrender_footer
Normal file
1
template/quitesimple/pagelistrender_group
Normal file
1
template/quitesimple/pagelistrender_group
Normal file
@ -0,0 +1 @@
|
||||
<div class="letter_search_result">{qswiki:var:groupname}</div>
|
1
template/quitesimple/pagelistrender_header
Normal file
1
template/quitesimple/pagelistrender_header
Normal file
@ -0,0 +1 @@
|
||||
Sort by: <a href="{qswiki:var:pagelistletterlink}">A-Z</a> - <a href="{qswiki:var:pagelistcreationdatelink}">Creation date</a>
|
1
template/quitesimple/pagelistrender_link
Normal file
1
template/quitesimple/pagelistrender_link
Normal file
@ -0,0 +1 @@
|
||||
<a href="{qswiki:var:href}">{qswiki:var:inner}</a><br>
|
Laden…
Verwijs in nieuw issue
Block a user