line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Ado::Plugin::AdoHelpers; |
2
|
23
|
|
|
23
|
|
25001
|
use Mojo::Base 'Ado::Plugin'; |
|
23
|
|
|
|
|
46
|
|
|
23
|
|
|
|
|
170
|
|
3
|
23
|
|
|
23
|
|
3517
|
use Mojo::Util qw(slurp decode); |
|
23
|
|
|
|
|
43
|
|
|
23
|
|
|
|
|
1465
|
|
4
|
23
|
|
|
23
|
|
132
|
use List::Util qw(first); |
|
23
|
|
|
|
|
42
|
|
|
23
|
|
|
|
|
24793
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# allow plugins to process SQL scripts while loading |
7
|
|
|
|
|
|
|
sub do_sql_file { |
8
|
3
|
|
|
3
|
1
|
1549
|
my ($app, $sql_file) = @_; |
9
|
3
|
|
|
|
|
26
|
my $dbh = $app->dbix->dbh; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
#$app->log->debug('do_sql_file:' . $sql_file) if $Ado::Control::DEV_MODE; |
12
|
|
|
|
|
|
|
|
13
|
3
|
|
|
|
|
5255
|
my $SQL = decode('UTF-8', slurp($sql_file)); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
#Remove multi-line comments |
16
|
2
|
|
|
|
|
209
|
$SQL =~ s|/\*+.+?\*/\s+?||gsmx; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
#$app->log->debug('$SQL:' . $SQL) if $Ado::Control::DEV_MODE; |
19
|
2
|
|
|
|
|
44
|
local $dbh->{RaiseError} = 1; |
20
|
2
|
|
|
|
|
42
|
my $last_statement = ''; |
21
|
|
|
|
|
|
|
return eval { |
22
|
|
|
|
|
|
|
$dbh->begin_work; |
23
|
|
|
|
|
|
|
for my $st (split /;/smx, $SQL) { |
24
|
|
|
|
|
|
|
$last_statement = $st; |
25
|
|
|
|
|
|
|
$dbh->do($st) if ($st =~ /\S+/smx); |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
$dbh->commit; |
28
|
2
|
|
66
|
|
|
4
|
} || do { |
29
|
|
|
|
|
|
|
my $e = "\nError in statement:$last_statement\n$@"; |
30
|
|
|
|
|
|
|
my $e2; |
31
|
|
|
|
|
|
|
eval { $dbh->rollback } || ($e2 = $/ . 'Additionally we have a rollback error:' . $@); |
32
|
|
|
|
|
|
|
$app->log->error($e . ($e2 ? $e2 : '')); |
33
|
|
|
|
|
|
|
Carp::croak($e . ($e2 ? $e2 : '')); |
34
|
|
|
|
|
|
|
}; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub register { |
38
|
24
|
|
|
24
|
1
|
2263
|
my ($self, $app, $conf) = shift->initialise(@_); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# Add helpers |
41
|
24
|
|
|
58
|
|
183
|
$app->helper(user => sub { shift->user(@_) }); |
|
58
|
|
|
|
|
10918
|
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# http://irclog.perlgeek.de/mojo/2014-10-03#i_9453021 |
44
|
24
|
|
|
1
|
|
617
|
$app->helper(to_json => sub { Mojo::JSON::to_json($_[1]) }); |
|
1
|
|
|
|
|
16
|
|
45
|
24
|
|
|
|
|
425
|
Mojo::Util::monkey_patch(ref($app), do_sql_file => \&Ado::Plugin::AdoHelpers::do_sql_file); |
46
|
|
|
|
|
|
|
|
47
|
24
|
|
|
|
|
633
|
$app->helper('head_css' => \&_head_css); |
48
|
24
|
|
|
|
|
551
|
$app->helper('head_javascript' => \&_head_javascript); |
49
|
|
|
|
|
|
|
|
50
|
24
|
|
|
|
|
708
|
return $self; |
51
|
|
|
|
|
|
|
} #end of register |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
my $file_re = qr/\w+\.\w+(\?.*)?$/; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub _head_css { |
56
|
442
|
|
|
442
|
|
1223100
|
my ($c, $assets) = @_; |
57
|
442
|
|
|
|
|
1549
|
my $assets_list = $c->stash('head_css'); |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
#append |
60
|
442
|
100
|
|
|
|
4521
|
if ($assets) { |
61
|
362
|
100
|
|
|
|
1261
|
$assets = [$assets] unless ref($assets) eq 'ARRAY'; |
62
|
362
|
|
|
|
|
767
|
foreach my $a (@$assets) { |
63
|
1295
|
100
|
|
12782
|
|
4871
|
push @$assets_list, $a unless first { $_ eq $a } @$assets_list; |
|
12782
|
|
|
|
|
13432
|
|
64
|
|
|
|
|
|
|
} |
65
|
362
|
|
|
|
|
1021
|
return; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# render |
69
|
80
|
|
|
|
|
188
|
my $css = ''; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
#everything in separate stylesheet begin end block or |
72
|
80
|
|
|
|
|
262
|
foreach my $a (@$assets_list) { |
73
|
1312
|
100
|
|
|
|
23675
|
if ($a =~ $file_re) { # a file |
|
|
50
|
|
|
|
|
|
74
|
1129
|
|
|
|
|
2320
|
$css .= qq|\n|; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
elsif (ref $a eq 'CODE') { # a code |
77
|
183
|
|
|
|
|
1491
|
$css .= $c->stylesheet($a) . $/; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
else { # a string |
80
|
0
|
|
|
0
|
|
0
|
$css .= $c->stylesheet(sub {$a}) . $/; |
|
0
|
|
|
|
|
0
|
|
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
} |
83
|
80
|
|
|
|
|
4141
|
return $css; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub _head_javascript { |
87
|
391
|
|
|
391
|
|
3897
|
my ($c, $assets) = @_; |
88
|
391
|
|
|
|
|
1098
|
my $assets_list = $c->stash('head_javascript'); |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
#append |
91
|
391
|
100
|
|
|
|
3615
|
if ($assets) { |
92
|
311
|
100
|
|
|
|
1142
|
$assets = [$assets] unless ref($assets) eq 'ARRAY'; |
93
|
311
|
|
|
|
|
613
|
foreach my $a (@$assets) { |
94
|
445
|
100
|
|
2650
|
|
1715
|
push @$assets_list, $a unless first { $_ eq $a } @$assets_list; |
|
2650
|
|
|
|
|
3564
|
|
95
|
|
|
|
|
|
|
} |
96
|
311
|
|
|
|
|
858
|
return; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
# render |
100
|
80
|
|
|
|
|
165
|
my $js = ''; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
#everything in separate javascript begin/end block or \n|; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
elsif (ref $a eq 'CODE') { # a code |
108
|
183
|
|
|
|
|
1184
|
$js .= $c->javascript($a) . $/; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
else { # a string |
111
|
0
|
|
|
0
|
|
0
|
$js .= $c->javascript(sub {$a}) . $/; |
|
0
|
|
|
|
|
0
|
|
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
} |
114
|
80
|
|
|
|
|
3635
|
return $js; |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
1; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=encoding utf8 |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head1 NAME |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
Ado::Plugin::AdoHelpers - Default Ado helpers plugin |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head1 SYNOPSIS |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
# Ado |
129
|
|
|
|
|
|
|
$self->plugin('AdoHelpers'); |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
# Mojolicious::Lite |
132
|
|
|
|
|
|
|
plugin 'AdoHelpers'; |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head1 DESCRIPTION |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
L is a collection of renderer helpers for |
137
|
|
|
|
|
|
|
L. |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
This is a core plugin, that means it is always enabled and its code a good |
140
|
|
|
|
|
|
|
example for learning to build new plugins, you're welcome to fork it. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
See L for a list of plugins that are available |
143
|
|
|
|
|
|
|
by default. |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head1 HELPERS |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
L implements the following helpers. |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head2 do_sql_file |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
Your plugin may need to add some new tables, add columns to already |
152
|
|
|
|
|
|
|
existing tables or insert some data. This method allows you to do that. |
153
|
|
|
|
|
|
|
See the source code of L for example. |
154
|
|
|
|
|
|
|
The SQL file will be slurped, multiline comments will be removed. |
155
|
|
|
|
|
|
|
The content will be split into C<';'> and each statement will be executed |
156
|
|
|
|
|
|
|
using L. |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
# in a plugin somewhere in register |
159
|
|
|
|
|
|
|
$app->do_sql_file(catfile($self->config_dir, $sql_file)); |
160
|
|
|
|
|
|
|
$app->do_sql_file($conf->{vest_data_sql_file}); |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
# on the command line |
163
|
|
|
|
|
|
|
$ ado eval 'app->do_sql_file(shift)' some_file.sql |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
# elsewhere in an application |
166
|
|
|
|
|
|
|
$app->do_sql_file($sql_file) |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head2 head_css, head_javascript |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Minimalist asset management for the CheadE> section. Appends and |
171
|
|
|
|
|
|
|
later renders assets (links to files and code-snippets) to |
172
|
|
|
|
|
|
|
C<$c-Estash('head_css')> and Cstash('head_javascript')>. The new |
173
|
|
|
|
|
|
|
assets are only appended if they are not already present in the corresponding |
174
|
|
|
|
|
|
|
list of assets. The defaults are populated in C. See also: |
175
|
|
|
|
|
|
|
L; L. |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
#in a template: |
178
|
|
|
|
|
|
|
#append |
179
|
|
|
|
|
|
|
<% |
180
|
|
|
|
|
|
|
head_css([ |
181
|
|
|
|
|
|
|
'vendor/SemanticUI/components/popup.min.css' |
182
|
|
|
|
|
|
|
'#myid { font-size:xx-small }' |
183
|
|
|
|
|
|
|
]); |
184
|
|
|
|
|
|
|
head_javascript([ |
185
|
|
|
|
|
|
|
'vendor/SemanticUI/components/popup.min.js' |
186
|
|
|
|
|
|
|
'jQuery( function($){ $('#ado-img').popup() })' |
187
|
|
|
|
|
|
|
]); |
188
|
|
|
|
|
|
|
%> |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
# or |
191
|
|
|
|
|
|
|
% head_javascript begin |
192
|
|
|
|
|
|
|
jQuery( function($){ $('#ado-img').popup() }); |
193
|
|
|
|
|
|
|
% end; |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
# render in templates/partials/head.html.ep |
196
|
|
|
|
|
|
|
%== head_css; |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
rel='stylesheet' type='text/css' /> |
200
|
|
|
|
|
|
|
%== head_javascript; |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=head2 to_json |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
Suitable for preparing JavaScript |
205
|
|
|
|
|
|
|
objects from Perl references that will be used from stash and in templates. |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
my $chars = $c->to_json({name =>'Петър',id=>2}); |
208
|
|
|
|
|
|
|
$c->stash(user_as_js => $chars); |
209
|
|
|
|
|
|
|
# in a javascript chunk of a template |
210
|
|
|
|
|
|
|
var user = <%== $user_as_js %>; |
211
|
|
|
|
|
|
|
var user_group_names = <%== to_json([user->ingroup]) %>; |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=head2 user |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
Returns the current user. This is the user C for not authenticated users. |
216
|
|
|
|
|
|
|
This helper is a wrapper for L. |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
$c->user(Ado::Model::Users->query("SELECT * from users WHERE login_name='guest'")); |
219
|
|
|
|
|
|
|
#in a controller action: |
220
|
|
|
|
|
|
|
my $current_user = $c->user; |
221
|
|
|
|
|
|
|
#in a template: |
222
|
|
|
|
|
|
|
Hello, <%=user->name%>! |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=head1 METHODS |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
L inherits all methods from |
227
|
|
|
|
|
|
|
L and implements the following new ones. |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=head2 register |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
$plugin->register(Ado->new); |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
Register helpers in L application. |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
=head1 SEE ALSO |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
L, L, L, |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
=head1 AUTHOR |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
Красимир Беров (Krasimir Berov) |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
Copyright 2013-2015 Красимир Беров (Krasimir Berov). |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
This program is free software, you can redistribute it and/or |
249
|
|
|
|
|
|
|
modify it under the terms of the |
250
|
|
|
|
|
|
|
GNU Lesser General Public License v3 (LGPL-3.0). |
251
|
|
|
|
|
|
|
You may copy, distribute and modify the software provided that |
252
|
|
|
|
|
|
|
modifications are open source. However, software that includes |
253
|
|
|
|
|
|
|
the license may release under a different license. |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
See http://opensource.org/licenses/lgpl-3.0.html for more information. |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
=cut |