Menu

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

Download this file

591 lines (529 with data), 12.7 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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
//
// C++ Implementation: views
//
// Description:
//
//
// Author: artik <artik@art-laptop>, (C) 2007
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "views.h"
#include <boost/format.hpp>
#include "blog.h"
#include "error.h"
#include "cxxmarkdown/markdowncxx.h"
using boost::format;
using boost::str;
using namespace dbixx;
void View_Comment::init(comment_t &com)
{
c["username"]=com.author;
if(com.url.size()!=0){
c["url"]=com.url;
}
c["content"]=com.content;
c["date"]=com.publish_time;
if(blog->userid!=-1){
c["delete_url"]=str(format(blog->fmt.del_comment) % com.id);
c["edit_url"]=str(format(blog->fmt.edit_comment) % com.id);
}
}
void View_Post::ini_share(post_t &p)
{
c["title"]=p.title;
c["subtitle"]=p.title;
c["date"]=p.publish;
c["author"]=p.author_name;
if(blog->userid!=-1){
c["edit_url"]=str(format(blog->fmt.edit_post) % p.id);
}
c["permlink"]=str(format(blog->fmt.post) % p.id);
c["comment_count"]=p.comment_count;
}
void View_Post::ini_full(post_t &p)
{
ini_share(p);
c["abstract"]=p.abstract;
if(p.content!="") {
c["content"]=p.content;
c["has_content"]=true;
}
else {
c["has_content"]=false;
}
string post_comment=str(format(blog->fmt.add_comment) % p.id);
int n=post_comment.size()/2;
string post_comment_url_2=post_comment.substr(n);
string post_comment_url_1=post_comment.substr(0,n);
c["post_comment_url_1"]=post_comment_url_1;
c["post_comment_url_2"]=post_comment_url_2;
result rs;
blog->sql<<
"SELECT id,author,email,url,publish_time,content "
"FROM comments "
"WHERE post_id=? "
"ORDER BY publish_time",p.id;
blog->sql.fetch(rs);
row cur;
content::vector_t &comments_list=c.vector("comments",rs.rows());
int i;
for(i=0;rs.next(cur);i++)
{
View_Comment com(blog,comments_list[i]);
comment_t comment;
cur >> comment.id >>comment.author >> comment.email >> comment.url
>> comment.publish_time >> comment.content ;
com.init(comment);
}
}
void View_Post::ini_short(post_t &p)
{
ini_share(p);
c["abstract"]=p.abstract;
c["has_content"]=(bool)p.has_content;
}
void View_Main_Page::ini_sidebar()
{
result res;
row r;
blog->sql<<
"SELECT value "
"FROM text_options "
"WHERE id='copyright'";
if(blog->sql.single(r)){
string val;
r>>val;
c["copyright_string"]=val;
}
blog->sql<<
"SELECT id,title "
"FROM pages "
"WHERE is_open=1",res;
content::vector_t &pages=c.vector("pages",res.rows());
int i;
for(i=0;res.next(r);i++) {
int id;
string title;
r>>id>>title;
pages[i]["title"]=title;
pages[i]["link"]=str(boost::format(blog->fmt.page) % id);
}
blog->sql<<
"SELECT link_cats.id,name,title,url,description "
"FROM link_cats,links "
"WHERE links.cat_id=link_cats.id "
"ORDER BY link_cats.id",res;
content::list_t &link_cats=c.list("link_cats");
int previd=-1;
content::list_t *lptr=NULL;
while(res.next(r)){
int id;
string gname,title,url,descr;
r>>id>>gname>>title>>url>>descr;
if(previd==-1 || id!=previd) {
link_cats.push_back(content());
link_cats.back()["title"]=gname;
lptr=&(link_cats.back().list("links"));
previd=id;
}
lptr->push_back(content());
content &ctmp=lptr->back();
ctmp["href"]=url;
ctmp["title"]=title;
if(descr!="")
ctmp["description"]=descr;
}
}
void View_Main_Page::ini_share()
{
int id;
string val;
result rs;
blog->sql<<"SELECT id,value FROM options";
blog->sql.fetch(rs);
row i;
for(;rs.next(i);){
i >>id>>val;
if(id==BLOG_TITLE)
c["blog_name"]=val;
else if(id==BLOG_DESCRIPTION)
c["blog_description"]=val;
else if(id==BLOG_CONTACT && val!="")
c["blog_contact"]=val;
}
c["media"]=blog->fmt.media;
c["admin_url"]=blog->fmt.admin;
c["base_url"]=global_config.sval("blog.script_path");
c["host"]=global_config.sval("blog.host");
c["rss_posts"]=blog->fmt.feed;
c["rss_comments"]=blog->fmt.feed_comments;
ini_sidebar();
}
void View_Main_Page::ini_rss_comments()
{
ini_share();
result res;
blog->sql<<
"SELECT id,post_id,author,content "
"FROM comments "
"ORDER BY id DESC "
"LIMIT 10",res;
content::vector_t &comments=c.vector("comments",res.rows());
row r;
int i;
for(i=0;res.next(r);i++){
int id,pid;
string author,content;
r>>id>>pid>>author>>content;
comments[i]["permlink"]=str(boost::format(blog->fmt.post) % pid);
comments[i]["author"]=author;
comments[i]["content"]=content;
comments[i]["id"]=id;
}
}
void View_Main_Page::ini_main(int id,bool feed)
{
ini_share();
int max_posts=feed ? 10 : 5;
content::vector_t &latest_posts=c.vector("posts",max_posts);
result rs;
if(id!=-1) {
blog->sql<<
"SELECT posts.id,users.username,posts.title, "
" posts.abstract, posts.content !='', "
" posts.publish,posts.comment_count "
"FROM posts "
"LEFT JOIN "
" users ON users.id=posts.author_id "
"WHERE posts.publish <= (SELECT publish FROM posts WHERE id=?) "
" AND posts.is_open=1 "
"ORDER BY posts.publish DESC "
"LIMIT ?",
id,max_posts+1;
}
else {
blog->sql<<
"SELECT posts.id,users.username,posts.title, "
" posts.abstract, posts.content!='', "
" posts.publish,posts.comment_count "
"FROM posts "
"LEFT JOIN "
" users ON users.id=posts.author_id "
"WHERE posts.is_open=1 "
"ORDER BY posts.publish DESC "
"LIMIT ?",(max_posts+1);
}
blog->sql.fetch(rs);
row r;
int counter,n;
for(counter=1,n=0;rs.next(r);counter++) {
if(counter==max_posts+1) {
int id;
r>>id;
c["next_page_link"]=str(format(blog->fmt.main_from) % id);
break;
}
post_t post;
r>>post.id;
r>>post.author_name;
r>>post.title;
r>>post.abstract;
r>>post.has_content;
r>>post.publish;
r>>post.comment_count;
View_Post post_v(blog,latest_posts[n]);
post_v.ini_short(post);
n++;
}
if(n<max_posts){
latest_posts.resize(n);
}
c["master_content"]=string("main_page");
}
void View_Main_Page::ini_page(int id,bool preview)
{
ini_share();
c["master_content"]=string("page");
row r;
blog->sql<<
"SELECT title,content,is_open "
"FROM pages "
"WHERE id=?",id;
if(!blog->sql.single(r))
throw Error(Error::E404);
string title;
string content;
int is_open;
r >> title>>content>>is_open;
if(!is_open && !preview) throw Error(Error::E404);
c["content"]=content;
c["title"]=title;
}
void View_Main_Page::ini_post(int id,bool preview)
{
View_Post post_v(blog,c);
post_t post;
post.id=-1;
row r;
blog->sql<<
"SELECT posts.id,users.username,posts.title, "
" posts.abstract, posts.content, "
" posts.publish,posts.is_open,posts.comment_count "
"FROM posts "
"JOIN users ON users.id=posts.author_id "
"WHERE posts.id=? ",
id;
if(!blog->sql.single(r)) {
throw Error(Error::E404);
}
r>> post.id>>post.author_name>>post.title>>
post.abstract>>post.content>>
post.publish>>post.is_open>>post.comment_count;
if(post.id==-1 || (!post.is_open && !preview)){
throw Error(Error::E404);
}
ini_share();
post_v.ini_full(post);
c["master_content"]=string("post");
}
void View_Main_Page::ini_error(int id)
{
ini_share();
c["master_content"]=string("error");
switch(id){
case Error::E404:
c["error_404"]=true;
c["error_comment"]=false;
break;
case Error::COMMENT_FIELDS:
c["error_404"]=false;
c["error_comment"]=true;
break;
}
}
void View_Admin::ini_share()
{
row r;
blog->sql<<
"SELECT value FROM options WHERE id=?",
(int)BLOG_TITLE;
string blog_name;
if(blog->sql.single(r))
r>>blog_name;
c["blog_name"]=blog_name;
c["media"]=blog->fmt.media;
c["base_url"]=global_config.sval("blog.script_path");
c["admin_url"]=blog->fmt.admin;
c["logout_url"]=blog->fmt.logout;
c["new_post_url"]=blog->fmt.new_post;
c["new_page_url"]=blog->fmt.new_page;
c["edit_options_url"]=blog->fmt.edit_options;
c["edit_links_url"]=blog->fmt.edit_links;
}
void View_Admin::ini_options()
{
ini_share();
result res;
blog->sql<<"SELECT id,value FROM options",res;
row r;
while(res.next(r)) {
int id;
string val;
r>>id>>val;
if(id==BLOG_TITLE)
c["blog_name"]=val;
else if(id==BLOG_DESCRIPTION)
c["blog_description"]=val;
else if(id==BLOG_CONTACT)
c["blog_contact"]=val;
}
blog->sql<<"SELECT value FROM text_options WHERE id='copyright'";
if(blog->sql.single(r)) {
string val;
r>>val;
c["copyright_string"]=val;
}
c["master_content"]=string("admin_editoptions");
c["post_options_url"]=blog->fmt.edit_options;
}
void View_Admin::ini_login()
{
ini_share();
c["master_content"]=string("admin_login");
c["login_url"]=blog->fmt.login;
}
void View_Admin::ini_cedit(int id)
{
ini_share();
c["master_content"]=string("admin_editcomment");
c["edit_comment_url"]=str(format(blog->fmt.update_comment) % id);
c["id"]=id;
string author,url,content,email;
blog->sql<<
"SELECT author,url,email,content "
"FROM comments "
"WHERE id=?",id;
row r;
if(!blog->sql.single(r)) {
throw Error(Error::E404);
}
r>>author>>url>>email>>content;
c["author"]=author;
c["url"]=url;
c["content"]=content;
c["email"]=email;
}
void View_Admin::ini_edit(int id,string ptype)
{
ini_share();
c["master_content"]=string("admin_editpost");
View_Admin_Post post(blog,c);
post.ini(id,ptype);
}
void View_Admin::ini_main()
{
ini_share();
View_Admin_Main main(blog,c);
main.ini();
c["master_content"]=string("admin_main");
}
void View_Admin_Main::ini()
{
result rs;
blog->sql<<
"SELECT id,title "
"FROM posts "
"WHERE is_open=0";
blog->sql.fetch(rs);
row r;
content::vector_t &unpublished_posts=c.vector("posts",rs.rows());
int i;
for(i=0;rs.next(r);i++) {
int id;
string intitle;
r>>id>>intitle;
string edit_url=str(format(blog->fmt.edit_post) % id);
unpublished_posts[i]["edit_url"]=edit_url;
if(intitle!="")
unpublished_posts[i]["title"]=intitle;
}
blog->sql<<
"SELECT id,title,is_open "
"FROM pages ";
blog->sql.fetch(rs);
content::vector_t &pages=c.vector("pages",rs.rows());
for(i=0;rs.next(r);i++) {
int id,is_open;
string title;
r>>id>>title>>is_open;
string edit_url=str(format(blog->fmt.edit_page) % id);
pages[i]["edit_url"]=edit_url;
if(title!="")
pages[i]["title"]=title;
pages[i]["published"]=bool(is_open);
}
blog->sql<<
"SELECT id,post_id,author "
"FROM comments "
"ORDER BY id DESC "
"LIMIT 10",rs;
content::vector_t &latest_comments=c.vector("comments",rs.rows());
for(i=0;rs.next(r);i++) {
int id,c_id;
string author;
r>>c_id>>id>>author;
latest_comments[i]["post_permlink"]=str(format(blog->fmt.post) % id);
latest_comments[i]["username"]=author;
latest_comments[i]["edit_url"]=str(format(blog->fmt.edit_comment) % c_id );
}
}
void View_Admin_Post::ini(int id,string ptype)
{
bool is_post=ptype=="post";
post_t post_data;
c["is_page"]=(bool)(!is_post);
if(id!=-1) {
c["post_id"]=id;
c["submit_post_url"]=str(format( is_post ? blog->fmt.update_post : blog->fmt.update_page) % id);
if(is_post)
c["preview_url"]=str(format(blog->fmt.preview) % id);
else
c["preview_url"]=str(format(blog->fmt.page_preview) % id);
}
else {
if(is_post)
c["submit_post_url"]=blog->fmt.add_post;
else
c["submit_post_url"]=blog->fmt.add_page;
return;
}
post_data.id=-1;
row r;
int is_open;
if(is_post){
blog->sql<<
"SELECT id,title,abstract,content,is_open "
"FROM posts "
"WHERE id=?",
id;
if(!blog->sql.single(r)){
throw Error(Error::E404);
}
r>> post_data.id>>post_data.title>>
post_data.abstract>>post_data.content>>is_open;
}
else {
blog->sql<<
"SELECT id,title,content,is_open "
"FROM pages "
"WHERE id=?",
id;
if(!blog->sql.single(r)){
throw Error(Error::E404);
}
r>> post_data.id>>post_data.title>>post_data.content>>is_open;
}
c["post_title"]=post_data.title;
if(is_post)
c["abstract"]=post_data.abstract;
c["content"]=post_data.content;
c["is_open"]=bool(is_open);
}
void View_Admin::ini_links()
{
ini_share();
result res;
row r;
blog->sql<<
"SELECT id,name FROM link_cats",res;
content::vector_t &cats=c.vector("link_cats",res.rows());
int i;
for(i=0;res.next(r);i++) {
int id;
string name;
r>>id>>name;
cats[i]["id"]=id;
cats[i]["del"]=true;
cats[i]["name"]=name;
}
c["master_content"]=string("admin_editlinks");
c["submit_url"]=blog->fmt.edit_links;
blog->sql<<
"SELECT id,cat_id,title,url,description "
"FROM links "
"ORDER BY cat_id",res;
content::vector_t &links=c.vector("links",res.rows());
for(i=0;res.next(r);i++) {
int id,cat_id;
string title,url,description;
r>>id>>cat_id>>title>>url>>description;
links[i]["id"]=id;
links[i]["cat_id"]=cat_id;
links[i]["name"]=title;
links[i]["url"]=url;
if(description!="")
links[i]["descr"]=description;
}
}
MongoDB Logo MongoDB