JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
added tar.php. metaform working
[wfpl.git] / metaform.php
1 <?php
2
3 #  Copyright (C) 2006 Jason Woofenden
4 #
5 #  This file is part of wfpl.
6 #
7 #  wfpl is free software; you can redistribute it and/or modify it
8 #  under the terms of the GNU General Public License as published by
9 #  the Free Software Foundation; either version 2, or (at your option)
10 #  any later version.
11 #
12 #  wfpl is distributed in the hope that it will be useful, but
13 #  WITHOUT ANY WARRANTY; without even the implied warranty of
14 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 #  General Public License for more details.
16 #
17 #  You should have received a copy of the GNU General Public License
18 #  along with wfpl; see the file COPYING.  If not, write to the
19 #  Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 #  MA 02111-1307, USA.
21
22
23 # This file writes the code for you (sql, php, html, email) to handle a form.
24
25 require_once('code/wfpl/template.php');
26 require_once('code/wfpl/tar.php');
27
28 # see code/wfpl/metaform/template.html for the html templates for these elements
29 $GLOBALS['types'] = array(
30 #    type                  input          format        sql     
31         'name' =>       array('textbox',     'oneline',    'varchar(200)'),
32         'textbox' =>    array('textbox',     'oneline',    'varchar(200)'),
33         'email' =>      array('textbox',     'email',      'varchar(100)'),
34         'phone' =>      array('textbox',     'phone',      'varchar(32)'),
35         'money' =>      array('textbox',     'money',      'varchar(32)'),
36         'dollars' =>    array('textbox',     'dollars',    'varchar(32)'),
37         'url' =>        array('textbox',     'url',        'varchar(200)'),
38         'textarea' =>   array('textarea',    'unix',       'text'),
39         'pulldown' =>   array('pulldown',    'options',    'int'),
40         'checkbox' =>   array('checkbox',    'yesno',      'int'),
41         'yesno' =>      array('checkbox',    'yesno',      'int'),
42         'submit' =>     array('submit',      'oneline',    'n/a')
43 );
44
45 if(isset($_REQUEST['form_name'])) {
46         $GLOBALS['form_name'] = ereg_replace('[^a-z0-9_-]', '', $_REQUEST['form_name']);
47 } else {
48         $GLOBALS['form_name'] = 'some_form';
49 }
50
51 if(isset($_REQUEST['fields'])) {
52         if(isset($_REQUEST['view_sql'])) {
53                 view_sql();
54                 exit();
55         } elseif(isset($_REQUEST['view_php'])) {
56                 view_php();
57                 exit();
58         } elseif(isset($_REQUEST['view_template'])) {
59                 view_template();
60                 exit();
61         } elseif(isset($_REQUEST['view_email'])) {
62                 view_email();
63                 exit();
64         } elseif(isset($_REQUEST['download_tar'])) {
65                 download_tar();
66                 exit();
67         } elseif(isset($_REQUEST['preview'])) {
68                 preview();
69                 exit();
70         } elseif(isset($_REQUEST['edit'])) {
71                 tem_set('fields', $_REQUEST['fields']);
72                 tem_set('form_name', $GLOBALS['form_name']);
73                 # fall through
74         } else {
75                 die("Sorry... couldn't tell which button you pressed");
76         }
77 }
78
79 set_form_action();
80 tem_output('code/wfpl/metaform/main.html');
81 exit();
82
83
84 function field_input($type)  { return $GLOBALS['types'][$type][0]; }
85 function field_format($type) { return $GLOBALS['types'][$type][1]; }
86 function field_sql($type)    { return $GLOBALS['types'][$type][2]; }
87
88 function get_fields() {
89         $fields_str = unix_newlines($_REQUEST['fields']);
90         $ret = array();
91         $fields_str = rtrim($fields_str);
92         $fields = split("\n", $fields_str);
93         foreach($fields as $field) {
94                 list($name, $type, $options) = split('  *', $field);
95                 if($options) $options = split(',', $options);
96                 if(!$type) $type = $name;
97                 $input = field_input($type);
98                 $format = field_format($type);
99                 $sql = field_sql($type);
100                 $ret[] = array($name, $type, $input, $format, $sql, $options);
101         }
102         return $ret;
103 }
104
105 function set_form_action() {
106         $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
107         if($action == '') $action = './';
108         tem_set('form_action', $action);
109 }
110
111 # perfect HTTP headers for viewing created files
112 function view_headers() {
113         header('Content-type: text/plain');
114 }
115         
116
117
118
119 function make_sql() {
120         $tem = new tem();
121         $tem->load('code/wfpl/metaform/template.sql');
122         $tem->set('form_name', $GLOBALS['form_name']);
123         $fields = get_fields();
124         foreach($fields as $field) {
125                 list($name, $type, $input, $format, $sql) = $field;
126                 if($sql != 'n/a') {
127                         $tem->set('name', $name);
128                         $tem->set('type', $sql);
129                         if($sql == 'int') {
130                                 $tem->set('default', '0');
131                         } else {
132                                 $tem->set('default', '""');
133                         }
134                         $tem->sub('column');
135                 }
136         }
137         view_headers();
138         return $tem->run();
139 }
140
141 function view_sql() {
142         view_headers();
143         echo make_sql();
144 }
145         
146
147 # pass false if you want to exclude the <head> and <body> tag etc.
148 function make_template($whole_file = true) {
149         $tem = new tem();
150         $tem->load('code/wfpl/metaform/template.html');
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($input);
158         }
159         $tem->set('name', 'save');
160         $tem->set('caption', 'Save');
161         $tem->sub('submit');
162         $tem->sub('body');
163         if($whole_file) {
164                 return $tem->run();
165         } else {
166                 return $tem->get('body');
167         }
168 }
169
170 function view_template() {
171         view_headers();
172         echo make_template();
173 }
174
175
176 function make_php() {
177         $tem = new tem();
178         $tem->load('code/wfpl/metaform/template.php');
179         $tem->set('form_name', $GLOBALS['form_name']);
180         $fields = get_fields();
181         $db_fields = '';
182         $always_field = false;
183         foreach($fields as $field) {
184                 list($name, $type, $input, $format, $sql) = $field;
185                 if($input != 'submit') {
186                         $tem->set('format', $format);
187                         $tem->set('name', $name);
188                         $tem->set('db_field', ''); # we don't want to use the value from last time
189                         if($sql != 'n/a') {
190                                 $tem->sub('db_field');
191                                 if($db_fields != '') $db_fields .= ',';
192                                 $db_fields .= $name;
193                         }
194                         $tem->sub('formats');
195                         if(!$always_field and $input != 'checkbox' and $input != 'radio') {
196                                 $always_field = $name;
197                         }
198                 }
199         }
200         # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
201         $tem->set('always_field', $always_field);
202         $tem->set('db_fields', $db_fields);
203         return $tem->run();
204 }
205
206 function view_php() {
207         view_headers();
208         echo make_php();
209 }
210
211
212 function make_email() {
213         $tem = new tem();
214         $tem->load('code/wfpl/metaform/template.email.txt');
215         $tem->set('form_name', $GLOBALS['form_name']);
216         $fields = get_fields();
217         foreach($fields as $field) {
218                 list($name, $type, $input, $format, $sql) = $field;
219                 $tem->set('name', $name);
220                 $tem->set('caption', $name); # fixme
221                 $tem->sub('fields');
222         }
223         return $tem->run();
224 }
225
226 function view_email() {
227         view_headers();
228         echo make_email();
229 }
230
231
232 function preview() {
233         $tem = new tem();
234         $tem->load('code/wfpl/metaform/preview.html');
235         $tem->set('form_name', $GLOBALS['form_name']);
236         $tem->set('fields', $_REQUEST['fields']);
237         $tem->set('preview', make_template(false));
238         set_form_action();
239         $tem->output();
240 }
241
242 function download_tar() {
243         $name = $GLOBALS['form_name'];
244         $data = array(
245                 "$name.html" => make_template(),
246                 "$name.sql" => make_sql(),
247                 "$name.email.txt" => make_email(),
248                 "$name.php" => make_php());
249         make_tar($name, $data);
250 }
251
252 ?>