4 |
use warnings; |
use warnings; |
5 |
|
|
6 |
use base qw(Jifty::Object Class::Accessor::Fast); |
use base qw(Jifty::Object Class::Accessor::Fast); |
7 |
__PACKAGE__->mk_accessors( qw(site) ); |
__PACKAGE__->mk_accessors( qw(instance uid) ); |
8 |
|
|
9 |
use DBI; |
use DBI; |
10 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
11 |
use Carp qw/confess/; |
use Carp qw/confess/; |
12 |
|
use Jifty; |
13 |
|
|
14 |
our $debug = 0; |
our $debug = 0; |
15 |
|
|
21 |
|
|
22 |
=head2 new |
=head2 new |
23 |
|
|
24 |
my $strix = Strix->new({ site => 'os-test0604-zg' }); |
my $strix = Strix->new({ instance => 'os-test0604-zg' }); |
25 |
|
|
26 |
=head2 dbh |
=head2 dbh |
27 |
|
|
28 |
my $dbh = Strix->dbh( $site_name ); |
my $dbh = Strix->dbh( $strix_instance ); |
29 |
|
|
30 |
my $dbh = $strix->dbh; |
my $dbh = $strix->dbh; |
31 |
|
|
32 |
=cut |
=cut |
33 |
|
|
34 |
|
our $instance_dbh; |
35 |
|
our @instances_active; |
36 |
|
|
37 |
sub dbh { |
sub dbh { |
38 |
my $self = shift; |
my $self = shift; |
39 |
|
|
40 |
my $site = shift || $self->site || confess "no site"; |
my $instance = shift || ref($self) && $self->instance || confess "no instance"; |
41 |
|
|
42 |
|
return $instance_dbh->{$instance} if $instance_dbh->{$instance}; |
43 |
|
|
44 |
my $config = Jifty->config->app('strix') or die "need strix config"; |
my $config = Jifty->config->app('strix') or die "need strix config"; |
45 |
my $database = $config->{database} or die "no strix.database in config"; |
my $database = $config->{database} or die "no strix.database in config"; |
47 |
Jifty->log->debug("using config ", dump( $database )); |
Jifty->log->debug("using config ", dump( $database )); |
48 |
|
|
49 |
my $dsn = |
my $dsn = |
50 |
'DBI:Pg:dbname=' . $site . |
'DBI:Pg:dbname=' . $instance . |
51 |
';host=' . $database->{host} . |
';host=' . $database->{host} . |
52 |
';port=' . $database->{port}; |
';port=' . $database->{port}; |
53 |
|
|
54 |
Jifty->log->info("Connect to site $site with dsn $dsn"); |
Jifty->log->info("Connect to instance $instance with dsn $dsn"); |
55 |
|
|
56 |
|
my $dbh = DBI->connect( $dsn, $database->{user}, $database->{passwd} ) or die "$DBI::errstr\n"; |
57 |
|
|
58 |
my $dbh = DBI->connect( $dsn, $database->{user}, $database->{passwd} ) or die $DBI::errstr; |
# force database to send us back UTF-8 no metter what it's encoding |
59 |
|
$dbh->do("set client_encoding='utf-8'"); |
60 |
|
$dbh->{pg_enable_utf8} = 1; |
61 |
|
|
62 |
|
$instance_dbh->{$instance} = $dbh; |
63 |
|
push @instances_active, $instance; |
64 |
|
|
65 |
|
if ( $#instances_active > 5 ) { |
66 |
|
my $i = shift @instances_active; |
67 |
|
warn "## remove connection to instance $instance\n"; |
68 |
|
delete( $instance_dbh->{$i} ); |
69 |
|
} |
70 |
|
|
71 |
|
warn "## instance_dbh = ",dump( $instance_dbh ) if $debug; |
72 |
|
|
73 |
return $dbh; |
return $dbh; |
74 |
} |
} |
129 |
}); |
}); |
130 |
$sth->execute( $category->{template_id} ); |
$sth->execute( $category->{template_id} ); |
131 |
|
|
132 |
|
sub module_args { |
133 |
|
my $row = shift; |
134 |
|
return undef unless $row->{module_args}; |
135 |
|
my $args; |
136 |
|
foreach my $a ( split(/\&/, $row->{module_args} ) ) { |
137 |
|
$args->{$1} = $2 if $a =~ m/^(.+)=(.+)$/; |
138 |
|
} |
139 |
|
return $args; |
140 |
|
} |
141 |
|
|
142 |
while (my $row = $sth->fetchrow_hashref() ) { |
while (my $row = $sth->fetchrow_hashref() ) { |
143 |
warn dump( $row ) if $debug; |
warn dump( $row ) if $debug; |
144 |
push @{ $page->{free} }, { $row->{name} => $row->{module_args} }; |
push @{ $page->{free} }, { $row->{name} => module_args( $row ) }; |
145 |
} |
} |
146 |
|
|
147 |
warn "### pre layout...\n" if $debug; |
warn "### pre layout...\n" if $debug; |
161 |
|
|
162 |
while (my $row = $sth->fetchrow_hashref() ) { |
while (my $row = $sth->fetchrow_hashref() ) { |
163 |
warn dump( $row ) if $debug; |
warn dump( $row ) if $debug; |
164 |
push @{ $page->{pre}->{ $row->{pos} } }, { $row->{name} => $row->{module_args} }; |
push @{ $page->{pre}->{ $row->{pos} } }, { $row->{name} => module_args( $row ) }; |
165 |
} |
} |
166 |
|
|
167 |
warn "### post layout...\n" if $debug; |
warn "### post layout...\n" if $debug; |
176 |
|
|
177 |
while (my $row = $sth->fetchrow_hashref() ) { |
while (my $row = $sth->fetchrow_hashref() ) { |
178 |
warn dump( $row ) if $debug; |
warn dump( $row ) if $debug; |
179 |
push @{ $page->{post}->{ $row->{pozicija} } }, { $row->{name} => $row->{module_args} }; |
push @{ $page->{post}->{ $row->{pozicija} } }, { $row->{name} => module_args( $row ) }; |
180 |
} |
} |
181 |
|
|
182 |
return $page; |
return $page; |
183 |
|
|
184 |
} |
} |
185 |
|
|
186 |
|
=head2 sites |
187 |
|
|
188 |
|
my @sites = $strix->sites; |
189 |
|
|
190 |
|
=cut |
191 |
|
|
192 |
|
sub sites { |
193 |
|
my $self = shift; |
194 |
|
|
195 |
|
my @sites; |
196 |
|
|
197 |
|
my $sth = $self->dbh->prepare( |
198 |
|
"SELECT *, coalesce(( length(ordstr)/3 ) - 1,0) AS depth FROM site ORDER BY ordstr" |
199 |
|
); |
200 |
|
$sth->execute; |
201 |
|
|
202 |
|
while (my $row = $sth->fetchrow_hashref() ) { |
203 |
|
push @sites, $row; |
204 |
|
} |
205 |
|
|
206 |
|
return @sites; |
207 |
|
} |
208 |
|
|
209 |
|
=head2 site_navigation |
210 |
|
|
211 |
|
my $navigation = $strix->site_navigation( $site_id, $uid ); |
212 |
|
|
213 |
|
=cut |
214 |
|
|
215 |
|
sub site_navigation { |
216 |
|
my $self = shift; |
217 |
|
|
218 |
|
my ( $site_id, $uid ) = @_; |
219 |
|
|
220 |
|
$uid ||= 1; # anonymous |
221 |
|
# $uid ||= 2; # admin |
222 |
|
|
223 |
|
my $sth = $self->dbh->prepare( |
224 |
|
"SELECT kategorija.*, ((length(prikaz)+length(coalesce(ordstr,'')))/3)-1 as depth FROM kategorija JOIN navigacija ON (kategorija.id = kategorija_id), site WHERE site_id = ? AND site.id = site_id AND userCanDoOnObject(?, 1, 'kats', kategorija.id) ORDER BY prikaz"); |
225 |
|
$sth->execute( $site_id, $uid ); |
226 |
|
|
227 |
|
Jifty->log->debug("site $site_id has ", $sth->rows, " categories for uid $uid"); |
228 |
|
|
229 |
|
my $navigation = []; |
230 |
|
|
231 |
|
my @pos = ( 0 ); |
232 |
|
|
233 |
|
while (my $kat = $sth->fetchrow_hashref() ) { |
234 |
|
warn "# kat = ",dump( $kat ) if $debug; |
235 |
|
if ( ! $kat->{depth} ) { |
236 |
|
Jifty->log->error("depth increased to 1 in ",dump( $kat )); |
237 |
|
$kat->{depth} = 1; |
238 |
|
} |
239 |
|
|
240 |
|
my $node = { type => 'category' }; |
241 |
|
foreach my $c ( qw/naziv url/ ) { |
242 |
|
$node->{$c} = $kat->{$c}; |
243 |
|
} |
244 |
|
|
245 |
|
my $depth = $kat->{depth}; |
246 |
|
if ( ! defined $pos[ $depth - 2 ] ) { |
247 |
|
warn "FIXING CATEGORY: ",dump( $kat ); |
248 |
|
$node->{class} = "error"; |
249 |
|
$depth--; |
250 |
|
} |
251 |
|
@pos = splice( @pos, 0, $depth ); |
252 |
|
$pos[ $depth - 1 ]++; |
253 |
|
|
254 |
|
warn "## category depth = $depth pos = ",dump( @pos ) if $debug; |
255 |
|
|
256 |
|
my $code = '$navigation'; |
257 |
|
map { $code .= '->[' . ( $_ - 1 ) . ']->{children}' } @pos; |
258 |
|
$code =~ s/->{children}$//; |
259 |
|
warn "## category code: $code\n" if $debug; |
260 |
|
eval $code . '= $node'; |
261 |
|
if ( $@ ) { |
262 |
|
warn "SKIPPED CATEGORY: $@ ",dump( $kat ); |
263 |
|
next; |
264 |
|
} |
265 |
|
|
266 |
|
my $sth_ms = $self->dbh->prepare( |
267 |
|
"SELECT |
268 |
|
multistatic.id, multistatic.title, multistatic.kname, |
269 |
|
multistatic_navigation.kategorija_id, multistatic_navigation.prikaz, |
270 |
|
(LENGTH(multistatic_navigation.prikaz)/3) AS depth |
271 |
|
FROM multistatic, multistatic_navigation |
272 |
|
WHERE multistatic.id = multistatic_navigation.multistatic_id |
273 |
|
AND multistatic_navigation.prikaz != '' |
274 |
|
AND multistatic_navigation.kategorija_id = ? |
275 |
|
AND multistatic.title != '' |
276 |
|
ORDER BY multistatic_navigation.prikaz"); |
277 |
|
$sth_ms->execute( $kat->{id} ); |
278 |
|
|
279 |
|
if ( my $rows = $sth_ms->rows ) { |
280 |
|
Jifty->log->debug("$site_id has $rows multistatic pages"); |
281 |
|
|
282 |
|
while (my $ms = $sth_ms->fetchrow_hashref() ) { |
283 |
|
warn "# ms = ",dump( $ms ) if $debug; |
284 |
|
|
285 |
|
my $node = { |
286 |
|
naziv => $ms->{title}, |
287 |
|
url => $kat->{url} . '?ms_nav=' . $ms->{prikaz}, |
288 |
|
type => 'multistatic', |
289 |
|
}; |
290 |
|
|
291 |
|
my $ms_depth = $ms->{depth} + $depth; |
292 |
|
my $p = $pos[ $ms_depth - 1 ]++; |
293 |
|
warn "## multistatic depth = $ms_depth pos = ",dump( @pos ) if $debug; |
294 |
|
|
295 |
|
my $ms_code = $code . '->{children}->[ ' . $p . '] = $node'; |
296 |
|
warn "## multistatic code: $ms_code\n" if $debug; |
297 |
|
eval $ms_code; |
298 |
|
if ( $@ ) { |
299 |
|
warn "SKIPPED MULTISTATIC: $@ ",dump( $ms ); |
300 |
|
next; |
301 |
|
} |
302 |
|
} |
303 |
|
} |
304 |
|
|
305 |
|
} |
306 |
|
|
307 |
|
return $navigation; |
308 |
|
|
309 |
|
} |
310 |
|
|
311 |
1; |
1; |