Menu

[r624]: / examples / trunk / mb / src / forums.cpp  Maximize  Restore  History

Download this file

98 lines (87 with data), 2.2 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "forums.h"
#include "forums_data.h"
#include "mb.h"
#include <boost/lexical_cast.hpp>
#include <cgicc/HTTPRedirectHeader.h>
using namespace dbixx;
namespace data {
new_topic_form::new_topic_form(cppcms::application &a) :
title("title",a.gettext("Tittle")),
author("author",a.gettext("Author")),
comment("comment",a.gettext("Comment")),
submit("submit",a.gettext("Create"))
{
*this & title & author & comment & submit;
title.set_nonempty();
author.set_limits(1,64);
comment.set_limits(1,256);
comment.rows=20;
comment.cols=60;
}
};
namespace apps {
forums::forums(mb &b) :
application(b.worker),
board(b)
{
url.add("^(/(\\w+)?)?$",boost::bind(&forums::display_forums,this,$2));
}
string forums::forums_url(int offset)
{
string link=env->getScriptName();
if(offset==0)
return link;
link.append("/");
link+=boost::lexical_cast<string>(offset);
return link;
}
void forums::display_forums(string page)
{
const unsigned topics_per_page=10;
data::forums c(*this);
board.ini(c);
if(env->getRequestMethod()=="POST") {
c.form.load(*cgi);
if(c.form.validate()) {
dbixx::transaction tr(board.sql);
board.sql<<
"INSERT INTO threads(title) VALUES(?)",
c.form.title.get(),exec();
int id=board.sql.rowid();
board.sql<<
"INSERT INTO messages(thread_id,reply_to,content,author) "
"VALUES (?,0,?,?)",
id,c.form.comment.get(),c.form.author.get(),exec();
tr.commit();
session["author"]=c.form.author.get();
add_header("Status: 302 Found");
set_header(new cgicc::HTTPRedirectHeader(board.thread.user_url(id)));
return;
}
}
int offset= page.empty() ? 0 : atoi(page.c_str());
dbixx::result res;
board.sql<<
"SELECT id,title "
"FROM threads "
"ORDER BY id DESC "
"LIMIT ?,?",offset*topics_per_page,topics_per_page,res;
c.topics.resize(res.rows());
dbixx::row r;
for(int i=0;res.next(r);i++) {
int id;
r>>id>>c.topics[i].title;
c.topics[i].url=board.thread.user_url(id);
}
if(c.topics.size()==topics_per_page) {
c.next_page=forums_url(offset+1);
}
if(offset>0) {
c.prev_page=forums_url(offset-1);
}
if(session.is_set("author")) {
c.form.author.set(session["author"]);
}
render("forums",c);
}
} // namespace apps
MongoDB Logo MongoDB