JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
metaform: added 'int' type, made preview more blatant, and ran the preview through...
[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         }
160         $tem->set('name', 'save');
161         $tem->set('caption', 'Save');
162         $tem->sub('submit');
163         $tem->sub('form');
164         if($whole_file) {
165                 return $tem->run();
166         } else {
167                 return $tem->get('form');
168         }
169 }
170
171 function view_template() {
172         view_headers();
173         echo make_template();
174 }
175
176
177 function make_php() {
178         $tem = new tem();
179         $tem->load('code/wfpl/metaform/template.php');
180         $tem->set('form_name', $GLOBALS['form_name']);
181         $fields = get_fields();
182         $db_fields = '';
183         $always_field = false;
184         foreach($fields as $field) {
185                 list($name, $type, $input, $format, $sql) = $field;
186                 if($input != 'submit') {
187                         $tem->set('format', $format);
188                         $tem->set('name', $name);
189                         $tem->set('db_field', ''); # we don't want to use the value from last time
190                         if($sql != 'n/a') {
191                                 $tem->sub('db_field');
192                                 if($db_fields != '') $db_fields .= ',';
193                                 $db_fields .= $name;
194                         }
195                         $tem->sub('formats');
196                         if(!$always_field and $input != 'checkbox' and $input != 'radio') {
197                                 $always_field = $name;
198                         }
199                 }
200         }
201         # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
202         $tem->set('always_field', $always_field);
203         $tem->set('db_fields', $db_fields);
204         return $tem->run();
205 }
206
207 function view_php() {
208         view_headers();
209         echo make_php();
210 }
211
212
213 function make_email() {
214         $tem = new tem();
215         $tem->load('code/wfpl/metaform/template.email.txt');
216         $tem->set('form_name', $GLOBALS['form_name']);
217         $fields = get_fields();
218         foreach($fields as $field) {
219                 list($name, $type, $input, $format, $sql) = $field;
220                 $tem->set('name', $name);
221                 $tem->set('caption', $name); # fixme
222                 $tem->sub('fields');
223         }
224         return $tem->run();
225 }
226
227 function view_email() {
228         view_headers();
229         echo make_email();
230 }
231
232
233 function preview() {
234         $tem = new tem();
235         $tem->load('code/wfpl/metaform/preview.html');
236         $tem->set('form_name', $GLOBALS['form_name']);
237         $tem->set('fields', $_REQUEST['fields']);
238         $preview_tem = new tem();
239         $preview = $preview_tem->run(make_template(false));
240         unset($preview_tem);
241         $tem->set('preview', $preview);
242         set_form_action();
243         $tem->output();
244 }
245
246 function download_tar() {
247         $name = $GLOBALS['form_name'];
248         $data = array(
249                 "$name.html" => make_template(),
250                 "$name.sql" => make_sql(),
251                 "$name.email.txt" => make_email(),
252                 "$name.php" => make_php());
253         make_tar($name, $data);
254 }
255
256 ?>