0.12_14 commit
[bse.git] / site / cgi-bin / modules / BSE / Edit / Article.pm
CommitLineData
ca9aa2bf
TC
1package BSE::Edit::Article;
2use strict;
3use HTML::Entities;
4use base qw(BSE::Edit::Base);
5use BSE::Util::Tags;
6use BSE::Util::SQL qw(now_sqldate);
9168c88c 7use BSE::Permissions;
ca9aa2bf
TC
8
9sub article_dispatch {
9168c88c
TC
10 my ($self, $req, $article, $articles) = @_;
11
12 BSE::Permissions->check_logon($req)
13 or return BSE::Template->get_refresh($req->url('logon'), $req->cfg);
14
15 my $cgi = $req->cgi;
ca9aa2bf
TC
16 my $action;
17 my %actions = $self->article_actions;
18 for my $check (keys %actions) {
19 if ($cgi->param($check) || $cgi->param("$check.x")) {
20 $action = $check;
21 last;
22 }
23 }
24 my @extraargs;
25 unless ($action) {
26 ($action, @extraargs) = $self->other_article_actions($cgi);
27 }
28 $action ||= 'edit';
29 my $method = $actions{$action};
9168c88c 30 return $self->$method($req, $article, $articles, @extraargs);
ca9aa2bf
TC
31}
32
33sub noarticle_dispatch {
9168c88c 34 my ($self, $req, $articles) = @_;
ca9aa2bf 35
9168c88c
TC
36 BSE::Permissions->check_logon($req)
37 or return BSE::Template->get_refresh($req->url('logon'), $req->cfg);
38
39 my $cgi = $req->cgi;
ca9aa2bf
TC
40 my $action = 'add';
41 my %actions = $self->noarticle_actions;
42 for my $check (keys %actions) {
43 if ($cgi->param($check) || $cgi->param("$check.x")) {
44 $action = $check;
45 last;
46 }
47 }
48 my $method = $actions{$action};
9168c88c 49 return $self->$method($req, $articles);
ca9aa2bf
TC
50}
51
52sub edit_sections {
53 my ($self, $req, $articles) = @_;
54
9168c88c
TC
55 BSE::Permissions->check_logon($req)
56 or return BSE::Template->get_refresh($req->url('logon'), $req->cfg);
57
ca9aa2bf
TC
58 my %article;
59 my @cols = Article->columns;
60 @article{@cols} = ('') x @cols;
61 $article{id} = '-1';
62 $article{parentid} = -1;
63 $article{level} = 0;
64 $article{body} = '';
65 $article{listed} = 0;
66 $article{generator} = $self->generator;
67
68 return $self->low_edit_form($req, \%article, $articles);
69}
70
71sub article_actions {
72 my ($self) = @_;
73
74 return
75 (
76 edit => 'edit_form',
77 save => 'save',
78 add_stepkid => 'add_stepkid',
79 del_stepkid => 'del_stepkid',
80 save_stepkids => 'save_stepkids',
81 add_stepparent => 'add_stepparent',
82 del_stepparent => 'del_stepparent',
83 save_stepparents => 'save_stepparents',
84 artimg => 'save_image_changes',
85 addimg => 'add_image',
6473c56f 86 remove => 'remove',
ca9aa2bf
TC
87 showimages => 'show_images',
88 process => 'save_image_changes',
89 removeimg => 'remove_img',
90 moveimgup => 'move_img_up',
91 moveimgdown => 'move_img_down',
92 filelist => 'filelist',
93 fileadd => 'fileadd',
94 fileswap => 'fileswap',
95 filedel => 'filedel',
96 filesave => 'filesave',
4010d92e
TC
97 hide => 'hide',
98 unhide => 'unhide',
ca9aa2bf
TC
99 );
100}
101
102sub other_article_actions {
103 my ($self, $cgi) = @_;
104
105 for my $param ($cgi->param) {
106 if ($param =~ /^removeimg_(\d+)(\.x)?$/) {
107 return ('removeimg', $1 );
108 }
109 }
110
111 return;
112}
113
114sub noarticle_actions {
115 return
116 (
117 add => 'add_form',
118 save => 'save_new',
119 );
120}
121
122sub get_parent {
123 my ($self, $parentid, $articles) = @_;
124
125 if ($parentid == -1) {
126 return
127 {
128 id => -1,
129 title=>'All Sections',
130 level => 0,
131 listed => 0,
132 parentid => undef,
133 };
134 }
135 else {
136 return $articles->getByPkey($parentid);
137 }
138}
139
140sub tag_hash {
141 my ($object, $args) = @_;
142
143 my $value = $object->{$args};
144 defined $value or $value = '';
145 encode_entities($value);
146}
147
148sub tag_art_type {
149 my ($level, $cfg) = @_;
150
151 encode_entities($cfg->entry('level names', $level, 'Article'));
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 }
245 return $cgi->popup_menu(-name=>'listed',
246 -values=>\@values,
247 -labels=>\%labels,
248 -default=>$article->{listed});
249 }
250 else {
9168c88c 251 my ($values, $labels) = $self->possible_parents($article, $articles, $req);
ca9aa2bf
TC
252 my $html;
253 if (defined $article->{parentid}) {
254 $html = $cgi->popup_menu(-name=>'parentid',
255 -values=> $values,
256 -labels => $labels,
257 -default => $article->{parentid},
258 -override=>1);
259 }
260 else {
261 $html = $cgi->popup_menu(-name=>'parentid',
262 -values=> $values,
263 -labels => $labels,
264 -override=>1);
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 {
281 my ($article) = @_;
282
283 $article->{id} or return;
284 $article->images;
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
TC
342 }
343 return $cgi->popup_menu(-name=>'template',
344 -values=>\@templates,
345 -default=>$default,
346 -override=>1);
347}
348
349sub title_images {
350 my ($self, $article) = @_;
351
352 my @title_images;
353 my $imagedir = $self->{cfg}->entry('paths', 'images', $Constants::IMAGEDIR);
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;
412 my $basedir = $self->{cfg}->entry('paths', 'templates', $Constants::TMPLDIR);
413 for my $dir (@dirs) {
414 my $path = File::Spec->catdir($basedir, $dir);
415 if (-d $path) {
416 if (opendir TEMPLATE_DIR, $path) {
417 push(@templates, sort map "$dir/$_",
418 grep -f "$path/$_" && /\.(tmpl|html)$/i, readdir TEMPLATE_DIR);
419 closedir TEMPLATE_DIR;
420 }
421 }
422 }
423 return (@templates, $self->extra_templates($article));
424}
425
426sub extra_templates {
427 my ($self, $article) = @_;
428
429 my $basedir = $self->{cfg}->entry('paths', 'templates', $Constants::TMPLDIR);
430 my @templates;
431 if (my $id = $article->{id}) {
432 push @templates, 'index.tmpl'
433 if $id == 1 && -f "$basedir/index.html";
434 push @templates, 'index2.tmpl'
435 if $id == 2 && -f "$basedir/index2.html";
436 my $shopid = $self->{cfg}->entryErr('articles', 'shop');
437 push @templates, "shop_sect.tmpl"
438 if $id == $shopid && -f "$basedir/shop_sect.tmpl";
439 my $section = "article $id";
440 my $extras = $self->{cfg}->entry($section, 'extra_templates');
441 push @templates, grep /\.(tmpl|html)$/i, split /,/, $extras
442 if $extras;
443 }
444
445 @templates;
446}
447
448sub edit_parent {
449 my ($article) = @_;
450
451 return '' unless $article->{id} && $article->{id} != -1;
452 return <<HTML;
453<a href="$ENV{SCRIPT_NAME}?id=$article->{parentid}">Edit parent</a> |
454HTML
455}
456
457sub iter_allkids {
458 my ($article) = @_;
459
460 return unless $article->{id} && $article->{id} > 0;
461 $article->allkids;
462}
463
464sub _load_step_kids {
465 my ($article, $step_kids) = @_;
466
467 my @stepkids = OtherParents->getBy(parentId=>$article->{id}) if $article->{id};
468 %$step_kids = map { $_->{childId} => $_ } @stepkids;
469 use Data::Dumper;
470 print STDERR "stepkids:\n", Dumper($step_kids);
471 $step_kids->{loaded} = 1;
472}
473
474sub tag_if_step_kid {
475 my ($article, $allkids, $rallkid_index, $step_kids) = @_;
476
477 _load_step_kids($article, $step_kids) unless $step_kids->{loaded};
478
479 my $kid = $allkids->[$$rallkid_index]
480 or return;
481 exists $step_kids->{$kid->{id}};
482}
483
484sub tag_step_kid {
485 my ($article, $allkids, $rallkid_index, $step_kids, $arg) = @_;
486
487 _load_step_kids($article, $step_kids) unless $step_kids->{loaded};
488
489 my $kid = $allkids->[$$rallkid_index]
490 or return '';
491 print STDERR "found kid (want $arg): ", Dumper $kid;
492 encode_entities($step_kids->{$kid->{id}}{$arg});
493}
494
495sub tag_move_stepkid {
496 my ($self, $cgi, $article, $allkids, $rallkids_index) = @_;
497
498 my $cgi_uri = $self->{cfg}->entry('uri', 'cgi', '/cgi-bin');
499 my $images_uri = $self->{cfg}->entry('uri', 'images', '/images');
500 my $html = '';
501 my $url = $ENV{SCRIPT_NAME} . "?id=$article->{id}";
502 if ($cgi->param('_t')) {
503 $url .= "&_t=".$cgi->param('_t');
504 }
505 $url .= "#step";
506 my $refreshto = CGI::escape($url);
507 if ($$rallkids_index < $#$allkids) {
508 $html .= <<HTML;
509<a href="$cgi_uri/admin/move.pl?stepparent=$article->{id}&d=swap&id=$allkids->[$$rallkids_index]{id}&other=$allkids->[$$rallkids_index+1]{id}&refreshto=$refreshto"><img src="$images_uri/admin/move_down.gif" width="17" height="13" border="0" alt="Move Down" align="absbottom"></a>
510HTML
511 }
512 if ($$rallkids_index > 0) {
513 $html .= <<HTML;
514<a href="$cgi_uri/admin/move.pl?stepparent=$article->{id}&d=swap&id=$allkids->[$$rallkids_index]{id}&other=$allkids->[$$rallkids_index-1]{id}&refreshto=$refreshto"><img src="$images_uri/admin/move_up.gif" width="17" height="13" border="0" alt="Move Up" align="absbottom"></a>
515HTML
516 }
517 return $html;
518}
519
520sub possible_stepkids {
521 my ($articles, $stepkids) = @_;
522
523 return sort { lc $a->{title} cmp lc $b->{title} }
524 grep !$stepkids->{$_->{id}}, $articles->all;
525}
526
527
528
529sub tag_possible_stepkids {
530 my ($step_kids, $article, $possstepkids, $articles, $cgi) = @_;
531
532 _load_step_kids($article, $step_kids) unless $step_kids->{loaded};
533 @$possstepkids = possible_stepkids($articles, $step_kids)
534 unless @$possstepkids;
535 my %labels = map { $_->{id} => "$_->{title} ($_->{id})" } @$possstepkids;
536 return
537 $cgi->popup_menu(-name=>'stepkid',
538 -values=> [ map $_->{id}, @$possstepkids ],
539 -labels => \%labels);
540}
541
542sub tag_if_possible_stepkids {
543 my ($step_kids, $article, $possstepkids, $articles, $cgi) = @_;
544
545 _load_step_kids($article, $step_kids) unless $step_kids->{loaded};
546 @$possstepkids = possible_stepkids($articles, $step_kids)
547 unless @$possstepkids;
548
549 @$possstepkids;
550}
551
552sub iter_get_stepparents {
553 my ($article) = @_;
554
555 return unless $article->{id} && $article->{id} > 0;
556
557 OtherParents->getBy(childId=>$article->{id});
558}
559
560sub tag_ifStepParents {
561 my ($args, $acts, $funcname, $templater) = @_;
562
563 return $templater->perform($acts, 'ifStepparents', '');
564}
565
566sub tag_stepparent_targ {
567 my ($article, $targs, $rindex, $arg) = @_;
568
569 if ($article->{id} && $article->{id} > 0 && !@$targs) {
570 @$targs = $article->step_parents;
571 }
572 encode_entities($targs->[$$rindex]{$arg});
573}
574
575sub tag_move_stepparent {
576 my ($self, $cgi, $article, $stepparents, $rindex) = @_;
577
578 my $cgi_uri = $self->{cfg}->entry('uri', 'cgi', '/cgi-bin');
579 my $images_uri = $self->{cfg}->entry('uri', 'images', '/images');
580 my $html = '';
581 my $url = $ENV{SCRIPT_NAME} . "?id=$article->{id}";
582 if ($cgi->param('_t')) {
583 $url .= "&_t=".$cgi->param('_t');
584 }
585 $url .= "#stepparents";
586 my $refreshto = CGI::escape($url);
587 if ($$rindex < $#$stepparents) {
588 $html .= <<HTML;
589<a href="$cgi_uri/admin/move.pl?stepchild=$article->{id}&id=$stepparents->[$$rindex]{parentId}&d=swap&other=$stepparents->[$$rindex+1]{parentId}&refreshto=$refreshto&all=1"><img src="$images_uri/admin/move_down.gif" width="17" height="13" border="0" alt="Move Down" align="absbottom"></a>
590HTML
591 }
592 if ($$rindex > 0) {
593 $html .= <<HTML;
594<a href="$cgi_uri/admin/move.pl?stepchild=$article->{id}&id=$stepparents->[$$rindex]{parentId}&d=swap&other=$stepparents->[$$rindex-1]{parentId}&refreshto=$refreshto&all=1"><img src="$images_uri/admin/move_up.gif" width="17" height="13" border="0" alt="Move Up" align="absbottom"></a>
595HTML
596 }
597 return $html;
598}
599
600sub tag_if_stepparent_possibles {
601 my ($article, $articles, $targs, $possibles) = @_;
602
603 if ($article->{id} && $article->{id} > 0) {
604 @$targs = $article->step_parents unless @$targs;
605 my %targs = map { $_->{id}, 1 } @$targs;
606 @$possibles = grep !$targs{$_->{id}}, $articles->all;
607 }
608 scalar @$possibles;
609}
610
611sub tag_stepparent_possibles {
612 my ($cgi, $article, $articles, $targs, $possibles) = @_;
613
614 if ($article->{id} && $article->{id} > 0) {
615 @$targs = $article->step_parents unless @$targs;
616 my %targs = map { $_->{id}, 1 } @$targs;
617 @$possibles = sort { lc $a->{title} cmp lc $b->{title} }
618 grep !$targs{$_->{id}}, $articles->all;
619 }
620 $cgi->popup_menu(-name=>'stepparent',
621 -values => [ map $_->{id}, @$possibles ],
622 -labels => { map { $_->{id}, "$_->{title} ($_->{id})" }
623 @$possibles });
624}
625
626sub iter_files {
627 my ($article) = @_;
628
629 return unless $article->{id} && $article->{id} > 0;
630
631 return $article->files;
632}
633
634sub tag_edit_parent {
635 my ($article) = @_;
636
637 return '' unless $article->{id} && $article->{id} != -1;
638
639 return <<HTML;
640<a href="$ENV{SCRIPT_NAME}?id=$article->{parentid}">Edit parent</a> |
641HTML
642}
643
644sub tag_if_children {
645 my ($args, $acts, $funcname, $templater) = @_;
646
647 return $templater->perform($acts, 'ifChildren', '');
648}
649
650sub tag_movechild {
abf5bbc6
TC
651 my ($self, $req, $article, $kids, $rindex) = @_;
652
653 $req->user_can('edit_reorder_children', $article)
654 or return '';
ca9aa2bf
TC
655
656 $$rindex >=0 && $$rindex < @$kids
657 or return '** movechild can only be used in the children iterator **';
658
659 my $cgi_uri = $self->{cfg}->entry('uri', 'cgi', '/cgi-bin');
660 my $images_uri = $self->{cfg}->entry('uri', 'images', '/images');
661 my $html = '';
662 my $nomove = '<img src="/images/trans_pixel.gif" width="17" height="13" border="0" alt="" align="absbottom">';
663 my $id = $kids->[$$rindex]{id};
664 if ($$rindex < $#$kids) {
665 $html .= <<HTML;
666<a href="$cgi_uri/admin/move.pl?id=$id&d=down&edit=1&all=1"><img src="$images_uri/admin/move_down.gif" width="17" height="13" alt="Move Down" border="0" align="absbottom"></a>
667HTML
668 }
669 else {
670 $html .= $nomove;
671 }
672 if ($$rindex > 0) {
673 $html .= <<HTML;
674<a href="$cgi_uri/admin/move.pl?id=$id&d=up&edit=1&all=1"><img src="$images_uri/admin/move_up.gif" width="17" height="13" alt="Move Up" border="0" align="absbottom"></a>
675HTML
676 }
677 else {
678 $html .= $nomove;
679 }
680 $html =~ tr/\n//d;
681
682 $html;
683}
684
685sub tag_edit_link {
686 my ($args, $acts, $funcname, $templater) = @_;
687 my ($which, $name) = split / /, $args, 2;
688 $name ||= 'Edit';
689 my $gen_class;
690 if ($acts->{$which}
691 && ($gen_class = $templater->perform($acts, $which, 'generator'))) {
692 eval "use $gen_class";
693 unless ($@) {
694 my $gen = $gen_class->new;
695 my $link = $gen->edit_link($templater->perform($acts, $which, 'id'));
696 return qq!<a href="$link">$name</a>!;
697 }
698 }
699 return '';
700}
701
702sub tag_imgmove {
abf5bbc6
TC
703 my ($req, $article, $rindex, $images) = @_;
704
705 $req->user_can(edit_images_reorder => $article)
706 or return '';
ca9aa2bf
TC
707
708 $$rindex >= 0 && $$rindex < @$images
709 or return '** imgmove can only be used in image iterator **';
710
711 my $html = '';
712 my $nomove = '<img src="/images/trans_pixel.gif" width="17" height="13" border="0" alt="" align="absbottom">';
713 my $image = $images->[$$rindex];
714 if ($$rindex > 0) {
715 $html .= <<HTML
716<a href="$ENV{SCRIPT_NAME}?id=$article->{id}&moveimgup=1&imageid=$image->{id}"><img src="/images/admin/move_up.gif" width="17" height="13" border="0" alt="Move Up" align="absbottom"></a>
717HTML
718 }
719 else {
720 $html .= $nomove;
721 }
722 if ($$rindex < $#$images) {
723 $html .= <<HTML
724<a href="$ENV{SCRIPT_NAME}?id=$article->{id}&moveimgdown=1&imageid=$image->{id}"><img src="/images/admin/move_down.gif" width="17" height="13" border="0" alt="Move Down" align="absbottom"></a>
725HTML
726 }
727 else {
728 $html .= $nomove;
729 }
730 return $html;
731}
732
733sub tag_movefiles {
abf5bbc6
TC
734 my ($self, $req, $article, $files, $rindex) = @_;
735
736 $req->user_can('edit_files_reorder', $article)
737 or return '';
ca9aa2bf
TC
738
739 my $html = '';
740
741 $$rindex >= 0 && $$rindex < @$files
742 or return '** movefiles can only be used in the files iterator **';
743
744 my $nomove = '<img src="/images/trans_pixel.gif" width="17" height="13" border="0" alt="" align="absbottom">';
745 my $images_uri = $self->{cfg}->entry('uri', 'images', '/images');
746
747 if ($$rindex < $#$files) {
748 $html .= <<HTML;
749<a href="$ENV{SCRIPT_NAME}?fileswap=1&id=$article->{id}&file1=$files->[$$rindex]{id}&file2=$files->[$$rindex+1]{id}"><img src="$images_uri/admin/move_down.gif" width="17" height="13" border="0" alt="Move Down" align="absbottom"></a>
750HTML
751 }
752 else {
753 $html .= $nomove;
754 }
755 if ($$rindex > 0) {
756 $html .= <<HTML;
757<a href="$ENV{SCRIPT_NAME}?fileswap=1&id=$article->{id}&file1=$files->[$$rindex]{id}&file2=$files->[$$rindex-1]{id}"><img src="$images_uri/admin/move_up.gif" width="17" height="13" border="0" alt="Move Up" align="absbottom"></a>
758HTML
759 }
760 else {
761 $html .= $nomove;
762 }
763 $html =~ tr/\n//d;
764 $html;
765}
766
767sub tag_old {
768 my ($article, $cgi, $args, $acts, $funcname, $templater) = @_;
769
770 my ($col, $func, $funcargs) = split ' ', $args, 3;
771 my $value = $cgi->param($col);
772 if (defined $value) {
773 return encode_entities($value);
774 }
775 else {
776 if ($func) {
777 return $templater->perform($acts, $func, $funcargs);
778 }
779 else {
780 $value = $article->{$args};
781 defined $value or $value = '';
782 return encode_entities($value);
783 }
784 }
785}
786
787sub tag_error_img {
788 my ($self, $errors, $args) = @_;
789
790 return '' unless $errors->{$args};
791 my $images_uri = $self->{cfg}->entry('uri', 'images', '/images');
792 my $encoded = encode_entities($errors->{$args});
793 return qq!<img src="$images_uri/admin/error.gif" alt="$encoded" title="$encoded" border="0" align="top">!;
794}
795
08123550
TC
796sub iter_admin_users {
797 require BSE::TB::AdminUsers;
798
799 BSE::TB::AdminUsers->all;
800}
801
802sub iter_admin_groups {
803 require BSE::TB::AdminGroups;
804
805 BSE::TB::AdminGroups->all;
806}
807
9168c88c
TC
808sub tag_if_field_perm {
809 my ($req, $article, $field) = @_;
810
abf5bbc6
TC
811 unless ($field =~ /^\w+$/) {
812 print STDERR "Bad fieldname '$field'\n";
813 return;
814 }
9168c88c 815 if ($article->{id}) {
4010d92e 816 print STDERR "checking field $field\n";
abf5bbc6 817 return $req->user_can("edit_field_edit_$field", $article);
9168c88c
TC
818 }
819 else {
4010d92e 820 #print STDERR "adding, always successful\n";
abf5bbc6 821 return 1;
9168c88c
TC
822 }
823}
824
825sub tag_default {
826 my ($self, $req, $article, $args, $acts, $funcname, $templater) = @_;
827
828 my ($col, $func, $funcargs) = split ' ', $args, 3;
829 if ($article->{id}) {
830 if ($func) {
831 return $templater->perform($acts, $func, $funcargs);
832 }
833 else {
834 my $value = $article->{$args};
835 defined $value or $value = '';
836 return encode_entities($value);
837 }
838 }
839 else {
840 my $value = $self->default_value($req, $article, $col);
841 return encode_entities($value);
842 }
843}
844
ca9aa2bf
TC
845sub low_edit_tags {
846 my ($self, $acts, $request, $article, $articles, $msg, $errors) = @_;
847
848 my $cgi = $request->cgi;
6473c56f 849 $msg ||= $cgi->param('message');
ca9aa2bf
TC
850 $msg ||= '';
851 $errors ||= {};
852 if (keys %$errors && !$msg) {
853 # try to get the errors in the same order as the table
854 my @cols = $self->table_object($articles)->rowClass->columns;
855 my %work = %$errors;
856 my @out = grep defined, delete @work{@cols};
857
858 $msg = join "<br>", @out, values %work;
859 }
abf5bbc6
TC
860 my $parent;
861 if ($article->{id}) {
862 if ($article->{parentid} > 0) {
863 $parent = $article->parent;
864 }
865 else {
866 $parent = { title=>"No parent - this is a section", id=>-1 };
867 }
868 }
869 else {
870 $parent = { title=>"How did we get here?", id=>0 };
871 }
ca9aa2bf
TC
872 my @images;
873 my $image_index;
874 my @children;
875 my $child_index;
876 my %stepkids;
877 my $cfg = $self->{cfg};
878 my @allkids;
879 my $allkid_index;
880 my @possstepkids;
881 my @stepparents;
882 my $stepparent_index;
883 my @stepparent_targs;
884 my @stepparentpossibles;
885 my @files;
886 my $file_index;
887 return
888 (
889 BSE::Util::Tags->basic($acts, $cgi, $cfg),
890 BSE::Util::Tags->admin($acts, $cfg),
9168c88c 891 BSE::Util::Tags->secure($request),
ca9aa2bf
TC
892 article => [ \&tag_hash, $article ],
893 old => [ \&tag_old, $article, $cgi ],
9168c88c 894 default => [ \&tag_default, $self, $request, $article ],
ca9aa2bf
TC
895 articleType => [ \&tag_art_type, $article->{level}, $cfg ],
896 parentType => [ \&tag_art_type, $article->{level}-1, $cfg ],
897 ifnew => [ \&tag_if_new, $article ],
9168c88c 898 list => [ \&tag_list, $self, $article, $articles, $cgi, $request ],
ca9aa2bf
TC
899 script => $ENV{SCRIPT_NAME},
900 level => $article->{level},
901 checked => \&tag_checked,
902 DevHelp::Tags->make_iterator2
903 ([ \&iter_get_images, $article ], 'image', 'images', \@images,
904 \$image_index),
abf5bbc6 905 imgmove => [ \&tag_imgmove, $request, $article, \$image_index, \@images ],
ca9aa2bf
TC
906 message => $msg,
907 DevHelp::Tags->make_iterator2
908 ([ \&iter_get_kids, $article, $articles ],
909 'child', 'children', \@children, \$child_index),
910 ifchildren => \&tag_if_children,
911 childtype => [ \&tag_art_type, $article->{level}+1, $cfg ],
912 ifHaveChildType => [ \&tag_if_have_child_type, $article->{level}, $cfg ],
abf5bbc6
TC
913 movechild => [ \&tag_movechild, $self, $request, $article, \@children,
914 \$child_index],
ca9aa2bf
TC
915 is => \&tag_is,
916 templates => [ \&tag_templates, $self, $article, $cfg, $cgi ],
917 titleImages => [ \&tag_title_images, $self, $article, $cfg, $cgi ],
918 editParent => [ \&tag_edit_parent, $article ],
919 DevHelp::Tags->make_iterator2
920 ([ \&iter_allkids, $article ], 'kid', 'kids', \@allkids, \$allkid_index),
921 ifStepKid =>
922 [ \&tag_if_step_kid, $article, \@allkids, \$allkid_index, \%stepkids ],
923 stepkid => [ \&tag_step_kid, $article, \@allkids, \$allkid_index,
924 \%stepkids ],
925 movestepkid =>
926 [ \&tag_move_stepkid, $self, $cgi, $article, \@allkids, \$allkid_index ],
927 possible_stepkids =>
928 [ \&tag_possible_stepkids, \%stepkids, $article, \@possstepkids,
929 $articles, $cgi ],
930 ifPossibles =>
931 [ \&tag_if_possible_stepkids, \%stepkids, $article, \@possstepkids,
932 $articles, $cgi ],
933 DevHelp::Tags->make_iterator2
934 ( [ \&iter_get_stepparents, $article ], 'stepparent', 'stepparents',
935 \@stepparents, \$stepparent_index),
936 ifStepParents => \&tag_ifStepParents,
937 stepparent_targ =>
938 [ \&tag_stepparent_targ, $article, \@stepparent_targs,
939 \$stepparent_index ],
940 movestepparent =>
941 [ \&tag_move_stepparent, $self, $cgi, $article, \@stepparents,
942 \$stepparent_index ],
943 ifStepparentPossibles =>
944 [ \&tag_if_stepparent_possibles, $article, $articles, \@stepparent_targs,
945 \@stepparentpossibles, ],
946 stepparent_possibles =>
947 [ \&tag_stepparent_possibles, $cgi, $article, $articles,
948 \@stepparent_targs, \@stepparentpossibles, ],
949 DevHelp::Tags->make_iterator2
950 ([ \&iter_files, $article ], 'file', 'files', \@files, \$file_index ),
abf5bbc6
TC
951 movefiles =>
952 [ \&tag_movefiles, $self, $request, $article, \@files, \$file_index ],
08123550
TC
953 DevHelp::Tags->make_iterator2
954 (\&iter_admin_users, 'iadminuser', 'adminusers'),
955 DevHelp::Tags->make_iterator2
956 (\&iter_admin_groups, 'iadmingroup', 'admingroups'),
ca9aa2bf
TC
957 edit => \&tag_edit_link,
958 error => [ \&tag_hash, $errors ],
959 error_img => [ \&tag_error_img, $self, $errors ],
9168c88c 960 ifFieldPerm => [ \&tag_if_field_perm, $request, $article ],
abf5bbc6 961 parent => [ \&tag_hash, $parent ],
ca9aa2bf
TC
962 );
963}
964
965sub edit_template {
966 my ($self, $article, $cgi) = @_;
967
968 my $base = $article->{level};
969 my $t = $cgi->param('_t');
970 if ($t && $t =~ /^\w+$/) {
971 $base = $t;
972 }
973 return $self->{cfg}->entry('admin templates', $base,
974 "admin/edit_$base");
975}
976
977sub add_template {
978 my ($self, $article, $cgi) = @_;
979
980 $self->edit_template($article, $cgi);
981}
982
983sub low_edit_form {
984 my ($self, $request, $article, $articles, $msg, $errors) = @_;
985
986 my $cgi = $request->cgi;
987 my %acts;
988 %acts = $self->low_edit_tags(\%acts, $request, $article, $articles, $msg,
989 $errors);
990 my $template = $article->{id} ?
991 $self->edit_template($article, $cgi) : $self->add_template($article, $cgi);
992
993 return BSE::Template->get_response($template, $request->cfg, \%acts);
994}
995
996sub edit_form {
997 my ($self, $request, $article, $articles, $msg, $errors) = @_;
998
999 return $self->low_edit_form($request, $article, $articles, $msg, $errors);
1000}
1001
1002sub add_form {
9168c88c 1003 my ($self, $req, $articles, $msg, $errors) = @_;
ca9aa2bf
TC
1004
1005 my $level;
9168c88c 1006 my $cgi = $req->cgi;
ca9aa2bf
TC
1007 my $parentid = $cgi->param('parentid');
1008 if ($parentid) {
1009 if ($parentid =~ /^\d+$/) {
1010 if (my $parent = $self->get_parent($parentid, $articles)) {
1011 $level = $parent->{level}+1;
1012 }
1013 else {
1014 $parentid = undef;
1015 }
1016 }
1017 elsif ($parentid eq "-1") {
1018 $level = 1;
1019 }
1020 }
1021 unless (defined $level) {
1022 $level = $cgi->param('level');
1023 undef $level unless defined $level && $level =~ /^\d+$/
1024 && $level > 0 && $level < 100;
1025 defined $level or $level = 3;
1026 }
1027
1028 my %article;
1029 my @cols = Article->columns;
1030 @article{@cols} = ('') x @cols;
1031 $article{id} = '';
1032 $article{parentid} = $parentid;
1033 $article{level} = $level;
1034 $article{body} = '<maximum of 64Kb>';
1035 $article{listed} = 1;
1036 $article{generator} = $self->generator;
1037
9168c88c
TC
1038 my ($values, $labels) = $self->possible_parents(\%article, $articles, $req);
1039 @$values
1040 or return $req->access_error("You can't add children to any article at that level");
1041
1042 return $self->low_edit_form($req, \%article, $articles, $msg, $errors);
ca9aa2bf
TC
1043}
1044
1045sub generator { 'Generate::Article' }
1046
1047sub _validate_common {
1048 my ($self, $data, $articles, $errors) = @_;
1049
1050 if (defined $data->{parentid} && $data->{parentid} =~ /^(?:-1|\d+)$/) {
1051 unless ($data->{parentid} == -1 or
1052 $articles->getByPkey($data->{parentid})) {
1053 $errors->{parentid} = "Selected parent article doesn't exist";
1054 }
1055 }
1056 else {
1057 $errors->{parentid} = "You need to select a valid parent";
1058 }
1059
1060 if (exists $data->{template} && $data->{template} =~ /\.\./) {
1061 $errors->{template} = "Please only select templates from the list provided";
1062 }
1063
1064}
1065
1066sub validate {
1067 my ($self, $data, $articles, $rmsg, $errors) = @_;
1068
1069 $self->_validate_common($data, $articles, $errors);
1070
1071 return !keys %$errors;
1072}
1073
1074sub validate_old {
15fb10f2 1075 my ($self, $article, $data, $articles, $rmsg, $errors) = @_;
ca9aa2bf
TC
1076
1077 $self->_validate_common($data, $articles, $errors);
1078
1079 return !keys %$errors;
1080}
1081
1082sub validate_parent {
1083 1;
1084}
1085
1086sub fill_new_data {
1087 my ($self, $req, $data, $articles) = @_;
1088
1089 1;
1090}
1091
1092sub make_link {
1093 my ($self, $article) = @_;
1094
1095 my $article_uri = $self->{cfg}->entry('uri', 'articles', '/a');
1096 my $link = "$article_uri/$article->{id}.html";
1097 my $link_titles = $self->{cfg}->entryBool('basic', 'link_titles', 0);
1098 if ($link_titles) {
1099 (my $extra = lc $article->{title}) =~ tr/a-z0-9/_/sc;
1100 $link .= "/".$extra;
1101 }
1102
1103 $link;
1104}
1105
1106sub save_new {
1107 my ($self, $req, $articles) = @_;
1108
1109 my $cgi = $req->cgi;
1110 my %data;
1111 my $table_object = $self->table_object($articles);
1112 my @columns = $table_object->rowClass->columns;
1113 $self->save_thumbnail($cgi, undef, \%data);
1114 for my $name (@columns) {
9168c88c
TC
1115 $data{$name} = $cgi->param($name)
1116 if defined $cgi->param($name);
ca9aa2bf
TC
1117 }
1118
1119 my $msg;
1120 my %errors;
1121 $self->validate(\%data, $articles, \$msg, \%errors)
1122 or return $self->add_form($req, $articles, $msg, \%errors);
1123
1124 my $parent;
1125 if ($data{parentid} > 0) {
1126 $parent = $articles->getByPkey($data{parentid}) or die;
9168c88c
TC
1127 $req->user_can('edit_add_child', $parent)
1128 or return $self->add_form($req, $articles,
1129 "You cannot add a child to that article");
1130 for my $name (@columns) {
1131 if (exists $data{$name} &&
1132 !$req->user_can("edit_add_field_$name", $parent)) {
1133 delete $data{$name};
1134 }
1135 }
ca9aa2bf 1136 }
9168c88c
TC
1137 else {
1138 $req->user_can('edit_add_child')
1139 or return $self->add_form($req, $articles,
1140 "You cannot create a top-level article");
1141 for my $name (@columns) {
1142 if (exists $data{$name} &&
1143 !$req->user_can("edit_add_field_$name")) {
1144 delete $data{$name};
1145 }
1146 }
1147 }
1148
ca9aa2bf
TC
1149 $self->validate_parent(\%data, $articles, $parent, \$msg)
1150 or return $self->add_form($req, $articles, $msg);
1151
1152 $self->fill_new_data($req, \%data, $articles);
1153 my $level = $parent ? $parent->{level}+1 : 1;
9168c88c 1154 $data{displayOrder} = time;
ca9aa2bf
TC
1155 $data{titleImage} ||= '';
1156 $data{imagePos} = 'tr';
1157 $data{release} = sql_date($data{release}) || now_sqldate();
1158 $data{expire} = sql_date($data{expire}) || $Constants::D_99;
1159 unless ($data{template}) {
1160 $data{template} ||=
1161 $self->{cfg}->entry("children of $data{parentid}", 'template');
1162 $data{template} ||=
1163 $self->{cfg}->entry("level $level", 'template');
1164 }
1165 $data{link} ||= '';
1166 $data{admin} ||= '';
1167 if ($parent) {
1168 $data{threshold} = $parent->{threshold}
1169 if !defined $data{threshold} || $data{threshold} =~ /^\s*$/;
1170 $data{summaryLength} = $parent->{summaryLength}
1171 if !defined $data{summaryLength} || $data{summaryLength} =~ /^\s*$/;
1172 }
1173 else {
1174 $data{threshold} = $self->{cfg}->entry("level $level", 'threshold', 5)
1175 if !defined $data{threshold} || $data{threshold} =~ /^\s*$/;
1176 $data{summaryLength} = 200
1177 if !defined $data{summaryLength} || $data{summaryLength} =~ /^\s*$/;
1178 }
1179 $data{generator} = $self->generator;
1180 $data{lastModified} = now_sqldate();
1181 $data{level} = $level;
1182 $data{listed} = 1 unless defined $data{listed};
1183
1184 shift @columns;
1185 my $article = $table_object->add(@data{@columns});
1186
1187 # we now have an id - generate the links
1188
1189 my $cgi_uri = $self->{cfg}->entry('uri', 'cgi', '/cgi-bin');
1190 $article->setAdmin("$cgi_uri/admin/admin.pl?id=$article->{id}");
1191 $article->setLink($self->make_link($article));
1192 $article->save();
1193
caa7299c
TC
1194 use Util 'generate_article';
1195 generate_article($articles, $article) if $Constants::AUTO_GENERATE;
1196
ca9aa2bf
TC
1197 my $urlbase = $self->{cfg}->entryVar('site', 'url');
1198 return BSE::Template->get_refresh($urlbase . $article->{admin},
1199 $self->{cfg});
1200}
1201
1202sub fill_old_data {
0d5ccc7f 1203 my ($self, $req, $article, $data) = @_;
ca9aa2bf 1204
4010d92e
TC
1205 if (exists $data->{body}) {
1206 $data->{body} =~ s/\x0D\x0A/\n/g;
1207 $data->{body} =~ tr/\r/\n/;
1208 }
ca9aa2bf
TC
1209 for my $col (Article->columns) {
1210 $article->{$col} = $data->{$col}
1211 if exists $data->{$col} && $col ne 'id' && $col ne 'parentid';
1212 }
1213
1214 return 1;
1215}
1216
1217sub save {
1218 my ($self, $req, $article, $articles) = @_;
4010d92e
TC
1219
1220 $req->user_can(edit_save => $article)
1221 or return $self->edit_form($req, $article, $articles,
1222 "You don't have access to save this article");
ca9aa2bf
TC
1223
1224 my $cgi = $req->cgi;
1225 my %data;
1226 for my $name ($article->columns) {
1227 $data{$name} = $cgi->param($name)
abf5bbc6
TC
1228 if defined($cgi->param($name)) and $name ne 'id' && $name ne 'parentid'
1229 && $req->user_can("edit_field_edit_$name", $article);
ca9aa2bf
TC
1230 }
1231 my %errors;
1232 $self->validate_old($article, \%data, $articles, \%errors)
1233 or return $self->edit_form($req, $article, $articles, undef, \%errors);
abf5bbc6
TC
1234 $self->save_thumbnail($cgi, $article, \%data)
1235 if $req->user_can('edit_field_edit_thumbImage', $article);
ca9aa2bf
TC
1236 $self->fill_old_data($req, $article, \%data);
1237 if (exists $article->{template} &&
1238 $article->{template} =~ m|\.\.|) {
1239 my $msg = "Please only select templates from the list provided";
1240 return $self->edit_form($req, $article, $articles, $msg);
1241 }
1242
1243 # reparenting
1244 my $newparentid = $cgi->param('parentid');
abf5bbc6
TC
1245 if ($newparentid && $req->user_can('edit_field_edit_parentid', $article)) {
1246 if ($newparentid == $article->{parentid}) {
1247 # nothing to do
1248 }
1249 elsif ($newparentid != -1) {
1250 print STDERR "Reparenting...\n";
1251 my $newparent = $articles->getByPkey($newparentid);
1252 if ($newparent) {
1253 if ($newparent->{level} != $article->{level}-1) {
1254 # the article cannot become a child of itself or one of it's
1255 # children
1256 if ($article->{id} == $newparentid
1257 || $self->is_descendant($article->{id}, $newparentid, $articles)) {
1258 my $msg = "Cannot become a child of itself or of a descendant";
1259 return $self->edit_form($req, $article, $articles, $msg);
1260 }
1261 my $shopid = $self->{cfg}->entryErr('articles', 'shop');
1262 if ($self->is_descendant($article->{id}, $shopid, $articles)) {
1263 my $msg = "Cannot become a descendant of the shop";
1264 return $self->edit_form($req, $article, $articles, $msg);
1265 }
1266 my $msg;
1267 $self->reparent($article, $newparentid, $articles, \$msg)
1268 or return $self->edit_form($req, $article, $articles, $msg);
ca9aa2bf 1269 }
abf5bbc6
TC
1270 else {
1271 # stays at the same level, nothing special
1272 $article->{parentid} = $newparentid;
ca9aa2bf 1273 }
ca9aa2bf 1274 }
abf5bbc6
TC
1275 # else ignore it
1276 }
1277 else {
1278 # becoming a section
1279 my $msg;
1280 $self->reparent($article, -1, $articles, \$msg)
1281 or return $self->edit_form($req, $article, $articles, $msg);
ca9aa2bf 1282 }
ca9aa2bf
TC
1283 }
1284
abf5bbc6
TC
1285 $article->{listed} = $cgi->param('listed')
1286 if defined $cgi->param('listed') &&
1287 $req->user_can('edit_field_edit_listed', $article);
1288 $article->{release} = sql_date($cgi->param('release'))
1289 if defined $cgi->param('release') &&
1290 $req->user_can('edit_field_edit_release', $article);
1291
1292 $article->{expire} = sql_date($cgi->param('expire')) || $Constants::D_99
1293 if defined $cgi->param('expire') &&
1294 $req->user_can('edit_field_edit_expire', $article);
ca9aa2bf
TC
1295 $article->{lastModified} = now_sqldate();
1296 my $link_titles = $self->{cfg}->entryBool('basic', 'link_titles', 0);
1297 if ($article->{id} != 1 && $article->{link} && $link_titles) {
1298 (my $extra = lc $article->{title}) =~ tr/a-z0-9/_/sc;
1299 my $article_uri = $self->{cfg}->entry('uri', 'articles', '/a');
1300 $article->{link} = "$article_uri/$article->{id}.html/$extra";
1301 }
1302
1303 $article->save();
caa7299c
TC
1304
1305 use Util 'generate_article';
1306 generate_article($articles, $article) if $Constants::AUTO_GENERATE;
1307
ca9aa2bf
TC
1308 my $urlbase = $self->{cfg}->entryVar('site', 'url');
1309 return BSE::Template->get_refresh($urlbase . $article->{admin},
1310 $self->{cfg});
1311}
1312
1313sub sql_date {
1314 my $str = shift;
1315 my ($year, $month, $day);
1316
1317 # look for a date
1318 if (($day, $month, $year) = ($str =~ m!(\d+)/(\d+)/(\d+)!)) {
1319 $year += 2000 if $year < 100;
1320
1321 return sprintf("%04d-%02d-%02d", $year, $month, $day);
1322 }
1323 return undef;
1324}
1325
1326sub reparent {
1327 my ($self, $article, $newparentid, $articles, $rmsg) = @_;
1328
1329 my $newlevel;
1330 if ($newparentid == -1) {
1331 $newlevel = 1;
1332 }
1333 else {
1334 my $parent = $articles->getByPkey($newparentid);
1335 unless ($parent) {
1336 $$rmsg = "Cannot get new parent article";
1337 return;
1338 }
1339 $newlevel = $parent->{level} + 1;
1340 }
1341 # the caller will save this one
1342 $article->{parentid} = $newparentid;
1343 $article->{level} = $newlevel;
1344 $article->{displayOrder} = time;
1345
1346 my @change = ( [ $article->{id}, $newlevel ] );
1347 while (@change) {
1348 my $this = shift @change;
1349 my ($art, $level) = @$this;
1350
1351 my @kids = $articles->getBy(parentid=>$art);
1352 push @change, map { [ $_->{id}, $level+1 ] } @kids;
1353
1354 for my $kid (@kids) {
1355 $kid->{level} = $level+1;
1356 $kid->save;
1357 }
1358 }
1359
1360 return 1;
1361}
1362
1363# tests if $desc is a descendant of $art
1364# where both are article ids
1365sub is_descendant {
1366 my ($self, $art, $desc, $articles) = @_;
1367
1368 my @check = ($art);
1369 while (@check) {
1370 my $parent = shift @check;
1371 $parent == $desc and return 1;
1372 my @kids = $articles->getBy(parentid=>$parent);
1373 push @check, map $_->{id}, @kids;
1374 }
1375
1376 return 0;
1377}
1378
1379sub save_thumbnail {
1380 my ($self, $cgi, $original, $newdata) = @_;
1381
1382 unless ($original) {
1383 @$newdata{qw/thumbImage thumbWidth thumbHeight/} = ('', 0, 0);
1384 }
1385 my $imagedir = $self->{cfg}->entry('paths', 'images', $Constants::IMAGEDIR);
1386 if ($cgi->param('remove_thumb') && $original && $original->{thumbImage}) {
1387 unlink("$imagedir/$original->{thumbImage}");
1388 @$newdata{qw/thumbImage thumbWidth thumbHeight/} = ('', 0, 0);
1389 }
1390 my $image = $cgi->param('thumbnail');
1391 if ($image && -s $image) {
1392 # where to put it...
1393 my $name = '';
1394 $image =~ /([\w.-]+)$/ and $name = $1;
1395 my $filename = time . "_" . $name;
1396
1397 use Fcntl;
1398 my $counter = "";
1399 $filename = time . '_' . $counter . '_' . $name
1400 until sysopen( OUTPUT, "$imagedir/$filename",
1401 O_WRONLY| O_CREAT| O_EXCL)
1402 || ++$counter > 100;
1403
1404 fileno(OUTPUT) or die "Could not open image file: $!";
1405 binmode OUTPUT;
1406 my $buffer;
1407
1408 #no strict 'refs';
1409
1410 # read the image in from the browser and output it to our
1411 # output filehandle
1412 print STDERR "\$image ",ref $image,"\n";
1413 seek $image, 0, 0;
1414 print OUTPUT $buffer while sysread $image, $buffer, 1024;
1415
1416 close OUTPUT
1417 or die "Could not close image output file: $!";
1418
1419 use Image::Size;
1420
1421 if ($original && $original->{thumbImage}) {
1422 #unlink("$imagedir/$original->{thumbImage}");
1423 }
1424 @$newdata{qw/thumbWidth thumbHeight/} = imgsize("$imagedir/$filename");
1425 $newdata->{thumbImage} = $filename;
1426 }
1427}
1428
1429sub child_types {
1430 my ($self, $article) = @_;
1431
1432 my $shopid = $self->{cfg}->entryErr('articles', 'shop');
1433 if ($article && $article->{id} && $article->{id} == $shopid) {
1434 return ( 'BSE::Edit::Catalog' );
1435 }
1436 return ( 'BSE::Edit::Article' );
1437}
1438
1439sub add_stepkid {
1440 my ($self, $req, $article, $articles) = @_;
1441
1442 my $cgi = $req->cgi;
1443 require 'BSE/Admin/StepParents.pm';
1444 eval {
1445 my $childId = $cgi->param('stepkid');
1446 defined $childId
1447 or die "No stepkid supplied to add_stepkid";
1448 $childId =~ /^\d+$/
1449 or die "Invalid stepkid supplied to add_stepkid";
1450 my $child = $articles->getByPkey($childId)
1451 or die "Article $childId not found";
1452
1453 use BSE::Util::Valid qw/valid_date/;
1454 my $release = $cgi->param('release');
1455 valid_date($release) or $release = undef;
1456 my $expire = $cgi->param('expire');
1457 valid_date($expire) or $expire = undef;
1458
1459 my $newentry =
1460 BSE::Admin::StepParents->add($article, $child, $release, $expire);
1461 };
1462 if ($@) {
1463 return $self->edit_form($req, $article, $articles, $@);
1464 }
1465 return $self->refresh($article, $cgi, 'step');
1466}
1467
1468sub del_stepkid {
1469 my ($self, $req, $article, $articles) = @_;
1470
1471 my $cgi = $req->cgi;
1472 require 'BSE/Admin/StepParents.pm';
1473 eval {
1474 my $childId = $cgi->param('stepkid');
1475 defined $childId
1476 or die "No stepkid supplied to add_stepkid";
1477 $childId =~ /^\d+$/
1478 or die "Invalid stepkid supplied to add_stepkid";
1479 my $child = $articles->getByPkey($childId)
1480 or die "Article $childId not found";
1481
1482 BSE::Admin::StepParents->del($article, $child);
1483 };
1484
1485 if ($@) {
1486 return $self->edit_form($req, $article, $articles, $@);
1487 }
1488 return $self->refresh($article, $cgi, 'step');
1489}
1490
1491sub save_stepkids {
1492 my ($self, $req, $article, $articles) = @_;
1493
1494 my $cgi = $req->cgi;
1495 require 'BSE/Admin/StepParents.pm';
1496 my @stepcats = OtherParents->getBy(parentId=>$article->{id});
1497 my %stepcats = map { $_->{parentId}, $_ } @stepcats;
1498 my %datedefs = ( release => '2000-01-01', expire=>'2999-12-31' );
1499 for my $stepcat (@stepcats) {
1500 for my $name (qw/release expire/) {
1501 my $date = $cgi->param($name.'_'.$stepcat->{childId});
1502 if (defined $date) {
1503 if ($date eq '') {
1504 $date = $datedefs{$name};
1505 }
1506 elsif (valid_date($date)) {
1507 use BSE::Util::SQL qw/date_to_sql/;
1508 $date = date_to_sql($date);
1509 }
1510 else {
1511 return $self->refresh($article, $cgi, '', "Invalid date '$date'");
1512 }
1513 $stepcat->{$name} = $date;
1514 }
1515 }
1516 eval {
1517 $stepcat->save();
1518 };
1519 $@ and return $self->refresh($article, $cgi, '', $@);
1520 }
1521 return $self->refresh($article, $cgi, 'step');
1522}
1523
1524sub add_stepparent {
1525 my ($self, $req, $article, $articles) = @_;
1526
1527 my $cgi = $req->cgi;
1528 require 'BSE/Admin/StepParents.pm';
1529 eval {
1530 my $step_parent_id = $cgi->param('stepparent');
1531 defined($step_parent_id)
1532 or die "No stepparent supplied to add_stepparent";
1533 int($step_parent_id) eq $step_parent_id
1534 or die "Invalid stepcat supplied to add_stepcat";
1535 my $step_parent = $articles->getByPkey($step_parent_id)
1536 or die "Parnet $step_parent_id not found\n";
1537
1538 my $release = $cgi->param('release');
1539 defined $release
1540 or $release = "01/01/2000";
1541 use BSE::Util::Valid qw/valid_date/;
1542 $release eq '' or valid_date($release)
1543 or die "Invalid release date";
1544 my $expire = $cgi->param('expire');
1545 defined $expire
1546 or $expire = '31/12/2999';
1547 $expire eq '' or valid_date($expire)
1548 or die "Invalid expire data";
1549
1550 my $newentry =
1551 BSE::Admin::StepParents->add($step_parent, $article, $release, $expire);
1552 };
1553 $@ and return $self->refresh($article, $cgi, 'step', $@);
1554
1555 return $self->refresh($article, $cgi, 'stepparents');
1556}
1557
1558sub del_stepparent {
1559 my ($self, $req, $article, $articles) = @_;
1560
1561 my $cgi = $req->cgi;
1562 require 'BSE/Admin/StepParents.pm';
1563 my $step_parent_id = $cgi->param('stepparent');
1564 defined($step_parent_id)
1565 or return $self->refresh($article, $cgi, 'stepparents',
1566 "No stepparent supplied to add_stepcat");
1567 int($step_parent_id) eq $step_parent_id
1568 or return $self->refresh($article, $cgi, 'stepparents',
1569 "Invalid stepparent supplied to add_stepparent");
1570 my $step_parent = $articles->getByPkey($step_parent_id)
1571 or return $self->refresh($article, $cgi, 'stepparent',
1572 "Stepparent $step_parent_id not found");
1573
1574 eval {
1575 BSE::Admin::StepParents->del($step_parent, $article);
1576 };
1577 $@ and return $self->refresh($article, $cgi, 'stepparents', $@);
1578
1579 return $self->refresh($article, $cgi, 'stepparents');
1580}
1581
1582sub save_stepparents {
1583 my ($self, $req, $article, $articles) = @_;
1584
1585 my $cgi = $req->cgi;
1586
1587 require 'BSE/Admin/StepParents.pm';
1588 my @stepparents = OtherParents->getBy(childId=>$article->{id});
1589 my %stepparents = map { $_->{parentId}, $_ } @stepparents;
1590 my %datedefs = ( release => '2000-01-01', expire=>'2999-12-31' );
1591 for my $stepparent (@stepparents) {
1592 for my $name (qw/release expire/) {
1593 my $date = $cgi->param($name.'_'.$stepparent->{parentId});
1594 if (defined $date) {
1595 if ($date eq '') {
1596 $date = $datedefs{$name};
1597 }
1598 elsif (valid_date($date)) {
1599 use BSE::Util::SQL qw/date_to_sql/;
1600 $date = date_to_sql($date);
1601 }
1602 else {
1603 return $self->refresh($article, $cgi, "Invalid date '$date'");
1604 }
1605 $stepparent->{$name} = $date;
1606 }
1607 }
1608 eval {
1609 $stepparent->save();
1610 };
1611 $@ and return $self->refresh($article, $cgi, '', $@);
1612 }
1613
1614 return $self->refresh($article, $cgi, 'stepparents');
1615}
1616
1617sub refresh {
1618 my ($self, $article, $cgi, $name, $message, $extras) = @_;
1619
1620 my $urlbase = $self->{cfg}->entryVar('site', 'url');
1621 my $url = "$urlbase$ENV{SCRIPT_NAME}?id=$article->{id}";
1622 $url .= "&message=" . CGI::escape($message) if $message;
1623 if ($cgi->param('_t')) {
1624 $url .= "&_t=".CGI::escape($cgi->param('_t'));
1625 }
1626 $url .= $extras if defined $extras;
1627 $url .= "#$name" if $name;
1628
1629 return BSE::Template->get_refresh($url, $self->{cfg});
1630}
1631
1632sub show_images {
1633 my ($self, $req, $article, $articles, $msg) = @_;
1634
1635 my %acts;
1636 %acts = $self->low_edit_tags(\%acts, $req, $article, $articles, $msg);
1637 my $template = 'admin/article_img';
1638
1639 return BSE::Template->get_response($template, $req->cfg, \%acts);
1640}
1641
1642sub save_image_changes {
1643 my ($self, $req, $article, $articles) = @_;
1644
abf5bbc6
TC
1645 $req->user_can(edit_images_save => $article)
1646 or return $self->show_images($req, $article, $articles,
1647 "You don't have access to save image information for this article");
1648
ca9aa2bf
TC
1649 my $cgi = $req->cgi;
1650 my $image_pos = $cgi->param('imagePos');
1651 if ($image_pos
1652 && $image_pos =~ /^(?:tl|tr|bl|br)$/
1653 && $image_pos ne $article->{imagePos}) {
1654 $article->{imagePos} = $image_pos;
1655 $article->save;
1656 }
1657 my @images = $article->images;
1658
1659 my $changed;
1660 my @alt = $cgi->param('alt');
1661 if (@alt) {
1662 ++$changed;
1663 for my $index (0..$#images) {
1664 $index < @alt or last;
1665 $images[$index]{alt} = $alt[$index];
1666 }
1667 }
1668 my @urls = $cgi->param('url');
1669 if (@urls) {
1670 ++$changed;
1671 for my $index (0..$#images) {
1672 $index < @urls or next;
1673 $images[$index]{url} = $urls[$index];
1674 }
1675 }
1676 if ($changed) {
1677 for my $image (@images) {
1678 $image->save;
1679 }
1680 }
55753022 1681 return $self->refresh($article, $cgi, undef, undef, '&showimages=1');
ca9aa2bf
TC
1682}
1683
1684sub add_image {
1685 my ($self, $req, $article, $articles) = @_;
1686
abf5bbc6
TC
1687 $req->user_can(edit_images_add => $article)
1688 or return $self->show_images($req, $article, $articles,
1689 "You don't have access to add new images to this article");
1690
ca9aa2bf
TC
1691 my $cgi = $req->cgi;
1692
1693 my $image = $cgi->param('image');
1694 unless ($image) {
1695 return $self->show_images($req, $article, $articles,
1696 'Enter or select the name of an image file on your machine');
1697 }
1698 if (-z $image) {
1699 return $self->show_images($req, $article, $articles,
1700 'Image file is empty');
1701 }
1702 my $imagename = $image;
1703 $imagename .= ''; # force it into a string
1704 my $basename = '';
1705 $imagename =~ /([\w.-]+)$/ and $basename = $1;
1706
1707 # create a filename that we hope is unique
1708 my $filename = time. '_'. $basename;
1709
1710 # for the sysopen() constants
1711 use Fcntl;
1712
1713 my $imagedir = $req->cfg->entry('paths', 'images', $Constants::IMAGEDIR);
1714 # loop until we have a unique filename
1715 my $counter="";
1716 $filename = time. '_' . $counter . '_' . $basename
1717 until sysopen( OUTPUT, "$imagedir/$filename", O_WRONLY| O_CREAT| O_EXCL)
1718 || ++$counter > 100;
1719
1720 fileno(OUTPUT) or die "Could not open image file: $!";
1721
1722 # for OSs with special text line endings
1723 binmode OUTPUT;
1724
1725 my $buffer;
1726
1727 no strict 'refs';
1728
1729 # read the image in from the browser and output it to our output filehandle
1730 print OUTPUT $buffer while read $image, $buffer, 1024;
1731
1732 # close and flush
1733 close OUTPUT
1734 or die "Could not close image file $filename: $!";
1735
1736 use Image::Size;
1737
1738
1739 my($width,$height) = imgsize("$imagedir/$filename");
1740
1741 my $alt = $cgi->param('altIn');
1742 defined $alt or $alt = '';
1743 my $url = $cgi->param('url');
1744 defined $url or $url = '';
1745 my %image =
1746 (
1747 articleId => $article->{id},
1748 image => $filename,
1749 alt=>$alt,
1750 width=>$width,
1751 height => $height,
1752 url => $url,
1753 displayOrder=>time,
1754 );
1755 require Images;
1756 my @cols = Image->columns;
1757 shift @cols;
1758 my $imageobj = Images->add(@image{@cols});
1759
55753022 1760 return $self->refresh($article, $cgi, undef, undef, '&showimages=1');
ca9aa2bf
TC
1761}
1762
1763# remove an image
1764sub remove_img {
1765 my ($self, $req, $article, $articles, $imageid) = @_;
1766
abf5bbc6
TC
1767 $req->user_can(edit_images_delete => $article)
1768 or return $self->show_images($req, $article, $articles,
1769 "You don't have access to delete images from this article");
1770
ca9aa2bf
TC
1771 $imageid or die;
1772
1773 my @images = $article->images();
1774 my ($image) = grep $_->{id} == $imageid, @images
1775 or return $self->show_images($req, $article, $articles, "No such image");
1776 my $imagedir = $req->cfg->entry('paths', 'images', $Constants::IMAGEDIR);
6473c56f 1777 unlink "$imagedir$image->{image}";
ca9aa2bf
TC
1778 $image->remove;
1779
6473c56f 1780 return $self->refresh($article, $req->cgi, undef, undef, '&showimages=1');
ca9aa2bf
TC
1781}
1782
1783sub move_img_up {
1784 my ($self, $req, $article, $articles) = @_;
1785
abf5bbc6
TC
1786 $req->user_can(edit_images_reorder => $article)
1787 or return $self->show_images($req, $article, $articles,
1788 "You don't have access to reorder images in this article");
1789
ca9aa2bf
TC
1790 my $imageid = $req->cgi->param('imageid');
1791 my @images = $article->images;
1792 my ($imgindex) = grep $images[$_]{id} == $imageid, 0..$#images
1793 or return $self->show_images($req, $article, $articles, "No such image");
1794 $imgindex > 0
1795 or return $self->show_images($req, $article, $articles, "Image is already at the top");
1796 my ($to, $from) = @images[$imgindex-1, $imgindex];
1797 ($to->{displayOrder}, $from->{displayOrder}) =
1798 ($from->{displayOrder}, $to->{displayOrder});
1799 $to->save;
1800 $from->save;
1801
1802 return $self->refresh($article, $req->cgi, undef, undef, '&showimage=1');
1803}
1804
1805sub move_img_down {
1806 my ($self, $req, $article, $articles) = @_;
1807
abf5bbc6
TC
1808 $req->user_can(edit_images_reorder => $article)
1809 or return $self->show_images($req, $article, $articles,
1810 "You don't have access to reorder images in this article");
1811
ca9aa2bf
TC
1812 my $imageid = $req->cgi->param('imageid');
1813 my @images = $article->images;
1814 my ($imgindex) = grep $images[$_]{id} == $imageid, 0..$#images
1815 or return $self->show_images($req, $article, $articles, "No such image");
1816 $imgindex < $#images
1817 or return $self->show_images($req, $article, $articles, "Image is already at the end");
1818 my ($to, $from) = @images[$imgindex+1, $imgindex];
1819 ($to->{displayOrder}, $from->{displayOrder}) =
1820 ($from->{displayOrder}, $to->{displayOrder});
1821 $to->save;
1822 $from->save;
1823
1824 return $self->refresh($article, $req->cgi, undef, undef, '&showimage=1');
1825}
1826
1827sub get_article {
1828 my ($self, $articles, $article) = @_;
1829
1830 return $article;
1831}
1832
1833sub table_object {
1834 my ($self, $articles) = @_;
1835
1836 $articles;
1837}
1838
1839my %types =
1840 (
1841 qw(
1842 pdf application/pdf
1843 txt text/plain
1844 htm text/html
1845 html text/html
1846 gif image/gif
1847 jpg image/jpeg
1848 jpeg image/jpeg
1849 doc application/msword
1850 rtf application/rtf
1851 zip application/zip
1852 png image/png
1853 bmp image/bmp
1854 tif image/tiff
1855 tiff image/tiff
1856 sgm text/sgml
1857 sgml text/sgml
1858 xml text/xml
1859 mov video/quicktime
1860 )
1861 );
1862
1863sub _refresh_filelist {
1864 my ($self, $req, $article) = @_;
1865
1866 return $self->refresh($article, $req->cgi, undef, undef, '&filelist=1');
1867}
1868
1869sub filelist {
1870 my ($self, $req, $article, $articles, $msg) = @_;
1871
1872 my %acts;
1873 %acts = $self->low_edit_tags(\%acts, $req, $article, $articles, $msg);
1874 my $template = 'admin/filelist';
1875
1876 return BSE::Template->get_response($template, $req->cfg, \%acts);
1877}
1878
1879sub fileadd {
1880 my ($self, $req, $article, $articles) = @_;
1881
abf5bbc6
TC
1882 $req->user_can(edit_files_add => $article)
1883 or return $self->filelist($req, $article, $articles,
1884 "You don't have access to add files to this article");
1885
ca9aa2bf
TC
1886 my %file;
1887 my $cgi = $req->cgi;
1888 require ArticleFile;
1889 my @cols = ArticleFile->columns;
1890 shift @cols;
1891 for my $col (@cols) {
1892 if (defined $cgi->param($col)) {
1893 $file{$col} = $cgi->param($col);
1894 }
1895 }
1896
1897 $file{forSale} = 0 + exists $file{forSale};
1898 $file{articleId} = $article->{id};
1899 $file{download} = 0 + exists $file{download};
1900 $file{requireUser} = 0 + exists $file{requireUser};
1901
1902 my $downloadPath = $self->{cfg}->entryVar('paths', 'downloads');
1903
1904 # build a filename
1905 my $file = $cgi->param('file');
1906 unless ($file) {
1907 return $self->filelist($req, $article, $articles,
1908 "Enter or select the name of a file on your machine");
1909 }
1910 if (-z $file) {
1911 return $self->filelist($req, $article, $articles,
1912 message=>"File is empty");
1913 }
1914
1915 unless ($file{contentType}) {
1916 unless ($file =~ /\.([^.]+)$/) {
1917 $file{contentType} = "application/octet-stream";
1918 }
1919 unless ($file{contentType}) {
1920 my $ext = lc $1;
1921 my $type = $types{$ext};
1922 unless ($type) {
1923 $type = $self->{cfg}->entry('extensions', $ext)
1924 || $self->{cfg}->entry('extensions', ".$ext")
1925 || "application/octet-stream";
1926 }
1927 $file{contentType} = $type;
1928 }
1929 }
1930
1931 my $basename = '';
1932 $file =~ /([\w.-]+)$/ and $basename = $1;
1933
1934 my $filename = time. '_'. $basename;
1935
1936 # for the sysopen() constants
1937 use Fcntl;
1938
1939 # loop until we have a unique filename
1940 my $counter="";
1941 $filename = time. '_' . $counter . '_' . $basename
1942 until sysopen( OUTPUT, "$downloadPath/$filename",
1943 O_WRONLY| O_CREAT| O_EXCL)
1944 || ++$counter > 100;
1945
1946 fileno(OUTPUT) or die "Could not open file: $!";
1947
1948 # for OSs with special text line endings
1949 binmode OUTPUT;
1950
1951 my $buffer;
1952
1953 no strict 'refs';
1954
1955 # read the image in from the browser and output it to our output filehandle
1956 print OUTPUT $buffer while read $file, $buffer, 8192;
1957
1958 # close and flush
1959 close OUTPUT
1960 or die "Could not close file $filename: $!";
1961
1962 use BSE::Util::SQL qw/now_datetime/;
1963 $file{filename} = $filename;
1964 $file{displayName} = $basename;
1965 $file{sizeInBytes} = -s $file;
1966 $file{displayOrder} = time;
1967 $file{whenUploaded} = now_datetime();
1968
1969 require ArticleFiles;
1970 my $fileobj = ArticleFiles->add(@file{@cols});
1971
1972 $self->_refresh_filelist($req, $article);
1973}
1974
1975sub fileswap {
1976 my ($self, $req, $article, $articles) = @_;
1977
abf5bbc6
TC
1978 $req->user_can('edit_files_reorder', $article)
1979 or return $self->filelist($req, $article, $articles,
1980 "You don't have access to reorder files in this article");
1981
ca9aa2bf
TC
1982 my $cgi = $req->cgi;
1983 my $id1 = $cgi->param('file1');
1984 my $id2 = $cgi->param('file2');
1985
1986 if ($id1 && $id2) {
1987 my @files = $article->files;
1988
1989 my ($file1) = grep $_->{id} == $id1, @files;
1990 my ($file2) = grep $_->{id} == $id2, @files;
1991
1992 if ($file1 && $file2) {
1993 ($file1->{displayOrder}, $file2->{displayOrder})
1994 = ($file2->{displayOrder}, $file1->{displayOrder});
1995 $file1->save;
1996 $file2->save;
1997 }
1998 }
1999
2000 $self->_refresh_filelist($req, $article);
2001}
2002
2003sub filedel {
2004 my ($self, $req, $article, $articles) = @_;
2005
abf5bbc6
TC
2006 $req->user_can('edit_files_delete', $article)
2007 or return $self->filelist($req, $article, $articles,
2008 "You don't have access to delete files from this article");
2009
ca9aa2bf
TC
2010 my $cgi = $req->cgi;
2011 my $fileid = $cgi->param('file');
2012 if ($fileid) {
2013 my @files = $article->files;
2014
2015 my ($file) = grep $_->{id} == $fileid, @files;
2016
2017 if ($file) {
2018 my $downloadPath = $req->cfg->entryErr('paths', 'downloads');
2019 my $filename = $downloadPath . "/" . $file->{filename};
2020 my $debug_del = $req->cfg->entryBool('debug', 'file_unlink', 0);
2021 if ($debug_del) {
2022 unlink $filename
2023 or print STDERR "Error deleting $filename: $!\n";
2024 }
2025 else {
2026 unlink $filename;
2027 }
2028 $file->remove();
2029 }
2030 }
2031
2032 $self->_refresh_filelist($req, $article);
2033}
2034
2035sub filesave {
abf5bbc6 2036 my ($self, $req, $article, $articles) = @_;
ca9aa2bf 2037
abf5bbc6
TC
2038 $req->user_can('edit_files_save', $article)
2039 or return $self->filelist($req, $article, $articles,
2040 "You don't have access to save file information for this article");
ca9aa2bf
TC
2041 my @files = $article->files;
2042
2043 my $cgi = $req->cgi;
2044 for my $file (@files) {
2045 if (defined $cgi->param("description_$file->{id}")) {
2046 $file->{description} = $cgi->param("description_$file->{id}");
2047 if (my $type = $cgi->param("contentType_$file->{id}")) {
2048 $file->{contentType} = $type;
2049 }
2050 $file->{download} = 0 + defined $cgi->param("download_$file->{id}");
2051 $file->{forSale} = 0 + defined $cgi->param("forSale_$file->{id}");
2052 $file->{requireUser} = 0 + defined $cgi->param("requireUser_$file->{id}");
2053 $file->save;
2054 }
2055 }
2056
2057 $self->_refresh_filelist($req, $article);
2058}
2059
6473c56f
TC
2060sub can_remove {
2061 my ($self, $req, $article, $articles, $rmsg) = @_;
2062
abf5bbc6
TC
2063 unless ($req->user_can('edit_delete_article', $article, $rmsg)) {
2064 $$rmsg ||= "Access denied";
2065 return;
2066 }
2067
6473c56f
TC
2068 if ($articles->children($article->{id})) {
2069 $$rmsg = "This article has children. You must delete the children first (or change their parents)";
2070 return;
2071 }
2072 if (grep $_ == $article->{id}, @Constants::NO_DELETE) {
2073 $$rmsg = "Sorry, these pages are essential to the site structure - they cannot be deleted";
2074 return;
2075 }
2076 if ($article->{id} == $Constants::SHOPID) {
2077 $$rmsg = "Sorry, these pages are essential to the store - they cannot be deleted - you may want to hide the the store instead.";
2078 return;
2079 }
2080
2081 return 1;
2082}
2083
2084sub remove {
2085 my ($self, $req, $article, $articles) = @_;
2086
2087 my $why_not;
2088 unless ($self->can_remove($req, $article, $articles, \$why_not)) {
2089 return $self->edit_form($req, $article, $articles, $why_not);
2090 }
2091
2092 require Images;
2093 my @images = Images->getBy(articleId=>$article->{id});
2094 my $imagedir = $self->{cfg}->entry('paths', 'images', $Constants::IMAGEDIR);
2095 for my $image (@images) {
2096 unlink("$imagedir/$image->{image}");
2097 $image->remove();
2098 }
2099
2100 # remove any step(child|parent) links
2101 require OtherParents;
2102 my @steprels = OtherParents->anylinks($article->{id});
2103 for my $link (@steprels) {
2104 $link->remove();
2105 }
2106
2107 my $parentid = $article->{parentid};
2108 $article->remove;
2109 my $urlbase = $self->{cfg}->entryVar('site', 'url');
2110 my $url = "$urlbase$ENV{SCRIPT_NAME}?id=$parentid";
2111 $url .= "&message=Article+deleted";
2112 return BSE::Template->get_refresh($url, $self->{cfg});
2113}
2114
4010d92e
TC
2115sub unhide {
2116 my ($self, $req, $article, $articles) = @_;
2117
2118 if ($req->user_can(edit_field_edit_listed => $article)
2119 && $req->user_can(edit_save => $article)) {
2120 $article->{listed} = 1;
2121 $article->save;
2122
2123 use Util 'generate_article';
2124 generate_article($articles, $article) if $Constants::AUTO_GENERATE;
2125 }
2126 my $r = $req->cgi->param('r');
2127 unless ($r) {
2128 $r = $req->cfg->entryVar('site', 'url') . "/cgi-bin/admin/add.pl?id=" . $article->{parentid};
2129 }
2130 return BSE::Template->get_refresh($r, $req->cfg);
2131}
2132
2133sub hide {
2134 my ($self, $req, $article, $articles) = @_;
2135
2136 if ($req->user_can(edit_field_edit_listed => $article)
2137 && $req->user_can(edit_save => $article)) {
2138 $article->{listed} = 0;
2139 $article->save;
2140
2141 use Util 'generate_article';
2142 generate_article($articles, $article) if $Constants::AUTO_GENERATE;
2143 }
2144 my $r = $req->cgi->param('r');
2145 unless ($r) {
2146 $r = $req->cfg->entryVar('site', 'url') . "/cgi-bin/admin/add.pl?id=" . $article->{parentid};
2147 }
2148 return BSE::Template->get_refresh($r, $req->cfg);
2149}
2150
9168c88c
TC
2151sub default_value {
2152 my ($self, $req, $article, $col) = @_;
2153
2154 if ($article->{parentid}) {
2155 my $section = "children of $article->{parentid}";
2156 my $value = $req->cfg->entry($section, $col);
2157 if (defined $value) {
2158 }
2159 }
2160 my $section = "level $article->{level}";
2161 my $value = $req->cfg->entry($section, $col);
2162 defined($value) and return encode_entities($value);
2163
2164 return '';
2165}
2166
ca9aa2bf
TC
21671;
2168
2169=head1 NAME
2170
2171 BSE::Edit::Article - editing functionality for BSE articles
2172
2173=head1 AUTHOR
2174
2175Tony Cook <tony@develop-help.com>
2176
2177=head1 REVISION
2178
2179$Revision$
2180
2181=cut