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']);
75 $GLOBALS['form_name'] = 'some_form';
78 if(isset($_REQUEST['fields'])) {
79 if(isset($_REQUEST['view_sql'])) {
82 } elseif(isset($_REQUEST['view_php'])) {
85 } elseif(isset($_REQUEST['view_html'])) {
88 } elseif(isset($_REQUEST['view_email'])) {
91 } elseif(isset($_REQUEST['download_tar'])) {
94 } elseif(isset($_REQUEST['preview'])) {
97 } elseif(isset($_REQUEST['edit'])) {
98 tem_set('fields', $_REQUEST['fields']);
99 tem_set('form_name', $GLOBALS['form_name']);
102 die("Sorry... couldn't tell which button you pressed");
108 list_available_types();
109 tem_output('code/wfpl/metaform/main.html');
113 function field_input($type) { return $GLOBALS['types'][$type][0]; }
114 function field_format($type) { return $GLOBALS['types'][$type][1]; }
115 function field_sql($type) { return $GLOBALS['types'][$type][2]; }
117 function get_fields() {
118 $fields_str = unix_newlines($_REQUEST['fields']);
120 $fields_str = rtrim($fields_str);
121 $fields = split("\n", $fields_str);
122 foreach($fields as $field) {
123 list($name, $type, $options) = split(' *', $field);
124 if($options) $options = split(',', $options);
125 if(!$type) $type = $name;
126 $input = field_input($type);
127 $format = field_format($type);
128 $sql = field_sql($type);
129 $ret[] = array($name, $type, $input, $format, $sql, $options);
134 # this one, that you're using to create forms
135 function set_form_action() {
136 $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
137 if($action == '') $action = './';
138 tem_set('form_action', $action);
141 # perfect HTTP headers for viewing created files
142 function view_headers() {
143 header('Content-type: text/plain');
149 function make_sql() {
151 $tem->load('code/wfpl/metaform/template.sql');
152 $tem->set('form_name', $GLOBALS['form_name']);
153 $fields = get_fields();
154 foreach($fields as $field) {
155 list($name, $type, $input, $format, $sql) = $field;
157 $tem->set('name', $name);
158 $tem->set('type', $sql);
160 $tem->set('default', '0');
162 $tem->set('default', '""');
171 function view_sql() {
177 # pass false if you want to exclude the <head> and <body> tag etc.
178 function make_html($whole_file = true) {
179 $uploads_output_already = false;
180 $has_html_editors = false;
182 $tem->load('code/wfpl/metaform/template.html');
183 $tem->set('form_name', $GLOBALS['form_name']);
184 $fields = get_fields();
185 foreach($fields as $field) {
186 list($name, $type, $input, $format, $sql) = $field;
187 $tem->set('name', $name);
188 $tem->set('caption', $name); # fixme
190 if($input != 'hidden') {
193 if($input == 'image' && !$uploads_output_already) {
194 $tem->sub('uploads');
195 $tem->set('enctype_attr', '" enctype="multipart/form-data');
196 $uploads_output_already = true;
197 } elseif($input == 'html') {
198 $has_html_editors = true;
199 $tem->set('html_field_name', $name);
200 $tem->sub('replace_textarea');
204 if($GLOBALS['opt_db'] == 'Yes') {
205 $tem->sub('opt_db_1');
207 $tem->sub('opt_db_1_else');
209 $tem->set('name', 'save');
210 $tem->set('caption', 'Save');
215 if($has_html_editors) {
216 $tem->sub('html_editor_headers');
222 return $tem->get('form');
226 function view_html() {
232 function make_php() {
234 $tem->load('code/wfpl/metaform/template.php');
235 $tem->set('form_name', $GLOBALS['form_name']);
236 $fields = get_fields();
239 $always_field = false;
240 $image_included_yet = false;
241 foreach($fields as $field) {
242 list($name, $type, $input, $format, $sql) = $field;
243 if($input != 'submit') {
244 $tem->set('format', $format);
245 $tem->set('name', $name);
246 $tem->set('db_field', ''); # we don't want to use the value from last time
248 if($db_fields != '') $db_fields .= ',';
250 if($php_fields != '') $php_fields .= ', ';
251 $php_fields .= '$' . $name;
253 if($input == 'image') {
254 $tem->sub('image_upload');
255 $tem->sub('image_db');
256 if(!$image_included_yet) {
257 $tem->sub('image_include');
258 $tem->sub('upload_max');
259 $tem->sub('upload_settings');
260 $image_included_yet = true;
263 $tem->sub('formats');
264 $tem->sub('tem_sets');
266 if(!$always_field and $input != 'checkbox' and $input != 'radio') {
267 $always_field = $name;
271 # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
272 $tem->set('always_field', $always_field);
273 $tem->set('db_fields', $db_fields);
274 $tem->set('php_fields', $php_fields);
275 $tem->set('metaform_url', edit_url());
276 if($GLOBALS['opt_db'] == 'Yes') {
277 $tem->sub('opt_db_1');
278 $tem->sub('opt_db_2');
279 $tem->sub('opt_db_3');
280 $tem->sub('opt_db_4');
281 $tem->sub('opt_db_5');
283 if($GLOBALS['opt_email'] == 'Yes') {
284 $tem->sub('opt_email_1');
285 $tem->sub('opt_email_2');
290 # make a URL for the edit page with all the fields filled in
291 function edit_url() {
293 $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
294 $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
295 $url = ereg_replace('/[a-z0-9_.]*\?', '/?', $url);
299 function view_php() {
305 function make_email() {
307 $tem->load('code/wfpl/metaform/template.email.txt');
308 $tem->set('form_name', $GLOBALS['form_name']);
309 $fields = get_fields();
310 foreach($fields as $field) {
311 list($name, $type, $input, $format, $sql) = $field;
312 $tem->set('name', $name);
313 $tem->set('caption', $name); # fixme
314 if($type == 'textarea') {
315 $tem->sub('multi_line');
323 function make_htaccess() {
325 $tem->set('form', $GLOBALS['form_name']);
326 return $tem->run('code/wfpl/metaform/template.htaccess');
329 function view_email() {
336 tem_load('code/wfpl/metaform/preview.html');
337 tem_set('form_name', $GLOBALS['form_name']);
338 tem_set('fields', $_REQUEST['fields']);
339 $preview_tem = new tem();
340 $preview = $preview_tem->run(make_html(false));
342 tem_set('preview', $preview);
347 function download_tar() {
348 $name = $GLOBALS['form_name'];
350 ".htaccess" => make_htaccess(),
351 "run.php ->" => 'code/wfpl/run.php',
352 "$name.html" => make_html(),
353 "$name.sql" => make_sql(),
354 "$name.php" => make_php());
355 if($GLOBALS['opt_email'] == 'Yes') {
356 $data["$name.email.txt"] = make_email();
358 make_wfpl_tar($name, $data);