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', 'options', 'varchar(100)'),
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', 'n/a', 'n/a'),
56 '{' => array('fieldset', 'n/a', 'n/a'),
57 '}' => array('end_fieldset','n/a', 'n/a')
60 function list_available_types() {
61 ksort($GLOBALS['types']);
62 foreach($GLOBALS['types'] as $key => $value) {
63 tem_set('type', $key);
65 tem_show('types_sep');
70 function tem_set_globals(&$tem) {
76 foreach($vars as $var) {
77 $tem->set($var, $GLOBALS[$var]);
86 foreach($bools as $bool) {
94 if(isset($_REQUEST['singular'])) {
95 $GLOBALS['file_name'] = format_varname($_REQUEST['file_name']);
96 $GLOBALS['table_name'] = format_varname($_REQUEST['table_name']);
97 $GLOBALS['plural'] = format_oneline($_REQUEST['plural']);
98 # backwards compatibility:
99 if(isset($_REQUEST['form_name'])) {
100 $GLOBALS['file_name'] = $GLOBALS['table_name'] = $GLOBALS['plural'] = format_varname($_REQUEST['form_name']);
103 $GLOBALS['singular'] = format_oneline($_REQUEST['singular']);
104 $GLOBALS['opt_email'] = format_bool($_REQUEST['opt_email']);
105 $GLOBALS['opt_db'] = format_bool($_REQUEST['opt_db']);
106 $GLOBALS['opt_listing'] = format_bool($_REQUEST['opt_listing']);
107 $GLOBALS['opt_display'] = format_bool($_REQUEST['opt_display']);
108 $GLOBALS['opt_pass'] = format_bool($_REQUEST['opt_pass']);
111 tem_set_globals($GLOBALS['wfpl_template']);
114 if(isset($_REQUEST['fields'])) {
115 if(isset($_REQUEST['view_sql'])) {
118 } elseif(isset($_REQUEST['view_php'])) {
121 } elseif(isset($_REQUEST['view_html'])) {
124 } elseif(isset($_REQUEST['view_email'])) {
127 } elseif(isset($_REQUEST['download_tar'])) {
130 } elseif(isset($_REQUEST['preview'])) {
133 } elseif(isset($_REQUEST['edit'])) {
134 tem_set('fields', $_REQUEST['fields']);
137 die("Sorry... couldn't tell which button you pressed");
143 tem_load('code/wfpl/metaform/main.html');
144 list_available_types();
149 function field_input($type) { return $GLOBALS['types'][$type][0]; }
150 function field_format($type) { return $GLOBALS['types'][$type][1]; }
151 function field_sql($type) { return $GLOBALS['types'][$type][2]; }
153 function get_fields() {
154 # no sense in doing all this so many times
155 if(isset($GLOBALS['gotten_fields'])) {
156 return $GLOBALS['gotten_fields'];
159 $fields_str = unix_newlines($_REQUEST['fields']);
160 $GLOBALS['gotten_fields'] = array();
161 $fields_str = rtrim($fields_str);
162 $fields = split("\n", $fields_str);
163 foreach($fields as $field) {
164 $field = trim($field);
165 if(substr($field, -1) == '{') {
166 $name = trim(substr($field, 0, -1)); # FIXME: stop this from getting enc_caption()ed
169 } elseif(substr($field, -1) == '{') {
174 list($name, $type, $options) = split(' *', $field);
175 if($options) $options = explode(',', $options);
176 if(!$type) $type = $name;
178 $input = field_input($type);
179 $format = field_format($type);
180 $sql = field_sql($type);
181 $GLOBALS['gotten_fields'][] = array($name, $type, $input, $format, $sql, $options);
184 return $GLOBALS['gotten_fields'];
187 # this one, that you're using to create forms
188 function set_form_action() {
189 $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
190 if($action == '') $action = './';
191 tem_set('form_action', $action);
194 # perfect HTTP headers for viewing created files
195 function view_headers() {
196 header('Content-type: text/plain');
202 function make_sql() {
204 $tem->load('code/wfpl/metaform/template.sql');
205 tem_set_globals($tem);
206 $fields = get_fields();
207 foreach($fields as $field) {
208 list($name, $type, $input, $format, $sql) = $field;
210 $tem->set('name', $name);
211 $tem->set('type', $sql);
212 if(substr($sql, 0, 3) == 'int' || substr($sql, 0, 7) == 'decimal') {
213 $tem->set('default', '0');
214 } elseif($format == 'yesno') {
215 $tem->set('default', '"No"');
217 $tem->set('default', '""');
219 $tem->show('column');
226 function view_sql() {
231 # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
232 function find_always_field($fields) {
233 foreach($fields as $field) {
234 list($name, $type, $input, $format, $sql) = $field;
235 if($input != 'submit' && $input != 'image' && $input != 'file' && $input != 'checkbox' && $input != 'radio' && $type != '{') {
245 # pass false if you want to exclude the <head> and <body> tag etc.
246 function make_html($whole_file = true) {
247 $has_html_editors = false;
249 $tem->load('code/wfpl/metaform/template.html');
250 tem_set_globals($tem);
251 $fields = get_fields();
252 $tem->set('always_field', find_always_field($fields));
253 $hidden_fields = array();
254 $visible_fields = array();
255 $display_fields = array();
256 $listing_headers = array();
257 $listing_fields = array();
258 foreach($fields as $field) {
259 list($name, $type, $input, $format, $sql) = $field;
260 if($input == 'hidden') {
261 $hidden_fields[] = array('name' => $name);
263 $visible_fields[] = array($input => array(
265 'caption' => format_caption($name)));
268 if($input == 'image' || $input == 'file') {
269 $tem->set('uploads');
270 $tem->set('enctype_attr', '" enctype="multipart/form-data');
271 } elseif($input == 'html') {
272 $has_html_editors = true;
273 $tem->set('html_field_name', $name);
274 $tem->set('replace_textarea');
283 $display_type = $input;
286 $display_type = 'short';
288 if($format != 'n/a') {
289 $display_fields[] = array($display_type => array(
290 'name' => $name, 'caption' => format_caption($name)));
293 if(show_in_listing($type, $input, $format, $sql)) {
294 $listing_headers[] = array('caption' => format_caption($name));
295 $listing_field = array('name' => $name);
296 if($format == 'bool' || $format == 'yesno') {
297 $listing_field['enc'] = 'yesno';
298 } elseif($input == 'date') {
299 $listing_field['enc'] = 'mmddyyyy';
300 } elseif($type == 'thumb') {
301 $listing_field['thumb'] = true;
303 $listing_field['enc'] = 'html';
305 $listing_fields[] = $listing_field;
310 if($GLOBALS['opt_email'] == 'Yes' && !$GLOBALS['opt_db']) {
311 $visible_fields[] = array('submit' => array(
313 'caption' => 'Send'));
315 $visible_fields[] = array('submit' => array(
317 'caption' => 'Save'));
320 $tem->set('form', array(
321 'visible_fields' => $visible_fields,
322 'hidden_fields' => $hidden_fields));
324 # opt_display and opt_listing control whether these are actually displayed
325 $tem->set('display_fields', $display_fields);
326 $tem->set('listing_headers', $listing_headers);
327 $tem->set('listing_fields', $listing_fields);
330 if($has_html_editors) {
331 $tem->set('html_editor_headers');
338 $tem2->load_str('<!--~form~-->');
344 function view_html() {
349 function show_in_listing($type, $input, $format, $sql) {
360 if($type == 'image') {
367 function pulldown_options_array($options) {
369 $pulldown_options = array();
370 foreach($options as $option) {
371 $option = preg_replace("/['\\\\]/", '\\\\$0', $option);
372 $pulldown_options[] = "'$option'";
374 $pulldown_options = 'array(' . join(', ', $pulldown_options) . ')';
376 $pulldown_options = "array(array('op1', 'Option One'), array('op2', 'Option Two'), 'n/a')";
379 return $pulldown_options;
382 function make_php() {
383 $has_html_editors = false;
385 $tem->load('code/wfpl/metaform/template.php');
386 tem_set_globals($tem);
387 $fields = get_fields();
389 $always_field = find_always_field($fields);
390 $image_included_yet = false;
391 foreach($fields as $field) {
392 list($name, $type, $input, $format, $sql, $options) = $field;
393 if($input != 'submit') {
394 $tem->set('format', $format);
395 $tem->set('name', $name);
396 $tem->set('db_field', ''); # we don't want to use the value from last time
398 if($db_fields != '') $db_fields .= ',';
401 if($input == 'image') {
402 if($type == 'thumb') {
403 $tem->show('thumb_settings');
404 $tem->show('thumb_upload_params');
405 $tem->show('thumb_w_h');
407 $tem->show('image_settings');
408 $tem->show('image_upload');
410 } else if($input == 'file') {
411 $tem->show('file_settings');
412 $tem->show('file_upload');
415 if($input == 'html') {
416 $has_html_editors = true;
417 } elseif($input == 'pulldown' || $input == 'radio') {
418 $pulldown_options = pulldown_options_array($options);
419 $tem->set('pulldown_options', $pulldown_options);
420 $tem->show('pulldowns');
421 $tem->show('pulldown_format_extra');
423 if($format != 'n/a') {
424 $tem->show('formats');
429 if($GLOBALS['opt_listing']) {
430 if(show_in_listing($type, $input, $format, $sql)) {
431 $tem->show('listing_fields_1');
432 $tem->show('listing_fields_2');
437 $tem->show('uploads_include');
438 $tem->show('upload_max');
439 $tem->show('upload_settings');
440 $image_included_yet = true;
443 if($has_html_editors) {
444 $tem->show('show_extra_headers');
447 $tem->set('always_field', $always_field);
448 $tem->set('db_fields', $db_fields);
449 $tem->set('metaform_url', edit_url());
450 if($GLOBALS['opt_email']) {
451 $this_domain = $_SERVER['HTTP_HOST'];
452 if(substr($this_domain, -2) == '.l') {
453 $this_domain = substr($this_domain, 0, -1) . 'com';
455 $tem->set('this_domain', $this_domain);
460 # make a URL for the edit page with all the fields filled in
461 function edit_url() {
463 $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
464 $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
465 $url = ereg_replace('/[a-z0-9_.]*\?', '/?', $url);
466 $url = str_replace('jasonwoof.l', 'jasonwoof.com', $url); # so that code generated on Jason's home computer will display a publically accessible link.
470 function view_php() {
475 function make_email() {
477 $tem->load('code/wfpl/metaform/template.email.txt');
478 tem_set_globals($tem);
479 $fields = get_fields();
480 foreach($fields as $field) {
481 list($name, $type, $input, $format, $sql) = $field;
482 $tem->set('name', $name);
483 $tem->set('caption', format_caption($name));
484 if($type == 'textarea') {
485 $tem->show('multi_line');
486 } elseif($type == 'checkbox') {
487 $tem->show('checkbox');
489 $tem->show('normal');
491 $tem->show('fields');
496 function make_htaccess() {
498 $tem->set('form', $GLOBALS['file_name']);
499 return $tem->run('code/wfpl/metaform/htaccess');
502 function view_email() {
508 tem_load('code/wfpl/metaform/preview.html');
509 tem_set_globals($GLOBALS['wfpl_template']);
510 tem_set('fields', $_REQUEST['fields']);
511 $preview_tem = new tem();
512 $preview_tem->load_str(make_html(false));
513 if($GLOBALS['opt_db']) {
514 $preview_tem->show('new_msg');
516 $fields = get_fields();
517 foreach($fields as $field) {
518 list($name, $type, $input, $format, $sql, $options) = $field;
519 if($type == 'pulldown' || $type == 'radio') {
520 pulldown($name, eval('return ' . pulldown_options_array($options) . ';'));
523 $preview = $preview_tem->run();
525 $preview = ereg_replace('type="submit"', 'type="submit" disabled="disabled"', $preview);
526 tem_set('preview', $preview);
532 function download_tar() {
533 $name = $GLOBALS['file_name'];
535 "INSTALL" => read_whole_file('code/wfpl/metaform/INSTALL'),
536 ".htaccess" => make_htaccess(),
537 #"run.php ->" => 'code/wfpl/run.php',
538 "style.css" => read_whole_file('code/wfpl/metaform/style.css'),
539 "$name.html" => make_html(),
540 "$name.php" => make_php());
541 if($GLOBALS['opt_db']) {
542 $data["$name.sql"] = make_sql();
544 if($GLOBALS['opt_email']) {
545 $data["$name.email.txt"] = make_email();
547 make_tar($name, $data);