/[A3C]/lib/Strix.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /lib/Strix.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.26