/[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 169 by dpavlin, Mon Jun 16 12:44: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 152  sub layout { Line 152  sub layout {
152    
153  }  }
154    
155    =head2 sitemap
156    
157            my $sitemap = $strix->sitemap( $site_id, $uid );
158    
159    =cut
160    
161    sub sitemap {
162            my $self = shift;
163    
164            my ( $site_id, $uid ) = @_;
165            my $sitemap;
166    
167            my $query = "SELECT *, ( length(ordstr)/3 ) - 1 AS depth FROM site WHERE id = ?";
168            $query = "SELECT *, coalesce(( length(ordstr)/3 ) - 1,0) AS depth FROM site ORDER BY ordstr" unless $site_id;
169    
170            my $sth = $self->dbh->prepare( $query );
171            if ( $site_id ) {
172                    $sth->execute( $site_id );
173            } else {
174                    $sth->execute;
175            }
176    
177            while (my $row = $sth->fetchrow_hashref() ) {
178                    warn dump( $row ) if $debug;
179    
180                    $sitemap->{ $row->{naziv} } = $self->site_navigation( $site_id, $uid );
181            }
182    
183            return $sitemap;
184    }
185    
186    =head2 site_navigation
187    
188      my $navigation = $strix->site_navigation( $site_id, $uid );
189    
190    =cut
191    
192    sub site_navigation {
193            my $self = shift;
194    
195            my ( $site_id, $uid ) = @_;
196    
197            $uid ||= 1; # anonymous
198    #       $uid ||= 2; # admin
199    
200            my $sth = $self->dbh->prepare(
201            "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");
202            $sth->execute( $site_id, $uid );
203    
204            Jifty->log->debug("site $site_id has ", $sth->rows, " categories for uid $uid");
205    
206            my $navigation = [];
207    
208            my @pos = ( 0 );
209    
210            while (my $kat = $sth->fetchrow_hashref() ) {
211                    warn "# kat = ",dump( $kat ) if $debug;
212                    die "no depth" unless $kat->{depth};
213    
214                    my $node = { type => 'category' };
215                    foreach my $c ( qw/naziv url/ ) {
216                            $node->{$c} = $kat->{$c};
217                    }
218    
219                    my $depth = $kat->{depth};
220                    @pos = splice( @pos, 0, $depth );
221                    $pos[ $depth - 1 ]++;
222    
223                    warn "## category depth = $depth pos = ",dump( @pos );
224    
225                    my $code = '$navigation';
226                    map { $code .= '->[' . ( $_ - 1 ) . ']->{children}' } @pos;
227                    $code =~ s/->{children}$//;
228                    warn "## category code: $code\n";
229                    eval $code . '= $node';
230                    if ( $@ ) {
231                            warn "SKIPPED CATEGORY: $@ ",dump( $kat );
232                            next;
233                    }
234    
235                    my $sth_ms = $self->dbh->prepare(
236                    "SELECT
237                            multistatic.id, multistatic.title, multistatic.kname,
238                            multistatic_navigation.kategorija_id, multistatic_navigation.prikaz,
239                            (LENGTH(multistatic_navigation.prikaz)/3) AS depth
240                    FROM multistatic, multistatic_navigation
241                    WHERE multistatic.id = multistatic_navigation.multistatic_id
242                            AND multistatic_navigation.prikaz != ''
243                            AND multistatic_navigation.kategorija_id = ?
244                            AND multistatic.title != ''
245                    ORDER BY multistatic_navigation.prikaz");
246                    $sth_ms->execute( $kat->{id} );
247    
248                    if ( my $rows = $sth_ms->rows ) {
249                            Jifty->log->debug("$site_id has $rows multistatic pages");
250    
251                            while (my $ms = $sth_ms->fetchrow_hashref() ) {
252                                    warn "# ms = ",dump( $ms ) if $debug;
253    
254                                    my $node = {
255                                            naziv => $ms->{title},
256                                            url => $kat->{url} . '?ms_nav=' . $ms->{prikaz},
257                                            type => 'multistatic',
258                                    };
259                                    
260                                    my $ms_depth = $ms->{depth} + $depth;
261                                    my $p = $pos[ $ms_depth - 1 ]++;
262                                    warn "## multistatic depth = $ms_depth pos = ",dump( @pos );
263    
264                                    my $ms_code = $code . '->{children}->[ ' . $p . ']  = $node';
265                                    warn "## multistatic code: $ms_code\n";
266                                    eval $ms_code;
267                                    if ( $@ ) {
268                                            warn "SKIPPED MULTISTATIC: $@ ",dump( $ms );
269                                            next;
270                                    }
271                            }
272                    }
273    
274            }
275    
276            return $navigation;
277    
278    }
279    
280  1;  1;

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

  ViewVC Help
Powered by ViewVC 1.1.26