3 # Copyright (C) 2006 Jason Woofenden
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 # This file writes the code for you (sql, php, html, email) to handle a form.
21 require_once('code/wfpl/template.php');
22 require_once('code/wfpl/http.php');
23 require_once('code/wfpl/tar.php');
24 require_once('code/wfpl/format.php');
26 # see code/wfpl/metaform/template.html for the html templates for these elements
27 $GLOBALS['types'] = array(
28 # type input format sql
29 'varname' => array('textbox', 'varname', 'varchar(50)'),
30 'name' => array('textbox', 'oneline', 'varchar(200)'),
31 'textbox' => array('textbox', 'oneline', 'varchar(200)'),
32 'int' => array('textbox', 'int', 'int'),
33 'decimal' => array('textbox', 'decimal', 'decimal(12,12)'),
34 'bigint' => array('textbox', 'int', 'varchar(100)'), # up to 100 digits, stored as a string
35 'zip' => array('textbox', 'zip', 'varchar(20)'),
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 'date' => array('date', 'mdy_to_ymd', 'char(10)'),
41 'dollars' => array('textbox', 'dollars', 'varchar(32)'),
42 'url' => array('textbox', 'url', 'varchar(200)'),
43 'hidden' => array('hidden', 'unix', 'varchar(200)'),
44 'password' => array('password', 'oneline', 'varchar(200)'),
45 'textarea' => array('textarea', 'unix', 'text'),
46 'html' => array('html', 'unix', 'text'),
47 'pulldown' => array('pulldown', 'options', 'varchar(100)'),
48 'radio' => array('radio', 'options', 'varchar(100)'),
49 'checkbox' => array('checkbox', 'bool', 'int(1)'),
50 'yesno' => array('checkbox', 'yesno', 'varchar(3)'),
51 'delete' => array('checkbox', 'yesno', 'n/a'),
52 'image' => array('image', 'oneline', 'varchar(120)'),
53 'thumb' => array('image', 'oneline', 'varchar(240)'),
54 'file' => array('file', 'oneline', 'varchar(100)'),
55 'submit' => array('submit', 'n/a', 'n/a'),
56 '{' => array('fieldset', 'n/a', 'n/a'),
57 '}' => array('end_fieldset','n/a', 'n/a')
60 function list_available_types() {
61 ksort($GLOBALS['types']);
62 foreach($GLOBALS['types'] as $key => $value) {
63 tem_set('type', $key);
65 tem_show('types_sep');
70 function tem_set_globals(&$tem) {
76 foreach($vars as $var) {
77 $tem->set($var, $GLOBALS[$var]);
86 foreach($bools as $bool) {
87 if(format_bool($GLOBALS[$bool])) {
94 if(isset($_REQUEST['singular'])) {
95 $GLOBALS['file_name'] = format_varname($_REQUEST['file_name']);
96 $GLOBALS['table_name'] = format_varname($_REQUEST['table_name']);
97 $GLOBALS['plural'] = format_oneline($_REQUEST['plural']);
98 # backwards compatibility:
99 if(isset($_REQUEST['form_name'])) {
100 $GLOBALS['file_name'] = $GLOBALS['table_name'] = $GLOBALS['plural'] = format_varname($_REQUEST['form_name']);
103 $GLOBALS['singular'] = format_oneline($_REQUEST['singular']);
104 $GLOBALS['opt_email'] = format_yesno($_REQUEST['opt_email']);
105 $GLOBALS['opt_db'] = format_yesno($_REQUEST['opt_db']);
106 $GLOBALS['opt_listing'] = format_yesno($_REQUEST['opt_listing']);
107 $GLOBALS['opt_display'] = format_yesno($_REQUEST['opt_display']);
108 $GLOBALS['opt_pass'] = format_yesno($_REQUEST['opt_pass']);
111 tem_set_globals($GLOBALS['wfpl_template']);
114 if(isset($_REQUEST['fields'])) {
115 if(isset($_REQUEST['view_sql'])) {
118 } elseif(isset($_REQUEST['view_php'])) {
121 } elseif(isset($_REQUEST['view_html'])) {
124 } elseif(isset($_REQUEST['view_email'])) {
127 } elseif(isset($_REQUEST['download_tar'])) {
130 } elseif(isset($_REQUEST['preview'])) {
133 } elseif(isset($_REQUEST['edit'])) {
134 tem_set('fields', $_REQUEST['fields']);
137 die("Sorry... couldn't tell which button you pressed");
143 tem_load('code/wfpl/metaform/main.html');
144 list_available_types();
149 function field_input($type) { return $GLOBALS['types'][$type][0]; }
150 function field_format($type) { return $GLOBALS['types'][$type][1]; }
151 function field_sql($type) { return $GLOBALS['types'][$type][2]; }
153 function get_fields() {
154 # no sense in doing all this so many times
155 if(isset($GLOBALS['gotten_fields'])) {
156 return $GLOBALS['gotten_fields'];
159 $fields_str = unix_newlines($_REQUEST['fields']);
160 $GLOBALS['gotten_fields'] = array();
161 $fields_str = rtrim($fields_str);
162 $fields = split("\n", $fields_str);
163 foreach($fields as $field) {
164 $field = trim($field);
165 if(substr($field, -1) == '{') {
166 $name = trim(substr($field, 0, -1)); # FIXME: stop this from getting enc_caption()ed
169 } elseif(substr($field, -1) == '{') {
174 list($name, $type, $options) = split(' *', $field);
175 if($options) $options = split(',', $options);
176 if(!$type) $type = $name;
178 $input = field_input($type);
179 $format = field_format($type);
180 $sql = field_sql($type);
181 $GLOBALS['gotten_fields'][] = array($name, $type, $input, $format, $sql, $options);
184 return $GLOBALS['gotten_fields'];
187 # this one, that you're using to create forms
188 function set_form_action() {
189 $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
190 if($action == '') $action = './';
191 tem_set('form_action', $action);
194 # perfect HTTP headers for viewing created files
195 function view_headers() {
196 header('Content-type: text/plain');
202 function make_sql() {
204 $tem->load('code/wfpl/metaform/template.sql');
205 tem_set_globals($tem);
206 $fields = get_fields();
207 foreach($fields as $field) {
208 list($name, $type, $input, $format, $sql) = $field;
210 $tem->set('name', $name);
211 $tem->set('type', $sql);
212 if(substr($sql, 0, 3) == 'int' || substr($sql, 0, 7) == 'decimal') {
213 $tem->set('default', '0');
214 } elseif($format == 'yesno') {
215 $tem->set('default', '"No"');
217 $tem->set('default', '""');
219 $tem->show('column');
226 function view_sql() {
231 # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
232 function find_always_field($fields) {
233 foreach($fields as $field) {
234 list($name, $type, $input, $format, $sql) = $field;
235 if($input != 'submit' && $input != 'image' && $input != 'file' && $input != 'checkbox' && $input != 'radio') {
245 # pass false if you want to exclude the <head> and <body> tag etc.
246 function make_html($whole_file = true) {
247 $has_html_editors = false;
249 $tem->load('code/wfpl/metaform/template.html');
250 tem_set_globals($tem);
251 $fields = get_fields();
252 $tem->set('always_field', find_always_field($fields));
253 $hidden_fields = array();
254 $visible_fields = array();
255 $display_fields = array();
256 $listing_headers = array();
257 $listing_fields = array();
258 foreach($fields as $field) {
259 list($name, $type, $input, $format, $sql) = $field;
260 if($input == 'hidden') {
261 $hidden_fields[] = array('name' => $name);
263 $visible_fields[] = array($input => array(
265 'caption' => format_caption($name)));
268 if($input == 'image' || $input == 'file') {
269 $tem->set('uploads');
270 $tem->set('enctype_attr', '" enctype="multipart/form-data');
271 } elseif($input == 'html') {
272 $has_html_editors = true;
273 $tem->set('html_field_name', $name);
274 $tem->set('replace_textarea');
283 $display_type = $input;
286 $display_type = 'short';
288 if($format != 'n/a') {
289 $display_fields[] = array($display_type => array(
290 'name' => $name, 'caption' => format_caption($name)));
293 if(show_in_listing($type, $input, $format, $sql)) {
294 $listing_headers[] = array('caption' => format_caption($name));
295 $listing_field = array('name' => $name);
296 if($format == 'bool' || $format == 'yesno') {
297 $listing_field['enc'] = 'yesno';
298 } elseif($input == 'date') {
299 $listing_field['enc'] = 'mmddyyyy';
300 } elseif($type == 'thumb') {
301 $listing_field['thumb'] = true;
303 $listing_field['enc'] = 'html';
305 $listing_fields[] = $listing_field;
310 if($GLOBALS['opt_email'] == 'Yes' && $GLOBALS['opt_db'] != 'Yes') {
311 $visible_fields[] = array('submit' => array(
313 'caption' => 'Send'));
315 $visible_fields[] = array('submit' => array(
317 'caption' => 'Save'));
320 $tem->set('form', array(
321 'visible_fields' => $visible_fields,
322 'hidden_fields' => $hidden_fields));
324 # opt_display and opt_listing control whether these are actually displayed
325 $tem->set('display_fields', $display_fields);
326 $tem->set('listing_headers', $listing_headers);
327 $tem->set('listing_fields', $listing_fields);
330 if($has_html_editors) {
331 $tem->set('html_editor_headers');
338 $tem2->load_str('<!--~form~-->');
344 function view_html() {
349 function show_in_listing($type, $input, $format, $sql) {
360 if($type == 'image') {
367 function make_php() {
368 $has_html_editors = false;
370 $tem->load('code/wfpl/metaform/template.php');
371 tem_set_globals($tem);
372 $fields = get_fields();
374 $always_field = find_always_field($fields);
375 $image_included_yet = false;
376 foreach($fields as $field) {
377 list($name, $type, $input, $format, $sql) = $field;
378 if($input != 'submit') {
379 $tem->set('format', $format);
380 $tem->set('name', $name);
381 $tem->set('db_field', ''); # we don't want to use the value from last time
383 if($db_fields != '') $db_fields .= ',';
386 if($input == 'image') {
387 if($type == 'thumb') {
388 $tem->show('thumb_settings');
389 $tem->show('thumb_upload_params');
390 $tem->show('thumb_w_h');
392 $tem->show('image_settings');
393 $tem->show('image_upload');
395 } else if($input == 'file') {
396 $tem->show('file_settings');
397 $tem->show('file_upload');
400 if($input == 'html') {
401 $has_html_editors = true;
402 } elseif($input == 'pulldown' || $input == 'radio') {
403 $tem->show('pulldowns');
404 $tem->show('pulldown_format_extra');
406 if($format != 'n/a') {
407 $tem->show('formats');
412 if($GLOBALS['opt_listing'] == 'Yes') {
413 if(show_in_listing($type, $input, $format, $sql)) {
414 $tem->show('listing_fields_1');
415 $tem->show('listing_fields_2');
420 $tem->show('uploads_include');
421 $tem->show('upload_max');
422 $tem->show('upload_settings');
423 $image_included_yet = true;
426 if($has_html_editors) {
427 $tem->show('show_extra_headers');
430 $tem->set('always_field', $always_field);
431 $tem->set('db_fields', $db_fields);
432 $tem->set('metaform_url', edit_url());
433 if($GLOBALS['opt_email'] == 'Yes') {
434 $this_domain = $_SERVER['HTTP_HOST'];
435 if(substr($this_domain, -2) == '.l') {
436 $this_domain = substr($this_domain, 0, -1) . 'com';
438 $tem->set('this_domain', $this_domain);
443 # make a URL for the edit page with all the fields filled in
444 function edit_url() {
446 $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
447 $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
448 $url = ereg_replace('/[a-z0-9_.]*\?', '/?', $url);
449 $url = str_replace('jasonwoof.l', 'jasonwoof.com', $url); # so that code generated on Jason's home computer will display a publically accessible link.
453 function view_php() {
458 function make_email() {
460 $tem->load('code/wfpl/metaform/template.email.txt');
461 tem_set_globals($tem);
462 $fields = get_fields();
463 foreach($fields as $field) {
464 list($name, $type, $input, $format, $sql) = $field;
465 $tem->set('name', $name);
466 $tem->set('caption', format_caption($name));
467 if($type == 'textarea') {
468 $tem->show('multi_line');
469 } elseif($type == 'checkbox') {
470 $tem->show('checkbox');
472 $tem->show('normal');
474 $tem->show('fields');
479 function make_htaccess() {
481 $tem->set('form', $GLOBALS['file_name']);
482 return $tem->run('code/wfpl/metaform/htaccess');
485 function view_email() {
491 tem_load('code/wfpl/metaform/preview.html');
492 tem_set_globals($GLOBALS['wfpl_template']);
493 tem_set('fields', $_REQUEST['fields']);
494 $preview_tem = new tem();
495 $preview_tem->load_str(make_html(false));
496 if($GLOBALS['opt_db'] == 'Yes') {
497 $preview_tem->show('new_msg');
499 $fields = get_fields();
500 foreach($fields as $field) {
501 list($name, $type, $input, $format, $sql) = $field;
502 if($type == 'pulldown' || $type == 'radio') {
503 pulldown($name, array('option 1', 'option 2', 'option 3'));
506 $preview = $preview_tem->run();
508 $preview = ereg_replace('type="submit"', 'type="submit" disabled="disabled"', $preview);
509 tem_set('preview', $preview);
515 function download_tar() {
516 $name = $GLOBALS['file_name'];
518 "INSTALL" => read_whole_file('code/wfpl/metaform/INSTALL'),
519 ".htaccess" => make_htaccess(),
520 #"run.php ->" => 'code/wfpl/run.php',
521 "style.css" => read_whole_file('code/wfpl/metaform/style.css'),
522 "$name.html" => make_html(),
523 "$name.php" => make_php());
524 if($GLOBALS['opt_db'] == 'Yes') {
525 $data["$name.sql"] = make_sql();
527 if($GLOBALS['opt_email'] == 'Yes') {
528 $data["$name.email.txt"] = make_email();
530 make_tar($name, $data);