JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
metaform sets main_body for site-wide template integration, and defaults to 'empty...
[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         tem_show('main_body');
92 }
93
94 function _~form_name~_main() {<!--~opt_http_pass_2 start~-->
95         # To remove password protection, just delete this block:
96         if (!isset($_SERVER['PHP_AUTH_USER']) || $_SERVER['PHP_AUTH_USER'] != AUTH_USER || $_SERVER['PHP_AUTH_PW'] != AUTH_PASS) {
97                 header('WWW-Authenticate: Basic realm="' . AUTH_REALM . '"');
98                 header('HTTP/1.0 401 Unauthorized');
99                 echo '401 Unauthorized';
100                 exit;
101         }
102         <!--~end~--><!--~pulldowns start~-->
103         pulldown('~name~', array('option 1', 'option 2', 'option 3'));
104         <!--~end~--><!--~opt_db_3 start~-->
105         $edit_id = format_int($_REQUEST['~form_name~_edit_id']);
106         unset($_REQUEST['~form_name~_edit_id']);
107         if($edit_id) {
108                 # add hidden field for database id of row we're editing
109                 tem_set('~form_name~_edit_id', $edit_id);
110                 tem_show('editing');
111         }
112
113         $delete_id = format_int($_REQUEST['~form_name~_delete_id']);
114         unset($_REQUEST['~form_name~_delete_id']);
115         if($delete_id) {
116                 db_delete('~form_name~', 'where id=%i', $delete_id);
117                 message('Entry deleted.');
118
119                 return './~form_name~.html';
120         }
121
122         if(!$edit_id) {<!--~opt_listing_1 start~-->
123                 if(!isset($_REQUEST['~form_name~_new']) && !isset($_REQUEST['~always_field~'])) {
124                         ~form_name~_display_listing();
125                         return;
126                 }
127                 <!--~end~-->
128                 tem_show('new_msg');
129         }<!--~end~-->
130
131         if(isset($_REQUEST['~always_field~'])) {
132                 list(~php_fields~) = ~form_name~_get_fields();
133
134                 if("you're happy with the POSTed values") {<!--~opt_db_4 start~-->
135                         if($edit_id) {
136                                 db_update('~form_name~', ~form_name.upper~_DB_FIELDS, ~php_fields~, 'where id=%i', $edit_id);
137                                 message('Entry updated.');
138                         } else {
139                                 db_insert('~form_name~', ~form_name.upper~_DB_FIELDS, ~php_fields~);
140                                 message('Entry saved.');
141                         }<!--~end~--><!--~opt_email_2 start~-->
142                         if($GLOBALS['~form_name~_form_recipient'] != "fixme@example.com") {
143                                 $to = $GLOBALS['~form_name~_form_recipient'];
144                                 if(isset($_REQUEST['email']) and valid_email($_REQUEST['email'])) {
145                                         $from = $_REQUEST['email'];
146                                         if($_REQUEST['name'] and ereg('^[a-zA-Z0-9_\' -]*$', $_REQUEST['name']) !== false) {
147                                                 $from = "$_REQUEST[name] <$from>";
148                                         }
149                                 } else {
150                                         $from = $to;
151                                 }
152                                 $subject = '~form_name~ form submitted';
153                                 $message = tem_run('~form_name~.email.txt');
154                                 $cc = '';
155                                 $bcc = '';
156                                 if(email($from, $to, $subject, $message, $cc, $bcc)) {
157                                         message('Due to an internal error, your message could not be sent. Please try again later.');
158                                         $error = true;
159                                 }
160                         }<!--~end~-->
161                         if($error !== true) {<!--~opt_listing_4 start~-->
162                                 ~form_name~_display_listing();<!--~end~--><!--~opt_listing_4_else start~-->
163                                 tem_show('thankyou');<!--~end~-->
164                                 return;
165                         }
166                 }
167                 # otherwise, we display the form again. ~form_name~_get_fields() has
168                 # already put the posted values back into the template engine, so they will
169                 # show up in the form fields. You should add some message asking people to
170                 # fix their entry in whatever way you require.<!--~opt_db_5 start~-->
171         } elseif($edit_id) {
172                 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
173                 list(~php_fields~) = db_get_row('~form_name~', ~form_name.upper~_DB_FIELDS, 'where id=%i', $edit_id);
174                 ~form_name~_tem_sets(~php_fields~);<!--~end~-->
175         } else {
176                 # form not submitted, you can set default values like so:
177                 #tem_set('~always_field~', 'Yes');
178         }<!--~upload_max start~-->
179
180         tem_set('upload_max_filesize', upload_max_filesize());<!--~end~-->
181
182         # this has to be later in the file because it requres that ~always_field~ be set already
183         if($edit_id) {
184                 tem_show('edit_msg');
185         }
186
187         tem_show('form');
188 }
189
190 ?>