/[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 167 by dpavlin, Sun Jun 15 23:19:02 2008 UTC revision 174 by dpavlin, Mon Jun 16 16:48:16 2008 UTC
# Line 3  package Strix; Line 3  package Strix;
3  use strict;  use strict;
4  use warnings;  use warnings;
5    
6  use base qw/Jifty::Object/;  use base qw(Jifty::Object Class::Accessor::Fast);
7    __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/;
12    
13    our $debug = 0;
14    
15  =head1 NAME  =head1 NAME
16    
# Line 14  Strix Line 18  Strix
18    
19  =head1 METHODS  =head1 METHODS
20    
21    =head2 new
22    
23      my $strix = Strix->new({ instance => 'os-test0604-zg' });
24    
25  =head2 dbh  =head2 dbh
26    
27          my $dbh = Strix->dbh( $site_name );    my $dbh = Strix->dbh( $strix_instance );
28    
29      my $dbh = $strix->dbh;
30    
31  =cut  =cut
32    
33    our $instance_dbh;
34    
35  sub dbh {  sub dbh {
36          my $self = shift;          my $self = shift;
37    
38          my $site = shift or die "no site?";          my $instance = shift || $self->instance || confess "no instance";
39    
40            return $instance_dbh->{$instance} if $instance_dbh->{$instance};
41    
42          my $config = Jifty->config->app('strix') or die "need strix config";          my $config = Jifty->config->app('strix') or die "need strix config";
43          my $database = $config->{database} or die "no strix.database in config";          my $database = $config->{database} or die "no strix.database in config";
# Line 31  sub dbh { Line 45  sub dbh {
45          Jifty->log->debug("using config ", dump( $database ));          Jifty->log->debug("using config ", dump( $database ));
46    
47          my $dsn =          my $dsn =
48                  'DBI:Pg:dbname=' . $site .                  'DBI:Pg:dbname=' . $instance .
49                  ';host=' . $database->{host} .                  ';host=' . $database->{host} .
50                  ';port=' . $database->{port};                  ';port=' . $database->{port};
51    
52          Jifty->log->info("Connect to site $site with dsn $dsn");          Jifty->log->info("Connect to instance $instance with dsn $dsn");
53    
54          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;
55    
56            $instance_dbh->{$instance} = $dbh;
57    
58            warn "## instance_dbh = ",dump( $instance_dbh ) if $debug;
59    
60          return $dbh;          return $dbh;
61  }  }
62    
63    =head2 category
64    
65            my $category = Strix->category( $url );
66    
67    =cut
68    
69    sub category {
70            my $self = shift;
71    
72            my $url = shift || confess "no url";
73    
74            # sysinc/profiles.php
75            my $sth = $self->dbh->prepare(qq{
76            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
77            });
78            $sth->execute($url);
79    
80            my $category = $sth->fetchrow_hashref() or die "can't fetch category $url";
81            return $category;
82    }
83    
84    =head2 layout
85    
86            my $layout = $strix->layout( $url );
87    
88    =cut
89    
90    sub layout {
91            my $self = shift;
92    
93            my $url = shift || confess "no url";
94    
95            my $dbh = $self->dbh;
96            my $category = $self->category( $url );
97    
98            my $sth = $dbh->prepare(qq{
99            SELECT template.tfilename, template.tflags FROM template WHERE id = ?
100            });
101            $sth->execute( $category->{template_id} );
102    
103            my $template = $sth->fetchrow_hashref() or die "can't fetch template";
104    
105            warn "template = ",dump( $template ) if $debug;
106    
107            my $page;
108            warn "### free layout...\n" if $debug;
109    
110            # index.php
111            $sth = $dbh->prepare(qq{
112            SELECT layout_id, module_id, pozicija, module_args, name, fname, notitle, class_name
113            FROM pre_layout, modules
114            WHERE id=module_id AND ? = template_id AND redoslijed >= 0
115            ORDER BY redoslijed DESC
116            });
117            $sth->execute( $category->{template_id} );
118    
119            sub module_args {
120                    my $row = shift;
121                    return undef unless $row->{module_args};
122                    my $args;
123                    foreach my $a ( split(/\&/, $row->{module_args} ) ) {
124                            $args->{$1} = $2 if $a =~ m/^(.+)=(.+)$/;
125                    }
126                    return $args;
127            }
128    
129            while (my $row = $sth->fetchrow_hashref() ) {
130                    warn dump( $row ) if $debug;
131                    push @{ $page->{free} }, { $row->{name} => module_args( $row ) };
132            }
133    
134            warn "### pre layout...\n" if $debug;
135    
136            $sth = $dbh->prepare(qq{
137            SELECT
138                    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,
139                    m.name, m.fname, m.hidden, m.nocache, m.pos, m.class_name,
140                    acl_module.acl_register_id
141            FROM layout as l, modules as m LEFT JOIN acl_module ON (acl_module.modules_id = m.id)
142            WHERE l.user_id=?
143            AND l.kategorija_id=?
144            AND m.id=l.module_id
145            ORDER BY pozicija,redoslijed DESC
146            });
147            $sth->execute( 1, $category->{id} );
148    
149            while (my $row = $sth->fetchrow_hashref() ) {
150                    warn dump( $row ) if $debug;
151                    push @{ $page->{pre}->{ $row->{pos} } }, { $row->{name} => module_args( $row ) };
152            }
153    
154            warn "### post layout...\n" if $debug;
155    
156            $sth = $dbh->prepare(qq{
157            SELECT layout_id, module_id, pozicija, module_args, name, notitle
158            FROM pre_layout, modules
159            WHERE id=module_id AND ? = template_id AND redoslijed < 0
160            ORDER BY redoslijed DESC
161            });
162            $sth->execute( $category->{template_id} );
163    
164            while (my $row = $sth->fetchrow_hashref() ) {
165                    warn dump( $row ) if $debug;
166                    push @{ $page->{post}->{ $row->{pozicija} } }, { $row->{name} => module_args( $row ) };
167            }
168    
169            return $page;
170    
171    }
172    
173    =head2 sitemap
174    
175            my $sitemap = $strix->sitemap( $site_id, $uid );
176    
177    =cut
178    
179    sub sitemap {
180            my $self = shift;
181    
182            my ( $site_id, $uid ) = @_;
183            my $sitemap;
184    
185            my $query = "SELECT *, ( length(ordstr)/3 ) - 1 AS depth FROM site WHERE id = ?";
186            $query = "SELECT *, coalesce(( length(ordstr)/3 ) - 1,0) AS depth FROM site ORDER BY ordstr" unless $site_id;
187    
188            my $sth = $self->dbh->prepare( $query );
189            if ( $site_id ) {
190                    $sth->execute( $site_id );
191            } else {
192                    $sth->execute;
193            }
194    
195            while (my $row = $sth->fetchrow_hashref() ) {
196                    warn dump( $row ) if $debug;
197    
198                    $sitemap->{ $row->{naziv} } = $self->site_navigation( $site_id, $uid );
199            }
200    
201            return $sitemap;
202    }
203    
204    =head2 site_navigation
205    
206      my $navigation = $strix->site_navigation( $site_id, $uid );
207    
208    =cut
209    
210    sub site_navigation {
211            my $self = shift;
212    
213            my ( $site_id, $uid ) = @_;
214    
215            $uid ||= 1; # anonymous
216    #       $uid ||= 2; # admin
217    
218            my $sth = $self->dbh->prepare(
219            "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");
220            $sth->execute( $site_id, $uid );
221    
222            Jifty->log->debug("site $site_id has ", $sth->rows, " categories for uid $uid");
223    
224            my $navigation = [];
225    
226            my @pos = ( 0 );
227    
228            while (my $kat = $sth->fetchrow_hashref() ) {
229                    warn "# kat = ",dump( $kat ) if $debug;
230                    die "no depth" unless $kat->{depth};
231    
232                    my $node = { type => 'category' };
233                    foreach my $c ( qw/naziv url/ ) {
234                            $node->{$c} = $kat->{$c};
235                    }
236    
237                    my $depth = $kat->{depth};
238                    @pos = splice( @pos, 0, $depth );
239                    $pos[ $depth - 1 ]++;
240    
241                    warn "## category depth = $depth pos = ",dump( @pos ) if $debug;
242    
243                    my $code = '$navigation';
244                    map { $code .= '->[' . ( $_ - 1 ) . ']->{children}' } @pos;
245                    $code =~ s/->{children}$//;
246                    warn "## category code: $code\n" if $debug;
247                    eval $code . '= $node';
248                    if ( $@ ) {
249                            warn "SKIPPED CATEGORY: $@ ",dump( $kat );
250                            next;
251                    }
252    
253                    my $sth_ms = $self->dbh->prepare(
254                    "SELECT
255                            multistatic.id, multistatic.title, multistatic.kname,
256                            multistatic_navigation.kategorija_id, multistatic_navigation.prikaz,
257                            (LENGTH(multistatic_navigation.prikaz)/3) AS depth
258                    FROM multistatic, multistatic_navigation
259                    WHERE multistatic.id = multistatic_navigation.multistatic_id
260                            AND multistatic_navigation.prikaz != ''
261                            AND multistatic_navigation.kategorija_id = ?
262                            AND multistatic.title != ''
263                    ORDER BY multistatic_navigation.prikaz");
264                    $sth_ms->execute( $kat->{id} );
265    
266                    if ( my $rows = $sth_ms->rows ) {
267                            Jifty->log->debug("$site_id has $rows multistatic pages");
268    
269                            while (my $ms = $sth_ms->fetchrow_hashref() ) {
270                                    warn "# ms = ",dump( $ms ) if $debug;
271    
272                                    my $node = {
273                                            naziv => $ms->{title},
274                                            url => $kat->{url} . '?ms_nav=' . $ms->{prikaz},
275                                            type => 'multistatic',
276                                    };
277                                    
278                                    my $ms_depth = $ms->{depth} + $depth;
279                                    my $p = $pos[ $ms_depth - 1 ]++;
280                                    warn "## multistatic depth = $ms_depth pos = ",dump( @pos ) if $debug;
281    
282                                    my $ms_code = $code . '->{children}->[ ' . $p . ']  = $node';
283                                    warn "## multistatic code: $ms_code\n" if $debug;
284                                    eval $ms_code;
285                                    if ( $@ ) {
286                                            warn "SKIPPED MULTISTATIC: $@ ",dump( $ms );
287                                            next;
288                                    }
289                            }
290                    }
291    
292            }
293    
294            return $navigation;
295    
296    }
297    
298  1;  1;

Legend:
Removed from v.167  
changed lines
  Added in v.174

  ViewVC Help
Powered by ViewVC 1.1.26