Jifty/Plugin/SetupWizard/View.pm untested

filter: Statementstmt 0.0% 0 / 150 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 208 CC 7 SCAR 24.1
CC
7
Cov
0%
config_field
20.0
step_link
12.0
_configure_database_connectivity
6.0
CRAP 11.2
1 package Jifty::Plugin::SetupWizard::View;
2 use strict;
3 use warnings;
4 use Jifty::View::Declare -base;
5  
6 template '/__jifty/admin/setupwizard' => page {
7 my $appname = Jifty->config->framework('ApplicationName');
8 h1 { _("Welcome to %1!", $appname) };
9  
10 render_region(
11 name => 'WizardStep',
12 path => "/__jifty/admin/setupwizard/step",
13 arguments => {
14 step => 0,
15 },
16 );
17 };
18  
19 template '/__jifty/admin/setupwizard/step' => sub {
20 my $step = get('step');
21 my $steps = Jifty->find_plugin('Jifty::Plugin::SetupWizard')->steps;
22 my $step_info = $steps->[$step] or die "Invalid step index";
23  
24 div {
25 class is 'setupwizard-step';
26  
27 form {
28 h3 { _('%1. %2', $step+1, $step_info->{header}) } if $step_info->{header};
29  
30 show "/__jifty/admin/setupwizard/$step_info->{template}";
31  
32 unless ($step_info->{hide_button}) {
33 form_submit(
34 label => _('Next'),
35 onclick => {
36 # Submit all actions
37 submit => undef,
38  
39 # Advance to the next step
40 refresh_self => 1,
41 arguments => {
42 step => $step + 1,
43 },
44 },
45 );
46 }
47 };
48 };
49  
50 div {
51 class is 'setupwizard-links';
52  
53 span { 'Skip to: ' };
54  
55 for my $i (0 .. @$steps - 1) {
56 # Separator
57 if ($i > 0) {
58 span { ' | ' }
59 }
60  
61 step_link(
62 index => $i,
63 label => "%1",
64 current => $i == $step,
65 );
66 }
67 };
68 };
69  
70 sub step_link {
71 my %args = (
72 index => 0,
73 label => "%1",
74 current => 0,
75 @_,
76 );
77  
78 my $index = $args{index};
79  
80 my $steps = Jifty->find_plugin('Jifty::Plugin::SetupWizard')->steps;
81 return unless $index >= 0 && $index < @$steps;
82  
83 my $info = $steps->[$index];
84 my $name = $info->{link} || $info->{header} || $info->{template};
85  
86 if ($args{current}) {
87 b { _($args{label}, $name) },
88 }
89 else {
90 hyperlink(
91 label => _($args{label}, $name),
92 onclick => {
93 replace_self => 1,
94 arguments => {
95 step => $index,
96 },
97 },
98 );
99 }
100 }
101  
102 sub config_field {
103 my %args = @_;
104  
105 my $action = new_action('AddConfig');
106  
107 my %value_args = %{ $args{value_args} || {} };
108  
109 # Grab a sensible default, the current value of config
110 if (!exists($value_args{default_value})) {
111 $value_args{default_value} = Jifty->config->contextual_get($args{context}, $args{field});
112 }
113  
114 # Grab sensible label, the value of field
115 if (!exists($value_args{label})) {
116 $value_args{label} = $args{field};
117 }
118  
119 outs_raw($action->form_field('value' => %value_args));
120  
121 for my $field (qw/field context target_file message empty_is_undef/) {
122 outs_raw($action->form_field(
123 $field,
124 render_as => 'hidden',
125 (exists($args{$field}) ? (default_value => $args{$field}) : ()),
126 ));
127 }
128  
129 return $action;
130 }
131  
132 template '/__jifty/admin/setupwizard/welcome' => sub {
133 my $appname = Jifty->config->framework('ApplicationName');
134  
135 p {
136 _("This installer will help you configure %1 by walking you through the following steps.", $appname)
137 }
138  
139 ol {
140 for my $i (0 .. @{ Jifty->find_plugin('Jifty::Plugin::SetupWizard')->steps } - 1) {
141 li {
142 step_link(
143 index => $i,
144 label => "%1",
145 current => $i == get('step'),
146 );
147 }
148 }
149 }
150  
151 p {
152 _("At any time you may close the wizard; your progress will be saved for next time. You may also skip around, doing these steps in whatever order suits you.");
153 }
154  
155 p {
156 outs_raw _("This setup wizard was activated by the presence of <tt>SetupMode: 1</tt> in one of your configuration files. If you are seeing this erroneously, you may restore normal operation by adjusting the <tt>etc/site_config.yml</tt> file to have <tt>SetupMode: 0</tt> set under <tt>framework</tt>.");
157 };
158  
159 form_submit(
160 label => _('Begin'),
161 onclick => {
162 # Advance to the next step
163 refresh_self => 1,
164 arguments => {
165 step => get('step') + 1,
166 },
167 },
168 );
169 };
170  
171 template '/__jifty/admin/setupwizard/language' => sub {
172 p { _("You may select a different language.") };
173 };
174  
175 template '/__jifty/admin/setupwizard/database' => sub {
176 # XXX: We've got to add a sane way to unquote stuff in onfoo handlers...
177 my $onchange = 'Jifty.update('
178 . Jifty::JSON::encode_json({
179 actions => {},
180 action_arguments => {},
181 fragments => [
182 {
183 mode => 'Replace',
184 path => '/__jifty/admin/setupwizard/database/PLACEHOLDER',
185 region => Jifty->web->qualified_region('database_details'),
186 },
187 ],
188 continuation => undef,
189  
190 })
191 . ', this)';
192  
193 $onchange =~ s/PLACEHOLDER/"+this.value+"/;
194  
195 # Only show them drivers they have available
196 my (@available_drivers, @unavailable_drivers);
197 my @all_drivers = (
198 { display => 'SQLite', value => 'SQLite' },
199 { display => 'MySQL', value => 'mysql' },
200 { display => 'PostgreSQL', value => 'Pg' },
201 #{ display => 'Oracle', value => 'Oracle' },
202 );
203 for (@all_drivers) {
204 if (Jifty->handle->is_available_driver($_->{value})) {
205 push @available_drivers, $_;
206 }
207 else {
208 push @unavailable_drivers, $_;
209 }
210 }
211  
212 my $current_driver = Jifty->config->framework('Database')->{Driver};
213 my $appname = Jifty->config->framework('ApplicationName');
214  
215 p { _("You may choose a database engine.") };
216  
217 p { _("%1 works with a number of different databases. MySQL, PostgreSQL, and SQLite are all supported. You should choose the database that you or your local database administrator knows best.", $appname) };
218  
219 p { _("SQLite is a small database engine that does not need a server or any configuration. We recommend it for testing, demonstration, and development, but it is not quite right for a high-volume production server.") };
220  
221 p { _("MySQL and PostgreSQL are well-supported production-quality database engines. ") };
222  
223 if (@unavailable_drivers) {
224 my @drivers = map { "DBD::$_->{value}" } @unavailable_drivers;
225 my $drivers = join ', ', @drivers;
226  
227 $drivers =~ s/, (?!.*,)/ or /;
228  
229 p { _("If your preferred database is not listed in the dropdown below, that means we could not find a database driver for it. You may be able to remedy this by using CPAN to download and install $drivers.") };
230 }
231  
232 config_field(
233 field => 'Driver',
234 context => '/framework/Database',
235 value_args => {
236 label => _('Database Engine'),
237 render_as => 'select',
238 available_values => \@available_drivers,
239 onchange => [$onchange],
240 },
241 );
242  
243 config_field(
244 field => 'Database',
245 context => '/framework/Database',
246 value_args => {
247 label => _('Database Name'),
248 },
249 );
250  
251 render_region(
252 name => 'database_details',
253 path => "/__jifty/admin/setupwizard/database/$current_driver",
254 );
255  
256 render_region(
257 name => 'test_connectivity',
258 path => '/__jifty/admin/setupwizard/database/test_connectivity_button',
259 );
260 };
261  
262 template '/__jifty/admin/setupwizard/database/SQLite' => sub {
263 # Nothing more needed!
264 };
265  
266 sub _configure_database_connectivity {
267 my $driver = shift;
268  
269 config_field(
270 field => 'Host',
271 context => '/framework/Database',
272 value_args => {
273 hints => _('The domain name of your database server (for example, db.example.com)'),
274 },
275 empty_is_undef => 1,
276 );
277  
278 config_field(
279 field => 'Port',
280 context => '/framework/Database',
281 value_args => {
282 hints => _('Leave blank to use the default value for your database'),
283 },
284 empty_is_undef => 1,
285 );
286  
287 # Better default for postgres ("root" is Jifty's current default)
288 my %user_value_args;
289 $user_value_args{default_value} = 'postgres'
290 if $driver eq 'Pg'
291 && Jifty->config->framework('Database')->{User} eq 'root';
292  
293 config_field(
294 field => 'User',
295 context => '/framework/Database',
296 empty_is_undef => 1,
297 value_args => \%user_value_args,
298 );
299  
300 config_field(
301 field => 'Password',
302 context => '/framework/Database',
303 value_args => {
304 render_as => 'password',
305 },
306 empty_is_undef => 1,
307 );
308 }
309  
310 template '/__jifty/admin/setupwizard/database/mysql' => sub {
311 _configure_database_connectivity('mysql');
312 };
313  
314 template '/__jifty/admin/setupwizard/database/Pg' => sub {
315 _configure_database_connectivity('Pg');
316  
317 config_field(
318 field => 'RequireSSL',
319 context => '/framework/Database',
320 value_args => {
321 label => _('Use SSL?'),
322 render_as => 'checkbox',
323 },
324 );
325 };
326  
327 template '/__jifty/admin/setupwizard/database/test_connectivity_button' => sub {
328 hyperlink(
329 label => _("Test connectivity"),
330 as_button => 1,
331 onclick => {
332 # Submit all actions
333 submit => undef,
334  
335 # Actually test connectivity
336 replace_with => '/__jifty/admin/setupwizard/database/test_connectivity',
337 },
338 );
339 };
340  
341 template '/__jifty/admin/setupwizard/database/test_connectivity' => sub {
342 my $db_args = Jifty->config->framework('Database');
343 my $action = Jifty::Plugin::SetupWizard::Action::TestDatabaseConnectivity->new(
344 arguments => {
345 driver => $db_args->{Driver},
346 database => $db_args->{Database},
347 host => $db_args->{Host},
348 port => $db_args->{Port},
349 user => $db_args->{User},
350 password => $db_args->{Password},
351 requiressl => $db_args->{RequireSSL},
352 },
353 );
354 $action->validate;
355 $action->run;
356  
357 if ($action->result->success) {
358 p {
359 attr { class => 'popup_message' };
360 outs $action->result->message;
361 }
362 }
363 else {
364 p {
365 attr { class => 'popup_error' };
366 outs $action->result->error;
367 }
368 }
369  
370 show '/__jifty/admin/setupwizard/database/test_connectivity_button';
371 };
372  
373 template '/__jifty/admin/setupwizard/web' => sub {
374 p { _("You may change web server settings.") };
375  
376 my $appname = lc Jifty->config->framework('ApplicationName');
377 $appname =~ s/-//g;
378  
379 config_field(
380 field => 'BaseURL',
381 context => '/framework/Web',
382 value_args => {
383 hints => _('The root URL for the web server (examples: http://%1.yourcompany.com, http://business.com/%1)', $appname),
384 },
385 );
386  
387 config_field(
388 field => 'Port',
389 context => '/framework/Web',
390 );
391 };
392  
393 template '/__jifty/admin/setupwizard/finalize' => sub {
394 p { _("You may finalize your configuration.") };
395  
396 my $appname = Jifty->config->framework('ApplicationName');
397 my $action = config_field(
398 field => 'SetupMode',
399 context => '/framework',
400 message => _('Setup finished. Welcome to %1!', $appname),
401 value_args => {
402 render_as => 'hidden',
403 default_value => 0,
404 },
405 );
406  
407 form_next_page url => '/';
408 form_submit( label => _('Done!') );
409 };
410  
411 1;
412  
413 __END__