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

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

  ViewVC Help
Powered by ViewVC 1.1.26