JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
metaform: fields not are in the right order. added CSS for captions
[wfpl.git] / metaform.php
1 <?php
2
3 #  Copyright (C) 2006 Jason Woofenden
4 #
5 #  This file is part of wfpl.
6 #
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)
10 #  any later version.
11 #
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.
16 #
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,
20 #  MA 02111-1307, USA.
21
22
23 # This file writes the code for you (sql, php, html, email) to handle a form.
24
25 require_once('code/wfpl/template.php');
26 require_once('code/wfpl/tar.php');
27
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         'textarea' =>   array('textarea',    'unix',       'text'),
40         'pulldown' =>   array('pulldown',    'options',    'int'),
41         'checkbox' =>   array('checkbox',    'yesno',      'int'),
42         'yesno' =>      array('checkbox',    'yesno',      'int'),
43         'submit' =>     array('submit',      'oneline',    'n/a')
44 );
45
46 if(isset($_REQUEST['form_name'])) {
47         $GLOBALS['form_name'] = ereg_replace('[^a-z0-9_-]', '', $_REQUEST['form_name']);
48 } else {
49         $GLOBALS['form_name'] = 'some_form';
50 }
51
52 if(isset($_REQUEST['fields'])) {
53         if(isset($_REQUEST['view_sql'])) {
54                 view_sql();
55                 exit();
56         } elseif(isset($_REQUEST['view_php'])) {
57                 view_php();
58                 exit();
59         } elseif(isset($_REQUEST['view_template'])) {
60                 view_template();
61                 exit();
62         } elseif(isset($_REQUEST['view_email'])) {
63                 view_email();
64                 exit();
65         } elseif(isset($_REQUEST['download_tar'])) {
66                 download_tar();
67                 exit();
68         } elseif(isset($_REQUEST['preview'])) {
69                 preview();
70                 exit();
71         } elseif(isset($_REQUEST['edit'])) {
72                 tem_set('fields', $_REQUEST['fields']);
73                 tem_set('form_name', $GLOBALS['form_name']);
74                 # fall through
75         } else {
76                 die("Sorry... couldn't tell which button you pressed");
77         }
78 }
79
80 set_form_action();
81 tem_output('code/wfpl/metaform/main.html');
82 exit();
83
84
85 function field_input($type)  { return $GLOBALS['types'][$type][0]; }
86 function field_format($type) { return $GLOBALS['types'][$type][1]; }
87 function field_sql($type)    { return $GLOBALS['types'][$type][2]; }
88
89 function get_fields() {
90         $fields_str = unix_newlines($_REQUEST['fields']);
91         $ret = array();
92         $fields_str = rtrim($fields_str);
93         $fields = split("\n", $fields_str);
94         foreach($fields as $field) {
95                 list($name, $type, $options) = split('  *', $field);
96                 if($options) $options = split(',', $options);
97                 if(!$type) $type = $name;
98                 $input = field_input($type);
99                 $format = field_format($type);
100                 $sql = field_sql($type);
101                 $ret[] = array($name, $type, $input, $format, $sql, $options);
102         }
103         return $ret;
104 }
105
106 function set_form_action() {
107         $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
108         if($action == '') $action = './';
109         tem_set('form_action', $action);
110 }
111
112 # perfect HTTP headers for viewing created files
113 function view_headers() {
114         header('Content-type: text/plain');
115 }
116         
117
118
119
120 function make_sql() {
121         $tem = new tem();
122         $tem->load('code/wfpl/metaform/template.sql');
123         $tem->set('form_name', $GLOBALS['form_name']);
124         $fields = get_fields();
125         foreach($fields as $field) {
126                 list($name, $type, $input, $format, $sql) = $field;
127                 if($sql != 'n/a') {
128                         $tem->set('name', $name);
129                         $tem->set('type', $sql);
130                         if($sql == 'int') {
131                                 $tem->set('default', '0');
132                         } else {
133                                 $tem->set('default', '""');
134                         }
135                         $tem->sub('column');
136                 }
137         }
138         view_headers();
139         return $tem->run();
140 }
141
142 function view_sql() {
143         view_headers();
144         echo make_sql();
145 }
146         
147
148 # pass false if you want to exclude the <head> and <body> tag etc.
149 function make_template($whole_file = true) {
150         $tem = new tem();
151         $tem->load('code/wfpl/metaform/template.html');
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;
156                 $tem->set('name', $name);
157                 $tem->set('caption', $name); # fixme
158                 $tem->sub($input);
159                 $tem->sub('row');
160         }
161         $tem->set('name', 'save');
162         $tem->set('caption', 'Save');
163         $tem->sub('submit');
164         $tem->sub('row');
165         $tem->sub('form');
166         if($whole_file) {
167                 return $tem->run();
168         } else {
169                 return $tem->get('form');
170         }
171 }
172
173 function view_template() {
174         view_headers();
175         echo make_template();
176 }
177
178
179 function make_php() {
180         $tem = new tem();
181         $tem->load('code/wfpl/metaform/template.php');
182         $tem->set('form_name', $GLOBALS['form_name']);
183         $fields = get_fields();
184         $db_fields = '';
185         $always_field = false;
186         foreach($fields as $field) {
187                 list($name, $type, $input, $format, $sql) = $field;
188                 if($input != 'submit') {
189                         $tem->set('format', $format);
190                         $tem->set('name', $name);
191                         $tem->set('db_field', ''); # we don't want to use the value from last time
192                         if($sql != 'n/a') {
193                                 $tem->sub('db_field');
194                                 if($db_fields != '') $db_fields .= ',';
195                                 $db_fields .= $name;
196                         }
197                         $tem->sub('formats');
198                         if(!$always_field and $input != 'checkbox' and $input != 'radio') {
199                                 $always_field = $name;
200                         }
201                 }
202         }
203         # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
204         $tem->set('always_field', $always_field);
205         $tem->set('db_fields', $db_fields);
206         return $tem->run();
207 }
208
209 function view_php() {
210         view_headers();
211         echo make_php();
212 }
213
214
215 function make_email() {
216         $tem = new tem();
217         $tem->load('code/wfpl/metaform/template.email.txt');
218         $tem->set('form_name', $GLOBALS['form_name']);
219         $fields = get_fields();
220         foreach($fields as $field) {
221                 list($name, $type, $input, $format, $sql) = $field;
222                 $tem->set('name', $name);
223                 $tem->set('caption', $name); # fixme
224                 $tem->sub('fields');
225         }
226         return $tem->run();
227 }
228
229 function view_email() {
230         view_headers();
231         echo make_email();
232 }
233
234
235 function preview() {
236         $tem = new tem();
237         $tem->load('code/wfpl/metaform/preview.html');
238         $tem->set('form_name', $GLOBALS['form_name']);
239         $tem->set('fields', $_REQUEST['fields']);
240         $preview_tem = new tem();
241         $preview = $preview_tem->run(make_template(false));
242         unset($preview_tem);
243         $tem->set('preview', $preview);
244         set_form_action();
245         $tem->output();
246 }
247
248 function download_tar() {
249         $name = $GLOBALS['form_name'];
250         $data = array(
251                 "$name.html" => make_template(),
252                 "$name.sql" => make_sql(),
253                 "$name.email.txt" => make_email(),
254                 "$name.php" => make_php());
255         make_tar($name, $data);
256 }
257
258 ?>