JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fixed conflict
[wfpl.git] / metaform / template.php
1 <?php
2
3 # This form requires wfpl. If you didn't recieve wfpl along with this file,
4 # see: http://jasonwoof.org/wfpl
5
6 # This form was auto-generated. If you would like to alter the parameters and
7 # generate a new one try this URL:
8 #
9 # ~metaform_url~
10
11 # SETUP
12 <!--~opt_email_1 start~-->
13 # To send results by e-mail, all you have to do is set your e-mail address here:
14 $GLOBALS['~form_name~_form_recipient'] = "fixme@example.com";
15 <!--~end~--><!--~opt_db_1 start~-->
16 # To save results to a database, you'll need to create the ~form_name~ table
17 # (the file ~form_name~.sql should help with this), and create a file called
18 # 'db_connect.php' or 'code/db_connect.php' which calls db_connect() see:
19 # code/wfpl/examples/db_connect.php
20 <!--~end~--><!--~upload_settings start~-->
21 # Set this to the path to your uploads directory. It can be relative to the
22 # location of this script. IT MUST END WITH A SLASH
23 $GLOBALS['upload_directory'] = 'uploads/';
24 <!--~end~-->
25
26 if(!file_exists('code/wfpl/template.php')) { die('This form requires <a href="http://jasonwoof.org/wfpl">wfpl</a>.'); }
27 require_once('code/wfpl/template.php');
28 require_once('code/wfpl/format.php');
29 require_once('code/wfpl/messages.php');
30 require_once('code/wfpl/email.php');<!--~opt_db_2 start~-->
31 require_once('code/wfpl/db.php');<!--~end~--><!--~image_include start~-->
32 require_once('code/wfpl/upload.php');<!--~end~-->
33
34 function ~form_name~_get_fields() {<!--~formats start~-->
35         $~name~ = format_~format~($_REQUEST['~name~']);<!--~end~--><!--~image_upload start~-->
36         if($_FILE['~name~'] && $_FILE['~name~']['error'] == 0) {
37                 $~name~ = substr(save_uploaded_image('~name~', $GLOBALS['upload_directory']), strlen($GLOBALS['upload_directory']));
38         } else {
39                 $~name~ = format_filename($_REQUEST['old_~name~']);
40         }<!--~end~-->
41         <!--~tem_sets start~-->
42         tem_set('~name~', $~name~);<!--~end~-->
43
44         return array(~php_fields~);
45 }
46
47 function ~form_name~() {<!--~opt_db_3 start~-->
48         $edit_id = format_int($_REQUEST['~form_name~_edit_id']);
49         unset($_REQUEST['~form_name~_edit_id']);
50         if($edit_id) {
51                 # add hidden field for database id of row we're editing
52                 tem_set('~form_name~_edit_id', $edit_id);
53                 tem_sub('editing');
54                 tem_sub('edit_msg');
55         }
56
57         $delete_id = format_int($_REQUEST['~form_name~_delete_id']);
58         unset($_REQUEST['~form_name~_delete_id']);
59         if($delete_id) {
60                 db_delete('~form_name~', 'where id=%i', $delete_id);
61                 message('Entry deleted.');
62
63                 # FIXME: what to do after delete?
64                 return;
65         }
66
67         if(!$edit_id && !$delet_id) {
68                 tem_sub('new_msg');
69         }<!--~end~-->
70
71         if(isset($_REQUEST['~always_field~'])) {
72                 list(~php_fields~) = ~form_name~_get_fields();
73
74                 if("you're happy with the POSTed values") {<!--~opt_db_4 start~-->
75                         if(file_exists($db_connector = 'db_connect.php') || file_exists($db_connector = 'code/db_connect.php')) {
76                                 require_once($db_connector);
77                                 if($edit_id) {<!--~image_db start~-->
78                                         # uploading nothing means leaving it as is.
79                                         if(!$~name~ && $delete_~name~ != 'Yes') {
80                                                 $~name~ = db_get_value('~form_name~', '~name~', 'where id=%i', $edit_id);
81                                         }
82                                         <!--~end~-->
83                                         db_update('~form_name~', '~db_fields~', ~php_fields~, 'where id=%i', $edit_id);
84                                         message('Entry updated.');
85                                 } else {
86                                         db_insert('~form_name~', '~db_fields~', ~php_fields~);
87                                         message('Entry saved.');
88                                 }
89                         }<!--~end~--><!--~opt_email_2 start~-->
90                         if($GLOBALS['~form_name~_form_recipient'] != "fixme@example.com") {
91                                 $to = $GLOBALS['~form_name~_form_recipient'];
92                                 if(isset($_REQUEST['email']) and valid_email($_REQUEST['email'])) {
93                                         $from = $_REQUEST['email'];
94                                         if($_REQUEST['name'] and ereg('^[a-zA-Z0-9_\' -]*$', $_REQUEST['name']) !== false) {
95                                                 $from = "$_REQUEST[name] <$from>";
96                                         }
97                                 } else {
98                                         $from = $to;
99                                 }
100                                 $subject = '~form_name~ form submitted';
101                                 $message = tem_run('~form_name~.email.txt');
102                                 $cc = '';
103                                 $bcc = '';
104                                 if(email($from, $to, $subject, $message, $cc, $bcc)) {
105                                         tem_set('error_message', 'Due to an internal error, your message could not be sent. Please try again later.');
106                                         tem_sub('error');
107                                         $error = true;
108                                 }
109                         }<!--~end~-->
110                         if($error !== true) {
111                                 tem_load('~form_name~.html');
112                                 tem_sub('thankyou');
113                                 tem_output();
114                                 exit();
115                         }
116                 }
117                 # otherwise, we display the form again. ~form_name~_get_fields() has
118                 # already put the posted values back into the template engine, so they will
119                 # show up in the form fields. You should add some message asking people to
120                 # fix their entry in whatever way you require.<!--~opt_db_5 start~-->
121         } elseif($edit_id) {
122                 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
123                 list(~php_fields~) = db_get_row('~form_name~', '~db_fields~', 'where id=%i', $edit_id);
124                 ~tem_sets.tab~<!--~end~-->
125         } else {
126                 # form not submitted, you can set default values like so:
127                 #tem_set('~always_field~', 'Yes');
128         }<!--~upload_max start~-->
129
130         tem_set('upload_max_filesize', upload_max_filesize());<!--~end~-->
131
132         tem_sub('form');
133 }
134
135 ?>