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