Menu

[r73]: / cms / trunk / views.cpp  Maximize  Restore  History

Download this file

276 lines (246 with data), 5.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
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//
// C++ Implementation: views
//
// Description:
//
//
// Author: artik <artik@art-laptop>, (C) 2007
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "views.h"
#include "templates/look.h"
#include <cppcms/text_tool.h>
#include <boost/format.hpp>
#include "blog.h"
using boost::format;
using boost::str;
void View_Comment::init(comment_t &c)
{
Text_Tool tt;
user_t user;
if(c.author_id!=-1 && users->id.get(c.author_id,user)) {
tt.text2html(user.username,author);
}
else if(!(c.author=="")) {
tt.text2html(c.author,author);
url=(char const *)c.url;
}
else {
author="unknown";
}
string text;
texts->get(c.content_id,text);
tt.markdown2html(text,message);
blog->date(c.publish_time,date);
}
int View_Comment::render(Renderer &r,Content &c, string &out)
{
c[TV_username]=author;
c[TV_content]=message;
c[TV_date]=date;
if(url!="") {
c[TV_url]=url;
}
else {
c[TV_url].reset();
}
return r.render(out);
}
void View_Post::ini_share(post_t &p)
{
Text_Tool tt;
tt.text2html(p.title,title);
blog->date(p.publish,date);
user_t user;
if(users->id.get(p.author_id,user)) {
tt.text2html(user.username,author);
}
permlink=str(format(blog->fmt.post) % p.id);
}
void View_Post::ini_full(post_t &p)
{
Text_Tool tt;
ini_share(p);
string abstract;
string content;
texts->get(p.abstract_id,abstract);
tt.markdown2html(abstract,this->abstract);
if(p.content_id!=-1) {
texts->get(p.content_id,content);
tt.markdown2html(content,this->content);
has_content=true;
}
else {
has_content=false;
}
string post_comment=str(format(blog->fmt.add_comment) % p.id);
int n=post_comment.size()/2;
post_comment2=post_comment.substr(n);
post_comment1=post_comment.substr(0,n-1);
Comments::posttime_c cur(comments->posttime);
has_comments=false;
for(cur.gte(comment_t::sec_t(p.id,0));
cur && cur.val().post_id==p.id;
cur.next())
{
if(!cur.val().moderated) {
continue;
}
shared_ptr<View_Comment> com(new View_Comment(blog));
comments_list.push_back(com);
comment_t comment=cur;
com->init(comment);
has_comments=true;
}
}
void View_Post::ini_short(post_t &p)
{
Text_Tool tt;
ini_share(p);
string abstract;
texts->get(p.abstract_id,abstract);
tt.markdown2html(abstract,this->abstract);
if(p.content_id!=-1) {
has_content=true;
}
else {
has_content=false;
}
has_comments=false;
}
void View_Post::ini_feed(post_t &p)
{
Text_Tool tt;
ini_share(p);
string abstract,abstract_html;
texts->get(p.abstract_id,abstract);
tt.markdown2html(abstract,abstract_html);
// For xml feed we need convert html to text
tt.text2html(abstract_html,this->abstract);
has_content=has_comments=false;
}
int View_Post::render(Renderer &r,Content &c, string &out)
{
c[TV_post_permlink]=permlink;
c[TV_post_title]=title;
c[TV_date]=date;
c[TV_author]=author;
c[TV_post_abstract]=abstract;
c[TV_post_comment_url_1]=post_comment1;
c[TV_post_comment_url_2]=post_comment2;
if(has_content) {
c[TV_content]=content;
}
else {
c[TV_content].reset();
}
list<shared_ptr<View_Comment> >::iterator i=comments_list.begin();
id=r.render(out);
while(id) {
if(id==TV_get_comment) {
if(has_comments && i!=comments_list.end()) {
c[TV_next_comment]=true;
id=(*i)->render(r,c,out);
i++;
}
else {
c[TV_next_comment]=false;
return r.render(out);
}
}
else {
return id;
}
}
}
void View_Main_Page::ini_share()
{
option_t opt;
options->get(BLOG_TITLE,opt);
title=(char const*)opt.value;
options->get(BLOG_DESCRIPTION,opt);
description=(char const*)opt.value;
}
void View_Main_Page::ini_main(int id)
{
ini_share();
Posts::publish_c cur(posts->publish);
if(id==-1) {
cur.end();
}
else {
post_t p;
posts->id.get(id,p);
cur.lte(p.publish);
}
int counter=0;
latest_posts.reserve(10);
for(;;) {
if(cur) {
if(!cur.val().is_open)
continue;
if(counter==10) {
from=str(format(blog->fmt.main_from) % cur.val().id);
break;
}
post_t post;
cur.get(post);
shared_ptr<View_Post> ptr(new View_Post(blog));
ptr->ini_short(post);
latest_posts.push_back(ptr);
counter++;
cur.next();
}
break;
}
disp=SUMMARY;
}
void View_Main_Page::ini_post(int id)
{
shared_ptr<View_Post> ptr(new View_Post(blog));
post_t post;
if(!(posts->id.get(id,post))){
throw HTTP_Error("Post Not found",true);
}
single_post=ptr;
ini_share();
ptr->ini_full(post);
disp=SINGLE;
}
int View_Main_Page::render( Renderer &r,Content &c,string &out)
{
c[TV_media]=blog->fmt.media;
c[TV_title]=title;
c[TV_blog_name]=title;
c[TV_blog_description]=description;
c[TV_base_url]=global_config.sval("blog.script_path").c_str();
if(disp==SINGLE) {
c[TV_subtitle]=single_post->title;
c[TV_master_content]=TT_post;
return single_post->render(r,c,out);
}
else {
c[TV_master_content]=TT_main_page;
int id;
id=r.render(out);
vector<shared_ptr<View_Post> >::iterator i=latest_posts.begin();
for(;;){
if(id==TV_get_post){
if(i!=latest_posts.end()) {
c[TV_next_post]=true;
id=(*i)->render(r,c,out);
i++;
}
else {
c[TV_next_post]=false;
id=r.render(out);
}
}
else {
return id;
}
}
}
}
MongoDB Logo MongoDB