JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
5a5c0eeb00bd0fea4feae02f9ecf679fd793d711
[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 <!--~upload_settings start~-->
11 # Set this to the path to your uploads directory. It can be relative to the
12 # location of this script. IT MUST END WITH A SLASH
13 $GLOBALS['upload_directory'] = 'uploads/';
14 <!--~end~-->
15 # To save results to a database, you'll need to create the ~form_name~ table
16 # (the file ~form_name~.sql should help with this), and create a file called
17 # 'db_connect.php' which calls db_connect() see:
18 # code/wfpl/examples/db_connect.php
19
20 if(!file_exists('code/wfpl/template.php')) { die('This form requires <a href="http://jasonwoof.org/wfpl">wfpl</a>.'); }
21 require_once('code/wfpl/template.php');
22 require_once('code/wfpl/format.php');
23 require_once('code/wfpl/email.php');
24 require_once('code/wfpl/db.php');<!--~image_include start~-->
25 require_once('code/wfpl/upload.php');<!--~end~-->
26
27 function ~form_name~_get_fields() {
28         $fields = array();
29         <!--~formats start~-->
30         $~name~ = format_~format~($_REQUEST['~name~']);<!--~end~--><!--~image_upload start~-->
31         $~name~ = save_uploaded_image('~name~', $GLOBALS['upload_directory']);<!--~end~-->
32         <!--~tem_sets start~-->
33         tem_set('~name~', $~name~);<!--~end~-->
34
35         return array(~php_fields~);
36 }
37
38 function ~form_name~() {
39         $edit_id = format_int($_REQUEST['~form_name~_edit_id']);
40         if($edit_id) {
41                 # add hidden field for database id of row we're editing
42                 tem_set('~form_name~_edit_id', $edit_id);
43                 tem_sub('editing');
44         }
45
46         $delete_id = format_int($_REQUEST['~form_name~_delete_id']);
47         if($delete_id) {
48                 db_delete('~form_name~', 'id = %"', $delete_id);
49
50                 # FIXME: what to do after delete?
51                 return;
52         }
53
54         if(isset($_REQUEST['~always_field~'])) {
55                 list(~php_fields~) = ~form_name~_get_fields();
56
57                 if("you're happy with the POSTed values") {
58                         # to enable saving to a database, create a file called 'db_connect.php'
59                         # see: code/wfpl/examples/db_connect.php
60                         if(file_exists('db_connect.php')) {
61                                 require_once('db_connect.php');
62                                 if($edit_id) {<!--~image_db start~-->
63                                         # uploading nothing means leaving it as is.
64                                         if(!$~name~ && $delete_~name~ != 'Yes') {
65                                                 $~name~ = db_get_value('~form_name~', '~name~', 'id = %"', $edit_id);
66                                         }
67                                         <!--~end~-->
68                                         db_update('~form_name~', '~db_fields~', ~php_fields~, 'id = %"', $edit_id);
69                                         tem_set('did', 'updated');
70                                 } else {
71                                         db_insert('~form_name~', '~db_fields~', ~php_fields~);
72                                         tem_set('did', 'saved');
73                                 }
74                         }
75                         if($GLOBALS['~form_name~_form_recipient'] != "fixme@example.com") {
76                                 $to = $GLOBALS['~form_name~_form_recipient'];
77                                 if(isset($_REQUEST['email']) and valid_email($_REQUEST['email'])) {
78                                         $from = $_REQUEST['email'];
79                                         if($_REQUEST['name'] and ereg('^[a-zA-Z0-9_\' -]*$', $_REQUEST['name']) !== false) {
80                                                 $from = "$_REQUEST[name] <$from>";
81                                         }
82                                 } else {
83                                         $from = $to;
84                                 }
85                                 $subject = '~form_name~ form submitted';
86                                 $message = tem_run('~form_name~.email.txt');
87                                 $cc = '';
88                                 $bcc = '';
89                                 email($from, $to, $subject, $message, $cc, $bcc);
90                         }
91                         tem_load('~form_name~.html');
92                         tem_sub('thankyou');
93                         tem_output();
94                         exit();
95                 }
96                 # otherwise, we display the form again. ~form_name~_get_fields() has
97                 # already put the posted values back into the template engine, so they will
98                 # show up in the form fields. You should add some message asking people to
99                 # fix their entry in whatever way you require.
100         } elseif($edit_id) {
101                 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
102                 list(~php_fields~) = db_get_row('~form_name~', '~db_fields~', 'id = %"', $edit_id);
103                 ~tem_sets.tab~
104         } else {
105                 # form not submitted, you can set default values like so
106                 #tem_set('~always_field~', 'Yes');
107         }<!--~upload_max start~-->
108
109         tem_set('upload_max_filesize', upload_max_filesize());<!--~end~-->
110
111         tem_sub('form');
112 }
113
114 # emulate run.php if it's not being used
115 if(!function_exists('run_php')) {
116         tem_load('~form_name~.html');
117         ~form_name~();
118         tem_output();
119 }
120
121 ?>