--- lib/Strix.pm 2008/06/15 23:19:02 167 +++ lib/Strix.pm 2008/06/16 14:43:07 173 @@ -3,10 +3,14 @@ use strict; use warnings; -use base qw/Jifty::Object/; +use base qw(Jifty::Object Class::Accessor::Fast); +__PACKAGE__->mk_accessors( qw(site uid) ); use DBI; use Data::Dump qw/dump/; +use Carp qw/confess/; + +our $debug = 0; =head1 NAME @@ -14,16 +18,26 @@ =head1 METHODS +=head2 new + + my $strix = Strix->new({ site => 'os-test0604-zg' }); + =head2 dbh - my $dbh = Strix->dbh( $site_name ); + my $dbh = Strix->dbh( $site_name ); + + my $dbh = $strix->dbh; =cut +our $site_dbh; + sub dbh { my $self = shift; - my $site = shift or die "no site?"; + my $site = shift || $self->site || confess "no site"; + + return $site_dbh->{$site} if $site_dbh->{$site}; my $config = Jifty->config->app('strix') or die "need strix config"; my $database = $config->{database} or die "no strix.database in config"; @@ -39,7 +53,246 @@ my $dbh = DBI->connect( $dsn, $database->{user}, $database->{passwd} ) or die $DBI::errstr; + $site_dbh->{$site} = $dbh; + + warn "## site_dbh = ",dump( $site_dbh ) if $debug; + return $dbh; } +=head2 category + + my $category = Strix->category( $url ); + +=cut + +sub category { + my $self = shift; + + my $url = shift || confess "no url"; + + # sysinc/profiles.php + my $sth = $self->dbh->prepare(qq{ + 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 + }); + $sth->execute($url); + + my $category = $sth->fetchrow_hashref() or die "can't fetch category $url"; + return $category; +} + +=head2 layout + + my $layout = $strix->layout( $url ); + +=cut + +sub layout { + my $self = shift; + + my $url = shift || confess "no url"; + + my $dbh = $self->dbh; + my $category = $self->category( $url ); + + my $sth = $dbh->prepare(qq{ + SELECT template.tfilename, template.tflags FROM template WHERE id = ? + }); + $sth->execute( $category->{template_id} ); + + my $template = $sth->fetchrow_hashref() or die "can't fetch template"; + + warn "template = ",dump( $template ) if $debug; + + my $page; + warn "### free layout...\n" if $debug; + + # index.php + $sth = $dbh->prepare(qq{ + SELECT layout_id, module_id, pozicija, module_args, name, fname, notitle, class_name + FROM pre_layout, modules + WHERE id=module_id AND ? = template_id AND redoslijed >= 0 + ORDER BY redoslijed DESC + }); + $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} => module_args( $row ) }; + } + + warn "### pre layout...\n" if $debug; + + $sth = $dbh->prepare(qq{ + SELECT + 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, + m.name, m.fname, m.hidden, m.nocache, m.pos, m.class_name, + acl_module.acl_register_id + FROM layout as l, modules as m LEFT JOIN acl_module ON (acl_module.modules_id = m.id) + WHERE l.user_id=? + AND l.kategorija_id=? + AND m.id=l.module_id + ORDER BY pozicija,redoslijed DESC + }); + $sth->execute( 1, $category->{id} ); + + while (my $row = $sth->fetchrow_hashref() ) { + warn dump( $row ) if $debug; + push @{ $page->{pre}->{ $row->{pos} } }, { $row->{name} => module_args( $row ) }; + } + + warn "### post layout...\n" if $debug; + + $sth = $dbh->prepare(qq{ + SELECT layout_id, module_id, pozicija, module_args, name, notitle + FROM pre_layout, modules + WHERE id=module_id AND ? = template_id AND redoslijed < 0 + ORDER BY redoslijed DESC + }); + $sth->execute( $category->{template_id} ); + + while (my $row = $sth->fetchrow_hashref() ) { + warn dump( $row ) if $debug; + 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;