1 package FS::template_image;
2 use base qw( FS::Agent_Mixin FS::Record );
5 use FS::Record qw( qsearchs );
6 use File::Slurp qw( slurp );
7 use MIME::Base64 qw( encode_base64 );
10 'jpeg' => 'image/jpeg',
11 'jpg' => 'image/jpeg',
18 FS::template_image - Object methods for template_image records
22 use FS::template_image;
24 $record = new FS::template_image {
26 'agentnum' => $agentnum,
27 'base64' => encode_base64($rawdata),
28 'mime_type' => 'image/jpg',
31 $error = $record->insert;
33 $error = $new_record->replace($old_record);
35 $error = $record->delete;
37 $error = $record->check;
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:
46 =item imgnum - primary key
48 =item name - unique name, for selecting/editing images
50 =item agentnum - image agent
52 =item mime-type - image mime-type
54 =item base64 - base64-encoded raw contents of image file
64 Creates a new object. To add the object to the database, see L<"insert">.
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.
71 # the new method can be inherited from FS::Record, if a table method is defined
73 sub table { 'template_image'; }
77 Adds this record to the database. If there is an error, returns the error,
78 otherwise returns false.
82 # the insert method can be inherited from FS::Record
86 Delete this record from the database.
90 # the delete method can be inherited from FS::Record
92 =item replace OLD_RECORD
94 Replaces the OLD_RECORD with this one in the database. If there is an error,
95 returns the error, otherwise returns false.
99 # the replace method can be inherited from FS::Record
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
109 # the check method should currently be supplied - FS::Record contains some
110 # data checking routines
116 $self->ut_numbern('imgnum','agentnum')
117 || $self->ut_text('name','mime-type')
118 || $self->ut_anything('base64')
120 return $error if $error;
127 Returns a data url for this image, incorporating mime_type & base64
141 Returns html for a basic img tag for this image (no attributes)
152 =item process_image_delete
154 Process for deleting an image. Run as a job using L<FS::queue>.
158 sub process_image_delete {
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;
168 =item process_image_upload
170 Process for uploading an image. Run as a job using L<FS::queue>.
174 sub process_image_upload {
178 my $files = $param->{'uploaded_files'}
179 or die "No files provided.\n";
181 my (%files) = map { /^(\w+):([\.\w]+)$/ ? ($1,$2):() } split /,/, $files;
183 my $dir = '%%%FREESIDE_CACHE%%%/cache.'. $FS::UID::datasrc. '/';
184 my $file = $dir. $files{'file'};
187 if ( $file =~ /\.(\w+)$/i ) {
189 die "Unrecognized file extension $ext"
190 unless $ext_to_type{$ext};
191 $type = $ext_to_type{$ext};
193 die "Cannot upload image file without extension"
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'), '' ),
202 my $error = $template_image->insert();
203 die $error if $error;
213 Will be described here once found.