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(__DIR__.'/'.'template.php');
22 require_once(__DIR__.'/'.'http.php');
23 require_once(__DIR__.'/'.'tar.php');
24 require_once(__DIR__.'/'.'format.php');
26 # see 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,2)'),
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) {
98 function metaform_main() {
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(__DIR__.'/'.'metaform/main.html');
157 list_available_types();
159 exit(); # in case we're being called by wfpl_main or something
163 function field_input($type) { return $GLOBALS['types'][$type][0]; }
164 function field_format($type) { return $GLOBALS['types'][$type][1]; }
165 function field_sql($type) { return $GLOBALS['types'][$type][2]; }
167 function get_fields() {
168 # no sense in doing all this so many times
169 if(isset($GLOBALS['gotten_fields'])) {
170 return $GLOBALS['gotten_fields'];
173 $fields_str = unix_newlines($_REQUEST['fields']);
174 $GLOBALS['gotten_fields'] = array();
175 $fields_str = rtrim($fields_str);
176 $fields = split("\n", $fields_str);
177 foreach($fields as $field) {
178 $field = trim($field);
179 if(substr($field, -1) == '{') {
180 $caption = trim(substr($field, 0, -1));
181 $name = format_varname($caption);
184 # FIXME restore parsing of option lists for pulldowns
188 $div = strpos($field, ' ');
189 if ($div === false) {
190 $div = strpos($field, "\t");
192 if ($div === false) {
193 if (isset($GLOBALS['types'][$field])) {
194 # if just one word, and it's a type, use it as name/caption and type
198 $first_word = trim(substr($field, 0, $div));
199 if (isset($GLOBALS['types'][$first_word])) {
200 # if the first word (of multiple) is a type, remove it from the name/caption
202 $field = trim(substr($field, $div + 1));
205 # see if there's an explicit name/caption splitter
206 $div = strpos($field, '|');
207 if ($div !== false) {
208 $name = trim(substr($field, 0, $div));
209 $caption = trim(substr($field, $div + 1));
210 if (isset($GLOBALS['types'][$name])) {
212 } elseif (isset($GLOBALS['types'][strtolower($caption)])) {
213 $type = strtolower($caption);
216 $name = format_varname($field);
217 $caption = format_caption($field);
220 if ($type === null) {
224 $input = field_input($type);
225 $format = field_format($type);
226 $sql = field_sql($type);
227 $GLOBALS['gotten_fields'][] = array(
228 'caption' => $caption,
234 'options' => $options
238 return $GLOBALS['gotten_fields'];
241 # this one, that you're using to create forms
242 function set_form_action() {
243 $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
244 if($action == '') $action = './';
245 tem_set('form_action', $action);
248 # perfect HTTP headers for viewing created files
249 function view_headers() {
250 header('Content-type: text/plain');
256 function make_sql() {
258 $tem->load(__DIR__.'/'.'metaform/template.sql');
259 tem_set_globals($tem);
260 $fields = get_fields();
261 foreach($fields as $field) {
262 if($field['sql'] != 'n/a') {
263 $tem->set('name', $field['name']);
264 $tem->set('type', $field['sql']);
265 if(substr($field['sql'], 0, 3) == 'int' || substr($field['sql'], 0, 7) == 'decimal') {
266 $tem->set('default', '0');
267 } elseif($field['format'] == 'yesno') {
268 $tem->set('default', '"No"');
270 $tem->set('default', '""');
272 $tem->show('column');
279 function view_sql() {
284 # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
285 function find_always_field(&$fields) {
286 foreach($fields as $field) {
287 if($field['input'] != 'submit' && $field['input'] != 'image' && $field['input'] != 'file' && $field['input'] != 'checkbox' && $field['input'] != 'radio' && $field['type'] != '{' && $field['type'] != '}') {
288 return $field['name'];
297 # pass false if you want to exclude the <head> and <body> tag etc.
298 function make_html($whole_file = true) {
299 $has_html_editors = false;
301 $tem->load(__DIR__.'/'.'metaform/template.html');
302 tem_set_globals($tem);
303 $fields = get_fields();
304 $tem->set('always_field', find_always_field($fields));
305 $hidden_fields = array();
306 $visible_fields = array();
307 $display_fields = array();
308 $listing_headers = array();
309 $listing_fields = array();
310 foreach($fields as $field) {
311 if($field['input'] == 'hidden') {
312 $hidden_fields[] = array('name' => $field['name']);
314 $visible_fields[] = array(
315 $field['input'] => array(
316 'name' => $field['name'],
317 'caption' => $field['caption']
322 if($field['input'] == 'image' || $field['input'] == 'file') {
323 $tem->set('uploads');
324 $tem->set('enctype_attr', '" enctype="multipart/form-data');
325 } elseif($field['input'] == 'html') {
326 $has_html_editors = true;
327 $tem->set('html_field_name', $field['name']);
328 $tem->set('replace_textarea');
331 switch($field['input']) {
337 $display_type = $field['input'];
340 $display_type = 'short';
342 if($field['format'] != 'n/a') {
343 $display_fields[] = array($display_type => array(
344 'name' => $field['name'], 'caption' => $field['caption']));
347 if(show_in_listing($field['type'], $field['input'], $field['format'], $field['sql'])) {
348 $listing_headers[] = array(
349 'caption' => $field['caption'],
350 'name' => $field['name']
352 $listing_field = array(
353 'name' => $field['name']
355 if($field['format'] == 'bool' || $field['format'] == 'yesno') {
356 $listing_field['enc'] = 'yesno';
357 } elseif($field['input'] == 'date') {
358 $listing_field['enc'] = 'mmddyyyy';
359 } elseif($field['type'] == 'thumb') {
360 $listing_field['thumb'] = true;
362 $listing_field['enc'] = 'html';
364 $listing_fields[] = $listing_field;
369 if($GLOBALS['opt_email'] == 'Yes' && !$GLOBALS['opt_db']) {
370 $visible_fields[] = array('submit' => array(
372 'caption' => 'Send'));
374 $visible_fields[] = array('submit' => array(
376 'caption' => 'Save'));
379 $form_fields = array();
380 $form_fields['visible_fields'] = $visible_fields;
382 $form_fields['hidden_fields'] = $hidden_fields;
384 $tem->set('form', $form_fields);
386 # opt_display and opt_listing control whether these are actually displayed
387 $tem->set('display_fields', $display_fields);
388 $tem->set('listing_headers', $listing_headers);
389 $tem->set('listing_fields', $listing_fields);
392 if($has_html_editors) {
393 $tem->set('html_editor_headers');
400 $tem2->load_str('<!--~form~-->');
406 function view_html() {
411 function show_in_listing($type, $input, $format, $sql) {
422 if($type == 'image') {
429 function pulldown_options_array($options) {
431 $pulldown_options = array();
432 foreach($options as $option) {
433 $option = preg_replace("/['\\\\]/", '\\\\$0', $option);
434 $pulldown_options[] = "'$option'";
436 $pulldown_options = 'array(' . join(', ', $pulldown_options) . ')';
438 $pulldown_options = "array(array('op1', 'Option One'), array('op2', 'Option Two'), 'n/a')";
441 return $pulldown_options;
444 function make_php() {
445 $has_html_editors = false;
447 $tem->load(__DIR__.'/'.'metaform/template.php');
448 tem_set_globals($tem);
449 $fields = get_fields();
451 $always_field = find_always_field($fields);
452 $image_included_yet = false;
453 $name_to_caption = array();
454 foreach($fields as $field) {
455 $name_to_caption[] = array('name' => $field['name'], 'caption' => $field['caption']);
456 if($field['input'] != 'submit') {
457 $tem->set('format', $field['format']);
458 $tem->set('name', $field['name']);
459 $tem->set('db_field', ''); # we don't want to use the value from last time
460 if($field['sql'] != 'n/a') {
461 if($db_fields != '') $db_fields .= ',';
462 $db_fields .= $field['name'];
464 if($field['input'] == 'image') {
465 if($field['type'] == 'thumb') {
466 $tem->show('thumb_settings');
467 $tem->show('thumb_upload_params');
468 $tem->show('thumb_w_h');
470 $tem->show('image_settings');
471 $tem->show('image_upload');
473 } else if($field['input'] == 'file') {
474 $tem->show('file_settings');
475 $tem->show('file_upload');
478 if($field['input'] == 'html') {
479 $has_html_editors = true;
480 } elseif($field['input'] == 'pulldown' || $field['input'] == 'radio') {
481 $pulldown_options = pulldown_options_array($field['options']);
482 $tem->set('pulldown_options', $pulldown_options);
483 $tem->set('has_pulldowns');
484 $tem->show('pulldowns');
485 $tem->show('pulldown_format_extra');
487 if($field['format'] != 'n/a') {
488 $tem->show('formats');
493 if($GLOBALS['opt_listing']) {
494 if(show_in_listing($field['type'], $field['input'], $field['format'], $field['sql'])) {
495 $tem->show('listing_fields_1');
496 $tem->show('listing_fields_2');
501 $tem->set('name_to_caption', $name_to_caption);
504 $tem->show('uploads_include');
505 $tem->show('upload_max');
506 $tem->show('upload_settings');
507 $image_included_yet = true;
510 if($has_html_editors) {
511 $tem->show('show_extra_headers');
514 $tem->set('always_field', $always_field);
515 $tem->set('db_fields', $db_fields);
516 $tem->set('metaform_url', edit_url());
517 if($GLOBALS['opt_email']) {
518 $this_domain = $_SERVER['HTTP_HOST'];
519 if(substr($this_domain, -2) == '.l') {
520 $this_domain = substr($this_domain, 0, -1) . 'com';
522 $tem->set('this_domain', $this_domain);
527 # make a URL for the edit page with all the fields filled in
528 function edit_url() {
530 $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
531 $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
532 $url = ereg_replace('/[a-z0-9_.]*\?', '/?', $url);
536 function view_php() {
541 function make_email() {
543 $tem->load(__DIR__.'/'.'metaform/template.email.txt');
544 tem_set_globals($tem);
545 $fields = get_fields();
546 foreach($fields as $field) {
547 $tem->set('name', $field['name']);
548 $tem->set('caption', $field['caption']);
549 if($field['type'] == 'textarea') {
550 $tem->show('multi_line');
551 } elseif($field['type'] == 'checkbox') {
552 $tem->show('checkbox');
554 $tem->show('normal');
556 $tem->show('fields');
561 function make_htaccess() {
563 $tem->set('form', $GLOBALS['file_name']);
564 return $tem->run(__DIR__.'/'.'metaform/htaccess');
567 function view_email() {
573 tem_load(__DIR__.'/'.'metaform/preview.html');
574 tem_set_globals($GLOBALS['wfpl_template']);
575 tem_set('fields', $_REQUEST['fields']);
576 $preview_tem = new tem();
577 $preview_tem->load_str(make_html(false));
578 if($GLOBALS['opt_db']) {
579 $preview_tem->show('new_msg');
581 $fields = get_fields();
582 foreach($fields as $field) {
583 if($field['type'] == 'pulldown' || $field['type'] == 'radio') {
584 pulldown($field['name'], eval('return ' . pulldown_options_array($field['options']) . ';'));
587 $preview = $preview_tem->run();
589 $preview = ereg_replace('type="submit"', 'type="submit" disabled="disabled"', $preview);
590 tem_set('preview', $preview);
596 function download_tar() {
597 $admin_name = $GLOBALS['file_name'];
598 $nice_name = $GLOBALS['public_file_name'];
600 "README" => "These files are meant to work with wfpl.\n\nSee: http://sametwice.com/wfpl\n",
601 ".htaccess" => make_htaccess(),
602 "config.php" => "<?php\n\nrequire_once(__DIR__.'/'.'inc/wfpl/main.php');\nmetaform_main();\n",
603 "styl.styl" => read_whole_file(__DIR__.'/'.'metaform/styl.styl'),
604 "template.html" => read_whole_file(__DIR__.'/'.'metaform/site-template.html'),
605 "$admin_name.html" => make_html(),
606 "$admin_name.php" => make_php());
607 if($GLOBALS['opt_public_something']) {
608 $files["$nice_name.html ->"] = "$admin_name.html";
609 $files["$nice_name.php ->"] = "$admin_name.php";
611 if($GLOBALS['opt_db']) {
612 $files["$nice_name.sql"] = make_sql();
614 if($GLOBALS['opt_email']) {
615 $files["$nice_name.email.txt"] = make_email();
617 make_tar($nice_name, $files);