/[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

Diff of /lib/Strix.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 168 by dpavlin, Sun Jun 15 23:59:51 2008 UTC revision 171 by dpavlin, Mon Jun 16 13:16:57 2008 UTC
# Line 4  use strict; Line 4  use strict;
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(site uid) );
8    
9  use DBI;  use DBI;
10  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
# Line 30  Strix Line 30  Strix
30    
31  =cut  =cut
32    
33    our $site_dbh;
34    
35  sub dbh {  sub dbh {
36          my $self = shift;          my $self = shift;
37    
38          my $site = shift || $self->site || confess "no site";          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";          my $config = Jifty->config->app('strix') or die "need strix config";
43          my $database = $config->{database} or die "no strix.database in config";          my $database = $config->{database} or die "no strix.database in config";
44    
# Line 49  sub dbh { Line 53  sub dbh {
53    
54          my $dbh = DBI->connect( $dsn, $database->{user}, $database->{passwd} ) or die $DBI::errstr;          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;          return $dbh;
61  }  }
62    
# Line 152  sub layout { Line 160  sub layout {
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;  1;

Legend:
Removed from v.168  
changed lines
  Added in v.171

  ViewVC Help
Powered by ViewVC 1.1.26