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('textbox', '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 'leftcheck' => array('leftcheck', 'yesno', 'varchar(3)'),
50 'checkbox' => array('checkbox', 'bool', 'int(1)'),
51 'yesno' => array('checkbox', 'yesno', 'varchar(3)'),
52 'delete' => array('checkbox', 'yesno', 'n/a'),
53 'image' => array('image', 'oneline', 'varchar(200)'),
54 'submit' => array('submit', 'oneline', 'n/a')
57 function list_available_types() {
59 foreach($GLOBALS['types'] as $key => $value) {
65 tem_set('available_types', $types);
70 if(isset($_REQUEST['form_name'])) {
71 $GLOBALS['form_name'] = ereg_replace('[^a-z0-9_-]', '', $_REQUEST['form_name']);
72 $GLOBALS['opt_email'] = format_yesno($_REQUEST['opt_email']);
73 tem_set('opt_email', $GLOBALS['opt_email']);
74 $GLOBALS['opt_db'] = format_yesno($_REQUEST['opt_db']);
75 tem_set('opt_db', $GLOBALS['opt_db']);
76 $GLOBALS['opt_listing'] = format_yesno($_REQUEST['opt_listing']);
77 tem_set('opt_listing', $GLOBALS['opt_listing']);
78 $GLOBALS['opt_http_pass'] = format_yesno($_REQUEST['opt_http_pass']);
79 tem_set('opt_http_pass', $GLOBALS['opt_http_pass']);
81 $GLOBALS['form_name'] = 'some_form';
84 if(isset($_REQUEST['fields'])) {
85 if(isset($_REQUEST['view_sql'])) {
88 } elseif(isset($_REQUEST['view_php'])) {
91 } elseif(isset($_REQUEST['view_html'])) {
94 } elseif(isset($_REQUEST['view_email'])) {
97 } elseif(isset($_REQUEST['download_tar'])) {
100 } elseif(isset($_REQUEST['preview'])) {
103 } elseif(isset($_REQUEST['edit'])) {
104 tem_set('fields', $_REQUEST['fields']);
105 tem_set('form_name', $GLOBALS['form_name']);
108 die("Sorry... couldn't tell which button you pressed");
114 list_available_types();
115 tem_output('code/wfpl/metaform/main.html');
119 function field_input($type) { return $GLOBALS['types'][$type][0]; }
120 function field_format($type) { return $GLOBALS['types'][$type][1]; }
121 function field_sql($type) { return $GLOBALS['types'][$type][2]; }
123 function get_fields() {
124 # no sense in doing all this so many times
125 if(isset($GLOBALS['gotten_fields'])) {
126 return $GLOBALS['gotten_fields'];
129 $fields_str = unix_newlines($_REQUEST['fields']);
130 $GLOBALS['gotten_fields'] = array();
131 $fields_str = rtrim($fields_str);
132 $fields = split("\n", $fields_str);
133 foreach($fields as $field) {
134 list($name, $type, $options) = split(' *', $field);
135 if($options) $options = split(',', $options);
136 if(!$type) $type = $name;
137 $input = field_input($type);
138 $format = field_format($type);
139 $sql = field_sql($type);
140 $GLOBALS['gotten_fields'][] = array($name, $type, $input, $format, $sql, $options);
142 return $GLOBALS['gotten_fields'];
145 # this one, that you're using to create forms
146 function set_form_action() {
147 $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
148 if($action == '') $action = './';
149 tem_set('form_action', $action);
152 # perfect HTTP headers for viewing created files
153 function view_headers() {
154 header('Content-type: text/plain');
160 function make_sql() {
162 $tem->load('code/wfpl/metaform/template.sql');
163 $tem->set('form_name', $GLOBALS['form_name']);
164 $fields = get_fields();
165 foreach($fields as $field) {
166 list($name, $type, $input, $format, $sql) = $field;
168 $tem->set('name', $name);
169 $tem->set('type', $sql);
170 if(substr($sql, 0, 3) == 'int' || substr($sql, 0, 7) == 'decimal') {
171 $tem->set('default', '0');
172 } elseif($format == 'yesno') {
173 $tem->set('default', '"No"');
175 $tem->set('default', '""');
177 $tem->show('column');
184 function view_sql() {
189 # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
190 function find_always_field($fields) {
191 foreach($fields as $field) {
192 list($name, $type, $input, $format, $sql) = $field;
193 if($input != 'submit' && $input != 'checkbox' && $input != 'radio') {
203 # pass false if you want to exclude the <head> and <body> tag etc.
204 function make_html($whole_file = true) {
205 $uploads_output_already = false;
206 $has_html_editors = false;
208 $tem->load('code/wfpl/metaform/template.html');
209 $tem->set('form_name', $GLOBALS['form_name']);
210 $fields = get_fields();
211 $tem->set('always_field', find_always_field($fields));
212 foreach($fields as $field) {
213 list($name, $type, $input, $format, $sql) = $field;
214 $tem->set('name', $name);
215 $tem->set('caption', format_caption($name));
217 if($input != 'hidden') {
220 if($input == 'image' && !$uploads_output_already) {
221 $tem->show('uploads');
222 $tem->set('enctype_attr', '" enctype="multipart/form-data');
223 $uploads_output_already = true;
224 } elseif($input == 'html') {
225 $has_html_editors = true;
226 $tem->set('html_field_name', $name);
227 $tem->show('replace_textarea');
231 if($GLOBALS['opt_db'] == 'Yes') {
232 $tem->show('opt_db_1');
233 $tem->show('opt_db_2');
235 $tem->show('opt_db_1_else');
238 if($GLOBALS['opt_listing'] == 'Yes') {
239 $tem->show('opt_listing_1');
241 $tem->show('opt_listing_1_else');
244 if($GLOBALS['opt_email'] == 'Yes' && $GLOBALS['opt_db'] != 'Yes') {
245 $tem->set('name', 'send');
246 $tem->set('caption', 'Send');
248 $tem->set('name', 'save');
249 $tem->set('caption', 'Save');
251 $tem->show('submit');
256 if($has_html_editors) {
257 $tem->show('html_editor_headers');
263 return $tem->get('form');
267 function view_html() {
273 function make_php() {
275 $tem->load('code/wfpl/metaform/template.php');
276 $tem->set('form_name', $GLOBALS['form_name']);
277 $fields = get_fields();
280 $always_field = find_always_field($fields);
281 $image_included_yet = false;
282 foreach($fields as $field) {
283 list($name, $type, $input, $format, $sql) = $field;
284 if($input != 'submit') {
285 $tem->set('format', $format);
286 $tem->set('name', $name);
287 $tem->set('db_field', ''); # we don't want to use the value from last time
289 if($db_fields != '') $db_fields .= ',';
291 if($php_fields != '') $php_fields .= ', ';
292 $php_fields .= '$' . $name;
294 if($input == 'image') {
295 $tem->show('image_upload');
296 $tem->show('image_db');
297 if(!$image_included_yet) {
298 $tem->show('image_include');
299 $tem->show('upload_max');
300 $tem->show('upload_settings');
301 $image_included_yet = true;
304 if($input == 'pulldown') {
305 $tem->show('pulldowns');
306 $tem->show('pulldown_format_extra');
308 $tem->show('formats');
310 $tem->show('tem_sets');
314 $tem->set('always_field', $always_field);
315 $tem->set('db_fields', $db_fields);
316 $tem->set('php_fields', $php_fields);
317 $tem->set('metaform_url', edit_url());
318 if($GLOBALS['opt_listing'] == 'Yes') {
319 $tem->show('opt_listing_1');
320 $tem->show('opt_listing_2');
321 $tem->show('opt_listing_4');
323 $tem->show('opt_listing_4_else');
325 if($GLOBALS['opt_db'] == 'Yes') {
326 $tem->show('opt_db_1');
327 $tem->show('opt_db_2');
328 $tem->show('opt_db_3');
329 $tem->show('opt_db_4');
330 $tem->show('opt_db_5');
332 if($GLOBALS['opt_email'] == 'Yes') {
333 $tem->show('opt_email_1');
334 $tem->show('opt_email_2');
336 if($GLOBALS['opt_http_pass'] == 'Yes') {
337 $tem->show('opt_http_pass_1');
338 $tem->show('opt_http_pass_2');
343 # make a URL for the edit page with all the fields filled in
344 function edit_url() {
346 $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
347 $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
348 $url = ereg_replace('/[a-z0-9_.]*\?', '/?', $url);
349 $url = str_replace('jasonwoof.l', 'jasonwoof.com', $url); # so that code generated on Jason's home computer will display a publically accessible link.
353 function view_php() {
359 function make_email() {
361 $tem->load('code/wfpl/metaform/template.email.txt');
362 $tem->set('form_name', $GLOBALS['form_name']);
363 $fields = get_fields();
364 foreach($fields as $field) {
365 list($name, $type, $input, $format, $sql) = $field;
366 $tem->set('name', $name);
367 $tem->set('caption', $name); # fixme
368 if($type == 'textarea') {
369 $tem->show('multi_line');
370 } elseif($type == 'checkbox') {
371 $tem->show('checkbox');
373 $tem->show('normal');
375 $tem->show('fields');
380 function make_htaccess() {
382 $tem->set('form', $GLOBALS['form_name']);
383 return $tem->run('code/wfpl/metaform/htaccess');
386 function view_email() {
393 tem_load('code/wfpl/metaform/preview.html');
394 tem_set('form_name', $GLOBALS['form_name']);
395 tem_set('fields', $_REQUEST['fields']);
396 $preview_tem = new tem();
397 $preview_tem->load_str(make_html(false));
398 if($GLOBALS['opt_db'] == 'Yes') {
399 $preview_tem->show('new_msg');
401 $fields = get_fields();
402 foreach($fields as $field) {
403 list($name, $type, $input, $format, $sql) = $field;
404 if($type == 'pulldown') {
405 pulldown($name, array('option 1', 'option 2', 'option 3'));
408 $preview = $preview_tem->run();
410 $preview = ereg_replace('type="submit"', 'type="submit" disabled="disabled"', $preview);
411 tem_set('preview', $preview);
416 function download_tar() {
417 $name = $GLOBALS['form_name'];
419 ".htaccess" => make_htaccess(),
420 "run.php ->" => 'code/wfpl/run.php',
421 "style.css" => read_whole_file('code/wfpl/metaform/style.css'),
422 "$name.html" => make_html(),
423 "$name.php" => make_php());
424 if($GLOBALS['opt_db'] == 'Yes') {
425 $data["$name.sql"] = make_sql();
427 if($GLOBALS['opt_email'] == 'Yes') {
428 $data["$name.email.txt"] = make_email();
430 make_wfpl_tar($name, $data);