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', 'oneline', 'varchar(200)'),
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', 'oneline', 'n/a')
58 function list_available_types() {
59 ksort($GLOBALS['types']);
60 foreach($GLOBALS['types'] as $key => $value) {
61 tem_set('type', $key);
63 tem_show('types_sep');
68 function tem_set_globals(&$tem) {
74 foreach($vars as $var) {
75 $tem->set($var, $GLOBALS[$var]);
84 foreach($bools as $bool) {
85 if(format_bool($GLOBALS[$bool])) {
92 if(isset($_REQUEST['singular'])) {
93 $GLOBALS['file_name'] = format_varname($_REQUEST['file_name']);
94 $GLOBALS['table_name'] = format_varname($_REQUEST['table_name']);
95 $GLOBALS['plural'] = format_oneline($_REQUEST['plural']);
96 # backwards compatibility:
97 if(isset($_REQUEST['form_name'])) {
98 $GLOBALS['file_name'] = $GLOBALS['table_name'] = $GLOBALS['plural'] = format_varname($_REQUEST['form_name']);
101 $GLOBALS['singular'] = format_oneline($_REQUEST['singular']);
102 $GLOBALS['opt_email'] = format_yesno($_REQUEST['opt_email']);
103 $GLOBALS['opt_db'] = format_yesno($_REQUEST['opt_db']);
104 $GLOBALS['opt_listing'] = format_yesno($_REQUEST['opt_listing']);
105 $GLOBALS['opt_display'] = format_yesno($_REQUEST['opt_display']);
106 $GLOBALS['opt_pass'] = format_yesno($_REQUEST['opt_pass']);
109 tem_set_globals($GLOBALS['wfpl_template']);
112 if(isset($_REQUEST['fields'])) {
113 if(isset($_REQUEST['view_sql'])) {
116 } elseif(isset($_REQUEST['view_php'])) {
119 } elseif(isset($_REQUEST['view_html'])) {
122 } elseif(isset($_REQUEST['view_email'])) {
125 } elseif(isset($_REQUEST['download_tar'])) {
128 } elseif(isset($_REQUEST['preview'])) {
131 } elseif(isset($_REQUEST['edit'])) {
132 tem_set('fields', $_REQUEST['fields']);
135 die("Sorry... couldn't tell which button you pressed");
141 tem_load('code/wfpl/metaform/main.html');
142 list_available_types();
147 function field_input($type) { return $GLOBALS['types'][$type][0]; }
148 function field_format($type) { return $GLOBALS['types'][$type][1]; }
149 function field_sql($type) { return $GLOBALS['types'][$type][2]; }
151 function get_fields() {
152 # no sense in doing all this so many times
153 if(isset($GLOBALS['gotten_fields'])) {
154 return $GLOBALS['gotten_fields'];
157 $fields_str = unix_newlines($_REQUEST['fields']);
158 $GLOBALS['gotten_fields'] = array();
159 $fields_str = rtrim($fields_str);
160 $fields = split("\n", $fields_str);
161 foreach($fields as $field) {
162 list($name, $type, $options) = split(' *', $field);
163 if($options) $options = split(',', $options);
164 if(!$type) $type = $name;
165 $input = field_input($type);
166 $format = field_format($type);
167 $sql = field_sql($type);
168 $GLOBALS['gotten_fields'][] = array($name, $type, $input, $format, $sql, $options);
170 return $GLOBALS['gotten_fields'];
173 # this one, that you're using to create forms
174 function set_form_action() {
175 $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
176 if($action == '') $action = './';
177 tem_set('form_action', $action);
180 # perfect HTTP headers for viewing created files
181 function view_headers() {
182 header('Content-type: text/plain');
188 function make_sql() {
190 $tem->load('code/wfpl/metaform/template.sql');
191 tem_set_globals($tem);
192 $fields = get_fields();
193 foreach($fields as $field) {
194 list($name, $type, $input, $format, $sql) = $field;
196 $tem->set('name', $name);
197 $tem->set('type', $sql);
198 if(substr($sql, 0, 3) == 'int' || substr($sql, 0, 7) == 'decimal') {
199 $tem->set('default', '0');
200 } elseif($format == 'yesno') {
201 $tem->set('default', '"No"');
203 $tem->set('default', '""');
205 $tem->show('column');
212 function view_sql() {
217 # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
218 function find_always_field($fields) {
219 foreach($fields as $field) {
220 list($name, $type, $input, $format, $sql) = $field;
221 if($input != 'submit' && $input != 'image' && $input != 'file' && $input != 'checkbox' && $input != 'radio') {
231 # pass false if you want to exclude the <head> and <body> tag etc.
232 function make_html($whole_file = true) {
233 $has_html_editors = false;
235 $tem->load('code/wfpl/metaform/template.html');
236 tem_set_globals($tem);
237 $fields = get_fields();
238 $tem->set('always_field', find_always_field($fields));
239 $hidden_fields = array();
240 $visible_fields = array();
241 $display_fields = array();
242 $listing_headers = array();
243 $listing_fields = array();
244 foreach($fields as $field) {
245 list($name, $type, $input, $format, $sql) = $field;
246 if($input == 'hidden') {
247 $hidden_fields[] = array('name' => $name);
249 $visible_fields[] = array($input => array(
251 'caption' => format_caption($name)));
254 if($input == 'image' || $input == 'file') {
255 $tem->set('uploads');
256 $tem->set('enctype_attr', '" enctype="multipart/form-data');
257 } elseif($input == 'html') {
258 $has_html_editors = true;
259 $tem->set('html_field_name', $name);
260 $tem->set('replace_textarea');
269 $display_type = $input;
272 $display_type = 'short';
274 $display_fields[] = array($display_type => array(
275 'name' => $name, 'caption' => format_caption($name)));
277 if(show_in_listing($type, $input, $format, $sql)) {
278 $listing_headers[] = array('caption' => format_caption($name));
279 $listing_field = array('name' => $name);
280 if($format == 'bool' || $format == 'yesno') {
281 $listing_field['enc'] = 'yesno';
282 } elseif($input == 'date') {
283 $listing_field['enc'] = 'mmddyyyy';
284 } elseif($type == 'thumb') {
285 $listing_field['thumb'] = true;
287 $listing_field['enc'] = 'html';
289 $listing_fields[] = $listing_field;
294 if($GLOBALS['opt_email'] == 'Yes' && $GLOBALS['opt_db'] != 'Yes') {
295 $visible_fields[] = array('submit' => array(
297 'caption' => 'Send'));
299 $visible_fields[] = array('submit' => array(
301 'caption' => 'Save'));
304 $tem->set('form', array(
305 'visible_fields' => $visible_fields,
306 'hidden_fields' => $hidden_fields));
308 # opt_display and opt_listing control whether these are actually displayed
309 $tem->set('display_fields', $display_fields);
310 $tem->set('listing_headers', $listing_headers);
311 $tem->set('listing_fields', $listing_fields);
314 if($has_html_editors) {
315 $tem->set('html_editor_headers');
322 $tem2->load_str('<!--~form~-->');
328 function view_html() {
333 function show_in_listing($type, $input, $format, $sql) {
342 if($type == 'image') {
349 function make_php() {
350 $has_html_editors = false;
352 $tem->load('code/wfpl/metaform/template.php');
353 tem_set_globals($tem);
354 $fields = get_fields();
356 $always_field = find_always_field($fields);
357 $image_included_yet = false;
358 foreach($fields as $field) {
359 list($name, $type, $input, $format, $sql) = $field;
360 if($input != 'submit') {
361 $tem->set('format', $format);
362 $tem->set('name', $name);
363 $tem->set('db_field', ''); # we don't want to use the value from last time
365 if($db_fields != '') $db_fields .= ',';
368 if($input == 'image') {
369 if($type == 'thumb') {
370 $tem->show('thumb_settings');
371 $tem->show('thumb_upload_params');
372 $tem->show('thumb_w_h');
374 $tem->show('image_settings');
375 $tem->show('image_upload');
377 } else if($input == 'file') {
378 $tem->show('file_settings');
379 $tem->show('file_upload');
382 if($input == 'html') {
383 $has_html_editors = true;
384 } elseif($input == 'pulldown') {
385 $tem->show('pulldowns');
386 $tem->show('pulldown_format_extra');
388 $tem->show('formats');
392 if($GLOBALS['opt_listing'] == 'Yes') {
393 if(show_in_listing($type, $input, $format, $sql)) {
394 $tem->show('listing_fields_1');
395 $tem->show('listing_fields_2');
400 $tem->show('uploads_include');
401 $tem->show('upload_max');
402 $tem->show('upload_settings');
403 $image_included_yet = true;
406 if($has_html_editors) {
407 $tem->show('show_extra_headers');
410 $tem->set('always_field', $always_field);
411 $tem->set('db_fields', $db_fields);
412 $tem->set('metaform_url', edit_url());
413 if($GLOBALS['opt_email'] == 'Yes') {
414 $this_domain = $_SERVER['HTTP_HOST'];
415 if(substr($this_domain, -2) == '.l') {
416 $this_domain = substr($this_domain, 0, -1) . 'com';
418 $tem->set('this_domain', $this_domain);
423 # make a URL for the edit page with all the fields filled in
424 function edit_url() {
426 $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
427 $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
428 $url = ereg_replace('/[a-z0-9_.]*\?', '/?', $url);
429 $url = str_replace('jasonwoof.l', 'jasonwoof.com', $url); # so that code generated on Jason's home computer will display a publically accessible link.
433 function view_php() {
438 function make_email() {
440 $tem->load('code/wfpl/metaform/template.email.txt');
441 tem_set_globals($tem);
442 $fields = get_fields();
443 foreach($fields as $field) {
444 list($name, $type, $input, $format, $sql) = $field;
445 $tem->set('name', $name);
446 $tem->set('caption', format_caption($name));
447 if($type == 'textarea') {
448 $tem->show('multi_line');
449 } elseif($type == 'checkbox') {
450 $tem->show('checkbox');
452 $tem->show('normal');
454 $tem->show('fields');
459 function make_htaccess() {
461 $tem->set('form', $GLOBALS['file_name']);
462 return $tem->run('code/wfpl/metaform/htaccess');
465 function view_email() {
471 tem_load('code/wfpl/metaform/preview.html');
472 tem_set_globals($GLOBALS['wfpl_template']);
473 tem_set('fields', $_REQUEST['fields']);
474 $preview_tem = new tem();
475 $preview_tem->load_str(make_html(false));
476 if($GLOBALS['opt_db'] == 'Yes') {
477 $preview_tem->show('new_msg');
479 $fields = get_fields();
480 foreach($fields as $field) {
481 list($name, $type, $input, $format, $sql) = $field;
482 if($type == 'pulldown') {
483 pulldown($name, array('option 1', 'option 2', 'option 3'));
486 $preview = $preview_tem->run();
488 $preview = ereg_replace('type="submit"', 'type="submit" disabled="disabled"', $preview);
489 tem_set('preview', $preview);
495 function download_tar() {
496 $name = $GLOBALS['file_name'];
498 "INSTALL" => read_whole_file('code/wfpl/metaform/INSTALL'),
499 ".htaccess" => make_htaccess(),
500 #"run.php ->" => 'code/wfpl/run.php',
501 "style.css" => read_whole_file('code/wfpl/metaform/style.css'),
502 "$name.html" => make_html(),
503 "$name.php" => make_php());
504 if($GLOBALS['opt_db'] == 'Yes') {
505 $data["$name.sql"] = make_sql();
507 if($GLOBALS['opt_email'] == 'Yes') {
508 $data["$name.email.txt"] = make_email();
510 make_tar($name, $data);