--- lib/Strix.pm 2008/06/15 23:59:51 168 +++ lib/Strix.pm 2008/06/16 16:48:16 174 @@ -4,7 +4,7 @@ use warnings; use base qw(Jifty::Object Class::Accessor::Fast); -__PACKAGE__->mk_accessors( qw(site) ); +__PACKAGE__->mk_accessors( qw(instance uid) ); use DBI; use Data::Dump qw/dump/; @@ -20,20 +20,24 @@ =head2 new - my $strix = Strix->new({ site => 'os-test0604-zg' }); + my $strix = Strix->new({ instance => 'os-test0604-zg' }); =head2 dbh - my $dbh = Strix->dbh( $site_name ); + my $dbh = Strix->dbh( $strix_instance ); my $dbh = $strix->dbh; =cut +our $instance_dbh; + sub dbh { my $self = shift; - my $site = shift || $self->site || confess "no site"; + my $instance = shift || $self->instance || confess "no instance"; + + return $instance_dbh->{$instance} if $instance_dbh->{$instance}; my $config = Jifty->config->app('strix') or die "need strix config"; my $database = $config->{database} or die "no strix.database in config"; @@ -41,14 +45,18 @@ Jifty->log->debug("using config ", dump( $database )); my $dsn = - 'DBI:Pg:dbname=' . $site . + 'DBI:Pg:dbname=' . $instance . ';host=' . $database->{host} . ';port=' . $database->{port}; - Jifty->log->info("Connect to site $site with dsn $dsn"); + Jifty->log->info("Connect to instance $instance with dsn $dsn"); my $dbh = DBI->connect( $dsn, $database->{user}, $database->{passwd} ) or die $DBI::errstr; + $instance_dbh->{$instance} = $dbh; + + warn "## instance_dbh = ",dump( $instance_dbh ) if $debug; + return $dbh; } @@ -108,9 +116,19 @@ }); $sth->execute( $category->{template_id} ); + sub module_args { + my $row = shift; + return undef unless $row->{module_args}; + my $args; + foreach my $a ( split(/\&/, $row->{module_args} ) ) { + $args->{$1} = $2 if $a =~ m/^(.+)=(.+)$/; + } + return $args; + } + while (my $row = $sth->fetchrow_hashref() ) { warn dump( $row ) if $debug; - push @{ $page->{free} }, { $row->{name} => $row->{module_args} }; + push @{ $page->{free} }, { $row->{name} => module_args( $row ) }; } warn "### pre layout...\n" if $debug; @@ -130,7 +148,7 @@ while (my $row = $sth->fetchrow_hashref() ) { warn dump( $row ) if $debug; - push @{ $page->{pre}->{ $row->{pos} } }, { $row->{name} => $row->{module_args} }; + push @{ $page->{pre}->{ $row->{pos} } }, { $row->{name} => module_args( $row ) }; } warn "### post layout...\n" if $debug; @@ -145,11 +163,136 @@ while (my $row = $sth->fetchrow_hashref() ) { warn dump( $row ) if $debug; - push @{ $page->{post}->{ $row->{pozicija} } }, { $row->{name} => $row->{module_args} }; + push @{ $page->{post}->{ $row->{pozicija} } }, { $row->{name} => module_args( $row ) }; } return $page; } +=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 ) if $debug; + + my $code = '$navigation'; + map { $code .= '->[' . ( $_ - 1 ) . ']->{children}' } @pos; + $code =~ s/->{children}$//; + warn "## category code: $code\n" if $debug; + 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 ) if $debug; + + my $ms_code = $code . '->{children}->[ ' . $p . '] = $node'; + warn "## multistatic code: $ms_code\n" if $debug; + eval $ms_code; + if ( $@ ) { + warn "SKIPPED MULTISTATIC: $@ ",dump( $ms ); + next; + } + } + } + + } + + return $navigation; + +} + 1;