JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
added radios (untested)
[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 under the
8 #  terms of the GNU Lesser General Public License as published by the Free
9 #  Software Foundation; either version 2.1 of the License, or (at your option)
10 #  any later version.
11 #
12 #  wfpl is distributed in the hope that it will be useful, but WITHOUT ANY
13 #  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 #  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
15 #  more details.
16 #
17 #  You should have received a copy of the GNU Lesser General Public License
18 #  along with wfpl; if not, write to the Free Software Foundation, Inc., 51
19 #  Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20
21
22 # This file writes the code for you (sql, php, html, email) to handle a form.
23
24 require_once('code/wfpl/template.php');
25 require_once('code/wfpl/http.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         'int' =>        array('textbox',     'int',        'int'),
34         'bigint' =>     array('textbox',     'int',        'varchar(100)'), # up to 100 digits, stored as a string
35         'email' =>      array('textbox',     'email',      'varchar(100)'),
36         'phone' =>      array('textbox',     'phone',      'varchar(32)'),
37         'state' =>      array('states',      'oneline',    'varchar(2)'),
38         'money' =>      array('textbox',     'money',      'varchar(32)'),
39         'dollars' =>    array('textbox',     'dollars',    'varchar(32)'),
40         'url' =>        array('textbox',     'url',        'varchar(200)'),
41         'hidden' =>     array('hidden',      'unix',       'varchar(200)'),
42         'password' =>   array('password',    'oneline',    'varchar(200)'),
43         'textarea' =>   array('textarea',    'unix',       'text'),
44         'pulldown' =>   array('pulldown',    'options',    'int'),
45         'radio' =>      array('radio',       'oneline',    'varchar(200)'),
46         'checkbox' =>   array('checkbox',    'yesno',      'varchar(3)'),
47         'yesno' =>      array('checkbox',    'yesno',      'varchar(3)'),
48         'delete' =>     array('checkbox',    'yesno',      'n/a'),
49         'image' =>      array('image',       'oneline',    'varchar(200)'),
50         'submit' =>     array('submit',      'oneline',    'n/a')
51 );
52
53 if(isset($_REQUEST['form_name'])) {
54         $GLOBALS['form_name'] = ereg_replace('[^a-z0-9_-]', '', $_REQUEST['form_name']);
55 } else {
56         $GLOBALS['form_name'] = 'some_form';
57 }
58
59 if(isset($_REQUEST['fields'])) {
60         if(isset($_REQUEST['view_sql'])) {
61                 view_sql();
62                 exit();
63         } elseif(isset($_REQUEST['view_php'])) {
64                 view_php();
65                 exit();
66         } elseif(isset($_REQUEST['view_template'])) {
67                 view_template();
68                 exit();
69         } elseif(isset($_REQUEST['view_email'])) {
70                 view_email();
71                 exit();
72         } elseif(isset($_REQUEST['download_tar'])) {
73                 download_tar();
74                 exit();
75         } elseif(isset($_REQUEST['preview'])) {
76                 preview();
77                 exit();
78         } elseif(isset($_REQUEST['edit'])) {
79                 tem_set('fields', $_REQUEST['fields']);
80                 tem_set('form_name', $GLOBALS['form_name']);
81                 # fall through
82         } else {
83                 die("Sorry... couldn't tell which button you pressed");
84         }
85 }
86
87 set_form_action();
88 tem_output('code/wfpl/metaform/main.html');
89 exit();
90
91
92 function field_input($type)  { return $GLOBALS['types'][$type][0]; }
93 function field_format($type) { return $GLOBALS['types'][$type][1]; }
94 function field_sql($type)    { return $GLOBALS['types'][$type][2]; }
95
96 function get_fields() {
97         $fields_str = unix_newlines($_REQUEST['fields']);
98         $ret = array();
99         $fields_str = rtrim($fields_str);
100         $fields = split("\n", $fields_str);
101         foreach($fields as $field) {
102                 list($name, $type, $options) = split('  *', $field);
103                 if($options) $options = split(',', $options);
104                 if(!$type) $type = $name;
105                 $input = field_input($type);
106                 $format = field_format($type);
107                 $sql = field_sql($type);
108                 $ret[] = array($name, $type, $input, $format, $sql, $options);
109         }
110         return $ret;
111 }
112
113 # this one, that you're using to create forms
114 function set_form_action() {
115         $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
116         if($action == '') $action = './';
117         tem_set('form_action', $action);
118 }
119
120 # perfect HTTP headers for viewing created files
121 function view_headers() {
122         header('Content-type: text/plain');
123 }
124         
125
126
127
128 function make_sql() {
129         $tem = new tem();
130         $tem->load('code/wfpl/metaform/template.sql');
131         $tem->set('form_name', $GLOBALS['form_name']);
132         $fields = get_fields();
133         foreach($fields as $field) {
134                 list($name, $type, $input, $format, $sql) = $field;
135                 if($sql != 'n/a') {
136                         $tem->set('name', $name);
137                         $tem->set('type', $sql);
138                         if($sql == 'int') {
139                                 $tem->set('default', '0');
140                         } else {
141                                 $tem->set('default', '""');
142                         }
143                         $tem->sub('column');
144                 }
145         }
146         view_headers();
147         return $tem->run();
148 }
149
150 function view_sql() {
151         view_headers();
152         echo make_sql();
153 }
154         
155
156 # pass false if you want to exclude the <head> and <body> tag etc.
157 function make_template($whole_file = true) {
158         $uploads_output_already = false;
159         $tem = new tem();
160         $tem->load('code/wfpl/metaform/template.html');
161         $tem->set('form_name', $GLOBALS['form_name']);
162         $fields = get_fields();
163         foreach($fields as $field) {
164                 list($name, $type, $input, $format, $sql) = $field;
165                 $tem->set('name', $name);
166                 $tem->set('caption', $name); # fixme
167                 $tem->sub($input);
168                 if($input != 'hidden') {
169                         $tem->sub('row');
170                 }
171                 if($input == 'image' && !$uploads_output_already) {
172                         $tem->sub('uploads');
173                         $tem->set('enctype_attr', '" enctype="multipart/form-data');
174                         $uploads_output_already = true;
175                 }
176         }
177         $tem->set('name', 'save');
178         $tem->set('caption', 'Save');
179         $tem->sub('submit');
180         $tem->sub('row');
181         $tem->sub('form');
182         if($whole_file) {
183                 return $tem->run();
184         } else {
185                 return $tem->get('form');
186         }
187 }
188
189 function view_template() {
190         view_headers();
191         echo make_template();
192 }
193
194
195 function make_php() {
196         $tem = new tem();
197         $tem->load('code/wfpl/metaform/template.php');
198         $tem->set('form_name', $GLOBALS['form_name']);
199         $fields = get_fields();
200         $db_fields = '';
201         $php_fields = '';
202         $always_field = false;
203         $image_included_yet = false;
204         foreach($fields as $field) {
205                 list($name, $type, $input, $format, $sql) = $field;
206                 if($input != 'submit') {
207                         $tem->set('format', $format);
208                         $tem->set('name', $name);
209                         $tem->set('db_field', ''); # we don't want to use the value from last time
210                         if($sql != 'n/a') {
211                                 if($db_fields != '') $db_fields .= ',';
212                                 $db_fields .= $name;
213                                 if($php_fields != '') $php_fields .= ', ';
214                                 $php_fields .= '$' . $name;
215                         }
216                         if($input == 'image') {
217                                 $tem->sub('image_upload');
218                                 $tem->sub('image_db');
219                                 if(!$image_included_yet) {
220                                         $tem->sub('image_include');
221                                         $tem->sub('upload_max');
222                                         $tem->sub('upload_settings');
223                                         $image_included_yet = true;
224                                 }
225                         } else {
226                                 $tem->sub('formats');
227                                 $tem->sub('tem_sets');
228                         }
229                         if(!$always_field and $input != 'checkbox' and $input != 'radio') {
230                                 $always_field = $name;
231                         }
232                 }
233         }
234         # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
235         $tem->set('always_field', $always_field);
236         $tem->set('db_fields', $db_fields);
237         $tem->set('php_fields', $php_fields);
238         $tem->set('metaform_url', edit_url());
239         return $tem->run();
240 }
241
242 # make a URL for the edit page with all the fields filled in
243 function edit_url() {
244         $url = this_url();
245         $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
246         $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
247         $url = ereg_replace('/[a-z0-9_.]*\?', '/?', $url);
248         return $url;
249 }
250
251 function view_php() {
252         view_headers();
253         echo make_php();
254 }
255
256
257 function make_email() {
258         $tem = new tem();
259         $tem->load('code/wfpl/metaform/template.email.txt');
260         $tem->set('form_name', $GLOBALS['form_name']);
261         $fields = get_fields();
262         foreach($fields as $field) {
263                 list($name, $type, $input, $format, $sql) = $field;
264                 $tem->set('name', $name);
265                 $tem->set('caption', $name); # fixme
266                 if($type == 'textarea') {
267                         $tem->sub('multi_line');
268                 } else {
269                         $tem->sub('fields');
270                 }
271         }
272         return $tem->run();
273 }
274
275 function view_email() {
276         view_headers();
277         echo make_email();
278 }
279
280
281 function preview() {
282         $tem = new tem();
283         $tem->load('code/wfpl/metaform/preview.html');
284         $tem->set('form_name', $GLOBALS['form_name']);
285         $tem->set('fields', $_REQUEST['fields']);
286         $preview_tem = new tem();
287         $preview = $preview_tem->run(make_template(false));
288         unset($preview_tem);
289         $tem->set('preview', $preview);
290         set_form_action();
291         $tem->output();
292 }
293
294 function download_tar() {
295         $name = $GLOBALS['form_name'];
296         $data = array(
297                 "$name.html" => make_template(),
298                 "$name.sql" => make_sql(),
299                 "$name.email.txt" => make_email(),
300                 "$name.php" => make_php());
301         make_wfpl_tar($name, $data);
302 }
303
304 ?>