]>
Commit | Line | Data |
---|---|---|
3ac5cdbf TC |
1 | package BSE::UI::AdminPage; |
2 | use strict; | |
e0ed81d7 | 3 | use BSE::TB::Articles; |
3ac5cdbf TC |
4 | use BSE::Util::HTML qw(escape_uri); |
5 | use BSE::UI::AdminDispatch; | |
e0ed81d7 | 6 | use BSE::TB::Articles; |
3ac5cdbf TC |
7 | our @ISA = qw(BSE::UI::AdminDispatch); |
8 | ||
e0ed81d7 | 9 | our $VERSION = "1.001"; |
3ac5cdbf TC |
10 | |
11 | my %actions = | |
12 | ( | |
13 | adminpage => "", | |
14 | warnings => "", | |
15 | ); | |
16 | ||
17 | sub actions { | |
18 | \%actions | |
19 | } | |
20 | ||
21 | sub rights { | |
22 | \%actions | |
23 | } | |
24 | ||
25 | sub default_action { 'adminpage' } | |
26 | ||
27 | sub req_adminpage { | |
28 | my ($self, $req) = @_; | |
29 | ||
30 | my $cgi = $req->cgi; | |
31 | my $id = $cgi->param('id'); | |
32 | defined $id or $id = 1; | |
33 | my $admin = 1; | |
34 | $admin = $cgi->param('admin') if defined $cgi->param('admin'); | |
35 | my $admin_links = $admin; | |
36 | $admin_links = $cgi->param('admin_links') | |
37 | if defined $cgi->param('admin_links'); | |
38 | ||
e0ed81d7 AO |
39 | #my $articles = BSE::TB::Articles->new; |
40 | my $articles = 'BSE::TB::Articles'; | |
3ac5cdbf TC |
41 | |
42 | my $article; | |
43 | $article = $articles->getByPkey($id) if $id =~ /^\d+$/; | |
44 | ||
45 | if ($article) { | |
46 | local *OLDERR = *STDERR; | |
47 | local *STDERR; | |
48 | ||
49 | my $cap = tie *STDERR, "BSE::UI::AdminPage::CaptureErrors", \*OLDERR; | |
50 | ||
51 | eval "use $article->{generator}"; | |
52 | die $@ if $@; | |
53 | my $generator = $article->{generator}->new | |
54 | ( | |
55 | admin=>$admin, | |
56 | admin_links => $admin_links, | |
57 | articles=>$articles, | |
58 | cfg=>$req->cfg, | |
59 | request=>$req, | |
60 | top=>$article | |
61 | ); | |
62 | ||
63 | if ($article->is_dynamic) { | |
64 | my $content = $generator->generate($article, $articles); | |
65 | (my $dyn_gen_class = $article->{generator}) =~ s/.*\W//; | |
66 | $dyn_gen_class = "BSE::Dynamic::".$dyn_gen_class; | |
67 | (my $dyn_gen_file = $dyn_gen_class . ".pm") =~ s!::!/!g; | |
68 | require $dyn_gen_file; | |
69 | my $dyn_gen = $dyn_gen_class->new | |
70 | ( | |
71 | $req, | |
72 | admin => $admin, | |
73 | admin_links => $admin_links, | |
74 | ); | |
75 | $article = $dyn_gen->get_real_article($article); | |
76 | ||
77 | my $result = $dyn_gen->generate($article, $content); | |
78 | ||
79 | $req->cache_set("page_warnings-".$article->id => [ $cap->data ]); | |
80 | ||
81 | return $result; | |
82 | } | |
83 | else { | |
84 | my $type = BSE::Template->html_type($req->cfg); | |
85 | my $page = $generator->generate($article, $articles); | |
86 | if ($req->utf8) { | |
87 | require Encode; | |
88 | $page = Encode::encode($req->charset, $page); | |
89 | } | |
90 | ||
91 | $req->cache_set("page_warnings-".$article->id => [ $cap->data ]); | |
92 | return | |
93 | { | |
94 | content => $page, | |
95 | type => $type, | |
96 | }; | |
97 | } | |
98 | } | |
99 | else { | |
100 | # display a message on the admin menu | |
101 | $req->flash_error("No such article '$id'"); | |
102 | return $req->get_refresh($req->url("menu")); | |
103 | } | |
104 | } | |
105 | ||
106 | sub req_warnings { | |
107 | my ($self, $req) = @_; | |
108 | ||
109 | my $id = $req->cgi->param("id"); | |
110 | ||
111 | defined $id && $id =~ /^[0-9]+$/ | |
112 | or return $req->field_error($req, { id => "id must be numeric" }); | |
113 | ||
114 | my $warnings = $req->cache_get("page_warnings-$id") || []; | |
115 | ||
116 | return $req->json_content | |
117 | ({ | |
118 | success => 1, | |
119 | warnings => $warnings, | |
120 | }); | |
121 | } | |
122 | ||
123 | package BSE::UI::AdminPage::CaptureErrors; | |
124 | use base 'Tie::Handle'; | |
125 | ||
126 | sub TIEHANDLE { | |
127 | my ($class, $handle) = @_; | |
128 | ||
129 | return bless { data => "", old => $handle }, $class; | |
130 | } | |
131 | ||
132 | sub WRITE { | |
133 | my ($self, $buf, $len, $offset) = @_; | |
134 | ||
135 | $self->{data} .= substr($buf, $offset, $len); | |
136 | print {$self->{old}} substr($buf, $offset, $len); | |
137 | ||
138 | 1; | |
139 | } | |
140 | ||
141 | sub data { | |
142 | return split /\n/, $_[0]{data}; | |
143 | } | |
144 | ||
145 | 1; |