1 # Copyright 2002-2007 Interchange Development Group and others
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version. See the LICENSE file for details.
8 # $Id: banner.coretag,v 1.6 2007-03-30 23:40:49 pajamian Exp $
10 UserTag banner Order category
11 UserTag banner addAttr
12 UserTag banner PosNumber 1
13 UserTag banner Version $Revision: 1.6 $
14 UserTag banner Routine <<EOR
16 my ($place, $opt) = @_;
19 sub initialize_banner_directory {
20 my ($dir, $category, $opt) = @_;
21 mkdir $dir, 0777 if ! -d $dir;
22 my $t = $opt->{table} || 'banner';
27 $append .= ($opt->{c_field} || 'category');
28 $category =~ s/'/''/g;
29 $append .= " = '$category'";
31 my $db = database_exists_ref($t);
33 my $weight_file = "$dir/total_weight";
34 return undef if -f $weight_file;
35 $t = "no banners db $t\n";
36 Vend::Util::writefile( $weight_file, $t, $opt);
40 my $w_field = $opt->{w_field} || 'weight';
41 my $b_field = $opt->{b_field} || 'banner';
42 my $q = "select $w_field, $b_field from $t where $w_field >= 1$append";
43 my $banners = $db->query({
49 my ($weight, $text) = @$_;
51 Vend::Util::writefile(">$dir/$i", $text, $opt);
55 Vend::Util::writefile(">$dir/total_weight", $i, $opt);
59 sub tag_weighted_banner {
60 my ($category, $opt) = @_;
61 my $dir = catfile($Vend::Cfg->{ScratchDir}, 'Banners');
62 mkdir $dir, 0777 if ! -d $dir;
68 my $statfile = $Vend::Cfg->{ConfDir};
69 $statfile .= "/status.$Vend::Cat";
74 elsif(! -f $statfile) {
75 Vend::Util::writefile( $statfile, "banners initialized " . time() . "\n");
79 $start_time = (stat(_))[9];
81 my $weight_file = "$dir/total_weight";
82 initialize_banner_directory($dir, $category, $opt)
83 if ( ! -f $weight_file or (stat(_))[9] < $start_time );
84 my $n = int( rand( readfile($weight_file) ) );
85 return Vend::Util::readfile("$dir/$n");
87 return tag_weighted_banner($place, $opt) if $opt->{weighted};
89 my $table = $opt->{table} || 'banner';
90 my $r_field = $opt->{r_field} || 'rotate';
91 my $b_field = $opt->{b_field} || 'banner';
92 my $sep = $opt->{separator} || ':';
93 my $delim = $opt->{delimiter} || "{or}";
94 $place = 'default' if ! $place;
98 $totrot = tag_data($table, $r_field, $place);
99 if(! length $totrot) {
101 unless ($place =~ /$sep/ or $place eq 'default') {
107 my $current = $::Scratch->{"rotate_$place"}++ || 0;
108 my $data = tag_data($table, $b_field, $place);
109 my(@banners) = split /\Q$delim/, $data;
110 return '' unless @banners;
111 return $banners[$current % scalar(@banners)];
114 return tag_data($table, $b_field, $place);
116 } while $place =~ s/(.*)$sep.*/$1/;