JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
metaform: added pulldown support, added leftcheck
[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 require_once('code/wfpl/format.php');
28
29 # see code/wfpl/metaform/template.html for the html templates for these elements
30 $GLOBALS['types'] = array(
31 #    type                  input          format        sql     
32         'name' =>       array('textbox',     'oneline',    'varchar(200)'),
33         'textbox' =>    array('textbox',     'oneline',    'varchar(200)'),
34         'int' =>        array('textbox',     'int',        'int'),
35         'bigint' =>     array('textbox',     'int',        'varchar(100)'), # up to 100 digits, stored as a string
36         'email' =>      array('textbox',     'email',      'varchar(100)'),
37         'phone' =>      array('textbox',     'phone',      'varchar(32)'),
38         'state' =>      array('states',      'oneline',    'varchar(2)'),
39         'money' =>      array('textbox',     'money',      'varchar(32)'),
40         'dollars' =>    array('textbox',     'dollars',    'varchar(32)'),
41         'url' =>        array('textbox',     'url',        'varchar(200)'),
42         'hidden' =>     array('hidden',      'unix',       'varchar(200)'),
43         'password' =>   array('password',    'oneline',    'varchar(200)'),
44         'textarea' =>   array('textarea',    'unix',       'text'),
45         'html' =>       array('html',        'unix',       'text'),
46         'pulldown' =>   array('pulldown',    'options',    'varchar(100)'),
47         'radio' =>      array('radio',       'oneline',    'varchar(200)'),
48         'leftcheck' =>  array('leftcheck',   'yesno',      'varchar(3)'),
49         'checkbox' =>   array('checkbox',    'yesno',      'varchar(3)'),
50         'yesno' =>      array('checkbox',    'yesno',      'varchar(3)'),
51         'delete' =>     array('checkbox',    'yesno',      'n/a'),
52         'image' =>      array('image',       'oneline',    'varchar(200)'),
53         'submit' =>     array('submit',      'oneline',    'n/a')
54 );
55
56 function list_available_types() {
57         $types = '';
58         foreach($GLOBALS['types'] as $key => $value) {
59                 if($types) {
60                         $types .= ', ';
61                 }
62                 $types .= $key;
63         }
64         tem_set('available_types', $types);
65 }
66
67
68 function metaform() {
69         if(isset($_REQUEST['form_name'])) {
70                 $GLOBALS['form_name'] = ereg_replace('[^a-z0-9_-]', '', $_REQUEST['form_name']);
71                 $GLOBALS['opt_email'] = format_yesno($_REQUEST['opt_email']);
72                 tem_set('opt_email', $GLOBALS['opt_email']);
73                 $GLOBALS['opt_db'] = format_yesno($_REQUEST['opt_db']);
74                 tem_set('opt_db', $GLOBALS['opt_db']);
75                 $GLOBALS['opt_http_pass'] = format_yesno($_REQUEST['opt_http_pass']);
76                 tem_set('opt_http_pass', $GLOBALS['opt_http_pass']);
77         } else {
78                 $GLOBALS['form_name'] = 'some_form';
79         }
80
81         if(isset($_REQUEST['fields'])) {
82                 if(isset($_REQUEST['view_sql'])) {
83                         view_sql();
84                         exit();
85                 } elseif(isset($_REQUEST['view_php'])) {
86                         view_php();
87                         exit();
88                 } elseif(isset($_REQUEST['view_html'])) {
89                         view_html();
90                         exit();
91                 } elseif(isset($_REQUEST['view_email'])) {
92                         view_email();
93                         exit();
94                 } elseif(isset($_REQUEST['download_tar'])) {
95                         download_tar();
96                         exit();
97                 } elseif(isset($_REQUEST['preview'])) {
98                         preview();
99                         exit();
100                 } elseif(isset($_REQUEST['edit'])) {
101                         tem_set('fields', $_REQUEST['fields']);
102                         tem_set('form_name', $GLOBALS['form_name']);
103                         # fall through
104                 } else {
105                         die("Sorry... couldn't tell which button you pressed");
106                 }
107         }
108
109
110         set_form_action();
111         list_available_types();
112         tem_output('code/wfpl/metaform/main.html');
113 }
114
115
116 function field_input($type)  { return $GLOBALS['types'][$type][0]; }
117 function field_format($type) { return $GLOBALS['types'][$type][1]; }
118 function field_sql($type)    { return $GLOBALS['types'][$type][2]; }
119
120 function get_fields() {
121         # no sense in doing all this so many times
122         if(isset($GLOBALS['gotten_fields'])) {
123                 return $GLOBALS['gotten_fields'];
124         }
125
126         $fields_str = unix_newlines($_REQUEST['fields']);
127         $GLOBALS['gotten_fields'] = array();
128         $fields_str = rtrim($fields_str);
129         $fields = split("\n", $fields_str);
130         foreach($fields as $field) {
131                 list($name, $type, $options) = split('  *', $field);
132                 if($options) $options = split(',', $options);
133                 if(!$type) $type = $name;
134                 $input = field_input($type);
135                 $format = field_format($type);
136                 $sql = field_sql($type);
137                 $GLOBALS['gotten_fields'][] = array($name, $type, $input, $format, $sql, $options);
138         }
139         return $GLOBALS['gotten_fields'];
140 }
141
142 # this one, that you're using to create forms
143 function set_form_action() {
144         $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
145         if($action == '') $action = './';
146         tem_set('form_action', $action);
147 }
148
149 # perfect HTTP headers for viewing created files
150 function view_headers() {
151         header('Content-type: text/plain');
152 }
153         
154
155
156
157 function make_sql() {
158         $tem = new tem();
159         $tem->load('code/wfpl/metaform/template.sql');
160         $tem->set('form_name', $GLOBALS['form_name']);
161         $fields = get_fields();
162         foreach($fields as $field) {
163                 list($name, $type, $input, $format, $sql) = $field;
164                 if($sql != 'n/a') {
165                         $tem->set('name', $name);
166                         $tem->set('type', $sql);
167                         if($sql == 'int') {
168                                 $tem->set('default', '0');
169                         } else {
170                                 $tem->set('default', '""');
171                         }
172                         $tem->sub('column');
173                 }
174         }
175         view_headers();
176         return $tem->run();
177 }
178
179 function view_sql() {
180         view_headers();
181         echo make_sql();
182 }
183         
184
185 # pass false if you want to exclude the <head> and <body> tag etc.
186 function make_html($whole_file = true) {
187         $uploads_output_already = false;
188         $has_html_editors = false;
189         $tem = new tem();
190         $tem->load('code/wfpl/metaform/template.html');
191         $tem->set('form_name', $GLOBALS['form_name']);
192         $fields = get_fields();
193         foreach($fields as $field) {
194                 list($name, $type, $input, $format, $sql) = $field;
195                 $tem->set('name', $name);
196                 $tem->set('caption', format_caption($name));
197                 $tem->sub($input);
198                 if($input != 'hidden') {
199                         $tem->sub('row');
200                 }
201                 if($input == 'image' && !$uploads_output_already) {
202                         $tem->sub('uploads');
203                         $tem->set('enctype_attr', '" enctype="multipart/form-data');
204                         $uploads_output_already = true;
205                 } elseif($input == 'html') {
206                         $has_html_editors = true;
207                         $tem->set('html_field_name', $name);
208                         $tem->sub('replace_textarea');
209                 }
210         }
211
212         if($GLOBALS['opt_db'] == 'Yes') {
213                 $tem->sub('opt_db_1');
214                 $tem->sub('opt_db_2');
215         } else {
216                 $tem->sub('opt_db_1_else');
217         }
218
219         if($GLOBALS['opt_email'] == 'Yes' && $GLOBALS['opt_db'] != 'Yes') {
220                 $tem->set('name', 'send');
221                 $tem->set('caption', 'Send');
222         } else {
223                 $tem->set('name', 'save');
224                 $tem->set('caption', 'Save');
225         }
226         $tem->sub('submit');
227         $tem->sub('row');
228
229         $tem->sub('form');
230
231         if($has_html_editors) {
232                 $tem->sub('html_editor_headers');
233         }
234
235         if($whole_file) {
236                 return $tem->run();
237         } else {
238                 return $tem->get('form');
239         }
240 }
241
242 function view_html() {
243         view_headers();
244         echo make_html();
245 }
246
247
248 function make_php() {
249         $tem = new tem();
250         $tem->load('code/wfpl/metaform/template.php');
251         $tem->set('form_name', $GLOBALS['form_name']);
252         $fields = get_fields();
253         $db_fields = '';
254         $php_fields = '';
255         $always_field = false;
256         $image_included_yet = false;
257         foreach($fields as $field) {
258                 list($name, $type, $input, $format, $sql) = $field;
259                 if($input != 'submit') {
260                         $tem->set('format', $format);
261                         $tem->set('name', $name);
262                         $tem->set('db_field', ''); # we don't want to use the value from last time
263                         if($sql != 'n/a') {
264                                 if($db_fields != '') $db_fields .= ',';
265                                 $db_fields .= $name;
266                                 if($php_fields != '') $php_fields .= ', ';
267                                 $php_fields .= '$' . $name;
268                         }
269                         if($input == 'image') {
270                                 $tem->sub('image_upload');
271                                 $tem->sub('image_db');
272                                 if(!$image_included_yet) {
273                                         $tem->sub('image_include');
274                                         $tem->sub('upload_max');
275                                         $tem->sub('upload_settings');
276                                         $image_included_yet = true;
277                                 }
278                         } else {
279                                 if($input == 'pulldown') {
280                                         $tem->sub('pulldowns');
281                                         $tem->sub('pulldown_format_extra');
282                                 }
283                                 $tem->sub('formats');
284                         }
285                         $tem->sub('tem_sets');
286                         if(!$always_field and $input != 'checkbox' and $input != 'radio') {
287                                 $always_field = $name;
288                         }
289                 }
290         }
291         # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
292         $tem->set('always_field', $always_field);
293         $tem->set('db_fields', $db_fields);
294         $tem->set('php_fields', $php_fields);
295         $tem->set('metaform_url', edit_url());
296         if($GLOBALS['opt_db'] == 'Yes') {
297                 $tem->sub('opt_db_1');
298                 $tem->sub('opt_db_2');
299                 $tem->sub('opt_db_3');
300                 $tem->sub('opt_db_4');
301                 $tem->sub('opt_db_5');
302         }
303         if($GLOBALS['opt_email'] == 'Yes') {
304                 $tem->sub('opt_email_1');
305                 $tem->sub('opt_email_2');
306         }
307         if($GLOBALS['opt_http_pass'] == 'Yes') {
308                 $tem->sub('opt_http_pass_1');
309                 $tem->sub('opt_http_pass_2');
310         }
311         return $tem->run();
312 }
313
314 # make a URL for the edit page with all the fields filled in
315 function edit_url() {
316         $url = this_url();
317         $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
318         $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
319         $url = ereg_replace('/[a-z0-9_.]*\?', '/?', $url);
320         $url = str_replace('jasonwoof.l', 'jasonwoof.com', $url); # so that code generated on Jason's home computer will display a publically accessible link.
321         return $url;
322 }
323
324 function view_php() {
325         view_headers();
326         echo make_php();
327 }
328
329
330 function make_email() {
331         $tem = new tem();
332         $tem->load('code/wfpl/metaform/template.email.txt');
333         $tem->set('form_name', $GLOBALS['form_name']);
334         $fields = get_fields();
335         foreach($fields as $field) {
336                 list($name, $type, $input, $format, $sql) = $field;
337                 $tem->set('name', $name);
338                 $tem->set('caption', $name); # fixme
339                 if($type == 'textarea') {
340                         $tem->sub('multi_line');
341                 } else {
342                         $tem->sub('fields');
343                 }
344         }
345         return $tem->run();
346 }
347
348 function make_htaccess() {
349         $tem = new tem();
350         $tem->set('form', $GLOBALS['form_name']);
351         return $tem->run('code/wfpl/metaform/htaccess');
352 }
353
354 function view_email() {
355         view_headers();
356         echo make_email();
357 }
358
359
360 function preview() {
361         tem_load('code/wfpl/metaform/preview.html');
362         tem_set('form_name', $GLOBALS['form_name']);
363         tem_set('fields', $_REQUEST['fields']);
364         $preview_tem = new tem();
365         $preview_tem->load_str(make_html(false));
366         if($GLOBALS['opt_db'] == 'Yes') {
367                 $preview_tem->sub('new_msg');
368         }
369         $fields = get_fields();
370         foreach($fields as $field) {
371                 list($name, $type, $input, $format, $sql) = $field;
372                 if($type == 'pulldown') {
373                         pulldown($name, array('option 1', 'option 2', 'option 3'));
374                 }
375         }
376         $preview = $preview_tem->run();
377         unset($preview_tem);
378         tem_set('preview', $preview);
379         set_form_action();
380         tem_output();
381 }
382
383 function download_tar() {
384         $name = $GLOBALS['form_name'];
385         $data = array(
386                 ".htaccess" => make_htaccess(),
387                 "run.php ->" => 'code/wfpl/run.php',
388                 "style.css" => read_whole_file('code/wfpl/metaform/style.css'),
389                 "$name.html" => make_html(),
390                 "$name.php" => make_php());
391         if($GLOBALS['opt_db'] == 'Yes') {
392                 $data["$name.sql"] = make_sql();
393         }
394         if($GLOBALS['opt_email'] == 'Yes') {
395                 $data["$name.email.txt"] = make_email();
396         }
397         make_wfpl_tar($name, $data);
398 }
399
400
401 metaform();
402 exit();
403
404 ?>