* Don't autovifivy @fields array entries.
[interchange.git] / code / Widget / imagehelper.widget
1 # Copyright 2005-2007 Interchange Development Group and others
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.  See the LICENSE file for details.
7
8 # $Id: imagehelper.widget,v 1.6 2007-03-30 23:40:58 pajamian Exp $
9
10 CodeDef imagehelper  Widget  1
11 CodeDef imagehelper  Description Image upload
12 CodeDef imagehelper  Routine <<EOR
13 sub  {
14     my ($opt) = @_;
15
16         my $name = $opt->{name};
17         my $size = $opt->{cols} || $opt->{width};
18         my $val  = $opt->{value};
19         my $path = $opt->{image_path} || $opt->{outboard};
20         my $name_from = $opt->{name_from_field} || '';
21         my $imagebase = $opt->{image_base} || $opt->{prepend};
22
23         Vend::Interpolate::vars_and_comments(\$path);
24         Vend::Interpolate::vars_and_comments(\$imagebase);
25         if ($imagebase ||= '') {
26                 $imagebase =~ s/^\s+//;
27                 $imagebase =~ s:[\s/]*$:/:;
28         }
29
30         my $of_widget;
31         if($path =~ s!/\*(?:\.([^/]+))?$!!) {
32                 my $spec = $1;
33                 my @files = UI::Primitive::list_images($path, $spec);
34                 unshift(@files, "=(none)");
35                 my $passed = join ",", map { s/,/&#44;/g; $_} @files;
36                 my $opt = {
37                         type => 'select',
38                         default => $val,
39                         attribute => 'mv_data_file_oldfile',
40                         passed => $passed,
41                 };
42                 $of_widget = Vend::Interpolate::tag_accessories(
43                                 undef, undef, $opt, { 'mv_data_file_oldfile' => $val } );
44         }
45         else {
46                 $of_widget = qq{<INPUT TYPE=hidden NAME=mv_data_file_oldfile VALUE="$val">};
47         }
48         $size = qq{ SIZE="$size"} if $size > 0;
49     if ($val) {
50         qq{<A HREF="$imagebase$path/$val">$val</A>&nbsp;<INPUT TYPE=hidden NAME=mv_data_file_field VALUE="$name"><INPUT TYPE=hidden NAME=mv_data_file_name_from VALUE="$name_from">
51 <INPUT TYPE=hidden NAME=mv_data_file_path VALUE="$path">$of_widget<INPUT TYPE=file NAME="$name" VALUE="$val">};      
52     } else {
53         qq{<INPUT TYPE=hidden NAME=mv_data_file_field VALUE="$name"><INPUT TYPE=hidden NAME=mv_data_file_name_from VALUE="$name_from">
54 <INPUT TYPE=hidden NAME=mv_data_file_path VALUE="$path">$of_widget<INPUT TYPE=file NAME="$name"$size>};
55     }
56 }
57 EOR
58
59 CodeDef imagehelper ExtraMeta <<EOM
60 {
61         _order => [ qw/
62                         image_path
63                         name_from_field
64                 /],
65         image_path => {
66                 label => 'Image Path',
67                 help => 'default is <b>images/items</b> in <i>standard catalog</i>',
68                 widget => 'text_30',
69         },
70         name_from_field => {
71                 label => 'Name from field',
72                 help => 'Usually named from products SKU',
73                 widget => 'select',
74                 options => 'columns::',
75         },
76 }
77 EOM