1 |
package A3C::View::Strix; |
2 |
|
3 |
=head1 NAME |
4 |
|
5 |
A3C::View::Strix |
6 |
|
7 |
=head1 DESCRIPTION |
8 |
|
9 |
Display information about Strix instances |
10 |
|
11 |
=head1 TEMPLATES |
12 |
|
13 |
=cut |
14 |
|
15 |
use strict; |
16 |
use warnings; |
17 |
|
18 |
use Jifty::View::Declare -base; |
19 |
use Data::Dump qw/dump/; |
20 |
|
21 |
=head2 / |
22 |
|
23 |
Display instaces search and some stats |
24 |
|
25 |
=cut |
26 |
|
27 |
template 'index.html' => page { |
28 |
|
29 |
title is _('Strix instances'); |
30 |
|
31 |
my $orgs = A3C::Model::StrixInstanceCollection->new; |
32 |
$orgs->unlimit; |
33 |
|
34 |
div { _('Number of instances in Strix: %1', $orgs->count ) }; |
35 |
|
36 |
render_region( |
37 |
name => 'selected-instances', |
38 |
path => '/strix/selected-instances' |
39 |
); |
40 |
|
41 |
render_region( |
42 |
name => 'search-instances', |
43 |
path => '/strix/search-instances', |
44 |
); |
45 |
|
46 |
}; |
47 |
|
48 |
=head2 errors |
49 |
|
50 |
=cut |
51 |
|
52 |
template 'errors' => page { |
53 |
|
54 |
title is _('Strix errors'); |
55 |
|
56 |
render_region( |
57 |
name => 'selected-instances', |
58 |
path => '/strix/selected-instances' |
59 |
); |
60 |
|
61 |
h2 { _('Name differences') } |
62 |
|
63 |
my $name_diff = A3C::SQL->new({ query => qq{ |
64 |
select |
65 |
instance,hreduorgurl, |
66 |
_site_name,o |
67 |
from strix_instances |
68 |
join hr_edu_orgs on cn = instance |
69 |
where o != _site_name |
70 |
}}); |
71 |
|
72 |
if ( $name_diff->count > 0 ) { |
73 |
|
74 |
table { |
75 |
row { |
76 |
th {} |
77 |
th { _('Instance') } |
78 |
th { _('Strix instance name') } |
79 |
th { _('hrEduOrg.o') } |
80 |
}; |
81 |
while ( my $row = $name_diff->next ) { |
82 |
row { |
83 |
cell { show( 'instance-op', 'Create', '+', $row->instance ) } |
84 |
cell { $row->instance } |
85 |
cell { $row->_site_name } |
86 |
cell { $row->o } |
87 |
} |
88 |
} |
89 |
} |
90 |
|
91 |
} else { |
92 |
div { _("Can't find any instance of strix which has different name than data from LDAP") } |
93 |
} |
94 |
|
95 |
}; |
96 |
|
97 |
=head2 instances |
98 |
|
99 |
=cut |
100 |
|
101 |
template 'instances' => page { |
102 |
|
103 |
title is _('Strix instances'); |
104 |
|
105 |
my $sql = A3C::SQL->new({ query => qq{ |
106 |
select |
107 |
hrEduOrgUrl, o, l, postalAddress, telephoneNumber, facsimileTelephoneNumber |
108 |
from strix_instances |
109 |
join hr_edu_orgs on cn = instance |
110 |
order by l,o |
111 |
}}); |
112 |
|
113 |
if ( $sql->count > 0 ) { |
114 |
|
115 |
table { |
116 |
row { |
117 |
th { _('hrEduOrgUrl') } |
118 |
th { _('o') } |
119 |
th { _('l') } |
120 |
th { _('postalAddress') } |
121 |
th { _('telephoneNumber') } |
122 |
th { _('facsimileTelephoneNumber') } |
123 |
}; |
124 |
while ( my $row = $sql->next ) { |
125 |
row { |
126 |
cell { outs_raw '<a href="http://' . $row->hrEduOrgUrl . '">' . $row->hrEduOrgUrl . '</a>' } |
127 |
cell { $row->o } |
128 |
cell { $row->l } |
129 |
cell { $row->postalAddress } |
130 |
cell { $row->telephoneNumber } |
131 |
cell { $row->facsimileTelephoneNumber } |
132 |
} |
133 |
} |
134 |
} |
135 |
|
136 |
div { _("Found total of %1 strix instances", $sql->count) } |
137 |
|
138 |
} else { |
139 |
div { _("Can't find any strix instances") } |
140 |
} |
141 |
|
142 |
}; |
143 |
|
144 |
=head2 sql |
145 |
|
146 |
Execute SQL query on instance |
147 |
|
148 |
=cut |
149 |
|
150 |
template 'sql' => page { |
151 |
|
152 |
title is _('Execute SQL'); |
153 |
|
154 |
render_region( |
155 |
name => 'selected-instances', |
156 |
path => '/strix/selected-instances' |
157 |
); |
158 |
|
159 |
render_region( |
160 |
name => 'execute-sql', |
161 |
path => '/strix/execute-sql', |
162 |
); |
163 |
|
164 |
}; |
165 |
|
166 |
sub strix { |
167 |
my $instance = get('instance'); |
168 |
return Strix->new({ instance => $instance }); |
169 |
} |
170 |
|
171 |
sub strix_link { |
172 |
my ( $url, $label ) = @_; |
173 |
hyperlink( |
174 |
url => 'http://' . get('instance') . Jifty->config->app('strix')->{domain} . $url, |
175 |
label => $label || $url, |
176 |
target => 'strix', |
177 |
); |
178 |
} |
179 |
|
180 |
=head2 navigation |
181 |
|
182 |
=cut |
183 |
|
184 |
template 'navigation' => page { |
185 |
|
186 |
title is _('Site navigation'); |
187 |
|
188 |
render_region( |
189 |
name => 'selected-instances', |
190 |
path => '/strix/selected-instances' |
191 |
); |
192 |
|
193 |
render_region( |
194 |
name => 'strix-site', |
195 |
path => '/strix/site' |
196 |
); |
197 |
|
198 |
}; |
199 |
|
200 |
=head1 REGIONS |
201 |
|
202 |
=head2 execute-sql |
203 |
|
204 |
Execute SQL query on instance |
205 |
|
206 |
=cut |
207 |
|
208 |
template 'execute-sql' => sub { |
209 |
|
210 |
my $sql = get('sql') || Jifty->web->session->get('sql'); |
211 |
warn ">>>> sql = $sql"; |
212 |
my $instance = get('instance'); |
213 |
warn ">>>> instance = $instance"; |
214 |
|
215 |
my $action = new_action( |
216 |
class => 'StrixSQL', |
217 |
moniker => 'strix-sql', |
218 |
sticky_on_success => 1, |
219 |
sticky_on_failure => 1, |
220 |
arguments => { |
221 |
instance => $instance, |
222 |
sql => $sql, |
223 |
}, |
224 |
); |
225 |
|
226 |
form { |
227 |
render_action( $action, [ 'instance', 'sql' ] ); |
228 |
form_submit( label => _('Execute SQL') ); |
229 |
}; |
230 |
|
231 |
if ( my $sql = $action->result->content('sql') ) { |
232 |
Jifty->web->session->set( sql => $sql->query ); |
233 |
div { _('Found %1 results for %2', $sql->count, $instance ) } |
234 |
table { |
235 |
row { map { th { $_ } } $sql->_column_names }; |
236 |
while (my $row = $sql->next) { |
237 |
row { |
238 |
foreach my $col ( $sql->_column_names ) { |
239 |
cell { $row->$col } |
240 |
} |
241 |
} |
242 |
} |
243 |
} |
244 |
} |
245 |
warn ">>>> sql (at end) = ",Jifty->web->session->get('sql'); |
246 |
}; |
247 |
|
248 |
=head2 search-instances |
249 |
|
250 |
=cut |
251 |
|
252 |
template 'search-instances' => sub { |
253 |
|
254 |
h1 { _('Find instance') } |
255 |
|
256 |
my $action = new_action( |
257 |
class => 'SearchStrixInstance', |
258 |
moniker => 'search-strix-instance', |
259 |
sticky_on_success => 1, |
260 |
sticky_on_failure => 1, |
261 |
); |
262 |
|
263 |
|
264 |
form { |
265 |
render_action( $action => [ 'instance_contains', '_site_name_contains' ] ); |
266 |
form_submit( label => _('Search') ); |
267 |
}; |
268 |
|
269 |
# warn dump( $action->result->content ); |
270 |
|
271 |
if ( my $search = $action->result->content('search') ) { |
272 |
div { _('Found %1 results', $search->count ) } |
273 |
table { |
274 |
while (my $strix = $search->next) { |
275 |
row { |
276 |
cell { show( 'instance-op', 'Create', '+', $strix->instance ) } |
277 |
cell { tt { $strix->instance } } |
278 |
cell { $strix->_site_name } |
279 |
} |
280 |
} |
281 |
} |
282 |
} |
283 |
|
284 |
}; |
285 |
|
286 |
=head2 selected-instances |
287 |
|
288 |
Show Selected instances for current user |
289 |
|
290 |
=cut |
291 |
|
292 |
template 'selected-instances' => sub { |
293 |
my $self = shift; |
294 |
|
295 |
# warn "## IN selected-instances ",dump( @_ ); |
296 |
|
297 |
if ( my $op = get 'op' ) { |
298 |
my $op_instance = get 'op_instance'; |
299 |
return unless $op_instance; |
300 |
warn "# selected-instances $op on $op_instance"; |
301 |
|
302 |
my $a; |
303 |
|
304 |
if ( $op eq 'Create' ) { |
305 |
|
306 |
$a = new_action( |
307 |
class => $op . 'StrixInstanceSelection', |
308 |
moniker => $op, |
309 |
arguments => { |
310 |
instance => $op_instance, |
311 |
by_user => $self->current_user->id, |
312 |
}, |
313 |
); |
314 |
|
315 |
} elsif ( $op eq 'Delete' ) { |
316 |
|
317 |
my $strix = A3C::Model::StrixInstanceSelection->new; |
318 |
$strix->load_by_cols( instance => $op_instance, by_user => $self->current_user->id ); |
319 |
warn "can't find instance $op_instance" unless $strix->id; |
320 |
$a = $strix->as_delete_action; |
321 |
|
322 |
} |
323 |
# warn "# argument_values = ",dump( $a->argument_values ); |
324 |
$a->run; |
325 |
|
326 |
if ( $a->result->error ) { |
327 |
div { |
328 |
{ class is 'note error' } |
329 |
$a->result->error; |
330 |
} |
331 |
} |
332 |
|
333 |
set( op => '' ); |
334 |
} |
335 |
|
336 |
my $selected = A3C::Model::StrixInstanceSelectionCollection->new; |
337 |
$selected->limit( column => 'by_user', value => Jifty->web->current_user->id ); |
338 |
|
339 |
if ( $selected->count > 0 ) { |
340 |
|
341 |
my $instance = get('instance'); |
342 |
warn "# selected-instances -- selected: $instance\n"; |
343 |
|
344 |
div { _('%1 instances selected', $selected->count ) }; |
345 |
table { |
346 |
while (my $s = $selected->next) { |
347 |
row { |
348 |
cell { tt { |
349 |
if ( $s->instance->instance eq $instance ) { |
350 |
b { $instance } |
351 |
} else { |
352 |
hyperlink( |
353 |
url => '?instance=' . $s->instance->instance, |
354 |
label => $s->instance->instance |
355 |
) |
356 |
} |
357 |
} } |
358 |
cell { $s->instance->_site_name } |
359 |
cell { show( 'instance-op', 'Delete', '-', $s->instance->instance ) } |
360 |
} |
361 |
} |
362 |
} |
363 |
} else { |
364 |
div { _('No instances selected') } |
365 |
} |
366 |
}; |
367 |
|
368 |
=head2 instance-op |
369 |
|
370 |
Display button to add/remove instance from selection |
371 |
|
372 |
show( 'instance-op', 'Delete', '-', $strix->instace ); |
373 |
|
374 |
=cut |
375 |
|
376 |
template 'instance-op' => sub { |
377 |
my $self = shift; |
378 |
|
379 |
# warn "# instance-op = ",dump( @_ ); |
380 |
|
381 |
my ( $op, $label, $instance ) = @_; |
382 |
|
383 |
form { |
384 |
hyperlink( |
385 |
label => $label, |
386 |
onclick => { |
387 |
refresh => 'selected-instances', |
388 |
path => '/strix/selected-instances', |
389 |
args => { |
390 |
op_instance => $instance, |
391 |
op => $op, |
392 |
} |
393 |
}, |
394 |
); |
395 |
} |
396 |
|
397 |
}; |
398 |
|
399 |
=head2 site |
400 |
|
401 |
=cut |
402 |
|
403 |
template 'site' => sub { |
404 |
|
405 |
my $action = new_action( |
406 |
class => 'StrixSelectSite', |
407 |
moniker => 'strix-select-site', |
408 |
); |
409 |
|
410 |
warn "# action = ", dump( $action ); |
411 |
|
412 |
warn "# argument_values = ", dump( $action->argument_values ); |
413 |
|
414 |
if ( ! $action->argument_value('instance') ) { |
415 |
$action->argument_value( 'instance', get('instance') ); |
416 |
warn "# run action with instance\n"; |
417 |
$action->run; |
418 |
} |
419 |
|
420 |
my $magic = [ |
421 |
{ submit => $action, refresh_self => 1 }, |
422 |
# this is basically a closure |
423 |
{ refresh => 'selected-instances', path => '/strix/selected-instances', args => { |
424 |
instance => { result_of => $action, name => 'instance' } |
425 |
} }, |
426 |
{ refresh => 'strix-site-layout', path => '/__jifty/empty' }, |
427 |
]; |
428 |
|
429 |
form { |
430 |
render_param( $action, 'instance', onchange => $magic ); |
431 |
render_param( $action, 'site_id', onchange => $magic ); |
432 |
form_submit( label => _('Show navigation'), onclick => $magic ); |
433 |
}; |
434 |
|
435 |
warn "## select-site action ",dump( $action->result ); |
436 |
|
437 |
render_region( |
438 |
name => 'layout', |
439 |
path => '/__jifty/empty', |
440 |
); |
441 |
|
442 |
if ( my $site_id = $action->result->content('site_id') ) { |
443 |
show('navigation-tree', $action->result->content('instance'), $site_id); |
444 |
} |
445 |
}; |
446 |
|
447 |
=head2 layout |
448 |
|
449 |
Shows layout for C<url> |
450 |
|
451 |
=cut |
452 |
|
453 |
template 'layout' => sub { |
454 |
|
455 |
my $url = get('url') || '/'; |
456 |
my $category = strix->category( $url ); |
457 |
my $layout = strix->layout( $url ); |
458 |
|
459 |
h1 { $category->{naziv} } |
460 |
pre { |
461 |
dump( $layout ); |
462 |
} |
463 |
|
464 |
}; |
465 |
|
466 |
=head2 category |
467 |
|
468 |
Show category data for C<url> |
469 |
|
470 |
=cut |
471 |
|
472 |
template 'category' => sub { |
473 |
|
474 |
my $url = get('url') || '/'; |
475 |
my $category = strix->category( $url ); |
476 |
|
477 |
h1 { $category->{naziv} } |
478 |
pre { |
479 |
dump( $category ); |
480 |
} |
481 |
|
482 |
}; |
483 |
|
484 |
=head1 PRIVATE TEMPLATES |
485 |
|
486 |
=head2 navigation-tree-category |
487 |
|
488 |
show('navigation-tree-category',$kat_row); |
489 |
|
490 |
=cut |
491 |
|
492 |
private template 'navigation-tree-category' => sub { |
493 |
my $self = shift; |
494 |
#warn "## navigation-tree-category",dump( @_ ); |
495 |
my $p = shift; |
496 |
hyperlink( |
497 |
onclick => { |
498 |
region => 'strix-site-layout', # FIXME do we have to hard-code region name here? |
499 |
replace_with => '/strix/category', |
500 |
args => { |
501 |
url => $p->{url}, |
502 |
instance => get('instance'), |
503 |
} |
504 |
}, |
505 |
label => $p->{naziv}, |
506 |
); |
507 |
outs_raw(' '); |
508 |
if ( $p->{type} eq 'category' ) { |
509 |
hyperlink( |
510 |
# url => '/strix/layout?url=' . $p->{url} . ';instance=' . get('instance'), |
511 |
onclick => { |
512 |
region => 'strix-site-layout', # FIXME do we have to hard-code region name here? |
513 |
replace_with => '/strix/layout', |
514 |
args => { |
515 |
url => $p->{url}, |
516 |
instance => get('instance'), |
517 |
} |
518 |
}, |
519 |
label => _('layout'), |
520 |
class => 'layout', |
521 |
); |
522 |
} |
523 |
outs_raw(' '); |
524 |
strix_link( $p->{url}, '>>' ); |
525 |
}; |
526 |
|
527 |
=head2 navigation-tree |
528 |
|
529 |
show('navigation-tree',$instance,$site_id); |
530 |
|
531 |
=cut |
532 |
|
533 |
private template 'navigation-tree' => sub { |
534 |
my $self = shift; |
535 |
my ( $instance, $site_id ) = @_; |
536 |
|
537 |
warn "## navigation-tree instance: $instance site_id: $site_id"; |
538 |
|
539 |
set 'instance' => $instance; |
540 |
|
541 |
sub children { |
542 |
my $c = shift; |
543 |
return unless defined $c->{children}; |
544 |
ul { |
545 |
foreach my $p ( @{ $c->{children} } ) { |
546 |
li { |
547 |
if ( defined( $p->{class} ) ) { |
548 |
{ class is $p->{class} }; |
549 |
} |
550 |
show( 'navigation-tree-category', $p ); |
551 |
children( $p ); |
552 |
} |
553 |
} |
554 |
} |
555 |
} |
556 |
|
557 |
my $strix = Strix->new({ instance => $instance }); |
558 |
|
559 |
my $navigation = $strix->site_navigation( $site_id ); |
560 |
#warn "## navigation = ",dump( $navigation ); |
561 |
if ( $navigation ) { |
562 |
ul { |
563 |
{ class is 'navigation' }; |
564 |
foreach my $p ( @$navigation ) { |
565 |
li { |
566 |
show( 'navigation-tree-category', $p ); |
567 |
children( $p ); |
568 |
} |
569 |
} |
570 |
} |
571 |
} else { |
572 |
div { |
573 |
{ class is 'note error' } |
574 |
_('No navigation found for instance %1 site_id %2', $instance, $site_id) |
575 |
} |
576 |
|
577 |
} |
578 |
|
579 |
}; |
580 |
|
581 |
1; |