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) binary'),
30 'name' => array('textbox', 'oneline', 'varchar(200) binary'),
31 'textbox' => array('textbox', 'oneline', 'varchar(200) binary'),
32 'int' => array('textbox', 'int', 'int'),
33 'decimal' => array('textbox', 'decimal', 'decimal(12,12)'),
34 'bigint' => array('textbox', 'int', 'varchar(100) binary'), # up to 100 digits, stored as a string
35 'zip' => array('textbox', 'zip', 'varchar(20) binary'),
36 'email' => array('email', 'email', 'varchar(100) binary'),
37 'search' => array('search', 'oneline', 'varchar(200) binary'),
38 'phone' => array('textbox', 'phone', 'varchar(32) binary'),
39 'state' => array('states', 'oneline', 'varchar(2) binary'),
40 'money' => array('textbox', 'money', 'varchar(32) binary'),
41 'date' => array('date', 'mdy_to_ymd', 'char(10) binary'),
42 'dollars' => array('textbox', 'dollars', 'varchar(32) binary'),
43 'url' => array('textbox', 'url', 'varchar(200) binary'),
44 'hidden' => array('hidden', 'unix', 'varchar(200) binary'),
45 'password' => array('password', 'oneline', 'varchar(200) binary'),
46 'textarea' => array('textarea', 'unix', 'text binary'),
47 'html' => array('html', 'unix', 'mediumtext binary'),
48 'pulldown' => array('pulldown', 'options', 'varchar(100) binary'),
49 'radio' => array('radio', 'options', 'varchar(100) binary'),
50 'checkbox' => array('checkbox', 'bool', 'int(1)'),
51 'yesno' => array('checkbox', 'yesno', 'varchar(3) binary'),
52 'delete' => array('checkbox', 'yesno', 'n/a'),
53 'image' => array('image', 'oneline', 'varchar(120) binary'),
54 'thumb' => array('image', 'oneline', 'varchar(240) binary'),
55 'file' => array('file', 'oneline', 'varchar(100) binary'),
56 'submit' => array('submit', 'n/a', 'n/a'),
57 '{' => array('fieldset', 'n/a', 'n/a'),
58 '}' => array('end_fieldset','n/a', 'n/a')
61 function list_available_types() {
62 ksort($GLOBALS['types']);
63 foreach($GLOBALS['types'] as $key => $value) {
64 tem_set('type', $key);
66 tem_show('types_sep');
71 function tem_set_globals(&$tem) {
78 foreach($vars as $var) {
79 $tem->set($var, $GLOBALS[$var]);
90 'opt_public_something');
91 foreach($bools as $bool) {
99 if(isset($_REQUEST['singular'])) {
100 $GLOBALS['file_name'] = format_varname($_REQUEST['file_name']);
101 $GLOBALS['table_name'] = format_varname($_REQUEST['table_name']);
102 $GLOBALS['plural'] = format_oneline($_REQUEST['plural']);
103 # backwards compatibility:
104 if(isset($_REQUEST['form_name'])) {
105 $GLOBALS['file_name'] = $GLOBALS['table_name'] = $GLOBALS['plural'] = format_varname($_REQUEST['form_name']);
108 $GLOBALS['singular'] = format_oneline($_REQUEST['singular']);
109 $GLOBALS['opt_email'] = format_bool($_REQUEST['opt_email']);
110 $GLOBALS['opt_db'] = format_bool($_REQUEST['opt_db']);
111 $GLOBALS['opt_listing'] = format_bool($_REQUEST['opt_listing']);
112 $GLOBALS['opt_display'] = format_bool($_REQUEST['opt_display']);
113 $GLOBALS['opt_pass'] = format_bool($_REQUEST['opt_pass']);
114 $GLOBALS['opt_public_form'] = format_bool($_REQUEST['opt_public_form']);
115 $GLOBALS['opt_public_display'] = format_bool($_REQUEST['opt_public_display']);
117 $GLOBALS['public_file_name'] = $GLOBALS['file_name'];
118 if($GLOBALS['opt_public_form'] || $GLOBALS['opt_public_display']) {
119 $GLOBALS['opt_public_something'] = 1;
120 $GLOBALS['file_name'] = $GLOBALS['file_name'] . _admin;
124 tem_set_globals($GLOBALS['wfpl_template']);
127 if(isset($_REQUEST['fields'])) {
128 if(isset($_REQUEST['view_sql'])) {
131 } elseif(isset($_REQUEST['view_php'])) {
134 } elseif(isset($_REQUEST['view_html'])) {
137 } elseif(isset($_REQUEST['view_email'])) {
140 } elseif(isset($_REQUEST['download_tar'])) {
143 } elseif(isset($_REQUEST['preview'])) {
146 } elseif(isset($_REQUEST['edit'])) {
147 tem_set('fields', $_REQUEST['fields']);
150 die("Sorry... couldn't tell which button you pressed");
156 tem_load('code/wfpl/metaform/main.html');
157 list_available_types();
162 function field_input($type) { return $GLOBALS['types'][$type][0]; }
163 function field_format($type) { return $GLOBALS['types'][$type][1]; }
164 function field_sql($type) { return $GLOBALS['types'][$type][2]; }
166 function get_fields() {
167 # no sense in doing all this so many times
168 if(isset($GLOBALS['gotten_fields'])) {
169 return $GLOBALS['gotten_fields'];
172 $fields_str = unix_newlines($_REQUEST['fields']);
173 $GLOBALS['gotten_fields'] = array();
174 $fields_str = rtrim($fields_str);
175 $fields = split("\n", $fields_str);
176 foreach($fields as $field) {
177 $field = trim($field);
178 if(substr($field, -1) == '{') {
179 $name = trim(substr($field, 0, -1)); # FIXME: stop this from getting enc_caption()ed
182 } elseif(substr($field, -1) == '{') {
187 list($name, $type, $options) = split(' *', $field);
188 if($options) $options = explode(',', $options);
189 if(!$type) $type = $name;
191 $input = field_input($type);
192 $format = field_format($type);
193 $sql = field_sql($type);
194 $GLOBALS['gotten_fields'][] = array($name, $type, $input, $format, $sql, $options);
197 return $GLOBALS['gotten_fields'];
200 # this one, that you're using to create forms
201 function set_form_action() {
202 $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
203 if($action == '') $action = './';
204 tem_set('form_action', $action);
207 # perfect HTTP headers for viewing created files
208 function view_headers() {
209 header('Content-type: text/plain');
215 function make_sql() {
217 $tem->load('code/wfpl/metaform/template.sql');
218 tem_set_globals($tem);
219 $fields = get_fields();
220 foreach($fields as $field) {
221 list($name, $type, $input, $format, $sql) = $field;
223 $tem->set('name', $name);
224 $tem->set('type', $sql);
225 if(substr($sql, 0, 3) == 'int' || substr($sql, 0, 7) == 'decimal') {
226 $tem->set('default', '0');
227 } elseif($format == 'yesno') {
228 $tem->set('default', '"No"');
230 $tem->set('default', '""');
232 $tem->show('column');
239 function view_sql() {
244 # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
245 function find_always_field($fields) {
246 foreach($fields as $field) {
247 list($name, $type, $input, $format, $sql) = $field;
248 if($input != 'submit' && $input != 'image' && $input != 'file' && $input != 'checkbox' && $input != 'radio' && $type != '{') {
258 # pass false if you want to exclude the <head> and <body> tag etc.
259 function make_html($whole_file = true) {
260 $has_html_editors = false;
262 $tem->load('code/wfpl/metaform/template.html');
263 tem_set_globals($tem);
264 $fields = get_fields();
265 $tem->set('always_field', find_always_field($fields));
266 $hidden_fields = array();
267 $visible_fields = array();
268 $display_fields = array();
269 $listing_headers = array();
270 $listing_fields = array();
271 foreach($fields as $field) {
272 list($name, $type, $input, $format, $sql) = $field;
273 if($input == 'hidden') {
274 $hidden_fields[] = array('name' => $name);
276 $visible_fields[] = array($input => array(
278 'caption' => format_caption($name)));
281 if($input == 'image' || $input == 'file') {
282 $tem->set('uploads');
283 $tem->set('enctype_attr', '" enctype="multipart/form-data');
284 } elseif($input == 'html') {
285 $has_html_editors = true;
286 $tem->set('html_field_name', $name);
287 $tem->set('replace_textarea');
296 $display_type = $input;
299 $display_type = 'short';
301 if($format != 'n/a') {
302 $display_fields[] = array($display_type => array(
303 'name' => $name, 'caption' => format_caption($name)));
306 if(show_in_listing($type, $input, $format, $sql)) {
307 $listing_headers[] = array('caption' => format_caption($name), 'name' => $name);
308 $listing_field = array('name' => $name);
309 if($format == 'bool' || $format == 'yesno') {
310 $listing_field['enc'] = 'yesno';
311 } elseif($input == 'date') {
312 $listing_field['enc'] = 'mmddyyyy';
313 } elseif($type == 'thumb') {
314 $listing_field['thumb'] = true;
316 $listing_field['enc'] = 'html';
318 $listing_fields[] = $listing_field;
323 if($GLOBALS['opt_email'] == 'Yes' && !$GLOBALS['opt_db']) {
324 $visible_fields[] = array('submit' => array(
326 'caption' => 'Send'));
328 $visible_fields[] = array('submit' => array(
330 'caption' => 'Save'));
333 $form_fields = array();
334 $form_fields['visible_fields'] = $visible_fields;
336 $form_fields['hidden_fields'] = $hidden_fields;
338 $tem->set('form', $form_fields);
340 # opt_display and opt_listing control whether these are actually displayed
341 $tem->set('display_fields', $display_fields);
342 $tem->set('listing_headers', $listing_headers);
343 $tem->set('listing_fields', $listing_fields);
346 if($has_html_editors) {
347 $tem->set('html_editor_headers');
354 $tem2->load_str('<!--~form~-->');
360 function view_html() {
365 function show_in_listing($type, $input, $format, $sql) {
376 if($type == 'image') {
383 function pulldown_options_array($options) {
385 $pulldown_options = array();
386 foreach($options as $option) {
387 $option = preg_replace("/['\\\\]/", '\\\\$0', $option);
388 $pulldown_options[] = "'$option'";
390 $pulldown_options = 'array(' . join(', ', $pulldown_options) . ')';
392 $pulldown_options = "array(array('op1', 'Option One'), array('op2', 'Option Two'), 'n/a')";
395 return $pulldown_options;
398 function make_php() {
399 $has_html_editors = false;
401 $tem->load('code/wfpl/metaform/template.php');
402 tem_set_globals($tem);
403 $fields = get_fields();
405 $always_field = find_always_field($fields);
406 $image_included_yet = false;
407 foreach($fields as $field) {
408 list($name, $type, $input, $format, $sql, $options) = $field;
409 if($input != 'submit') {
410 $tem->set('format', $format);
411 $tem->set('name', $name);
412 $tem->set('db_field', ''); # we don't want to use the value from last time
414 if($db_fields != '') $db_fields .= ',';
417 if($input == 'image') {
418 if($type == 'thumb') {
419 $tem->show('thumb_settings');
420 $tem->show('thumb_upload_params');
421 $tem->show('thumb_w_h');
423 $tem->show('image_settings');
424 $tem->show('image_upload');
426 } else if($input == 'file') {
427 $tem->show('file_settings');
428 $tem->show('file_upload');
431 if($input == 'html') {
432 $has_html_editors = true;
433 } elseif($input == 'pulldown' || $input == 'radio') {
434 $pulldown_options = pulldown_options_array($options);
435 $tem->set('pulldown_options', $pulldown_options);
436 $tem->set('has_pulldowns');
437 $tem->show('pulldowns');
438 $tem->show('pulldown_format_extra');
440 if($format != 'n/a') {
441 $tem->show('formats');
446 if($GLOBALS['opt_listing']) {
447 if(show_in_listing($type, $input, $format, $sql)) {
448 $tem->show('listing_fields_1');
449 $tem->show('listing_fields_2');
454 $tem->show('uploads_include');
455 $tem->show('upload_max');
456 $tem->show('upload_settings');
457 $image_included_yet = true;
460 if($has_html_editors) {
461 $tem->show('show_extra_headers');
464 $tem->set('always_field', $always_field);
465 $tem->set('db_fields', $db_fields);
466 $tem->set('metaform_url', edit_url());
467 if($GLOBALS['opt_email']) {
468 $this_domain = $_SERVER['HTTP_HOST'];
469 if(substr($this_domain, -2) == '.l') {
470 $this_domain = substr($this_domain, 0, -1) . 'com';
472 $tem->set('this_domain', $this_domain);
477 # make a URL for the edit page with all the fields filled in
478 function edit_url() {
480 $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
481 $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
482 $url = ereg_replace('/[a-z0-9_.]*\?', '/?', $url);
483 $url = str_replace('jasonwoof.l', 'jasonwoof.com', $url); # so that code generated on Jason's home computer will display a publically accessible link.
487 function view_php() {
492 function make_email() {
494 $tem->load('code/wfpl/metaform/template.email.txt');
495 tem_set_globals($tem);
496 $fields = get_fields();
497 foreach($fields as $field) {
498 list($name, $type, $input, $format, $sql) = $field;
499 $tem->set('name', $name);
500 $tem->set('caption', format_caption($name));
501 if($type == 'textarea') {
502 $tem->show('multi_line');
503 } elseif($type == 'checkbox') {
504 $tem->show('checkbox');
506 $tem->show('normal');
508 $tem->show('fields');
513 function make_htaccess() {
515 $tem->set('form', $GLOBALS['file_name']);
516 return $tem->run('code/wfpl/metaform/htaccess');
519 function view_email() {
525 tem_load('code/wfpl/metaform/preview.html');
526 tem_set_globals($GLOBALS['wfpl_template']);
527 tem_set('fields', $_REQUEST['fields']);
528 $preview_tem = new tem();
529 $preview_tem->load_str(make_html(false));
530 if($GLOBALS['opt_db']) {
531 $preview_tem->show('new_msg');
533 $fields = get_fields();
534 foreach($fields as $field) {
535 list($name, $type, $input, $format, $sql, $options) = $field;
536 if($type == 'pulldown' || $type == 'radio') {
537 pulldown($name, eval('return ' . pulldown_options_array($options) . ';'));
540 $preview = $preview_tem->run();
542 $preview = ereg_replace('type="submit"', 'type="submit" disabled="disabled"', $preview);
543 tem_set('preview', $preview);
549 function download_tar() {
550 $admin_name = $GLOBALS['file_name'];
551 $nice_name = $GLOBALS['public_file_name'];
553 "INSTALL" => read_whole_file('code/wfpl/metaform/INSTALL'),
554 ".htaccess" => make_htaccess(),
555 "run.php ->" => 'code/wfpl/run.php',
556 "style.less" => read_whole_file('code/wfpl/metaform/style.less'),
557 "template.html" => read_whole_file('code/wfpl/metaform/site-template.html'),
558 "$admin_name.html" => make_html(),
559 "$admin_name.php" => make_php());
560 if($GLOBALS['opt_public_something']) {
561 $files["$nice_name.html ->"] = "$admin_name.html";
562 $files["$nice_name.php ->"] = "$admin_name.php";
564 if($GLOBALS['opt_db']) {
565 $files["$nice_name.sql"] = make_sql();
567 if($GLOBALS['opt_email']) {
568 $files["$nice_name.email.txt"] = make_email();
570 make_tar($nice_name, $files);