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