/[webpac2]/Webpacus/lib/Webpacus/Controller/Editor.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /Webpacus/lib/Webpacus/Controller/Editor.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 250 - (show annotations)
Thu Dec 15 02:03:25 2005 UTC (18 years, 5 months ago) by dpavlin
File size: 6966 byte(s)
 r11706@llin:  dpavlin | 2005-12-15 03:49:56 +0100
 select multiple databases in editor [0.19]

1 package Webpacus::Controller::Editor;
2
3 use strict;
4 use warnings;
5 use base 'Catalyst::Controller';
6
7 use HTML::Tidy;
8 use Data::Dumper;
9
10 =head1 NAME
11
12 Webpacus::Controller::Editor - AJAX template and CSS editor
13
14 =head1 SYNOPSIS
15
16 See L<Webpacus>, L<WebPAC>
17
18 =head1 DESCRIPTION
19
20 Catalyst Controller.
21
22 =head1 METHODS
23
24 =over 4
25
26 =item default
27
28 =cut
29
30 sub default : Private {
31 my ( $self, $c ) = @_;
32
33 my $webpac = $c->comp('Model::WebPAC');
34 my $params = $c->req->params;
35 my $log = $c->log;
36
37 my $template_filename =
38 $c->req->params->{template_filename} ||
39 $c->config->{webpac}->{default_template} ||
40 $c->log->fatal("need default_template");
41
42 $c->stash->{template_filename} = $template_filename;
43
44 # get databases from config
45 my @databases;
46 my $config_dbs = $c->config->{databases};
47
48 if (! $config_dbs) {
49 $c->log->fail("didn't find databases in config");
50 } else {
51 # convert to resonable format for TT
52 foreach my $db (keys %{ $config_dbs }) {
53 my $d = $config_dbs->{$db} || die;
54 my $inputs = $d->{input};
55 $c->log->debug("inputs [1]: ", Dumper($inputs));
56 $inputs = [ $inputs ] if (ref($inputs) ne 'ARRAY');
57 $c->log->debug("inputs [2]: ", Dumper($inputs));
58
59 foreach my $i ( @{ $inputs } ) {
60 my $el = {
61 input => $i->{name},
62 name => $d->{name} || $d,
63 prefix => $db . '/' . $i->{name},
64 };
65 push @databases, $el;
66 }
67 }
68 }
69
70 $c->stash->{databases} = \@databases;
71
72 $c->stash->{template} = 'editor.tt';
73 }
74
75 =item template
76
77 fetch template using C<template_filename> in request or
78 C<< webpac->default_template >>.
79
80 =cut
81
82 sub template : Local {
83 my ( $self, $c ) = @_;
84
85 my $template_filename =
86 $c->req->params->{template_filename} ||
87 $c->config->{webpac}->{default_template} ||
88 $c->log->fatal("need default_template");
89
90 my $webpac = $c->comp('Model::WebPAC');
91
92 my $template_path = $webpac->{template_path} || $c->log->fatal("can't find template_path in Model::WebPAC");
93
94 my $template_full_path = "$template_path/$template_filename";
95
96 if ($c->req->params->{save_template}) {
97
98 $c->response->content_type('text/html; charset=utf-8');
99
100 my $t = $c->req->params->{template_content};
101 my $size = length($t);
102 $c->log->debug("saving $template_full_path, $size bytes");
103 if ($webpac->save_html( $template_full_path, $t )) {
104 $c->res->output("<span>saved $template_filename, $size bytes</span>");
105 } else {
106 $c->res->output("<span class=\"error\">error saving $template_filename!</span>");
107 }
108 return;
109 }
110
111 $c->log->debug("loading $template_full_path");
112
113 if (! -r $template_full_path) {
114 $c->log->warn("can't find '$template_full_path': $!");
115 $c->response->content_type('text/html; charset=utf-8');
116 $c->res->output("can't find template '$template_full_path'");
117 return;
118 }
119
120 # load template html
121 $c->stash->{'template_content'} =
122 $webpac->load_html( $template_full_path );
123
124 # load template list
125
126 opendir(my $d, $template_path) || $c->log->fatal("can't opendir '$template_path': $!");
127 my @templates = grep { /\.tt$/i } readdir($d);
128
129 $c->stash->{template_list} = \@templates;
130 $c->stash->{template_filename} = $template_filename;
131
132 $c->stash->{template} = 'editor/template.tt';
133 }
134
135 =item css
136
137 Return css.
138
139 B<Not finished>
140
141 =cut
142
143 sub css : Local {
144 my ( $self, $c ) = @_;
145
146 my $css_filename = $c->config->{webpac}->{default_css} || die("need default_css");
147 my $css_path = $c->config->{webpac}->{css_path} || die("need css_path");
148 my $webpac = $c->comp('Model::WebPAC');
149
150 my $css_full_path = "$css_path/$css_filename";
151
152 if ($c->req->params->{save_css}) {
153
154 $c->response->content_type('text/html; charset=utf-8');
155
156 my $t = $c->req->params->{css_content};
157 my $size = length($t);
158 $c->log->debug("saving $css_full_path, $size bytes");
159 if ($webpac->save_html( $css_full_path, $t )) {
160 $c->res->output("<span>saved $css_filename, $size bytes</span>");
161 } else {
162 $c->res->output("<span class=\"error\">error saving $css_filename!</span>");
163 }
164 return;
165 }
166
167 $c->log->debug("loading $css_full_path");
168
169 $c->stash->{'css_content'} = $webpac->load_html( $css_full_path );
170
171 $c->stash->{'css_list'} = [ qw/user.css foobar.css/ ];
172
173 $c->stash->{template} = 'editor/css.tt';
174 }
175
176 =item record
177
178 Return html of record taking C<mfn> and C<template_filename> from request.
179
180 =cut
181
182 sub record : Local {
183 my ( $self, $c ) = @_;
184
185 $c->log->debug('record params '.Dumper($c->req->params));
186
187 my $record_uri = $c->req->params->{record_uri};
188 $record_uri ||= $c->config->{editor}->{default_record_uri} and
189 $c->log->warn("using editor: default_record_uri");
190
191 if (! $record_uri) {
192 $c->log->fatal("record called without record_uri and default_record_uri not found in config under editor");
193 $c->stash->{error} = 'record retrival failed';
194 $c->stash->{template} = 'error.tt';
195 return;
196 }
197
198 my $template_filename = $c->req->params->{template_filename};
199 if (! $template_filename) {
200 $template_filename = $c->config->{webpac}->{default_template} || $c->log->fatal("no webpac->default_template in configuration!");
201 $c->log->warn("no template_filename using $template_filename");
202 }
203
204 my $webpac = $c->comp('Model::WebPAC');
205
206 my $html = $webpac->record( record_uri => $record_uri, template => $template_filename );
207
208 if ($html) {
209 $c->log->debug('check html with tidy');
210 my $tidy = new HTML::Tidy;
211 $tidy->ignore( text => [
212 qr/DOCTYPE/, qr/unsupported/, qr/proprietary/i,
213 qr/invalid character code/,
214 qr/inserting missing 'title' element/,
215 qr/lacks "summary" attribute/,
216 ] );
217
218 my @lines = split( "\n", $html );
219 $_ = "$_\n" for @lines;
220
221 if ( $tidy->parse('tidy', @lines) ) {
222 if ( $tidy->messages ) {
223 $html .= <<__TIDY_CLOSE__;
224 <div id="tidy_show" class="notice" style="display: none;" onclick="Element.hide('tidy_show'); Element.show('tidy');">?</div>
225 <div id="tidy" class="tidy">
226 <a href="#" onclick="Element.hide('tidy'); Element.show('tidy_show'); return false;">close</a>
227 HTML Tidy output:
228 <br/>
229 __TIDY_CLOSE__
230 # Escape <, >, & and ", and to produce valid XML
231 my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
232 my $escape_re = join '|' => keys %escape;
233
234 foreach my $m ( $tidy->messages ) {
235 my $c = $lines[ ( $m->line - 1 ) ];
236 my $txt = $m->as_string;
237 $c =~ s/($escape_re)/$escape{$1}/g;
238 $txt =~ s/($escape_re)/$escape{$1}/g;
239 my $class = '';
240 $class = ' class="tidy_'.lc($1).'"' if ($txt =~ /(Warning|Error):/s);
241 $html .= 'line ' . $m->line . ":<span${class}>$txt<tt>$c</tt></span>";
242 }
243 $html .= '</div>';
244 }
245 } else {
246 $html .= qq{<div class="tidy tidy_error">Can't parse this record with HTML Tidy!</div>};
247 }
248 } else {
249 $html .= qq{<div class="no_results">Can't find record</div>};
250 }
251
252 $c->response->content_type('text/html; charset=utf-8');
253 $c->response->body( $html );
254 }
255
256
257
258 =back
259
260 =head1 AUTHOR
261
262 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
263
264 =head1 LICENSE
265
266 This library is free software, you can redistribute it and/or modify
267 it under the same terms as Perl itself.
268
269 =cut
270
271 1;

  ViewVC Help
Powered by ViewVC 1.1.26