3 # Copyright (C) 2006 Jason Woofenden
5 # This file is part of wfpl.
7 # wfpl is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU Lesser General Public License as published by the Free
9 # Software Foundation; either version 2.1 of the License, or (at your option)
12 # wfpl is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
17 # You should have received a copy of the GNU Lesser General Public License
18 # along with wfpl; if not, write to the Free Software Foundation, Inc., 51
19 # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 # This file writes the code for you (sql, php, html, email) to handle a form.
24 require_once('code/wfpl/template.php');
25 require_once('code/wfpl/http.php');
26 require_once('code/wfpl/tar.php');
27 require_once('code/wfpl/format.php');
29 # see code/wfpl/metaform/template.html for the html templates for these elements
30 $GLOBALS['types'] = array(
31 # type input format sql
32 'varname' => array('textbox', 'varname', 'varchar(50)'),
33 'name' => array('textbox', 'oneline', 'varchar(200)'),
34 'textbox' => array('textbox', 'oneline', 'varchar(200)'),
35 'int' => array('textbox', 'int', 'int'),
36 'decimal' => array('textbox', 'decimal', 'decimal(12,12)'),
37 'bigint' => array('textbox', 'int', 'varchar(100)'), # up to 100 digits, stored as a string
38 'zip' => array('textbox', 'zip', 'varchar(20)'),
39 'email' => array('textbox', 'email', 'varchar(100)'),
40 'phone' => array('textbox', 'phone', 'varchar(32)'),
41 'state' => array('states', 'oneline', 'varchar(2)'),
42 'money' => array('textbox', 'money', 'varchar(32)'),
43 'date' => array('textbox', 'mdy_to_ymd', 'char(10)'),
44 'dollars' => array('textbox', 'dollars', 'varchar(32)'),
45 'url' => array('textbox', 'url', 'varchar(200)'),
46 'hidden' => array('hidden', 'unix', 'varchar(200)'),
47 'password' => array('password', 'oneline', 'varchar(200)'),
48 'textarea' => array('textarea', 'unix', 'text'),
49 'html' => array('html', 'unix', 'text'),
50 'pulldown' => array('pulldown', 'options', 'varchar(100)'),
51 'radio' => array('radio', 'oneline', 'varchar(200)'),
52 'leftcheck' => array('leftcheck', 'yesno', 'varchar(3)'),
53 'checkbox' => array('checkbox', 'yesno', 'varchar(3)'),
54 'yesno' => array('checkbox', 'yesno', 'varchar(3)'),
55 'delete' => array('checkbox', 'yesno', 'n/a'),
56 'image' => array('image', 'oneline', 'varchar(200)'),
57 'submit' => array('submit', 'oneline', 'n/a')
60 function list_available_types() {
62 foreach($GLOBALS['types'] as $key => $value) {
68 tem_set('available_types', $types);
73 if(isset($_REQUEST['form_name'])) {
74 $GLOBALS['form_name'] = ereg_replace('[^a-z0-9_-]', '', $_REQUEST['form_name']);
75 $GLOBALS['opt_email'] = format_yesno($_REQUEST['opt_email']);
76 tem_set('opt_email', $GLOBALS['opt_email']);
77 $GLOBALS['opt_db'] = format_yesno($_REQUEST['opt_db']);
78 tem_set('opt_db', $GLOBALS['opt_db']);
79 $GLOBALS['opt_listing'] = format_yesno($_REQUEST['opt_listing']);
80 tem_set('opt_listing', $GLOBALS['opt_listing']);
81 $GLOBALS['opt_http_pass'] = format_yesno($_REQUEST['opt_http_pass']);
82 tem_set('opt_http_pass', $GLOBALS['opt_http_pass']);
84 $GLOBALS['form_name'] = 'some_form';
87 if(isset($_REQUEST['fields'])) {
88 if(isset($_REQUEST['view_sql'])) {
91 } elseif(isset($_REQUEST['view_php'])) {
94 } elseif(isset($_REQUEST['view_html'])) {
97 } elseif(isset($_REQUEST['view_email'])) {
100 } elseif(isset($_REQUEST['download_tar'])) {
103 } elseif(isset($_REQUEST['preview'])) {
106 } elseif(isset($_REQUEST['edit'])) {
107 tem_set('fields', $_REQUEST['fields']);
108 tem_set('form_name', $GLOBALS['form_name']);
111 die("Sorry... couldn't tell which button you pressed");
117 list_available_types();
118 tem_output('code/wfpl/metaform/main.html');
122 function field_input($type) { return $GLOBALS['types'][$type][0]; }
123 function field_format($type) { return $GLOBALS['types'][$type][1]; }
124 function field_sql($type) { return $GLOBALS['types'][$type][2]; }
126 function get_fields() {
127 # no sense in doing all this so many times
128 if(isset($GLOBALS['gotten_fields'])) {
129 return $GLOBALS['gotten_fields'];
132 $fields_str = unix_newlines($_REQUEST['fields']);
133 $GLOBALS['gotten_fields'] = array();
134 $fields_str = rtrim($fields_str);
135 $fields = split("\n", $fields_str);
136 foreach($fields as $field) {
137 list($name, $type, $options) = split(' *', $field);
138 if($options) $options = split(',', $options);
139 if(!$type) $type = $name;
140 $input = field_input($type);
141 $format = field_format($type);
142 $sql = field_sql($type);
143 $GLOBALS['gotten_fields'][] = array($name, $type, $input, $format, $sql, $options);
145 return $GLOBALS['gotten_fields'];
148 # this one, that you're using to create forms
149 function set_form_action() {
150 $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
151 if($action == '') $action = './';
152 tem_set('form_action', $action);
155 # perfect HTTP headers for viewing created files
156 function view_headers() {
157 header('Content-type: text/plain');
163 function make_sql() {
165 $tem->load('code/wfpl/metaform/template.sql');
166 $tem->set('form_name', $GLOBALS['form_name']);
167 $fields = get_fields();
168 foreach($fields as $field) {
169 list($name, $type, $input, $format, $sql) = $field;
171 $tem->set('name', $name);
172 $tem->set('type', $sql);
174 $tem->set('default', '0');
176 $tem->set('default', '""');
185 function view_sql() {
190 # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
191 function find_always_field($fields) {
192 foreach($fields as $field) {
193 list($name, $type, $input, $format, $sql) = $field;
194 if($input != 'submit' && $input != 'checkbox' && $input != 'radio') {
204 # pass false if you want to exclude the <head> and <body> tag etc.
205 function make_html($whole_file = true) {
206 $uploads_output_already = false;
207 $has_html_editors = false;
209 $tem->load('code/wfpl/metaform/template.html');
210 $tem->set('form_name', $GLOBALS['form_name']);
211 $fields = get_fields();
212 $tem->set('always_field', find_always_field($fields));
213 foreach($fields as $field) {
214 list($name, $type, $input, $format, $sql) = $field;
215 $tem->set('name', $name);
216 $tem->set('caption', format_caption($name));
218 if($input != 'hidden') {
221 if($input == 'image' && !$uploads_output_already) {
222 $tem->sub('uploads');
223 $tem->set('enctype_attr', '" enctype="multipart/form-data');
224 $uploads_output_already = true;
225 } elseif($input == 'html') {
226 $has_html_editors = true;
227 $tem->set('html_field_name', $name);
228 $tem->sub('replace_textarea');
232 if($GLOBALS['opt_db'] == 'Yes') {
233 $tem->sub('opt_db_1');
234 $tem->sub('opt_db_2');
236 $tem->sub('opt_db_1_else');
239 if($GLOBALS['opt_listing'] == 'Yes') {
240 $tem->sub('opt_listing_1');
242 $tem->sub('opt_listing_1_else');
245 if($GLOBALS['opt_email'] == 'Yes' && $GLOBALS['opt_db'] != 'Yes') {
246 $tem->set('name', 'send');
247 $tem->set('caption', 'Send');
249 $tem->set('name', 'save');
250 $tem->set('caption', 'Save');
257 if($has_html_editors) {
258 $tem->sub('html_editor_headers');
264 return $tem->get('form');
268 function view_html() {
274 function make_php() {
276 $tem->load('code/wfpl/metaform/template.php');
277 $tem->set('form_name', $GLOBALS['form_name']);
278 $fields = get_fields();
281 $always_field = find_always_field($fields);
282 $image_included_yet = false;
283 foreach($fields as $field) {
284 list($name, $type, $input, $format, $sql) = $field;
285 if($input != 'submit') {
286 $tem->set('format', $format);
287 $tem->set('name', $name);
288 $tem->set('db_field', ''); # we don't want to use the value from last time
290 if($db_fields != '') $db_fields .= ',';
292 if($php_fields != '') $php_fields .= ', ';
293 $php_fields .= '$' . $name;
295 if($input == 'image') {
296 $tem->sub('image_upload');
297 $tem->sub('image_db');
298 if(!$image_included_yet) {
299 $tem->sub('image_include');
300 $tem->sub('upload_max');
301 $tem->sub('upload_settings');
302 $image_included_yet = true;
305 if($input == 'pulldown') {
306 $tem->sub('pulldowns');
307 $tem->sub('pulldown_format_extra');
309 $tem->sub('formats');
311 $tem->sub('tem_sets');
315 $tem->set('always_field', $always_field);
316 $tem->set('db_fields', $db_fields);
317 $tem->set('php_fields', $php_fields);
318 $tem->set('metaform_url', edit_url());
319 if($GLOBALS['opt_listing'] == 'Yes') {
320 $tem->sub('opt_listing_1');
321 $tem->sub('opt_listing_2');
322 $tem->sub('opt_listing_3');
323 $tem->sub('opt_listing_4');
325 $tem->sub('opt_listing_3_else');
326 $tem->sub('opt_listing_4_else');
328 if($GLOBALS['opt_db'] == 'Yes') {
329 $tem->sub('opt_db_1');
330 $tem->sub('opt_db_2');
331 $tem->sub('opt_db_3');
332 $tem->sub('opt_db_4');
333 $tem->sub('opt_db_5');
335 if($GLOBALS['opt_email'] == 'Yes') {
336 $tem->sub('opt_email_1');
337 $tem->sub('opt_email_2');
339 if($GLOBALS['opt_http_pass'] == 'Yes') {
340 $tem->sub('opt_http_pass_1');
341 $tem->sub('opt_http_pass_2');
346 # make a URL for the edit page with all the fields filled in
347 function edit_url() {
349 $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
350 $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
351 $url = ereg_replace('/[a-z0-9_.]*\?', '/?', $url);
352 $url = str_replace('jasonwoof.l', 'jasonwoof.com', $url); # so that code generated on Jason's home computer will display a publically accessible link.
356 function view_php() {
362 function make_email() {
364 $tem->load('code/wfpl/metaform/template.email.txt');
365 $tem->set('form_name', $GLOBALS['form_name']);
366 $fields = get_fields();
367 foreach($fields as $field) {
368 list($name, $type, $input, $format, $sql) = $field;
369 $tem->set('name', $name);
370 $tem->set('caption', $name); # fixme
371 if($type == 'textarea') {
372 $tem->sub('multi_line');
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->sub('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 tem_set('preview', $preview);
415 function download_tar() {
416 $name = $GLOBALS['form_name'];
418 ".htaccess" => make_htaccess(),
419 "run.php ->" => 'code/wfpl/run.php',
420 "style.css" => read_whole_file('code/wfpl/metaform/style.css'),
421 "$name.html" => make_html(),
422 "$name.php" => make_php());
423 if($GLOBALS['opt_db'] == 'Yes') {
424 $data["$name.sql"] = make_sql();
426 if($GLOBALS['opt_email'] == 'Yes') {
427 $data["$name.email.txt"] = make_email();
429 make_wfpl_tar($name, $data);