JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
metaform: merge ckeditor settings from cms
[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(__DIR__.'/'.'template.php');
22 require_once(__DIR__.'/'.'http.php');
23 require_once(__DIR__.'/'.'tar.php');
24 require_once(__DIR__.'/'.'format.php');
25
26 # see 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) binary'),
30         'name' =>       array('textbox',     'oneline',    'varchar(200) binary'),
31         'textbox' =>    array('textbox',     'oneline',    'varchar(200) binary'),
32         'int' =>        array('textbox',     'int',        'int'),
33         'decimal' =>    array('textbox',     'decimal',    'decimal(12,2)'),
34         'bigint' =>     array('textbox',     'int',        'varchar(100) binary'), # up to 100 digits, stored as a string
35         'zip' =>        array('textbox',     'zip',        'varchar(20) binary'),
36         'email' =>      array('email',       'email',      'varchar(100) binary'),
37         'search' =>     array('search',      'oneline',    'varchar(200) binary'),
38         'phone' =>      array('textbox',     'phone',      'varchar(32) binary'),
39         'state' =>      array('states',      'oneline',    'varchar(2) binary'),
40         'money' =>      array('textbox',     'money',      'varchar(32) binary'),
41         'date' =>       array('date',        'mdy_to_ymd', 'char(10) binary'),
42         'dollars' =>    array('textbox',     'dollars',    'varchar(32) binary'),
43         'url' =>        array('textbox',     'url',        'varchar(200) binary'),
44         'hidden' =>     array('hidden',      'unix',       'varchar(200) binary'),
45         'password' =>   array('password',    'oneline',    'varchar(200) binary'),
46         'textarea' =>   array('textarea',    'unix',       'text binary'),
47         'html' =>       array('html',        'unix',       'mediumtext binary'),
48         'pulldown' =>   array('pulldown',    'options',    'varchar(100) binary'),
49         'radio' =>      array('radio',       'options',    'varchar(100) binary'),
50         'checkbox' =>   array('checkbox',    'bool',       'int(1)'),
51         'yesno' =>      array('checkbox',    'yesno',      'varchar(3) binary'),
52         'delete' =>     array('checkbox',    'yesno',      'n/a'),
53         'image' =>      array('image',       'oneline',    'varchar(120) binary'),
54         'thumb' =>      array('image',       'oneline',    'varchar(240) binary'),
55         'file' =>       array('file',        'oneline',    'varchar(100) binary'),
56         'submit' =>     array('submit',      'n/a',        'n/a'),
57         '{' =>          array('fieldset',    'n/a',        'n/a'),
58         '}' =>          array('end_fieldset','n/a',        'n/a')
59 );
60
61 function list_available_types() {
62         ksort($GLOBALS['types']);
63         foreach($GLOBALS['types'] as $key => $value) {
64                 tem_set('type', $key);
65                 tem_show('types');
66                 tem_show('types_sep');
67         }
68 }
69
70
71 function tem_set_globals(&$tem) {
72         $vars = array(
73                 'file_name',
74                 'public_file_name',
75                 'table_name',
76                 'plural',
77                 'singular');
78         foreach($vars as $var) {
79                 $tem->set($var, $GLOBALS[$var]);
80         }
81
82         $bools = array(
83                 'opt_email',
84                 'opt_db',
85                 'opt_listing',
86                 'opt_display',
87                 'opt_pass',
88                 'opt_public_form',
89                 'opt_public_display',
90                 'opt_public_something');
91         foreach($bools as $bool) {
92                 if($GLOBALS[$bool]) {
93                         $tem->set($bool);
94                 }
95         }
96 }
97
98 function metaform_main() {
99         if(isset($_REQUEST['singular'])) {
100                 $GLOBALS['file_name'] = format_varname($_REQUEST['file_name']);
101                 $GLOBALS['table_name'] = format_varname($_REQUEST['table_name']);
102                 $GLOBALS['plural'] = format_oneline($_REQUEST['plural']);
103                 # backwards compatibility:
104                 if(isset($_REQUEST['form_name'])) {
105                         $GLOBALS['file_name'] = $GLOBALS['table_name'] = $GLOBALS['plural'] = format_varname($_REQUEST['form_name']);
106                 }
107
108                 $GLOBALS['singular'] = format_oneline($_REQUEST['singular']);
109                 $GLOBALS['opt_email'] = format_bool($_REQUEST['opt_email']);
110                 $GLOBALS['opt_db'] = format_bool($_REQUEST['opt_db']);
111                 $GLOBALS['opt_listing'] = format_bool($_REQUEST['opt_listing']);
112                 $GLOBALS['opt_display'] = format_bool($_REQUEST['opt_display']);
113                 $GLOBALS['opt_pass'] = format_bool($_REQUEST['opt_pass']);
114                 $GLOBALS['opt_public_form'] = format_bool($_REQUEST['opt_public_form']);
115                 $GLOBALS['opt_public_display'] = format_bool($_REQUEST['opt_public_display']);
116
117                 $GLOBALS['public_file_name'] = $GLOBALS['file_name'];
118                 if($GLOBALS['opt_public_form'] || $GLOBALS['opt_public_display']) {
119                         $GLOBALS['opt_public_something'] = 1;
120                         $GLOBALS['file_name'] = $GLOBALS['file_name'] . _admin;
121                 }
122
123                 tem_init();
124                 tem_set_globals($GLOBALS['wfpl_template']);
125         }
126
127         if(isset($_REQUEST['fields'])) {
128                 if(isset($_REQUEST['view_sql'])) {
129                         view_sql();
130                         exit();
131                 } elseif(isset($_REQUEST['view_php'])) {
132                         view_php();
133                         exit();
134                 } elseif(isset($_REQUEST['view_html'])) {
135                         view_html();
136                         exit();
137                 } elseif(isset($_REQUEST['view_email'])) {
138                         view_email();
139                         exit();
140                 } elseif(isset($_REQUEST['download_tar'])) {
141                         download_tar();
142                         exit();
143                 } elseif(isset($_REQUEST['preview'])) {
144                         preview();
145                         exit();
146                 } elseif(isset($_REQUEST['edit'])) {
147                         tem_set('fields', $_REQUEST['fields']);
148                         # fall through
149                 } else {
150                         die("Sorry... couldn't tell which button you pressed");
151                 }
152         }
153
154
155         set_form_action();
156         tem_load(__DIR__.'/'.'metaform/main.html');
157         list_available_types();
158         tem_output();
159         exit(); # in case we're being called by wfpl_main or something
160 }
161
162
163 function field_input($type)  { return $GLOBALS['types'][$type][0]; }
164 function field_format($type) { return $GLOBALS['types'][$type][1]; }
165 function field_sql($type)    { return $GLOBALS['types'][$type][2]; }
166
167 function get_fields() {
168         # no sense in doing all this so many times
169         if(isset($GLOBALS['gotten_fields'])) {
170                 return $GLOBALS['gotten_fields'];
171         }
172
173         $fields_str = unix_newlines($_REQUEST['fields']);
174         $GLOBALS['gotten_fields'] = array();
175         $fields_str = rtrim($fields_str);
176         $fields = split("\n", $fields_str);
177         foreach($fields as $field) {
178                 $first_char = substr($field, 0, 1);
179                 $field = trim($field);
180                 if ($first_char === ' ' || $first_char === "\t") {
181                         $i = count($GLOBALS['gotten_fields']);
182                         if($i > 0) {
183                                 $i -= 1;
184                                 if (!$GLOBALS['gotten_fields'][$i]['options']) {
185                                         $GLOBALS['gotten_fields'][$i]['options'] = array();
186                                 }
187                                 $GLOBALS['gotten_fields'][$i]['options'][] = $field;
188                                 continue;
189                         }
190                 }
191                 if(substr($field, -1) == '{') {
192                         $caption = trim(substr($field, 0, -1));
193                         $name = format_varname($caption);
194                         $type = '{';
195                         $options = null;
196                 } else {
197                         $options = null;
198                         $type = null;
199                         $div = strpos($field, ' ');
200                         if ($div === false) {
201                                 $div = strpos($field, "\t");
202                         }
203                         if ($div === false) {
204                                 if (isset($GLOBALS['types'][$field])) {
205                                         # if just one word, and it's a type, use it as name/caption and type
206                                         $type = $field;
207                                 }
208                         } else {
209                                 $first_word = trim(substr($field, 0, $div));
210                                 if (isset($GLOBALS['types'][$first_word])) {
211                                         # if the first word (of multiple) is a type, remove it from the name/caption
212                                         $type = $first_word;
213                                         $field = trim(substr($field, $div + 1));
214                                 }
215                         }
216                         # see if there's an explicit name/caption splitter
217                         $div = strpos($field, '|');
218                         if ($div !== false) {
219                                 $name = trim(substr($field, 0, $div));
220                                 $caption = trim(substr($field, $div + 1));
221                                 if (isset($GLOBALS['types'][$name])) {
222                                         $type = $name;
223                                 } elseif (isset($GLOBALS['types'][strtolower($caption)])) {
224                                         $type = strtolower($caption);
225                                 }
226                         } else {
227                                 $name = format_varname($field);
228                                 $caption = format_caption($field);
229                         }
230
231                         if ($type === null) {
232                                 $type = 'textbox';
233                         }
234                 }
235                 $input = field_input($type);
236                 $format = field_format($type);
237                 $sql = field_sql($type);
238                 $GLOBALS['gotten_fields'][] = array(
239                         'caption' => $caption,
240                         'name' => $name,
241                         'type' => $type,
242                         'input' => $input,
243                         'format' => $format,
244                         'sql' => $sql,
245                         'options' => $options
246                 );
247         }
248
249         return $GLOBALS['gotten_fields'];
250 }
251
252 # this one, that you're using to create forms
253 function set_form_action() {
254         $action = preg_replace('|.*/|', '', $_SERVER['REQUEST_URI']);
255         if($action == '') $action = './';
256         tem_set('form_action', $action);
257 }
258
259 # perfect HTTP headers for viewing created files
260 function view_headers() {
261         header('Content-type: text/plain');
262 }
263         
264
265
266
267 function make_sql() {
268         $tem = new tem();
269         $tem->load(__DIR__.'/'.'metaform/template.sql');
270         tem_set_globals($tem);
271         $fields = get_fields();
272         foreach($fields as $field) {
273                 if($field['sql'] != 'n/a') {
274                         $tem->set('name', $field['name']);
275                         $tem->set('type', $field['sql']);
276                         if(substr($field['sql'], 0, 3) == 'int' || substr($field['sql'], 0, 7) == 'decimal') {
277                                 $tem->set('default', '0');
278                         } elseif($field['format'] == 'yesno') {
279                                 $tem->set('default', '"No"');
280                         } else {
281                                 $tem->set('default', '""');
282                         }
283                         $tem->show('column');
284                 }
285         }
286         view_headers();
287         return $tem->run();
288 }
289
290 function view_sql() {
291         view_headers();
292         echo make_sql();
293 }
294
295 # always_field is a form field that always submits (unlike say, checkboxes). It's used to detect if the form has submitted or not.
296 function find_always_field(&$fields) {
297         foreach($fields as $field) {
298                 if($field['input'] != 'submit' && $field['input'] != 'image' && $field['input'] != 'file' && $field['input'] != 'checkbox' && $field['input'] != 'radio' && $field['type'] != '{' && $field['type'] != '}') {
299                         return $field['name'];
300                 }
301         }
302
303         return 'FIXME';
304 }
305         
306         
307
308 # pass false if you want to exclude the <head> and <body> tag etc.
309 function make_html($whole_file = true) {
310         $has_html_editors = false;
311         $tem = new tem();
312         $tem->load(__DIR__.'/'.'metaform/template.html');
313         tem_set_globals($tem);
314         $fields = get_fields();
315         $tem->set('always_field', find_always_field($fields));
316         $hidden_fields = array();
317         $visible_fields = array();
318         $display_fields = array();
319         $listing_headers = array();
320         $listing_fields = array();
321         foreach($fields as $field) {
322                 if($field['input'] == 'hidden') {
323                         $hidden_fields[] = array('name' => $field['name']);
324                 } else {
325                         $visible_fields[] = array(
326                                 $field['input'] => array(
327                                         'name' => $field['name'],
328                                         'caption' => $field['caption']
329                                 )
330                         );
331                         if($field['type'] == 'radio') {
332                                 $i = 0;
333                                 $opts = array();
334                                 foreach ($field['options'] as $row) {
335                                         if (is_array($row)) {
336                                                 $cap = $row[1];
337                                         } else {
338                                                 $cap = $row;
339                                         }
340                                         $opts[] = array('i' => $i, 'option_caption' => $cap);
341                                         $i += 1;
342                                 }
343                                 $visible_fields[count($visible_fields) - 1]['options'] = $opts;
344                         }
345                 }
346
347                 if($field['input'] == 'image' || $field['input'] == 'file') {
348                         $tem->set('uploads');
349                         $tem->set('enctype_attr', '" enctype="multipart/form-data');
350                 } elseif($field['input'] == 'html') {
351                         $has_html_editors = true;
352                         $tem->set('html_field_name', $field['name']);
353                         $tem->set('replace_textarea');
354                 }
355
356                 switch($field['input']) {
357                         case 'image':
358                         case 'checkbox':
359                         case 'date':
360                         case 'textarea':
361                         case 'html':
362                                 $display_type = $field['input'];
363                         break;
364                         default:
365                                 $display_type = 'short';
366                 }
367                 if($field['format'] != 'n/a') {
368                         $display_fields[] = array($display_type => array(
369                                 'name' => $field['name'], 'caption' => $field['caption']));
370                 }
371
372                 if(show_in_listing($field['type'], $field['input'], $field['format'], $field['sql'])) {
373                         $listing_headers[] = array(
374                                 'caption' => $field['caption'],
375                                 'name' => $field['name']
376                         );
377                         $listing_field = array(
378                                 'name' => $field['name']
379                         );
380                         if($field['format'] == 'bool' || $field['format'] == 'yesno') {
381                                 $listing_field['enc'] = 'yesno';
382                         } elseif($field['input'] == 'date') {
383                                 $listing_field['enc'] = 'mmddyyyy';
384                         } elseif($field['type'] == 'thumb') {
385                                 $listing_field['thumb'] = true;
386                         } else {
387                                 $listing_field['enc'] = 'html';
388                         }
389                         $listing_fields[] = $listing_field;
390                 }
391         }
392
393         # Submit/Send button
394         if($GLOBALS['opt_email'] == 'Yes' && !$GLOBALS['opt_db']) {
395                 $visible_fields[] = array('submit' => array(
396                         'name' => 'send',
397                         'caption' => 'Send'));
398         } else {
399                 $visible_fields[] = array('submit' => array(
400                         'name' => 'save',
401                         'caption' => 'Save'));
402         }
403
404         $form_fields = array();
405         $form_fields['visible_fields'] = $visible_fields;
406         if($hidden_fields) {
407                 $form_fields['hidden_fields'] = $hidden_fields;
408         }
409         $tem->set('form', $form_fields);
410
411         # opt_display and opt_listing control whether these are actually displayed
412         $tem->set('display_fields', $display_fields);
413         $tem->set('listing_headers', $listing_headers);
414         $tem->set('listing_fields', $listing_fields);
415
416
417         if($has_html_editors) {
418                 $tem->set('html_editor_headers');
419         }
420
421         if($whole_file) {
422                 return $tem->run();
423         } else {
424                 $tem2 = new tem();
425                 $tem2->load_str('<!--~form~-->');
426                 $tem2->merge($tem);
427                 return $tem2->run();
428         }
429 }
430
431 function view_html() {
432         view_headers();
433         echo make_html();
434 }
435
436 function show_in_listing($type, $input, $format, $sql) {
437         switch($input) {
438                 case 'submit':
439                 case 'hidden':
440                 case 'password':
441                 case 'textarea':
442                 case 'html':
443                 case 'fieldset':
444                 case 'end_fieldset':
445                         return false;
446         }
447         if($type == 'image') {
448                 return false;
449         }
450
451         return true;
452 }
453
454 function pulldown_options_array($options) {
455         if($options) {
456                 $pulldown_options = array();
457                 foreach($options as $option) {
458                         $option = preg_replace("/['\\\\]/", '\\\\$0', $option);
459                         $pulldown_options[] = "'$option'";
460                 }
461                 $pulldown_options = 'array(' . join(', ', $pulldown_options) . ')';
462         } else {
463                 $pulldown_options = "array(array('op1', 'Option One'), array('op2', 'Option Two'), 'n/a')";
464         }
465
466         return $pulldown_options;
467 }
468
469 function make_php() {
470         $has_html_editors = false;
471         $tem = new tem();
472         $tem->load(__DIR__.'/'.'metaform/template.php');
473         tem_set_globals($tem);
474         $fields = get_fields();
475         $db_fields = '';
476         $always_field = find_always_field($fields);
477         $image_included_yet = false;
478         $name_to_caption = array();
479         foreach($fields as $field) {
480                 $name_to_caption[] = array('name' => $field['name'], 'caption' => $field['caption']);
481                 if($field['input'] != 'submit') {
482                         $tem->set('format', $field['format']);
483                         $tem->set('name', $field['name']);
484                         $tem->set('db_field', ''); # we don't want to use the value from last time
485                         if($field['sql'] != 'n/a') {
486                                 if($db_fields != '') $db_fields .= ',';
487                                 $db_fields .= $field['name'];
488                         }
489                         if($field['input'] == 'image') {
490                                 if($field['type'] == 'thumb') {
491                                         $tem->show('thumb_settings');
492                                         $tem->show('thumb_upload_params');
493                                         $tem->show('thumb_w_h');
494                                 }
495                                 $tem->show('image_settings');
496                                 $tem->show('image_upload');
497                                 $has_uploads = true;
498                         } else if($field['input'] == 'file') {
499                                 $tem->show('file_settings');
500                                 $tem->show('file_upload');
501                                 $has_uploads = true;
502                         } else {
503                                 if($field['input'] == 'html') {
504                                         $has_html_editors = true;
505                                 } elseif($field['input'] == 'pulldown' || $field['input'] == 'radio') {
506                                         $pulldown_options = pulldown_options_array($field['options']);
507                                         $tem->set('pulldown_options', $pulldown_options);
508                                         $tem->set('has_pulldowns');
509                                         $tem->show('pulldowns');
510                                         $tem->show('pulldown_format_extra');
511                                 }
512                                 if($field['format'] != 'n/a') {
513                                         $tem->show('formats');
514                                 }
515                         }
516                 }
517
518                 if($GLOBALS['opt_listing']) {
519                         if(show_in_listing($field['type'], $field['input'], $field['format'], $field['sql'])) {
520                                 $tem->show('listing_fields_1');
521                                 $tem->show('listing_fields_2');
522                         }
523                 }
524         }
525
526         $tem->set('name_to_caption', $name_to_caption);
527
528         if($has_uploads) {
529                 $tem->show('uploads_include');
530                 $tem->show('upload_max');
531                 $tem->show('upload_settings');
532                 $image_included_yet = true;
533         }
534
535         if($has_html_editors) {
536                 $tem->show('show_extra_headers');
537         }
538
539         $tem->set('always_field', $always_field);
540         $tem->set('db_fields', $db_fields);
541         $tem->set('metaform_url', edit_url());
542         if($GLOBALS['opt_email']) {
543                 $this_domain = $_SERVER['HTTP_HOST'];
544                 if(substr($this_domain, -2) == '.l') {
545                         $this_domain = substr($this_domain, 0, -1) . 'com';
546                 }
547                 $tem->set('this_domain', $this_domain);
548         }
549         return $tem->run();
550 }
551
552 # make a URL for the edit page with all the fields filled in
553 function edit_url() {
554         $url = this_url();
555         $url = preg_replace('|view_php=[^&]*|', 'edit=yes', $url);
556         $url = preg_replace('|download_tar=[^&]*|', 'edit=yes', $url);
557         $url = preg_replace('|/[a-z0-9_.]*\?|', '/?', $url);
558         return $url;
559 }
560
561 function view_php() {
562         view_headers();
563         echo make_php();
564 }
565
566 function make_email() {
567         $tem = new tem();
568         $tem->load(__DIR__.'/'.'metaform/template.email.txt');
569         tem_set_globals($tem);
570         $fields = get_fields();
571         foreach($fields as $field) {
572                 $tem->set('name', $field['name']);
573                 $tem->set('caption', $field['caption']);
574                 if($field['type'] == 'textarea') {
575                         $tem->show('multi_line');
576                 } elseif($field['type'] == 'checkbox') {
577                         $tem->show('checkbox');
578                 } else {
579                         $tem->show('normal');
580                 }
581                 $tem->show('fields');
582         }
583         return $tem->run();
584 }
585
586 function make_htaccess() {
587         $tem = new tem();
588         $tem->set('form', $GLOBALS['file_name']);
589         return $tem->run(__DIR__.'/'.'metaform/htaccess');
590 }
591
592 function view_email() {
593         view_headers();
594         echo make_email();
595 }
596
597 function preview() {
598         tem_load(__DIR__.'/'.'metaform/preview.html');
599         tem_set_globals($GLOBALS['wfpl_template']);
600         tem_set('fields', $_REQUEST['fields']);
601         $preview_tem = new tem();
602         $preview_tem->load_str(make_html(false));
603         if($GLOBALS['opt_db']) {
604                 $preview_tem->show('new_msg');
605         }
606         $fields = get_fields();
607         foreach($fields as $field) {
608                 if($field['type'] == 'pulldown' || $field['type'] == 'radio') {
609                         pulldown($field['name'], eval('return ' . pulldown_options_array($field['options']) . ';'));
610                 }
611         }
612         $preview = $preview_tem->run();
613         unset($preview_tem);
614         $preview = preg_replace('|type="submit"|', 'type="submit" disabled="disabled"', $preview);
615         tem_set('preview', $preview);
616         tem_show('hiddens');
617         set_form_action();
618         tem_output();
619 }
620
621 function download_tar() {
622         $admin_name = $GLOBALS['file_name'];
623         $nice_name = $GLOBALS['public_file_name'];
624         $files = array(
625                 "README" => "These files are meant to work with wfpl.\n\nSee: http://sametwice.com/wfpl\n",
626                 ".htaccess" => make_htaccess(),
627                 "config.php" => "<?php\n\n# put your site-wide configuration here\n",
628                 "wfpl_main.php" => read_whole_file(__DIR__.'/'.'metaform/wfpl_main.php'),
629                 "styl.styl" => read_whole_file(__DIR__.'/'.'metaform/styl.styl'),
630                 "template.html" => read_whole_file(__DIR__.'/'.'metaform/site-template.html'),
631                 "$admin_name.html" => make_html(),
632                 "$admin_name.php" => make_php());
633         if($GLOBALS['opt_public_something']) {
634                 $files["$nice_name.html ->"] = "$admin_name.html";
635                 $files["$nice_name.php ->"] = "$admin_name.php";
636         }
637         if($GLOBALS['opt_db']) {
638                 $files["$nice_name.sql"] = make_sql();
639         }
640         if($GLOBALS['opt_email']) {
641                 $files["$nice_name.email.txt"] = make_email();
642         }
643         make_tar($nice_name, $files);
644 }