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_varname($_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_varname($_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($GLOBALS['opt_display'] != 'Yes') {
272 $tem->show('opt_display_a_else');
274 if(show_in_listing($type, $input, $format, $sql)) {
275 if($format == 'bool' || $format == 'yesno') {
276 $tem->set('listing_enc', 'yesno');
277 $tem->show('listing_value_enc');
278 } elseif($input == 'date') {
279 $tem->set('listing_enc', 'mmddyyyy');
280 $tem->show('listing_value_enc');
281 } elseif($type == 'thumb') {
282 $tem->show('listing_value_thumb');
284 $tem->set('listing_enc', 'html');
285 $tem->show('listing_value_enc');
287 $tem->show('listing_head_col');
288 $tem->show('listing_row_col');
293 if($GLOBALS['opt_db'] == 'Yes') {
294 $tem->show('opt_db_1');
295 $tem->show('opt_db_2');
297 $tem->show('opt_db_1_else');
300 if($GLOBALS['opt_listing'] == 'Yes') {
301 $tem->show('opt_listing_1');
304 if($GLOBALS['opt_display'] == 'Yes') {
305 $tem->show('opt_display_1');
306 $tem->show('opt_display_2');
309 if($GLOBALS['opt_email'] == 'Yes' && $GLOBALS['opt_db'] != 'Yes') {
310 $tem->set('name', 'send');
311 $tem->set('caption', 'Send');
313 $tem->set('name', 'save');
314 $tem->set('caption', 'Save');
316 $tem->show('submit');
321 if($has_html_editors) {
322 $tem->show('html_editor_headers');
328 return $tem->get('form');
332 function view_html() {
337 function show_in_listing($type, $input, $format, $sql) {
346 if($type == 'image') {
353 function make_php() {
354 $has_html_editors = false;
356 $tem->load('code/wfpl/metaform/template.php');
357 $tem->set('file_name', $GLOBALS['file_name']);
358 $tem->set('table_name', $GLOBALS['table_name']);
359 $tem->set('singular', $GLOBALS['singular']);
360 $tem->set('plural', $GLOBALS['plural']);
361 $fields = get_fields();
364 $always_field = find_always_field($fields);
365 $image_included_yet = false;
366 foreach($fields as $field) {
367 list($name, $type, $input, $format, $sql) = $field;
368 if($input != 'submit') {
369 $tem->set('format', $format);
370 $tem->set('name', $name);
371 $tem->set('db_field', ''); # we don't want to use the value from last time
373 if($db_fields != '') $db_fields .= ',';
375 if($php_fields != '') $php_fields .= ', ';
376 $php_fields .= '$' . $name;
378 if($input == 'image') {
379 if($type == 'thumb') {
380 $tem->show('thumb_settings');
381 $tem->show('thumb_upload_params');
382 $tem->show('thumb_w_h');
384 $tem->show('image_settings');
385 $tem->show('image_upload');
386 if(!$image_included_yet) {
387 $tem->show('image_include');
388 $tem->show('upload_max');
389 $tem->show('upload_settings');
390 $image_included_yet = true;
393 if($input == 'html') {
394 $has_html_editors = true;
395 } elseif($input == 'pulldown') {
396 $tem->show('pulldowns');
397 $tem->show('pulldown_format_extra');
399 $tem->show('formats');
401 $tem->show('tem_sets');
404 if($GLOBALS['opt_listing'] == 'Yes') {
405 if(show_in_listing($type, $input, $format, $sql)) {
406 $tem->show('listing_fields_1');
407 $tem->show('listing_fields_2');
412 if($has_html_editors) {
413 $tem->show('show_extra_headers');
416 $tem->set('always_field', $always_field);
417 $tem->set('db_fields', $db_fields);
418 $tem->set('php_fields', $php_fields);
419 $tem->set('metaform_url', edit_url());
420 if($GLOBALS['opt_listing'] == 'Yes') {
421 $tem->show('opt_listing_1');
422 $tem->show('opt_listing_2');
424 if($GLOBALS['opt_display'] == 'Yes') {
425 $tem->show('opt_display_1');
426 $tem->show('opt_display_2');
428 $tem->show('opt_display_1_else');
429 $tem->show('opt_display_2_else');
431 if($GLOBALS['opt_db'] == 'Yes') {
432 $tem->show('opt_db_1');
433 $tem->show('opt_db_2');
434 $tem->show('opt_db_3');
435 $tem->show('opt_db_4');
436 $tem->show('opt_db_5');
438 if($GLOBALS['opt_email'] == 'Yes') {
439 $tem->show('opt_email_1');
440 $tem->show('opt_email_2');
442 if($GLOBALS['opt_http_pass'] == 'Yes') {
443 $tem->show('opt_http_pass_1');
444 $tem->show('opt_http_pass_2');
449 # make a URL for the edit page with all the fields filled in
450 function edit_url() {
452 $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
453 $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
454 $url = ereg_replace('/[a-z0-9_.]*\?', '/?', $url);
455 $url = str_replace('jasonwoof.l', 'jasonwoof.com', $url); # so that code generated on Jason's home computer will display a publically accessible link.
459 function view_php() {
464 function make_email() {
466 $tem->load('code/wfpl/metaform/template.email.txt');
467 $tem->set('file_name', $GLOBALS['file_name']);
468 $tem->set('table_name', $GLOBALS['table_name']);
469 $tem->set('singular', $GLOBALS['singular']);
470 $tem->set('plural', $GLOBALS['plural']);
471 $fields = get_fields();
472 foreach($fields as $field) {
473 list($name, $type, $input, $format, $sql) = $field;
474 $tem->set('name', $name);
475 $tem->set('caption', $name); # fixme
476 if($type == 'textarea') {
477 $tem->show('multi_line');
478 } elseif($type == 'checkbox') {
479 $tem->show('checkbox');
481 $tem->show('normal');
483 $tem->show('fields');
488 function make_htaccess() {
490 $tem->set('form', $GLOBALS['file_name']);
491 return $tem->run('code/wfpl/metaform/htaccess');
494 function view_email() {
500 tem_load('code/wfpl/metaform/preview.html');
501 tem_set('file_name', $GLOBALS['file_name']);
502 tem_set('table_name', $GLOBALS['table_name']);
503 tem_set('singular', $GLOBALS['singular']);
504 tem_set('plural', $GLOBALS['plural']);
505 tem_set('fields', $_REQUEST['fields']);
506 $preview_tem = new tem();
507 $preview_tem->load_str(make_html(false));
508 if($GLOBALS['opt_db'] == 'Yes') {
509 $preview_tem->show('new_msg');
511 $fields = get_fields();
512 foreach($fields as $field) {
513 list($name, $type, $input, $format, $sql) = $field;
514 if($type == 'pulldown') {
515 pulldown($name, array('option 1', 'option 2', 'option 3'));
518 $preview = $preview_tem->run();
520 $preview = ereg_replace('type="submit"', 'type="submit" disabled="disabled"', $preview);
521 tem_set('preview', $preview);
527 function download_tar() {
528 $name = $GLOBALS['file_name'];
530 ".htaccess" => make_htaccess(),
531 "run.php ->" => 'code/wfpl/run.php',
532 "style.css" => read_whole_file('code/wfpl/metaform/style.css'),
533 "$name.html" => make_html(),
534 "$name.php" => make_php());
535 if($GLOBALS['opt_db'] == 'Yes') {
536 $data["$name.sql"] = make_sql();
538 if($GLOBALS['opt_email'] == 'Yes') {
539 $data["$name.email.txt"] = make_email();
541 make_tar($name, $data);