Added some template changes - Document List now shows description
[biaweb2.git] / biawebdoclist.hpp
1 #ifndef __BIAWEBDOCITEMLIST__
2 #define __BIAWEBDOCITEMLIST__
3 #include <ctime>
4 #include <string>
5 #include <fstream>
6 #include <iostream>
7 #include <iomanip>
8 #include <list>
9 #include <fmt/format.h>
10 #include <fmt/chrono.h>
11 #include "biawebstrings.hpp"
12 #include "biawebdocument.hpp"
13 #include "biawebtemplate.hpp"
14
15 // to implement a list of items (documents) with creation/modified date/time display
16 namespace biaweb {
17 // to implement a single document item
18 class DocListItem {
19 protected:
20 // Just the required fields to build the item
21 std::string title;
22 std::string url;
23 std::string desc;
24 std::time_t ctime;
25 std::time_t mtime;
26 public:
27 DocListItem (std::string title, std::string url,
28 std::time_t ctime, std::time_t mtime, std::string desc ) {
29 this->title = escape_html (title);
30 this->url = url;
31 this->ctime = ctime;
32 this->mtime = mtime;
33 this->desc = desc;
34 }
35
36 std::string get_desc () {
37 return this->desc;
38 }
39
40 void set_desc (std::string desc) {
41 this->desc = escape_html (desc);
42 }
43
44 std::time_t get_mtime() {
45 return this->mtime;
46 }
47 void set_mtime(std::time_t mtime) {
48 this->mtime = mtime;
49 }
50 std::time_t get_ctime() {
51 return this->ctime;
52 }
53 void set_ctime(std::time_t ctime) {
54 this->ctime = ctime;
55 }
56
57 std::string get_url() {
58 return this->url;
59 }
60
61 void set_url(std::string url) {
62 this->url = url;
63 }
64
65 std::string get_title() {
66 return this->title;
67 }
68 void set_title(std::string title) {
69 this->title = escape_html (title);
70 }
71
72 // output to HTML vide the template
73 std::string to_html (Template *t);
74 };
75
76 // output to HTML vide the template
77 std::string DocListItem::to_html (Template *t) {
78 std::string templstr = t->get_doclistitem_tpl ();
79 std::tm c, m;
80 c = *std::localtime (&this->ctime);
81 m = *std::localtime (&this->mtime);
82
83 std::string outputhtml = fmt::format (templstr, fmt::arg("url", this->url),
84 fmt::arg("doctitle", this->title),
85 fmt::arg("desc", this->desc),
86 fmt::arg("cdate", c),
87 fmt::arg("mdate", m));
88
89 return outputhtml;
90 }
91
92 // to implement a document list (or table)
93 class DocList {
94 protected:
95 std::string title;
96 std::list<DocListItem> items;
97 public:
98 void set_title (std::string title) {
99 this->title = escape_html (title);
100 }
101 // add a document item
102 void add_document_item (DocListItem docitem) {
103 this->items.insert (this->items.cend(), docitem);
104 }
105 // render to HTML from a template
106 std::string to_html (Template *t);
107 };
108
109 std::string DocList::to_html (Template *t) {
110 std::string templstr = t->get_doclist_tpl ();
111
112 std::string outputhtml = "";
113 // if the number of elements is non zero
114 if (this->items.size () != 0) {
115 std::string docitems = "";
116 for (DocListItem item : this->items)
117 docitems += item.to_html (t);
118
119 outputhtml = fmt::format (templstr,
120 fmt::arg ("title", this->title),
121 fmt::arg ("docitems", docitems));
122 }
123 return outputhtml;
124 }
125 }
126
127 #endif