JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
metaform working. added some encodings and formatters.
[wfpl.git] / metaform.php
1 <?php
2
3 require_once('code/wfpl/template.php');
4
5 # see code/wfpl/metaform/template.html for the html templates for these elements
6 $GLOBALS['types'] = array(
7 #    type                  input          format        sql     
8         'name' =>       array('textbox',     'oneline',    'varchar(200)'),
9         'textbox' =>    array('textbox',     'oneline',    'varchar(200)'),
10         'email' =>      array('textbox',     'email',      'varchar(100)'),
11         'phone' =>      array('textbox',     'phone',      'varchar(32)'),
12         'money' =>      array('textbox',     'money',      'varchar(32)'),
13         'dollars' =>    array('textbox',     'dollars',    'varchar(32)'),
14         'url' =>        array('textbox',     'url',        'varchar(200)'),
15         'textarea' =>   array('textarea',    'unix',       'text'),
16         'pulldown' =>   array('pulldown',    'options',    'int'),
17         'checkbox' =>   array('checkbox',    'yesno',   'int'),
18         'yesno' =>      array('checkbox',    'yesno',   'int'),
19         'submit' =>     array('submit',      'oneline',    'n/a')
20 );
21
22 if(isset($_REQUEST['form_name'])) {
23         $GLOBALS['form_name'] = $_REQUEST['form_name'];
24 } else {
25         $GLOBALS['form_name'] = 'some_form';
26 }
27
28 if(isset($_REQUEST['fields'])) {
29         if(isset($_REQUEST['download_sql'])) {
30                 download_sql();
31                 exit();
32         } elseif(isset($_REQUEST['download_php'])) {
33                 download_php();
34                 exit();
35         } elseif(isset($_REQUEST['download_template'])) {
36                 download_template();
37                 exit();
38         } elseif(isset($_REQUEST['download_email'])) {
39                 download_email();
40                 exit();
41         } else {
42                 tem_set('message', "Sorry... couldn't tell which button you pressed");
43                 # fall through
44         }
45 } else {
46         tem_output('code/wfpl/metaform/main.html');
47 }
48
49 function field_input($type)  { return $GLOBALS['types'][$type][0]; }
50 function field_format($type) { return $GLOBALS['types'][$type][1]; }
51 function field_sql($type)    { return $GLOBALS['types'][$type][2]; }
52
53 function get_fields() {
54         $fields_str = unix_newlines($_REQUEST['fields']);
55         $ret = array();
56         $fields_str = rtrim($fields_str);
57         $fields = split("\n", $fields_str);
58         foreach($fields as $field) {
59                 list($name, $type, $options) = split('  *', $field);
60                 if($options) $options = split(',', $options);
61                 if(!$type) $type = $name;
62                 $input = field_input($type);
63                 $format = field_format($type);
64                 $sql = field_sql($type);
65                 $ret[] = array($name, $type, $input, $format, $sql, $options);
66         }
67         return $ret;
68 }
69
70 function download_headers() {
71         header('Content-type: application/octet-stream');
72         header('Content-disposition: download'); # is this correct? does it do anything?
73 }
74         
75
76
77
78 function download_sql() {
79         tem_load('code/wfpl/metaform/template.sql');
80         tem_set('form_name', $GLOBALS['form_name']);
81         $fields = get_fields();
82         foreach($fields as $field) {
83                 list($name, $type, $input, $format, $sql) = $field;
84                 if($sql != 'n/a') {
85                         tem_set('name', $name);
86                         tem_set('type', $sql);
87                         if($sql == 'int') {
88                                 tem_set('default', '0');
89                         } else {
90                                 tem_set('default', '""');
91                         }
92                         tem_sub('column');
93                 }
94         }
95         download_headers();
96         tem_output();
97 }
98         
99
100 function download_template() {
101         tem_load('code/wfpl/metaform/template.html');
102         tem_set('form_name', $GLOBALS['form_name']);
103         $fields = get_fields();
104         foreach($fields as $field) {
105                 list($name, $type, $input, $format, $sql) = $field;
106                 tem_set('name', $name);
107                 tem_set('caption', $name); # fixme
108                 tem_sub($input);
109         }
110         tem_set('name', 'save');
111         tem_set('caption', 'Save');
112         tem_sub('submit');
113         download_headers();
114         tem_output();
115 }
116
117
118 function download_php() {
119         tem_load('code/wfpl/metaform/template.php');
120         tem_set('form_name', $GLOBALS['form_name']);
121         $fields = get_fields();
122         $db_fields = '';
123         $always_field = false;
124         foreach($fields as $field) {
125                 list($name, $type, $input, $format, $sql) = $field;
126                 if($input != 'submit') {
127                         tem_set('format', $format);
128                         tem_set('name', $name);
129                         tem_set('db_field', ''); # we don't want to use the value from last time
130                         if($sql != 'n/a') {
131                                 tem_sub('db_field');
132                                 if($db_fields != '') $db_fields .= ',';
133                                 $db_fields .= $name;
134                         }
135                         tem_sub('formats');
136                         if(!$always_field and $input != 'checkbox' and $input != 'radio') {
137                                 $always_field = $name;
138                         }
139                 }
140         }
141         # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
142         tem_set('always_field', $always_field);
143         tem_set('db_fields', $db_fields);
144         download_headers();
145         tem_output();
146 }
147
148
149 function download_email() {
150         tem_load('code/wfpl/metaform/template.email.txt');
151         tem_set('form_name', $GLOBALS['form_name']);
152         $fields = get_fields();
153         foreach($fields as $field) {
154                 list($name, $type, $input, $format, $sql) = $field;
155                 tem_set('name', $name);
156                 tem_set('caption', $name); # fixme
157                 tem_sub('fields');
158         }
159         download_headers();
160         tem_output();
161 }
162
163 ?>