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/tar.php');
27 # see code/wfpl/metaform/template.html for the html templates for these elements
28 $GLOBALS['types'] = array(
29 # type input format sql
30 'name' => array('textbox', 'oneline', 'varchar(200)'),
31 'textbox' => array('textbox', 'oneline', 'varchar(200)'),
32 'int' => array('textbox', 'int', 'varchar(100)'),
33 'email' => array('textbox', 'email', 'varchar(100)'),
34 'phone' => array('textbox', 'phone', 'varchar(32)'),
35 'money' => array('textbox', 'money', 'varchar(32)'),
36 'dollars' => array('textbox', 'dollars', 'varchar(32)'),
37 'url' => array('textbox', 'url', 'varchar(200)'),
38 'hidden' => array('hidden', 'unix', 'varchar(200)'),
39 'password' => array('password', 'oneline', 'varchar(200)'),
40 'textarea' => array('textarea', 'unix', 'text'),
41 'pulldown' => array('pulldown', 'options', 'int'),
42 'checkbox' => array('checkbox', 'yesno', 'varchar(3)'),
43 'yesno' => array('checkbox', 'yesno', 'varchar(3)'),
44 'delete' => array('checkbox', 'yesno', 'n/a'),
45 'image' => array('image', 'oneline', 'varchar(200)'),
46 'submit' => array('submit', 'oneline', 'n/a')
49 if(isset($_REQUEST['form_name'])) {
50 $GLOBALS['form_name'] = ereg_replace('[^a-z0-9_-]', '', $_REQUEST['form_name']);
52 $GLOBALS['form_name'] = 'some_form';
55 if(isset($_REQUEST['fields'])) {
56 if(isset($_REQUEST['view_sql'])) {
59 } elseif(isset($_REQUEST['view_php'])) {
62 } elseif(isset($_REQUEST['view_template'])) {
65 } elseif(isset($_REQUEST['view_email'])) {
68 } elseif(isset($_REQUEST['download_tar'])) {
71 } elseif(isset($_REQUEST['preview'])) {
74 } elseif(isset($_REQUEST['edit'])) {
75 tem_set('fields', $_REQUEST['fields']);
76 tem_set('form_name', $GLOBALS['form_name']);
79 die("Sorry... couldn't tell which button you pressed");
84 tem_output('code/wfpl/metaform/main.html');
88 function field_input($type) { return $GLOBALS['types'][$type][0]; }
89 function field_format($type) { return $GLOBALS['types'][$type][1]; }
90 function field_sql($type) { return $GLOBALS['types'][$type][2]; }
92 function get_fields() {
93 $fields_str = unix_newlines($_REQUEST['fields']);
95 $fields_str = rtrim($fields_str);
96 $fields = split("\n", $fields_str);
97 foreach($fields as $field) {
98 list($name, $type, $options) = split(' *', $field);
99 if($options) $options = split(',', $options);
100 if(!$type) $type = $name;
101 $input = field_input($type);
102 $format = field_format($type);
103 $sql = field_sql($type);
104 $ret[] = array($name, $type, $input, $format, $sql, $options);
109 # this one, that you're using to create forms
110 function set_form_action() {
111 $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
112 if($action == '') $action = './';
113 tem_set('form_action', $action);
116 # perfect HTTP headers for viewing created files
117 function view_headers() {
118 header('Content-type: text/plain');
124 function make_sql() {
126 $tem->load('code/wfpl/metaform/template.sql');
127 $tem->set('form_name', $GLOBALS['form_name']);
128 $fields = get_fields();
129 foreach($fields as $field) {
130 list($name, $type, $input, $format, $sql) = $field;
132 $tem->set('name', $name);
133 $tem->set('type', $sql);
135 $tem->set('default', '0');
137 $tem->set('default', '""');
146 function view_sql() {
152 # pass false if you want to exclude the <head> and <body> tag etc.
153 function make_template($whole_file = true) {
154 $uploads_output_already = false;
156 $tem->load('code/wfpl/metaform/template.html');
157 $tem->set('form_name', $GLOBALS['form_name']);
158 $fields = get_fields();
159 foreach($fields as $field) {
160 list($name, $type, $input, $format, $sql) = $field;
161 $tem->set('name', $name);
162 $tem->set('caption', $name); # fixme
164 if($input != 'hidden') {
167 if($input == 'image' && !$uploads_output_already) {
168 $tem->sub('uploads');
169 $tem->set('enctype_attr', '" enctype="multipart/form-data');
170 $uploads_output_already = true;
173 $tem->set('name', 'save');
174 $tem->set('caption', 'Save');
181 return $tem->get('form');
185 function view_template() {
187 echo make_template();
191 function make_php() {
193 $tem->load('code/wfpl/metaform/template.php');
194 $tem->set('form_name', $GLOBALS['form_name']);
195 $fields = get_fields();
198 $always_field = false;
199 $image_included_yet = false;
200 foreach($fields as $field) {
201 list($name, $type, $input, $format, $sql) = $field;
202 if($input != 'submit') {
203 $tem->set('format', $format);
204 $tem->set('name', $name);
205 $tem->set('db_field', ''); # we don't want to use the value from last time
207 if($db_fields != '') $db_fields .= ',';
209 if($php_fields != '') $php_fields .= ', ';
210 $php_fields .= '$' . $name;
212 if($input == 'image') {
213 $tem->sub('image_upload');
214 $tem->sub('image_db');
215 if(!$image_included_yet) {
216 $tem->sub('image_include');
217 $tem->sub('upload_max');
218 $tem->sub('upload_settings');
219 $image_included_yet = true;
222 $tem->sub('formats');
223 $tem->sub('tem_sets');
225 if(!$always_field and $input != 'checkbox' and $input != 'radio') {
226 $always_field = $name;
230 # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
231 $tem->set('always_field', $always_field);
232 $tem->set('db_fields', $db_fields);
233 $tem->set('php_fields', $php_fields);
237 function view_php() {
243 function make_email() {
245 $tem->load('code/wfpl/metaform/template.email.txt');
246 $tem->set('form_name', $GLOBALS['form_name']);
247 $fields = get_fields();
248 foreach($fields as $field) {
249 list($name, $type, $input, $format, $sql) = $field;
250 $tem->set('name', $name);
251 $tem->set('caption', $name); # fixme
257 function view_email() {
265 $tem->load('code/wfpl/metaform/preview.html');
266 $tem->set('form_name', $GLOBALS['form_name']);
267 $tem->set('fields', $_REQUEST['fields']);
268 $preview_tem = new tem();
269 $preview = $preview_tem->run(make_template(false));
271 $tem->set('preview', $preview);
276 function download_tar() {
277 $name = $GLOBALS['form_name'];
279 "$name.html" => make_template(),
280 "$name.sql" => make_sql(),
281 "$name.email.txt" => make_email(),
282 "$name.php" => make_php());
283 make_wfpl_tar($name, $data);