blob: c07df479cd20ab5cbd8cfc4b059ed0fd553f6610 [file] [log] [blame]
Nils Diewald5d1ffb42014-05-21 17:45:34 +00001package Korap::Plugin::Notifications;
2use Mojo::Base 'Mojolicious::Plugin';
3use Mojo::Util qw/camelize/;
4
5our $TYPE_RE = qr/^[-a-zA-Z_]+$/;
6
7# Todo: Support multiple notification center,
8# so the notifications can be part of
9# json as well as XML
10# Possibly use 'n!.a' for flash as this will be in the cookie!
11
12sub register {
13 my ($plugin, $mojo, $param) = @_;
14
15 $param ||= {};
16
17 # Load parameter from Config file
18 if (my $config_param = $mojo->config('Notifications')) {
19 $param = { %$config_param, %$param };
20 };
21
22 my $debug = $mojo->mode eq 'development' ? 1 : 0;
23
24 my $center = camelize(delete $param->{use} // __PACKAGE__ . '::HTML');
25
26 if (index($center,'::') < 0) {
27 $center = __PACKAGE__ . '::' . $center;
28 };
29
30 my $center_plugin = $mojo->plugins->load_plugin($center);
31 $center_plugin->register($mojo, $param);
32
33 # Add notifications
34 $mojo->helper(
35 notify => sub {
36 my $c = shift;
37 my $type = shift;
38
39 return if $type !~ $TYPE_RE || (!$debug && $type eq 'debug');
40
41 my $array;
42
43 # New notifications
44 if ($array = $c->stash('notify.array')) {
45 push @$array, [$type => @_];
46 }
47
48 # Notifications already set
49 else {
50 $c->stash->{'notify.array'} = [[$type => @_]];
51 };
52 }
53 );
54
55 # Make notifications flash in case of a redirect
56 $mojo->hook(
57 after_dispatch => sub {
58 my ($c) = @_;
59 if ($c->stash('notify.array') && $c->res->is_status_class(300)) {
60 $c->flash('notify.array' => delete $c->stash->{'notify.array'});
61 };
62 }
63 );
64
65 # Embed notification display
66 $mojo->helper(
67 notifications => sub {
68 my $c = shift;
69
70 my @notify_array;
71
72 # Get flash notifications
73 my $flash = $c->flash('notify.array');
74 if ($flash && ref $flash eq 'ARRAY') {
75
76 # Ensure that no harmful types are injected
77 push @notify_array, grep { $_->[0] =~ $TYPE_RE } @$flash;
78
79 $c->flash('notify.array' => undef);
80 };
81
82 # Get stash notifications
83 if ($c->stash('notify.array')) {
84 push @notify_array, @{ delete $c->stash->{'notify.array'} };
85 };
86
87 # Nothing to do
88 return '' unless @notify_array or @_;
89
90 # Forward messages to notification center
91 $center_plugin->notifications($c, \@notify_array, @_);
92 }
93 );
94};
95
96
971;
98
99
100__END__
101
102=head2 notify
103
104 $c->notify(error => 'Something went wrong');
105 $c->notify(error => { timeout => 4000 } => 'Something went wrong');
106
107=head2 notifications
108
109 %= include_notification_center
110 %= include_notification_center qw/warn error success/
111
112The notification won't be included in case no notifications are
113in the queue and no parameters are passed.
114
115
116
117% if (flash('fine') || flash('alert') || stash('fine') || stash('alert')) {
118%= javascript '/js/humane.min.js'
119%= javascript begin
120 humane.baseCls = 'humane-libnotify';
121% if (flash('fine') || stash('fine')) {
122 humane.log("<%= l(flash('fine') || stash('fine')) %>", {
123 timeout: 3000,
124 clickToClose: true,
125 addnCls: 'humane-libnotify-success'
126 });
127% };
128% if (flash('alert') || stash('alert')) {
129 humane.log("<%= l(flash('alert') || stash('alert')) %>", {
130 timeout: 3000,
131 clickToClose: true,
132 addnCls: 'humane-libnotify-error'
133 });
134% };
135%= end
136% };