1 package Gruta
::Template
::Artemus
;
7 use base
'Gruta::Template::BASE';
16 my $a = bless( {}, $class );
18 $a->{_artemus
} = undef;
19 $a->{path
} = $args{path
};
28 if (not $self->{_artemus
}) {
29 my $data = $self->data();
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 {
55 $ret = $data->topic($topic)->get($p) || '';
62 foreach my $p (Gruta
::Data
::Story
->new->afields()) {
63 $f{'story_' . $p} = sub {
69 $ret = $data->story($topic_id, $id)->get($p);
76 $f{story_tags
} = sub {
82 my $story = $data->story($topic_id, $id);
84 $ret = join(', ', $story->tags());
90 $f{story_body
} = sub {
91 my $story = $data->story($_[0], $_[1]);
92 my $ret = $story->get('body');
94 if (not $data->auth()) {
101 $f{story_date
} = sub {
103 my $topic_id = shift;
108 $ret = $data->story($topic_id, $id)->date($format);
114 foreach my $p (Gruta
::Data
::User
->new->afields()) {
115 $f{'user_' . $p} = sub {
120 $ret = $data->user($id)->get($p);
132 $t = $data->template->template($t);
133 $ret = $self->{_artemus
}->armor($t);
139 $f{save_template
} = sub {
140 my $template = 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;
154 return join($sep, map {
155 my $t = $data->topic($_);
156 sprintf('{-%s|%s|%s}',
157 $template, $t->get('name'),
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;
186 return join($_[1], map { "{-$_[0]|$_}" }
187 $data->story($topic_id, $id)->tags());
190 $f{story_loop_by_date
} = sub {
194 my $template = shift;
200 return join($sep, map { "{-$template|$topic|$_}" }
201 $data->stories_by_date(
212 $f{is_logged_in
} = sub {
213 return $data->auth() ?
1 : 0;
217 if ($data->auth() and $data->auth->get('is_admin')) {
223 $f{is_topic_editor
} = sub {
226 if (not $auth = $data->auth()) {
230 if ($auth->get('is_admin')) {
236 if (not $topic = $data->topic($_[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/;
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');
262 return $error_msg || 'Login incorrect.';
267 $data->cgi->redirect('?t=INDEX');
273 my $redir = shift || 'ADMIN';
276 $data->cgi->redirect('?t=' . $redir);
287 $ret = $data->auth->get('username');
293 $f{search_stories
} = sub {
294 my $topic_id = shift;
297 return "search_stories: FIXME";
300 $f{story_loop_top_ten
} = sub {
302 my $internal = shift; # ignored
303 my $template = 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()) {
321 $f{redir_if_archived
} = sub {
322 my $template = shift;
323 my $topic_id = shift;
326 if ($topic_id =~ /-arch$/) {
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',
336 $story->get('topic_id'),
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';
354 if (not $topic = $data->topic($topic_id)) {
355 $topic = Gruta
::Data
::Topic
->new (
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);
365 if ($topic->source()) {
366 $topic = $topic->save();
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';
381 if (not $story = $data->story($topic_id, $id)) {
382 $story = Gruta
::Data
::Story
->new (
383 topic_id
=> $topic_id,
389 $content = $self->{_artemus
}->unarmor($content);
391 $story->set('content', $content);
393 # pick date and drop time
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();
414 $story = $data->insert_story($story);
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';
433 if (not $user = $data->user($id)) {
434 $user = Gruta
::Data
::User
->new (
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);
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();
472 $user = $data->insert_user($user);
475 return $user ?
'OK' : 'Error 2';
480 $data->cgi->upload($_[0], $_[1]);
484 $f{delete_story
} = sub {
485 my $topic_id = shift || return 'Error 1';
488 $data->story($topic_id, $id)->delete();
490 # drop all cached stories
491 $data->flush_story_cache();
496 $f{search_stories_by_tag
} = sub {
498 my $template = shift;
501 return join($sep, map { "{-$template|$_->[0]|$_->[1]}" }
502 $data->search_stories_by_tag($tag)
507 $self->{unresolved
} = [];
509 $self->{_artemus
} = Artemus
->new(
510 'include-path' => $self->{path
},
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});
528 return $self->{_artemus
};
536 if (defined($data)) {
537 $self->{data
} = $data;
538 $self->{_artemus
} = undef;
541 return $self->{data
};
549 $self->{cgi_vars
} = shift;
550 $self->{_artemus
} = undef;
553 return $self->{cgi_vars
};
557 sub process
{ $_[0]->_artemus->process('{-' . $_[1] . '}'); }