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]);
87 'opt_public_display');
88 foreach($bools as $bool) {
96 if(isset($_REQUEST['singular'])) {
97 $GLOBALS['file_name'] = format_varname($_REQUEST['file_name']);
98 $GLOBALS['table_name'] = format_varname($_REQUEST['table_name']);
99 $GLOBALS['plural'] = format_oneline($_REQUEST['plural']);
100 # backwards compatibility:
101 if(isset($_REQUEST['form_name'])) {
102 $GLOBALS['file_name'] = $GLOBALS['table_name'] = $GLOBALS['plural'] = format_varname($_REQUEST['form_name']);
105 $GLOBALS['singular'] = format_oneline($_REQUEST['singular']);
106 $GLOBALS['opt_email'] = format_bool($_REQUEST['opt_email']);
107 $GLOBALS['opt_db'] = format_bool($_REQUEST['opt_db']);
108 $GLOBALS['opt_listing'] = format_bool($_REQUEST['opt_listing']);
109 $GLOBALS['opt_display'] = format_bool($_REQUEST['opt_display']);
110 $GLOBALS['opt_pass'] = format_bool($_REQUEST['opt_pass']);
111 $GLOBALS['opt_public_form'] = format_bool($_REQUEST['opt_public_form']);
112 $GLOBALS['opt_public_display'] = format_bool($_REQUEST['opt_public_display']);
115 tem_set_globals($GLOBALS['wfpl_template']);
118 if(isset($_REQUEST['fields'])) {
119 if(isset($_REQUEST['view_sql'])) {
122 } elseif(isset($_REQUEST['view_php'])) {
125 } elseif(isset($_REQUEST['view_html'])) {
128 } elseif(isset($_REQUEST['view_email'])) {
131 } elseif(isset($_REQUEST['download_tar'])) {
134 } elseif(isset($_REQUEST['preview'])) {
137 } elseif(isset($_REQUEST['edit'])) {
138 tem_set('fields', $_REQUEST['fields']);
141 die("Sorry... couldn't tell which button you pressed");
147 tem_load('code/wfpl/metaform/main.html');
148 list_available_types();
153 function field_input($type) { return $GLOBALS['types'][$type][0]; }
154 function field_format($type) { return $GLOBALS['types'][$type][1]; }
155 function field_sql($type) { return $GLOBALS['types'][$type][2]; }
157 function get_fields() {
158 # no sense in doing all this so many times
159 if(isset($GLOBALS['gotten_fields'])) {
160 return $GLOBALS['gotten_fields'];
163 $fields_str = unix_newlines($_REQUEST['fields']);
164 $GLOBALS['gotten_fields'] = array();
165 $fields_str = rtrim($fields_str);
166 $fields = split("\n", $fields_str);
167 foreach($fields as $field) {
168 $field = trim($field);
169 if(substr($field, -1) == '{') {
170 $name = trim(substr($field, 0, -1)); # FIXME: stop this from getting enc_caption()ed
173 } elseif(substr($field, -1) == '{') {
178 list($name, $type, $options) = split(' *', $field);
179 if($options) $options = explode(',', $options);
180 if(!$type) $type = $name;
182 $input = field_input($type);
183 $format = field_format($type);
184 $sql = field_sql($type);
185 $GLOBALS['gotten_fields'][] = array($name, $type, $input, $format, $sql, $options);
188 return $GLOBALS['gotten_fields'];
191 # this one, that you're using to create forms
192 function set_form_action() {
193 $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
194 if($action == '') $action = './';
195 tem_set('form_action', $action);
198 # perfect HTTP headers for viewing created files
199 function view_headers() {
200 header('Content-type: text/plain');
206 function make_sql() {
208 $tem->load('code/wfpl/metaform/template.sql');
209 tem_set_globals($tem);
210 $fields = get_fields();
211 foreach($fields as $field) {
212 list($name, $type, $input, $format, $sql) = $field;
214 $tem->set('name', $name);
215 $tem->set('type', $sql);
216 if(substr($sql, 0, 3) == 'int' || substr($sql, 0, 7) == 'decimal') {
217 $tem->set('default', '0');
218 } elseif($format == 'yesno') {
219 $tem->set('default', '"No"');
221 $tem->set('default', '""');
223 $tem->show('column');
230 function view_sql() {
235 # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
236 function find_always_field($fields) {
237 foreach($fields as $field) {
238 list($name, $type, $input, $format, $sql) = $field;
239 if($input != 'submit' && $input != 'image' && $input != 'file' && $input != 'checkbox' && $input != 'radio' && $type != '{') {
249 # pass false if you want to exclude the <head> and <body> tag etc.
250 function make_html($whole_file = true) {
251 $has_html_editors = false;
253 $tem->load('code/wfpl/metaform/template.html');
254 tem_set_globals($tem);
255 $fields = get_fields();
256 $tem->set('always_field', find_always_field($fields));
257 $hidden_fields = array();
258 $visible_fields = array();
259 $display_fields = array();
260 $listing_headers = array();
261 $listing_fields = array();
262 foreach($fields as $field) {
263 list($name, $type, $input, $format, $sql) = $field;
264 if($input == 'hidden') {
265 $hidden_fields[] = array('name' => $name);
267 $visible_fields[] = array($input => array(
269 'caption' => format_caption($name)));
272 if($input == 'image' || $input == 'file') {
273 $tem->set('uploads');
274 $tem->set('enctype_attr', '" enctype="multipart/form-data');
275 } elseif($input == 'html') {
276 $has_html_editors = true;
277 $tem->set('html_field_name', $name);
278 $tem->set('replace_textarea');
287 $display_type = $input;
290 $display_type = 'short';
292 if($format != 'n/a') {
293 $display_fields[] = array($display_type => array(
294 'name' => $name, 'caption' => format_caption($name)));
297 if(show_in_listing($type, $input, $format, $sql)) {
298 $listing_headers[] = array('caption' => format_caption($name));
299 $listing_field = array('name' => $name);
300 if($format == 'bool' || $format == 'yesno') {
301 $listing_field['enc'] = 'yesno';
302 } elseif($input == 'date') {
303 $listing_field['enc'] = 'mmddyyyy';
304 } elseif($type == 'thumb') {
305 $listing_field['thumb'] = true;
307 $listing_field['enc'] = 'html';
309 $listing_fields[] = $listing_field;
314 if($GLOBALS['opt_email'] == 'Yes' && !$GLOBALS['opt_db']) {
315 $visible_fields[] = array('submit' => array(
317 'caption' => 'Send'));
319 $visible_fields[] = array('submit' => array(
321 'caption' => 'Save'));
324 $form_fields = array();
325 $form_fields['visible_fields'] = $visible_fields;
327 $form_fields['hidden_fields'] = $hidden_fields;
329 $tem->set('form', $form_fields);
331 # opt_display and opt_listing control whether these are actually displayed
332 $tem->set('display_fields', $display_fields);
333 $tem->set('listing_headers', $listing_headers);
334 $tem->set('listing_fields', $listing_fields);
337 if($has_html_editors) {
338 $tem->set('html_editor_headers');
345 $tem2->load_str('<!--~form~-->');
351 function view_html() {
356 function show_in_listing($type, $input, $format, $sql) {
367 if($type == 'image') {
374 function pulldown_options_array($options) {
376 $pulldown_options = array();
377 foreach($options as $option) {
378 $option = preg_replace("/['\\\\]/", '\\\\$0', $option);
379 $pulldown_options[] = "'$option'";
381 $pulldown_options = 'array(' . join(', ', $pulldown_options) . ')';
383 $pulldown_options = "array(array('op1', 'Option One'), array('op2', 'Option Two'), 'n/a')";
386 return $pulldown_options;
389 function make_php() {
390 $has_html_editors = false;
392 $tem->load('code/wfpl/metaform/template.php');
393 tem_set_globals($tem);
394 $fields = get_fields();
396 $always_field = find_always_field($fields);
397 $image_included_yet = false;
398 foreach($fields as $field) {
399 list($name, $type, $input, $format, $sql, $options) = $field;
400 if($input != 'submit') {
401 $tem->set('format', $format);
402 $tem->set('name', $name);
403 $tem->set('db_field', ''); # we don't want to use the value from last time
405 if($db_fields != '') $db_fields .= ',';
408 if($input == 'image') {
409 if($type == 'thumb') {
410 $tem->show('thumb_settings');
411 $tem->show('thumb_upload_params');
412 $tem->show('thumb_w_h');
414 $tem->show('image_settings');
415 $tem->show('image_upload');
417 } else if($input == 'file') {
418 $tem->show('file_settings');
419 $tem->show('file_upload');
422 if($input == 'html') {
423 $has_html_editors = true;
424 } elseif($input == 'pulldown' || $input == 'radio') {
425 $pulldown_options = pulldown_options_array($options);
426 $tem->set('pulldown_options', $pulldown_options);
427 $tem->show('pulldowns');
428 $tem->show('pulldown_format_extra');
430 if($format != 'n/a') {
431 $tem->show('formats');
436 if($GLOBALS['opt_listing']) {
437 if(show_in_listing($type, $input, $format, $sql)) {
438 $tem->show('listing_fields_1');
439 $tem->show('listing_fields_2');
444 $tem->show('uploads_include');
445 $tem->show('upload_max');
446 $tem->show('upload_settings');
447 $image_included_yet = true;
450 if($has_html_editors) {
451 $tem->show('show_extra_headers');
454 $tem->set('always_field', $always_field);
455 $tem->set('db_fields', $db_fields);
456 $tem->set('metaform_url', edit_url());
457 if($GLOBALS['opt_email']) {
458 $this_domain = $_SERVER['HTTP_HOST'];
459 if(substr($this_domain, -2) == '.l') {
460 $this_domain = substr($this_domain, 0, -1) . 'com';
462 $tem->set('this_domain', $this_domain);
467 # make a URL for the edit page with all the fields filled in
468 function edit_url() {
470 $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
471 $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
472 $url = ereg_replace('/[a-z0-9_.]*\?', '/?', $url);
473 $url = str_replace('jasonwoof.l', 'jasonwoof.com', $url); # so that code generated on Jason's home computer will display a publically accessible link.
477 function view_php() {
482 function make_email() {
484 $tem->load('code/wfpl/metaform/template.email.txt');
485 tem_set_globals($tem);
486 $fields = get_fields();
487 foreach($fields as $field) {
488 list($name, $type, $input, $format, $sql) = $field;
489 $tem->set('name', $name);
490 $tem->set('caption', format_caption($name));
491 if($type == 'textarea') {
492 $tem->show('multi_line');
493 } elseif($type == 'checkbox') {
494 $tem->show('checkbox');
496 $tem->show('normal');
498 $tem->show('fields');
503 function make_htaccess() {
505 $tem->set('form', $GLOBALS['file_name']);
506 return $tem->run('code/wfpl/metaform/htaccess');
509 function view_email() {
515 tem_load('code/wfpl/metaform/preview.html');
516 tem_set_globals($GLOBALS['wfpl_template']);
517 tem_set('fields', $_REQUEST['fields']);
518 $preview_tem = new tem();
519 $preview_tem->load_str(make_html(false));
520 if($GLOBALS['opt_db']) {
521 $preview_tem->show('new_msg');
523 $fields = get_fields();
524 foreach($fields as $field) {
525 list($name, $type, $input, $format, $sql, $options) = $field;
526 if($type == 'pulldown' || $type == 'radio') {
527 pulldown($name, eval('return ' . pulldown_options_array($options) . ';'));
530 $preview = $preview_tem->run();
532 $preview = ereg_replace('type="submit"', 'type="submit" disabled="disabled"', $preview);
533 tem_set('preview', $preview);
539 function download_tar() {
540 $name = $GLOBALS['file_name'];
542 "INSTALL" => read_whole_file('code/wfpl/metaform/INSTALL'),
543 ".htaccess" => make_htaccess(),
544 #"run.php ->" => 'code/wfpl/run.php',
545 "style.less" => read_whole_file('code/wfpl/metaform/style.less'),
546 "template.html" => read_whole_file('code/wfpl/metaform/site-template.html'),
547 "$name.html" => make_html(),
548 "$name.php" => make_php());
549 if($GLOBALS['opt_db']) {
550 $data["$name.sql"] = make_sql();
552 if($GLOBALS['opt_email']) {
553 $data["$name.email.txt"] = make_email();
555 make_tar($name, $data);