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])) {
88 $tem->set($bool . '_else', 1);
95 if(isset($_REQUEST['singular'])) {
96 $GLOBALS['file_name'] = format_varname($_REQUEST['file_name']);
97 $GLOBALS['table_name'] = format_varname($_REQUEST['table_name']);
98 $GLOBALS['plural'] = format_oneline($_REQUEST['plural']);
99 # backwards compatibility:
100 if(isset($_REQUEST['form_name'])) {
101 $GLOBALS['file_name'] = $GLOBALS['table_name'] = $GLOBALS['plural'] = format_varname($_REQUEST['form_name']);
104 $GLOBALS['singular'] = format_oneline($_REQUEST['singular']);
105 $GLOBALS['opt_email'] = format_yesno($_REQUEST['opt_email']);
106 $GLOBALS['opt_db'] = format_yesno($_REQUEST['opt_db']);
107 $GLOBALS['opt_listing'] = format_yesno($_REQUEST['opt_listing']);
108 $GLOBALS['opt_display'] = format_yesno($_REQUEST['opt_display']);
109 $GLOBALS['opt_pass'] = format_yesno($_REQUEST['opt_pass']);
112 tem_set_globals($GLOBALS['wfpl_template']);
115 if(isset($_REQUEST['fields'])) {
116 if(isset($_REQUEST['view_sql'])) {
119 } elseif(isset($_REQUEST['view_php'])) {
122 } elseif(isset($_REQUEST['view_html'])) {
125 } elseif(isset($_REQUEST['view_email'])) {
128 } elseif(isset($_REQUEST['download_tar'])) {
131 } elseif(isset($_REQUEST['preview'])) {
134 } elseif(isset($_REQUEST['edit'])) {
135 tem_set('fields', $_REQUEST['fields']);
138 die("Sorry... couldn't tell which button you pressed");
144 tem_load('code/wfpl/metaform/main.html');
145 list_available_types();
150 function field_input($type) { return $GLOBALS['types'][$type][0]; }
151 function field_format($type) { return $GLOBALS['types'][$type][1]; }
152 function field_sql($type) { return $GLOBALS['types'][$type][2]; }
154 function get_fields() {
155 # no sense in doing all this so many times
156 if(isset($GLOBALS['gotten_fields'])) {
157 return $GLOBALS['gotten_fields'];
160 $fields_str = unix_newlines($_REQUEST['fields']);
161 $GLOBALS['gotten_fields'] = array();
162 $fields_str = rtrim($fields_str);
163 $fields = split("\n", $fields_str);
164 foreach($fields as $field) {
165 list($name, $type, $options) = split(' *', $field);
166 if($options) $options = split(',', $options);
167 if(!$type) $type = $name;
168 $input = field_input($type);
169 $format = field_format($type);
170 $sql = field_sql($type);
171 $GLOBALS['gotten_fields'][] = array($name, $type, $input, $format, $sql, $options);
173 return $GLOBALS['gotten_fields'];
176 # this one, that you're using to create forms
177 function set_form_action() {
178 $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
179 if($action == '') $action = './';
180 tem_set('form_action', $action);
183 # perfect HTTP headers for viewing created files
184 function view_headers() {
185 header('Content-type: text/plain');
191 function make_sql() {
193 $tem->load('code/wfpl/metaform/template.sql');
194 tem_set_globals($tem);
195 $fields = get_fields();
196 foreach($fields as $field) {
197 list($name, $type, $input, $format, $sql) = $field;
199 $tem->set('name', $name);
200 $tem->set('type', $sql);
201 if(substr($sql, 0, 3) == 'int' || substr($sql, 0, 7) == 'decimal') {
202 $tem->set('default', '0');
203 } elseif($format == 'yesno') {
204 $tem->set('default', '"No"');
206 $tem->set('default', '""');
208 $tem->show('column');
215 function view_sql() {
220 # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
221 function find_always_field($fields) {
222 foreach($fields as $field) {
223 list($name, $type, $input, $format, $sql) = $field;
224 if($input != 'submit' && $input != 'image' && $input != 'file' && $input != 'checkbox' && $input != 'radio') {
234 # pass false if you want to exclude the <head> and <body> tag etc.
235 function make_html($whole_file = true) {
236 $uploads_output_already = false;
237 $has_html_editors = false;
239 $tem->load('code/wfpl/metaform/template.html');
240 tem_set_globals($tem);
241 $fields = get_fields();
242 $tem->set('always_field', find_always_field($fields));
243 foreach($fields as $field) {
244 list($name, $type, $input, $format, $sql) = $field;
245 $tem->set('name', $name);
246 $tem->set('caption', format_caption($name));
248 if($input != 'hidden') {
252 if(($input == 'image' || $input == 'file') && !$uploads_output_already) {
253 $tem->show('uploads');
254 $tem->set('enctype_attr', '" enctype="multipart/form-data');
255 $uploads_output_already = true;
256 } elseif($input == 'html') {
257 $has_html_editors = true;
258 $tem->set('html_field_name', $name);
259 $tem->show('replace_textarea');
262 if($GLOBALS['opt_display'] == 'Yes') {
265 $tem->show('display_image');
268 $tem->show('display_yesno');
271 $tem->show('display_date');
274 $tem->show('display_multiline');
277 $tem->show('display_html');
280 $tem->show('display_short');
282 $tem->show('display_row');
285 if($GLOBALS['opt_listing'] == 'Yes') {
286 if(show_in_listing($type, $input, $format, $sql)) {
287 if($format == 'bool' || $format == 'yesno') {
288 $tem->set('listing_enc', 'yesno');
289 $tem->show('listing_value_enc');
290 } elseif($input == 'date') {
291 $tem->set('listing_enc', 'mmddyyyy');
292 $tem->show('listing_value_enc');
293 } elseif($type == 'thumb') {
294 $tem->show('listing_value_thumb');
296 $tem->set('listing_enc', 'html');
297 $tem->show('listing_value_enc');
300 $tem->show('listing_head_col');
301 $tem->show('listing_row_col');
306 if($GLOBALS['opt_email'] == 'Yes' && $GLOBALS['opt_db'] != 'Yes') {
307 $tem->set('name', 'send');
308 $tem->set('caption', 'Send');
310 $tem->set('name', 'save');
311 $tem->set('caption', 'Save');
313 $tem->show('submit');
318 if($has_html_editors) {
319 $tem->show('html_editor_headers');
325 return $tem->get('form');
329 function view_html() {
334 function show_in_listing($type, $input, $format, $sql) {
343 if($type == 'image') {
350 function make_php() {
351 $has_html_editors = false;
353 $tem->load('code/wfpl/metaform/template.php');
354 tem_set_globals($tem);
355 $fields = get_fields();
357 $always_field = find_always_field($fields);
358 $image_included_yet = false;
359 foreach($fields as $field) {
360 list($name, $type, $input, $format, $sql) = $field;
361 if($input != 'submit') {
362 $tem->set('format', $format);
363 $tem->set('name', $name);
364 $tem->set('db_field', ''); # we don't want to use the value from last time
366 if($db_fields != '') $db_fields .= ',';
369 if($input == 'image') {
370 if($type == 'thumb') {
371 $tem->show('thumb_settings');
372 $tem->show('thumb_upload_params');
373 $tem->show('thumb_w_h');
375 $tem->show('image_settings');
376 $tem->show('image_upload');
378 } else if($input == 'file') {
379 $tem->show('file_settings');
380 $tem->show('file_upload');
383 if($input == 'html') {
384 $has_html_editors = true;
385 } elseif($input == 'pulldown') {
386 $tem->show('pulldowns');
387 $tem->show('pulldown_format_extra');
389 $tem->show('formats');
393 if($GLOBALS['opt_listing'] == 'Yes') {
394 if(show_in_listing($type, $input, $format, $sql)) {
395 $tem->show('listing_fields_1');
396 $tem->show('listing_fields_2');
401 $tem->show('uploads_include');
402 $tem->show('upload_max');
403 $tem->show('upload_settings');
404 $image_included_yet = true;
407 if($has_html_editors) {
408 $tem->show('show_extra_headers');
411 $tem->set('always_field', $always_field);
412 $tem->set('db_fields', $db_fields);
413 $tem->set('metaform_url', edit_url());
414 if($GLOBALS['opt_email'] == 'Yes') {
415 $this_domain = $_SERVER['HTTP_HOST'];
416 if(substr($this_domain, -2) == '.l') {
417 $this_domain = substr($this_domain, 0, -1) . 'com';
419 $tem->set('this_domain', $this_domain);
424 # make a URL for the edit page with all the fields filled in
425 function edit_url() {
427 $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
428 $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
429 $url = ereg_replace('/[a-z0-9_.]*\?', '/?', $url);
430 $url = str_replace('jasonwoof.l', 'jasonwoof.com', $url); # so that code generated on Jason's home computer will display a publically accessible link.
434 function view_php() {
439 function make_email() {
441 $tem->load('code/wfpl/metaform/template.email.txt');
442 tem_set_globals($tem);
443 $fields = get_fields();
444 foreach($fields as $field) {
445 list($name, $type, $input, $format, $sql) = $field;
446 $tem->set('name', $name);
447 $tem->set('caption', format_caption($name));
448 if($type == 'textarea') {
449 $tem->show('multi_line');
450 } elseif($type == 'checkbox') {
451 $tem->show('checkbox');
453 $tem->show('normal');
455 $tem->show('fields');
460 function make_htaccess() {
462 $tem->set('form', $GLOBALS['file_name']);
463 return $tem->run('code/wfpl/metaform/htaccess');
466 function view_email() {
472 tem_load('code/wfpl/metaform/preview.html');
473 tem_set_globals($GLOBALS['wfpl_template']);
474 tem_set('fields', $_REQUEST['fields']);
475 $preview_tem = new tem();
476 $preview_tem->load_str(make_html(false));
477 if($GLOBALS['opt_db'] == 'Yes') {
478 $preview_tem->show('new_msg');
480 $fields = get_fields();
481 foreach($fields as $field) {
482 list($name, $type, $input, $format, $sql) = $field;
483 if($type == 'pulldown') {
484 pulldown($name, array('option 1', 'option 2', 'option 3'));
487 $preview = $preview_tem->run();
489 $preview = ereg_replace('type="submit"', 'type="submit" disabled="disabled"', $preview);
490 tem_set('preview', $preview);
496 function download_tar() {
497 $name = $GLOBALS['file_name'];
499 "INSTALL" => read_whole_file('code/wfpl/metaform/INSTALL'),
500 ".htaccess" => make_htaccess(),
501 #"run.php ->" => 'code/wfpl/run.php',
502 "style.css" => read_whole_file('code/wfpl/metaform/style.css'),
503 "$name.html" => make_html(),
504 "$name.php" => make_php());
505 if($GLOBALS['opt_db'] == 'Yes') {
506 $data["$name.sql"] = make_sql();
508 if($GLOBALS['opt_email'] == 'Yes') {
509 $data["$name.email.txt"] = make_email();
511 make_tar($name, $data);