Space separators in tags are optional.
[gruta.git] / Gruta / Template / Artemus.pm
blobf0cf6519c89601aecf3dd4388a889b6254dba764
1 package Gruta::Template::Artemus;
3 use strict;
4 use warnings;
5 use Carp;
7 use base 'Gruta::Template::BASE';
9 use Artemus;
10 use Gruta::Data;
12 sub new {
13 my $class = shift;
14 my %args = @_;
16 my $a = bless( {}, $class );
18 $a->{_artemus} = undef;
19 $a->{path} = $args{path};
21 return $a;
25 sub _artemus {
26 my $self = shift;
28 if (not $self->{_artemus}) {
29 my $data = $self->data();
31 my %f = ();
32 my %v = ();
34 $f{l} = sub {
35 my $t = shift;
37 return '?t=' . $t . ';' . join(';', @_);
40 $f{'add'} = sub { $_[0] + $_[1]; };
41 $f{'sub'} = sub { $_[0] - $_[1]; };
42 $f{'gt'} = sub { $_[0] > $_[1]; };
43 $f{'lt'} = sub { $_[0] < $_[1]; };
44 $f{'eq'} = sub { $_[0] eq $_[1] ? 1 : 0; };
46 $f{date} = sub { $data->today(); };
47 $f{random} = sub { $_[rand(scalar(@_))]; };
49 foreach my $p (Gruta::Data::Topic->new->afields()) {
50 $f{'topic_' . $p} = sub {
51 my $topic = shift;
52 my $ret = '';
54 if ($topic ne '[]') {
55 $ret = $data->topic($topic)->get($p) || '';
58 return $ret;
62 foreach my $p (Gruta::Data::Story->new->afields()) {
63 $f{'story_' . $p} = sub {
64 my $topic_id = shift;
65 my $id = shift;
66 my $ret = '';
68 if ($id ne '[]') {
69 $ret = $data->story($topic_id, $id)->get($p);
72 return $ret;
76 $f{story_tags} = sub {
77 my $topic_id = shift;
78 my $id = shift;
79 my $ret = '';
81 if ($id ne '[]') {
82 my $story = $data->story($topic_id, $id);
84 $ret = join(', ', $story->tags());
87 return $ret;
90 $f{story_body} = sub {
91 my $story = $data->story($_[0], $_[1]);
92 my $ret = $story->get('body');
94 if (not $data->auth()) {
95 $story->touch();
98 return $ret;
101 $f{story_date} = sub {
102 my $format = shift;
103 my $topic_id = shift;
104 my $id = shift;
105 my $ret = '';
107 if ($id ne '[]') {
108 $ret = $data->story($topic_id, $id)->date($format);
111 return $ret;
114 foreach my $p (Gruta::Data::User->new->afields()) {
115 $f{'user_' . $p} = sub {
116 my $id = shift;
117 my $ret = '';
119 if ($id ne '[]') {
120 $ret = $data->user($id)->get($p);
123 return $ret;
127 $f{template} = sub {
128 my $t = shift;
129 my $ret = '';
131 if ($t ne '[]') {
132 $t = $data->template->template($t);
133 $ret = $self->{_artemus}->armor($t);
136 return $ret;
139 $f{save_template} = sub {
140 my $template = shift;
141 my $content = shift;
142 my $msg = shift;
144 $content = $self->{_artemus}->unarmor($content);
145 $data->template->save_template($template, $content);
147 return $msg || "Template saved.";
150 $f{loop_topics} = sub {
151 my $template = shift;
152 my $sep = shift;
154 return join($sep, map {
155 my $t = $data->topic($_);
156 sprintf('{-%s|%s|%s}',
157 $template, $t->get('name'),
158 $t->get('id')
160 } $data->topics());
163 $f{loop_users} = sub {
164 return join($_[1], map { "{-$_[0]|$_}" } $data->users());
167 $f{loop_renderers} = sub {
168 return join($_[1], map { "{-$_[0]|$_}" }
169 sort(keys(%{$data->{renderers_h}})));
172 $f{loop_templates} = sub {
173 return join($_[1], map { "{-$_[0]|$_}" }
174 $data->template->templates());
177 $f{loop_upload_dirs} = sub {
178 return join($_[1], map { "{-$_[0]|$_}" }
179 $data->cgi->upload_dirs());
182 $f{loop_story_tags} = sub {
183 my $topic_id = shift;
184 my $id = shift;
186 return join($_[1], map { "{-$_[0]|$_}" }
187 $data->story($topic_id, $id)->tags());
190 $f{story_loop_by_date} = sub {
191 my $topic = shift;
192 my $num = shift;
193 my $offset = shift;
194 my $template = shift;
195 my $sep = shift;
196 my $from = shift;
197 my $to = shift;
198 my $future = shift;
200 return join($sep, map { "{-$template|$topic|$_}" }
201 $data->stories_by_date(
202 $topic,
203 num => $num,
204 offset => $offset,
205 from => $from,
206 to => $to,
207 future => $future
212 $f{is_logged_in} = sub {
213 return $data->auth() ? 1 : 0;
216 $f{is_admin} = sub {
217 if ($data->auth() and $data->auth->get('is_admin')) {
218 return 1;
220 return 0;
223 $f{is_topic_editor} = sub {
224 my $auth;
226 if (not $auth = $data->auth()) {
227 return 0;
230 if ($auth->get('is_admin')) {
231 return 1;
234 my $topic;
236 if (not $topic = $data->topic($_[0])) {
237 return 0;
240 if (my $editors = $topic->get('editors') and
241 my $user_id = $auth->get('id')) {
242 return 1 if $editors =~/\b$user_id\b/;
245 return 0;
248 $f{login} = sub {
249 my $user_id = shift;
250 my $password = shift;
251 my $error_msg = shift;
253 if ($user_id eq '' || $user_id eq 'cgi-userid') {
254 $error_msg = '{-login_box}';
256 elsif (my $sid = $data->login($user_id, $password)) {
257 $data->cgi->cookie("sid=$sid");
258 $data->cgi->redirect('?t=INDEX');
259 $self->{abort} = 1;
262 return $error_msg || 'Login incorrect.';
265 $f{logout} = sub {
266 $data->logout();
267 $data->cgi->redirect('?t=INDEX');
268 $self->{abort} = 1;
271 $f{assert} = sub {
272 my $cond = shift;
273 my $redir = shift || 'ADMIN';
275 if (! $cond) {
276 $data->cgi->redirect('?t=' . $redir);
277 $self->{abort} = 1;
280 return '';
283 $f{username} = sub {
284 my $ret = '';
286 if ($data->auth()) {
287 $ret = $data->auth->get('username');
290 return $ret;
293 $f{search_stories} = sub {
294 my $topic_id = shift;
295 my $query = shift;
297 return "search_stories: FIXME";
300 $f{story_loop_top_ten} = sub {
301 my $num = shift;
302 my $internal = shift; # ignored
303 my $template = shift;
304 my $sep = shift;
306 return join($sep, map { "{-$template|$_->[1]|$_->[2]}" }
307 $data->stories_top_ten($num)
311 $f{is_visible_story} = sub {
312 my $story = $data->story($_[0], $_[1]);
314 if (!$data->auth() && $story->get('date') > $data->today()) {
315 return 0;
318 return 1;
321 $f{redir_if_archived} = sub {
322 my $template = shift;
323 my $topic_id = shift;
324 my $id = shift;
326 if ($topic_id =~ /-arch$/) {
327 return '';
330 my $story = $data->story($topic_id, $id);
332 if ($story->get('topic_id') =~ /-arch$/) {
333 $data->cgi->redirect(
334 sprintf('?t=%s;topic=%s;id=%s',
335 $template,
336 $story->get('topic_id'),
337 $id)
339 $self->{abort} = 1;
342 return '';
345 $f{topic_has_archive} = sub {
346 return $data->topic($_[0] . '-arch') ? 1 : 0;
349 $f{save_topic} = sub {
350 my $topic_id = shift || return 'Error 1';
352 my $topic = undef;
354 if (not $topic = $data->topic($topic_id)) {
355 $topic = Gruta::Data::Topic->new (
356 id => $topic_id );
359 $topic->set('name', shift);
360 $topic->set('editors', shift);
361 $topic->set('internal', shift eq 'on' ? 1 : 0);
362 $topic->set('max_stories', shift);
364 # update or insert
365 if ($topic->source()) {
366 $topic = $topic->save();
368 else {
369 $topic = $data->insert_topic($topic);
372 return $topic ? 'OK' : 'Error 2';
375 $f{save_story} = sub {
376 my $topic_id = shift || return 'Error 1';
377 my $id = shift;
379 my $story = undef;
381 if (not $story = $data->story($topic_id, $id)) {
382 $story = Gruta::Data::Story->new (
383 topic_id => $topic_id,
384 id => $id
388 my $content = shift;
389 $content = $self->{_artemus}->unarmor($content);
391 $story->set('content', $content);
393 # pick date and drop time
394 my $y = shift;
395 my $m = shift;
396 my $d = shift;
397 shift; shift; shift;
398 my $date = $data->today();
400 if ($y && $m && $d) {
401 $date = sprintf("%04d%02d%02d000000", $y, $m, $d);
404 $story->set('date', $date);
405 $story->set('format', shift || 'grutatxt');
407 # drop all cached stories
408 $data->flush_story_cache();
410 if ($story->source()) {
411 $story = $story->save();
413 else {
414 $story = $data->insert_story($story);
417 # store the tags
418 my $tags = shift;
420 if ($tags ne 'cgi-tags') {
421 $story->tags(split(/\s*,\s*/, $tags));
424 return $story ? $story->get('id') : 'Error 2';
427 $f{save_user} = sub {
428 shift; # new (ignored)
429 my $id = shift || return 'Error 1';
431 my $user = undef;
433 if (not $user = $data->user($id)) {
434 $user = Gruta::Data::User->new (
435 id => $id,
436 is_admin => 0,
437 can_upload => 0
441 $user->set('username', shift);
442 $user->set('email', shift);
444 # these params can only be set by an admin
445 if ($data->auth() && $data->auth->get('is_admin')) {
446 $user->set('is_admin', shift);
447 $user->set('can_upload', shift);
449 else {
450 shift;
451 shift;
454 my $pass1 = shift;
455 my $pass2 = shift;
457 if ($pass1 and $pass2) {
458 if ($pass1 ne $pass2) {
459 croak "Passwords are different";
462 my $salt = sprintf('%02d', rand(100));
463 my $pw = crypt($pass1, $salt);
465 $user->set('password', $pw);
468 if ($user->source()) {
469 $user = $user->save();
471 else {
472 $user = $data->insert_user($user);
475 return $user ? 'OK' : 'Error 2';
478 $f{upload} = sub {
480 $data->cgi->upload($_[0], $_[1]);
481 return 'OK';
484 $f{delete_story} = sub {
485 my $topic_id = shift || return 'Error 1';
486 my $id = shift;
488 $data->story($topic_id, $id)->delete();
490 # drop all cached stories
491 $data->flush_story_cache();
493 return 'OK';
496 $f{search_stories_by_tag} = sub {
497 my $tag = shift;
498 my $template = shift;
499 my $sep = shift;
501 return join($sep, map { "{-$template|$_->[0]|$_->[1]}" }
502 $data->search_stories_by_tag($tag)
506 $self->{abort} = 0;
507 $self->{unresolved} = [];
509 $self->{_artemus} = Artemus->new(
510 'include-path' => $self->{path},
511 'funcs' => \%f,
512 'vars' => \%v,
513 'unresolved' => $self->{unresolved},
514 'abort' => \$self->{abort},
517 if ($self->{cgi_vars}) {
518 foreach my $k (keys(%{ $self->{cgi_vars} })) {
519 my $c = $self->{_artemus}->
520 armor($self->{cgi_vars}->{$k});
521 $c =~ s/\r//g;
523 $v{"cgi-${k}"} = $c;
528 return $self->{_artemus};
532 sub data {
533 my $self = shift;
534 my $data = shift;
536 if (defined($data)) {
537 $self->{data} = $data;
538 $self->{_artemus} = undef;
541 return $self->{data};
545 sub cgi_vars {
546 my $self = shift;
548 if (@_) {
549 $self->{cgi_vars} = shift;
550 $self->{_artemus} = undef;
553 return $self->{cgi_vars};
557 sub process { $_[0]->_artemus->process('{-' . $_[1] . '}'); }