svc_hardware: better error messages for bad hw_addr when not validating as a MAC...
[freeside.git] / FS / FS / template_image.pm
1 package FS::template_image;
2 use base qw( FS::Agent_Mixin FS::Record );
3
4 use strict;
5 use FS::Record qw( qsearchs );
6 use File::Slurp qw( slurp );
7 use MIME::Base64 qw( encode_base64 );
8
9 my %ext_to_type = (
10   'jpeg' => 'image/jpeg',
11   'jpg'  => 'image/jpeg',
12   'png'  => 'image/png',
13   'gif'  => 'image/gif',
14 );
15
16 =head1 NAME
17
18 FS::template_image - Object methods for template_image records
19
20 =head1 SYNOPSIS
21
22   use FS::template_image;
23
24   $record = new FS::template_image {
25               'name'      => 'logo',
26               'agentnum'  => $agentnum,
27               'base64'    => encode_base64($rawdata),
28               'mime_type' => 'image/jpg',
29   };
30
31   $error = $record->insert;
32
33   $error = $new_record->replace($old_record);
34
35   $error = $record->delete;
36
37   $error = $record->check;
38
39 =head1 DESCRIPTION
40
41 An FS::template_image object represents an uploaded image for insertion into templates.
42 FS::template_image inherits from FS::Record.  The following fields are currently supported:
43
44 =over 4
45
46 =item imgnum - primary key
47
48 =item name - unique name, for selecting/editing images
49
50 =item agentnum - image agent
51
52 =item mime-type - image mime-type
53
54 =item base64 - base64-encoded raw contents of image file
55
56 =back
57
58 =head1 METHODS
59
60 =over 4
61
62 =item new HASHREF
63
64 Creates a new object.  To add the object to the database, see L<"insert">.
65
66 Note that this stores the hash reference, not a distinct copy of the hash it
67 points to.  You can ask the object for a copy with the I<hash> method.
68
69 =cut
70
71 # the new method can be inherited from FS::Record, if a table method is defined
72
73 sub table { 'template_image'; }
74
75 =item insert
76
77 Adds this record to the database.  If there is an error, returns the error,
78 otherwise returns false.
79
80 =cut
81
82 # the insert method can be inherited from FS::Record
83
84 =item delete
85
86 Delete this record from the database.
87
88 =cut
89
90 # the delete method can be inherited from FS::Record
91
92 =item replace OLD_RECORD
93
94 Replaces the OLD_RECORD with this one in the database.  If there is an error,
95 returns the error, otherwise returns false.
96
97 =cut
98
99 # the replace method can be inherited from FS::Record
100
101 =item check
102
103 Checks all fields to make sure this is a valid example.  If there is
104 an error, returns the error, otherwise returns false.  Called by the insert
105 and replace methods.
106
107 =cut
108
109 # the check method should currently be supplied - FS::Record contains some
110 # data checking routines
111
112 sub check {
113   my $self = shift;
114
115   my $error = 
116     $self->ut_numbern('imgnum','agentnum')
117     || $self->ut_text('name','mime-type')
118     || $self->ut_anything('base64')
119   ;
120   return $error if $error;
121
122   $self->SUPER::check;
123 }
124
125 =item src
126
127 Returns a data url for this image, incorporating mime_type & base64
128
129 =cut
130
131 sub src {
132   my $self = shift;
133   'data:'
134   . $self->mime_type
135   . ';base64,'
136   . $self->base64;
137 }
138
139 =item html
140
141 Returns html for a basic img tag for this image (no attributes)
142
143 =cut
144
145 sub html {
146   my $self = shift;
147   '<IMG SRC="'
148   . $self->src
149   . '">';
150 }
151
152 =item process_image_delete
153
154 Process for deleting an image.  Run as a job using L<FS::queue>.
155
156 =cut
157
158 sub process_image_delete {
159   my $job = shift;
160   my $param = shift;
161   my $template_image = qsearchs('template_image',{ 'imgnum' => $param->{'imgnum'} })
162     or die "Could not load template_image";
163   my $error = $template_image->delete;
164   die $error if $error;
165   '';
166 }
167
168 =item process_image_upload
169
170 Process for uploading an image.  Run as a job using L<FS::queue>.
171
172 =cut
173
174 sub process_image_upload {
175   my $job = shift;
176   my $param = shift;
177
178   my $files = $param->{'uploaded_files'}
179     or die "No files provided.\n";
180
181   my (%files) = map { /^(\w+):([\.\w]+)$/ ? ($1,$2):() } split /,/, $files;
182
183   my $dir = '%%%FREESIDE_CACHE%%%/cache.'. $FS::UID::datasrc. '/';
184   my $file = $dir. $files{'file'};
185
186   my $type;
187   if ( $file =~ /\.(\w+)$/i ) {
188     my $ext = lc($1);
189     die "Unrecognized file extension $ext"
190       unless $ext_to_type{$ext};
191     $type = $ext_to_type{$ext};
192   } else {
193     die "Cannot upload image file without extension"
194   }
195
196   my $template_image = new FS::template_image {
197     'name'   => $param->{'name'},
198     'mime_type' => $type,
199     'agentnum'  => $param->{'agentnum'},
200     'base64'    => encode_base64( slurp($file, binmode => ':raw'), '' ),
201   };
202   my $error = $template_image->insert();
203   die $error if $error;
204   unlink $file;
205   '';
206
207 }
208
209 =back
210
211 =head1 BUGS
212
213 Will be described here once found.
214
215 =head1 SEE ALSO
216
217 L<FS::Record>
218
219 =cut
220
221 1;
222