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