JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fixed session_get() and session_set() and metaform url (all untested)
[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 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)
10 #  any later version.
11 #
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
15 #  more details.
16 #
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
20
21
22 # This file writes the code for you (sql, php, html, email) to handle a form.
23
24 require_once('code/wfpl/template.php');
25 require_once('code/wfpl/http.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',        'int'),
34         'bigint' =>     array('textbox',     'int',        'varchar(100)'), # up to 100 digits, stored as a string
35         'email' =>      array('textbox',     'email',      'varchar(100)'),
36         'phone' =>      array('textbox',     'phone',      'varchar(32)'),
37         'money' =>      array('textbox',     'money',      'varchar(32)'),
38         'dollars' =>    array('textbox',     'dollars',    'varchar(32)'),
39         'url' =>        array('textbox',     'url',        'varchar(200)'),
40         'hidden' =>     array('hidden',      'unix',       'varchar(200)'),
41         'password' =>   array('password',    'oneline',    'varchar(200)'),
42         'textarea' =>   array('textarea',    'unix',       'text'),
43         'pulldown' =>   array('pulldown',    'options',    'int'),
44         'checkbox' =>   array('checkbox',    'yesno',      'varchar(3)'),
45         'yesno' =>      array('checkbox',    'yesno',      'varchar(3)'),
46         'delete' =>     array('checkbox',    'yesno',      'n/a'),
47         'image' =>      array('image',       'oneline',    'varchar(200)'),
48         'submit' =>     array('submit',      'oneline',    'n/a')
49 );
50
51 if(isset($_REQUEST['form_name'])) {
52         $GLOBALS['form_name'] = ereg_replace('[^a-z0-9_-]', '', $_REQUEST['form_name']);
53 } else {
54         $GLOBALS['form_name'] = 'some_form';
55 }
56
57 if(isset($_REQUEST['fields'])) {
58         if(isset($_REQUEST['view_sql'])) {
59                 view_sql();
60                 exit();
61         } elseif(isset($_REQUEST['view_php'])) {
62                 view_php();
63                 exit();
64         } elseif(isset($_REQUEST['view_template'])) {
65                 view_template();
66                 exit();
67         } elseif(isset($_REQUEST['view_email'])) {
68                 view_email();
69                 exit();
70         } elseif(isset($_REQUEST['download_tar'])) {
71                 download_tar();
72                 exit();
73         } elseif(isset($_REQUEST['preview'])) {
74                 preview();
75                 exit();
76         } elseif(isset($_REQUEST['edit'])) {
77                 tem_set('fields', $_REQUEST['fields']);
78                 tem_set('form_name', $GLOBALS['form_name']);
79                 # fall through
80         } else {
81                 die("Sorry... couldn't tell which button you pressed");
82         }
83 }
84
85 set_form_action();
86 tem_output('code/wfpl/metaform/main.html');
87 exit();
88
89
90 function field_input($type)  { return $GLOBALS['types'][$type][0]; }
91 function field_format($type) { return $GLOBALS['types'][$type][1]; }
92 function field_sql($type)    { return $GLOBALS['types'][$type][2]; }
93
94 function get_fields() {
95         $fields_str = unix_newlines($_REQUEST['fields']);
96         $ret = array();
97         $fields_str = rtrim($fields_str);
98         $fields = split("\n", $fields_str);
99         foreach($fields as $field) {
100                 list($name, $type, $options) = split('  *', $field);
101                 if($options) $options = split(',', $options);
102                 if(!$type) $type = $name;
103                 $input = field_input($type);
104                 $format = field_format($type);
105                 $sql = field_sql($type);
106                 $ret[] = array($name, $type, $input, $format, $sql, $options);
107         }
108         return $ret;
109 }
110
111 # this one, that you're using to create forms
112 function set_form_action() {
113         $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
114         if($action == '') $action = './';
115         tem_set('form_action', $action);
116 }
117
118 # perfect HTTP headers for viewing created files
119 function view_headers() {
120         header('Content-type: text/plain');
121 }
122         
123
124
125
126 function make_sql() {
127         $tem = new tem();
128         $tem->load('code/wfpl/metaform/template.sql');
129         $tem->set('form_name', $GLOBALS['form_name']);
130         $fields = get_fields();
131         foreach($fields as $field) {
132                 list($name, $type, $input, $format, $sql) = $field;
133                 if($sql != 'n/a') {
134                         $tem->set('name', $name);
135                         $tem->set('type', $sql);
136                         if($sql == 'int') {
137                                 $tem->set('default', '0');
138                         } else {
139                                 $tem->set('default', '""');
140                         }
141                         $tem->sub('column');
142                 }
143         }
144         view_headers();
145         return $tem->run();
146 }
147
148 function view_sql() {
149         view_headers();
150         echo make_sql();
151 }
152         
153
154 # pass false if you want to exclude the <head> and <body> tag etc.
155 function make_template($whole_file = true) {
156         $uploads_output_already = false;
157         $tem = new tem();
158         $tem->load('code/wfpl/metaform/template.html');
159         $tem->set('form_name', $GLOBALS['form_name']);
160         $fields = get_fields();
161         foreach($fields as $field) {
162                 list($name, $type, $input, $format, $sql) = $field;
163                 $tem->set('name', $name);
164                 $tem->set('caption', $name); # fixme
165                 $tem->sub($input);
166                 if($input != 'hidden') {
167                         $tem->sub('row');
168                 }
169                 if($input == 'image' && !$uploads_output_already) {
170                         $tem->sub('uploads');
171                         $tem->set('enctype_attr', '" enctype="multipart/form-data');
172                         $uploads_output_already = true;
173                 }
174         }
175         $tem->set('name', 'save');
176         $tem->set('caption', 'Save');
177         $tem->sub('submit');
178         $tem->sub('row');
179         $tem->sub('form');
180         if($whole_file) {
181                 return $tem->run();
182         } else {
183                 return $tem->get('form');
184         }
185 }
186
187 function view_template() {
188         view_headers();
189         echo make_template();
190 }
191
192
193 function make_php() {
194         $tem = new tem();
195         $tem->load('code/wfpl/metaform/template.php');
196         $tem->set('form_name', $GLOBALS['form_name']);
197         $fields = get_fields();
198         $db_fields = '';
199         $php_fields = '';
200         $always_field = false;
201         $image_included_yet = false;
202         foreach($fields as $field) {
203                 list($name, $type, $input, $format, $sql) = $field;
204                 if($input != 'submit') {
205                         $tem->set('format', $format);
206                         $tem->set('name', $name);
207                         $tem->set('db_field', ''); # we don't want to use the value from last time
208                         if($sql != 'n/a') {
209                                 if($db_fields != '') $db_fields .= ',';
210                                 $db_fields .= $name;
211                                 if($php_fields != '') $php_fields .= ', ';
212                                 $php_fields .= '$' . $name;
213                         }
214                         if($input == 'image') {
215                                 $tem->sub('image_upload');
216                                 $tem->sub('image_db');
217                                 if(!$image_included_yet) {
218                                         $tem->sub('image_include');
219                                         $tem->sub('upload_max');
220                                         $tem->sub('upload_settings');
221                                         $image_included_yet = true;
222                                 }
223                         } else {
224                                 $tem->sub('formats');
225                                 $tem->sub('tem_sets');
226                         }
227                         if(!$always_field and $input != 'checkbox' and $input != 'radio') {
228                                 $always_field = $name;
229                         }
230                 }
231         }
232         # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
233         $tem->set('always_field', $always_field);
234         $tem->set('db_fields', $db_fields);
235         $tem->set('php_fields', $php_fields);
236         $tem->set('metaform_url', edit_url());
237         return $tem->run();
238 }
239
240 # make a URL for the edit page with all the fields filled in
241 function edit_url() {
242         $url = this_url();
243         $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
244         $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
245         $url = ereg_replace('/[a-z0-9_.]*?', '/?', $url);
246         return $url;
247 }
248
249 function view_php() {
250         view_headers();
251         echo make_php();
252 }
253
254
255 function make_email() {
256         $tem = new tem();
257         $tem->load('code/wfpl/metaform/template.email.txt');
258         $tem->set('form_name', $GLOBALS['form_name']);
259         $fields = get_fields();
260         foreach($fields as $field) {
261                 list($name, $type, $input, $format, $sql) = $field;
262                 $tem->set('name', $name);
263                 $tem->set('caption', $name); # fixme
264                 $tem->sub('fields');
265         }
266         return $tem->run();
267 }
268
269 function view_email() {
270         view_headers();
271         echo make_email();
272 }
273
274
275 function preview() {
276         $tem = new tem();
277         $tem->load('code/wfpl/metaform/preview.html');
278         $tem->set('form_name', $GLOBALS['form_name']);
279         $tem->set('fields', $_REQUEST['fields']);
280         $preview_tem = new tem();
281         $preview = $preview_tem->run(make_template(false));
282         unset($preview_tem);
283         $tem->set('preview', $preview);
284         set_form_action();
285         $tem->output();
286 }
287
288 function download_tar() {
289         $name = $GLOBALS['form_name'];
290         $data = array(
291                 "$name.html" => make_template(),
292                 "$name.sql" => make_sql(),
293                 "$name.email.txt" => make_email(),
294                 "$name.php" => make_php());
295         make_wfpl_tar($name, $data);
296 }
297
298 ?>