/[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 181 by dpavlin, Mon Jun 16 20:08:28 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(instance uid) );
8    
9  use DBI;  use DBI;
10  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
11  use Carp qw/confess/;  use Carp qw/confess/;
12    use Jifty;
13    
14  our $debug = 0;  our $debug = 0;
15    
# Line 20  Strix Line 21  Strix
21    
22  =head2 new  =head2 new
23    
24    my $strix = Strix->new({ site => 'os-test0604-zg' });    my $strix = Strix->new({ instance => 'os-test0604-zg' });
25    
26  =head2 dbh  =head2 dbh
27    
28    my $dbh = Strix->dbh( $site_name );    my $dbh = Strix->dbh( $strix_instance );
29    
30    my $dbh = $strix->dbh;    my $dbh = $strix->dbh;
31    
32  =cut  =cut
33    
34    our $instance_dbh;
35    
36  sub dbh {  sub dbh {
37          my $self = shift;          my $self = shift;
38    
39          my $site = shift || $self->site || confess "no site";          my $instance = shift || ref($self) && $self->instance || confess "no instance";
40    
41            return $instance_dbh->{$instance} if $instance_dbh->{$instance};
42    
43          my $config = Jifty->config->app('strix') or die "need strix config";          my $config = Jifty->config->app('strix') or die "need strix config";
44          my $database = $config->{database} or die "no strix.database in config";          my $database = $config->{database} or die "no strix.database in config";
# Line 41  sub dbh { Line 46  sub dbh {
46          Jifty->log->debug("using config ", dump( $database ));          Jifty->log->debug("using config ", dump( $database ));
47    
48          my $dsn =          my $dsn =
49                  'DBI:Pg:dbname=' . $site .                  'DBI:Pg:dbname=' . $instance .
50                  ';host=' . $database->{host} .                  ';host=' . $database->{host} .
51                  ';port=' . $database->{port};                  ';port=' . $database->{port};
52    
53          Jifty->log->info("Connect to site $site with dsn $dsn");          Jifty->log->info("Connect to instance $instance with dsn $dsn");
54    
55          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;
56    
57            $instance_dbh->{$instance} = $dbh;
58    
59            warn "## instance_dbh = ",dump( $instance_dbh ) if $debug;
60    
61          return $dbh;          return $dbh;
62  }  }
63    
# Line 108  sub layout { Line 117  sub layout {
117          });          });
118          $sth->execute( $category->{template_id} );          $sth->execute( $category->{template_id} );
119    
120            sub module_args {
121                    my $row = shift;
122                    return undef unless $row->{module_args};
123                    my $args;
124                    foreach my $a ( split(/\&/, $row->{module_args} ) ) {
125                            $args->{$1} = $2 if $a =~ m/^(.+)=(.+)$/;
126                    }
127                    return $args;
128            }
129    
130          while (my $row = $sth->fetchrow_hashref() ) {          while (my $row = $sth->fetchrow_hashref() ) {
131                  warn dump( $row ) if $debug;                  warn dump( $row ) if $debug;
132                  push @{ $page->{free} }, { $row->{name} => $row->{module_args} };                  push @{ $page->{free} }, { $row->{name} => module_args( $row ) };
133          }          }
134    
135          warn "### pre layout...\n" if $debug;          warn "### pre layout...\n" if $debug;
# Line 130  sub layout { Line 149  sub layout {
149    
150          while (my $row = $sth->fetchrow_hashref() ) {          while (my $row = $sth->fetchrow_hashref() ) {
151                  warn dump( $row ) if $debug;                  warn dump( $row ) if $debug;
152                  push @{ $page->{pre}->{ $row->{pos} } }, { $row->{name} => $row->{module_args} };                  push @{ $page->{pre}->{ $row->{pos} } }, { $row->{name} => module_args( $row ) };
153          }          }
154    
155          warn "### post layout...\n" if $debug;          warn "### post layout...\n" if $debug;
# Line 145  sub layout { Line 164  sub layout {
164    
165          while (my $row = $sth->fetchrow_hashref() ) {          while (my $row = $sth->fetchrow_hashref() ) {
166                  warn dump( $row ) if $debug;                  warn dump( $row ) if $debug;
167                  push @{ $page->{post}->{ $row->{pozicija} } }, { $row->{name} => $row->{module_args} };                  push @{ $page->{post}->{ $row->{pozicija} } }, { $row->{name} => module_args( $row ) };
168          }          }
169    
170          return $page;          return $page;
171    
172  }  }
173    
174    =head2 sitemap
175    
176            my $sitemap = $strix->sitemap( $site_id, $uid );
177    
178    =cut
179    
180    sub sitemap {
181            my $self = shift;
182    
183            my ( $site_id, $uid ) = @_;
184            my $sitemap;
185    
186            my $query = "SELECT *, ( length(ordstr)/3 ) - 1 AS depth FROM site WHERE id = ?";
187            $query = "SELECT *, coalesce(( length(ordstr)/3 ) - 1,0) AS depth FROM site ORDER BY ordstr" unless $site_id;
188    
189            my $sth = $self->dbh->prepare( $query );
190            if ( $site_id ) {
191                    $sth->execute( $site_id );
192            } else {
193                    $sth->execute;
194            }
195    
196            while (my $row = $sth->fetchrow_hashref() ) {
197                    warn dump( $row ) if $debug;
198    
199                    $sitemap->{ $row->{naziv} } = $self->site_navigation( $site_id, $uid );
200            }
201    
202            return $sitemap;
203    }
204    
205    =head2 site_navigation
206    
207      my $navigation = $strix->site_navigation( $site_id, $uid );
208    
209    =cut
210    
211    sub site_navigation {
212            my $self = shift;
213    
214            my ( $site_id, $uid ) = @_;
215    
216            $uid ||= 1; # anonymous
217    #       $uid ||= 2; # admin
218    
219            my $sth = $self->dbh->prepare(
220            "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");
221            $sth->execute( $site_id, $uid );
222    
223            Jifty->log->debug("site $site_id has ", $sth->rows, " categories for uid $uid");
224    
225            my $navigation = [];
226    
227            my @pos = ( 0 );
228    
229            while (my $kat = $sth->fetchrow_hashref() ) {
230                    warn "# kat = ",dump( $kat ) if $debug;
231                    die "no depth" unless $kat->{depth};
232    
233                    my $node = { type => 'category' };
234                    foreach my $c ( qw/naziv url/ ) {
235                            $node->{$c} = $kat->{$c};
236                    }
237    
238                    my $depth = $kat->{depth};
239                    @pos = splice( @pos, 0, $depth );
240                    $pos[ $depth - 1 ]++;
241    
242                    warn "## category depth = $depth pos = ",dump( @pos ) if $debug;
243    
244                    my $code = '$navigation';
245                    map { $code .= '->[' . ( $_ - 1 ) . ']->{children}' } @pos;
246                    $code =~ s/->{children}$//;
247                    warn "## category code: $code\n" if $debug;
248                    eval $code . '= $node';
249                    if ( $@ ) {
250                            warn "SKIPPED CATEGORY: $@ ",dump( $kat );
251                            next;
252                    }
253    
254                    my $sth_ms = $self->dbh->prepare(
255                    "SELECT
256                            multistatic.id, multistatic.title, multistatic.kname,
257                            multistatic_navigation.kategorija_id, multistatic_navigation.prikaz,
258                            (LENGTH(multistatic_navigation.prikaz)/3) AS depth
259                    FROM multistatic, multistatic_navigation
260                    WHERE multistatic.id = multistatic_navigation.multistatic_id
261                            AND multistatic_navigation.prikaz != ''
262                            AND multistatic_navigation.kategorija_id = ?
263                            AND multistatic.title != ''
264                    ORDER BY multistatic_navigation.prikaz");
265                    $sth_ms->execute( $kat->{id} );
266    
267                    if ( my $rows = $sth_ms->rows ) {
268                            Jifty->log->debug("$site_id has $rows multistatic pages");
269    
270                            while (my $ms = $sth_ms->fetchrow_hashref() ) {
271                                    warn "# ms = ",dump( $ms ) if $debug;
272    
273                                    my $node = {
274                                            naziv => $ms->{title},
275                                            url => $kat->{url} . '?ms_nav=' . $ms->{prikaz},
276                                            type => 'multistatic',
277                                    };
278                                    
279                                    my $ms_depth = $ms->{depth} + $depth;
280                                    my $p = $pos[ $ms_depth - 1 ]++;
281                                    warn "## multistatic depth = $ms_depth pos = ",dump( @pos ) if $debug;
282    
283                                    my $ms_code = $code . '->{children}->[ ' . $p . ']  = $node';
284                                    warn "## multistatic code: $ms_code\n" if $debug;
285                                    eval $ms_code;
286                                    if ( $@ ) {
287                                            warn "SKIPPED MULTISTATIC: $@ ",dump( $ms );
288                                            next;
289                                    }
290                            }
291                    }
292    
293            }
294    
295            return $navigation;
296    
297    }
298    
299  1;  1;

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

  ViewVC Help
Powered by ViewVC 1.1.26