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