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