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])) {
92 if(isset($_REQUEST['singular'])) {
93 $GLOBALS['file_name'] = format_varname($_REQUEST['file_name']);
94 $GLOBALS['table_name'] = format_varname($_REQUEST['table_name']);
95 $GLOBALS['plural'] = format_oneline($_REQUEST['plural']);
96 # backwards compatibility:
97 if(isset($_REQUEST['form_name'])) {
98 $GLOBALS['file_name'] = $GLOBALS['table_name'] = $GLOBALS['plural'] = format_varname($_REQUEST['form_name']);
101 $GLOBALS['singular'] = format_oneline($_REQUEST['singular']);
102 $GLOBALS['opt_email'] = format_yesno($_REQUEST['opt_email']);
103 $GLOBALS['opt_db'] = format_yesno($_REQUEST['opt_db']);
104 $GLOBALS['opt_listing'] = format_yesno($_REQUEST['opt_listing']);
105 $GLOBALS['opt_display'] = format_yesno($_REQUEST['opt_display']);
106 $GLOBALS['opt_pass'] = format_yesno($_REQUEST['opt_pass']);
109 tem_set_globals($GLOBALS['wfpl_template']);
112 if(isset($_REQUEST['fields'])) {
113 if(isset($_REQUEST['view_sql'])) {
116 } elseif(isset($_REQUEST['view_php'])) {
119 } elseif(isset($_REQUEST['view_html'])) {
122 } elseif(isset($_REQUEST['view_email'])) {
125 } elseif(isset($_REQUEST['download_tar'])) {
128 } elseif(isset($_REQUEST['preview'])) {
131 } elseif(isset($_REQUEST['edit'])) {
132 tem_set('fields', $_REQUEST['fields']);
135 die("Sorry... couldn't tell which button you pressed");
141 tem_load('code/wfpl/metaform/main.html');
142 list_available_types();
147 function field_input($type) { return $GLOBALS['types'][$type][0]; }
148 function field_format($type) { return $GLOBALS['types'][$type][1]; }
149 function field_sql($type) { return $GLOBALS['types'][$type][2]; }
151 function get_fields() {
152 # no sense in doing all this so many times
153 if(isset($GLOBALS['gotten_fields'])) {
154 return $GLOBALS['gotten_fields'];
157 $fields_str = unix_newlines($_REQUEST['fields']);
158 $GLOBALS['gotten_fields'] = array();
159 $fields_str = rtrim($fields_str);
160 $fields = split("\n", $fields_str);
161 foreach($fields as $field) {
162 list($name, $type, $options) = split(' *', $field);
163 if($options) $options = split(',', $options);
164 if(!$type) $type = $name;
165 $input = field_input($type);
166 $format = field_format($type);
167 $sql = field_sql($type);
168 $GLOBALS['gotten_fields'][] = array($name, $type, $input, $format, $sql, $options);
170 return $GLOBALS['gotten_fields'];
173 # this one, that you're using to create forms
174 function set_form_action() {
175 $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
176 if($action == '') $action = './';
177 tem_set('form_action', $action);
180 # perfect HTTP headers for viewing created files
181 function view_headers() {
182 header('Content-type: text/plain');
188 function make_sql() {
190 $tem->load('code/wfpl/metaform/template.sql');
191 tem_set_globals($tem);
192 $fields = get_fields();
193 foreach($fields as $field) {
194 list($name, $type, $input, $format, $sql) = $field;
196 $tem->set('name', $name);
197 $tem->set('type', $sql);
198 if(substr($sql, 0, 3) == 'int' || substr($sql, 0, 7) == 'decimal') {
199 $tem->set('default', '0');
200 } elseif($format == 'yesno') {
201 $tem->set('default', '"No"');
203 $tem->set('default', '""');
205 $tem->show('column');
212 function view_sql() {
217 # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
218 function find_always_field($fields) {
219 foreach($fields as $field) {
220 list($name, $type, $input, $format, $sql) = $field;
221 if($input != 'submit' && $input != 'image' && $input != 'file' && $input != 'checkbox' && $input != 'radio') {
231 # pass false if you want to exclude the <head> and <body> tag etc.
232 function make_html($whole_file = true) {
233 $uploads_output_already = false;
234 $has_html_editors = false;
236 $tem->load('code/wfpl/metaform/template.html');
237 tem_set_globals($tem);
238 $fields = get_fields();
239 $tem->set('always_field', find_always_field($fields));
240 foreach($fields as $field) {
241 list($name, $type, $input, $format, $sql) = $field;
242 $tem->set('name', $name);
243 $tem->set('caption', format_caption($name));
245 if($input != 'hidden') {
249 if(($input == 'image' || $input == 'file') && !$uploads_output_already) {
250 $tem->show('uploads');
251 $tem->set('enctype_attr', '" enctype="multipart/form-data');
252 $uploads_output_already = true;
253 } elseif($input == 'html') {
254 $has_html_editors = true;
255 $tem->set('html_field_name', $name);
256 $tem->show('replace_textarea');
259 if($GLOBALS['opt_display'] == 'Yes') {
262 $tem->show('display_image');
265 $tem->show('display_yesno');
268 $tem->show('display_date');
271 $tem->show('display_multiline');
274 $tem->show('display_html');
277 $tem->show('display_short');
279 $tem->show('display_row');
282 if($GLOBALS['opt_listing'] == 'Yes') {
283 if(show_in_listing($type, $input, $format, $sql)) {
284 if($format == 'bool' || $format == 'yesno') {
285 $tem->set('listing_enc', 'yesno');
286 $tem->show('listing_value_enc');
287 } elseif($input == 'date') {
288 $tem->set('listing_enc', 'mmddyyyy');
289 $tem->show('listing_value_enc');
290 } elseif($type == 'thumb') {
291 $tem->show('listing_value_thumb');
293 $tem->set('listing_enc', 'html');
294 $tem->show('listing_value_enc');
297 $tem->show('listing_head_col');
298 $tem->show('listing_row_col');
303 if($GLOBALS['opt_email'] == 'Yes' && $GLOBALS['opt_db'] != 'Yes') {
304 $tem->set('name', 'send');
305 $tem->set('caption', 'Send');
307 $tem->set('name', 'save');
308 $tem->set('caption', 'Save');
310 $tem->show('submit');
315 if($has_html_editors) {
316 $tem->show('html_editor_headers');
322 return $tem->get('form');
326 function view_html() {
331 function show_in_listing($type, $input, $format, $sql) {
340 if($type == 'image') {
347 function make_php() {
348 $has_html_editors = false;
350 $tem->load('code/wfpl/metaform/template.php');
351 tem_set_globals($tem);
352 $fields = get_fields();
354 $always_field = find_always_field($fields);
355 $image_included_yet = false;
356 foreach($fields as $field) {
357 list($name, $type, $input, $format, $sql) = $field;
358 if($input != 'submit') {
359 $tem->set('format', $format);
360 $tem->set('name', $name);
361 $tem->set('db_field', ''); # we don't want to use the value from last time
363 if($db_fields != '') $db_fields .= ',';
366 if($input == 'image') {
367 if($type == 'thumb') {
368 $tem->show('thumb_settings');
369 $tem->show('thumb_upload_params');
370 $tem->show('thumb_w_h');
372 $tem->show('image_settings');
373 $tem->show('image_upload');
375 } else if($input == 'file') {
376 $tem->show('file_settings');
377 $tem->show('file_upload');
380 if($input == 'html') {
381 $has_html_editors = true;
382 } elseif($input == 'pulldown') {
383 $tem->show('pulldowns');
384 $tem->show('pulldown_format_extra');
386 $tem->show('formats');
390 if($GLOBALS['opt_listing'] == 'Yes') {
391 if(show_in_listing($type, $input, $format, $sql)) {
392 $tem->show('listing_fields_1');
393 $tem->show('listing_fields_2');
398 $tem->show('uploads_include');
399 $tem->show('upload_max');
400 $tem->show('upload_settings');
401 $image_included_yet = true;
404 if($has_html_editors) {
405 $tem->show('show_extra_headers');
408 $tem->set('always_field', $always_field);
409 $tem->set('db_fields', $db_fields);
410 $tem->set('metaform_url', edit_url());
411 if($GLOBALS['opt_email'] == 'Yes') {
412 $this_domain = $_SERVER['HTTP_HOST'];
413 if(substr($this_domain, -2) == '.l') {
414 $this_domain = substr($this_domain, 0, -1) . 'com';
416 $tem->set('this_domain', $this_domain);
421 # make a URL for the edit page with all the fields filled in
422 function edit_url() {
424 $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
425 $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
426 $url = ereg_replace('/[a-z0-9_.]*\?', '/?', $url);
427 $url = str_replace('jasonwoof.l', 'jasonwoof.com', $url); # so that code generated on Jason's home computer will display a publically accessible link.
431 function view_php() {
436 function make_email() {
438 $tem->load('code/wfpl/metaform/template.email.txt');
439 tem_set_globals($tem);
440 $fields = get_fields();
441 foreach($fields as $field) {
442 list($name, $type, $input, $format, $sql) = $field;
443 $tem->set('name', $name);
444 $tem->set('caption', format_caption($name));
445 if($type == 'textarea') {
446 $tem->show('multi_line');
447 } elseif($type == 'checkbox') {
448 $tem->show('checkbox');
450 $tem->show('normal');
452 $tem->show('fields');
457 function make_htaccess() {
459 $tem->set('form', $GLOBALS['file_name']);
460 return $tem->run('code/wfpl/metaform/htaccess');
463 function view_email() {
469 tem_load('code/wfpl/metaform/preview.html');
470 tem_set_globals($GLOBALS['wfpl_template']);
471 tem_set('fields', $_REQUEST['fields']);
472 $preview_tem = new tem();
473 $preview_tem->load_str(make_html(false));
474 if($GLOBALS['opt_db'] == 'Yes') {
475 $preview_tem->show('new_msg');
477 $fields = get_fields();
478 foreach($fields as $field) {
479 list($name, $type, $input, $format, $sql) = $field;
480 if($type == 'pulldown') {
481 pulldown($name, array('option 1', 'option 2', 'option 3'));
484 $preview = $preview_tem->run();
486 $preview = ereg_replace('type="submit"', 'type="submit" disabled="disabled"', $preview);
487 tem_set('preview', $preview);
493 function download_tar() {
494 $name = $GLOBALS['file_name'];
496 "INSTALL" => read_whole_file('code/wfpl/metaform/INSTALL'),
497 ".htaccess" => make_htaccess(),
498 #"run.php ->" => 'code/wfpl/run.php',
499 "style.css" => read_whole_file('code/wfpl/metaform/style.css'),
500 "$name.html" => make_html(),
501 "$name.php" => make_php());
502 if($GLOBALS['opt_db'] == 'Yes') {
503 $data["$name.sql"] = make_sql();
505 if($GLOBALS['opt_email'] == 'Yes') {
506 $data["$name.email.txt"] = make_email();
508 make_tar($name, $data);