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