]> git.decadent.org.uk Git - memories.git/blob - Memories.pm
Don't want to forget that one.
[memories.git] / Memories.pm
1 package Memories;
2 use strict;
3 our $VERSION = "1.2";
4 use Maypole::Application qw(Upload Authentication::UserSessionCookie -Debug);
5 use HTML::TagCloud;
6 use URI;
7 use Memories::Config;
8 use Memories::DBI;
9 use Memories::Photo;
10 use Memories::Comment;
11 use Memories::Tag;
12 use Memories::SystemTag;
13 use Memories::User;
14 use Memories::Album;
15 use URI::Escape;
16 use Calendar::Simple;
17 use XML::RSS;
18
19 Memories->config->auth->{ user_field } = "name";
20 Memories->config->model("Maypole::Model::CDBI::Plain");
21 Memories->setup([qw/ Memories::Photo Memories::User Memories::Tag
22 Memories::Album Memories::SystemTag/]);
23
24 sub message {
25     my ($self, $message) = @_;
26     push @{$self->{template_args}{messages}}, $message;
27 }
28
29 sub check_credentials {
30     my $r = shift;
31     my ($uid, $user) = $r->SUPER::check_credentials;
32     #if (!$uid) { return (-1, undef) }
33     return ($uid, $user);
34 }
35
36 sub do_rss {
37     my $r = shift;
38     $r->model_class->process($r);
39     my $photos = $r->get_photos;
40     my $rss = XML::RSS->new(version => "2.0");
41     $rss->channel(
42         title => ($r->config->{application_name}. " : ".ucfirst($r->action)." ".ucfirst($r->table)." ".($r->objects||[])->[0]) ,
43         link  => $r->config->{uri_base}."/".$r->path
44     );
45     for my $item (@$photos) { 
46         my $link = $r->config->{uri_base}."photo/view/".$item->id;
47         $rss->add_item( title => $item->title, link => $link,
48             description => 
49     "<a href=\"$link\">
50     <img src=\"". $item->thumb_url."\" alt=\"".$item->title."\"></a>",
51             dc => { subject => join " ", $item->tags },
52             pubDate => $item->uploaded->strftime("%a, %d %b %Y %H:%M:%S %z")
53         )
54     }
55     $r->output($rss->as_string);
56     $r->content_type("application/rss+xml");
57     return
58 }
59
60 sub get_photos {
61     my $r = shift;
62     my $maybe_photos = $r->{objects}||[];
63     return (@$maybe_photos && $maybe_photos->[0]->isa("Memories::Photo")) 
64             ? $maybe_photos :
65             ($r->{template_args}->{photos} || []);
66 }
67
68 sub last_search {
69     my $r = shift;
70     my $photos = $r->get_photos; 
71     $r->{session}{last_search} = join ",", map { $_->id } @$photos 
72         if @$photos > 1;
73 }
74
75 sub additional_data { 
76     my $r = shift;
77     if ($r->params->{view_cal}) { 
78     $r->{template_args}{view_cal} = eval {
79             Time::Piece->strptime($r->{params}{view_cal}, "%Y-%m-%d") }; 
80     }
81     $r->{template_args}{now} = Time::Piece->new;
82     if ($r->session) {
83         (tied %{$r->session})->{lock_manager}->clean('/var/lib/memories/sessionlock', 3600) #remove files older than 1 hour
84     }
85     return $r->do_rss if ($r->params->{format} =~ /rss/)
86 }
87
88 use Maypole::Constants;
89 sub authenticate {
90    my ($self, $r) = @_;
91    return DECLINED if $self->path =~/static|store/; # XXX
92    $r->get_user;
93    return OK; 
94 }
95
96
97 use Cache::SharedMemoryCache;
98 my %cache_options = ( 'namespace' => 'MemoriesStuff',
99                    'default_expires_in' => 600 );
100 my $cache =
101    new Cache::SharedMemoryCache( \%cache_options ) or
102      croak( "Couldn't instantiate SharedMemoryCache" );
103
104 sub zap_cache { $cache->Clear }
105 use Storable qw(freeze); use MIME::Base64;
106 sub do_cached {
107     my ($self, $codeblock,$arg) = @_;
108     my $key = 0+$codeblock; if ($arg) { $key .=":".encode_base64(freeze(\$arg));  }
109     my $c = $cache->get(0+$codeblock); return @$c if $c;
110     my @stuff = $codeblock->($arg);
111     $cache->set(0+$codeblock, [ @stuff ]);
112     return @stuff;
113 }
114
115 sub _recent_uploads { Memories::Photo->search_recent() }
116
117 sub recent_uploads { shift->do_cached(\&_recent_uploads) }
118 sub tagcloud { shift->do_cached(\&_tagcloud) }
119
120 sub _tagcloud {
121     my $cloud = HTML::TagCloud->new();
122     my $base = Memories->config->uri_base."tag/view/";
123     for my $tagging (Memories::Tagging->search_summary) {
124         my $name = $tagging->tag->name;
125         $cloud->add($name,
126             $base.uri_escape($name),
127             $tagging->{count}
128         )
129     }
130     $cloud
131 }
132
133 sub calendar {
134     # shift->do_cached(\&_calendar, shift ) }
135 #sub _calendar {
136     my $self = shift;
137     my $arg = shift;
138     my ($y, $m) = split /-/, ($arg || Time::Piece->new->ymd);
139     my @m = Calendar::Simple::calendar($m, $y);
140     my @month;
141     foreach my $week (@m) {
142         my @weekdays;
143         foreach my $day (@$week) {
144                 my $d = { day => $day };
145                 if ($day) {
146                     my $tag = "date:$y-$m-".sprintf("%02d", $day);
147                     my ($x) = Memories::SystemTag->search(name => $tag);
148                     if ($x) { $d->{tag} = "/system_tag/view/$tag" }
149                 }
150                 push(@weekdays, $d);
151         }
152         push(@month, \@weekdays);
153     }
154     return \@month;
155 }
156
157 # THIS IS A HACK
158
159 use Time::Seconds;
160 sub Time::Piece::next_month {
161     my $tp = shift;
162     my $month = $tp + ONE_MONTH;
163     return if $month > Time::Piece->new;
164     return $month
165 }
166 sub Time::Piece::prev_month {
167     my $tp = shift;
168     my $month = $tp - ONE_MONTH;
169     return $month
170 }
171
172
173 sub tag_select {
174     my ($r, $tags) = @_;
175     my %counter;
176     my @photos = Memories::Photo->sth_to_objects(Memories::Tag->multi_search(@$tags));
177     for (map {$_->tags} @photos) { 
178         $counter{$_->name}++; 
179     } 
180     delete $counter{$_->name} for @$tags;
181     my @super;
182
183     my $cloud = HTML::TagCloud->new();
184     my $base = $r->config->uri_base.$r->path."/";
185     my $tags;
186     for my $name (sort {$a cmp $b} keys %counter) {
187         if ($counter{$name} == @photos) {
188             push @super, $name;
189         } else {
190             $cloud->add($name, $base.uri_escape($name), $counter{$name});
191             $tags++;
192         }
193     }
194     my %res;
195     if (@super) { $res{super} = \@super }
196     if ($tags) { $res{cloud} = $cloud }
197     \%res;
198 }
199 1;