1 |
package Strix; |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
|
6 |
use base qw(Jifty::Object Class::Accessor::Fast); |
7 |
__PACKAGE__->mk_accessors( qw(site uid) ); |
8 |
|
9 |
use DBI; |
10 |
use Data::Dump qw/dump/; |
11 |
use Carp qw/confess/; |
12 |
|
13 |
our $debug = 0; |
14 |
|
15 |
=head1 NAME |
16 |
|
17 |
Strix |
18 |
|
19 |
=head1 METHODS |
20 |
|
21 |
=head2 new |
22 |
|
23 |
my $strix = Strix->new({ site => 'os-test0604-zg' }); |
24 |
|
25 |
=head2 dbh |
26 |
|
27 |
my $dbh = Strix->dbh( $site_name ); |
28 |
|
29 |
my $dbh = $strix->dbh; |
30 |
|
31 |
=cut |
32 |
|
33 |
our $site_dbh; |
34 |
|
35 |
sub dbh { |
36 |
my $self = shift; |
37 |
|
38 |
my $site = shift || $self->site || confess "no site"; |
39 |
|
40 |
return $site_dbh->{$site} if $site_dbh->{$site}; |
41 |
|
42 |
my $config = Jifty->config->app('strix') or die "need strix config"; |
43 |
my $database = $config->{database} or die "no strix.database in config"; |
44 |
|
45 |
Jifty->log->debug("using config ", dump( $database )); |
46 |
|
47 |
my $dsn = |
48 |
'DBI:Pg:dbname=' . $site . |
49 |
';host=' . $database->{host} . |
50 |
';port=' . $database->{port}; |
51 |
|
52 |
Jifty->log->info("Connect to site $site with dsn $dsn"); |
53 |
|
54 |
my $dbh = DBI->connect( $dsn, $database->{user}, $database->{passwd} ) or die $DBI::errstr; |
55 |
|
56 |
$site_dbh->{$site} = $dbh; |
57 |
|
58 |
warn "## site_dbh = ",dump( $site_dbh ); |
59 |
|
60 |
return $dbh; |
61 |
} |
62 |
|
63 |
=head2 category |
64 |
|
65 |
my $category = Strix->category( $url ); |
66 |
|
67 |
=cut |
68 |
|
69 |
sub category { |
70 |
my $self = shift; |
71 |
|
72 |
my $url = shift || confess "no url"; |
73 |
|
74 |
# sysinc/profiles.php |
75 |
my $sth = $self->dbh->prepare(qq{ |
76 |
SELECT kategorija.*, lang.langid, lang.locale, template.tfilename, template.tflags, site.naziv as sitename, site.admin_mail, site.address, site.root as site_root, getPathFromNav(kategorija.id) as path, site.ordstr as site_ordstr FROM kategorija, template, site, lang WHERE kategorija.url = ? AND kategorija.template_id = template.id AND kategorija.site_id = site.id AND lang.id = kategorija.lang |
77 |
}); |
78 |
$sth->execute($url); |
79 |
|
80 |
my $category = $sth->fetchrow_hashref() or die "can't fetch category $url"; |
81 |
return $category; |
82 |
} |
83 |
|
84 |
=head2 layout |
85 |
|
86 |
my $layout = $strix->layout( $url ); |
87 |
|
88 |
=cut |
89 |
|
90 |
sub layout { |
91 |
my $self = shift; |
92 |
|
93 |
my $url = shift || confess "no url"; |
94 |
|
95 |
my $dbh = $self->dbh; |
96 |
my $category = $self->category( $url ); |
97 |
|
98 |
my $sth = $dbh->prepare(qq{ |
99 |
SELECT template.tfilename, template.tflags FROM template WHERE id = ? |
100 |
}); |
101 |
$sth->execute( $category->{template_id} ); |
102 |
|
103 |
my $template = $sth->fetchrow_hashref() or die "can't fetch template"; |
104 |
|
105 |
warn "template = ",dump( $template ) if $debug; |
106 |
|
107 |
my $page; |
108 |
warn "### free layout...\n" if $debug; |
109 |
|
110 |
# index.php |
111 |
$sth = $dbh->prepare(qq{ |
112 |
SELECT layout_id, module_id, pozicija, module_args, name, fname, notitle, class_name |
113 |
FROM pre_layout, modules |
114 |
WHERE id=module_id AND ? = template_id AND redoslijed >= 0 |
115 |
ORDER BY redoslijed DESC |
116 |
}); |
117 |
$sth->execute( $category->{template_id} ); |
118 |
|
119 |
while (my $row = $sth->fetchrow_hashref() ) { |
120 |
warn dump( $row ) if $debug; |
121 |
push @{ $page->{free} }, { $row->{name} => $row->{module_args} }; |
122 |
} |
123 |
|
124 |
warn "### pre layout...\n" if $debug; |
125 |
|
126 |
$sth = $dbh->prepare(qq{ |
127 |
SELECT |
128 |
l.id as layout_id, l.user_id, l.kategorija_id, l.module_id, l.pozicija, l.redoslijed, l.module_args, l.state, l.notitle, |
129 |
m.name, m.fname, m.hidden, m.nocache, m.pos, m.class_name, |
130 |
acl_module.acl_register_id |
131 |
FROM layout as l, modules as m LEFT JOIN acl_module ON (acl_module.modules_id = m.id) |
132 |
WHERE l.user_id=? |
133 |
AND l.kategorija_id=? |
134 |
AND m.id=l.module_id |
135 |
ORDER BY pozicija,redoslijed DESC |
136 |
}); |
137 |
$sth->execute( 1, $category->{id} ); |
138 |
|
139 |
while (my $row = $sth->fetchrow_hashref() ) { |
140 |
warn dump( $row ) if $debug; |
141 |
push @{ $page->{pre}->{ $row->{pos} } }, { $row->{name} => $row->{module_args} }; |
142 |
} |
143 |
|
144 |
warn "### post layout...\n" if $debug; |
145 |
|
146 |
$sth = $dbh->prepare(qq{ |
147 |
SELECT layout_id, module_id, pozicija, module_args, name, notitle |
148 |
FROM pre_layout, modules |
149 |
WHERE id=module_id AND ? = template_id AND redoslijed < 0 |
150 |
ORDER BY redoslijed DESC |
151 |
}); |
152 |
$sth->execute( $category->{template_id} ); |
153 |
|
154 |
while (my $row = $sth->fetchrow_hashref() ) { |
155 |
warn dump( $row ) if $debug; |
156 |
push @{ $page->{post}->{ $row->{pozicija} } }, { $row->{name} => $row->{module_args} }; |
157 |
} |
158 |
|
159 |
return $page; |
160 |
|
161 |
} |
162 |
|
163 |
=head2 sitemap |
164 |
|
165 |
my $sitemap = $strix->sitemap( $site_id, $uid ); |
166 |
|
167 |
=cut |
168 |
|
169 |
sub sitemap { |
170 |
my $self = shift; |
171 |
|
172 |
my ( $site_id, $uid ) = @_; |
173 |
my $sitemap; |
174 |
|
175 |
my $query = "SELECT *, ( length(ordstr)/3 ) - 1 AS depth FROM site WHERE id = ?"; |
176 |
$query = "SELECT *, coalesce(( length(ordstr)/3 ) - 1,0) AS depth FROM site ORDER BY ordstr" unless $site_id; |
177 |
|
178 |
my $sth = $self->dbh->prepare( $query ); |
179 |
if ( $site_id ) { |
180 |
$sth->execute( $site_id ); |
181 |
} else { |
182 |
$sth->execute; |
183 |
} |
184 |
|
185 |
while (my $row = $sth->fetchrow_hashref() ) { |
186 |
warn dump( $row ) if $debug; |
187 |
|
188 |
$sitemap->{ $row->{naziv} } = $self->site_navigation( $site_id, $uid ); |
189 |
} |
190 |
|
191 |
return $sitemap; |
192 |
} |
193 |
|
194 |
=head2 site_navigation |
195 |
|
196 |
my $navigation = $strix->site_navigation( $site_id, $uid ); |
197 |
|
198 |
=cut |
199 |
|
200 |
sub site_navigation { |
201 |
my $self = shift; |
202 |
|
203 |
my ( $site_id, $uid ) = @_; |
204 |
|
205 |
$uid ||= 1; # anonymous |
206 |
# $uid ||= 2; # admin |
207 |
|
208 |
my $sth = $self->dbh->prepare( |
209 |
"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"); |
210 |
$sth->execute( $site_id, $uid ); |
211 |
|
212 |
Jifty->log->debug("site $site_id has ", $sth->rows, " categories for uid $uid"); |
213 |
|
214 |
my $navigation = []; |
215 |
|
216 |
my @pos = ( 0 ); |
217 |
|
218 |
while (my $kat = $sth->fetchrow_hashref() ) { |
219 |
warn "# kat = ",dump( $kat ) if $debug; |
220 |
die "no depth" unless $kat->{depth}; |
221 |
|
222 |
my $node = { type => 'category' }; |
223 |
foreach my $c ( qw/naziv url/ ) { |
224 |
$node->{$c} = $kat->{$c}; |
225 |
} |
226 |
|
227 |
my $depth = $kat->{depth}; |
228 |
@pos = splice( @pos, 0, $depth ); |
229 |
$pos[ $depth - 1 ]++; |
230 |
|
231 |
warn "## category depth = $depth pos = ",dump( @pos ); |
232 |
|
233 |
my $code = '$navigation'; |
234 |
map { $code .= '->[' . ( $_ - 1 ) . ']->{children}' } @pos; |
235 |
$code =~ s/->{children}$//; |
236 |
warn "## category code: $code\n"; |
237 |
eval $code . '= $node'; |
238 |
if ( $@ ) { |
239 |
warn "SKIPPED CATEGORY: $@ ",dump( $kat ); |
240 |
next; |
241 |
} |
242 |
|
243 |
my $sth_ms = $self->dbh->prepare( |
244 |
"SELECT |
245 |
multistatic.id, multistatic.title, multistatic.kname, |
246 |
multistatic_navigation.kategorija_id, multistatic_navigation.prikaz, |
247 |
(LENGTH(multistatic_navigation.prikaz)/3) AS depth |
248 |
FROM multistatic, multistatic_navigation |
249 |
WHERE multistatic.id = multistatic_navigation.multistatic_id |
250 |
AND multistatic_navigation.prikaz != '' |
251 |
AND multistatic_navigation.kategorija_id = ? |
252 |
AND multistatic.title != '' |
253 |
ORDER BY multistatic_navigation.prikaz"); |
254 |
$sth_ms->execute( $kat->{id} ); |
255 |
|
256 |
if ( my $rows = $sth_ms->rows ) { |
257 |
Jifty->log->debug("$site_id has $rows multistatic pages"); |
258 |
|
259 |
while (my $ms = $sth_ms->fetchrow_hashref() ) { |
260 |
warn "# ms = ",dump( $ms ) if $debug; |
261 |
|
262 |
my $node = { |
263 |
naziv => $ms->{title}, |
264 |
url => $kat->{url} . '?ms_nav=' . $ms->{prikaz}, |
265 |
type => 'multistatic', |
266 |
}; |
267 |
|
268 |
my $ms_depth = $ms->{depth} + $depth; |
269 |
my $p = $pos[ $ms_depth - 1 ]++; |
270 |
warn "## multistatic depth = $ms_depth pos = ",dump( @pos ); |
271 |
|
272 |
my $ms_code = $code . '->{children}->[ ' . $p . '] = $node'; |
273 |
warn "## multistatic code: $ms_code\n"; |
274 |
eval $ms_code; |
275 |
if ( $@ ) { |
276 |
warn "SKIPPED MULTISTATIC: $@ ",dump( $ms ); |
277 |
next; |
278 |
} |
279 |
} |
280 |
} |
281 |
|
282 |
} |
283 |
|
284 |
return $navigation; |
285 |
|
286 |
} |
287 |
|
288 |
1; |