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 'dollars' => array('textbox', 'dollars', 'varchar(32)'),
44 'url' => array('textbox', 'url', 'varchar(200)'),
45 'hidden' => array('hidden', 'unix', 'varchar(200)'),
46 'password' => array('password', 'oneline', 'varchar(200)'),
47 'textarea' => array('textarea', 'unix', 'text'),
48 'html' => array('html', 'unix', 'text'),
49 'pulldown' => array('pulldown', 'options', 'varchar(100)'),
50 'radio' => array('radio', 'oneline', 'varchar(200)'),
51 'leftcheck' => array('leftcheck', 'yesno', 'varchar(3)'),
52 'checkbox' => array('checkbox', 'yesno', 'varchar(3)'),
53 'yesno' => array('checkbox', 'yesno', 'varchar(3)'),
54 'delete' => array('checkbox', 'yesno', 'n/a'),
55 'image' => array('image', 'oneline', 'varchar(200)'),
56 'submit' => array('submit', 'oneline', 'n/a')
59 function list_available_types() {
61 foreach($GLOBALS['types'] as $key => $value) {
67 tem_set('available_types', $types);
72 if(isset($_REQUEST['form_name'])) {
73 $GLOBALS['form_name'] = ereg_replace('[^a-z0-9_-]', '', $_REQUEST['form_name']);
74 $GLOBALS['opt_email'] = format_yesno($_REQUEST['opt_email']);
75 tem_set('opt_email', $GLOBALS['opt_email']);
76 $GLOBALS['opt_db'] = format_yesno($_REQUEST['opt_db']);
77 tem_set('opt_db', $GLOBALS['opt_db']);
78 $GLOBALS['opt_listing'] = format_yesno($_REQUEST['opt_listing']);
79 tem_set('opt_listing', $GLOBALS['opt_listing']);
80 $GLOBALS['opt_http_pass'] = format_yesno($_REQUEST['opt_http_pass']);
81 tem_set('opt_http_pass', $GLOBALS['opt_http_pass']);
83 $GLOBALS['form_name'] = 'some_form';
86 if(isset($_REQUEST['fields'])) {
87 if(isset($_REQUEST['view_sql'])) {
90 } elseif(isset($_REQUEST['view_php'])) {
93 } elseif(isset($_REQUEST['view_html'])) {
96 } elseif(isset($_REQUEST['view_email'])) {
99 } elseif(isset($_REQUEST['download_tar'])) {
102 } elseif(isset($_REQUEST['preview'])) {
105 } elseif(isset($_REQUEST['edit'])) {
106 tem_set('fields', $_REQUEST['fields']);
107 tem_set('form_name', $GLOBALS['form_name']);
110 die("Sorry... couldn't tell which button you pressed");
116 list_available_types();
117 tem_output('code/wfpl/metaform/main.html');
121 function field_input($type) { return $GLOBALS['types'][$type][0]; }
122 function field_format($type) { return $GLOBALS['types'][$type][1]; }
123 function field_sql($type) { return $GLOBALS['types'][$type][2]; }
125 function get_fields() {
126 # no sense in doing all this so many times
127 if(isset($GLOBALS['gotten_fields'])) {
128 return $GLOBALS['gotten_fields'];
131 $fields_str = unix_newlines($_REQUEST['fields']);
132 $GLOBALS['gotten_fields'] = array();
133 $fields_str = rtrim($fields_str);
134 $fields = split("\n", $fields_str);
135 foreach($fields as $field) {
136 list($name, $type, $options) = split(' *', $field);
137 if($options) $options = split(',', $options);
138 if(!$type) $type = $name;
139 $input = field_input($type);
140 $format = field_format($type);
141 $sql = field_sql($type);
142 $GLOBALS['gotten_fields'][] = array($name, $type, $input, $format, $sql, $options);
144 return $GLOBALS['gotten_fields'];
147 # this one, that you're using to create forms
148 function set_form_action() {
149 $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
150 if($action == '') $action = './';
151 tem_set('form_action', $action);
154 # perfect HTTP headers for viewing created files
155 function view_headers() {
156 header('Content-type: text/plain');
162 function make_sql() {
164 $tem->load('code/wfpl/metaform/template.sql');
165 $tem->set('form_name', $GLOBALS['form_name']);
166 $fields = get_fields();
167 foreach($fields as $field) {
168 list($name, $type, $input, $format, $sql) = $field;
170 $tem->set('name', $name);
171 $tem->set('type', $sql);
173 $tem->set('default', '0');
175 $tem->set('default', '""');
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->sub('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->sub('replace_textarea');
231 if($GLOBALS['opt_db'] == 'Yes') {
232 $tem->sub('opt_db_1');
233 $tem->sub('opt_db_2');
235 $tem->sub('opt_db_1_else');
238 if($GLOBALS['opt_listing'] == 'Yes') {
239 $tem->sub('opt_listing_1');
241 $tem->sub('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');
256 if($has_html_editors) {
257 $tem->sub('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->sub('image_upload');
296 $tem->sub('image_db');
297 if(!$image_included_yet) {
298 $tem->sub('image_include');
299 $tem->sub('upload_max');
300 $tem->sub('upload_settings');
301 $image_included_yet = true;
304 if($input == 'pulldown') {
305 $tem->sub('pulldowns');
306 $tem->sub('pulldown_format_extra');
308 $tem->sub('formats');
310 $tem->sub('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->sub('opt_listing_1');
320 $tem->sub('opt_listing_2');
321 $tem->sub('opt_listing_3');
322 $tem->sub('opt_listing_4');
324 $tem->sub('opt_listing_3_else');
325 $tem->sub('opt_listing_4_else');
327 if($GLOBALS['opt_db'] == 'Yes') {
328 $tem->sub('opt_db_1');
329 $tem->sub('opt_db_2');
330 $tem->sub('opt_db_3');
331 $tem->sub('opt_db_4');
332 $tem->sub('opt_db_5');
334 if($GLOBALS['opt_email'] == 'Yes') {
335 $tem->sub('opt_email_1');
336 $tem->sub('opt_email_2');
338 if($GLOBALS['opt_http_pass'] == 'Yes') {
339 $tem->sub('opt_http_pass_1');
340 $tem->sub('opt_http_pass_2');
345 # make a URL for the edit page with all the fields filled in
346 function edit_url() {
348 $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
349 $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
350 $url = ereg_replace('/[a-z0-9_.]*\?', '/?', $url);
351 $url = str_replace('jasonwoof.l', 'jasonwoof.com', $url); # so that code generated on Jason's home computer will display a publically accessible link.
355 function view_php() {
361 function make_email() {
363 $tem->load('code/wfpl/metaform/template.email.txt');
364 $tem->set('form_name', $GLOBALS['form_name']);
365 $fields = get_fields();
366 foreach($fields as $field) {
367 list($name, $type, $input, $format, $sql) = $field;
368 $tem->set('name', $name);
369 $tem->set('caption', $name); # fixme
370 if($type == 'textarea') {
371 $tem->sub('multi_line');
379 function make_htaccess() {
381 $tem->set('form', $GLOBALS['form_name']);
382 return $tem->run('code/wfpl/metaform/htaccess');
385 function view_email() {
392 tem_load('code/wfpl/metaform/preview.html');
393 tem_set('form_name', $GLOBALS['form_name']);
394 tem_set('fields', $_REQUEST['fields']);
395 $preview_tem = new tem();
396 $preview_tem->load_str(make_html(false));
397 if($GLOBALS['opt_db'] == 'Yes') {
398 $preview_tem->sub('new_msg');
400 $fields = get_fields();
401 foreach($fields as $field) {
402 list($name, $type, $input, $format, $sql) = $field;
403 if($type == 'pulldown') {
404 pulldown($name, array('option 1', 'option 2', 'option 3'));
407 $preview = $preview_tem->run();
409 tem_set('preview', $preview);
414 function download_tar() {
415 $name = $GLOBALS['form_name'];
417 ".htaccess" => make_htaccess(),
418 "run.php ->" => 'code/wfpl/run.php',
419 "style.css" => read_whole_file('code/wfpl/metaform/style.css'),
420 "$name.html" => make_html(),
421 "$name.php" => make_php());
422 if($GLOBALS['opt_db'] == 'Yes') {
423 $data["$name.sql"] = make_sql();
425 if($GLOBALS['opt_email'] == 'Yes') {
426 $data["$name.email.txt"] = make_email();
428 make_wfpl_tar($name, $data);