JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
revamped uploaded image handling, added thumbnailing support to metaform
[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['~file_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 ~table_name~ table
17 # (the file ~file_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('~file_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~--><!--~image_settings start~-->
29 $GLOBALS['~name~_max_width'] = '400';
30 $GLOBALS['~name~_max_height'] = '400';<!--~thumb_settings start~-->
31 $GLOBALS['~name~_thumb_max_width'] = '70';
32 $GLOBALS['~name~_thumb_max_height'] = '70';
33 $GLOBALS['~name~_file_name'] = uniqid() . getmypid() . '.jpg'; # comment this out to use uploader's filename
34 <!--~end~--><!--~end~--><!--~opt_http_pass_1 start~-->
35 # Define the username and password required to view this form:
36 define('AUTH_REALM', '~file_name~ administration area');
37 define('AUTH_USER', 'fixme');
38 define('AUTH_PASS', 'fixme');
39 <!--~end~-->
40
41 require_once('code/wfpl/template.php');
42 require_once('code/wfpl/format.php');
43 require_once('code/wfpl/messages.php');
44 require_once('code/wfpl/email.php');<!--~opt_db_2 start~-->
45 require_once('code/db_connect.php');<!--~end~--><!--~image_include start~-->
46 require_once('code/wfpl/upload.php');<!--~end~-->
47
48 function ~file_name~_get_fields() {<!--~formats start~-->
49         $~name~ = format_~format~($_REQUEST['~name~']<!--~pulldown_format_extra start~-->, '~name~'<!--~end~-->);<!--~end~--><!--~image_upload start~-->
50         if($_FILES['~name~'] && $_FILES['~name~']['error'] == 0) {
51                 $~name~ = convert_uploaded_image('~name~', $GLOBALS['upload_directory'] . $GLOBALS['~name~_file_name'], $GLOBALS['~name~_max_width'], $GLOBALS['~name~_max_height']<!--~thumb_upload_params start~-->, $GLOBALS['~name~_thumb_max_width'], $GLOBALS['~name~_thumb_max_height']<!--~end~-->);
52         } else {
53                 if($_REQUEST['delete_~name~'] == 'Yes') {
54                         $~name~ = '';
55                 } else {
56                         $~name~ = format_image_w_h<!--~thumb_w_h start~-->_thumb_w_h<!--~end~-->($_REQUEST['old_~name~']);
57                 }
58         }<!--~end~-->
59
60         ~file_name~_tem_sets(~php_fields~);
61
62         return array(~php_fields~);
63 }
64
65 function ~file_name~_tem_sets(~php_fields~) {<!--~tem_sets start~-->
66         tem_set('~name~', $~name~);<!--~end~-->
67 }
68 <!--~opt_listing_2 start~-->
69 # You may pass a "where clause" for the db query.
70 function ~file_name~_display_listing($where = 'order by ~always_field~ limit 100') {
71         $rows = db_get_rows('~table_name~', 'id<!--~listing_fields_1 start~-->,~name~<!--~end~-->', $where);
72         if($rows == false || count($rows) == 0) {
73                 tem_show('empty_listing');
74                 tem_show('listings');
75                 return false;
76         }
77
78         foreach($rows as $row) {
79                 list($id<!--~listing_fields_2 start~-->, $~name~<!--~end~-->) = $row;
80                 tem_set('id', $id);
81                 if($~always_field~ == '') {
82                         $~always_field~ = '--';
83                 }
84                 ~file_name~_tem_sets(~php_fields~);
85                 tem_show('listing_row');
86         }
87         tem_show('populated_listing');
88         tem_show('listings');
89         return true;
90 }
91 <!--~end~-->
92 function ~file_name~_main() {<!--~opt_display_1 start~-->
93         if(isset($_REQUEST['~file_name~_id'])) {
94                 $ret = ~file_name~_display_main();
95                 if($ret) {
96                         return $ret;
97                 }
98                 tem_show('display_body');
99         } else {
100                 $ret = ~file_name~_edit_main();
101                 if($ret) {
102                         return $ret;
103                 }
104                 tem_show('edit_body');
105         }
106 <!--~end~--><!--~opt_display_1_else start~-->
107         $ret = _~file_name~_main();
108         if($ret) {
109                 return $ret;
110         }
111 <!--~end~-->
112         # sections displayed with tem_show() will be coppied to the main template if you have one.
113         tem_show('main_body');
114 }<!--~opt_display_2 start~-->
115
116 function ~file_name~_display_main() {
117         $id = format_int($_REQUEST['~file_name~_id']);
118         unset($_REQUEST['~file_name~_id']);
119         if(!$id) {
120                 message('Error: Broken link');
121                 return './~file_name~';
122         }
123         $row = db_get_row('~table_name~', ~file_name.upper~_DB_FIELDS, 'where id=%i', $id);
124         if(!$row) {
125                 message('Error: ~singular.cap~ not found');
126                 return './~file_name~';
127         }
128         list(~php_fields~) = $row;
129         ~file_name~_tem_sets(~php_fields~);
130         tem_set('id', $id);
131 }
132
133 function ~file_name~_edit_main() {<!--~end~--><!--~opt_display_2_else start~-->
134
135
136 function _~file_name~_main() {<!--~end~--><!--~opt_http_pass_2 start~-->
137         # To remove password protection, just delete this block:
138         if (!isset($_SERVER['PHP_AUTH_USER']) || $_SERVER['PHP_AUTH_USER'] != AUTH_USER || $_SERVER['PHP_AUTH_PW'] != AUTH_PASS) {
139                 header('WWW-Authenticate: Basic realm="' . AUTH_REALM . '"');
140                 header('HTTP/1.0 401 Unauthorized');
141                 echo '401 Unauthorized';
142                 exit;
143         }
144 <!--~end~--><!--~pulldowns start~-->
145         pulldown('~name~', array('option 1', 'option 2', 'option 3'));
146 <!--~end~--><!--~opt_db_3 start~-->
147         $edit_id = format_int($_REQUEST['~file_name~_edit_id']);
148         unset($_REQUEST['~file_name~_edit_id']);
149         if($edit_id) {
150                 # add hidden field for database id of row we're editing
151                 tem_set('~file_name~_edit_id', $edit_id);
152                 tem_show('editing');
153         }
154
155         $delete_id = format_int($_REQUEST['~file_name~_delete_id']);
156         unset($_REQUEST['~file_name~_delete_id']);
157         if($delete_id) {
158                 db_delete('~table_name~', 'where id=%i', $delete_id);
159                 message('~singular.cap~ deleted.');
160
161                 return './~file_name~';
162         }
163
164         if(!$edit_id) {<!--~opt_listing_1 start~-->
165                 if(!isset($_REQUEST['~file_name~_new']) && !isset($_REQUEST['~always_field~'])) {
166                         ~file_name~_display_listing();
167                         return;
168                 }
169 <!--~end~-->
170                 tem_show('new_msg');
171         }<!--~end~-->
172
173         if(isset($_POST['~always_field~'])) {
174                 list(~php_fields~) = ~file_name~_get_fields();
175
176                 if("you're happy with the POSTed values") {<!--~opt_db_4 start~-->
177                         if($edit_id) {
178                                 db_update('~table_name~', ~file_name.upper~_DB_FIELDS, ~php_fields~, 'where id=%i', $edit_id);
179                                 message('~singular.cap~ updated.');
180                         } else {
181                                 db_insert('~table_name~', ~file_name.upper~_DB_FIELDS, ~php_fields~);
182                                 message('~singular.cap~ saved.');
183                         }<!--~end~--><!--~opt_email_2 start~-->
184                         if($GLOBALS['~file_name~_form_recipient'] != "fixme@example.com") {
185                                 $to = $GLOBALS['~file_name~_form_recipient'];
186                                 $from = $to;
187                                 $reply_to = '';
188                                 if(isset($_REQUEST['email']) and valid_email($_REQUEST['email'])) {
189                                         $reply_to = $_REQUEST['email'];
190                                         if($_REQUEST['name'] and ereg('^[a-zA-Z0-9_\' -]*$', $_REQUEST['name']) !== false) {
191                                                 $reply_to = "$_REQUEST[name] <$reply_to>";
192                                         }
193                                 }
194                                 $subject = '~plural.cap~ form submitted';
195                                 $message = tem_run('~file_name~.email.txt');
196                                 $cc = '';
197                                 $bcc = '';
198                                 if(email($from, $to, $subject, $message, $reply_to, $cc, $bcc)) {
199                                         message('Due to an internal error, your message could not be sent. Please try again later.');
200                                         $error = true;
201                                 } else {
202                                         message('Message sent');
203                                 }
204                         }<!--~end~-->
205                         if($error !== true) {
206                                 return './~file_name~'; # FIXME is this the page you want to go to after successful form submission?
207                         }
208                 }
209                 # otherwise, we display the form again. ~file_name~_get_fields() has
210                 # already put the posted values back into the template engine, so they will
211                 # show up in the form fields. You should add some message asking people to
212                 # fix their entry in whatever way you require.<!--~opt_db_5 start~-->
213         } elseif($edit_id) {
214                 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
215                 list(~php_fields~) = db_get_row('~table_name~', ~file_name.upper~_DB_FIELDS, 'where id=%i', $edit_id);
216                 ~file_name~_tem_sets(~php_fields~);<!--~end~-->
217         } else {
218                 # form not submitted, you can set default values like so:
219                 #tem_set('~always_field~', 'Yes');
220         }<!--~upload_max start~-->
221
222         tem_set('upload_max_filesize', upload_max_filesize());<!--~end~-->
223
224         # this has to be later in the file because it requres that ~always_field~ be set already
225         if($edit_id) {
226                 tem_show('edit_msg');
227         }
228
229         tem_show('form');
230 }
231
232 ?>