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 'name' => array('textbox', 'oneline', 'varchar(200)'),
33 'textbox' => array('textbox', 'oneline', 'varchar(200)'),
34 'int' => array('textbox', 'int', 'int'),
35 'bigint' => array('textbox', 'int', 'varchar(100)'), # up to 100 digits, stored as a string
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 'dollars' => array('textbox', 'dollars', 'varchar(32)'),
41 'url' => array('textbox', 'url', 'varchar(200)'),
42 'hidden' => array('hidden', 'unix', 'varchar(200)'),
43 'password' => array('password', 'oneline', 'varchar(200)'),
44 'textarea' => array('textarea', 'unix', 'text'),
45 'html' => array('html', 'unix', 'text'),
46 'pulldown' => array('pulldown', 'options', 'varchar(100)'),
47 'radio' => array('radio', 'oneline', 'varchar(200)'),
48 'leftcheck' => array('leftcheck', 'yesno', 'varchar(3)'),
49 'checkbox' => array('checkbox', 'yesno', 'varchar(3)'),
50 'yesno' => array('checkbox', 'yesno', 'varchar(3)'),
51 'delete' => array('checkbox', 'yesno', 'n/a'),
52 'image' => array('image', 'oneline', 'varchar(200)'),
53 'submit' => array('submit', 'oneline', 'n/a')
56 function list_available_types() {
58 foreach($GLOBALS['types'] as $key => $value) {
64 tem_set('available_types', $types);
69 if(isset($_REQUEST['form_name'])) {
70 $GLOBALS['form_name'] = ereg_replace('[^a-z0-9_-]', '', $_REQUEST['form_name']);
71 $GLOBALS['opt_email'] = format_yesno($_REQUEST['opt_email']);
72 tem_set('opt_email', $GLOBALS['opt_email']);
73 $GLOBALS['opt_db'] = format_yesno($_REQUEST['opt_db']);
74 tem_set('opt_db', $GLOBALS['opt_db']);
75 $GLOBALS['opt_http_pass'] = format_yesno($_REQUEST['opt_http_pass']);
76 tem_set('opt_http_pass', $GLOBALS['opt_http_pass']);
78 $GLOBALS['form_name'] = 'some_form';
81 if(isset($_REQUEST['fields'])) {
82 if(isset($_REQUEST['view_sql'])) {
85 } elseif(isset($_REQUEST['view_php'])) {
88 } elseif(isset($_REQUEST['view_html'])) {
91 } elseif(isset($_REQUEST['view_email'])) {
94 } elseif(isset($_REQUEST['download_tar'])) {
97 } elseif(isset($_REQUEST['preview'])) {
100 } elseif(isset($_REQUEST['edit'])) {
101 tem_set('fields', $_REQUEST['fields']);
102 tem_set('form_name', $GLOBALS['form_name']);
105 die("Sorry... couldn't tell which button you pressed");
111 list_available_types();
112 tem_output('code/wfpl/metaform/main.html');
116 function field_input($type) { return $GLOBALS['types'][$type][0]; }
117 function field_format($type) { return $GLOBALS['types'][$type][1]; }
118 function field_sql($type) { return $GLOBALS['types'][$type][2]; }
120 function get_fields() {
121 # no sense in doing all this so many times
122 if(isset($GLOBALS['gotten_fields'])) {
123 return $GLOBALS['gotten_fields'];
126 $fields_str = unix_newlines($_REQUEST['fields']);
127 $GLOBALS['gotten_fields'] = array();
128 $fields_str = rtrim($fields_str);
129 $fields = split("\n", $fields_str);
130 foreach($fields as $field) {
131 list($name, $type, $options) = split(' *', $field);
132 if($options) $options = split(',', $options);
133 if(!$type) $type = $name;
134 $input = field_input($type);
135 $format = field_format($type);
136 $sql = field_sql($type);
137 $GLOBALS['gotten_fields'][] = array($name, $type, $input, $format, $sql, $options);
139 return $GLOBALS['gotten_fields'];
142 # this one, that you're using to create forms
143 function set_form_action() {
144 $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
145 if($action == '') $action = './';
146 tem_set('form_action', $action);
149 # perfect HTTP headers for viewing created files
150 function view_headers() {
151 header('Content-type: text/plain');
157 function make_sql() {
159 $tem->load('code/wfpl/metaform/template.sql');
160 $tem->set('form_name', $GLOBALS['form_name']);
161 $fields = get_fields();
162 foreach($fields as $field) {
163 list($name, $type, $input, $format, $sql) = $field;
165 $tem->set('name', $name);
166 $tem->set('type', $sql);
168 $tem->set('default', '0');
170 $tem->set('default', '""');
179 function view_sql() {
185 # pass false if you want to exclude the <head> and <body> tag etc.
186 function make_html($whole_file = true) {
187 $uploads_output_already = false;
188 $has_html_editors = false;
190 $tem->load('code/wfpl/metaform/template.html');
191 $tem->set('form_name', $GLOBALS['form_name']);
192 $fields = get_fields();
193 foreach($fields as $field) {
194 list($name, $type, $input, $format, $sql) = $field;
195 $tem->set('name', $name);
196 $tem->set('caption', format_caption($name));
198 if($input != 'hidden') {
201 if($input == 'image' && !$uploads_output_already) {
202 $tem->sub('uploads');
203 $tem->set('enctype_attr', '" enctype="multipart/form-data');
204 $uploads_output_already = true;
205 } elseif($input == 'html') {
206 $has_html_editors = true;
207 $tem->set('html_field_name', $name);
208 $tem->sub('replace_textarea');
212 if($GLOBALS['opt_db'] == 'Yes') {
213 $tem->sub('opt_db_1');
214 $tem->sub('opt_db_2');
216 $tem->sub('opt_db_1_else');
219 if($GLOBALS['opt_email'] == 'Yes' && $GLOBALS['opt_db'] != 'Yes') {
220 $tem->set('name', 'send');
221 $tem->set('caption', 'Send');
223 $tem->set('name', 'save');
224 $tem->set('caption', 'Save');
231 if($has_html_editors) {
232 $tem->sub('html_editor_headers');
238 return $tem->get('form');
242 function view_html() {
248 function make_php() {
250 $tem->load('code/wfpl/metaform/template.php');
251 $tem->set('form_name', $GLOBALS['form_name']);
252 $fields = get_fields();
255 $always_field = false;
256 $image_included_yet = false;
257 foreach($fields as $field) {
258 list($name, $type, $input, $format, $sql) = $field;
259 if($input != 'submit') {
260 $tem->set('format', $format);
261 $tem->set('name', $name);
262 $tem->set('db_field', ''); # we don't want to use the value from last time
264 if($db_fields != '') $db_fields .= ',';
266 if($php_fields != '') $php_fields .= ', ';
267 $php_fields .= '$' . $name;
269 if($input == 'image') {
270 $tem->sub('image_upload');
271 $tem->sub('image_db');
272 if(!$image_included_yet) {
273 $tem->sub('image_include');
274 $tem->sub('upload_max');
275 $tem->sub('upload_settings');
276 $image_included_yet = true;
279 if($input == 'pulldown') {
280 $tem->sub('pulldowns');
281 $tem->sub('pulldown_format_extra');
283 $tem->sub('formats');
285 $tem->sub('tem_sets');
286 if(!$always_field and $input != 'checkbox' and $input != 'radio') {
287 $always_field = $name;
291 # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
292 $tem->set('always_field', $always_field);
293 $tem->set('db_fields', $db_fields);
294 $tem->set('php_fields', $php_fields);
295 $tem->set('metaform_url', edit_url());
296 if($GLOBALS['opt_db'] == 'Yes') {
297 $tem->sub('opt_db_1');
298 $tem->sub('opt_db_2');
299 $tem->sub('opt_db_3');
300 $tem->sub('opt_db_4');
301 $tem->sub('opt_db_5');
303 if($GLOBALS['opt_email'] == 'Yes') {
304 $tem->sub('opt_email_1');
305 $tem->sub('opt_email_2');
307 if($GLOBALS['opt_http_pass'] == 'Yes') {
308 $tem->sub('opt_http_pass_1');
309 $tem->sub('opt_http_pass_2');
314 # make a URL for the edit page with all the fields filled in
315 function edit_url() {
317 $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
318 $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
319 $url = ereg_replace('/[a-z0-9_.]*\?', '/?', $url);
320 $url = str_replace('jasonwoof.l', 'jasonwoof.com', $url); # so that code generated on Jason's home computer will display a publically accessible link.
324 function view_php() {
330 function make_email() {
332 $tem->load('code/wfpl/metaform/template.email.txt');
333 $tem->set('form_name', $GLOBALS['form_name']);
334 $fields = get_fields();
335 foreach($fields as $field) {
336 list($name, $type, $input, $format, $sql) = $field;
337 $tem->set('name', $name);
338 $tem->set('caption', $name); # fixme
339 if($type == 'textarea') {
340 $tem->sub('multi_line');
348 function make_htaccess() {
350 $tem->set('form', $GLOBALS['form_name']);
351 return $tem->run('code/wfpl/metaform/htaccess');
354 function view_email() {
361 tem_load('code/wfpl/metaform/preview.html');
362 tem_set('form_name', $GLOBALS['form_name']);
363 tem_set('fields', $_REQUEST['fields']);
364 $preview_tem = new tem();
365 $preview_tem->load_str(make_html(false));
366 if($GLOBALS['opt_db'] == 'Yes') {
367 $preview_tem->sub('new_msg');
369 $fields = get_fields();
370 foreach($fields as $field) {
371 list($name, $type, $input, $format, $sql) = $field;
372 if($type == 'pulldown') {
373 pulldown($name, array('option 1', 'option 2', 'option 3'));
376 $preview = $preview_tem->run();
378 tem_set('preview', $preview);
383 function download_tar() {
384 $name = $GLOBALS['form_name'];
386 ".htaccess" => make_htaccess(),
387 "run.php ->" => 'code/wfpl/run.php',
388 "style.css" => read_whole_file('code/wfpl/metaform/style.css'),
389 "$name.html" => make_html(),
390 "$name.php" => make_php());
391 if($GLOBALS['opt_db'] == 'Yes') {
392 $data["$name.sql"] = make_sql();
394 if($GLOBALS['opt_email'] == 'Yes') {
395 $data["$name.email.txt"] = make_email();
397 make_wfpl_tar($name, $data);