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(120)'),
55 'thumb' => array('image', 'oneline', 'varchar(240)'),
56 'submit' => array('submit', 'oneline', 'n/a')
59 function list_available_types() {
60 ksort($GLOBALS['types']);
61 foreach($GLOBALS['types'] as $key => $value) {
62 tem_set('type', $key);
64 tem_show('types_sep');
70 if(isset($_REQUEST['singular'])) {
71 $GLOBALS['file_name'] = format_varname($_REQUEST['file_name']);
72 $GLOBALS['table_name'] = format_varname($_REQUEST['table_name']);
73 $GLOBALS['plural'] = format_oneline($_REQUEST['plural']);
74 # backwards compatibility:
75 if(isset($_REQUEST['form_name'])) {
76 $GLOBALS['file_name'] = $GLOBALS['table_name'] = $GLOBALS['plural'] = format_varname($_REQUEST['form_name']);
78 tem_set('file_name', $GLOBALS['file_name']);
79 tem_set('table_name', $GLOBALS['table_name']);
80 tem_set('plural', $GLOBALS['plural']);
82 $GLOBALS['singular'] = format_oneline($_REQUEST['singular']);
83 tem_set('singular', $GLOBALS['singular']);
84 $GLOBALS['opt_email'] = format_yesno($_REQUEST['opt_email']);
85 tem_set('opt_email', $GLOBALS['opt_email']);
86 $GLOBALS['opt_db'] = format_yesno($_REQUEST['opt_db']);
87 tem_set('opt_db', $GLOBALS['opt_db']);
88 $GLOBALS['opt_listing'] = format_yesno($_REQUEST['opt_listing']);
89 tem_set('opt_listing', $GLOBALS['opt_listing']);
90 $GLOBALS['opt_display'] = format_yesno($_REQUEST['opt_display']);
91 tem_set('opt_display', $GLOBALS['opt_display']);
92 $GLOBALS['opt_http_pass'] = format_yesno($_REQUEST['opt_http_pass']);
93 tem_set('opt_http_pass', $GLOBALS['opt_http_pass']);
96 if(isset($_REQUEST['fields'])) {
97 if(isset($_REQUEST['view_sql'])) {
100 } elseif(isset($_REQUEST['view_php'])) {
103 } elseif(isset($_REQUEST['view_html'])) {
106 } elseif(isset($_REQUEST['view_email'])) {
109 } elseif(isset($_REQUEST['download_tar'])) {
112 } elseif(isset($_REQUEST['preview'])) {
115 } elseif(isset($_REQUEST['edit'])) {
116 tem_set('fields', $_REQUEST['fields']);
119 die("Sorry... couldn't tell which button you pressed");
125 tem_load('code/wfpl/metaform/main.html');
126 list_available_types();
131 function field_input($type) { return $GLOBALS['types'][$type][0]; }
132 function field_format($type) { return $GLOBALS['types'][$type][1]; }
133 function field_sql($type) { return $GLOBALS['types'][$type][2]; }
135 function get_fields() {
136 # no sense in doing all this so many times
137 if(isset($GLOBALS['gotten_fields'])) {
138 return $GLOBALS['gotten_fields'];
141 $fields_str = unix_newlines($_REQUEST['fields']);
142 $GLOBALS['gotten_fields'] = array();
143 $fields_str = rtrim($fields_str);
144 $fields = split("\n", $fields_str);
145 foreach($fields as $field) {
146 list($name, $type, $options) = split(' *', $field);
147 if($options) $options = split(',', $options);
148 if(!$type) $type = $name;
149 $input = field_input($type);
150 $format = field_format($type);
151 $sql = field_sql($type);
152 $GLOBALS['gotten_fields'][] = array($name, $type, $input, $format, $sql, $options);
154 return $GLOBALS['gotten_fields'];
157 # this one, that you're using to create forms
158 function set_form_action() {
159 $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
160 if($action == '') $action = './';
161 tem_set('form_action', $action);
164 # perfect HTTP headers for viewing created files
165 function view_headers() {
166 header('Content-type: text/plain');
172 function make_sql() {
174 $tem->load('code/wfpl/metaform/template.sql');
175 $tem->set('table_name', $GLOBALS['table_name']);
176 $fields = get_fields();
177 foreach($fields as $field) {
178 list($name, $type, $input, $format, $sql) = $field;
180 $tem->set('name', $name);
181 $tem->set('type', $sql);
182 if(substr($sql, 0, 3) == 'int' || substr($sql, 0, 7) == 'decimal') {
183 $tem->set('default', '0');
184 } elseif($format == 'yesno') {
185 $tem->set('default', '"No"');
187 $tem->set('default', '""');
189 $tem->show('column');
196 function view_sql() {
201 # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
202 function find_always_field($fields) {
203 foreach($fields as $field) {
204 list($name, $type, $input, $format, $sql) = $field;
205 if($input != 'submit' && $input != 'checkbox' && $input != 'radio') {
215 # pass false if you want to exclude the <head> and <body> tag etc.
216 function make_html($whole_file = true) {
217 $uploads_output_already = false;
218 $has_html_editors = false;
220 $tem->load('code/wfpl/metaform/template.html');
221 $tem->set('file_name', $GLOBALS['file_name']);
222 $tem->set('table_name', $GLOBALS['table_name']);
223 $tem->set('singular', $GLOBALS['singular']);
224 $tem->set('plural', $GLOBALS['plural']);
225 $fields = get_fields();
226 $tem->set('always_field', find_always_field($fields));
227 foreach($fields as $field) {
228 list($name, $type, $input, $format, $sql) = $field;
229 $tem->set('name', $name);
230 $tem->set('caption', format_caption($name));
232 if($input != 'hidden') {
236 if($input == 'image' && !$uploads_output_already) {
237 $tem->show('uploads');
238 $tem->set('enctype_attr', '" enctype="multipart/form-data');
239 $uploads_output_already = true;
240 } elseif($input == 'html') {
241 $has_html_editors = true;
242 $tem->set('html_field_name', $name);
243 $tem->show('replace_textarea');
246 if($GLOBALS['opt_display'] == 'Yes') {
249 $tem->show('display_image');
253 $tem->show('display_yesno');
256 $tem->show('display_date');
259 $tem->show('display_multiline');
262 $tem->show('display_html');
265 $tem->show('display_short');
267 $tem->show('display_row');
270 if($GLOBALS['opt_listing'] == 'Yes') {
271 if(show_in_listing($type, $input, $format, $sql)) {
272 if($format == 'bool' || $format == 'yesno') {
273 $tem->set('listing_enc', 'yesno');
274 $tem->show('listing_value_enc');
275 } elseif($input == 'date') {
276 $tem->set('listing_enc', 'mmddyyyy');
277 $tem->show('listing_value_enc');
278 } elseif($type == 'thumb') {
279 $tem->show('listing_value_thumb');
281 $tem->set('listing_enc', 'html');
282 $tem->show('listing_value_enc');
285 if($GLOBALS['opt_display'] != 'Yes') {
286 $tem->show('opt_display_a_else');
288 $tem->show('listing_head_col');
289 $tem->show('listing_row_col');
294 if($GLOBALS['opt_db'] == 'Yes') {
295 $tem->show('opt_db_1');
296 $tem->show('opt_db_2');
298 $tem->show('opt_db_1_else');
301 if($GLOBALS['opt_listing'] == 'Yes') {
302 $tem->show('opt_listing_1');
305 if($GLOBALS['opt_display'] == 'Yes') {
306 $tem->show('opt_display_1');
307 $tem->show('opt_display_2');
310 if($GLOBALS['opt_email'] == 'Yes' && $GLOBALS['opt_db'] != 'Yes') {
311 $tem->set('name', 'send');
312 $tem->set('caption', 'Send');
314 $tem->set('name', 'save');
315 $tem->set('caption', 'Save');
317 $tem->show('submit');
322 if($has_html_editors) {
323 $tem->show('html_editor_headers');
329 return $tem->get('form');
333 function view_html() {
338 function show_in_listing($type, $input, $format, $sql) {
347 if($type == 'image') {
354 function make_php() {
355 $has_html_editors = false;
357 $tem->load('code/wfpl/metaform/template.php');
358 $tem->set('file_name', $GLOBALS['file_name']);
359 $tem->set('table_name', $GLOBALS['table_name']);
360 $tem->set('singular', $GLOBALS['singular']);
361 $tem->set('plural', $GLOBALS['plural']);
362 $fields = get_fields();
365 $always_field = find_always_field($fields);
366 $image_included_yet = false;
367 foreach($fields as $field) {
368 list($name, $type, $input, $format, $sql) = $field;
369 if($input != 'submit') {
370 $tem->set('format', $format);
371 $tem->set('name', $name);
372 $tem->set('db_field', ''); # we don't want to use the value from last time
374 if($db_fields != '') $db_fields .= ',';
376 if($php_fields != '') $php_fields .= ', ';
377 $php_fields .= '$' . $name;
379 if($input == 'image') {
380 if($type == 'thumb') {
381 $tem->show('thumb_settings');
382 $tem->show('thumb_upload_params');
383 $tem->show('thumb_w_h');
385 $tem->show('image_settings');
386 $tem->show('image_upload');
387 if(!$image_included_yet) {
388 $tem->show('image_include');
389 $tem->show('upload_max');
390 $tem->show('upload_settings');
391 $image_included_yet = true;
394 if($input == 'html') {
395 $has_html_editors = true;
396 } elseif($input == 'pulldown') {
397 $tem->show('pulldowns');
398 $tem->show('pulldown_format_extra');
400 $tem->show('formats');
402 $tem->show('tem_sets');
405 if($GLOBALS['opt_listing'] == 'Yes') {
406 if(show_in_listing($type, $input, $format, $sql)) {
407 $tem->show('listing_fields_1');
408 $tem->show('listing_fields_2');
413 if($has_html_editors) {
414 $tem->show('show_extra_headers');
417 $tem->set('always_field', $always_field);
418 $tem->set('db_fields', $db_fields);
419 $tem->set('php_fields', $php_fields);
420 $tem->set('metaform_url', edit_url());
421 if($GLOBALS['opt_listing'] == 'Yes') {
422 $tem->show('opt_listing_1');
423 $tem->show('opt_listing_2');
425 if($GLOBALS['opt_display'] == 'Yes') {
426 $tem->show('opt_display_1');
427 $tem->show('opt_display_2');
429 $tem->show('opt_display_1_else');
430 $tem->show('opt_display_2_else');
432 if($GLOBALS['opt_db'] == 'Yes') {
433 $tem->show('opt_db_1');
434 $tem->show('opt_db_2');
435 $tem->show('opt_db_3');
436 $tem->show('opt_db_4');
437 $tem->show('opt_db_5');
439 if($GLOBALS['opt_email'] == 'Yes') {
440 $tem->show('opt_email_1');
441 $tem->show('opt_email_2');
443 if($GLOBALS['opt_http_pass'] == 'Yes') {
444 $tem->show('opt_http_pass_1');
445 $tem->show('opt_http_pass_2');
450 # make a URL for the edit page with all the fields filled in
451 function edit_url() {
453 $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
454 $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
455 $url = ereg_replace('/[a-z0-9_.]*\?', '/?', $url);
456 $url = str_replace('jasonwoof.l', 'jasonwoof.com', $url); # so that code generated on Jason's home computer will display a publically accessible link.
460 function view_php() {
465 function make_email() {
467 $tem->load('code/wfpl/metaform/template.email.txt');
468 $tem->set('file_name', $GLOBALS['file_name']);
469 $tem->set('table_name', $GLOBALS['table_name']);
470 $tem->set('singular', $GLOBALS['singular']);
471 $tem->set('plural', $GLOBALS['plural']);
472 $fields = get_fields();
473 foreach($fields as $field) {
474 list($name, $type, $input, $format, $sql) = $field;
475 $tem->set('name', $name);
476 $tem->set('caption', $name); # fixme
477 if($type == 'textarea') {
478 $tem->show('multi_line');
479 } elseif($type == 'checkbox') {
480 $tem->show('checkbox');
482 $tem->show('normal');
484 $tem->show('fields');
489 function make_htaccess() {
491 $tem->set('form', $GLOBALS['file_name']);
492 return $tem->run('code/wfpl/metaform/htaccess');
495 function view_email() {
501 tem_load('code/wfpl/metaform/preview.html');
502 tem_set('file_name', $GLOBALS['file_name']);
503 tem_set('table_name', $GLOBALS['table_name']);
504 tem_set('singular', $GLOBALS['singular']);
505 tem_set('plural', $GLOBALS['plural']);
506 tem_set('fields', $_REQUEST['fields']);
507 $preview_tem = new tem();
508 $preview_tem->load_str(make_html(false));
509 if($GLOBALS['opt_db'] == 'Yes') {
510 $preview_tem->show('new_msg');
512 $fields = get_fields();
513 foreach($fields as $field) {
514 list($name, $type, $input, $format, $sql) = $field;
515 if($type == 'pulldown') {
516 pulldown($name, array('option 1', 'option 2', 'option 3'));
519 $preview = $preview_tem->run();
521 $preview = ereg_replace('type="submit"', 'type="submit" disabled="disabled"', $preview);
522 tem_set('preview', $preview);
528 function download_tar() {
529 $name = $GLOBALS['file_name'];
531 ".htaccess" => make_htaccess(),
532 #"run.php ->" => 'code/wfpl/run.php',
533 "style.css" => read_whole_file('code/wfpl/metaform/style.css'),
534 "$name.html" => make_html(),
535 "$name.php" => make_php());
536 if($GLOBALS['opt_db'] == 'Yes') {
537 $data["$name.sql"] = make_sql();
539 if($GLOBALS['opt_email'] == 'Yes') {
540 $data["$name.email.txt"] = make_email();
542 make_tar($name, $data);