JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
make metaform support file uploads
authorJason Woofenden <jason@jasonwoof.com>
Tue, 5 Oct 2010 20:56:14 +0000 (16:56 -0400)
committerJason Woofenden <jason@jasonwoof.com>
Tue, 5 Oct 2010 20:56:14 +0000 (16:56 -0400)
metaform.php
metaform/template.html
metaform/template.php

index 3e8c380..e66a477 100644 (file)
@@ -51,6 +51,7 @@ $GLOBALS['types'] = array(
        'delete' =>     array('checkbox',    'yesno',      'n/a'),
        'image' =>      array('image',       'oneline',    'varchar(120)'),
        'thumb' =>      array('image',       'oneline',    'varchar(240)'),
+       'file' =>       array('file',        'oneline',    'varchar(100)'),
        'submit' =>     array('submit',      'oneline',    'n/a')
 );
 
@@ -200,7 +201,7 @@ function view_sql() {
 function find_always_field($fields) {
        foreach($fields as $field) {
                list($name, $type, $input, $format, $sql) = $field;
-               if($input != 'submit' && $input != 'image' && $input != 'checkbox' && $input != 'radio') {
+               if($input != 'submit' && $input != 'image' && $input != 'file' && $input != 'checkbox' && $input != 'radio') {
                        return $name;
                }
        }
@@ -231,7 +232,7 @@ function make_html($whole_file = true) {
                        $tem->show('row');
                }
 
-               if($input == 'image' && !$uploads_output_already) {
+               if(($input == 'image' || $input='file') && !$uploads_output_already) {
                        $tem->show('uploads');
                        $tem->set('enctype_attr', '" enctype="multipart/form-data');
                        $uploads_output_already = true;
@@ -377,12 +378,11 @@ function make_php() {
                                }
                                $tem->show('image_settings');
                                $tem->show('image_upload');
-                               if(!$image_included_yet) {
-                                       $tem->show('image_include');
-                                       $tem->show('upload_max');
-                                       $tem->show('upload_settings');
-                                       $image_included_yet = true;
-                               }
+                               $has_uploads = true;
+                       } else if($input == 'file') {
+                               $tem->show('file_settings');
+                               $tem->show('file_upload');
+                               $has_uploads = true;
                        } else {
                                if($input == 'html') {
                                        $has_html_editors = true;
@@ -401,6 +401,12 @@ function make_php() {
                        }
                }
        }
+       if($has_uploads) {
+               $tem->show('uploads_include');
+               $tem->show('upload_max');
+               $tem->show('upload_settings');
+               $image_included_yet = true;
+       }
 
        if($has_html_editors) {
                $tem->show('show_extra_headers');
index 93702c3..613013e 100644 (file)
@@ -79,6 +79,8 @@
     <form action="~file_name~" method="post~enctype_attr~"><!--~opt_db_2 {~--><!--~~editing {~~--><div style="display: none"><input type="hidden" name="~file_name~_edit_id" value="~~~file_name~_edit_id attr~~" /></div><!--~~}~~--><!--~}~--><!--~uploads {~--><input type="hidden" name="MAX_FILE_SIZE" value="~~upload_max_filesize~~" /><!--~}~-->
 <!--~row {~--><!--~image {~-->
       <div class="caption">~caption html~</div>
+      <div class="field"><input type="file" name="~name~" /><input type="hidden" name="old_~name~" value="~~~name~ attr~~" /></div><!--~}~--><!--~file {~-->
+      <div class="caption">~caption html~</div>
       <div class="field"><input type="file" name="~name~" /><input type="hidden" name="old_~name~" value="~~~name~ attr~~" /></div><!--~}~--><!--~textbox {~-->
       <div class="caption">~caption html~</div>
       <div class="field"><input type="text" name="~name~" value="~~~name~ attr~~" /></div><!--~}~--><!--~date {~-->
index 6ae0602..daf23ed 100644 (file)
@@ -25,6 +25,8 @@ define('~file_name upper~_DB_FIELDS', '~db_fields~');
 # Set this to the path to your uploads directory. It can be relative to the
 # location of this script. IT MUST END WITH A SLASH
 $GLOBALS['upload_directory'] = 'uploads/';
+<!--~}~--><!--~file_settings {~-->
+$GLOBALS['~name~_file_name'] = uniqid() . getmypid() . '.txt'; # comment this out to use uploader's filename
 <!--~}~--><!--~image_settings {~-->
 $GLOBALS['~name~_max_width'] = '400';
 $GLOBALS['~name~_max_height'] = '400';<!--~thumb_settings {~-->
@@ -37,7 +39,7 @@ require_once('code/wfpl/template.php');
 require_once('code/wfpl/format.php');
 require_once('code/wfpl/messages.php');
 require_once('code/wfpl/email.php');<!--~opt_db_2 {~-->
-require_once('code/db_connect.php');<!--~}~--><!--~image_include {~-->
+require_once('code/db_connect.php');<!--~}~--><!--~uploads_include {~-->
 require_once('code/wfpl/upload.php');<!--~}~-->
 
 function ~file_name~_get_fields() {
@@ -52,6 +54,19 @@ function ~file_name~_get_fields() {
                } else {
                        $data['~name~'] = format_image_w_h<!--~thumb_w_h {~-->_thumb_w_h<!--~}~-->($_REQUEST['old_~name~']);
                }
+       }<!--~}~--><!--~file_upload {~-->
+       $~name~_filename_tmp = $GLOBALS['~name~_file_name'];
+       if(!$~name~_filename_tmp) {
+               $~name~_filename_tmp = format_filename($_REQUEST['~name~']);
+       }
+       if($_FILES['~name~'] && $_FILES['~name~']['error'] == 0) {
+               $data['~name~'] = save_uploaded_file('~name~', $GLOBALS['upload_directory'] . $~name~_filename_tmp);
+       } else {
+               if($_REQUEST['delete_~name~'] == 'Yes') {
+                       $data['~name~'] = '';
+               } else {
+                       $data['~name~'] = format_path($_REQUEST['old_~name~']);
+               }
        }<!--~}~-->
 
        return $data;