JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fixed conflict
[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 require_once('code/wfpl/format.php');
28
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')
53 );
54
55 function list_available_types() {
56         $types = '';
57         foreach($GLOBALS['types'] as $key => $value) {
58                 if($types) {
59                         $types .= ', ';
60                 }
61                 $types .= $key;
62         }
63         tem_set('available_types', $types);
64 }
65
66
67 function metaform() {
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']);
74         } else {
75                 $GLOBALS['form_name'] = 'some_form';
76         }
77
78         if(isset($_REQUEST['fields'])) {
79                 if(isset($_REQUEST['view_sql'])) {
80                         view_sql();
81                         exit();
82                 } elseif(isset($_REQUEST['view_php'])) {
83                         view_php();
84                         exit();
85                 } elseif(isset($_REQUEST['view_html'])) {
86                         view_html();
87                         exit();
88                 } elseif(isset($_REQUEST['view_email'])) {
89                         view_email();
90                         exit();
91                 } elseif(isset($_REQUEST['download_tar'])) {
92                         download_tar();
93                         exit();
94                 } elseif(isset($_REQUEST['preview'])) {
95                         preview();
96                         exit();
97                 } elseif(isset($_REQUEST['edit'])) {
98                         tem_set('fields', $_REQUEST['fields']);
99                         tem_set('form_name', $GLOBALS['form_name']);
100                         # fall through
101                 } else {
102                         die("Sorry... couldn't tell which button you pressed");
103                 }
104         }
105
106
107         set_form_action();
108         list_available_types();
109         tem_output('code/wfpl/metaform/main.html');
110 }
111
112
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]; }
116
117 function get_fields() {
118         $fields_str = unix_newlines($_REQUEST['fields']);
119         $ret = array();
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);
130         }
131         return $ret;
132 }
133
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);
139 }
140
141 # perfect HTTP headers for viewing created files
142 function view_headers() {
143         header('Content-type: text/plain');
144 }
145         
146
147
148
149 function make_sql() {
150         $tem = new tem();
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;
156                 if($sql != 'n/a') {
157                         $tem->set('name', $name);
158                         $tem->set('type', $sql);
159                         if($sql == 'int') {
160                                 $tem->set('default', '0');
161                         } else {
162                                 $tem->set('default', '""');
163                         }
164                         $tem->sub('column');
165                 }
166         }
167         view_headers();
168         return $tem->run();
169 }
170
171 function view_sql() {
172         view_headers();
173         echo make_sql();
174 }
175         
176
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;
181         $tem = new tem();
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
189                 $tem->sub($input);
190                 if($input != 'hidden') {
191                         $tem->sub('row');
192                 }
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');
201                 }
202         }
203
204         if($GLOBALS['opt_db'] == 'Yes') {
205                 $tem->sub('opt_db_1');
206         } else {
207                 $tem->sub('opt_db_1_else');
208         }
209         $tem->set('name', 'save');
210         $tem->set('caption', 'Save');
211         $tem->sub('submit');
212         $tem->sub('row');
213         $tem->sub('form');
214
215         if($has_html_editors) {
216                 $tem->sub('html_editor_headers');
217         }
218
219         if($whole_file) {
220                 return $tem->run();
221         } else {
222                 return $tem->get('form');
223         }
224 }
225
226 function view_html() {
227         view_headers();
228         echo make_html();
229 }
230
231
232 function make_php() {
233         $tem = new tem();
234         $tem->load('code/wfpl/metaform/template.php');
235         $tem->set('form_name', $GLOBALS['form_name']);
236         $fields = get_fields();
237         $db_fields = '';
238         $php_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
247                         if($sql != 'n/a') {
248                                 if($db_fields != '') $db_fields .= ',';
249                                 $db_fields .= $name;
250                                 if($php_fields != '') $php_fields .= ', ';
251                                 $php_fields .= '$' . $name;
252                         }
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;
261                                 }
262                         } else {
263                                 $tem->sub('formats');
264                         }
265                         $tem->sub('tem_sets');
266                         if(!$always_field and $input != 'checkbox' and $input != 'radio') {
267                                 $always_field = $name;
268                         }
269                 }
270         }
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');
282         }
283         if($GLOBALS['opt_email'] == 'Yes') {
284                 $tem->sub('opt_email_1');
285                 $tem->sub('opt_email_2');
286         }
287         return $tem->run();
288 }
289
290 # make a URL for the edit page with all the fields filled in
291 function edit_url() {
292         $url = this_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);
296         return $url;
297 }
298
299 function view_php() {
300         view_headers();
301         echo make_php();
302 }
303
304
305 function make_email() {
306         $tem = new tem();
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');
316                 } else {
317                         $tem->sub('fields');
318                 }
319         }
320         return $tem->run();
321 }
322
323 function make_htaccess() {
324         $tem = new tem();
325         $tem->set('form', $GLOBALS['form_name']);
326         return $tem->run('code/wfpl/metaform/template.htaccess');
327 }
328
329 function view_email() {
330         view_headers();
331         echo make_email();
332 }
333
334
335 function preview() {
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));
341         unset($preview_tem);
342         tem_set('preview', $preview);
343         set_form_action();
344         tem_output();
345 }
346
347 function download_tar() {
348         $name = $GLOBALS['form_name'];
349         $data = array(
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();
357         }
358         make_wfpl_tar($name, $data);
359 }
360
361
362 metaform();
363 exit();
364
365 ?>