JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
7697c483cb0a0d1ae36a64e569dce82dde035024
[wfpl.git] / metaform / template.php
1 <?php
2
3 # This form requires wfpl. See: http://jasonwoof.org/wfpl
4
5 # This form was initially auto-generated. If you would like to alter the
6 # parameters and generate a new one try this URL:
7 #
8 # ~metaform_url~
9
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 the file
18 # 'code/db_connect.php' which calls db_connect() see:
19 # code/wfpl/examples/db_connect.php
20 #
21 # if you rename any of the database fields, you'll need to update this:
22
23 define('~form_name.upper~_DB_FIELDS', '~db_fields~');
24 <!--~end~--><!--~upload_settings start~-->
25 # Set this to the path to your uploads directory. It can be relative to the
26 # location of this script. IT MUST END WITH A SLASH
27 $GLOBALS['upload_directory'] = 'uploads/';
28 <!--~end~--><!--~opt_http_pass_1 start~-->
29 # Define the username and password required to view this form:
30 define('AUTH_REALM', '~form_name~ administration area');
31 define('AUTH_USER', 'fixme');
32 define('AUTH_PASS', 'fixme');
33 <!--~end~-->
34
35 require_once('code/wfpl/template.php');
36 require_once('code/wfpl/format.php');
37 require_once('code/wfpl/messages.php');
38 require_once('code/wfpl/email.php');<!--~opt_db_2 start~-->
39 require_once('code/db_connect.php');<!--~end~--><!--~image_include start~-->
40 require_once('code/wfpl/upload.php');<!--~end~-->
41
42 function ~form_name~_get_fields() {<!--~formats start~-->
43         $~name~ = format_~format~($_REQUEST['~name~']<!--~pulldown_format_extra start~-->, '~name~'<!--~end~-->);<!--~end~--><!--~image_upload start~-->
44         if($_FILES['~name~'] && $_FILES['~name~']['error'] == 0) {
45                 $~name~ = substr(save_uploaded_image('~name~', $GLOBALS['upload_directory']), strlen($GLOBALS['upload_directory']));
46         } else {
47                 if($_REQUEST['delete_~name~'] == 'Yes') {
48                         $~name~ = '';
49                 } else {
50                         $~name~ = format_filename($_REQUEST['old_~name~']);
51                 }
52         }<!--~end~-->
53
54         ~form_name~_tem_sets(~php_fields~);
55
56         return array(~php_fields~);
57 }
58
59 function ~form_name~_tem_sets(~php_fields~) {<!--~tem_sets start~-->
60         tem_set('~name~', $~name~);<!--~end~-->
61 }
62 <!--~opt_listing_2 start~-->
63 # You may pass a "where clause" for the db query.
64 function ~form_name~_display_listing($where = 'order by ~always_field~ limit 100') {
65         $rows = db_get_rows('~form_name~', 'id,~always_field~', $where);
66         if($rows == false || count($rows) == 0) {
67                 tem_show('empty_listing');
68                 tem_show('listings');
69                 return false;
70         }
71
72         foreach($rows as $row) {
73                 list($id, $~always_field~) = $row;
74                 tem_set('id', $id);
75                 if($~always_field~ == '') {
76                         $~always_field~ = '--';
77                 }
78                 tem_set('~always_field~', $~always_field~);
79                 tem_show('listing_row');
80         }
81         tem_show('populated_listing');
82         tem_show('listings');
83         return true;
84 }
85 <!--~end~-->
86 function ~form_name~_main() {
87         $ret = _~form_name~_main();
88         if($ret) {
89                 return $ret;
90         }
91
92         # sections displayed with tem_show() will be coppied to the main template if you have one.
93         tem_show('main_body');
94 }
95
96 function _~form_name~_main() {<!--~opt_http_pass_2 start~-->
97         # To remove password protection, just delete this block:
98         if (!isset($_SERVER['PHP_AUTH_USER']) || $_SERVER['PHP_AUTH_USER'] != AUTH_USER || $_SERVER['PHP_AUTH_PW'] != AUTH_PASS) {
99                 header('WWW-Authenticate: Basic realm="' . AUTH_REALM . '"');
100                 header('HTTP/1.0 401 Unauthorized');
101                 echo '401 Unauthorized';
102                 exit;
103         }
104         <!--~end~--><!--~pulldowns start~-->
105         pulldown('~name~', array('option 1', 'option 2', 'option 3'));
106         <!--~end~--><!--~opt_db_3 start~-->
107         $edit_id = format_int($_REQUEST['~form_name~_edit_id']);
108         unset($_REQUEST['~form_name~_edit_id']);
109         if($edit_id) {
110                 # add hidden field for database id of row we're editing
111                 tem_set('~form_name~_edit_id', $edit_id);
112                 tem_show('editing');
113         }
114
115         $delete_id = format_int($_REQUEST['~form_name~_delete_id']);
116         unset($_REQUEST['~form_name~_delete_id']);
117         if($delete_id) {
118                 db_delete('~form_name~', 'where id=%i', $delete_id);
119                 message('Entry deleted.');
120
121                 return './~form_name~.html';
122         }
123
124         if(!$edit_id) {<!--~opt_listing_1 start~-->
125                 if(!isset($_REQUEST['~form_name~_new']) && !isset($_REQUEST['~always_field~'])) {
126                         ~form_name~_display_listing();
127                         return;
128                 }
129                 <!--~end~-->
130                 tem_show('new_msg');
131         }<!--~end~-->
132
133         if(isset($_REQUEST['~always_field~'])) {
134                 list(~php_fields~) = ~form_name~_get_fields();
135
136                 if("you're happy with the POSTed values") {<!--~opt_db_4 start~-->
137                         if($edit_id) {
138                                 db_update('~form_name~', ~form_name.upper~_DB_FIELDS, ~php_fields~, 'where id=%i', $edit_id);
139                                 message('Entry updated.');
140                         } else {
141                                 db_insert('~form_name~', ~form_name.upper~_DB_FIELDS, ~php_fields~);
142                                 message('Entry saved.');
143                         }<!--~end~--><!--~opt_email_2 start~-->
144                         if($GLOBALS['~form_name~_form_recipient'] != "fixme@example.com") {
145                                 $to = $GLOBALS['~form_name~_form_recipient'];
146                                 $from = $to;
147                                 $reply_to = '';
148                                 if(isset($_REQUEST['email']) and valid_email($_REQUEST['email'])) {
149                                         $reply_to = $_REQUEST['email'];
150                                         if($_REQUEST['name'] and ereg('^[a-zA-Z0-9_\' -]*$', $_REQUEST['name']) !== false) {
151                                                 $reply_to = "$_REQUEST[name] <$reply_to>";
152                                         }
153                                 }
154                                 $subject = '~form_name~ form submitted';
155                                 $message = tem_run('~form_name~.email.txt');
156                                 $cc = '';
157                                 $bcc = '';
158                                 if(email($from, $to, $subject, $message, $reply_to, $cc, $bcc)) {
159                                         message('Due to an internal error, your message could not be sent. Please try again later.');
160                                         $error = true;
161                                 }
162                         }<!--~end~-->
163                         if($error !== true) {<!--~opt_listing_4 start~-->
164                                 ~form_name~_display_listing();<!--~end~--><!--~opt_listing_4_else start~-->
165                                 tem_show('thankyou');<!--~end~-->
166                                 return;
167                         }
168                 }
169                 # otherwise, we display the form again. ~form_name~_get_fields() has
170                 # already put the posted values back into the template engine, so they will
171                 # show up in the form fields. You should add some message asking people to
172                 # fix their entry in whatever way you require.<!--~opt_db_5 start~-->
173         } elseif($edit_id) {
174                 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
175                 list(~php_fields~) = db_get_row('~form_name~', ~form_name.upper~_DB_FIELDS, 'where id=%i', $edit_id);
176                 ~form_name~_tem_sets(~php_fields~);<!--~end~-->
177         } else {
178                 # form not submitted, you can set default values like so:
179                 #tem_set('~always_field~', 'Yes');
180         }<!--~upload_max start~-->
181
182         tem_set('upload_max_filesize', upload_max_filesize());<!--~end~-->
183
184         # this has to be later in the file because it requres that ~always_field~ be set already
185         if($edit_id) {
186                 tem_show('edit_msg');
187         }
188
189         tem_show('form');
190 }
191
192 ?>