JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
metaform e-mails sort multi-line fields properly
[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         'varname' =>    array('textbox',     'varname',    'varchar(50)'),
33         'name' =>       array('textbox',     'oneline',    'varchar(200)'),
34         'textbox' =>    array('textbox',     'oneline',    'varchar(200)'),
35         'int' =>        array('textbox',     'int',        'int'),
36         'decimal' =>    array('textbox',     'decimal',    'decimal(12,12)'),
37         'bigint' =>     array('textbox',     'int',        'varchar(100)'), # up to 100 digits, stored as a string
38         'zip' =>        array('textbox',     'zip',        'varchar(20)'),
39         'email' =>      array('textbox',     'email',      'varchar(100)'),
40         'phone' =>      array('textbox',     'phone',      'varchar(32)'),
41         'state' =>      array('states',      'oneline',    'varchar(2)'),
42         'money' =>      array('textbox',     'money',      'varchar(32)'),
43         'date' =>       array('textbox',     'mdy_to_ymd', 'char(10)'),
44         'dollars' =>    array('textbox',     'dollars',    'varchar(32)'),
45         'url' =>        array('textbox',     'url',        'varchar(200)'),
46         'hidden' =>     array('hidden',      'unix',       'varchar(200)'),
47         'password' =>   array('password',    'oneline',    'varchar(200)'),
48         'textarea' =>   array('textarea',    'unix',       'text'),
49         'html' =>       array('html',        'unix',       'text'),
50         'pulldown' =>   array('pulldown',    'options',    'varchar(100)'),
51         'radio' =>      array('radio',       'oneline',    'varchar(200)'),
52         'leftcheck' =>  array('leftcheck',   'yesno',      'varchar(3)'),
53         'checkbox' =>   array('checkbox',    'yesno',      'varchar(3)'),
54         'yesno' =>      array('checkbox',    'yesno',      'varchar(3)'),
55         'delete' =>     array('checkbox',    'yesno',      'n/a'),
56         'image' =>      array('image',       'oneline',    'varchar(200)'),
57         'submit' =>     array('submit',      'oneline',    'n/a')
58 );
59
60 function list_available_types() {
61         $types = '';
62         foreach($GLOBALS['types'] as $key => $value) {
63                 if($types) {
64                         $types .= ', ';
65                 }
66                 $types .= $key;
67         }
68         tem_set('available_types', $types);
69 }
70
71
72 function metaform() {
73         if(isset($_REQUEST['form_name'])) {
74                 $GLOBALS['form_name'] = ereg_replace('[^a-z0-9_-]', '', $_REQUEST['form_name']);
75                 $GLOBALS['opt_email'] = format_yesno($_REQUEST['opt_email']);
76                 tem_set('opt_email', $GLOBALS['opt_email']);
77                 $GLOBALS['opt_db'] = format_yesno($_REQUEST['opt_db']);
78                 tem_set('opt_db', $GLOBALS['opt_db']);
79                 $GLOBALS['opt_listing'] = format_yesno($_REQUEST['opt_listing']);
80                 tem_set('opt_listing', $GLOBALS['opt_listing']);
81                 $GLOBALS['opt_http_pass'] = format_yesno($_REQUEST['opt_http_pass']);
82                 tem_set('opt_http_pass', $GLOBALS['opt_http_pass']);
83         } else {
84                 $GLOBALS['form_name'] = 'some_form';
85         }
86
87         if(isset($_REQUEST['fields'])) {
88                 if(isset($_REQUEST['view_sql'])) {
89                         view_sql();
90                         exit();
91                 } elseif(isset($_REQUEST['view_php'])) {
92                         view_php();
93                         exit();
94                 } elseif(isset($_REQUEST['view_html'])) {
95                         view_html();
96                         exit();
97                 } elseif(isset($_REQUEST['view_email'])) {
98                         view_email();
99                         exit();
100                 } elseif(isset($_REQUEST['download_tar'])) {
101                         download_tar();
102                         exit();
103                 } elseif(isset($_REQUEST['preview'])) {
104                         preview();
105                         exit();
106                 } elseif(isset($_REQUEST['edit'])) {
107                         tem_set('fields', $_REQUEST['fields']);
108                         tem_set('form_name', $GLOBALS['form_name']);
109                         # fall through
110                 } else {
111                         die("Sorry... couldn't tell which button you pressed");
112                 }
113         }
114
115
116         set_form_action();
117         list_available_types();
118         tem_output('code/wfpl/metaform/main.html');
119 }
120
121
122 function field_input($type)  { return $GLOBALS['types'][$type][0]; }
123 function field_format($type) { return $GLOBALS['types'][$type][1]; }
124 function field_sql($type)    { return $GLOBALS['types'][$type][2]; }
125
126 function get_fields() {
127         # no sense in doing all this so many times
128         if(isset($GLOBALS['gotten_fields'])) {
129                 return $GLOBALS['gotten_fields'];
130         }
131
132         $fields_str = unix_newlines($_REQUEST['fields']);
133         $GLOBALS['gotten_fields'] = array();
134         $fields_str = rtrim($fields_str);
135         $fields = split("\n", $fields_str);
136         foreach($fields as $field) {
137                 list($name, $type, $options) = split('  *', $field);
138                 if($options) $options = split(',', $options);
139                 if(!$type) $type = $name;
140                 $input = field_input($type);
141                 $format = field_format($type);
142                 $sql = field_sql($type);
143                 $GLOBALS['gotten_fields'][] = array($name, $type, $input, $format, $sql, $options);
144         }
145         return $GLOBALS['gotten_fields'];
146 }
147
148 # this one, that you're using to create forms
149 function set_form_action() {
150         $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
151         if($action == '') $action = './';
152         tem_set('form_action', $action);
153 }
154
155 # perfect HTTP headers for viewing created files
156 function view_headers() {
157         header('Content-type: text/plain');
158 }
159         
160
161
162
163 function make_sql() {
164         $tem = new tem();
165         $tem->load('code/wfpl/metaform/template.sql');
166         $tem->set('form_name', $GLOBALS['form_name']);
167         $fields = get_fields();
168         foreach($fields as $field) {
169                 list($name, $type, $input, $format, $sql) = $field;
170                 if($sql != 'n/a') {
171                         $tem->set('name', $name);
172                         $tem->set('type', $sql);
173                         if($sql == 'int') {
174                                 $tem->set('default', '0');
175                         } elseif($format == 'yesno') {
176                                 $tem->set('default', '"No"');
177                         } else {
178                                 $tem->set('default', '""');
179                         }
180                         $tem->show('column');
181                 }
182         }
183         view_headers();
184         return $tem->run();
185 }
186
187 function view_sql() {
188         view_headers();
189         echo make_sql();
190 }
191
192 # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
193 function find_always_field($fields) {
194         foreach($fields as $field) {
195                 list($name, $type, $input, $format, $sql) = $field;
196                 if($input != 'submit' && $input != 'checkbox' && $input != 'radio') {
197                         return $name;
198                 }
199         }
200
201         return false;
202 }
203         
204         
205
206 # pass false if you want to exclude the <head> and <body> tag etc.
207 function make_html($whole_file = true) {
208         $uploads_output_already = false;
209         $has_html_editors = false;
210         $tem = new tem();
211         $tem->load('code/wfpl/metaform/template.html');
212         $tem->set('form_name', $GLOBALS['form_name']);
213         $fields = get_fields();
214         $tem->set('always_field', find_always_field($fields));
215         foreach($fields as $field) {
216                 list($name, $type, $input, $format, $sql) = $field;
217                 $tem->set('name', $name);
218                 $tem->set('caption', format_caption($name));
219                 $tem->show($input);
220                 if($input != 'hidden') {
221                         $tem->show('row');
222                 }
223                 if($input == 'image' && !$uploads_output_already) {
224                         $tem->show('uploads');
225                         $tem->set('enctype_attr', '" enctype="multipart/form-data');
226                         $uploads_output_already = true;
227                 } elseif($input == 'html') {
228                         $has_html_editors = true;
229                         $tem->set('html_field_name', $name);
230                         $tem->show('replace_textarea');
231                 }
232         }
233
234         if($GLOBALS['opt_db'] == 'Yes') {
235                 $tem->show('opt_db_1');
236                 $tem->show('opt_db_2');
237         } else {
238                 $tem->show('opt_db_1_else');
239         }
240
241         if($GLOBALS['opt_listing'] == 'Yes') {
242                 $tem->show('opt_listing_1');
243         } else {
244                 $tem->show('opt_listing_1_else');
245         }
246
247         if($GLOBALS['opt_email'] == 'Yes' && $GLOBALS['opt_db'] != 'Yes') {
248                 $tem->set('name', 'send');
249                 $tem->set('caption', 'Send');
250         } else {
251                 $tem->set('name', 'save');
252                 $tem->set('caption', 'Save');
253         }
254         $tem->show('submit');
255         $tem->show('row');
256
257         $tem->show('form');
258
259         if($has_html_editors) {
260                 $tem->show('html_editor_headers');
261         }
262
263         if($whole_file) {
264                 return $tem->run();
265         } else {
266                 return $tem->get('form');
267         }
268 }
269
270 function view_html() {
271         view_headers();
272         echo make_html();
273 }
274
275
276 function make_php() {
277         $tem = new tem();
278         $tem->load('code/wfpl/metaform/template.php');
279         $tem->set('form_name', $GLOBALS['form_name']);
280         $fields = get_fields();
281         $db_fields = '';
282         $php_fields = '';
283         $always_field = find_always_field($fields);
284         $image_included_yet = false;
285         foreach($fields as $field) {
286                 list($name, $type, $input, $format, $sql) = $field;
287                 if($input != 'submit') {
288                         $tem->set('format', $format);
289                         $tem->set('name', $name);
290                         $tem->set('db_field', ''); # we don't want to use the value from last time
291                         if($sql != 'n/a') {
292                                 if($db_fields != '') $db_fields .= ',';
293                                 $db_fields .= $name;
294                                 if($php_fields != '') $php_fields .= ', ';
295                                 $php_fields .= '$' . $name;
296                         }
297                         if($input == 'image') {
298                                 $tem->show('image_upload');
299                                 $tem->show('image_db');
300                                 if(!$image_included_yet) {
301                                         $tem->show('image_include');
302                                         $tem->show('upload_max');
303                                         $tem->show('upload_settings');
304                                         $image_included_yet = true;
305                                 }
306                         } else {
307                                 if($input == 'pulldown') {
308                                         $tem->show('pulldowns');
309                                         $tem->show('pulldown_format_extra');
310                                 }
311                                 $tem->show('formats');
312                         }
313                         $tem->show('tem_sets');
314                 }
315         }
316
317         $tem->set('always_field', $always_field);
318         $tem->set('db_fields', $db_fields);
319         $tem->set('php_fields', $php_fields);
320         $tem->set('metaform_url', edit_url());
321         if($GLOBALS['opt_listing'] == 'Yes') {
322                 $tem->show('opt_listing_1');
323                 $tem->show('opt_listing_2');
324                 $tem->show('opt_listing_3');
325                 $tem->show('opt_listing_4');
326         } else {
327                 $tem->show('opt_listing_3_else');
328                 $tem->show('opt_listing_4_else');
329         }
330         if($GLOBALS['opt_db'] == 'Yes') {
331                 $tem->show('opt_db_1');
332                 $tem->show('opt_db_2');
333                 $tem->show('opt_db_3');
334                 $tem->show('opt_db_4');
335                 $tem->show('opt_db_5');
336         }
337         if($GLOBALS['opt_email'] == 'Yes') {
338                 $tem->show('opt_email_1');
339                 $tem->show('opt_email_2');
340         }
341         if($GLOBALS['opt_http_pass'] == 'Yes') {
342                 $tem->show('opt_http_pass_1');
343                 $tem->show('opt_http_pass_2');
344         }
345         return $tem->run();
346 }
347
348 # make a URL for the edit page with all the fields filled in
349 function edit_url() {
350         $url = this_url();
351         $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
352         $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
353         $url = ereg_replace('/[a-z0-9_.]*\?', '/?', $url);
354         $url = str_replace('jasonwoof.l', 'jasonwoof.com', $url); # so that code generated on Jason's home computer will display a publically accessible link.
355         return $url;
356 }
357
358 function view_php() {
359         view_headers();
360         echo make_php();
361 }
362
363
364 function make_email() {
365         $tem = new tem();
366         $tem->load('code/wfpl/metaform/template.email.txt');
367         $tem->set('form_name', $GLOBALS['form_name']);
368         $fields = get_fields();
369         foreach($fields as $field) {
370                 list($name, $type, $input, $format, $sql) = $field;
371                 $tem->set('name', $name);
372                 $tem->set('caption', $name); # fixme
373                 if($type == 'textarea') {
374                         $tem->show('multi_line');
375                 } else {
376                         $tem->show('normal');
377                 }
378                 $tem->show('fields');
379         }
380         return $tem->run();
381 }
382
383 function make_htaccess() {
384         $tem = new tem();
385         $tem->set('form', $GLOBALS['form_name']);
386         return $tem->run('code/wfpl/metaform/htaccess');
387 }
388
389 function view_email() {
390         view_headers();
391         echo make_email();
392 }
393
394
395 function preview() {
396         tem_load('code/wfpl/metaform/preview.html');
397         tem_set('form_name', $GLOBALS['form_name']);
398         tem_set('fields', $_REQUEST['fields']);
399         $preview_tem = new tem();
400         $preview_tem->load_str(make_html(false));
401         if($GLOBALS['opt_db'] == 'Yes') {
402                 $preview_tem->show('new_msg');
403         }
404         $fields = get_fields();
405         foreach($fields as $field) {
406                 list($name, $type, $input, $format, $sql) = $field;
407                 if($type == 'pulldown') {
408                         pulldown($name, array('option 1', 'option 2', 'option 3'));
409                 }
410         }
411         $preview = $preview_tem->run();
412         unset($preview_tem);
413         tem_set('preview', $preview);
414         set_form_action();
415         tem_output();
416 }
417
418 function download_tar() {
419         $name = $GLOBALS['form_name'];
420         $data = array(
421                 ".htaccess" => make_htaccess(),
422                 "run.php ->" => 'code/wfpl/run.php',
423                 "style.css" => read_whole_file('code/wfpl/metaform/style.css'),
424                 "$name.html" => make_html(),
425                 "$name.php" => make_php());
426         if($GLOBALS['opt_db'] == 'Yes') {
427                 $data["$name.sql"] = make_sql();
428         }
429         if($GLOBALS['opt_email'] == 'Yes') {
430                 $data["$name.email.txt"] = make_email();
431         }
432         make_wfpl_tar($name, $data);
433 }
434
435
436 metaform();
437 exit();
438
439 ?>