0.15_18 commit
[bse.git] / site / cgi-bin / modules / BSE / Edit / Article.pm
CommitLineData
ca9aa2bf
TC
1package BSE::Edit::Article;
2use strict;
ca9aa2bf 3use base qw(BSE::Edit::Base);
b553afa2 4use BSE::Util::Tags qw(tag_error_img);
41f10371 5use BSE::Util::SQL qw(now_sqldate now_sqldatetime);
b553afa2 6use BSE::Util::Valid qw/valid_date/;
9168c88c 7use BSE::Permissions;
d09682dd
TC
8use DevHelp::HTML qw(:default popup_menu);
9use BSE::Arrows;
ab2cd916
TC
10use BSE::CfgInfo qw(custom_class admin_base_url cfg_image_dir);
11use BSE::Util::Iterate;
ca9aa2bf
TC
12
13sub article_dispatch {
9168c88c
TC
14 my ($self, $req, $article, $articles) = @_;
15
16 BSE::Permissions->check_logon($req)
17 or return BSE::Template->get_refresh($req->url('logon'), $req->cfg);
18
19 my $cgi = $req->cgi;
ca9aa2bf
TC
20 my $action;
21 my %actions = $self->article_actions;
22 for my $check (keys %actions) {
23 if ($cgi->param($check) || $cgi->param("$check.x")) {
24 $action = $check;
25 last;
26 }
27 }
28 my @extraargs;
29 unless ($action) {
30 ($action, @extraargs) = $self->other_article_actions($cgi);
31 }
32 $action ||= 'edit';
33 my $method = $actions{$action};
9168c88c 34 return $self->$method($req, $article, $articles, @extraargs);
ca9aa2bf
TC
35}
36
37sub noarticle_dispatch {
9168c88c 38 my ($self, $req, $articles) = @_;
ca9aa2bf 39
9168c88c
TC
40 BSE::Permissions->check_logon($req)
41 or return BSE::Template->get_refresh($req->url('logon'), $req->cfg);
42
43 my $cgi = $req->cgi;
ca9aa2bf
TC
44 my $action = 'add';
45 my %actions = $self->noarticle_actions;
46 for my $check (keys %actions) {
47 if ($cgi->param($check) || $cgi->param("$check.x")) {
48 $action = $check;
49 last;
50 }
51 }
52 my $method = $actions{$action};
9168c88c 53 return $self->$method($req, $articles);
ca9aa2bf
TC
54}
55
ca9aa2bf
TC
56sub article_actions {
57 my ($self) = @_;
58
59 return
60 (
61 edit => 'edit_form',
62 save => 'save',
63 add_stepkid => 'add_stepkid',
64 del_stepkid => 'del_stepkid',
65 save_stepkids => 'save_stepkids',
66 add_stepparent => 'add_stepparent',
67 del_stepparent => 'del_stepparent',
68 save_stepparents => 'save_stepparents',
69 artimg => 'save_image_changes',
70 addimg => 'add_image',
6473c56f 71 remove => 'remove',
ca9aa2bf
TC
72 showimages => 'show_images',
73 process => 'save_image_changes',
74 removeimg => 'remove_img',
75 moveimgup => 'move_img_up',
76 moveimgdown => 'move_img_down',
77 filelist => 'filelist',
78 fileadd => 'fileadd',
79 fileswap => 'fileswap',
80 filedel => 'filedel',
81 filesave => 'filesave',
4010d92e
TC
82 hide => 'hide',
83 unhide => 'unhide',
ab2cd916 84 a_thumb => 'req_thumb',
ca9aa2bf
TC
85 );
86}
87
88sub other_article_actions {
89 my ($self, $cgi) = @_;
90
91 for my $param ($cgi->param) {
92 if ($param =~ /^removeimg_(\d+)(\.x)?$/) {
93 return ('removeimg', $1 );
94 }
95 }
96
97 return;
98}
99
100sub noarticle_actions {
101 return
102 (
103 add => 'add_form',
104 save => 'save_new',
105 );
106}
107
108sub get_parent {
109 my ($self, $parentid, $articles) = @_;
110
111 if ($parentid == -1) {
112 return
113 {
114 id => -1,
115 title=>'All Sections',
116 level => 0,
117 listed => 0,
118 parentid => undef,
119 };
120 }
121 else {
122 return $articles->getByPkey($parentid);
123 }
124}
125
126sub tag_hash {
127 my ($object, $args) = @_;
128
129 my $value = $object->{$args};
130 defined $value or $value = '';
7b81711b
TC
131 if ($value =~ /\cJ/ && $value =~ /\cM/) {
132 $value =~ tr/\cM//d;
133 }
77804754 134 escape_html($value);
ca9aa2bf
TC
135}
136
62533efa
TC
137sub tag_hash_mbcs {
138 my ($object, $args) = @_;
139
140 my $value = $object->{$args};
141 defined $value or $value = '';
142 if ($value =~ /\cJ/ && $value =~ /\cM/) {
143 $value =~ tr/\cM//d;
144 }
145 escape_html($value, '<>&"');
146}
147
ca9aa2bf
TC
148sub tag_art_type {
149 my ($level, $cfg) = @_;
150
77804754 151 escape_html($cfg->entry('level names', $level, 'Article'));
ca9aa2bf
TC
152}
153
154sub tag_if_new {
155 my ($article) = @_;
156
157 !$article->{id};
158}
159
160sub reparent_updown {
161 return 1;
162}
163
164sub should_be_catalog {
165 my ($self, $article, $parent, $articles) = @_;
166
167 if ($article->{parentid} && (!$parent || $parent->{id} != $article->{parentid})) {
168 $parent = $articles->getByPkey($article->{id});
169 }
170
171 my $shopid = $self->{cfg}->entryErr('articles', 'shop');
172
173 return $article->{parentid} && $parent &&
174 ($article->{parentid} == $shopid ||
175 $parent->{generator} eq 'Generate::Catalog');
176}
177
178sub possible_parents {
9168c88c 179 my ($self, $article, $articles, $req) = @_;
ca9aa2bf
TC
180
181 my %labels;
182 my @values;
183
184 my $shopid = $self->{cfg}->entryErr('articles', 'shop');
185 my @parents = $articles->getBy('level', $article->{level}-1);
186 @parents = grep { $_->{generator} eq 'Generate::Article'
187 && $_->{id} != $shopid } @parents;
9168c88c
TC
188
189 # user can only select parent they can add to
190 @parents = grep $req->user_can('edit_add_child', $_), @parents;
ca9aa2bf
TC
191
192 @values = ( map {$_->{id}} @parents );
193 %labels = ( map { $_->{id} => "$_->{title} ($_->{id})" } @parents );
194
9168c88c 195 if ($article->{level} == 1 && $req->user_can('edit_add_child')) {
ca9aa2bf
TC
196 push @values, -1;
197 $labels{-1} = "No parent - this is a section";
198 }
199
200 if ($article->{id} && $self->reparent_updown($article)) {
201 # we also list the siblings and grandparent (if any)
202 my @siblings = grep $_->{id} != $article->{id} && $_->{id} != $shopid,
203 $articles->getBy(parentid => $article->{parentid});
9168c88c 204 @siblings = grep $req->user_can('edit_add_child', $_), @siblings;
ca9aa2bf
TC
205 push @values, map $_->{id}, @siblings;
206 @labels{map $_->{id}, @siblings} =
207 map { "-- move down a level -- $_->{title} ($_->{id})" } @siblings;
208
209 if ($article->{parentid} != -1) {
210 my $parent = $articles->getByPkey($article->{parentid});
211 if ($parent->{parentid} != -1) {
212 my $gparent = $articles->getByPkey($parent->{parentid});
9168c88c
TC
213 if ($req->user_can('edit_add_child', $gparent)) {
214 push @values, $gparent->{id};
215 $labels{$gparent->{id}} =
216 "-- move up a level -- $gparent->{title} ($gparent->{id})";
217 }
ca9aa2bf
TC
218 }
219 else {
9168c88c
TC
220 if ($req->user_can('edit_add_child')) {
221 push @values, -1;
222 $labels{-1} = "-- move up a level -- become a section";
223 }
ca9aa2bf
TC
224 }
225 }
226 }
227
228 return (\@values, \%labels);
229}
230
231sub tag_list {
9168c88c 232 my ($self, $article, $articles, $cgi, $req, $what) = @_;
ca9aa2bf
TC
233
234 if ($what eq 'listed') {
235 my @values = qw(0 1);
236 my %labels = ( 0=>"No", 1=>"Yes");
237 if ($article->{level} <= 2) {
238 $labels{2} = "In Sections, but not menu";
239 push(@values, 2);
240 }
241 else {
242 $labels{2} = "In content, but not menus";
243 push(@values, 2);
244 }
d09682dd
TC
245 return popup_menu(-name=>'listed',
246 -values=>\@values,
247 -labels=>\%labels,
248 -default=>$article->{listed});
ca9aa2bf
TC
249 }
250 else {
9168c88c 251 my ($values, $labels) = $self->possible_parents($article, $articles, $req);
ca9aa2bf
TC
252 my $html;
253 if (defined $article->{parentid}) {
d09682dd
TC
254 $html = popup_menu(-name=>'parentid',
255 -values=> $values,
256 -labels => $labels,
257 -default => $article->{parentid},
258 -override=>1);
ca9aa2bf
TC
259 }
260 else {
d09682dd
TC
261 $html = popup_menu(-name=>'parentid',
262 -values=> $values,
263 -labels => $labels,
264 -override=>1);
ca9aa2bf
TC
265 }
266
267 # munge the html - we display a default value, so we need to wrap the
268 # default <select /> around this one
269 $html =~ s!^<select[^>]+>|</select>!!gi;
270 return $html;
271 }
272}
273
274sub tag_checked {
275 my ($arg, $acts, $funcname, $templater) = @_;
276 my ($func, $args) = split ' ', $arg, 2;
277 return $templater->perform($acts, $func, $args) ? 'checked' : '';
278}
279
280sub iter_get_images {
daee3409 281 my ($self, $article) = @_;
ca9aa2bf
TC
282
283 $article->{id} or return;
daee3409 284 $self->get_images($article);
ca9aa2bf
TC
285}
286
287sub iter_get_kids {
288 my ($article, $articles) = @_;
289
15fb10f2 290 my @children;
ca9aa2bf
TC
291 $article->{id} or return;
292 if (UNIVERSAL::isa($article, 'Article')) {
15fb10f2 293 @children = $article->children;
ca9aa2bf
TC
294 }
295 elsif ($article->{id}) {
15fb10f2 296 @children = $articles->children($article->{id});
ca9aa2bf 297 }
15fb10f2
TC
298
299 return sort { $b->{displayOrder} <=> $a->{displayOrder} } @children;
ca9aa2bf
TC
300}
301
302sub tag_if_have_child_type {
303 my ($level, $cfg) = @_;
304
305 defined $cfg->entry("level names", $level+1);
306}
307
308sub tag_is {
309 my ($args, $acts, $isname, $templater) = @_;
310
311 my ($func, $funcargs) = split ' ', $args, 2;
312 return $templater->perform($acts, $func, $funcargs) ? 'Yes' : 'No';
313}
314
caa7299c
TC
315sub default_template {
316 my ($self, $article, $cfg, $templates) = @_;
317
318 if ($article->{parentid}) {
319 my $template = $cfg->entry("children of $article->{parentid}", "template");
320 return $template
321 if $template && grep $_ eq $template, @$templates;
322 }
323 if ($article->{level}) {
324 my $template = $cfg->entry("level $article->{level}", "template");
325 return $template
326 if $template && grep $_ eq $template, @$templates;
327 }
328 return $templates->[0];
329}
330
ca9aa2bf
TC
331sub tag_templates {
332 my ($self, $article, $cfg, $cgi) = @_;
333
334 my @templates = sort $self->templates($article);
335 my $default;
336 if ($article->{template} && grep $_ eq $article->{template}, @templates) {
337 $default = $article->{template};
338 }
339 else {
caa7299c
TC
340 my @options;
341 $default = $self->default_template($article, $cfg, \@templates);
ca9aa2bf 342 }
d09682dd
TC
343 return popup_menu(-name=>'template',
344 -values=>\@templates,
345 -default=>$default,
346 -override=>1);
ca9aa2bf
TC
347}
348
349sub title_images {
350 my ($self, $article) = @_;
351
352 my @title_images;
ab2cd916 353 my $imagedir = cfg_image_dir($self->{cfg});
ca9aa2bf
TC
354 if (opendir TITLE_IMAGES, "$imagedir/titles") {
355 @title_images = sort
356 grep -f "$imagedir/titles/$_" && /\.(gif|jpeg|jpg|png)$/i,
357 readdir TITLE_IMAGES;
358 closedir TITLE_IMAGES;
359 }
360
361 @title_images;
362}
363
364sub tag_title_images {
365 my ($self, $article, $cfg, $cgi) = @_;
366
367 my @images = $self->title_images($article);
368 my @values = ( '', @images );
369 my %labels = ( '' => 'None', map { $_ => $_ } @images );
370 return $cgi->
371 popup_menu(-name=>'titleImage',
372 -values=>\@values,
373 -labels=>\%labels,
374 -default=>$article->{id} ? $article->{titleImage} : '',
375 -override=>1);
376}
377
378sub base_template_dirs {
379 return ( "common" );
380}
381
382sub template_dirs {
383 my ($self, $article) = @_;
384
385 my @dirs = $self->base_template_dirs;
386 if (my $parentid = $article->{parentid}) {
387 my $section = "children of $parentid";
388 if (my $dirs = $self->{cfg}->entry($section, 'template_dirs')) {
389 push @dirs, split /,/, $dirs;
390 }
391 }
392 if (my $id = $article->{id}) {
393 my $section = "article $id";
394 if (my $dirs = $self->{cfg}->entry($section, 'template_dirs')) {
395 push @dirs, split /,/, $dirs;
396 }
397 }
caa7299c
TC
398 if ($article->{level}) {
399 push @dirs, $article->{level};
400 my $dirs = $self->{cfg}->entry("level $article->{level}", 'template_dirs');
401 push @dirs, split /,/, $dirs if $dirs;
402 }
ca9aa2bf
TC
403
404 @dirs;
405}
406
407sub templates {
408 my ($self, $article) = @_;
409
410 my @dirs = $self->template_dirs($article);
411 my @templates;
918735d1
TC
412 my @basedirs = BSE::Template->template_dirs($self->{cfg});
413 for my $basedir (@basedirs) {
414 for my $dir (@dirs) {
415 my $path = File::Spec->catdir($basedir, $dir);
416 if (-d $path) {
417 if (opendir TEMPLATE_DIR, $path) {
418 push(@templates, sort map "$dir/$_",
419 grep -f "$path/$_" && /\.(tmpl|html)$/i, readdir TEMPLATE_DIR);
420 closedir TEMPLATE_DIR;
421 }
ca9aa2bf
TC
422 }
423 }
424 }
918735d1
TC
425
426 # eliminate any dups, and order it nicely
427 my %seen;
428 @templates = sort { lc($a) cmp lc($b) }
429 grep !$seen{$_}++, @templates;
430
ca9aa2bf
TC
431 return (@templates, $self->extra_templates($article));
432}
433
434sub extra_templates {
435 my ($self, $article) = @_;
436
aefcabcb 437 my $basedir = $self->{cfg}->entryVar('paths', 'templates');
ca9aa2bf
TC
438 my @templates;
439 if (my $id = $article->{id}) {
440 push @templates, 'index.tmpl'
441 if $id == 1 && -f "$basedir/index.html";
442 push @templates, 'index2.tmpl'
443 if $id == 2 && -f "$basedir/index2.html";
444 my $shopid = $self->{cfg}->entryErr('articles', 'shop');
445 push @templates, "shop_sect.tmpl"
446 if $id == $shopid && -f "$basedir/shop_sect.tmpl";
447 my $section = "article $id";
448 my $extras = $self->{cfg}->entry($section, 'extra_templates');
449 push @templates, grep /\.(tmpl|html)$/i, split /,/, $extras
450 if $extras;
451 }
452
453 @templates;
454}
455
456sub edit_parent {
457 my ($article) = @_;
458
459 return '' unless $article->{id} && $article->{id} != -1;
460 return <<HTML;
461<a href="$ENV{SCRIPT_NAME}?id=$article->{parentid}">Edit parent</a> |
462HTML
463}
464
465sub iter_allkids {
466 my ($article) = @_;
467
468 return unless $article->{id} && $article->{id} > 0;
469 $article->allkids;
470}
471
472sub _load_step_kids {
473 my ($article, $step_kids) = @_;
474
475 my @stepkids = OtherParents->getBy(parentId=>$article->{id}) if $article->{id};
476 %$step_kids = map { $_->{childId} => $_ } @stepkids;
ca9aa2bf
TC
477 $step_kids->{loaded} = 1;
478}
479
480sub tag_if_step_kid {
481 my ($article, $allkids, $rallkid_index, $step_kids) = @_;
482
483 _load_step_kids($article, $step_kids) unless $step_kids->{loaded};
484
485 my $kid = $allkids->[$$rallkid_index]
486 or return;
487 exists $step_kids->{$kid->{id}};
488}
489
490sub tag_step_kid {
491 my ($article, $allkids, $rallkid_index, $step_kids, $arg) = @_;
492
493 _load_step_kids($article, $step_kids) unless $step_kids->{loaded};
494
495 my $kid = $allkids->[$$rallkid_index]
496 or return '';
f0543260
TC
497 my $step_kid = $step_kids->{$kid->{id}}
498 or return '';
499 #use Data::Dumper;
500 #print STDERR "found kid (want $arg): ", Dumper($kid), Dumper($step_kid);
501 escape_html($step_kid->{$arg});
ca9aa2bf
TC
502}
503
504sub tag_move_stepkid {
8b0b2f34
TC
505 my ($self, $cgi, $req, $article, $allkids, $rallkids_index, $arg,
506 $acts, $funcname, $templater) = @_;
31a26b52
TC
507
508 $req->user_can(edit_reorder_children => $article)
509 or return '';
ca9aa2bf 510
aefcabcb
TC
511 @$allkids > 1 or return '';
512
8b0b2f34
TC
513 my ($img_prefix, $urladd) = DevHelp::Tags->get_parms($arg, $acts, $templater);
514 $img_prefix = '' unless defined $img_prefix;
515 $urladd = '' unless defined $urladd;
516
ca9aa2bf 517 my $cgi_uri = $self->{cfg}->entry('uri', 'cgi', '/cgi-bin');
ca9aa2bf
TC
518 my $url = $ENV{SCRIPT_NAME} . "?id=$article->{id}";
519 if ($cgi->param('_t')) {
520 $url .= "&_t=".$cgi->param('_t');
521 }
8b0b2f34 522 $url .= $urladd;
ca9aa2bf 523 $url .= "#step";
d09682dd 524 my $down_url = '';
ca9aa2bf 525 if ($$rallkids_index < $#$allkids) {
d09682dd 526 $down_url = "$cgi_uri/admin/move.pl?stepparent=$article->{id}&d=swap&id=$allkids->[$$rallkids_index]{id}&other=$allkids->[$$rallkids_index+1]{id}";
aefcabcb 527 }
d09682dd 528 my $up_url = '';
ca9aa2bf 529 if ($$rallkids_index > 0) {
d09682dd 530 $up_url = "$cgi_uri/admin/move.pl?stepparent=$article->{id}&d=swap&id=$allkids->[$$rallkids_index]{id}&other=$allkids->[$$rallkids_index-1]{id}";
ca9aa2bf 531 }
d09682dd
TC
532
533 return make_arrows($req->cfg, $down_url, $up_url, $url, $img_prefix);
ca9aa2bf
TC
534}
535
536sub possible_stepkids {
31a26b52
TC
537 my ($req, $article, $articles, $stepkids) = @_;
538
539 $req->user_can(edit_stepkid_add => $article)
540 or return;
ca9aa2bf 541
31a26b52 542 my @possible = sort { lc $a->{title} cmp lc $b->{title} }
de30d08a 543 grep !$stepkids->{$_->{id}} && $_->{id} != $article->{id}, $articles->all;
31a26b52
TC
544 if ($req->access_control) {
545 @possible = grep $req->user_can(edit_stepparent_add => $_), @possible;
546 }
547 return @possible;
ca9aa2bf
TC
548}
549
ca9aa2bf 550sub tag_possible_stepkids {
31a26b52 551 my ($step_kids, $req, $article, $possstepkids, $articles, $cgi) = @_;
ca9aa2bf
TC
552
553 _load_step_kids($article, $step_kids) unless $step_kids->{loaded};
31a26b52 554 @$possstepkids = possible_stepkids($req, $article, $articles, $step_kids)
ca9aa2bf
TC
555 unless @$possstepkids;
556 my %labels = map { $_->{id} => "$_->{title} ($_->{id})" } @$possstepkids;
557 return
d09682dd
TC
558 popup_menu(-name=>'stepkid',
559 -values=> [ map $_->{id}, @$possstepkids ],
560 -labels => \%labels);
ca9aa2bf
TC
561}
562
563sub tag_if_possible_stepkids {
31a26b52 564 my ($step_kids, $req, $article, $possstepkids, $articles, $cgi) = @_;
ca9aa2bf
TC
565
566 _load_step_kids($article, $step_kids) unless $step_kids->{loaded};
31a26b52 567 @$possstepkids = possible_stepkids($req, $article, $articles, $step_kids)
ca9aa2bf
TC
568 unless @$possstepkids;
569
570 @$possstepkids;
571}
572
573sub iter_get_stepparents {
574 my ($article) = @_;
575
576 return unless $article->{id} && $article->{id} > 0;
577
578 OtherParents->getBy(childId=>$article->{id});
579}
580
581sub tag_ifStepParents {
582 my ($args, $acts, $funcname, $templater) = @_;
583
584 return $templater->perform($acts, 'ifStepparents', '');
585}
586
587sub tag_stepparent_targ {
588 my ($article, $targs, $rindex, $arg) = @_;
589
590 if ($article->{id} && $article->{id} > 0 && !@$targs) {
591 @$targs = $article->step_parents;
592 }
77804754 593 escape_html($targs->[$$rindex]{$arg});
ca9aa2bf
TC
594}
595
596sub tag_move_stepparent {
8b0b2f34
TC
597 my ($self, $cgi, $req, $article, $stepparents, $rindex, $arg,
598 $acts, $funcname, $templater) = @_;
31a26b52
TC
599
600 $req->user_can(edit_reorder_stepparents => $article)
601 or return '';
ca9aa2bf 602
aefcabcb
TC
603 @$stepparents > 1 or return '';
604
8b0b2f34
TC
605 my ($img_prefix, $urladd) = DevHelp::Tags->get_parms($arg, $acts, $templater);
606 $img_prefix = '' unless defined $img_prefix;
607 $urladd = '' unless defined $urladd;
608
ca9aa2bf
TC
609 my $cgi_uri = $self->{cfg}->entry('uri', 'cgi', '/cgi-bin');
610 my $images_uri = $self->{cfg}->entry('uri', 'images', '/images');
611 my $html = '';
612 my $url = $ENV{SCRIPT_NAME} . "?id=$article->{id}";
613 if ($cgi->param('_t')) {
614 $url .= "&_t=".$cgi->param('_t');
615 }
8b0b2f34 616 $url .= $urladd;
ca9aa2bf 617 $url .= "#stepparents";
d794b180 618 my $blank = qq!<img src="$images_uri/trans_pixel.gif" width="17" height="13" border="0" align="absbottom" alt="" />!;
d09682dd 619 my $down_url = '';
ca9aa2bf 620 if ($$rindex < $#$stepparents) {
d09682dd 621 $down_url = "$cgi_uri/admin/move.pl?stepchild=$article->{id}&id=$stepparents->[$$rindex]{parentId}&d=swap&other=$stepparents->[$$rindex+1]{parentId}";
aefcabcb 622 }
d09682dd 623 my $up_url = '';
ca9aa2bf 624 if ($$rindex > 0) {
d09682dd 625 $up_url = "$cgi_uri/admin/move.pl?stepchild=$article->{id}&id=$stepparents->[$$rindex]{parentId}&d=swap&other=$stepparents->[$$rindex-1]{parentId}";
aefcabcb 626 }
d09682dd
TC
627
628 return make_arrows($req->cfg, $down_url, $up_url, $url, $img_prefix);
ca9aa2bf
TC
629}
630
de193691
TC
631sub _stepparent_possibles {
632 my ($req, $article, $articles, $targs) = @_;
633
634 $req->user_can(edit_stepparent_add => $article)
635 or return;
636
637 @$targs = $article->step_parents unless @$targs;
638 my %targs = map { $_->{id}, 1 } @$targs;
de30d08a
TC
639 my @possibles = grep !$targs{$_->{id}} && $_->{id} != $article->{id},
640 $articles->all;
de193691
TC
641 if ($req->access_control) {
642 @possibles = grep $req->user_can(edit_stepkid_add => $_), @possibles;
643 }
644 @possibles = sort { lc $a->{title} cmp lc $b->{title} } @possibles;
645
646 return @possibles;
647}
648
ca9aa2bf 649sub tag_if_stepparent_possibles {
31a26b52 650 my ($req, $article, $articles, $targs, $possibles) = @_;
ca9aa2bf 651
de193691
TC
652 if ($article->{id} && $article->{id} > 0 && !@$possibles) {
653 @$possibles = _stepparent_possibles($req, $article, $articles, $targs);
ca9aa2bf
TC
654 }
655 scalar @$possibles;
656}
657
658sub tag_stepparent_possibles {
31a26b52 659 my ($cgi, $req, $article, $articles, $targs, $possibles) = @_;
ca9aa2bf 660
de193691
TC
661 if ($article->{id} && $article->{id} > 0 && !@$possibles) {
662 @$possibles = _stepparent_possibles($req, $article, $articles, $targs);
ca9aa2bf 663 }
d09682dd
TC
664 popup_menu(-name=>'stepparent',
665 -values => [ map $_->{id}, @$possibles ],
666 -labels => { map { $_->{id}, "$_->{title} ($_->{id})" }
667 @$possibles });
ca9aa2bf
TC
668}
669
670sub iter_files {
671 my ($article) = @_;
672
673 return unless $article->{id} && $article->{id} > 0;
674
675 return $article->files;
676}
677
678sub tag_edit_parent {
679 my ($article) = @_;
680
681 return '' unless $article->{id} && $article->{id} != -1;
682
683 return <<HTML;
684<a href="$ENV{SCRIPT_NAME}?id=$article->{parentid}">Edit parent</a> |
685HTML
686}
687
688sub tag_if_children {
689 my ($args, $acts, $funcname, $templater) = @_;
690
691 return $templater->perform($acts, 'ifChildren', '');
692}
693
694sub tag_movechild {
8b0b2f34
TC
695 my ($self, $req, $article, $kids, $rindex, $arg,
696 $acts, $funcname, $templater) = @_;
abf5bbc6
TC
697
698 $req->user_can('edit_reorder_children', $article)
699 or return '';
ca9aa2bf 700
aefcabcb
TC
701 @$kids > 1 or return '';
702
ca9aa2bf
TC
703 $$rindex >=0 && $$rindex < @$kids
704 or return '** movechild can only be used in the children iterator **';
705
8b0b2f34
TC
706 my ($img_prefix, $urladd) = DevHelp::Tags->get_parms($arg, $acts, $templater);
707 $img_prefix = '' unless defined $img_prefix;
708 $urladd = '' unless defined $urladd;
709
ca9aa2bf
TC
710 my $cgi_uri = $self->{cfg}->entry('uri', 'cgi', '/cgi-bin');
711 my $images_uri = $self->{cfg}->entry('uri', 'images', '/images');
41f10371 712 my $urlbase = admin_base_url($req->cfg);
d09682dd 713 my $refresh_url = "$urlbase$ENV{SCRIPT_NAME}?id=$article->{id}";
cc9019d1
TC
714 my $t = $req->cgi->param('_t');
715 if ($t && $t =~ /^\w+$/) {
d09682dd 716 $refresh_url .= "&_t=$t";
cc9019d1 717 }
d09682dd
TC
718
719 $refresh_url .= $urladd;
720
ca9aa2bf 721 my $id = $kids->[$$rindex]{id};
d09682dd 722 my $down_url = '';
ca9aa2bf 723 if ($$rindex < $#$kids) {
d09682dd 724 $down_url = "$cgi_uri/admin/move.pl?id=$id&d=down&edit=1&all=1";
ca9aa2bf 725 }
d09682dd 726 my $up_url = '';
ca9aa2bf 727 if ($$rindex > 0) {
d09682dd 728 $up_url = "$cgi_uri/admin/move.pl?id=$id&d=up&edit=1&all=1"
ca9aa2bf 729 }
ca9aa2bf 730
d09682dd 731 return make_arrows($req->cfg, $down_url, $up_url, $refresh_url, $img_prefix);
ca9aa2bf
TC
732}
733
734sub tag_edit_link {
d09682dd 735 my ($article, $args, $acts, $funcname, $templater) = @_;
ca9aa2bf
TC
736 my ($which, $name) = split / /, $args, 2;
737 $name ||= 'Edit';
738 my $gen_class;
739 if ($acts->{$which}
740 && ($gen_class = $templater->perform($acts, $which, 'generator'))) {
741 eval "use $gen_class";
742 unless ($@) {
d09682dd 743 my $gen = $gen_class->new(top => $article);
ca9aa2bf
TC
744 my $link = $gen->edit_link($templater->perform($acts, $which, 'id'));
745 return qq!<a href="$link">$name</a>!;
746 }
747 }
748 return '';
749}
750
751sub tag_imgmove {
8b0b2f34
TC
752 my ($req, $article, $rindex, $images, $arg,
753 $acts, $funcname, $templater) = @_;
abf5bbc6
TC
754
755 $req->user_can(edit_images_reorder => $article)
756 or return '';
ca9aa2bf 757
aefcabcb
TC
758 @$images > 1 or return '';
759
ca9aa2bf
TC
760 $$rindex >= 0 && $$rindex < @$images
761 or return '** imgmove can only be used in image iterator **';
762
8b0b2f34
TC
763 my ($img_prefix, $urladd) = DevHelp::Tags->get_parms($arg, $acts, $templater);
764 $img_prefix = '' unless defined $img_prefix;
765 $urladd = '' unless defined $urladd;
766
41f10371 767 my $urlbase = admin_base_url($req->cfg);
cc9019d1
TC
768 my $url = "$urlbase$ENV{SCRIPT_NAME}?id=$article->{id}";
769 my $t = $req->cgi->param('_t');
770 if ($t && $t =~ /^\w+$/) {
771 $url .= "&_t=$t";
772 }
773 $url .= $urladd;
8b0b2f34 774
ca9aa2bf 775 my $image = $images->[$$rindex];
d09682dd 776 my $down_url;
ca9aa2bf 777 if ($$rindex < $#$images) {
d09682dd 778 $down_url = "$ENV{SCRIPT_NAME}?id=$article->{id}&moveimgdown=1&imageid=$image->{id}";
ca9aa2bf 779 }
d09682dd
TC
780 my $up_url = '';
781 if ($$rindex > 0) {
782 $up_url = "$ENV{SCRIPT_NAME}?id=$article->{id}&moveimgup=1&imageid=$image->{id}";
ca9aa2bf 783 }
d09682dd 784 return make_arrows($req->cfg, $down_url, $up_url, $url, $img_prefix);
ca9aa2bf
TC
785}
786
787sub tag_movefiles {
8b0b2f34
TC
788 my ($self, $req, $article, $files, $rindex, $arg,
789 $acts, $funcname, $templater) = @_;
abf5bbc6
TC
790
791 $req->user_can('edit_files_reorder', $article)
792 or return '';
ca9aa2bf 793
aefcabcb
TC
794 @$files > 1 or return '';
795
8b0b2f34
TC
796 my ($img_prefix, $urladd) = DevHelp::Tags->get_parms($arg, $acts, $templater);
797 $img_prefix = '' unless defined $img_prefix;
798 $urladd = '' unless defined $urladd;
799
ca9aa2bf
TC
800 $$rindex >= 0 && $$rindex < @$files
801 or return '** movefiles can only be used in the files iterator **';
802
41f10371 803 my $urlbase = admin_base_url($req->cfg);
9063386f
TC
804 my $url = "$urlbase$ENV{SCRIPT_NAME}?id=$article->{id}$urladd";
805 my $t = $req->cgi->param('_t');
806 if ($t && $t =~ /^\w+$/) {
807 $url .= "&_t=$t";
808 }
d09682dd
TC
809
810 my $down_url = "";
ca9aa2bf 811 if ($$rindex < $#$files) {
d09682dd 812 $down_url = "$ENV{SCRIPT_NAME}?fileswap=1&amp;id=$article->{id}&amp;file1=$files->[$$rindex]{id}&amp;file2=$files->[$$rindex+1]{id}";
ca9aa2bf 813 }
d09682dd 814 my $up_url = "";
ca9aa2bf 815 if ($$rindex > 0) {
d09682dd 816 $up_url = "$ENV{SCRIPT_NAME}?fileswap=1&amp;id=$article->{id}&amp;file1=$files->[$$rindex]{id}&amp;file2=$files->[$$rindex-1]{id}";
ca9aa2bf 817 }
d09682dd
TC
818
819 return make_arrows($req->cfg, $down_url, $up_url, $url, $img_prefix);
ca9aa2bf
TC
820}
821
822sub tag_old {
823 my ($article, $cgi, $args, $acts, $funcname, $templater) = @_;
824
825 my ($col, $func, $funcargs) = split ' ', $args, 3;
826 my $value = $cgi->param($col);
827 if (defined $value) {
77804754 828 return escape_html($value);
ca9aa2bf
TC
829 }
830 else {
831 if ($func) {
832 return $templater->perform($acts, $func, $funcargs);
833 }
834 else {
835 $value = $article->{$args};
836 defined $value or $value = '';
77804754 837 return escape_html($value);
ca9aa2bf
TC
838 }
839 }
840}
841
08123550
TC
842sub iter_admin_users {
843 require BSE::TB::AdminUsers;
844
845 BSE::TB::AdminUsers->all;
846}
847
848sub iter_admin_groups {
849 require BSE::TB::AdminGroups;
850
851 BSE::TB::AdminGroups->all;
852}
853
9168c88c
TC
854sub tag_if_field_perm {
855 my ($req, $article, $field) = @_;
856
abf5bbc6
TC
857 unless ($field =~ /^\w+$/) {
858 print STDERR "Bad fieldname '$field'\n";
859 return;
860 }
9168c88c 861 if ($article->{id}) {
abf5bbc6 862 return $req->user_can("edit_field_edit_$field", $article);
9168c88c
TC
863 }
864 else {
4010d92e 865 #print STDERR "adding, always successful\n";
abf5bbc6 866 return 1;
9168c88c
TC
867 }
868}
869
870sub tag_default {
871 my ($self, $req, $article, $args, $acts, $funcname, $templater) = @_;
872
873 my ($col, $func, $funcargs) = split ' ', $args, 3;
874 if ($article->{id}) {
875 if ($func) {
876 return $templater->perform($acts, $func, $funcargs);
877 }
878 else {
879 my $value = $article->{$args};
880 defined $value or $value = '';
77804754 881 return escape_html($value);
9168c88c
TC
882 }
883 }
884 else {
885 my $value = $self->default_value($req, $article, $col);
0ec4ac8a 886 defined $value or $value = '';
77804754 887 return escape_html($value);
9168c88c
TC
888 }
889}
890
918735d1
TC
891sub iter_flags {
892 my ($self) = @_;
893
894 $self->flags;
895}
896
897sub tag_if_flag_set {
898 my ($article, $arg, $acts, $funcname, $templater) = @_;
899
900 my @args = DevHelp::Tags->get_parms($arg, $acts, $templater);
901 @args or return;
902
903 return index($article->{flags}, $args[0]) >= 0;
904}
905
8b0b2f34
TC
906sub iter_crumbs {
907 my ($article, $articles) = @_;
908
909 my @crumbs;
910 my $temp = $article;
911 defined($temp->{parentid}) or return;
912 while ($temp->{parentid} > 0
913 and my $crumb = $articles->getByPkey($temp->{parentid})) {
914 unshift @crumbs, $crumb;
915 $temp = $crumb;
916 }
917
918 @crumbs;
919}
920
921sub tag_typename {
922 my ($args, $acts, $funcname, $templater) = @_;
923
924 exists $acts->{$args} or return "** need an article name **";
925 my $generator = $templater->perform($acts, $args, 'generator');
926
927 $generator =~ /^(?:BSE::)?Generate::(\w+)$/
928 or return "** invalid generator $generator **";
929
930 return $1;
931}
932
ab2cd916
TC
933sub _get_thumbs_class {
934 my ($self) = @_;
935
936 $self->{cfg}->entry('editor', 'allow_thumb', 0)
937 or return;
938
939 my $class = $self->{cfg}->entry('editor', 'thumbs_class')
940 or return;
941
942 (my $filename = "$class.pm") =~ s!::!/!g;
943 eval { require $filename; };
944 if ($@) {
945 print STDERR "** Error loading thumbs_class $class ($filename): $@\n";
946 return;
947 }
948 my $obj;
949 eval { $obj = $class->new($self->{cfg}) };
950 if ($@) {
951 print STDERR "** Error creating thumbs objects $class: $@\n";
952 return;
953 }
954
955 return $obj;
956}
957
958sub tag_thumbimage {
959 my ($cfg, $thumbs_obj, $current_image, $args) = @_;
960
6a8a6ac5
TC
961 $thumbs_obj or return '';
962
ab2cd916
TC
963 $$current_image or return '** no current image **';
964
965 my $imagedir = cfg_image_dir($cfg);
966
967 my $filename = "$imagedir/$$current_image->{image}";
968 -e $filename or return "** image file missing **";
969
970 my ($max_width, $max_height, $max_pixels) = split ' ', $args;
971 defined $max_width && $max_width eq '-' and undef $max_width;
972 defined $max_height && $max_height eq '-' and undef $max_height;
973 defined $max_pixels && $max_pixels eq '-' and undef $max_pixels;
974
975 my ($use_orig, $width, $height) = $thumbs_obj->thumb_dimensions
976 ($filename, $$current_image, $max_width, $max_height, $max_pixels);
977
978
979 my ($uri, $alt);
980 if ($use_orig) {
981 $alt = $$current_image->{alt};
982 $uri = "/images/$$current_image->{image}";
983 }
984 elsif ($width) {
985 $alt = "thumbnail of ".$$current_image->{alt};
986 $uri = "$ENV{SCRIPT_NAME}?a_thumb=1&id=$$current_image->{articleId}&im=$$current_image->{id}&w=$width&h=$height";
987 }
988 else {
989 # link to the default thumbnail
990 $uri = $cfg->entry('editor', 'default_thumbnail', '/images/admin/nothumb.png');
991 $width = $cfg->entry('editor', 'default_thumbnail_width', 100);
992 $height = $cfg->entry('editor', 'default_thumbnail_height', 100);
993 $alt = $cfg->entry('editor', 'default_thumbnail_alt',
994 "no thumbnail available");
995 }
996
997 $alt = escape_html($alt);
998 $uri = escape_html($uri);
999 return qq!<img src="$uri" width="$width" height="$height" alt="$alt" border="0" />!;
1000}
1001
ca9aa2bf
TC
1002sub low_edit_tags {
1003 my ($self, $acts, $request, $article, $articles, $msg, $errors) = @_;
1004
1005 my $cgi = $request->cgi;
ab2cd916 1006 my $show_full = $cgi->param('f_showfull');
16ac5598 1007 $msg ||= join "\n", map escape_html($_), $cgi->param('message'), $cgi->param('m');
ca9aa2bf
TC
1008 $msg ||= '';
1009 $errors ||= {};
1010 if (keys %$errors && !$msg) {
1011 # try to get the errors in the same order as the table
1012 my @cols = $self->table_object($articles)->rowClass->columns;
1013 my %work = %$errors;
1014 my @out = grep defined, delete @work{@cols};
1015
1016 $msg = join "<br>", @out, values %work;
1017 }
abf5bbc6
TC
1018 my $parent;
1019 if ($article->{id}) {
1020 if ($article->{parentid} > 0) {
1021 $parent = $article->parent;
1022 }
1023 else {
1024 $parent = { title=>"No parent - this is a section", id=>-1 };
1025 }
1026 }
1027 else {
1028 $parent = { title=>"How did we get here?", id=>0 };
1029 }
62533efa
TC
1030 my $cfg = $self->{cfg};
1031 my $mbcs = $cfg->entry('html', 'mbcs', 0);
1032 my $tag_hash = $mbcs ? \&tag_hash_mbcs : \&tag_hash;
ab2cd916
TC
1033 my $thumbs_obj_real = $self->_get_thumbs_class();
1034 my $thumbs_obj = $show_full ? undef : $thumbs_obj_real;
ca9aa2bf
TC
1035 my @images;
1036 my $image_index;
ab2cd916 1037 my $current_image;
ca9aa2bf
TC
1038 my @children;
1039 my $child_index;
1040 my %stepkids;
ca9aa2bf
TC
1041 my @allkids;
1042 my $allkid_index;
1043 my @possstepkids;
1044 my @stepparents;
1045 my $stepparent_index;
1046 my @stepparent_targs;
1047 my @stepparentpossibles;
1048 my @files;
1049 my $file_index;
ab2cd916 1050 my $it = BSE::Util::Iterate->new;
ca9aa2bf
TC
1051 return
1052 (
1053 BSE::Util::Tags->basic($acts, $cgi, $cfg),
1054 BSE::Util::Tags->admin($acts, $cfg),
9168c88c 1055 BSE::Util::Tags->secure($request),
62533efa 1056 article => [ $tag_hash, $article ],
ca9aa2bf 1057 old => [ \&tag_old, $article, $cgi ],
9168c88c 1058 default => [ \&tag_default, $self, $request, $article ],
ca9aa2bf
TC
1059 articleType => [ \&tag_art_type, $article->{level}, $cfg ],
1060 parentType => [ \&tag_art_type, $article->{level}-1, $cfg ],
918735d1 1061 ifNew => [ \&tag_if_new, $article ],
9168c88c 1062 list => [ \&tag_list, $self, $article, $articles, $cgi, $request ],
ca9aa2bf
TC
1063 script => $ENV{SCRIPT_NAME},
1064 level => $article->{level},
1065 checked => \&tag_checked,
ab2cd916 1066 $it->make_iterator
daee3409 1067 ([ \&iter_get_images, $self, $article ], 'image', 'images', \@images,
ab2cd916
TC
1068 \$image_index, undef, \$current_image),
1069 thumbimage => [ \&tag_thumbimage, $cfg, $thumbs_obj, \$current_image ],
1070 ifThumbs => defined($thumbs_obj),
1071 ifCanThumbs => defined($thumbs_obj_real),
abf5bbc6 1072 imgmove => [ \&tag_imgmove, $request, $article, \$image_index, \@images ],
ca9aa2bf
TC
1073 message => $msg,
1074 DevHelp::Tags->make_iterator2
1075 ([ \&iter_get_kids, $article, $articles ],
1076 'child', 'children', \@children, \$child_index),
1077 ifchildren => \&tag_if_children,
1078 childtype => [ \&tag_art_type, $article->{level}+1, $cfg ],
1079 ifHaveChildType => [ \&tag_if_have_child_type, $article->{level}, $cfg ],
abf5bbc6
TC
1080 movechild => [ \&tag_movechild, $self, $request, $article, \@children,
1081 \$child_index],
ca9aa2bf
TC
1082 is => \&tag_is,
1083 templates => [ \&tag_templates, $self, $article, $cfg, $cgi ],
1084 titleImages => [ \&tag_title_images, $self, $article, $cfg, $cgi ],
1085 editParent => [ \&tag_edit_parent, $article ],
1086 DevHelp::Tags->make_iterator2
1087 ([ \&iter_allkids, $article ], 'kid', 'kids', \@allkids, \$allkid_index),
1088 ifStepKid =>
1089 [ \&tag_if_step_kid, $article, \@allkids, \$allkid_index, \%stepkids ],
1090 stepkid => [ \&tag_step_kid, $article, \@allkids, \$allkid_index,
1091 \%stepkids ],
1092 movestepkid =>
31a26b52
TC
1093 [ \&tag_move_stepkid, $self, $cgi, $request, $article, \@allkids,
1094 \$allkid_index ],
ca9aa2bf 1095 possible_stepkids =>
31a26b52
TC
1096 [ \&tag_possible_stepkids, \%stepkids, $request, $article,
1097 \@possstepkids, $articles, $cgi ],
ca9aa2bf 1098 ifPossibles =>
31a26b52
TC
1099 [ \&tag_if_possible_stepkids, \%stepkids, $request, $article,
1100 \@possstepkids, $articles, $cgi ],
ca9aa2bf
TC
1101 DevHelp::Tags->make_iterator2
1102 ( [ \&iter_get_stepparents, $article ], 'stepparent', 'stepparents',
1103 \@stepparents, \$stepparent_index),
1104 ifStepParents => \&tag_ifStepParents,
1105 stepparent_targ =>
1106 [ \&tag_stepparent_targ, $article, \@stepparent_targs,
1107 \$stepparent_index ],
1108 movestepparent =>
31a26b52 1109 [ \&tag_move_stepparent, $self, $cgi, $request, $article, \@stepparents,
ca9aa2bf
TC
1110 \$stepparent_index ],
1111 ifStepparentPossibles =>
31a26b52
TC
1112 [ \&tag_if_stepparent_possibles, $request, $article, $articles,
1113 \@stepparent_targs, \@stepparentpossibles, ],
ca9aa2bf 1114 stepparent_possibles =>
31a26b52 1115 [ \&tag_stepparent_possibles, $cgi, $request, $article, $articles,
ca9aa2bf
TC
1116 \@stepparent_targs, \@stepparentpossibles, ],
1117 DevHelp::Tags->make_iterator2
1118 ([ \&iter_files, $article ], 'file', 'files', \@files, \$file_index ),
abf5bbc6
TC
1119 movefiles =>
1120 [ \&tag_movefiles, $self, $request, $article, \@files, \$file_index ],
08123550
TC
1121 DevHelp::Tags->make_iterator2
1122 (\&iter_admin_users, 'iadminuser', 'adminusers'),
1123 DevHelp::Tags->make_iterator2
1124 (\&iter_admin_groups, 'iadmingroup', 'admingroups'),
d09682dd 1125 edit => [ \&tag_edit_link, $article ],
62533efa 1126 error => [ $tag_hash, $errors ],
b553afa2 1127 error_img => [ \&tag_error_img, $cfg, $errors ],
9168c88c 1128 ifFieldPerm => [ \&tag_if_field_perm, $request, $article ],
62533efa 1129 parent => [ $tag_hash, $parent ],
918735d1
TC
1130 DevHelp::Tags->make_iterator2
1131 ([ \&iter_flags, $self ], 'flag', 'flags' ),
1132 ifFlagSet => [ \&tag_if_flag_set, $article ],
8b0b2f34
TC
1133 DevHelp::Tags->make_iterator2
1134 ([ \&iter_crumbs, $article, $articles ], 'crumb', 'crumbs' ),
1135 typename => \&tag_typename,
ca9aa2bf
TC
1136 );
1137}
1138
1139sub edit_template {
1140 my ($self, $article, $cgi) = @_;
1141
1142 my $base = $article->{level};
1143 my $t = $cgi->param('_t');
1144 if ($t && $t =~ /^\w+$/) {
1145 $base = $t;
1146 }
1147 return $self->{cfg}->entry('admin templates', $base,
1148 "admin/edit_$base");
1149}
1150
1151sub add_template {
1152 my ($self, $article, $cgi) = @_;
1153
1154 $self->edit_template($article, $cgi);
1155}
1156
1157sub low_edit_form {
1158 my ($self, $request, $article, $articles, $msg, $errors) = @_;
1159
1160 my $cgi = $request->cgi;
1161 my %acts;
1162 %acts = $self->low_edit_tags(\%acts, $request, $article, $articles, $msg,
1163 $errors);
1164 my $template = $article->{id} ?
1165 $self->edit_template($article, $cgi) : $self->add_template($article, $cgi);
1166
1167 return BSE::Template->get_response($template, $request->cfg, \%acts);
1168}
1169
1170sub edit_form {
1171 my ($self, $request, $article, $articles, $msg, $errors) = @_;
1172
1173 return $self->low_edit_form($request, $article, $articles, $msg, $errors);
1174}
1175
1176sub add_form {
9168c88c 1177 my ($self, $req, $articles, $msg, $errors) = @_;
ca9aa2bf
TC
1178
1179 my $level;
9168c88c 1180 my $cgi = $req->cgi;
ca9aa2bf
TC
1181 my $parentid = $cgi->param('parentid');
1182 if ($parentid) {
1183 if ($parentid =~ /^\d+$/) {
1184 if (my $parent = $self->get_parent($parentid, $articles)) {
1185 $level = $parent->{level}+1;
1186 }
1187 else {
1188 $parentid = undef;
1189 }
1190 }
1191 elsif ($parentid eq "-1") {
1192 $level = 1;
1193 }
1194 }
1195 unless (defined $level) {
1196 $level = $cgi->param('level');
1197 undef $level unless defined $level && $level =~ /^\d+$/
1198 && $level > 0 && $level < 100;
1199 defined $level or $level = 3;
1200 }
1201
1202 my %article;
1203 my @cols = Article->columns;
1204 @article{@cols} = ('') x @cols;
1205 $article{id} = '';
1206 $article{parentid} = $parentid;
1207 $article{level} = $level;
1208 $article{body} = '<maximum of 64Kb>';
1209 $article{listed} = 1;
1210 $article{generator} = $self->generator;
1211
9168c88c
TC
1212 my ($values, $labels) = $self->possible_parents(\%article, $articles, $req);
1213 @$values
a0a8147b
TC
1214 or return $self->edit_sections($req, $articles,
1215 "You can't add children to any article at that level");
9168c88c
TC
1216
1217 return $self->low_edit_form($req, \%article, $articles, $msg, $errors);
ca9aa2bf
TC
1218}
1219
1220sub generator { 'Generate::Article' }
1221
331fd099
TC
1222sub typename {
1223 my ($self) = @_;
1224
1225 my $gen = $self->generator;
1226
1227 ($gen =~ /(\w+)$/)[0] || 'Article';
1228}
1229
ca9aa2bf 1230sub _validate_common {
b553afa2 1231 my ($self, $data, $articles, $errors, $article) = @_;
ca9aa2bf 1232
918735d1
TC
1233# if (defined $data->{parentid} && $data->{parentid} =~ /^(?:-1|\d+)$/) {
1234# unless ($data->{parentid} == -1 or
1235# $articles->getByPkey($data->{parentid})) {
1236# $errors->{parentid} = "Selected parent article doesn't exist";
1237# }
1238# }
1239# else {
1240# $errors->{parentid} = "You need to select a valid parent";
1241# }
1242 if (exists $data->{title} && $data->{title} !~ /\S/) {
1243 $errors->{title} = "Please enter a title";
ca9aa2bf
TC
1244 }
1245
1246 if (exists $data->{template} && $data->{template} =~ /\.\./) {
1247 $errors->{template} = "Please only select templates from the list provided";
1248 }
1249
1250}
1251
1252sub validate {
918735d1 1253 my ($self, $data, $articles, $errors) = @_;
ca9aa2bf
TC
1254
1255 $self->_validate_common($data, $articles, $errors);
331fd099
TC
1256 custom_class($self->{cfg})
1257 ->article_validate($data, undef, $self->typename, $errors);
ca9aa2bf
TC
1258
1259 return !keys %$errors;
1260}
1261
1262sub validate_old {
918735d1 1263 my ($self, $article, $data, $articles, $errors) = @_;
ca9aa2bf 1264
b553afa2 1265 $self->_validate_common($data, $articles, $errors, $article);
331fd099
TC
1266 custom_class($self->{cfg})
1267 ->article_validate($data, $article, $self->typename, $errors);
ca9aa2bf 1268
b553afa2
TC
1269 if (exists $data->{release} && !valid_date($data->{release})) {
1270 $errors->{release} = "Invalid release date";
1271 }
1272
ca9aa2bf
TC
1273 return !keys %$errors;
1274}
1275
1276sub validate_parent {
1277 1;
1278}
1279
1280sub fill_new_data {
1281 my ($self, $req, $data, $articles) = @_;
1282
331fd099
TC
1283 custom_class($self->{cfg})
1284 ->article_fill_new($data, $self->typename);
1285
ca9aa2bf
TC
1286 1;
1287}
1288
95989433
TC
1289sub link_path {
1290 my ($self, $article) = @_;
1291
1292 # check the config for the article and any of its ancestors
1293 my $work_article = $article;
1294 my $path = $self->{cfg}->entry('article uris', $work_article->{id});
1295 while (!$path) {
1296 last if $work_article->{parentid} == -1;
1297 $work_article = $work_article->parent;
1298 $path = $self->{cfg}->entry('article uris', $work_article->{id});
1299 }
1300 return $path if $path;
1301
1302 $self->default_link_path($article);
1303}
1304
1305sub default_link_path {
1306 my ($self, $article) = @_;
1307
1308 $self->{cfg}->entry('uri', 'articles', '/a');
1309}
1310
ca9aa2bf
TC
1311sub make_link {
1312 my ($self, $article) = @_;
1313
95989433 1314 my $article_uri = $self->link_path($article);
ca9aa2bf
TC
1315 my $link = "$article_uri/$article->{id}.html";
1316 my $link_titles = $self->{cfg}->entryBool('basic', 'link_titles', 0);
1317 if ($link_titles) {
1318 (my $extra = lc $article->{title}) =~ tr/a-z0-9/_/sc;
d7538448 1319 $link .= "/" . $extra . "_html";
ca9aa2bf
TC
1320 }
1321
1322 $link;
1323}
1324
1325sub save_new {
1326 my ($self, $req, $articles) = @_;
1327
1328 my $cgi = $req->cgi;
1329 my %data;
1330 my $table_object = $self->table_object($articles);
1331 my @columns = $table_object->rowClass->columns;
1332 $self->save_thumbnail($cgi, undef, \%data);
1333 for my $name (@columns) {
9168c88c
TC
1334 $data{$name} = $cgi->param($name)
1335 if defined $cgi->param($name);
ca9aa2bf 1336 }
918735d1 1337 $data{flags} = join '', sort $cgi->param('flags');
ca9aa2bf
TC
1338
1339 my $msg;
1340 my %errors;
718a070d
TC
1341 if (!defined $data{parentid} || $data{parentid} eq '') {
1342 $errors{parentid} = "Please select a parent";
1343 }
1344 elsif ($data{parentid} !~ /^(?:-1|\d+)$/) {
1345 $errors{parentid} = "Invalid parent selection (template bug)";
1346 }
918735d1 1347 $self->validate(\%data, $articles, \%errors)
ca9aa2bf
TC
1348 or return $self->add_form($req, $articles, $msg, \%errors);
1349
1350 my $parent;
1351 if ($data{parentid} > 0) {
1352 $parent = $articles->getByPkey($data{parentid}) or die;
9168c88c
TC
1353 $req->user_can('edit_add_child', $parent)
1354 or return $self->add_form($req, $articles,
1355 "You cannot add a child to that article");
1356 for my $name (@columns) {
1357 if (exists $data{$name} &&
1358 !$req->user_can("edit_add_field_$name", $parent)) {
1359 delete $data{$name};
1360 }
1361 }
ca9aa2bf 1362 }
9168c88c
TC
1363 else {
1364 $req->user_can('edit_add_child')
1365 or return $self->add_form($req, $articles,
1366 "You cannot create a top-level article");
1367 for my $name (@columns) {
1368 if (exists $data{$name} &&
1369 !$req->user_can("edit_add_field_$name")) {
1370 delete $data{$name};
1371 }
1372 }
1373 }
1374
ca9aa2bf
TC
1375 $self->validate_parent(\%data, $articles, $parent, \$msg)
1376 or return $self->add_form($req, $articles, $msg);
1377
ca9aa2bf 1378 my $level = $parent ? $parent->{level}+1 : 1;
0ec4ac8a 1379 $data{level} = $level;
9168c88c 1380 $data{displayOrder} = time;
ca9aa2bf
TC
1381 $data{link} ||= '';
1382 $data{admin} ||= '';
ca9aa2bf 1383 $data{generator} = $self->generator;
41f10371 1384 $data{lastModified} = now_sqldatetime();
ca9aa2bf
TC
1385 $data{listed} = 1 unless defined $data{listed};
1386
9604a90c
TC
1387# Added by adrian
1388 $data{pageTitle} = '' unless defined $data{pageTitle};
1389 my $user = $req->getuser;
1390 $data{createdBy} = $user ? $user->{logon} : '';
1391 $data{lastModifiedBy} = $user ? $user->{logon} : '';
1392 $data{created} = now_sqldatetime();
1393
0ec4ac8a
TC
1394 $self->fill_new_data($req, \%data, $articles);
1395 for my $col (qw(titleImage imagePos template keyword)) {
1396 defined $data{$col}
1397 or $data{$col} = $self->default_value($req, \%data, $col);
1398 }
1399
718a070d
TC
1400 for my $col (qw(release expire)) {
1401 $data{$col} = sql_date($data{$col});
1402 }
1403
0ec4ac8a 1404 # these columns are handled a little differently
d7538448 1405 for my $col (qw(release expire threshold summaryLength )) {
0ec4ac8a
TC
1406 $data{$col}
1407 or $data{$col} = $self->default_value($req, \%data, $col);
1408 }
1409
ca9aa2bf
TC
1410 shift @columns;
1411 my $article = $table_object->add(@data{@columns});
1412
1413 # we now have an id - generate the links
1414
1415 my $cgi_uri = $self->{cfg}->entry('uri', 'cgi', '/cgi-bin');
1416 $article->setAdmin("$cgi_uri/admin/admin.pl?id=$article->{id}");
1417 $article->setLink($self->make_link($article));
1418 $article->save();
1419
caa7299c
TC
1420 use Util 'generate_article';
1421 generate_article($articles, $article) if $Constants::AUTO_GENERATE;
1422
8b0b2f34
TC
1423 my $r = $cgi->param('r');
1424 if ($r) {
1425 $r .= ($r =~ /\?/) ? '&' : '?';
1426 $r .= "id=$article->{id}";
1427 }
1428 else {
41f10371
TC
1429
1430 $r = admin_base_url($req->cfg) . $article->{admin};
8b0b2f34
TC
1431 }
1432 return BSE::Template->get_refresh($r, $self->{cfg});
1433
ca9aa2bf
TC
1434}
1435
1436sub fill_old_data {
0d5ccc7f 1437 my ($self, $req, $article, $data) = @_;
ca9aa2bf 1438
4010d92e
TC
1439 if (exists $data->{body}) {
1440 $data->{body} =~ s/\x0D\x0A/\n/g;
1441 $data->{body} =~ tr/\r/\n/;
1442 }
ca9aa2bf 1443 for my $col (Article->columns) {
331fd099 1444 next if $col =~ /^custom/;
ca9aa2bf
TC
1445 $article->{$col} = $data->{$col}
1446 if exists $data->{$col} && $col ne 'id' && $col ne 'parentid';
1447 }
331fd099
TC
1448 custom_class($self->{cfg})
1449 ->article_fill_old($article, $data, $self->typename);
ca9aa2bf
TC
1450
1451 return 1;
1452}
1453
1454sub save {
1455 my ($self, $req, $article, $articles) = @_;
4010d92e
TC
1456
1457 $req->user_can(edit_save => $article)
1458 or return $self->edit_form($req, $article, $articles,
1459 "You don't have access to save this article");
ca9aa2bf
TC
1460
1461 my $cgi = $req->cgi;
1462 my %data;
1463 for my $name ($article->columns) {
1464 $data{$name} = $cgi->param($name)
abf5bbc6
TC
1465 if defined($cgi->param($name)) and $name ne 'id' && $name ne 'parentid'
1466 && $req->user_can("edit_field_edit_$name", $article);
ca9aa2bf 1467 }
918735d1
TC
1468 # possibly this needs tighter error checking
1469 $data{flags} = join '', sort $cgi->param('flags')
1470 if $req->user_can("edit_field_edit_flags", $article);
ca9aa2bf
TC
1471 my %errors;
1472 $self->validate_old($article, \%data, $articles, \%errors)
1473 or return $self->edit_form($req, $article, $articles, undef, \%errors);
abf5bbc6
TC
1474 $self->save_thumbnail($cgi, $article, \%data)
1475 if $req->user_can('edit_field_edit_thumbImage', $article);
ca9aa2bf
TC
1476 $self->fill_old_data($req, $article, \%data);
1477 if (exists $article->{template} &&
1478 $article->{template} =~ m|\.\.|) {
1479 my $msg = "Please only select templates from the list provided";
1480 return $self->edit_form($req, $article, $articles, $msg);
1481 }
1482
1483 # reparenting
1484 my $newparentid = $cgi->param('parentid');
abf5bbc6
TC
1485 if ($newparentid && $req->user_can('edit_field_edit_parentid', $article)) {
1486 if ($newparentid == $article->{parentid}) {
1487 # nothing to do
1488 }
1489 elsif ($newparentid != -1) {
1490 print STDERR "Reparenting...\n";
1491 my $newparent = $articles->getByPkey($newparentid);
1492 if ($newparent) {
1493 if ($newparent->{level} != $article->{level}-1) {
1494 # the article cannot become a child of itself or one of it's
1495 # children
1496 if ($article->{id} == $newparentid
1497 || $self->is_descendant($article->{id}, $newparentid, $articles)) {
1498 my $msg = "Cannot become a child of itself or of a descendant";
1499 return $self->edit_form($req, $article, $articles, $msg);
1500 }
1501 my $shopid = $self->{cfg}->entryErr('articles', 'shop');
1502 if ($self->is_descendant($article->{id}, $shopid, $articles)) {
1503 my $msg = "Cannot become a descendant of the shop";
1504 return $self->edit_form($req, $article, $articles, $msg);
1505 }
1506 my $msg;
1507 $self->reparent($article, $newparentid, $articles, \$msg)
1508 or return $self->edit_form($req, $article, $articles, $msg);
ca9aa2bf 1509 }
abf5bbc6
TC
1510 else {
1511 # stays at the same level, nothing special
1512 $article->{parentid} = $newparentid;
ca9aa2bf 1513 }
ca9aa2bf 1514 }
abf5bbc6
TC
1515 # else ignore it
1516 }
1517 else {
1518 # becoming a section
1519 my $msg;
1520 $self->reparent($article, -1, $articles, \$msg)
1521 or return $self->edit_form($req, $article, $articles, $msg);
ca9aa2bf 1522 }
ca9aa2bf
TC
1523 }
1524
abf5bbc6
TC
1525 $article->{listed} = $cgi->param('listed')
1526 if defined $cgi->param('listed') &&
1527 $req->user_can('edit_field_edit_listed', $article);
1528 $article->{release} = sql_date($cgi->param('release'))
1529 if defined $cgi->param('release') &&
1530 $req->user_can('edit_field_edit_release', $article);
1531
1532 $article->{expire} = sql_date($cgi->param('expire')) || $Constants::D_99
1533 if defined $cgi->param('expire') &&
1534 $req->user_can('edit_field_edit_expire', $article);
41f10371 1535 $article->{lastModified} = now_sqldatetime();
95989433
TC
1536 if ($article->{link} &&
1537 !$self->{cfg}->entry('protect link', $article->{id})) {
1538 my $article_uri = $self->make_link($article);
95989433 1539 $article->setLink($article_uri);
ca9aa2bf
TC
1540 }
1541
9604a90c
TC
1542# Added by adrian
1543 my $user = $req->getuser;
1544 $article->{lastModifiedBy} = $user ? $user->{logon} : '';
1545
ca9aa2bf 1546 $article->save();
caa7299c
TC
1547
1548 use Util 'generate_article';
1549 generate_article($articles, $article) if $Constants::AUTO_GENERATE;
1550
8b0b2f34 1551 return $self->refresh($article, $cgi, undef, 'Article saved');
ca9aa2bf
TC
1552}
1553
1554sub sql_date {
1555 my $str = shift;
1556 my ($year, $month, $day);
1557
1558 # look for a date
1559 if (($day, $month, $year) = ($str =~ m!(\d+)/(\d+)/(\d+)!)) {
1560 $year += 2000 if $year < 100;
1561
1562 return sprintf("%04d-%02d-%02d", $year, $month, $day);
1563 }
1564 return undef;
1565}
1566
1567sub reparent {
1568 my ($self, $article, $newparentid, $articles, $rmsg) = @_;
1569
1570 my $newlevel;
1571 if ($newparentid == -1) {
1572 $newlevel = 1;
1573 }
1574 else {
1575 my $parent = $articles->getByPkey($newparentid);
1576 unless ($parent) {
1577 $$rmsg = "Cannot get new parent article";
1578 return;
1579 }
1580 $newlevel = $parent->{level} + 1;
1581 }
1582 # the caller will save this one
1583 $article->{parentid} = $newparentid;
1584 $article->{level} = $newlevel;
1585 $article->{displayOrder} = time;
1586
1587 my @change = ( [ $article->{id}, $newlevel ] );
1588 while (@change) {
1589 my $this = shift @change;
1590 my ($art, $level) = @$this;
1591
1592 my @kids = $articles->getBy(parentid=>$art);
1593 push @change, map { [ $_->{id}, $level+1 ] } @kids;
1594
1595 for my $kid (@kids) {
1596 $kid->{level} = $level+1;
1597 $kid->save;
1598 }
1599 }
1600
1601 return 1;
1602}
1603
1604# tests if $desc is a descendant of $art
1605# where both are article ids
1606sub is_descendant {
1607 my ($self, $art, $desc, $articles) = @_;
1608
1609 my @check = ($art);
1610 while (@check) {
1611 my $parent = shift @check;
1612 $parent == $desc and return 1;
1613 my @kids = $articles->getBy(parentid=>$parent);
1614 push @check, map $_->{id}, @kids;
1615 }
1616
1617 return 0;
1618}
1619
1620sub save_thumbnail {
1621 my ($self, $cgi, $original, $newdata) = @_;
1622
1623 unless ($original) {
1624 @$newdata{qw/thumbImage thumbWidth thumbHeight/} = ('', 0, 0);
1625 }
ab2cd916 1626 my $imagedir = cfg_image_dir($self->{cfg});
ca9aa2bf
TC
1627 if ($cgi->param('remove_thumb') && $original && $original->{thumbImage}) {
1628 unlink("$imagedir/$original->{thumbImage}");
1629 @$newdata{qw/thumbImage thumbWidth thumbHeight/} = ('', 0, 0);
1630 }
1631 my $image = $cgi->param('thumbnail');
1632 if ($image && -s $image) {
1633 # where to put it...
1634 my $name = '';
1635 $image =~ /([\w.-]+)$/ and $name = $1;
1636 my $filename = time . "_" . $name;
1637
1638 use Fcntl;
1639 my $counter = "";
1640 $filename = time . '_' . $counter . '_' . $name
1641 until sysopen( OUTPUT, "$imagedir/$filename",
1642 O_WRONLY| O_CREAT| O_EXCL)
1643 || ++$counter > 100;
1644
1645 fileno(OUTPUT) or die "Could not open image file: $!";
1646 binmode OUTPUT;
1647 my $buffer;
1648
1649 #no strict 'refs';
1650
1651 # read the image in from the browser and output it to our
1652 # output filehandle
1653 print STDERR "\$image ",ref $image,"\n";
1654 seek $image, 0, 0;
1655 print OUTPUT $buffer while sysread $image, $buffer, 1024;
1656
1657 close OUTPUT
1658 or die "Could not close image output file: $!";
1659
1660 use Image::Size;
1661
1662 if ($original && $original->{thumbImage}) {
1663 #unlink("$imagedir/$original->{thumbImage}");
1664 }
1665 @$newdata{qw/thumbWidth thumbHeight/} = imgsize("$imagedir/$filename");
1666 $newdata->{thumbImage} = $filename;
1667 }
1668}
1669
1670sub child_types {
1671 my ($self, $article) = @_;
1672
1673 my $shopid = $self->{cfg}->entryErr('articles', 'shop');
1674 if ($article && $article->{id} && $article->{id} == $shopid) {
1675 return ( 'BSE::Edit::Catalog' );
1676 }
1677 return ( 'BSE::Edit::Article' );
1678}
1679
1680sub add_stepkid {
1681 my ($self, $req, $article, $articles) = @_;
1682
31a26b52
TC
1683 $req->user_can(edit_stepkid_add => $article)
1684 or return $self->edit_form($req, $article, $articles,
1685 "You don't have access to add step children to this article");
1686
ca9aa2bf
TC
1687 my $cgi = $req->cgi;
1688 require 'BSE/Admin/StepParents.pm';
1689 eval {
1690 my $childId = $cgi->param('stepkid');
1691 defined $childId
1692 or die "No stepkid supplied to add_stepkid";
1693 $childId =~ /^\d+$/
1694 or die "Invalid stepkid supplied to add_stepkid";
1695 my $child = $articles->getByPkey($childId)
1696 or die "Article $childId not found";
31a26b52
TC
1697
1698 $req->user_can(edit_stepparent_add => $child)
1699 or die "You don't have access to add a stepparent to that article\n";
ca9aa2bf
TC
1700
1701 use BSE::Util::Valid qw/valid_date/;
1702 my $release = $cgi->param('release');
1703 valid_date($release) or $release = undef;
1704 my $expire = $cgi->param('expire');
1705 valid_date($expire) or $expire = undef;
1706
1707 my $newentry =
1708 BSE::Admin::StepParents->add($article, $child, $release, $expire);
1709 };
1710 if ($@) {
1711 return $self->edit_form($req, $article, $articles, $@);
1712 }
a0a8147b
TC
1713
1714 use Util 'generate_article';
1715 generate_article($articles, $article) if $Constants::AUTO_GENERATE;
1716
8b0b2f34 1717 return $self->refresh($article, $cgi, 'step', 'Stepchild added');
ca9aa2bf
TC
1718}
1719
1720sub del_stepkid {
1721 my ($self, $req, $article, $articles) = @_;
1722
31a26b52
TC
1723 $req->user_can(edit_stepkid_delete => $article)
1724 or return $self->edit_form($req, $article, $articles,
1725 "You don't have access to delete stepchildren from this article");
1726
ca9aa2bf
TC
1727 my $cgi = $req->cgi;
1728 require 'BSE/Admin/StepParents.pm';
1729 eval {
1730 my $childId = $cgi->param('stepkid');
1731 defined $childId
1732 or die "No stepkid supplied to add_stepkid";
1733 $childId =~ /^\d+$/
1734 or die "Invalid stepkid supplied to add_stepkid";
1735 my $child = $articles->getByPkey($childId)
1736 or die "Article $childId not found";
31a26b52
TC
1737
1738 $req->user_can(edit_stepparent_delete => $child)
1739 or die "You cannot remove stepparents from that article\n";
ca9aa2bf
TC
1740
1741 BSE::Admin::StepParents->del($article, $child);
1742 };
1743
1744 if ($@) {
1745 return $self->edit_form($req, $article, $articles, $@);
1746 }
a0a8147b
TC
1747 use Util 'generate_article';
1748 generate_article($articles, $article) if $Constants::AUTO_GENERATE;
1749
8b0b2f34 1750 return $self->refresh($article, $cgi, 'step', 'Stepchild deleted');
ca9aa2bf
TC
1751}
1752
1753sub save_stepkids {
1754 my ($self, $req, $article, $articles) = @_;
1755
31a26b52
TC
1756 $req->user_can(edit_stepkid_save => $article)
1757 or return $self->edit_form($req, $article, $articles,
1758 "No access to save stepkid data for this article");
1759
ca9aa2bf
TC
1760 my $cgi = $req->cgi;
1761 require 'BSE/Admin/StepParents.pm';
1762 my @stepcats = OtherParents->getBy(parentId=>$article->{id});
1763 my %stepcats = map { $_->{parentId}, $_ } @stepcats;
1764 my %datedefs = ( release => '2000-01-01', expire=>'2999-12-31' );
1765 for my $stepcat (@stepcats) {
31a26b52
TC
1766 $req->user_can(edit_stepparent_save => $stepcat->{childId})
1767 or next;
ca9aa2bf
TC
1768 for my $name (qw/release expire/) {
1769 my $date = $cgi->param($name.'_'.$stepcat->{childId});
1770 if (defined $date) {
1771 if ($date eq '') {
1772 $date = $datedefs{$name};
1773 }
1774 elsif (valid_date($date)) {
1775 use BSE::Util::SQL qw/date_to_sql/;
1776 $date = date_to_sql($date);
1777 }
1778 else {
1779 return $self->refresh($article, $cgi, '', "Invalid date '$date'");
1780 }
1781 $stepcat->{$name} = $date;
1782 }
1783 }
1784 eval {
1785 $stepcat->save();
1786 };
1787 $@ and return $self->refresh($article, $cgi, '', $@);
1788 }
a0a8147b
TC
1789 use Util 'generate_article';
1790 generate_article($articles, $article) if $Constants::AUTO_GENERATE;
1791
8b0b2f34 1792 return $self->refresh($article, $cgi, 'step', 'Stepchild information saved');
ca9aa2bf
TC
1793}
1794
1795sub add_stepparent {
1796 my ($self, $req, $article, $articles) = @_;
1797
31a26b52
TC
1798 $req->user_can(edit_stepparent_add => $article)
1799 or return $self->edit_form($req, $article, $articles,
1800 "You don't have access to add stepparents to this article");
1801
ca9aa2bf
TC
1802 my $cgi = $req->cgi;
1803 require 'BSE/Admin/StepParents.pm';
1804 eval {
1805 my $step_parent_id = $cgi->param('stepparent');
1806 defined($step_parent_id)
1807 or die "No stepparent supplied to add_stepparent";
1808 int($step_parent_id) eq $step_parent_id
1809 or die "Invalid stepcat supplied to add_stepcat";
1810 my $step_parent = $articles->getByPkey($step_parent_id)
31a26b52
TC
1811 or die "Parent $step_parent_id not found\n";
1812
1813 $req->user_can(edit_stepkid_add => $step_parent)
1814 or die "You don't have access to add a stepkid to that article\n";
ca9aa2bf
TC
1815
1816 my $release = $cgi->param('release');
1817 defined $release
1818 or $release = "01/01/2000";
1819 use BSE::Util::Valid qw/valid_date/;
1820 $release eq '' or valid_date($release)
1821 or die "Invalid release date";
1822 my $expire = $cgi->param('expire');
1823 defined $expire
1824 or $expire = '31/12/2999';
1825 $expire eq '' or valid_date($expire)
1826 or die "Invalid expire data";
1827
1828 my $newentry =
1829 BSE::Admin::StepParents->add($step_parent, $article, $release, $expire);
1830 };
1831 $@ and return $self->refresh($article, $cgi, 'step', $@);
1832
a0a8147b
TC
1833 use Util 'generate_article';
1834 generate_article($articles, $article) if $Constants::AUTO_GENERATE;
1835
8b0b2f34 1836 return $self->refresh($article, $cgi, 'stepparents', 'Stepparent added');
ca9aa2bf
TC
1837}
1838
1839sub del_stepparent {
1840 my ($self, $req, $article, $articles) = @_;
1841
31a26b52
TC
1842 $req->user_can(edit_stepparent_delete => $article)
1843 or return $self->edit_form($req, $article, $articles,
1844 "You cannot remove stepparents from that article");
1845
ca9aa2bf
TC
1846 my $cgi = $req->cgi;
1847 require 'BSE/Admin/StepParents.pm';
1848 my $step_parent_id = $cgi->param('stepparent');
1849 defined($step_parent_id)
1850 or return $self->refresh($article, $cgi, 'stepparents',
1851 "No stepparent supplied to add_stepcat");
1852 int($step_parent_id) eq $step_parent_id
1853 or return $self->refresh($article, $cgi, 'stepparents',
1854 "Invalid stepparent supplied to add_stepparent");
1855 my $step_parent = $articles->getByPkey($step_parent_id)
1856 or return $self->refresh($article, $cgi, 'stepparent',
1857 "Stepparent $step_parent_id not found");
1858
31a26b52
TC
1859 $req->user_can(edit_stepkid_delete => $step_parent)
1860 or die "You don't have access to remove the stepkid from that article\n";
1861
ca9aa2bf
TC
1862 eval {
1863 BSE::Admin::StepParents->del($step_parent, $article);
1864 };
1865 $@ and return $self->refresh($article, $cgi, 'stepparents', $@);
1866
a0a8147b
TC
1867 use Util 'generate_article';
1868 generate_article($articles, $article) if $Constants::AUTO_GENERATE;
1869
8b0b2f34 1870 return $self->refresh($article, $cgi, 'stepparents', 'Stepparent deleted');
ca9aa2bf
TC
1871}
1872
1873sub save_stepparents {
1874 my ($self, $req, $article, $articles) = @_;
1875
31a26b52
TC
1876 $req->user_can(edit_stepparent_save => $article)
1877 or return $self->edit_form($req, $article, $articles,
1878 "No access to save stepparent data for this artice");
1879
ca9aa2bf
TC
1880 my $cgi = $req->cgi;
1881
1882 require 'BSE/Admin/StepParents.pm';
1883 my @stepparents = OtherParents->getBy(childId=>$article->{id});
1884 my %stepparents = map { $_->{parentId}, $_ } @stepparents;
1885 my %datedefs = ( release => '2000-01-01', expire=>'2999-12-31' );
1886 for my $stepparent (@stepparents) {
31a26b52
TC
1887 $req->user_can(edit_stepkid_save => $stepparent->{parentId})
1888 or next;
ca9aa2bf
TC
1889 for my $name (qw/release expire/) {
1890 my $date = $cgi->param($name.'_'.$stepparent->{parentId});
1891 if (defined $date) {
1892 if ($date eq '') {
1893 $date = $datedefs{$name};
1894 }
1895 elsif (valid_date($date)) {
1896 use BSE::Util::SQL qw/date_to_sql/;
1897 $date = date_to_sql($date);
1898 }
1899 else {
1900 return $self->refresh($article, $cgi, "Invalid date '$date'");
1901 }
1902 $stepparent->{$name} = $date;
1903 }
1904 }
1905 eval {
1906 $stepparent->save();
1907 };
1908 $@ and return $self->refresh($article, $cgi, '', $@);
1909 }
1910
a0a8147b
TC
1911 use Util 'generate_article';
1912 generate_article($articles, $article) if $Constants::AUTO_GENERATE;
1913
8b0b2f34
TC
1914 return $self->refresh($article, $cgi, 'stepparents',
1915 'Stepparent information saved');
ca9aa2bf
TC
1916}
1917
1918sub refresh {
1919 my ($self, $article, $cgi, $name, $message, $extras) = @_;
1920
8b0b2f34 1921 my $url = $cgi->param('r');
16ac5598
TC
1922 if ($url) {
1923 if ($url !~ /[?&](m|message)=/ && $message) {
1924 # add in messages if none in the provided refresh
1925 my @msgs = ref $message ? @$message : $message;
1926 for my $msg (@msgs) {
1927 $url .= "&m=" . CGI::escape($msg);
1928 }
1929 }
1930 }
1931 else {
41f10371 1932 my $urlbase = admin_base_url($self->{cfg});
8b0b2f34 1933 $url = "$urlbase$ENV{SCRIPT_NAME}?id=$article->{id}";
16ac5598
TC
1934 if ($message) {
1935 my @msgs = ref $message ? @$message : $message;
1936 for my $msg (@msgs) {
1937 $url .= "&m=" . CGI::escape($msg);
1938 }
1939 }
8b0b2f34
TC
1940 if ($cgi->param('_t')) {
1941 $url .= "&_t=".CGI::escape($cgi->param('_t'));
1942 }
1943 $url .= $extras if defined $extras;
1944 my $cgiextras = $cgi->param('e');
1945 $url .= "#$name" if $name;
ca9aa2bf 1946 }
ca9aa2bf
TC
1947
1948 return BSE::Template->get_refresh($url, $self->{cfg});
1949}
1950
1951sub show_images {
918735d1 1952 my ($self, $req, $article, $articles, $msg, $errors) = @_;
ca9aa2bf
TC
1953
1954 my %acts;
918735d1 1955 %acts = $self->low_edit_tags(\%acts, $req, $article, $articles, $msg, $errors);
ca9aa2bf
TC
1956 my $template = 'admin/article_img';
1957
1958 return BSE::Template->get_response($template, $req->cfg, \%acts);
1959}
1960
1961sub save_image_changes {
1962 my ($self, $req, $article, $articles) = @_;
1963
abf5bbc6 1964 $req->user_can(edit_images_save => $article)
cc9019d1 1965 or return $self->edit_form($req, $article, $articles,
abf5bbc6
TC
1966 "You don't have access to save image information for this article");
1967
ca9aa2bf
TC
1968 my $cgi = $req->cgi;
1969 my $image_pos = $cgi->param('imagePos');
1970 if ($image_pos
1971 && $image_pos =~ /^(?:tl|tr|bl|br)$/
1972 && $image_pos ne $article->{imagePos}) {
1973 $article->{imagePos} = $image_pos;
1974 $article->save;
1975 }
daee3409 1976 my @images = $self->get_images($article);
4772671f
TC
1977
1978 @images or
1979 return $self->refresh($article, $cgi, undef, 'No images to save information for');
ca9aa2bf
TC
1980
1981 my $changed;
1982 my @alt = $cgi->param('alt');
1983 if (@alt) {
1984 ++$changed;
1985 for my $index (0..$#images) {
1986 $index < @alt or last;
1987 $images[$index]{alt} = $alt[$index];
1988 }
1989 }
1990 my @urls = $cgi->param('url');
1991 if (@urls) {
1992 ++$changed;
1993 for my $index (0..$#images) {
1994 $index < @urls or next;
1995 $images[$index]{url} = $urls[$index];
1996 }
1997 }
4772671f
TC
1998 my %errors;
1999 my @names = map scalar($cgi->param('name'.$_)), 0..$#images;
2000 if (@names) {
2001 # make sure there aren't any dups
2002 my %used;
2003 my $index = 0;
2004 for my $name (@names) {
2005 defined $name or $name = '';
2006 if ($name ne '') {
2007 if ($name =~ /^[a-z_]\w*$/i) {
2008 if ($used{lc $name}++) {
9604a90c 2009 $errors{"name$index"} = 'Image name must be empty or alphanumeric and unique to the article';
4772671f
TC
2010 }
2011 }
2012 else {
9604a90c 2013 $errors{"name$index"} = 'Image name must be unique to the article';
4772671f
TC
2014 }
2015 }
daee3409
TC
2016 unless ($errors{"name$index"}) {
2017 my $msg;
2018 $self->validate_image_name($name, \$msg)
2019 or $errors{"name$index"} = $msg;
2020 }
2021
4772671f
TC
2022 ++$index;
2023 }
2024 }
2025 keys %errors
2026 and return $self->edit_form($req, $article, $articles, undef,
2027 \%errors);
2028 for my $index (0..$#images) {
2029 $images[$index]{name} = $names[$index];
2030 }
ca9aa2bf
TC
2031 if ($changed) {
2032 for my $image (@images) {
2033 $image->save;
2034 }
2035 }
a0a8147b
TC
2036
2037 use Util 'generate_article';
2038 generate_article($articles, $article) if $Constants::AUTO_GENERATE;
2039
cc9019d1 2040 return $self->refresh($article, $cgi, undef, 'Image information saved');
ca9aa2bf
TC
2041}
2042
2043sub add_image {
2044 my ($self, $req, $article, $articles) = @_;
2045
abf5bbc6 2046 $req->user_can(edit_images_add => $article)
cc9019d1 2047 or return $self->edit_form($req, $article, $articles,
abf5bbc6
TC
2048 "You don't have access to add new images to this article");
2049
ca9aa2bf
TC
2050 my $cgi = $req->cgi;
2051
4772671f
TC
2052 my %errors;
2053 my $msg;
2054 my $imageref = $cgi->param('name');
d794b180 2055 if (defined $imageref && $imageref ne '') {
4772671f
TC
2056 if ($imageref =~ /^[a-z_]\w+$/i) {
2057 # make sure it's unique
daee3409 2058 my @images = $self->get_images($article);
4772671f
TC
2059 for my $img (@images) {
2060 if (defined $img->{name} && lc $img->{name} eq lc $imageref) {
9604a90c 2061 $errors{name} = 'Image name must be unique to the article';
4772671f
TC
2062 last;
2063 }
2064 }
2065 }
2066 else {
9604a90c 2067 $errors{name} = 'Image name must be empty or alphanumeric beginning with an alpha character';
4772671f
TC
2068 }
2069 }
2070 else {
2071 $imageref = '';
2072 }
daee3409 2073 unless ($errors{name}) {
d09682dd
TC
2074 my $workmsg;
2075 $self->validate_image_name($imageref, \$workmsg)
2076 or $errors{name} = $workmsg;
daee3409 2077 }
4772671f 2078
ca9aa2bf 2079 my $image = $cgi->param('image');
4772671f
TC
2080 if ($image) {
2081 if (-z $image) {
2082 $errors{image} = 'Image file is empty';
2083 }
ca9aa2bf 2084 }
4772671f 2085 else {
d09682dd 2086 #$msg = 'Enter or select the name of an image file on your machine';
4772671f
TC
2087 $errors{image} = 'Please enter an image filename';
2088 }
2089 if ($msg || keys %errors) {
2090 return $self->edit_form($req, $article, $articles, $msg, \%errors);
ca9aa2bf 2091 }
4772671f 2092
ca9aa2bf
TC
2093 my $imagename = $image;
2094 $imagename .= ''; # force it into a string
2095 my $basename = '';
2096 $imagename =~ /([\w.-]+)$/ and $basename = $1;
2097
2098 # create a filename that we hope is unique
2099 my $filename = time. '_'. $basename;
2100
2101 # for the sysopen() constants
2102 use Fcntl;
2103
ab2cd916 2104 my $imagedir = cfg_image_dir($req->cfg);
ca9aa2bf
TC
2105 # loop until we have a unique filename
2106 my $counter="";
2107 $filename = time. '_' . $counter . '_' . $basename
2108 until sysopen( OUTPUT, "$imagedir/$filename", O_WRONLY| O_CREAT| O_EXCL)
2109 || ++$counter > 100;
2110
2111 fileno(OUTPUT) or die "Could not open image file: $!";
2112
2113 # for OSs with special text line endings
2114 binmode OUTPUT;
2115
2116 my $buffer;
2117
2118 no strict 'refs';
2119
2120 # read the image in from the browser and output it to our output filehandle
2121 print OUTPUT $buffer while read $image, $buffer, 1024;
2122
2123 # close and flush
2124 close OUTPUT
2125 or die "Could not close image file $filename: $!";
2126
2127 use Image::Size;
2128
2129
2130 my($width,$height) = imgsize("$imagedir/$filename");
2131
2132 my $alt = $cgi->param('altIn');
2133 defined $alt or $alt = '';
2134 my $url = $cgi->param('url');
2135 defined $url or $url = '';
2136 my %image =
2137 (
2138 articleId => $article->{id},
2139 image => $filename,
2140 alt=>$alt,
2141 width=>$width,
2142 height => $height,
2143 url => $url,
2144 displayOrder=>time,
4772671f 2145 name => $imageref,
ca9aa2bf
TC
2146 );
2147 require Images;
2148 my @cols = Image->columns;
2149 shift @cols;
2150 my $imageobj = Images->add(@image{@cols});
a0a8147b
TC
2151
2152 use Util 'generate_article';
2153 generate_article($articles, $article) if $Constants::AUTO_GENERATE;
2154
cc9019d1 2155 return $self->refresh($article, $cgi, undef, 'New image added');
ca9aa2bf
TC
2156}
2157
2158# remove an image
2159sub remove_img {
2160 my ($self, $req, $article, $articles, $imageid) = @_;
2161
abf5bbc6 2162 $req->user_can(edit_images_delete => $article)
cc9019d1 2163 or return $self->edit_form($req, $article, $articles,
abf5bbc6
TC
2164 "You don't have access to delete images from this article");
2165
ca9aa2bf
TC
2166 $imageid or die;
2167
daee3409 2168 my @images = $self->get_images($article);
ca9aa2bf
TC
2169 my ($image) = grep $_->{id} == $imageid, @images
2170 or return $self->show_images($req, $article, $articles, "No such image");
ab2cd916 2171 my $imagedir = cfg_image_dir($req->cfg);
6473c56f 2172 unlink "$imagedir$image->{image}";
ca9aa2bf
TC
2173 $image->remove;
2174
a0a8147b
TC
2175 use Util 'generate_article';
2176 generate_article($articles, $article) if $Constants::AUTO_GENERATE;
2177
cc9019d1 2178 return $self->refresh($article, $req->cgi, undef, 'Image removed');
ca9aa2bf
TC
2179}
2180
2181sub move_img_up {
2182 my ($self, $req, $article, $articles) = @_;
2183
abf5bbc6 2184 $req->user_can(edit_images_reorder => $article)
cc9019d1 2185 or return $self->edit_form($req, $article, $articles,
abf5bbc6
TC
2186 "You don't have access to reorder images in this article");
2187
ca9aa2bf 2188 my $imageid = $req->cgi->param('imageid');
daee3409 2189 my @images = $self->get_images($article);
ca9aa2bf 2190 my ($imgindex) = grep $images[$_]{id} == $imageid, 0..$#images
cc9019d1 2191 or return $self->edit_form($req, $article, $articles, "No such image");
ca9aa2bf 2192 $imgindex > 0
cc9019d1 2193 or return $self->edit_form($req, $article, $articles, "Image is already at the top");
ca9aa2bf
TC
2194 my ($to, $from) = @images[$imgindex-1, $imgindex];
2195 ($to->{displayOrder}, $from->{displayOrder}) =
2196 ($from->{displayOrder}, $to->{displayOrder});
2197 $to->save;
2198 $from->save;
2199
a0a8147b
TC
2200 use Util 'generate_article';
2201 generate_article($articles, $article) if $Constants::AUTO_GENERATE;
2202
cc9019d1 2203 return $self->refresh($article, $req->cgi, undef, 'Image moved');
ca9aa2bf
TC
2204}
2205
2206sub move_img_down {
2207 my ($self, $req, $article, $articles) = @_;
2208
abf5bbc6 2209 $req->user_can(edit_images_reorder => $article)
cc9019d1 2210 or return $self->edit_form($req, $article, $articles,
abf5bbc6
TC
2211 "You don't have access to reorder images in this article");
2212
ca9aa2bf 2213 my $imageid = $req->cgi->param('imageid');
daee3409 2214 my @images = $self->get_images($article);
ca9aa2bf 2215 my ($imgindex) = grep $images[$_]{id} == $imageid, 0..$#images
cc9019d1 2216 or return $self->edit_form($req, $article, $articles, "No such image");
ca9aa2bf 2217 $imgindex < $#images
cc9019d1 2218 or return $self->edit_form($req, $article, $articles, "Image is already at the end");
ca9aa2bf
TC
2219 my ($to, $from) = @images[$imgindex+1, $imgindex];
2220 ($to->{displayOrder}, $from->{displayOrder}) =
2221 ($from->{displayOrder}, $to->{displayOrder});
2222 $to->save;
2223 $from->save;
2224
a0a8147b
TC
2225 use Util 'generate_article';
2226 generate_article($articles, $article) if $Constants::AUTO_GENERATE;
2227
cc9019d1 2228 return $self->refresh($article, $req->cgi, undef, 'Image moved');
ca9aa2bf
TC
2229}
2230
ab2cd916
TC
2231sub req_thumb {
2232 my ($self, $req, $article) = @_;
2233
2234 my $cgi = $req->cgi;
2235 my $cfg = $req->cfg;
2236 my $im_id = $cgi->param('im');
2237 my $image;
2238 if (defined $im_id && $im_id =~ /^\d+$/) {
6a8a6ac5 2239 ($image) = grep $_->{id} == $im_id, $self->get_images($article);
ab2cd916
TC
2240 }
2241 my $thumb_obj = $self->_get_thumbs_class();
2242 my ($data, $type);
2243 if ($image && $thumb_obj) {
2244 my $width = $cgi->param('w');
2245 my $height = $cgi->param('h');
2246 my $pixels = $cgi->param('p');
2247 my $imagedir = $cfg->entry('paths', 'images', $Constants::IMAGEDIR);
2248
2249 ($type, $data) = $thumb_obj->
2250 thumb_data("$imagedir/$image->{image}", $image, $width, $height,
2251 $pixels);
2252 }
2253
2254 if ($type && $data) {
2255
2256 return
2257 {
2258 type => $type,
2259 content => $data,
2260 headers => [
2261 "Content-Length: ".length($data),
2262 "Cache-Control: max-age=3600",
2263 ],
2264 };
2265 }
2266 else {
2267 # grab the nothumb image
2268 my $uri = $cfg->entry('editor', 'default_thumbnail', '/images/admin/nothumb.png');
2269 my $filebase = $Constants::CONTENTBASE;
2270 if (open IMG, "<$filebase/$uri") {
2271 binmode IMG;
2272 my $data = do { local $/; <IMG> };
2273 close IMG;
2274 my $type = $uri =~ /\.(\w+)$/ ? $1 : 'png';
2275 return
2276 {
2277 type => "image/$type",
2278 content => $data,
2279 headers => [ "Content-Length: ".length($data) ],
2280 };
2281 }
2282 else {
2283 return
2284 {
2285 type=>"text/html",
2286 content => "<html><body>Cannot make thumb or default image</body></html>",
2287 };
2288 }
2289 }
2290}
2291
ca9aa2bf
TC
2292sub get_article {
2293 my ($self, $articles, $article) = @_;
2294
2295 return $article;
2296}
2297
2298sub table_object {
2299 my ($self, $articles) = @_;
2300
2301 $articles;
2302}
2303
2304my %types =
2305 (
2306 qw(
6a8a6ac5
TC
2307 bash text/plain
2308 css text/css
2309 csv text/plain
2310 diff text/plain
ca9aa2bf
TC
2311 htm text/html
2312 html text/html
6a8a6ac5
TC
2313 ics text/calendar
2314 patch text/plain
2315 pl text/plain
2316 pm text/plain
2317 pod text/plain
2318 py text/plain
ad48b8d4
TC
2319 sgm text/sgml
2320 sgml text/sgml
6a8a6ac5
TC
2321 sh text/plain
2322 tcsh text/plain
2323 text text/plain
ad48b8d4 2324 tsv text/tab-separated-values
6a8a6ac5
TC
2325 txt text/plain
2326 vcf text/x-vcard
2327 vcs text/x-vcalendar
2328 xml text/xml
2329 zsh text/x-script.zsh
2330 bmp image/bmp
ca9aa2bf 2331 gif image/gif
ad48b8d4 2332 jp2 image/jpeg2000
6a8a6ac5
TC
2333 jpeg image/jpeg
2334 jpg image/jpeg
2335 pct image/pict
2336 pict image/pict
ca9aa2bf 2337 png image/png
ca9aa2bf
TC
2338 tif image/tiff
2339 tiff image/tiff
6a8a6ac5
TC
2340 Z application/x-compress
2341 dcr application/x-director
2342 dir application/x-director
2343 doc application/msword
2344 dxr application/x-director
ad48b8d4 2345 eps application/postscript
6a8a6ac5
TC
2346 fla application/x-shockwave-flash
2347 gz application/gzip
2348 hqx application/mac-binhex40
2349 js application/x-javascript
2350 lzh application/x-lzh
2351 pdf application/pdf
2352 pps application/ms-powerpoint
2353 ppt application/ms-powerpoint
ad48b8d4 2354 ps application/postscript
ad48b8d4 2355 rtf application/rtf
6a8a6ac5
TC
2356 sit application/x-stuffit
2357 swf application/x-shockwave-flash
ad48b8d4
TC
2358 tar application/x-tar
2359 tgz application/gzip
ad48b8d4 2360 xls application/ms-excel
6a8a6ac5
TC
2361 zip application/zip
2362 asf video/x-ms-asf
2363 avi video/avi
2364 flc video/flc
ad48b8d4 2365 moov video/quicktime
6a8a6ac5 2366 mov video/quicktime
41f10371 2367 mp4 video/mp4
ad48b8d4 2368 mpeg video/mpeg
6a8a6ac5 2369 mpg video/mpeg
ad48b8d4 2370 wmv video/x-ms-wmv
6a8a6ac5 2371 aa audio/audible
ad48b8d4
TC
2372 aif audio/aiff
2373 aiff audio/aiff
6a8a6ac5
TC
2374 m4a audio/m4a
2375 mid audio/midi
2376 mp2 audio/x-mpeg
2377 mp3 audio/x-mpeg
ad48b8d4
TC
2378 ra audio/x-realaudio
2379 ram audio/x-pn-realaudio
2380 rm audio/vnd.rm-realmedia
6a8a6ac5 2381 swa audio/mp3
ad48b8d4
TC
2382 wav audio/wav
2383 wma audio/x-ms-wma
6a8a6ac5 2384 3gp audio/3gpp
ca9aa2bf
TC
2385 )
2386 );
2387
2388sub _refresh_filelist {
8b0b2f34 2389 my ($self, $req, $article, $msg) = @_;
ca9aa2bf 2390
cc9019d1 2391 return $self->refresh($article, $req->cgi, undef, $msg);
ca9aa2bf
TC
2392}
2393
2394sub filelist {
918735d1 2395 my ($self, $req, $article, $articles, $msg, $errors) = @_;
ca9aa2bf
TC
2396
2397 my %acts;
918735d1 2398 %acts = $self->low_edit_tags(\%acts, $req, $article, $articles, $msg, $errors);
ca9aa2bf
TC
2399 my $template = 'admin/filelist';
2400
2401 return BSE::Template->get_response($template, $req->cfg, \%acts);
2402}
2403
2404sub fileadd {
2405 my ($self, $req, $article, $articles) = @_;
2406
abf5bbc6 2407 $req->user_can(edit_files_add => $article)
cc9019d1 2408 or return $self->edit_form($req, $article, $articles,
abf5bbc6
TC
2409 "You don't have access to add files to this article");
2410
ca9aa2bf
TC
2411 my %file;
2412 my $cgi = $req->cgi;
2413 require ArticleFile;
2414 my @cols = ArticleFile->columns;
2415 shift @cols;
2416 for my $col (@cols) {
2417 if (defined $cgi->param($col)) {
2418 $file{$col} = $cgi->param($col);
2419 }
2420 }
2421
2422 $file{forSale} = 0 + exists $file{forSale};
2423 $file{articleId} = $article->{id};
2424 $file{download} = 0 + exists $file{download};
2425 $file{requireUser} = 0 + exists $file{requireUser};
2426
2427 my $downloadPath = $self->{cfg}->entryVar('paths', 'downloads');
2428
2429 # build a filename
2430 my $file = $cgi->param('file');
2431 unless ($file) {
cc9019d1 2432 return $self->edit_form($req, $article, $articles,
918735d1
TC
2433 "Enter or select the name of a file on your machine",
2434 { file => 'Please enter a filename' });
ca9aa2bf
TC
2435 }
2436 if (-z $file) {
cc9019d1 2437 return $self->edit_form($req, $article, $articles,
918735d1
TC
2438 "File is empty",
2439 { file => 'File is empty' });
ca9aa2bf
TC
2440 }
2441
2442 unless ($file{contentType}) {
2443 unless ($file =~ /\.([^.]+)$/) {
2444 $file{contentType} = "application/octet-stream";
2445 }
2446 unless ($file{contentType}) {
2447 my $ext = lc $1;
2448 my $type = $types{$ext};
2449 unless ($type) {
2450 $type = $self->{cfg}->entry('extensions', $ext)
2451 || $self->{cfg}->entry('extensions', ".$ext")
2452 || "application/octet-stream";
2453 }
2454 $file{contentType} = $type;
2455 }
2456 }
2457
2458 my $basename = '';
6a8a205a
TC
2459 my $workfile = $file;
2460 $workfile =~ s![^\w.:/\\-]+!_!g;
2461 $workfile =~ tr/_/_/s;
2462 $workfile =~ /([ \w.-]+)$/ and $basename = $1;
f0543260 2463 $basename =~ tr/ /_/;
ca9aa2bf
TC
2464
2465 my $filename = time. '_'. $basename;
2466
2467 # for the sysopen() constants
2468 use Fcntl;
2469
2470 # loop until we have a unique filename
2471 my $counter="";
2472 $filename = time. '_' . $counter . '_' . $basename
2473 until sysopen( OUTPUT, "$downloadPath/$filename",
2474 O_WRONLY| O_CREAT| O_EXCL)
2475 || ++$counter > 100;
2476
2477 fileno(OUTPUT) or die "Could not open file: $!";
2478
2479 # for OSs with special text line endings
2480 binmode OUTPUT;
2481
2482 my $buffer;
2483
2484 no strict 'refs';
2485
2486 # read the image in from the browser and output it to our output filehandle
2487 print OUTPUT $buffer while read $file, $buffer, 8192;
2488
2489 # close and flush
2490 close OUTPUT
2491 or die "Could not close file $filename: $!";
2492
2493 use BSE::Util::SQL qw/now_datetime/;
2494 $file{filename} = $filename;
2495 $file{displayName} = $basename;
2496 $file{sizeInBytes} = -s $file;
2497 $file{displayOrder} = time;
2498 $file{whenUploaded} = now_datetime();
2499
2500 require ArticleFiles;
2501 my $fileobj = ArticleFiles->add(@file{@cols});
2502
a0a8147b
TC
2503 use Util 'generate_article';
2504 generate_article($articles, $article) if $Constants::AUTO_GENERATE;
2505
8b0b2f34 2506 $self->_refresh_filelist($req, $article, 'New file added');
ca9aa2bf
TC
2507}
2508
2509sub fileswap {
2510 my ($self, $req, $article, $articles) = @_;
2511
abf5bbc6 2512 $req->user_can('edit_files_reorder', $article)
cc9019d1 2513 or return $self->edit_form($req, $article, $articles,
abf5bbc6
TC
2514 "You don't have access to reorder files in this article");
2515
ca9aa2bf
TC
2516 my $cgi = $req->cgi;
2517 my $id1 = $cgi->param('file1');
2518 my $id2 = $cgi->param('file2');
2519
2520 if ($id1 && $id2) {
2521 my @files = $article->files;
2522
2523 my ($file1) = grep $_->{id} == $id1, @files;
2524 my ($file2) = grep $_->{id} == $id2, @files;
2525
2526 if ($file1 && $file2) {
2527 ($file1->{displayOrder}, $file2->{displayOrder})
2528 = ($file2->{displayOrder}, $file1->{displayOrder});
2529 $file1->save;
2530 $file2->save;
2531 }
2532 }
2533
a0a8147b
TC
2534 use Util 'generate_article';
2535 generate_article($articles, $article) if $Constants::AUTO_GENERATE;
2536
9063386f 2537 $self->refresh($article, $req->cgi, undef, 'File moved');
ca9aa2bf
TC
2538}
2539
2540sub filedel {
2541 my ($self, $req, $article, $articles) = @_;
2542
abf5bbc6 2543 $req->user_can('edit_files_delete', $article)
cc9019d1
TC
2544 or return $self->edit_form($req, $article, $articles,
2545 "You don't have access to delete files from this article");
abf5bbc6 2546
ca9aa2bf
TC
2547 my $cgi = $req->cgi;
2548 my $fileid = $cgi->param('file');
2549 if ($fileid) {
2550 my @files = $article->files;
2551
2552 my ($file) = grep $_->{id} == $fileid, @files;
2553
2554 if ($file) {
2555 my $downloadPath = $req->cfg->entryErr('paths', 'downloads');
2556 my $filename = $downloadPath . "/" . $file->{filename};
2557 my $debug_del = $req->cfg->entryBool('debug', 'file_unlink', 0);
2558 if ($debug_del) {
2559 unlink $filename
2560 or print STDERR "Error deleting $filename: $!\n";
2561 }
2562 else {
2563 unlink $filename;
2564 }
2565 $file->remove();
2566 }
2567 }
2568
a0a8147b
TC
2569 use Util 'generate_article';
2570 generate_article($articles, $article) if $Constants::AUTO_GENERATE;
2571
8b0b2f34 2572 $self->_refresh_filelist($req, $article, 'File deleted');
ca9aa2bf
TC
2573}
2574
2575sub filesave {
abf5bbc6 2576 my ($self, $req, $article, $articles) = @_;
ca9aa2bf 2577
abf5bbc6 2578 $req->user_can('edit_files_save', $article)
cc9019d1 2579 or return $self->edit_form($req, $article, $articles,
abf5bbc6 2580 "You don't have access to save file information for this article");
ca9aa2bf
TC
2581 my @files = $article->files;
2582
2583 my $cgi = $req->cgi;
2584 for my $file (@files) {
2585 if (defined $cgi->param("description_$file->{id}")) {
2586 $file->{description} = $cgi->param("description_$file->{id}");
2587 if (my $type = $cgi->param("contentType_$file->{id}")) {
2588 $file->{contentType} = $type;
2589 }
2590 $file->{download} = 0 + defined $cgi->param("download_$file->{id}");
2591 $file->{forSale} = 0 + defined $cgi->param("forSale_$file->{id}");
2592 $file->{requireUser} = 0 + defined $cgi->param("requireUser_$file->{id}");
2593 $file->save;
2594 }
2595 }
2596
a0a8147b
TC
2597 use Util 'generate_article';
2598 generate_article($articles, $article) if $Constants::AUTO_GENERATE;
2599
8b0b2f34 2600 $self->_refresh_filelist($req, $article, 'File information saved');
ca9aa2bf
TC
2601}
2602
6473c56f
TC
2603sub can_remove {
2604 my ($self, $req, $article, $articles, $rmsg) = @_;
2605
abf5bbc6
TC
2606 unless ($req->user_can('edit_delete_article', $article, $rmsg)) {
2607 $$rmsg ||= "Access denied";
2608 return;
2609 }
2610
6473c56f
TC
2611 if ($articles->children($article->{id})) {
2612 $$rmsg = "This article has children. You must delete the children first (or change their parents)";
2613 return;
2614 }
2615 if (grep $_ == $article->{id}, @Constants::NO_DELETE) {
2616 $$rmsg = "Sorry, these pages are essential to the site structure - they cannot be deleted";
2617 return;
2618 }
2619 if ($article->{id} == $Constants::SHOPID) {
2620 $$rmsg = "Sorry, these pages are essential to the store - they cannot be deleted - you may want to hide the the store instead.";
2621 return;
2622 }
2623
2624 return 1;
2625}
2626
2627sub remove {
2628 my ($self, $req, $article, $articles) = @_;
2629
2630 my $why_not;
2631 unless ($self->can_remove($req, $article, $articles, \$why_not)) {
2632 return $self->edit_form($req, $article, $articles, $why_not);
2633 }
2634
2635 require Images;
2636 my @images = Images->getBy(articleId=>$article->{id});
2637 my $imagedir = $self->{cfg}->entry('paths', 'images', $Constants::IMAGEDIR);
2638 for my $image (@images) {
2639 unlink("$imagedir/$image->{image}");
2640 $image->remove();
2641 }
2642
2643 # remove any step(child|parent) links
2644 require OtherParents;
2645 my @steprels = OtherParents->anylinks($article->{id});
2646 for my $link (@steprels) {
2647 $link->remove();
2648 }
2649
2650 my $parentid = $article->{parentid};
2651 $article->remove;
8b0b2f34
TC
2652 my $url = $req->cgi->param('r');
2653 unless ($url) {
41f10371 2654 my $urlbase = admin_base_url($req->cfg);
8b0b2f34
TC
2655 $url = "$urlbase$ENV{SCRIPT_NAME}?id=$parentid";
2656 $url .= "&message=Article+deleted";
2657 }
6473c56f
TC
2658 return BSE::Template->get_refresh($url, $self->{cfg});
2659}
2660
4010d92e
TC
2661sub unhide {
2662 my ($self, $req, $article, $articles) = @_;
2663
2664 if ($req->user_can(edit_field_edit_listed => $article)
2665 && $req->user_can(edit_save => $article)) {
2666 $article->{listed} = 1;
2667 $article->save;
2668
2669 use Util 'generate_article';
2670 generate_article($articles, $article) if $Constants::AUTO_GENERATE;
2671 }
8b0b2f34 2672 return $self->refresh($article, $req->cgi, undef, 'Article unhidden');
4010d92e
TC
2673}
2674
2675sub hide {
2676 my ($self, $req, $article, $articles) = @_;
2677
2678 if ($req->user_can(edit_field_edit_listed => $article)
2679 && $req->user_can(edit_save => $article)) {
2680 $article->{listed} = 0;
2681 $article->save;
2682
2683 use Util 'generate_article';
2684 generate_article($articles, $article) if $Constants::AUTO_GENERATE;
2685 }
2686 my $r = $req->cgi->param('r');
2687 unless ($r) {
41f10371
TC
2688 $r = admin_base_url($req->cfg)
2689 . "/cgi-bin/admin/add.pl?id=" . $article->{parentid};
4010d92e 2690 }
8b0b2f34 2691 return $self->refresh($article, $req->cgi, undef, 'Article hidden');
4010d92e
TC
2692}
2693
0ec4ac8a
TC
2694my %defaults =
2695 (
2696 titleImage => '',
2697 imagePos => 'tr',
2698 expire => $Constants::D_99,
2699 listed => 1,
2700 keyword => '',
5d88571c 2701 body => '<maximum of 64Kb>',
0ec4ac8a
TC
2702 );
2703
9168c88c
TC
2704sub default_value {
2705 my ($self, $req, $article, $col) = @_;
2706
2707 if ($article->{parentid}) {
2708 my $section = "children of $article->{parentid}";
2709 my $value = $req->cfg->entry($section, $col);
2710 if (defined $value) {
77804754 2711 return $value;
9168c88c
TC
2712 }
2713 }
2714 my $section = "level $article->{level}";
2715 my $value = $req->cfg->entry($section, $col);
77804754 2716 defined($value) and return $value;
0ec4ac8a 2717
deae2a52
TC
2718 $value = $self->type_default_value($req, $col);
2719 defined $value and return $value;
2720
0ec4ac8a
TC
2721 exists $defaults{$col} and return $defaults{$col};
2722
2723 $col eq 'release' and return now_sqldate();
2724
2725 if ($col eq 'threshold') {
d44b5da9 2726 my $parent = defined $article->{parentid} && $article->{parentid} != -1
0ec4ac8a
TC
2727 && Articles->getByPkey($article->{parentid});
2728
2729 $parent and return $parent->{threshold};
2730
2731 return 5;
2732 }
9168c88c 2733
0ec4ac8a 2734 if ($col eq 'summaryLength') {
5d88571c 2735 my $parent = defined $article->{parentid} && $article->{parentid} != -1
0ec4ac8a
TC
2736 && Articles->getByPkey($article->{parentid});
2737
2738 $parent and return $parent->{summaryLength};
2739
2740 return 200;
2741 }
2742
2743 return;
9168c88c
TC
2744}
2745
deae2a52
TC
2746sub type_default_value {
2747 my ($self, $req, $col) = @_;
2748
2749 return $req->cfg->entry('article defaults', $col);
2750}
2751
918735d1
TC
2752sub flag_sections {
2753 return ( 'article flags' );
2754}
2755
2756sub flags {
2757 my ($self) = @_;
2758
2759 my $cfg = $self->{cfg};
2760
2761 my @sections = $self->flag_sections;
2762
2763 my %flags = map $cfg->entriesCS($_), reverse @sections;
2764 my @valid = grep /^\w$/, keys %flags;
2765
2766 return map +{ id => $_, desc => $flags{$_} },
2767 sort { lc($flags{$a}) cmp lc($flags{$b}) }@valid;
2768}
2769
daee3409
TC
2770sub get_images {
2771 my ($self, $article) = @_;
2772
2773 $article->images;
2774}
2775
2776sub validate_image_name {
2777 my ($self, $name, $rmsg) = @_;
2778
2779 1; # no extra validation
2780}
2781
ca9aa2bf
TC
27821;
2783
2784=head1 NAME
2785
2786 BSE::Edit::Article - editing functionality for BSE articles
2787
2788=head1 AUTHOR
2789
2790Tony Cook <tony@develop-help.com>
2791
2792=head1 REVISION
2793
2794$Revision$
2795
2796=cut