1 |
dpavlin |
134 |
package Webpacus::Controller::Editor; |
2 |
|
|
|
3 |
|
|
use strict; |
4 |
|
|
use warnings; |
5 |
|
|
use base 'Catalyst::Controller'; |
6 |
|
|
|
7 |
dpavlin |
165 |
use Data::Dumper; |
8 |
|
|
|
9 |
dpavlin |
134 |
=head1 NAME |
10 |
|
|
|
11 |
dpavlin |
165 |
Webpacus::Controller::Editor - AJAX template and CSS editor |
12 |
dpavlin |
134 |
|
13 |
|
|
=head1 SYNOPSIS |
14 |
|
|
|
15 |
dpavlin |
165 |
See L<Webpacus>, L<WebPAC> |
16 |
dpavlin |
134 |
|
17 |
|
|
=head1 DESCRIPTION |
18 |
|
|
|
19 |
|
|
Catalyst Controller. |
20 |
|
|
|
21 |
|
|
=head1 METHODS |
22 |
|
|
|
23 |
|
|
=over 4 |
24 |
|
|
|
25 |
|
|
=item default |
26 |
|
|
|
27 |
|
|
=cut |
28 |
|
|
|
29 |
|
|
sub default : Private { |
30 |
dpavlin |
165 |
my ( $self, $c ) = @_; |
31 |
dpavlin |
134 |
|
32 |
dpavlin |
165 |
my $webpac = $c->comp('Model::WebPAC'); |
33 |
|
|
my $params = $c->req->params; |
34 |
|
|
my $log = $c->log; |
35 |
dpavlin |
134 |
|
36 |
dpavlin |
165 |
my $template_filename = |
37 |
|
|
$c->req->params->{template_filename} || |
38 |
|
|
$c->config->{webpac}->{default_template} || |
39 |
|
|
$c->log->fatal("need default_template"); |
40 |
|
|
|
41 |
|
|
$c->stash->{template_filename} = $template_filename; |
42 |
|
|
|
43 |
|
|
$c->stash->{template} = 'editor.tt'; |
44 |
dpavlin |
134 |
} |
45 |
|
|
|
46 |
dpavlin |
165 |
=item template |
47 |
|
|
|
48 |
|
|
fetch template using C<template_filename> in request or |
49 |
|
|
C<< webpac->default_template >>. |
50 |
|
|
|
51 |
|
|
=cut |
52 |
|
|
|
53 |
dpavlin |
134 |
sub template : Local { |
54 |
dpavlin |
135 |
my ( $self, $c ) = @_; |
55 |
dpavlin |
134 |
|
56 |
dpavlin |
167 |
my $template_filename = |
57 |
dpavlin |
165 |
$c->req->params->{template_filename} || |
58 |
|
|
$c->config->{webpac}->{default_template} || |
59 |
|
|
$c->log->fatal("need default_template"); |
60 |
dpavlin |
135 |
|
61 |
|
|
my $webpac = $c->comp('Model::WebPAC'); |
62 |
|
|
|
63 |
dpavlin |
167 |
my $template_path = $webpac->{template_path} || $c->log->fatal("can't find template_path in Model::WebPAC"); |
64 |
dpavlin |
135 |
|
65 |
dpavlin |
167 |
my $template_full_path = "$template_path/$template_filename"; |
66 |
|
|
|
67 |
dpavlin |
172 |
if ($c->req->params->{save_template}) { |
68 |
|
|
my $t = $c->req->params->{template_content}; |
69 |
|
|
my $size = length($t); |
70 |
|
|
$c->log->debug("saving $template_full_path, $size bytes"); |
71 |
|
|
if ($webpac->save_html( $template_full_path, $t )) { |
72 |
|
|
$c->res->output("<span>saved $template_filename, $size bytes</span>"); |
73 |
|
|
} else { |
74 |
|
|
$c->res->output("<span>error saving $template_filename!</span>"); |
75 |
|
|
} |
76 |
|
|
return; |
77 |
|
|
} |
78 |
|
|
|
79 |
dpavlin |
165 |
$c->log->debug("loading $template_full_path"); |
80 |
|
|
|
81 |
|
|
if (! -r $template_full_path) { |
82 |
|
|
$c->log->warn("can't find '$template_full_path': $!"); |
83 |
|
|
$c->res->output("can't find template '$template_full_path'"); |
84 |
|
|
return; |
85 |
|
|
} |
86 |
|
|
|
87 |
dpavlin |
167 |
# load template html |
88 |
dpavlin |
135 |
$c->stash->{'template_content'} = |
89 |
|
|
$webpac->load_html( $template_full_path ); |
90 |
|
|
|
91 |
dpavlin |
167 |
# load template list |
92 |
|
|
|
93 |
|
|
opendir(my $d, $template_path) || $c->log->fatal("can't opendir '$template_path': $!"); |
94 |
|
|
my @templates = grep { /\.tt$/i } readdir($d); |
95 |
|
|
|
96 |
|
|
$c->stash->{template_list} = \@templates; |
97 |
|
|
$c->stash->{template_filename} = $template_filename; |
98 |
|
|
|
99 |
dpavlin |
135 |
$c->stash->{template} = 'editor/template.tt'; |
100 |
dpavlin |
134 |
} |
101 |
|
|
|
102 |
dpavlin |
165 |
=item css |
103 |
|
|
|
104 |
dpavlin |
167 |
Return css. |
105 |
|
|
|
106 |
|
|
B<Not finished> |
107 |
|
|
|
108 |
dpavlin |
165 |
=cut |
109 |
|
|
|
110 |
dpavlin |
134 |
sub css : Local { |
111 |
dpavlin |
135 |
my ( $self, $c ) = @_; |
112 |
dpavlin |
134 |
|
113 |
dpavlin |
172 |
my $css_filename = $c->config->{webpac}->{default_css} || die("need default_css"); |
114 |
dpavlin |
135 |
my $css_path = $c->config->{webpac}->{css_path} || die("need css_path"); |
115 |
|
|
my $webpac = $c->comp('Model::WebPAC'); |
116 |
dpavlin |
165 |
|
117 |
dpavlin |
172 |
my $css_full_path = "$css_path/$css_filename"; |
118 |
dpavlin |
165 |
|
119 |
dpavlin |
172 |
if ($c->req->params->{save_css}) { |
120 |
|
|
my $t = $c->req->params->{css_content}; |
121 |
|
|
my $size = length($t); |
122 |
|
|
$c->log->debug("saving $css_full_path, $size bytes"); |
123 |
|
|
if ($webpac->save_html( $css_full_path, $t )) { |
124 |
|
|
$c->res->output("<span>saved $css_filename, $size bytes</span>"); |
125 |
|
|
} else { |
126 |
|
|
$c->res->output("<span>error saving $css_filename!</span>"); |
127 |
|
|
} |
128 |
|
|
return; |
129 |
|
|
} |
130 |
|
|
|
131 |
dpavlin |
165 |
$c->log->debug("loading $css_full_path"); |
132 |
|
|
|
133 |
|
|
$c->stash->{'css_content'} = $webpac->load_html( $css_full_path ); |
134 |
|
|
|
135 |
dpavlin |
135 |
$c->stash->{template} = 'editor/css.tt'; |
136 |
dpavlin |
134 |
} |
137 |
|
|
|
138 |
dpavlin |
165 |
=item record |
139 |
|
|
|
140 |
dpavlin |
167 |
Return html of record taking C<mfn> and C<template_filename> from request. |
141 |
|
|
|
142 |
dpavlin |
165 |
=cut |
143 |
|
|
|
144 |
|
|
sub record : Local { |
145 |
|
|
my ( $self, $c ) = @_; |
146 |
|
|
|
147 |
|
|
$c->log->debug('record params '.Dumper($c->req->params)); |
148 |
|
|
|
149 |
|
|
my $mfn = $c->req->params->{mfn}; |
150 |
|
|
|
151 |
|
|
if (! $mfn) { |
152 |
|
|
$c->log->warn("no mfn, using 1"); |
153 |
|
|
$mfn = 1; |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
my $template_filename = $c->req->params->{template_filename}; |
157 |
|
|
if (! $template_filename) { |
158 |
|
|
$template_filename = $c->config->{webpac}->{default_template} || $c->log->fatal("no webpac->default_template in configuration!"); |
159 |
|
|
$c->log->warn("no template_filename using $template_filename"); |
160 |
|
|
} |
161 |
|
|
|
162 |
|
|
my $webpac = $c->comp('Model::WebPAC'); |
163 |
|
|
|
164 |
|
|
$c->res->output( |
165 |
|
|
$webpac->record( mfn => $mfn, template => $template_filename ) |
166 |
|
|
); |
167 |
|
|
} |
168 |
|
|
|
169 |
dpavlin |
167 |
|
170 |
|
|
|
171 |
dpavlin |
134 |
=back |
172 |
|
|
|
173 |
|
|
=head1 AUTHOR |
174 |
|
|
|
175 |
dpavlin |
165 |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
176 |
dpavlin |
134 |
|
177 |
|
|
=head1 LICENSE |
178 |
|
|
|
179 |
|
|
This library is free software, you can redistribute it and/or modify |
180 |
|
|
it under the same terms as Perl itself. |
181 |
|
|
|
182 |
|
|
=cut |
183 |
|
|
|
184 |
|
|
1; |