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('leftcheck', 'bool', 'int(1)'),
50 'rightcheck' => array('checkbox', 'bool', 'int(1)'),
51 'rightyesno' => array('checkbox', 'yesno', 'varchar(3)'),
52 'yesno' => array('leftcheck', 'yesno', 'varchar(3)'),
53 'delete' => array('checkbox', 'yesno', 'n/a'),
54 'image' => array('image', 'oneline', 'varchar(200)'),
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');
69 if(isset($_REQUEST['form_name'])) {
70 $GLOBALS['form_name'] = format_varname($_REQUEST['form_name']);
71 $GLOBALS['singular'] = format_varname($_REQUEST['singular']);
72 tem_set('singular', $GLOBALS['singular']);
73 $GLOBALS['opt_email'] = format_yesno($_REQUEST['opt_email']);
74 tem_set('opt_email', $GLOBALS['opt_email']);
75 $GLOBALS['opt_db'] = format_yesno($_REQUEST['opt_db']);
76 tem_set('opt_db', $GLOBALS['opt_db']);
77 $GLOBALS['opt_listing'] = format_yesno($_REQUEST['opt_listing']);
78 tem_set('opt_listing', $GLOBALS['opt_listing']);
79 $GLOBALS['opt_display'] = format_yesno($_REQUEST['opt_display']);
80 tem_set('opt_display', $GLOBALS['opt_display']);
81 $GLOBALS['opt_http_pass'] = format_yesno($_REQUEST['opt_http_pass']);
82 tem_set('opt_http_pass', $GLOBALS['opt_http_pass']);
84 $GLOBALS['form_name'] = 'some_form';
87 if(isset($_REQUEST['fields'])) {
88 if(isset($_REQUEST['view_sql'])) {
91 } elseif(isset($_REQUEST['view_php'])) {
94 } elseif(isset($_REQUEST['view_html'])) {
97 } elseif(isset($_REQUEST['view_email'])) {
100 } elseif(isset($_REQUEST['download_tar'])) {
103 } elseif(isset($_REQUEST['preview'])) {
106 } elseif(isset($_REQUEST['edit'])) {
107 tem_set('fields', $_REQUEST['fields']);
108 tem_set('form_name', $GLOBALS['form_name']);
111 die("Sorry... couldn't tell which button you pressed");
117 tem_load('code/wfpl/metaform/main.html');
118 list_available_types();
123 function field_input($type) { return $GLOBALS['types'][$type][0]; }
124 function field_format($type) { return $GLOBALS['types'][$type][1]; }
125 function field_sql($type) { return $GLOBALS['types'][$type][2]; }
127 function get_fields() {
128 # no sense in doing all this so many times
129 if(isset($GLOBALS['gotten_fields'])) {
130 return $GLOBALS['gotten_fields'];
133 $fields_str = unix_newlines($_REQUEST['fields']);
134 $GLOBALS['gotten_fields'] = array();
135 $fields_str = rtrim($fields_str);
136 $fields = split("\n", $fields_str);
137 foreach($fields as $field) {
138 list($name, $type, $options) = split(' *', $field);
139 if($options) $options = split(',', $options);
140 if(!$type) $type = $name;
141 $input = field_input($type);
142 $format = field_format($type);
143 $sql = field_sql($type);
144 $GLOBALS['gotten_fields'][] = array($name, $type, $input, $format, $sql, $options);
146 return $GLOBALS['gotten_fields'];
149 # this one, that you're using to create forms
150 function set_form_action() {
151 $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
152 if($action == '') $action = './';
153 tem_set('form_action', $action);
156 # perfect HTTP headers for viewing created files
157 function view_headers() {
158 header('Content-type: text/plain');
164 function make_sql() {
166 $tem->load('code/wfpl/metaform/template.sql');
167 $tem->set('form_name', $GLOBALS['form_name']);
168 $fields = get_fields();
169 foreach($fields as $field) {
170 list($name, $type, $input, $format, $sql) = $field;
172 $tem->set('name', $name);
173 $tem->set('type', $sql);
174 if(substr($sql, 0, 3) == 'int' || substr($sql, 0, 7) == 'decimal') {
175 $tem->set('default', '0');
176 } elseif($format == 'yesno') {
177 $tem->set('default', '"No"');
179 $tem->set('default', '""');
181 $tem->show('column');
188 function view_sql() {
193 # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
194 function find_always_field($fields) {
195 foreach($fields as $field) {
196 list($name, $type, $input, $format, $sql) = $field;
197 if($input != 'submit' && $input != 'checkbox' && $input != 'radio') {
207 # pass false if you want to exclude the <head> and <body> tag etc.
208 function make_html($whole_file = true) {
209 $uploads_output_already = false;
210 $has_html_editors = false;
212 $tem->load('code/wfpl/metaform/template.html');
213 $tem->set('form_name', $GLOBALS['form_name']);
214 $tem->set('singular', $GLOBALS['singular']);
215 $fields = get_fields();
216 $tem->set('always_field', find_always_field($fields));
217 foreach($fields as $field) {
218 list($name, $type, $input, $format, $sql) = $field;
219 $tem->set('name', $name);
220 $tem->set('caption', format_caption($name));
222 if($input != 'hidden') {
226 if($input == 'image' && !$uploads_output_already) {
227 $tem->show('uploads');
228 $tem->set('enctype_attr', '" enctype="multipart/form-data');
229 $uploads_output_already = true;
230 } elseif($input == 'html') {
231 $has_html_editors = true;
232 $tem->set('html_field_name', $name);
233 $tem->show('replace_textarea');
236 if($GLOBALS['opt_display'] == 'Yes') {
240 $tem->show('display_yesno');
243 $tem->show('display_date');
246 $tem->show('display_multiline');
249 $tem->show('display_html');
252 $tem->show('display_short');
254 $tem->show('display_row');
257 if($GLOBALS['opt_listing'] == 'Yes') {
258 if($GLOBALS['opt_display'] != 'Yes') {
259 $tem->show('opt_display_a_else');
261 if(show_in_listing($type, $input, $format, $sql)) {
262 if($format == 'bool' || $format == 'yesno') {
263 $tem->set('listing_enc', 'yesno');
264 } elseif($input == 'date') {
265 $tem->set('listing_enc', 'mmddyyyy');
267 $tem->show('listing_head_col');
268 $tem->show('listing_row_col');
273 if($GLOBALS['opt_db'] == 'Yes') {
274 $tem->show('opt_db_1');
275 $tem->show('opt_db_2');
277 $tem->show('opt_db_1_else');
280 if($GLOBALS['opt_listing'] == 'Yes') {
281 $tem->show('opt_listing_1');
284 if($GLOBALS['opt_display'] == 'Yes') {
285 $tem->show('opt_display_1');
286 $tem->show('opt_display_2');
289 if($GLOBALS['opt_email'] == 'Yes' && $GLOBALS['opt_db'] != 'Yes') {
290 $tem->set('name', 'send');
291 $tem->set('caption', 'Send');
293 $tem->set('name', 'save');
294 $tem->set('caption', 'Save');
296 $tem->show('submit');
301 if($has_html_editors) {
302 $tem->show('html_editor_headers');
308 return $tem->get('form');
312 function view_html() {
317 function show_in_listing($type, $input, $format, $sql) {
331 function make_php() {
333 $tem->load('code/wfpl/metaform/template.php');
334 $tem->set('form_name', $GLOBALS['form_name']);
335 $tem->set('singular', $GLOBALS['singular']);
336 $fields = get_fields();
339 $always_field = find_always_field($fields);
340 $image_included_yet = false;
341 foreach($fields as $field) {
342 list($name, $type, $input, $format, $sql) = $field;
343 if($input != 'submit') {
344 $tem->set('format', $format);
345 $tem->set('name', $name);
346 $tem->set('db_field', ''); # we don't want to use the value from last time
348 if($db_fields != '') $db_fields .= ',';
350 if($php_fields != '') $php_fields .= ', ';
351 $php_fields .= '$' . $name;
353 if($input == 'image') {
354 $tem->show('image_upload');
355 $tem->show('image_db');
356 if(!$image_included_yet) {
357 $tem->show('image_include');
358 $tem->show('upload_max');
359 $tem->show('upload_settings');
360 $image_included_yet = true;
363 if($input == 'pulldown') {
364 $tem->show('pulldowns');
365 $tem->show('pulldown_format_extra');
367 $tem->show('formats');
369 $tem->show('tem_sets');
372 if($GLOBALS['opt_listing'] == 'Yes') {
373 if(show_in_listing($type, $input, $format, $sql)) {
374 $tem->show('listing_fields_1');
375 $tem->show('listing_fields_2');
380 $tem->set('always_field', $always_field);
381 $tem->set('db_fields', $db_fields);
382 $tem->set('php_fields', $php_fields);
383 $tem->set('metaform_url', edit_url());
384 if($GLOBALS['opt_listing'] == 'Yes') {
385 $tem->show('opt_listing_1');
386 $tem->show('opt_listing_2');
388 if($GLOBALS['opt_display'] == 'Yes') {
389 $tem->show('opt_display_1');
390 $tem->show('opt_display_2');
392 $tem->show('opt_display_1_else');
393 $tem->show('opt_display_2_else');
395 if($GLOBALS['opt_db'] == 'Yes') {
396 $tem->show('opt_db_1');
397 $tem->show('opt_db_2');
398 $tem->show('opt_db_3');
399 $tem->show('opt_db_4');
400 $tem->show('opt_db_5');
402 if($GLOBALS['opt_email'] == 'Yes') {
403 $tem->show('opt_email_1');
404 $tem->show('opt_email_2');
406 if($GLOBALS['opt_http_pass'] == 'Yes') {
407 $tem->show('opt_http_pass_1');
408 $tem->show('opt_http_pass_2');
413 # make a URL for the edit page with all the fields filled in
414 function edit_url() {
416 $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
417 $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
418 $url = ereg_replace('/[a-z0-9_.]*\?', '/?', $url);
419 $url = str_replace('jasonwoof.l', 'jasonwoof.com', $url); # so that code generated on Jason's home computer will display a publically accessible link.
423 function view_php() {
429 function make_email() {
431 $tem->load('code/wfpl/metaform/template.email.txt');
432 $tem->set('form_name', $GLOBALS['form_name']);
433 $fields = get_fields();
434 foreach($fields as $field) {
435 list($name, $type, $input, $format, $sql) = $field;
436 $tem->set('name', $name);
437 $tem->set('caption', $name); # fixme
438 if($type == 'textarea') {
439 $tem->show('multi_line');
440 } elseif($type == 'checkbox') {
441 $tem->show('checkbox');
443 $tem->show('normal');
445 $tem->show('fields');
450 function make_htaccess() {
452 $tem->set('form', $GLOBALS['form_name']);
453 return $tem->run('code/wfpl/metaform/htaccess');
456 function view_email() {
463 tem_load('code/wfpl/metaform/preview.html');
464 tem_set('form_name', $GLOBALS['form_name']);
465 tem_set('fields', $_REQUEST['fields']);
466 $preview_tem = new tem();
467 $preview_tem->load_str(make_html(false));
468 if($GLOBALS['opt_db'] == 'Yes') {
469 $preview_tem->show('new_msg');
471 $fields = get_fields();
472 foreach($fields as $field) {
473 list($name, $type, $input, $format, $sql) = $field;
474 if($type == 'pulldown') {
475 pulldown($name, array('option 1', 'option 2', 'option 3'));
478 $preview = $preview_tem->run();
480 $preview = ereg_replace('type="submit"', 'type="submit" disabled="disabled"', $preview);
481 tem_set('preview', $preview);
487 function download_tar() {
488 $name = $GLOBALS['form_name'];
490 ".htaccess" => make_htaccess(),
491 "run.php ->" => 'code/wfpl/run.php',
492 "style.css" => read_whole_file('code/wfpl/metaform/style.css'),
493 "$name.html" => make_html(),
494 "$name.php" => make_php());
495 if($GLOBALS['opt_db'] == 'Yes') {
496 $data["$name.sql"] = make_sql();
498 if($GLOBALS['opt_email'] == 'Yes') {
499 $data["$name.email.txt"] = make_email();
501 make_tar($name, $data);