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