--- lib/Strix.pm 2008/06/15 23:59:51 168 +++ lib/Strix.pm 2008/06/16 12:44:57 169 @@ -4,7 +4,7 @@ use warnings; use base qw(Jifty::Object Class::Accessor::Fast); -__PACKAGE__->mk_accessors( qw(site) ); +__PACKAGE__->mk_accessors( qw(site uid) ); use DBI; use Data::Dump qw/dump/; @@ -152,4 +152,129 @@ } +=head2 sitemap + + my $sitemap = $strix->sitemap( $site_id, $uid ); + +=cut + +sub sitemap { + my $self = shift; + + my ( $site_id, $uid ) = @_; + my $sitemap; + + my $query = "SELECT *, ( length(ordstr)/3 ) - 1 AS depth FROM site WHERE id = ?"; + $query = "SELECT *, coalesce(( length(ordstr)/3 ) - 1,0) AS depth FROM site ORDER BY ordstr" unless $site_id; + + my $sth = $self->dbh->prepare( $query ); + if ( $site_id ) { + $sth->execute( $site_id ); + } else { + $sth->execute; + } + + while (my $row = $sth->fetchrow_hashref() ) { + warn dump( $row ) if $debug; + + $sitemap->{ $row->{naziv} } = $self->site_navigation( $site_id, $uid ); + } + + return $sitemap; +} + +=head2 site_navigation + + my $navigation = $strix->site_navigation( $site_id, $uid ); + +=cut + +sub site_navigation { + my $self = shift; + + my ( $site_id, $uid ) = @_; + + $uid ||= 1; # anonymous +# $uid ||= 2; # admin + + my $sth = $self->dbh->prepare( + "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"); + $sth->execute( $site_id, $uid ); + + Jifty->log->debug("site $site_id has ", $sth->rows, " categories for uid $uid"); + + my $navigation = []; + + my @pos = ( 0 ); + + while (my $kat = $sth->fetchrow_hashref() ) { + warn "# kat = ",dump( $kat ) if $debug; + die "no depth" unless $kat->{depth}; + + my $node = { type => 'category' }; + foreach my $c ( qw/naziv url/ ) { + $node->{$c} = $kat->{$c}; + } + + my $depth = $kat->{depth}; + @pos = splice( @pos, 0, $depth ); + $pos[ $depth - 1 ]++; + + warn "## category depth = $depth pos = ",dump( @pos ); + + my $code = '$navigation'; + map { $code .= '->[' . ( $_ - 1 ) . ']->{children}' } @pos; + $code =~ s/->{children}$//; + warn "## category code: $code\n"; + eval $code . '= $node'; + if ( $@ ) { + warn "SKIPPED CATEGORY: $@ ",dump( $kat ); + next; + } + + my $sth_ms = $self->dbh->prepare( + "SELECT + multistatic.id, multistatic.title, multistatic.kname, + multistatic_navigation.kategorija_id, multistatic_navigation.prikaz, + (LENGTH(multistatic_navigation.prikaz)/3) AS depth + FROM multistatic, multistatic_navigation + WHERE multistatic.id = multistatic_navigation.multistatic_id + AND multistatic_navigation.prikaz != '' + AND multistatic_navigation.kategorija_id = ? + AND multistatic.title != '' + ORDER BY multistatic_navigation.prikaz"); + $sth_ms->execute( $kat->{id} ); + + if ( my $rows = $sth_ms->rows ) { + Jifty->log->debug("$site_id has $rows multistatic pages"); + + while (my $ms = $sth_ms->fetchrow_hashref() ) { + warn "# ms = ",dump( $ms ) if $debug; + + my $node = { + naziv => $ms->{title}, + url => $kat->{url} . '?ms_nav=' . $ms->{prikaz}, + type => 'multistatic', + }; + + my $ms_depth = $ms->{depth} + $depth; + my $p = $pos[ $ms_depth - 1 ]++; + warn "## multistatic depth = $ms_depth pos = ",dump( @pos ); + + my $ms_code = $code . '->{children}->[ ' . $p . '] = $node'; + warn "## multistatic code: $ms_code\n"; + eval $ms_code; + if ( $@ ) { + warn "SKIPPED MULTISTATIC: $@ ",dump( $ms ); + next; + } + } + } + + } + + return $navigation; + +} + 1;