Menu

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

Download this file

472 lines (426 with data), 9.6 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
//
// 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"
#include "error.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);
if(c.url[0]!=0){
tt.text2url(c.url,url);
}
}
else {
author="unknown";
}
string text;
texts->get(c.content_id,text);
tt.markdown2html(text,message);
blog->date(c.publish_time,date);
del_url=str(format(blog->fmt.del_comment) % c.id);
}
int View_Comment::render(Renderer &r,Content &c, string &out)
{
c[TV_username]=author;
c[TV_content]=message;
c[TV_date]=date;
if(blog->userid!=-1){
c[TV_delete_url]=del_url;
}
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);
}
edit_url=str(format(blog->fmt.edit_post) % p.id);
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);
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;
c[TV_has_content]=has_content;
if(blog->userid!=-1) {
c[TV_edit_url]=edit_url;
}
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,bool feed)
{
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;
int max_posts=feed ? 10 : 5;
latest_posts.reserve(max_posts);
for(;;) {
if(cur) {
if(!cur.val().is_open){
cur.next();
continue;
}
if(counter==max_posts) {
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));
if(feed){
ptr->ini_feed(post);
}
else {
ptr->ini_short(post);
}
latest_posts.push_back(ptr);
counter++;
cur.next();
}
else {
break;
}
}
disp=SUMMARY;
}
void View_Main_Page::ini_post(int id,bool preview)
{
shared_ptr<View_Post> ptr(new View_Post(blog));
post_t post;
if(!(posts->id.get(id,post)) || (!post.is_open && !preview)){
throw Error(Error::E404);
}
single_post=ptr;
ini_share();
ptr->ini_full(post);
disp=SINGLE;
}
void View_Main_Page::ini_error(int id)
{
ini_share();
disp=ERROR;
error_code=id;
}
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_admin_url]=blog->fmt.admin;
c[TV_base_url]=global_config.sval("blog.script_path").c_str();
c[TV_host]=global_config.sval("blog.host").c_str();
c[TV_rss_posts]=blog->fmt.feed;
if(from!="") {
c[TV_next_page_link]=from;
}
if(disp==SINGLE) {
c[TV_subtitle]=single_post->title;
c[TV_master_content]=TT_post;
return single_post->render(r,c,out);
}
else if(disp==SUMMARY){
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;
}
}
}
else if(disp==ERROR){
c[TV_master_content]=TT_error;
switch(error_code) {
case Error::E404:
c[TV_error_404]="";
break;
case Error::COMMENT_FIELDS:
c[TV_error_comment]="";
break;
}
return r.render(out);
}
}
void View_Admin::ini_share()
{
option_t opt;
options->get(BLOG_TITLE,opt);
blog_name=(char const*)opt.value;
}
void View_Admin::ini_login()
{
ini_share();
page=LOGIN;
}
void View_Admin::ini_edit(int id)
{
ini_share();
page=POST;
post=shared_ptr<View_Admin_Post>(new View_Admin_Post(blog));
post->ini(id);
}
void View_Admin::ini_main()
{
ini_share();
main=shared_ptr<View_Admin_Main>(new View_Admin_Main(blog));
main->ini();
page=MAIN;
}
int View_Admin::render( Renderer &r,Content &c,string &out)
{
c[TV_media]=blog->fmt.media;
c[TV_base_url]=global_config.sval("blog.script_path").c_str();
c[TV_admin_url]=blog->fmt.admin.c_str();
c[TV_logout_url]=blog->fmt.logout;
c[TV_blog_name]=blog_name;
switch(page){
case MAIN:
c[TV_master_content]=TT_admin_main;
return main->render(r,c,out);
case POST:
c[TV_master_content]=TT_admin_editpost;
return post->render(r,c,out);
case LOGIN:
c[TV_master_content]=TT_admin_login;
c[TV_login_url]=blog->fmt.login.c_str();
return r.render(out);
}
return r.render(out);
}
void View_Admin_Main::ini()
{
Text_Tool tt;
Posts::is_open_c cur(posts->is_open);
for(cur<=false;cur;cur.next()) {
post_ref post_data;
post_t const &post=cur;
post_data.edit_url=
str(format(blog->fmt.edit_post) % post.id);
tt.text2html(post.title,post_data.title);
unpublished_posts.push_back(post_data);
}
Comments::id_c post_cur(comments->id);
int i;
for(i=0,post_cur.end(); i<10 && post_cur; i++, post_cur.next()) {
comment_ref com;
com.url=str(format(blog->fmt.post) % post_cur.val().post_id);
tt.text2html(post_cur.val().author.c_str(),com.author);
latest_comments.push_back(com);
}
}
int View_Admin_Main::render(Renderer &r,Content &c,string &out)
{
c[TV_new_post_url]=blog->fmt.new_post;
int id=r.render(out);
list<post_ref>::iterator it=unpublished_posts.begin();
list<comment_ref>::iterator it_c=latest_comments.begin();
for(;;) {
if(id==TV_get_post && it!=unpublished_posts.end()) {
c[TV_next_post]=true;
c[TV_post_title]=it->title.c_str();
c[TV_edit_url]=it->edit_url;
it++;
}
else if(id==TV_get_post){
c[TV_next_post]=false;
}
else if(id==TV_get_comment && it_c!=latest_comments.end()){
c[TV_next_comment]=true;
c[TV_username]=it_c->author;
c[TV_post_permlink]=it_c->url;
it_c++;
}
else if(id==TV_get_comment) {
c[TV_next_comment]=false;
}
else
return id;
id=r.render(out);
}
}
void View_Admin_Post::ini( int id)
{
Text_Tool tt;
post_t post_data;
if(id!=-1) {
post_id=str(format("%1%") % id);
post_url=str(format(blog->fmt.update_post) % id);
preview=str(format(blog->fmt.preview) % id);
}
else {
post_url=blog->fmt.add_post;
return;
}
if(!posts->id.get(id,post_data)){
throw Error(Error::E404);
}
title=post_data.title.c_str();
string abs_tmp,cnt_tmp;
if(post_data.abstract_id!=-1 && texts->get(post_data.abstract_id,abs_tmp)) {
tt.text2html(abs_tmp,abstract);
}
if(post_data.content_id!=-1 && texts->get(post_data.content_id,cnt_tmp)) {
tt.text2html(cnt_tmp,content);
}
}
int View_Admin_Post::render( Renderer &r,Content &c,string &out)
{
c[TV_submit_post_url]=post_url.c_str();
if(post_id.size()>0) {
c[TV_post_id]=post_id.c_str();
c[TV_preview_url]=preview.c_str();
}
c[TV_post_title]=title.c_str();
c[TV_abstract]=abstract.c_str();
c[TV_content]=content.c_str();
return r.render(out);
}
MongoDB Logo MongoDB