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