JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
965f38c2dd22d53ec00652dc025438d555d9b904
[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() {<!--~opt_display_1 start~-->
87         if(isset($_REQUEST['~form_name~_id'])) {
88                 $ret = ~form_name~_display_main();
89                 if($ret) {
90                         return $ret;
91                 }
92                 tem_show('display_body');
93         } else {
94                 $ret = ~form_name~_edit_main();
95                 if($ret) {
96                         return $ret;
97                 }
98                 tem_show('edit_body');
99         }
100 <!--~end~--><!--~opt_display_1_else start~-->
101         $ret = _~form_name~_main();
102         if($ret) {
103                 return $ret;
104         }
105 <!--~end~-->
106         # sections displayed with tem_show() will be coppied to the main template if you have one.
107         tem_show('main_body');
108 }<!--~opt_display_2 start~-->
109
110 function ~form_name~_display_main() {
111         $id = format_int($_REQUEST['~form_name~_id']);
112         unset($_REQUEST['~form_name~_id']);
113         if(!$id) {
114                 message('Error: Broken link');
115                 return './~form_name~';
116         }
117         $row = db_get_row('~form_name~', ~form_name.upper~_DB_FIELDS, 'where id=%i', $id);
118         if(!$row) {
119                 message('Error: ~singular.cap~ not found');
120                 return './~form_name~';
121         }
122         list(~php_fields~) = $row;
123         ~form_name~_tem_sets(~php_fields~);
124         tem_set('id', $id);
125 }
126
127 function ~form_name~_edit_main() {<!--~end~--><!--~opt_display_2_else start~-->
128
129
130 function _~form_name~_main() {<!--~end~--><!--~opt_http_pass_2 start~-->
131         # To remove password protection, just delete this block:
132         if (!isset($_SERVER['PHP_AUTH_USER']) || $_SERVER['PHP_AUTH_USER'] != AUTH_USER || $_SERVER['PHP_AUTH_PW'] != AUTH_PASS) {
133                 header('WWW-Authenticate: Basic realm="' . AUTH_REALM . '"');
134                 header('HTTP/1.0 401 Unauthorized');
135                 echo '401 Unauthorized';
136                 exit;
137         }
138 <!--~end~--><!--~pulldowns start~-->
139         pulldown('~name~', array('option 1', 'option 2', 'option 3'));
140 <!--~end~--><!--~opt_db_3 start~-->
141         $edit_id = format_int($_REQUEST['~form_name~_edit_id']);
142         unset($_REQUEST['~form_name~_edit_id']);
143         if($edit_id) {
144                 # add hidden field for database id of row we're editing
145                 tem_set('~form_name~_edit_id', $edit_id);
146                 tem_show('editing');
147         }
148
149         $delete_id = format_int($_REQUEST['~form_name~_delete_id']);
150         unset($_REQUEST['~form_name~_delete_id']);
151         if($delete_id) {
152                 db_delete('~form_name~', 'where id=%i', $delete_id);
153                 message('~singular.cap~ deleted.');
154
155                 return './~form_name~';
156         }
157
158         if(!$edit_id) {<!--~opt_listing_1 start~-->
159                 if(!isset($_REQUEST['~form_name~_new']) && !isset($_REQUEST['~always_field~'])) {
160                         ~form_name~_display_listing();
161                         return;
162                 }
163 <!--~end~-->
164                 tem_show('new_msg');
165         }<!--~end~-->
166
167         if(isset($_REQUEST['~always_field~'])) {
168                 list(~php_fields~) = ~form_name~_get_fields();
169
170                 if("you're happy with the POSTed values") {<!--~opt_db_4 start~-->
171                         if($edit_id) {
172                                 db_update('~form_name~', ~form_name.upper~_DB_FIELDS, ~php_fields~, 'where id=%i', $edit_id);
173                                 message('~singular.cap~ updated.');
174                         } else {
175                                 db_insert('~form_name~', ~form_name.upper~_DB_FIELDS, ~php_fields~);
176                                 message('~singular.cap~ saved.');
177                         }<!--~end~--><!--~opt_email_2 start~-->
178                         if($GLOBALS['~form_name~_form_recipient'] != "fixme@example.com") {
179                                 $to = $GLOBALS['~form_name~_form_recipient'];
180                                 $from = $to;
181                                 $reply_to = '';
182                                 if(isset($_REQUEST['email']) and valid_email($_REQUEST['email'])) {
183                                         $reply_to = $_REQUEST['email'];
184                                         if($_REQUEST['name'] and ereg('^[a-zA-Z0-9_\' -]*$', $_REQUEST['name']) !== false) {
185                                                 $reply_to = "$_REQUEST[name] <$reply_to>";
186                                         }
187                                 }
188                                 $subject = '~form_name~ form submitted';
189                                 $message = tem_run('~form_name~.email.txt');
190                                 $cc = '';
191                                 $bcc = '';
192                                 if(email($from, $to, $subject, $message, $reply_to, $cc, $bcc)) {
193                                         message('Due to an internal error, your message could not be sent. Please try again later.');
194                                         $error = true;
195                                 } else {
196                                         message('Message sent');
197                                 }
198                         }<!--~end~-->
199                         if($error !== true) {
200                                 return './~form_name~'; # FIXME is this the page you want to go to after successful form submission?
201                         }
202                 }
203                 # otherwise, we display the form again. ~form_name~_get_fields() has
204                 # already put the posted values back into the template engine, so they will
205                 # show up in the form fields. You should add some message asking people to
206                 # fix their entry in whatever way you require.<!--~opt_db_5 start~-->
207         } elseif($edit_id) {
208                 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
209                 list(~php_fields~) = db_get_row('~form_name~', ~form_name.upper~_DB_FIELDS, 'where id=%i', $edit_id);
210                 ~form_name~_tem_sets(~php_fields~);<!--~end~-->
211         } else {
212                 # form not submitted, you can set default values like so:
213                 #tem_set('~always_field~', 'Yes');
214         }<!--~upload_max start~-->
215
216         tem_set('upload_max_filesize', upload_max_filesize());<!--~end~-->
217
218         # this has to be later in the file because it requres that ~always_field~ be set already
219         if($edit_id) {
220                 tem_show('edit_msg');
221         }
222
223         tem_show('form');
224 }
225
226 ?>