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) {
77 foreach($vars as $var) {
78 $tem->set($var, $GLOBALS[$var]);
89 'opt_public_something');
90 foreach($bools as $bool) {
98 if(isset($_REQUEST['singular'])) {
99 $GLOBALS['file_name'] = format_varname($_REQUEST['file_name']);
100 $GLOBALS['table_name'] = format_varname($_REQUEST['table_name']);
101 $GLOBALS['plural'] = format_oneline($_REQUEST['plural']);
102 # backwards compatibility:
103 if(isset($_REQUEST['form_name'])) {
104 $GLOBALS['file_name'] = $GLOBALS['table_name'] = $GLOBALS['plural'] = format_varname($_REQUEST['form_name']);
107 $GLOBALS['singular'] = format_oneline($_REQUEST['singular']);
108 $GLOBALS['opt_email'] = format_bool($_REQUEST['opt_email']);
109 $GLOBALS['opt_db'] = format_bool($_REQUEST['opt_db']);
110 $GLOBALS['opt_listing'] = format_bool($_REQUEST['opt_listing']);
111 $GLOBALS['opt_display'] = format_bool($_REQUEST['opt_display']);
112 $GLOBALS['opt_pass'] = format_bool($_REQUEST['opt_pass']);
113 $GLOBALS['opt_public_form'] = format_bool($_REQUEST['opt_public_form']);
114 $GLOBALS['opt_public_display'] = format_bool($_REQUEST['opt_public_display']);
116 $GLOBALS['public_file_name'] = $GLOBALS['file_name'];
117 if($GLOBALS['opt_public_form'] || $GLOBALS['opt_public_display']) {
118 $GLOBALS['opt_public_something'] = 1;
119 $GLOBALS['file_name'] = $GLOBALS['file_name'] . _admin;
123 tem_set_globals($GLOBALS['wfpl_template']);
126 if(isset($_REQUEST['fields'])) {
127 if(isset($_REQUEST['view_sql'])) {
130 } elseif(isset($_REQUEST['view_php'])) {
133 } elseif(isset($_REQUEST['view_html'])) {
136 } elseif(isset($_REQUEST['view_email'])) {
139 } elseif(isset($_REQUEST['download_tar'])) {
142 } elseif(isset($_REQUEST['preview'])) {
145 } elseif(isset($_REQUEST['edit'])) {
146 tem_set('fields', $_REQUEST['fields']);
149 die("Sorry... couldn't tell which button you pressed");
155 tem_load('code/wfpl/metaform/main.html');
156 list_available_types();
161 function field_input($type) { return $GLOBALS['types'][$type][0]; }
162 function field_format($type) { return $GLOBALS['types'][$type][1]; }
163 function field_sql($type) { return $GLOBALS['types'][$type][2]; }
165 function get_fields() {
166 # no sense in doing all this so many times
167 if(isset($GLOBALS['gotten_fields'])) {
168 return $GLOBALS['gotten_fields'];
171 $fields_str = unix_newlines($_REQUEST['fields']);
172 $GLOBALS['gotten_fields'] = array();
173 $fields_str = rtrim($fields_str);
174 $fields = split("\n", $fields_str);
175 foreach($fields as $field) {
176 $field = trim($field);
177 if(substr($field, -1) == '{') {
178 $name = trim(substr($field, 0, -1)); # FIXME: stop this from getting enc_caption()ed
181 } elseif(substr($field, -1) == '{') {
186 list($name, $type, $options) = split(' *', $field);
187 if($options) $options = explode(',', $options);
188 if(!$type) $type = $name;
190 $input = field_input($type);
191 $format = field_format($type);
192 $sql = field_sql($type);
193 $GLOBALS['gotten_fields'][] = array($name, $type, $input, $format, $sql, $options);
196 return $GLOBALS['gotten_fields'];
199 # this one, that you're using to create forms
200 function set_form_action() {
201 $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
202 if($action == '') $action = './';
203 tem_set('form_action', $action);
206 # perfect HTTP headers for viewing created files
207 function view_headers() {
208 header('Content-type: text/plain');
214 function make_sql() {
216 $tem->load('code/wfpl/metaform/template.sql');
217 tem_set_globals($tem);
218 $fields = get_fields();
219 foreach($fields as $field) {
220 list($name, $type, $input, $format, $sql) = $field;
222 $tem->set('name', $name);
223 $tem->set('type', $sql);
224 if(substr($sql, 0, 3) == 'int' || substr($sql, 0, 7) == 'decimal') {
225 $tem->set('default', '0');
226 } elseif($format == 'yesno') {
227 $tem->set('default', '"No"');
229 $tem->set('default', '""');
231 $tem->show('column');
238 function view_sql() {
243 # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
244 function find_always_field($fields) {
245 foreach($fields as $field) {
246 list($name, $type, $input, $format, $sql) = $field;
247 if($input != 'submit' && $input != 'image' && $input != 'file' && $input != 'checkbox' && $input != 'radio' && $type != '{') {
257 # pass false if you want to exclude the <head> and <body> tag etc.
258 function make_html($whole_file = true) {
259 $has_html_editors = false;
261 $tem->load('code/wfpl/metaform/template.html');
262 tem_set_globals($tem);
263 $fields = get_fields();
264 $tem->set('always_field', find_always_field($fields));
265 $hidden_fields = array();
266 $visible_fields = array();
267 $display_fields = array();
268 $listing_headers = array();
269 $listing_fields = array();
270 foreach($fields as $field) {
271 list($name, $type, $input, $format, $sql) = $field;
272 if($input == 'hidden') {
273 $hidden_fields[] = array('name' => $name);
275 $visible_fields[] = array($input => array(
277 'caption' => format_caption($name)));
280 if($input == 'image' || $input == 'file') {
281 $tem->set('uploads');
282 $tem->set('enctype_attr', '" enctype="multipart/form-data');
283 } elseif($input == 'html') {
284 $has_html_editors = true;
285 $tem->set('html_field_name', $name);
286 $tem->set('replace_textarea');
295 $display_type = $input;
298 $display_type = 'short';
300 if($format != 'n/a') {
301 $display_fields[] = array($display_type => array(
302 'name' => $name, 'caption' => format_caption($name)));
305 if(show_in_listing($type, $input, $format, $sql)) {
306 $listing_headers[] = array('caption' => format_caption($name));
307 $listing_field = array('name' => $name);
308 if($format == 'bool' || $format == 'yesno') {
309 $listing_field['enc'] = 'yesno';
310 } elseif($input == 'date') {
311 $listing_field['enc'] = 'mmddyyyy';
312 } elseif($type == 'thumb') {
313 $listing_field['thumb'] = true;
315 $listing_field['enc'] = 'html';
317 $listing_fields[] = $listing_field;
322 if($GLOBALS['opt_email'] == 'Yes' && !$GLOBALS['opt_db']) {
323 $visible_fields[] = array('submit' => array(
325 'caption' => 'Send'));
327 $visible_fields[] = array('submit' => array(
329 'caption' => 'Save'));
332 $form_fields = array();
333 $form_fields['visible_fields'] = $visible_fields;
335 $form_fields['hidden_fields'] = $hidden_fields;
337 $tem->set('form', $form_fields);
339 # opt_display and opt_listing control whether these are actually displayed
340 $tem->set('display_fields', $display_fields);
341 $tem->set('listing_headers', $listing_headers);
342 $tem->set('listing_fields', $listing_fields);
345 if($has_html_editors) {
346 $tem->set('html_editor_headers');
353 $tem2->load_str('<!--~form~-->');
359 function view_html() {
364 function show_in_listing($type, $input, $format, $sql) {
375 if($type == 'image') {
382 function pulldown_options_array($options) {
384 $pulldown_options = array();
385 foreach($options as $option) {
386 $option = preg_replace("/['\\\\]/", '\\\\$0', $option);
387 $pulldown_options[] = "'$option'";
389 $pulldown_options = 'array(' . join(', ', $pulldown_options) . ')';
391 $pulldown_options = "array(array('op1', 'Option One'), array('op2', 'Option Two'), 'n/a')";
394 return $pulldown_options;
397 function make_php() {
398 $has_html_editors = false;
400 $tem->load('code/wfpl/metaform/template.php');
401 tem_set_globals($tem);
402 $fields = get_fields();
404 $always_field = find_always_field($fields);
405 $image_included_yet = false;
406 foreach($fields as $field) {
407 list($name, $type, $input, $format, $sql, $options) = $field;
408 if($input != 'submit') {
409 $tem->set('format', $format);
410 $tem->set('name', $name);
411 $tem->set('db_field', ''); # we don't want to use the value from last time
413 if($db_fields != '') $db_fields .= ',';
416 if($input == 'image') {
417 if($type == 'thumb') {
418 $tem->show('thumb_settings');
419 $tem->show('thumb_upload_params');
420 $tem->show('thumb_w_h');
422 $tem->show('image_settings');
423 $tem->show('image_upload');
425 } else if($input == 'file') {
426 $tem->show('file_settings');
427 $tem->show('file_upload');
430 if($input == 'html') {
431 $has_html_editors = true;
432 } elseif($input == 'pulldown' || $input == 'radio') {
433 $pulldown_options = pulldown_options_array($options);
434 $tem->set('pulldown_options', $pulldown_options);
435 $tem->set('has_pulldowns');
436 $tem->show('pulldowns');
437 $tem->show('pulldown_format_extra');
439 if($format != 'n/a') {
440 $tem->show('formats');
445 if($GLOBALS['opt_listing']) {
446 if(show_in_listing($type, $input, $format, $sql)) {
447 $tem->show('listing_fields_1');
448 $tem->show('listing_fields_2');
453 $tem->show('uploads_include');
454 $tem->show('upload_max');
455 $tem->show('upload_settings');
456 $image_included_yet = true;
459 if($has_html_editors) {
460 $tem->show('show_extra_headers');
463 $tem->set('always_field', $always_field);
464 $tem->set('db_fields', $db_fields);
465 $tem->set('metaform_url', edit_url());
466 if($GLOBALS['opt_email']) {
467 $this_domain = $_SERVER['HTTP_HOST'];
468 if(substr($this_domain, -2) == '.l') {
469 $this_domain = substr($this_domain, 0, -1) . 'com';
471 $tem->set('this_domain', $this_domain);
476 # make a URL for the edit page with all the fields filled in
477 function edit_url() {
479 $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
480 $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
481 $url = ereg_replace('/[a-z0-9_.]*\?', '/?', $url);
482 $url = str_replace('jasonwoof.l', 'jasonwoof.com', $url); # so that code generated on Jason's home computer will display a publically accessible link.
486 function view_php() {
491 function make_email() {
493 $tem->load('code/wfpl/metaform/template.email.txt');
494 tem_set_globals($tem);
495 $fields = get_fields();
496 foreach($fields as $field) {
497 list($name, $type, $input, $format, $sql) = $field;
498 $tem->set('name', $name);
499 $tem->set('caption', format_caption($name));
500 if($type == 'textarea') {
501 $tem->show('multi_line');
502 } elseif($type == 'checkbox') {
503 $tem->show('checkbox');
505 $tem->show('normal');
507 $tem->show('fields');
512 function make_htaccess() {
514 $tem->set('form', $GLOBALS['file_name']);
515 return $tem->run('code/wfpl/metaform/htaccess');
518 function view_email() {
524 tem_load('code/wfpl/metaform/preview.html');
525 tem_set_globals($GLOBALS['wfpl_template']);
526 tem_set('fields', $_REQUEST['fields']);
527 $preview_tem = new tem();
528 $preview_tem->load_str(make_html(false));
529 if($GLOBALS['opt_db']) {
530 $preview_tem->show('new_msg');
532 $fields = get_fields();
533 foreach($fields as $field) {
534 list($name, $type, $input, $format, $sql, $options) = $field;
535 if($type == 'pulldown' || $type == 'radio') {
536 pulldown($name, eval('return ' . pulldown_options_array($options) . ';'));
539 $preview = $preview_tem->run();
541 $preview = ereg_replace('type="submit"', 'type="submit" disabled="disabled"', $preview);
542 tem_set('preview', $preview);
548 function download_tar() {
549 $admin_name = $GLOBALS['file_name'];
550 $nice_name = $GLOBALS['public_file_name'];
552 "INSTALL" => read_whole_file('code/wfpl/metaform/INSTALL'),
553 ".htaccess" => make_htaccess(),
554 "run.php ->" => 'code/wfpl/run.php',
555 "style.less" => read_whole_file('code/wfpl/metaform/style.less'),
556 "template.html" => read_whole_file('code/wfpl/metaform/site-template.html'),
557 "$admin_name.html" => make_html(),
558 "$admin_name.php" => make_php());
559 if($GLOBALS['opt_public_something']) {
560 $files["$nice_name.html ->"] = "$admin_name.html";
561 $files["$nice_name.php ->"] = "$admin_name.php";
563 if($GLOBALS['opt_db']) {
564 $files["$nice_name.sql"] = make_sql();
566 if($GLOBALS['opt_email']) {
567 $files["$nice_name.email.txt"] = make_email();
569 make_tar($nice_name, $files);