JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
added image support to metaform and fixed upload.php
[wfpl.git] / metaform.php
index d8cc07e..52a41fc 100644 (file)
@@ -36,10 +36,14 @@ $GLOBALS['types'] = array(
        'money' =>      array('textbox',     'money',      'varchar(32)'),
        'dollars' =>    array('textbox',     'dollars',    'varchar(32)'),
        'url' =>        array('textbox',     'url',        'varchar(200)'),
+       'hidden' =>     array('hidden',      'unix',       'varchar(200)'),
+       'password' =>   array('password',    'oneline',    'varchar(200)'),
        'textarea' =>   array('textarea',    'unix',       'text'),
        'pulldown' =>   array('pulldown',    'options',    'int'),
-       'checkbox' =>   array('checkbox',    'yesno',      'int'),
-       'yesno' =>      array('checkbox',    'yesno',      'int'),
+       'checkbox' =>   array('checkbox',    'yesno',      'varchar(3)'),
+       'yesno' =>      array('checkbox',    'yesno',      'varchar(3)'),
+       'delete' =>     array('checkbox',    'yesno',      'n/a'),
+       'image' =>      array('image',       'oneline',    'varchar(200)'),
        'submit' =>     array('submit',      'oneline',    'n/a')
 );
 
@@ -103,6 +107,7 @@ function get_fields() {
        return $ret;
 }
 
+# this one, that you're using to create forms
 function set_form_action() {
        $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
        if($action == '') $action = './';
@@ -147,6 +152,7 @@ function view_sql() {
 
 # pass false if you want to exclude the <head> and <body> tag etc.
 function make_template($whole_file = true) {
+       $uploads_output_already = false;
        $tem = new tem();
        $tem->load('code/wfpl/metaform/template.html');
        $tem->set('form_name', $GLOBALS['form_name']);
@@ -156,7 +162,14 @@ function make_template($whole_file = true) {
                $tem->set('name', $name);
                $tem->set('caption', $name); # fixme
                $tem->sub($input);
-               $tem->sub('row');
+               if($input != 'hidden') {
+                       $tem->sub('row');
+               }
+               if($input == 'image' && !$uploads_output_already) {
+                       $tem->sub('uploads');
+                       $tem->set('enctype_attr', '" enctype="multipart/form-data');
+                       $uploads_output_already = true;
+               }
        }
        $tem->set('name', 'save');
        $tem->set('caption', 'Save');
@@ -182,7 +195,9 @@ function make_php() {
        $tem->set('form_name', $GLOBALS['form_name']);
        $fields = get_fields();
        $db_fields = '';
+       $php_fields = '';
        $always_field = false;
+       $image_included_yet = false;
        foreach($fields as $field) {
                list($name, $type, $input, $format, $sql) = $field;
                if($input != 'submit') {
@@ -190,11 +205,24 @@ function make_php() {
                        $tem->set('name', $name);
                        $tem->set('db_field', ''); # we don't want to use the value from last time
                        if($sql != 'n/a') {
-                               $tem->sub('db_field');
                                if($db_fields != '') $db_fields .= ',';
                                $db_fields .= $name;
+                               if($php_fields != '') $php_fields .= ', ';
+                               $php_fields .= '$' . $name;
+                       }
+                       if($input == 'image') {
+                               $tem->sub('image_upload');
+                               $tem->sub('image_db');
+                               if(!$image_included_yet) {
+                                       $tem->sub('image_include');
+                                       $tem->sub('upload_max');
+                                       $tem->sub('upload_settings');
+                                       $image_included_yet = true;
+                               }
+                       } else {
+                               $tem->sub('formats');
+                               $tem->sub('tem_sets');
                        }
-                       $tem->sub('formats');
                        if(!$always_field and $input != 'checkbox' and $input != 'radio') {
                                $always_field = $name;
                        }
@@ -203,6 +231,7 @@ function make_php() {
        # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
        $tem->set('always_field', $always_field);
        $tem->set('db_fields', $db_fields);
+       $tem->set('php_fields', $php_fields);
        return $tem->run();
 }