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', 'int'),
47 'radio' => array('radio', 'oneline', 'varchar(200)'),
48 'checkbox' => array('checkbox', 'yesno', 'varchar(3)'),
49 'yesno' => array('checkbox', 'yesno', 'varchar(3)'),
50 'delete' => array('checkbox', 'yesno', 'n/a'),
51 'image' => array('image', 'oneline', 'varchar(200)'),
52 'submit' => array('submit', 'oneline', 'n/a')
55 function list_available_types() {
57 foreach($GLOBALS['types'] as $key => $value) {
63 tem_set('available_types', $types);
68 if(isset($_REQUEST['form_name'])) {
69 $GLOBALS['form_name'] = ereg_replace('[^a-z0-9_-]', '', $_REQUEST['form_name']);
70 $GLOBALS['opt_email'] = format_yesno($_REQUEST['opt_email']);
71 tem_set('opt_email', $GLOBALS['opt_email']);
72 $GLOBALS['opt_db'] = format_yesno($_REQUEST['opt_db']);
73 tem_set('opt_db', $GLOBALS['opt_db']);
74 $GLOBALS['opt_http_pass'] = format_yesno($_REQUEST['opt_http_pass']);
75 tem_set('opt_http_pass', $GLOBALS['opt_http_pass']);
77 $GLOBALS['form_name'] = 'some_form';
80 if(isset($_REQUEST['fields'])) {
81 if(isset($_REQUEST['view_sql'])) {
84 } elseif(isset($_REQUEST['view_php'])) {
87 } elseif(isset($_REQUEST['view_html'])) {
90 } elseif(isset($_REQUEST['view_email'])) {
93 } elseif(isset($_REQUEST['download_tar'])) {
96 } elseif(isset($_REQUEST['preview'])) {
99 } elseif(isset($_REQUEST['edit'])) {
100 tem_set('fields', $_REQUEST['fields']);
101 tem_set('form_name', $GLOBALS['form_name']);
104 die("Sorry... couldn't tell which button you pressed");
110 list_available_types();
111 tem_output('code/wfpl/metaform/main.html');
115 function field_input($type) { return $GLOBALS['types'][$type][0]; }
116 function field_format($type) { return $GLOBALS['types'][$type][1]; }
117 function field_sql($type) { return $GLOBALS['types'][$type][2]; }
119 function get_fields() {
120 $fields_str = unix_newlines($_REQUEST['fields']);
122 $fields_str = rtrim($fields_str);
123 $fields = split("\n", $fields_str);
124 foreach($fields as $field) {
125 list($name, $type, $options) = split(' *', $field);
126 if($options) $options = split(',', $options);
127 if(!$type) $type = $name;
128 $input = field_input($type);
129 $format = field_format($type);
130 $sql = field_sql($type);
131 $ret[] = array($name, $type, $input, $format, $sql, $options);
136 # this one, that you're using to create forms
137 function set_form_action() {
138 $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
139 if($action == '') $action = './';
140 tem_set('form_action', $action);
143 # perfect HTTP headers for viewing created files
144 function view_headers() {
145 header('Content-type: text/plain');
151 function make_sql() {
153 $tem->load('code/wfpl/metaform/template.sql');
154 $tem->set('form_name', $GLOBALS['form_name']);
155 $fields = get_fields();
156 foreach($fields as $field) {
157 list($name, $type, $input, $format, $sql) = $field;
159 $tem->set('name', $name);
160 $tem->set('type', $sql);
162 $tem->set('default', '0');
164 $tem->set('default', '""');
173 function view_sql() {
179 # pass false if you want to exclude the <head> and <body> tag etc.
180 function make_html($whole_file = true) {
181 $uploads_output_already = false;
182 $has_html_editors = false;
184 $tem->load('code/wfpl/metaform/template.html');
185 $tem->set('form_name', $GLOBALS['form_name']);
186 $fields = get_fields();
187 foreach($fields as $field) {
188 list($name, $type, $input, $format, $sql) = $field;
189 $tem->set('name', $name);
190 $tem->set('caption', format_caption($name));
192 if($input != 'hidden') {
195 if($input == 'image' && !$uploads_output_already) {
196 $tem->sub('uploads');
197 $tem->set('enctype_attr', '" enctype="multipart/form-data');
198 $uploads_output_already = true;
199 } elseif($input == 'html') {
200 $has_html_editors = true;
201 $tem->set('html_field_name', $name);
202 $tem->sub('replace_textarea');
206 if($GLOBALS['opt_db'] == 'Yes') {
207 $tem->sub('opt_db_1');
208 $tem->sub('opt_db_2');
210 $tem->sub('opt_db_1_else');
212 $tem->set('name', 'save');
213 $tem->set('caption', 'Save');
218 if($has_html_editors) {
219 $tem->sub('html_editor_headers');
225 return $tem->get('form');
229 function view_html() {
235 function make_php() {
237 $tem->load('code/wfpl/metaform/template.php');
238 $tem->set('form_name', $GLOBALS['form_name']);
239 $fields = get_fields();
242 $always_field = false;
243 $image_included_yet = false;
244 foreach($fields as $field) {
245 list($name, $type, $input, $format, $sql) = $field;
246 if($input != 'submit') {
247 $tem->set('format', $format);
248 $tem->set('name', $name);
249 $tem->set('db_field', ''); # we don't want to use the value from last time
251 if($db_fields != '') $db_fields .= ',';
253 if($php_fields != '') $php_fields .= ', ';
254 $php_fields .= '$' . $name;
256 if($input == 'image') {
257 $tem->sub('image_upload');
258 $tem->sub('image_db');
259 if(!$image_included_yet) {
260 $tem->sub('image_include');
261 $tem->sub('upload_max');
262 $tem->sub('upload_settings');
263 $image_included_yet = true;
266 $tem->sub('formats');
268 $tem->sub('tem_sets');
269 if(!$always_field and $input != 'checkbox' and $input != 'radio') {
270 $always_field = $name;
274 # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
275 $tem->set('always_field', $always_field);
276 $tem->set('db_fields', $db_fields);
277 $tem->set('php_fields', $php_fields);
278 $tem->set('metaform_url', edit_url());
279 if($GLOBALS['opt_db'] == 'Yes') {
280 $tem->sub('opt_db_1');
281 $tem->sub('opt_db_2');
282 $tem->sub('opt_db_3');
283 $tem->sub('opt_db_4');
284 $tem->sub('opt_db_5');
286 if($GLOBALS['opt_email'] == 'Yes') {
287 $tem->sub('opt_email_1');
288 $tem->sub('opt_email_2');
290 if($GLOBALS['opt_http_pass'] == 'Yes') {
291 $tem->sub('opt_http_pass_1');
292 $tem->sub('opt_http_pass_2');
297 # make a URL for the edit page with all the fields filled in
298 function edit_url() {
300 $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
301 $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
302 $url = ereg_replace('/[a-z0-9_.]*\?', '/?', $url);
303 $url = str_replace('jasonwoof.l', 'jasonwoof.com', $url); # so that code generated on Jason's home computer will display a publically accessible link.
307 function view_php() {
313 function make_email() {
315 $tem->load('code/wfpl/metaform/template.email.txt');
316 $tem->set('form_name', $GLOBALS['form_name']);
317 $fields = get_fields();
318 foreach($fields as $field) {
319 list($name, $type, $input, $format, $sql) = $field;
320 $tem->set('name', $name);
321 $tem->set('caption', $name); # fixme
322 if($type == 'textarea') {
323 $tem->sub('multi_line');
331 function make_htaccess() {
333 $tem->set('form', $GLOBALS['form_name']);
334 return $tem->run('code/wfpl/metaform/template.htaccess');
337 function view_email() {
344 tem_load('code/wfpl/metaform/preview.html');
345 tem_set('form_name', $GLOBALS['form_name']);
346 tem_set('fields', $_REQUEST['fields']);
347 $preview_tem = new tem();
348 $preview_tem->load_str(make_html(false));
349 if($GLOBALS['opt_db'] == 'Yes') {
350 $preview_tem->sub('new_msg');
352 $preview = $preview_tem->run();
354 tem_set('preview', $preview);
359 function download_tar() {
360 $name = $GLOBALS['form_name'];
362 ".htaccess" => make_htaccess(),
363 "run.php ->" => 'code/wfpl/run.php',
364 "$name.html" => make_html(),
365 "$name.php" => make_php());
366 if($GLOBALS['opt_db'] == 'Yes') {
367 $data["$name.sql"] = make_sql();
369 if($GLOBALS['opt_email'] == 'Yes') {
370 $data["$name.email.txt"] = make_email();
372 make_wfpl_tar($name, $data);