JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
added enc_tab(), fixed metaform php template so it looks right
[wfpl.git] / metaform / template.php
1 <?php
2
3 # This form requires wfpl. See: http://jasonwoof.org/wfpl
4
5 # This code can send form results by e-mail and/or save them to a database. See
6 # the next two comments to enable either or both.
7
8 # To send results by e-mail, all you have to do is set your e-mail address here:
9 $GLOBALS['~form_name~_form_recipient'] = "fixme@example.com";
10
11 # To save results to a database, you'll need to create the ~form_name~ table
12 # (the file ~form_name~.sql should help with this), and create a file called
13 # 'db_connect.php' which calls db_connect() see:
14 # code/wfpl/examples/db_connect.php
15
16 if(!file_exists('code/wfpl/template.php')) { die('This form requires <a href="http://jasonwoof.org/wfpl">wfpl</a>.'); }
17 require_once('code/wfpl/template.php');
18 require_once('code/wfpl/format.php');
19 require_once('code/wfpl/email.php');
20 require_once('code/wfpl/db.php');
21
22 function ~form_name~_get_fields() {
23         $fields = array();
24         <!--~formats start~-->
25         $~name~ = format_~format~($_REQUEST['~name~']);<!--~end~-->
26         <!--~tem_sets start~-->
27         tem_set('~name~', $~name~);<!--~end~-->
28
29         return array(~php_fields~);
30 }
31
32 function ~form_name~() {
33         $event_id = format_int($_REQUEST['~form_name~_edit_id']);
34         if($edit_id) {
35                 # add hidden field for database id of row we're editing
36                 tem_set('~form_name~_event_id', $edit_id);
37                 tem_sub('editing');
38         }
39
40         $delete_id = format_int($_REQUEST['~form_name~_delete_id']);
41         if($delete_id) {
42                 db_delete('~form_name~', 'id = %"', $delete_id);
43
44                 # FIXME: what to do after delete?
45                 return;
46         }
47
48         if(isset($_REQUEST['~always_field~'])) {
49                 list(~php_fields~) = ~form_name~_get_fields();
50
51                 if("you're happy with the POSTed values") {
52                         # to enable saving to a database, create a file called 'db_connect.php'
53                         # see: code/wfpl/examples/db_connect.php
54                         if(file_exists('db_connect.php') {
55                                 require_once('db_connect.php');
56                                 if($edit_id) {
57                                         db_update('~form_name~', '~db_fields~', ~php_fields~, 'id = %"', $edit_id);
58                                         tem_set('did', 'updated');
59                                 } else {
60                                         db_insert('~form_name~', '~db_fields~', ~php_fields~);
61                                         tem_set('did', 'saved');
62                                 }
63                         }
64                         if($GLOBALS['~form_name~_form_recipient'] != "fixme@example.com") {
65                                 $to = $GLOBALS['~form_name~_form_recipient'];
66                                 if(isset($_REQUEST['email']) and valid_email($_REQUEST['email'])) {
67                                         $from = $_REQUEST['email'];
68                                         if($_REQUEST['name'] and ereg('^[a-zA-Z0-9_\' -]*$', $_REQUEST['name']) !== false) {
69                                                 $from = "$_REQUEST[name] <$from>";
70                                         }
71                                 } else {
72                                         $from = $to;
73                                 }
74                                 $subject = '~form_name~ form submitted';
75                                 $message = tem_run('~form_name~.email.txt');
76                                 $cc = '';
77                                 $bcc = '';
78                                 email($from, $to, $subject, $message, $cc, $bcc);
79                         }
80                         tem_load('~form_name~.html');
81                         tem_sub('thankyou');
82                         tem_output();
83                         exit();
84                 }
85                 # otherwise, we display the form again. ~form_name~_get_fields() has
86                 # already put the posted values back into the template engine, so they will
87                 # show up in the form fields. You should add some message asking people to
88                 # fix their entry in whatever way you require.
89         }
90         } elseif($edit_id) {
91                 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
92                 list(~php_fields~) = db_get_row('events', '~db_fields~', 'id = %"', $event_id);
93                 ~tem_sets.tab~
94         } else {
95                 # form not submitted, you can set default values like so
96                 #tem_set('~always_field~', 'Yes');
97         }
98
99         tem_sub('form');
100 }
101
102 # emulate run.php if it's not being used
103 if(!function_exists('run_php')) {
104         tem_load('~form_name~.html');
105         ~form_name~();
106         tem_output();
107 }
108
109 ?>