JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
improve :first-child hack
[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 {~-->
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 <!--~}~--><!--~opt_db {~-->
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 <!--~}~--><!--~upload_settings {~-->
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 <!--~}~--><!--~file_settings {~-->
29 $GLOBALS['~name~_file_name'] = uniqid() . getmypid() . '.txt'; # comment this out to use uploader's filename
30 <!--~}~--><!--~image_settings {~-->
31 $GLOBALS['~name~_max_width'] = '400';
32 $GLOBALS['~name~_max_height'] = '400';<!--~thumb_settings {~-->
33 $GLOBALS['~name~_thumb_max_width'] = '70';
34 $GLOBALS['~name~_thumb_max_height'] = '70';
35 $GLOBALS['~name~_file_name'] = uniqid() . getmypid() . '.jpg'; # comment this out to use uploader's filename
36 <!--~}~--><!--~}~-->
37
38 require_once('code/wfpl/format.php');
39 require_once('code/wfpl/email.php');<!--~uploads_include {~-->
40 require_once('code/wfpl/upload.php');<!--~}~-->
41
42 function ~file_name~_get_fields() {
43         $data = array();
44 <!--~formats {~-->
45         $data['~name~'] = format_~format~($_REQUEST['~name~']<!--~pulldown_format_extra {~-->, '~name~'<!--~}~-->);<!--~}~--><!--~image_upload {~-->
46         if($_FILES['~name~'] && $_FILES['~name~']['error'] == 0) {
47                 $data['~name~'] = convert_uploaded_image('~name~', $GLOBALS['upload_directory'] . $GLOBALS['~name~_file_name'], $GLOBALS['~name~_max_width'], $GLOBALS['~name~_max_height']<!--~thumb_upload_params {~-->, $GLOBALS['~name~_thumb_max_width'], $GLOBALS['~name~_thumb_max_height']<!--~}~-->);
48         } else {
49                 if($_REQUEST['delete_~name~'] == 'Yes') {
50                         $data['~name~'] = '';
51                 } else {
52                         $data['~name~'] = format_image_w_h<!--~thumb_w_h {~-->_thumb_w_h<!--~}~-->($_REQUEST['old_~name~']);
53                 }
54         }<!--~}~--><!--~file_upload {~-->
55         $~name~_filename_tmp = $GLOBALS['~name~_file_name'];
56         if(!$~name~_filename_tmp) {
57                 $~name~_filename_tmp = format_filename($_REQUEST['~name~']);
58         }
59         if($_FILES['~name~'] && $_FILES['~name~']['error'] == 0) {
60                 $data['~name~'] = save_uploaded_file('~name~', $GLOBALS['upload_directory'] . $~name~_filename_tmp);
61         } else {
62                 if($_REQUEST['delete_~name~'] == 'Yes') {
63                         $data['~name~'] = '';
64                 } else {
65                         $data['~name~'] = format_path($_REQUEST['old_~name~']);
66                 }
67         }<!--~}~-->
68
69         return $data;
70 }
71
72
73 function ~file_name~_main() {<!--~opt_pass {~-->
74         if(!logged_in_as_admin()) {
75                 $_REQUEST['url'] = this_url();
76                 return 'admin_login';
77         }
78 <!--~}~--><!--~opt_display {~-->
79         if(isset($_REQUEST['~file_name~_id'])) {
80                 return ~file_name~_display_main();
81         } else {
82                 return ~file_name~_edit_main();
83         }
84 }
85
86 function ~file_name~_display_main() {
87         $id = format_int($_REQUEST['~file_name~_id']);
88         unset($_REQUEST['~file_name~_id']);
89         if(!$id) {
90                 message('Error: Broken link');
91                 return './~file_name~';
92         }
93         $data = db_get_assoc('~table_name~', 'id,'.~file_name upper~_DB_FIELDS, 'where id=%i', $id);
94         if(!$data) {
95                 message('Error: ~singular cap~ not found');
96                 return './~file_name~';
97         }
98         tem_set('display', $data);
99 }
100
101 function ~file_name~_edit_main() {<!--~}~--><!--~pulldowns {~-->
102         pulldown('~name~', ~pulldown_options~);
103 <!--~}~--><!--~opt_db {~-->
104         $edit_id = format_int($_REQUEST['~file_name~_edit_id']);
105         unset($_REQUEST['~file_name~_edit_id']);
106         if($edit_id) {
107                 # add hidden field for database id of row we're editing
108                 tem_set('~file_name~_edit_id', $edit_id);
109                 tem_set('editing');
110                 tem_set('edit_msg');
111         }
112
113         $delete_id = format_int($_REQUEST['~file_name~_delete_id']);
114         unset($_REQUEST['~file_name~_delete_id']);
115         if($delete_id) {
116                 db_delete('~table_name~', 'where id=%i', $delete_id);
117                 message('~singular cap~ deleted.');
118
119                 return './~file_name~';
120         }
121
122         if(!$edit_id) {<!--~opt_listing {~-->
123                 if(!isset($_REQUEST['~file_name~_new']) && !isset($_REQUEST['~always_field~'])) {
124                         $listing_rows = db_get_assocs('~table_name~', 'id<!--~listing_fields_1 {~-->,~name~<!--~}~-->', 'order by ~always_field~ limit 100');
125                         tem_set('listings', $listing_rows);
126                         return;
127                 }
128 <!--~}~-->
129                 tem_set('new_msg');
130         }
131 <!--~}~-->
132         if(isset($_POST['~always_field~'])) {
133                 $data = ~file_name~_get_fields();
134
135                 if("you're happy with the POSTed values") {<!--~opt_db {~-->
136                         if($edit_id) {
137                                 db_update_assoc('~table_name~', $data, 'where id=%i', $edit_id);
138                                 message('~singular cap~ updated.');
139                         } else {
140                                 db_insert_assoc('~table_name~', $data);
141                                 message('~singular cap~ saved.');
142                         }<!--~}~--><!--~opt_email {~-->
143                         if($GLOBALS['~file_name~_form_recipient'] != 'fixme@example.com') {
144                                 $to = $GLOBALS['~file_name~_form_recipient'];
145                                 $from = 'noreply@~this_domain~';
146                                 $reply_to = $to;
147                                 if(isset($_REQUEST['email']) and valid_email($_REQUEST['email'])) {
148                                         $reply_to = $_REQUEST['email'];
149                                         if($_REQUEST['name'] and ereg('^[a-zA-Z0-9_\' -]*$', $_REQUEST['name']) !== false) {
150                                                 $reply_to = "$_REQUEST[name] <$reply_to>";
151                                         }
152                                 }
153                                 $subject = '~plural cap~ form submitted';
154                                 $email_template = new tem();
155                                 $email_template->load('~file_name~.email.txt');
156                                 $email_template->sets($data);
157                                 $message = $email_template->run();
158                                 $cc = '';
159                                 $bcc = '';
160                                 if(email($from, $to, $subject, $message, $reply_to, $cc, $bcc)) {
161                                         message('Due to an internal error, your message could not be sent. Please try again later.');
162                                         $error = true;
163                                 } else {
164                                         message('Message sent');
165                                 }
166                         }<!--~}~-->
167                         if($error !== true) {
168                                 return './~file_name~'; # FIXME is this the page you want to go to after successful form submission?
169                         }
170                 }
171                 # otherwise, we display the form again. ~file_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 {~-->
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                 $data = db_get_assoc('~table_name~', ~file_name upper~_DB_FIELDS, 'where id=%i', $edit_id);<!--~}~-->
178         } else {
179                 # form not submitted, you can set default values like so:
180                 #$data = array('~always_field~' => 'Yes');
181                 $data = array();
182         }<!--~upload_max {~-->
183
184         tem_set('upload_max_filesize', upload_max_filesize());<!--~}~-->
185
186         tem_set('form', $data);<!--~show_extra_headers {~-->
187         tem_set('extra_headers');<!--~}~-->
188 }
189
190 ?>