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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2, or (at your option)
12 # wfpl is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with wfpl; see the file COPYING. If not, write to the
19 # Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23 # This file writes the code for you (sql, php, html, email) to handle a form.
25 require_once('code/wfpl/template.php');
26 require_once('code/wfpl/tar.php');
28 # see code/wfpl/metaform/template.html for the html templates for these elements
29 $GLOBALS['types'] = array(
30 # type input format sql
31 'name' => array('textbox', 'oneline', 'varchar(200)'),
32 'textbox' => array('textbox', 'oneline', 'varchar(200)'),
33 'int' => array('textbox', 'int', 'varchar(100)'),
34 'email' => array('textbox', 'email', 'varchar(100)'),
35 'phone' => array('textbox', 'phone', 'varchar(32)'),
36 'money' => array('textbox', 'money', 'varchar(32)'),
37 'dollars' => array('textbox', 'dollars', 'varchar(32)'),
38 'url' => array('textbox', 'url', 'varchar(200)'),
39 'hidden' => array('hidden', 'unix', 'varchar(200)'),
40 'password' => array('password', 'oneline', 'varchar(200)'),
41 'textarea' => array('textarea', 'unix', 'text'),
42 'pulldown' => array('pulldown', 'options', 'int'),
43 'checkbox' => array('checkbox', 'yesno', 'varchar(3)'),
44 'yesno' => array('checkbox', 'yesno', 'varchar(3)'),
45 'delete' => array('checkbox', 'yesno', 'n/a'),
46 'image' => array('image', 'oneline', 'varchar(200)'),
47 'submit' => array('submit', 'oneline', 'n/a')
50 if(isset($_REQUEST['form_name'])) {
51 $GLOBALS['form_name'] = ereg_replace('[^a-z0-9_-]', '', $_REQUEST['form_name']);
53 $GLOBALS['form_name'] = 'some_form';
56 if(isset($_REQUEST['fields'])) {
57 if(isset($_REQUEST['view_sql'])) {
60 } elseif(isset($_REQUEST['view_php'])) {
63 } elseif(isset($_REQUEST['view_template'])) {
66 } elseif(isset($_REQUEST['view_email'])) {
69 } elseif(isset($_REQUEST['download_tar'])) {
72 } elseif(isset($_REQUEST['preview'])) {
75 } elseif(isset($_REQUEST['edit'])) {
76 tem_set('fields', $_REQUEST['fields']);
77 tem_set('form_name', $GLOBALS['form_name']);
80 die("Sorry... couldn't tell which button you pressed");
85 tem_output('code/wfpl/metaform/main.html');
89 function field_input($type) { return $GLOBALS['types'][$type][0]; }
90 function field_format($type) { return $GLOBALS['types'][$type][1]; }
91 function field_sql($type) { return $GLOBALS['types'][$type][2]; }
93 function get_fields() {
94 $fields_str = unix_newlines($_REQUEST['fields']);
96 $fields_str = rtrim($fields_str);
97 $fields = split("\n", $fields_str);
98 foreach($fields as $field) {
99 list($name, $type, $options) = split(' *', $field);
100 if($options) $options = split(',', $options);
101 if(!$type) $type = $name;
102 $input = field_input($type);
103 $format = field_format($type);
104 $sql = field_sql($type);
105 $ret[] = array($name, $type, $input, $format, $sql, $options);
110 # this one, that you're using to create forms
111 function set_form_action() {
112 $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
113 if($action == '') $action = './';
114 tem_set('form_action', $action);
117 # perfect HTTP headers for viewing created files
118 function view_headers() {
119 header('Content-type: text/plain');
125 function make_sql() {
127 $tem->load('code/wfpl/metaform/template.sql');
128 $tem->set('form_name', $GLOBALS['form_name']);
129 $fields = get_fields();
130 foreach($fields as $field) {
131 list($name, $type, $input, $format, $sql) = $field;
133 $tem->set('name', $name);
134 $tem->set('type', $sql);
136 $tem->set('default', '0');
138 $tem->set('default', '""');
147 function view_sql() {
153 # pass false if you want to exclude the <head> and <body> tag etc.
154 function make_template($whole_file = true) {
155 $uploads_output_already = false;
157 $tem->load('code/wfpl/metaform/template.html');
158 $tem->set('form_name', $GLOBALS['form_name']);
159 $fields = get_fields();
160 foreach($fields as $field) {
161 list($name, $type, $input, $format, $sql) = $field;
162 $tem->set('name', $name);
163 $tem->set('caption', $name); # fixme
165 if($input != 'hidden') {
168 if($input == 'image' && !$uploads_output_already) {
169 $tem->sub('uploads');
170 $tem->set('enctype_attr', '" enctype="multipart/form-data');
171 $uploads_output_already = true;
174 $tem->set('name', 'save');
175 $tem->set('caption', 'Save');
182 return $tem->get('form');
186 function view_template() {
188 echo make_template();
192 function make_php() {
194 $tem->load('code/wfpl/metaform/template.php');
195 $tem->set('form_name', $GLOBALS['form_name']);
196 $fields = get_fields();
199 $always_field = false;
200 $image_included_yet = false;
201 foreach($fields as $field) {
202 list($name, $type, $input, $format, $sql) = $field;
203 if($input != 'submit') {
204 $tem->set('format', $format);
205 $tem->set('name', $name);
206 $tem->set('db_field', ''); # we don't want to use the value from last time
208 if($db_fields != '') $db_fields .= ',';
210 if($php_fields != '') $php_fields .= ', ';
211 $php_fields .= '$' . $name;
213 if($input == 'image') {
214 $tem->sub('image_upload');
215 $tem->sub('image_db');
216 if(!$image_included_yet) {
217 $tem->sub('image_include');
218 $tem->sub('upload_max');
219 $tem->sub('upload_settings');
220 $image_included_yet = true;
223 $tem->sub('formats');
224 $tem->sub('tem_sets');
226 if(!$always_field and $input != 'checkbox' and $input != 'radio') {
227 $always_field = $name;
231 # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
232 $tem->set('always_field', $always_field);
233 $tem->set('db_fields', $db_fields);
234 $tem->set('php_fields', $php_fields);
238 function view_php() {
244 function make_email() {
246 $tem->load('code/wfpl/metaform/template.email.txt');
247 $tem->set('form_name', $GLOBALS['form_name']);
248 $fields = get_fields();
249 foreach($fields as $field) {
250 list($name, $type, $input, $format, $sql) = $field;
251 $tem->set('name', $name);
252 $tem->set('caption', $name); # fixme
258 function view_email() {
266 $tem->load('code/wfpl/metaform/preview.html');
267 $tem->set('form_name', $GLOBALS['form_name']);
268 $tem->set('fields', $_REQUEST['fields']);
269 $preview_tem = new tem();
270 $preview = $preview_tem->run(make_template(false));
272 $tem->set('preview', $preview);
277 function download_tar() {
278 $name = $GLOBALS['form_name'];
280 "$name.html" => make_template(),
281 "$name.sql" => make_sql(),
282 "$name.email.txt" => make_email(),
283 "$name.php" => make_php());
284 make_tar($name, $data);