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 { Gruta
::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);
127 $f{user_xdate
} = sub {
133 $ret = $data->user($id)->xdate($format);
144 $t = $data->template->template($t);
145 $ret = $self->{_artemus
}->armor($t);
151 $f{save_template
} = sub {
152 my $template = shift;
156 $content = $self->{_artemus
}->unarmor($content);
157 $data->template->save_template($template, $content);
159 return $msg || "Template saved.";
162 $f{loop_topics
} = sub {
163 my $template = shift;
166 return join($sep, map {
167 my $t = $data->topic($_);
168 sprintf('{-%s|%s|%s}',
169 $template, $t->get('name'),
175 $f{loop_users
} = sub {
176 return join($_[1], map { "{-$_[0]|$_}" } $data->users());
179 $f{loop_renderers
} = sub {
180 return join($_[1], map { "{-$_[0]|$_}" }
181 sort(keys(%{$data->{renderers_h
}})));
184 $f{loop_templates
} = sub {
185 return join($_[1], map { "{-$_[0]|$_}" }
186 $data->template->templates());
189 $f{loop_upload_dirs
} = sub {
190 return join($_[1], map { "{-$_[0]|$_}" }
191 $data->cgi->upload_dirs());
194 $f{loop_story_tags
} = sub {
195 my $topic_id = shift;
198 return join($_[1], map { "{-$_[0]|$_}" }
199 $data->story($topic_id, $id)->tags());
202 $f{story_loop_by_date
} = sub {
206 my $template = shift;
212 return join($sep, map { "{-$template|$topic|$_}" }
213 $data->stories_by_date(
224 $f{is_logged_in
} = sub {
225 return $data->auth() ?
1 : 0;
229 if ($data->auth() and $data->auth->get('is_admin')) {
235 $f{is_topic_editor
} = sub {
238 if (not $auth = $data->auth()) {
242 if ($auth->get('is_admin')) {
248 if (not $topic = $data->topic($_[0])) {
252 if (my $editors = $topic->get('editors') and
253 my $user_id = $auth->get('id')) {
254 return 1 if $editors =~/\b$user_id\b/;
262 my $password = shift;
263 my $error_msg = shift;
265 if ($user_id eq '' || $user_id eq 'cgi-userid') {
266 $error_msg = '{-login_box}';
268 elsif (my $sid = $data->login($user_id, $password)) {
269 $data->cgi->cookie("sid=$sid");
270 $data->cgi->redirect('?t=INDEX');
274 return $error_msg || 'Login incorrect.';
279 $data->cgi->redirect('?t=INDEX');
285 my $redir = shift || 'ADMIN';
288 $data->cgi->redirect('?t=' . $redir);
299 $ret = $data->auth->get('username');
305 $f{search_stories
} = sub {
306 my $topic_id = shift;
309 my $template = shift || '_story_link_as_item_with_edit';
310 my $sep = shift || '';
313 my @l = $data->search_stories($topic_id, $query, $future);
316 $ret = "<p><b>{-topic_name|$topic_id}</b><br>\n";
318 $ret .= join($sep, map { "{-$template|$topic_id|$_}" } @l);
320 $self->{search_count
} += scalar(@l);
326 $f{story_loop_top_ten
} = sub {
328 my $internal = shift; # ignored
329 my $template = shift;
332 return join($sep, map { "{-$template|$_->[1]|$_->[2]}" }
333 $data->stories_top_ten($num)
337 $f{is_visible_story
} = sub {
338 my $story = $data->story($_[0], $_[1]);
340 if (!$data->auth() && $story->get('date') > Gruta
::Data
::today
()) {
347 $f{redir_if_archived
} = sub {
348 my $template = shift;
349 my $topic_id = shift;
352 if ($topic_id =~ /-arch$/) {
356 my $story = $data->story($topic_id, $id);
358 if ($story->get('topic_id') =~ /-arch$/) {
359 $data->cgi->redirect(
360 sprintf('?t=%s;topic=%s;id=%s',
362 $story->get('topic_id'),
371 $f{topic_has_archive
} = sub {
372 return $data->topic($_[0] . '-arch') ?
1 : 0;
375 $f{save_topic
} = sub {
376 my $topic_id = shift || return 'Error 1';
380 if (not $topic = $data->topic($topic_id)) {
381 $topic = Gruta
::Data
::Topic
->new (
385 $topic->set('name', shift);
386 $topic->set('editors', shift);
387 $topic->set('internal', shift eq 'on' ?
1 : 0);
388 $topic->set('max_stories', shift);
391 if ($topic->source()) {
392 $topic = $topic->save();
395 $topic = $data->insert_topic($topic);
398 return $topic ?
'OK' : 'Error 2';
401 $f{save_story
} = sub {
402 my $topic_id = shift || return 'Error 1';
407 if (not $story = $data->story($topic_id, $id)) {
408 $story = Gruta
::Data
::Story
->new (
409 topic_id
=> $topic_id,
415 $content = $self->{_artemus
}->unarmor($content);
417 $story->set('content', $content);
419 # pick date and drop time
424 my $date = Gruta
::Data
::today
();
426 if ($y && $m && $d) {
427 $date = sprintf("%04d%02d%02d000000", $y, $m, $d);
430 $story->set('date', $date);
431 $story->set('format', shift || 'grutatxt');
433 # drop all cached stories
434 $data->flush_story_cache();
436 if ($story->source()) {
437 $story = $story->save();
440 $story = $data->insert_story($story);
446 if ($tags ne 'cgi-tags') {
447 $story->tags(split(/\s*,\s*/, $tags));
450 return $story ?
$story->get('id') : 'Error 2';
453 $f{save_user
} = sub {
454 shift; # new (ignored)
455 my $id = shift || return 'Error 1';
456 my $username = shift;
458 my $is_admin = shift;
459 my $can_upload = shift;
468 if (not $user = $data->user($id)) {
469 $user = Gruta
::Data
::User
->new (
477 $user->set('username', $username);
478 $user->set('email', $email);
480 # these params can only be set by an admin
481 if ($data->auth() && $data->auth->get('is_admin')) {
483 $user->set('is_admin', $is_admin eq 'on' ?
1 : 0);
484 $user->set('can_upload', $can_upload eq 'on' ?
1 : 0);
486 if ($xy and $xm and $xd) {
488 sprintf('%04d%02d%02d000000',
492 $user->set('xdate', '');
496 if ($pass1 and $pass2) {
497 if ($pass1 ne $pass2) {
498 croak
"Passwords are different";
501 my $salt = sprintf('%02d', rand(100));
502 my $pw = crypt($pass1, $salt);
504 $user->set('password', $pw);
507 if ($user->source()) {
508 $user = $user->save();
511 $user = $data->insert_user($user);
514 return $user ?
'OK' : 'Error 2';
519 $data->cgi->upload($_[0], $_[1]);
523 $f{delete_story
} = sub {
524 my $topic_id = shift || return 'Error 1';
527 $data->story($topic_id, $id)->delete();
529 # drop all cached stories
530 $data->flush_story_cache();
535 $f{search_stories_by_tag
} = sub {
537 my $template = shift;
541 my @ret = $data->search_stories_by_tag($tag, $future);
542 $self->{search_count
} = scalar(@ret);
544 return join($sep, map { "{-$template|$_->[0]|$_->[1]}" } @ret);
547 $f{search_count
} = sub { $self->{search_count
}; };
550 $self->{unresolved
} = [];
551 $self->{search_count
} = 0;
553 $self->{_artemus
} = Artemus
->new(
554 'include-path' => $self->{path
},
557 'unresolved' => $self->{unresolved
},
558 'abort' => \
$self->{abort
},
561 if ($self->{cgi_vars
}) {
562 foreach my $k (keys(%{ $self->{cgi_vars
} })) {
563 my $c = $self->{_artemus
}->
564 armor
($self->{cgi_vars
}->{$k});
572 return $self->{_artemus
};
580 if (defined($data)) {
581 $self->{data
} = $data;
582 $self->{_artemus
} = undef;
585 return $self->{data
};
593 $self->{cgi_vars
} = shift;
594 $self->{_artemus
} = undef;
597 return $self->{cgi_vars
};
601 sub process
{ $_[0]->_artemus->process('{-' . $_[1] . '}'); }