JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
revamped uploaded image handling, added thumbnailing support to metaform
[wfpl.git] / metaform.php
1 <?php
2
3 #  Copyright (C) 2006 Jason Woofenden
4 #
5 #  This program is free software: you can redistribute it and/or modify
6 #  it under the terms of the GNU General Public License as published by
7 #  the Free Software Foundation, either version 3 of the License, or
8 #  (at your option) any later version.
9 #  
10 #  This program is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #  GNU General Public License for more details.
14 #  
15 #  You should have received a copy of the GNU General Public License
16 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18
19 # This file writes the code for you (sql, php, html, email) to handle a form.
20
21 require_once('code/wfpl/template.php');
22 require_once('code/wfpl/http.php');
23 require_once('code/wfpl/tar.php');
24 require_once('code/wfpl/format.php');
25
26 # see code/wfpl/metaform/template.html for the html templates for these elements
27 $GLOBALS['types'] = array(
28 #    type                  input          format        sql     
29         'varname' =>    array('textbox',     'varname',    'varchar(50)'),
30         'name' =>       array('textbox',     'oneline',    'varchar(200)'),
31         'textbox' =>    array('textbox',     'oneline',    'varchar(200)'),
32         'int' =>        array('textbox',     'int',        'int'),
33         'decimal' =>    array('textbox',     'decimal',    'decimal(12,12)'),
34         'bigint' =>     array('textbox',     'int',        'varchar(100)'), # up to 100 digits, stored as a string
35         'zip' =>        array('textbox',     'zip',        'varchar(20)'),
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         'date' =>       array('date',        'mdy_to_ymd', 'char(10)'),
41         'dollars' =>    array('textbox',     'dollars',    'varchar(32)'),
42         'url' =>        array('textbox',     'url',        'varchar(200)'),
43         'hidden' =>     array('hidden',      'unix',       'varchar(200)'),
44         'password' =>   array('password',    'oneline',    'varchar(200)'),
45         'textarea' =>   array('textarea',    'unix',       'text'),
46         'html' =>       array('html',        'unix',       'text'),
47         'pulldown' =>   array('pulldown',    'options',    'varchar(100)'),
48         'radio' =>      array('radio',       'oneline',    'varchar(200)'),
49         'checkbox' =>   array('leftcheck',   'bool',       'int(1)'),
50         'rightcheck' => array('checkbox',    'bool',       'int(1)'),
51         'rightyesno' => array('checkbox',    'yesno',      'varchar(3)'),
52         'yesno' =>      array('leftcheck',   'yesno',      'varchar(3)'),
53         'delete' =>     array('checkbox',    'yesno',      'n/a'),
54         'image' =>      array('image',       'oneline',    'varchar(120)'),
55         'thumb' =>      array('image',       'oneline',    'varchar(240)'),
56         'submit' =>     array('submit',      'oneline',    'n/a')
57 );
58
59 function list_available_types() {
60         ksort($GLOBALS['types']);
61         foreach($GLOBALS['types'] as $key => $value) {
62                 tem_set('type', $key);
63                 tem_show('types');
64                 tem_show('types_sep');
65         }
66 }
67
68
69 function metaform() {
70         if(isset($_REQUEST['singular'])) {
71                 $GLOBALS['file_name'] = format_varname($_REQUEST['file_name']);
72                 $GLOBALS['table_name'] = format_varname($_REQUEST['table_name']);
73                 $GLOBALS['plural'] = format_varname($_REQUEST['plural']);
74                 # backwards compatibility:
75                 if(isset($_REQUEST['form_name'])) {
76                         $GLOBALS['file_name'] = $GLOBALS['table_name'] = $GLOBALS['plural'] = format_varname($_REQUEST['form_name']);
77                 }
78                 tem_set('file_name', $GLOBALS['file_name']);
79                 tem_set('table_name', $GLOBALS['table_name']);
80                 tem_set('plural', $GLOBALS['plural']);
81
82                 $GLOBALS['singular'] = format_varname($_REQUEST['singular']);
83                 tem_set('singular', $GLOBALS['singular']);
84                 $GLOBALS['opt_email'] = format_yesno($_REQUEST['opt_email']);
85                 tem_set('opt_email', $GLOBALS['opt_email']);
86                 $GLOBALS['opt_db'] = format_yesno($_REQUEST['opt_db']);
87                 tem_set('opt_db', $GLOBALS['opt_db']);
88                 $GLOBALS['opt_listing'] = format_yesno($_REQUEST['opt_listing']);
89                 tem_set('opt_listing', $GLOBALS['opt_listing']);
90                 $GLOBALS['opt_display'] = format_yesno($_REQUEST['opt_display']);
91                 tem_set('opt_display', $GLOBALS['opt_display']);
92                 $GLOBALS['opt_http_pass'] = format_yesno($_REQUEST['opt_http_pass']);
93                 tem_set('opt_http_pass', $GLOBALS['opt_http_pass']);
94         }
95
96         if(isset($_REQUEST['fields'])) {
97                 if(isset($_REQUEST['view_sql'])) {
98                         view_sql();
99                         exit();
100                 } elseif(isset($_REQUEST['view_php'])) {
101                         view_php();
102                         exit();
103                 } elseif(isset($_REQUEST['view_html'])) {
104                         view_html();
105                         exit();
106                 } elseif(isset($_REQUEST['view_email'])) {
107                         view_email();
108                         exit();
109                 } elseif(isset($_REQUEST['download_tar'])) {
110                         download_tar();
111                         exit();
112                 } elseif(isset($_REQUEST['preview'])) {
113                         preview();
114                         exit();
115                 } elseif(isset($_REQUEST['edit'])) {
116                         tem_set('fields', $_REQUEST['fields']);
117                         # fall through
118                 } else {
119                         die("Sorry... couldn't tell which button you pressed");
120                 }
121         }
122
123
124         set_form_action();
125         tem_load('code/wfpl/metaform/main.html');
126         list_available_types();
127         tem_output();
128 }
129
130
131 function field_input($type)  { return $GLOBALS['types'][$type][0]; }
132 function field_format($type) { return $GLOBALS['types'][$type][1]; }
133 function field_sql($type)    { return $GLOBALS['types'][$type][2]; }
134
135 function get_fields() {
136         # no sense in doing all this so many times
137         if(isset($GLOBALS['gotten_fields'])) {
138                 return $GLOBALS['gotten_fields'];
139         }
140
141         $fields_str = unix_newlines($_REQUEST['fields']);
142         $GLOBALS['gotten_fields'] = array();
143         $fields_str = rtrim($fields_str);
144         $fields = split("\n", $fields_str);
145         foreach($fields as $field) {
146                 list($name, $type, $options) = split('  *', $field);
147                 if($options) $options = split(',', $options);
148                 if(!$type) $type = $name;
149                 $input = field_input($type);
150                 $format = field_format($type);
151                 $sql = field_sql($type);
152                 $GLOBALS['gotten_fields'][] = array($name, $type, $input, $format, $sql, $options);
153         }
154         return $GLOBALS['gotten_fields'];
155 }
156
157 # this one, that you're using to create forms
158 function set_form_action() {
159         $action = ereg_replace('.*/', '', $_SERVER['REQUEST_URI']);
160         if($action == '') $action = './';
161         tem_set('form_action', $action);
162 }
163
164 # perfect HTTP headers for viewing created files
165 function view_headers() {
166         header('Content-type: text/plain');
167 }
168         
169
170
171
172 function make_sql() {
173         $tem = new tem();
174         $tem->load('code/wfpl/metaform/template.sql');
175         $tem->set('table_name', $GLOBALS['table_name']);
176         $fields = get_fields();
177         foreach($fields as $field) {
178                 list($name, $type, $input, $format, $sql) = $field;
179                 if($sql != 'n/a') {
180                         $tem->set('name', $name);
181                         $tem->set('type', $sql);
182                         if(substr($sql, 0, 3) == 'int' || substr($sql, 0, 7) == 'decimal') {
183                                 $tem->set('default', '0');
184                         } elseif($format == 'yesno') {
185                                 $tem->set('default', '"No"');
186                         } else {
187                                 $tem->set('default', '""');
188                         }
189                         $tem->show('column');
190                 }
191         }
192         view_headers();
193         return $tem->run();
194 }
195
196 function view_sql() {
197         view_headers();
198         echo make_sql();
199 }
200
201 # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
202 function find_always_field($fields) {
203         foreach($fields as $field) {
204                 list($name, $type, $input, $format, $sql) = $field;
205                 if($input != 'submit' && $input != 'checkbox' && $input != 'radio') {
206                         return $name;
207                 }
208         }
209
210         return false;
211 }
212         
213         
214
215 # pass false if you want to exclude the <head> and <body> tag etc.
216 function make_html($whole_file = true) {
217         $uploads_output_already = false;
218         $has_html_editors = false;
219         $tem = new tem();
220         $tem->load('code/wfpl/metaform/template.html');
221         $tem->set('file_name', $GLOBALS['file_name']);
222         $tem->set('table_name', $GLOBALS['table_name']);
223         $tem->set('singular', $GLOBALS['singular']);
224         $tem->set('plural', $GLOBALS['plural']);
225         $fields = get_fields();
226         $tem->set('always_field', find_always_field($fields));
227         foreach($fields as $field) {
228                 list($name, $type, $input, $format, $sql) = $field;
229                 $tem->set('name', $name);
230                 $tem->set('caption', format_caption($name));
231                 $tem->show($input);
232                 if($input != 'hidden') {
233                         $tem->show('row');
234                 }
235
236                 if($input == 'image' && !$uploads_output_already) {
237                         $tem->show('uploads');
238                         $tem->set('enctype_attr', '" enctype="multipart/form-data');
239                         $uploads_output_already = true;
240                 } elseif($input == 'html') {
241                         $has_html_editors = true;
242                         $tem->set('html_field_name', $name);
243                         $tem->show('replace_textarea');
244                 }
245
246                 if($GLOBALS['opt_display'] == 'Yes') {
247                         switch($input) {
248                                 case 'image':
249                                         $tem->show('display_image');
250                                 break;
251                                 case 'checkbox':
252                                 case 'leftcheck':
253                                         $tem->show('display_yesno');
254                                 break;
255                                 case 'date':
256                                         $tem->show('display_date');
257                                 break;
258                                 case 'textarea':
259                                         $tem->show('display_multiline');
260                                 break;
261                                 case 'html':
262                                         $tem->show('display_html');
263                                 break;
264                                 default:
265                                         $tem->show('display_short');
266                         }
267                         $tem->show('display_row');
268                 }
269
270                 if($GLOBALS['opt_listing'] == 'Yes') {
271                         if($GLOBALS['opt_display'] != 'Yes') {
272                                 $tem->show('opt_display_a_else');
273                         }
274                         if(show_in_listing($type, $input, $format, $sql)) {
275                                 if($format == 'bool' || $format == 'yesno') {
276                                         $tem->set('listing_enc', 'yesno');
277                                         $tem->show('listing_value_enc');
278                                 } elseif($input == 'date') {
279                                         $tem->set('listing_enc', 'mmddyyyy');
280                                         $tem->show('listing_value_enc');
281                                 } elseif($type == 'thumb') {
282                                         $tem->show('listing_value_thumb');
283                                 } else {
284                                         $tem->set('listing_enc', 'html');
285                                         $tem->show('listing_value_enc');
286                                 }
287                                 $tem->show('listing_head_col');
288                                 $tem->show('listing_row_col');
289                         }
290                 }
291         }
292
293         if($GLOBALS['opt_db'] == 'Yes') {
294                 $tem->show('opt_db_1');
295                 $tem->show('opt_db_2');
296         } else {
297                 $tem->show('opt_db_1_else');
298         }
299
300         if($GLOBALS['opt_listing'] == 'Yes') {
301                 $tem->show('opt_listing_1');
302         }
303
304         if($GLOBALS['opt_display'] == 'Yes') {
305                 $tem->show('opt_display_1');
306                 $tem->show('opt_display_2');
307         }
308
309         if($GLOBALS['opt_email'] == 'Yes' && $GLOBALS['opt_db'] != 'Yes') {
310                 $tem->set('name', 'send');
311                 $tem->set('caption', 'Send');
312         } else {
313                 $tem->set('name', 'save');
314                 $tem->set('caption', 'Save');
315         }
316         $tem->show('submit');
317         $tem->show('row');
318
319         $tem->show('form');
320
321         if($has_html_editors) {
322                 $tem->show('html_editor_headers');
323         }
324
325         if($whole_file) {
326                 return $tem->run();
327         } else {
328                 return $tem->get('form');
329         }
330 }
331
332 function view_html() {
333         view_headers();
334         echo make_html();
335 }
336
337 function show_in_listing($type, $input, $format, $sql) {
338         switch($input) {
339                 case 'submit':
340                 case 'hidden':
341                 case 'password':
342                 case 'textarea':
343                 case 'html':
344                         return false;
345         }
346         if($type == 'image') {
347                 return false;
348         }
349
350         return true;
351 }
352
353 function make_php() {
354         $tem = new tem();
355         $tem->load('code/wfpl/metaform/template.php');
356         $tem->set('file_name', $GLOBALS['file_name']);
357         $tem->set('table_name', $GLOBALS['table_name']);
358         $tem->set('singular', $GLOBALS['singular']);
359         $tem->set('plural', $GLOBALS['plural']);
360         $fields = get_fields();
361         $db_fields = '';
362         $php_fields = '';
363         $always_field = find_always_field($fields);
364         $image_included_yet = false;
365         foreach($fields as $field) {
366                 list($name, $type, $input, $format, $sql) = $field;
367                 if($input != 'submit') {
368                         $tem->set('format', $format);
369                         $tem->set('name', $name);
370                         $tem->set('db_field', ''); # we don't want to use the value from last time
371                         if($sql != 'n/a') {
372                                 if($db_fields != '') $db_fields .= ',';
373                                 $db_fields .= $name;
374                                 if($php_fields != '') $php_fields .= ', ';
375                                 $php_fields .= '$' . $name;
376                         }
377                         if($input == 'image') {
378                                 if($type == 'thumb') {
379                                         $tem->show('thumb_settings');
380                                         $tem->show('thumb_upload_params');
381                                         $tem->show('thumb_w_h');
382                                 }
383                                 $tem->show('image_settings');
384                                 $tem->show('image_upload');
385                                 if(!$image_included_yet) {
386                                         $tem->show('image_include');
387                                         $tem->show('upload_max');
388                                         $tem->show('upload_settings');
389                                         $image_included_yet = true;
390                                 }
391                         } else {
392                                 if($input == 'pulldown') {
393                                         $tem->show('pulldowns');
394                                         $tem->show('pulldown_format_extra');
395                                 }
396                                 $tem->show('formats');
397                         }
398                         $tem->show('tem_sets');
399                 }
400
401                 if($GLOBALS['opt_listing'] == 'Yes') {
402                         if(show_in_listing($type, $input, $format, $sql)) {
403                                 $tem->show('listing_fields_1');
404                                 $tem->show('listing_fields_2');
405                         }
406                 }
407         }
408
409         $tem->set('always_field', $always_field);
410         $tem->set('db_fields', $db_fields);
411         $tem->set('php_fields', $php_fields);
412         $tem->set('metaform_url', edit_url());
413         if($GLOBALS['opt_listing'] == 'Yes') {
414                 $tem->show('opt_listing_1');
415                 $tem->show('opt_listing_2');
416         }
417         if($GLOBALS['opt_display'] == 'Yes') {
418                 $tem->show('opt_display_1');
419                 $tem->show('opt_display_2');
420         } else {
421                 $tem->show('opt_display_1_else');
422                 $tem->show('opt_display_2_else');
423         }
424         if($GLOBALS['opt_db'] == 'Yes') {
425                 $tem->show('opt_db_1');
426                 $tem->show('opt_db_2');
427                 $tem->show('opt_db_3');
428                 $tem->show('opt_db_4');
429                 $tem->show('opt_db_5');
430         }
431         if($GLOBALS['opt_email'] == 'Yes') {
432                 $tem->show('opt_email_1');
433                 $tem->show('opt_email_2');
434         }
435         if($GLOBALS['opt_http_pass'] == 'Yes') {
436                 $tem->show('opt_http_pass_1');
437                 $tem->show('opt_http_pass_2');
438         }
439         return $tem->run();
440 }
441
442 # make a URL for the edit page with all the fields filled in
443 function edit_url() {
444         $url = this_url();
445         $url = ereg_replace('view_php=[^&]*', 'edit=yes', $url);
446         $url = ereg_replace('download_tar=[^&]*', 'edit=yes', $url);
447         $url = ereg_replace('/[a-z0-9_.]*\?', '/?', $url);
448         $url = str_replace('jasonwoof.l', 'jasonwoof.com', $url); # so that code generated on Jason's home computer will display a publically accessible link.
449         return $url;
450 }
451
452 function view_php() {
453         view_headers();
454         echo make_php();
455 }
456
457 function make_email() {
458         $tem = new tem();
459         $tem->load('code/wfpl/metaform/template.email.txt');
460         $tem->set('file_name', $GLOBALS['file_name']);
461         $tem->set('table_name', $GLOBALS['table_name']);
462         $tem->set('singular', $GLOBALS['singular']);
463         $tem->set('plural', $GLOBALS['plural']);
464         $fields = get_fields();
465         foreach($fields as $field) {
466                 list($name, $type, $input, $format, $sql) = $field;
467                 $tem->set('name', $name);
468                 $tem->set('caption', $name); # fixme
469                 if($type == 'textarea') {
470                         $tem->show('multi_line');
471                 } elseif($type == 'checkbox') {
472                         $tem->show('checkbox');
473                 } else {
474                         $tem->show('normal');
475                 }
476                 $tem->show('fields');
477         }
478         return $tem->run();
479 }
480
481 function make_htaccess() {
482         $tem = new tem();
483         $tem->set('form', $GLOBALS['file_name']);
484         return $tem->run('code/wfpl/metaform/htaccess');
485 }
486
487 function view_email() {
488         view_headers();
489         echo make_email();
490 }
491
492 function preview() {
493         tem_load('code/wfpl/metaform/preview.html');
494         tem_set('file_name', $GLOBALS['file_name']);
495         tem_set('table_name', $GLOBALS['table_name']);
496         tem_set('singular', $GLOBALS['singular']);
497         tem_set('plural', $GLOBALS['plural']);
498         tem_set('fields', $_REQUEST['fields']);
499         $preview_tem = new tem();
500         $preview_tem->load_str(make_html(false));
501         if($GLOBALS['opt_db'] == 'Yes') {
502                 $preview_tem->show('new_msg');
503         }
504         $fields = get_fields();
505         foreach($fields as $field) {
506                 list($name, $type, $input, $format, $sql) = $field;
507                 if($type == 'pulldown') {
508                         pulldown($name, array('option 1', 'option 2', 'option 3'));
509                 }
510         }
511         $preview = $preview_tem->run();
512         unset($preview_tem);
513         $preview = ereg_replace('type="submit"', 'type="submit" disabled="disabled"', $preview);
514         tem_set('preview', $preview);
515         tem_show('hiddens');
516         set_form_action();
517         tem_output();
518 }
519
520 function download_tar() {
521         $name = $GLOBALS['file_name'];
522         $data = array(
523                 ".htaccess" => make_htaccess(),
524                 "run.php ->" => 'code/wfpl/run.php',
525                 "style.css" => read_whole_file('code/wfpl/metaform/style.css'),
526                 "$name.html" => make_html(),
527                 "$name.php" => make_php());
528         if($GLOBALS['opt_db'] == 'Yes') {
529                 $data["$name.sql"] = make_sql();
530         }
531         if($GLOBALS['opt_email'] == 'Yes') {
532                 $data["$name.email.txt"] = make_email();
533         }
534         make_tar($name, $data);
535 }
536
537
538 metaform();
539 exit();