JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
071728ffbda8a60ce7b0ae49cd887281280d1d9f
[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 {~-->
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_1 {~-->
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/template.php');
39 require_once('code/wfpl/format.php');
40 require_once('code/wfpl/messages.php');
41 require_once('code/wfpl/email.php');<!--~opt_db_2 {~-->
42 require_once('code/db_connect.php');<!--~}~--><!--~uploads_include {~-->
43 require_once('code/wfpl/upload.php');<!--~}~-->
44
45 function ~file_name~_get_fields() {
46         $data = array();
47 <!--~formats {~-->
48         $data['~name~'] = format_~format~($_REQUEST['~name~']<!--~pulldown_format_extra {~-->, '~name~'<!--~}~-->);<!--~}~--><!--~image_upload {~-->
49         if($_FILES['~name~'] && $_FILES['~name~']['error'] == 0) {
50                 $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']<!--~}~-->);
51         } else {
52                 if($_REQUEST['delete_~name~'] == 'Yes') {
53                         $data['~name~'] = '';
54                 } else {
55                         $data['~name~'] = format_image_w_h<!--~thumb_w_h {~-->_thumb_w_h<!--~}~-->($_REQUEST['old_~name~']);
56                 }
57         }<!--~}~--><!--~file_upload {~-->
58         $~name~_filename_tmp = $GLOBALS['~name~_file_name'];
59         if(!$~name~_filename_tmp) {
60                 $~name~_filename_tmp = format_filename($_REQUEST['~name~']);
61         }
62         if($_FILES['~name~'] && $_FILES['~name~']['error'] == 0) {
63                 $data['~name~'] = save_uploaded_file('~name~', $GLOBALS['upload_directory'] . $~name~_filename_tmp);
64         } else {
65                 if($_REQUEST['delete_~name~'] == 'Yes') {
66                         $data['~name~'] = '';
67                 } else {
68                         $data['~name~'] = format_path($_REQUEST['old_~name~']);
69                 }
70         }<!--~}~-->
71
72         return $data;
73 }
74
75 <!--~opt_listing_2 {~-->
76 # You may pass a "where clause" for the db query.
77 function ~file_name~_display_listing($where = 'order by ~always_field~ limit 100') {
78         $rows = db_get_assocs('~table_name~', 'id<!--~listing_fields_1 {~-->,~name~<!--~}~-->', $where);
79         if($rows == false || count($rows) == 0) {
80                 tem_set('listings', array('empty_listing' => true));
81                 return;
82         }
83
84         # make sure there's something clickable
85         foreach($rows as &$row) {
86                 if($row['~always_field~'] == '') {
87                         $row['~always_field~'] = '--';
88                 }
89         }
90         tem_set('listings', array(
91                 'populated_listing' => true,
92                 'rows' => $rows));
93         return true;
94 }
95 <!--~}~-->
96 function ~file_name~_main() {<!--~opt_pass {~-->
97         if(!logged_in_as_admin()) {
98                 $GLOBALS['url'] = this_url();
99                 return 'admin_login';
100         }
101 <!--~}~--><!--~opt_display_1 {~-->
102         if(isset($_REQUEST['~file_name~_id'])) {
103                 return ~file_name~_display_main();
104         } else {
105                 return ~file_name~_edit_main();
106         }
107 }
108
109 function ~file_name~_display_main() {
110         $id = format_int($_REQUEST['~file_name~_id']);
111         unset($_REQUEST['~file_name~_id']);
112         if(!$id) {
113                 message('Error: Broken link');
114                 return './~file_name~';
115         }
116         $row = db_get_assoc('~table_name~', 'id,'.~file_name upper~_DB_FIELDS, 'where id=%i', $id);
117         if(!$row) {
118                 message('Error: ~singular cap~ not found');
119                 return './~file_name~';
120         }
121         tem_set('display', $row);
122 }
123
124 function ~file_name~_edit_main() {<!--~}~--><!--~pulldowns {~-->
125         pulldown('~name~', array('option 1', 'option 2', 'option 3'));
126 <!--~}~--><!--~opt_db_3 {~-->
127         $edit_id = format_int($_REQUEST['~file_name~_edit_id']);
128         unset($_REQUEST['~file_name~_edit_id']);
129         if($edit_id) {
130                 # add hidden field for database id of row we're editing
131                 tem_set('~file_name~_edit_id', $edit_id);
132                 tem_set('editing', 'show');
133                 tem_set('edit_msg', 'show');
134         }
135
136         $delete_id = format_int($_REQUEST['~file_name~_delete_id']);
137         unset($_REQUEST['~file_name~_delete_id']);
138         if($delete_id) {
139                 db_delete('~table_name~', 'where id=%i', $delete_id);
140                 message('~singular cap~ deleted.');
141
142                 return './~file_name~';
143         }
144
145         if(!$edit_id) {<!--~opt_listing_1 {~-->
146                 if(!isset($_REQUEST['~file_name~_new']) && !isset($_REQUEST['~always_field~'])) {
147                         ~file_name~_display_listing();
148                         return;
149                 }
150 <!--~}~-->
151                 tem_set('new_msg', 'show');
152         }
153 <!--~}~-->
154         if(isset($_POST['~always_field~'])) {
155                 $data = ~file_name~_get_fields();
156
157                 if("you're happy with the POSTed values") {<!--~opt_db_4 {~-->
158                         if($edit_id) {
159                                 db_update_assoc('~table_name~', $data, 'where id=%i', $edit_id);
160                                 message('~singular cap~ updated.');
161                         } else {
162                                 db_insert_assoc('~table_name~', $data);
163                                 message('~singular cap~ saved.');
164                         }<!--~}~--><!--~opt_email_2 {~-->
165                         if($GLOBALS['~file_name~_form_recipient'] != 'fixme@example.com') {
166                                 $to = $GLOBALS['~file_name~_form_recipient'];
167                                 $from = 'noreply@~this_domain~';
168                                 $reply_to = $to;
169                                 if(isset($_REQUEST['email']) and valid_email($_REQUEST['email'])) {
170                                         $reply_to = $_REQUEST['email'];
171                                         if($_REQUEST['name'] and ereg('^[a-zA-Z0-9_\' -]*$', $_REQUEST['name']) !== false) {
172                                                 $reply_to = "$_REQUEST[name] <$reply_to>";
173                                         }
174                                 }
175                                 $subject = '~plural cap~ form submitted';
176                                 $email_template = new tem();
177                                 $email_template->load('~file_name~.email.txt');
178                                 $email_template->sets($data);
179                                 $message = $email_template->run();
180                                 $cc = '';
181                                 $bcc = '';
182                                 if(email($from, $to, $subject, $message, $reply_to, $cc, $bcc)) {
183                                         message('Due to an internal error, your message could not be sent. Please try again later.');
184                                         $error = true;
185                                 } else {
186                                         message('Message sent');
187                                 }
188                         }<!--~}~-->
189                         if($error !== true) {
190                                 return './~file_name~'; # FIXME is this the page you want to go to after successful form submission?
191                         }
192                 }
193                 # otherwise, we display the form again. ~file_name~_get_fields() has
194                 # already put the posted values back into the template engine, so they will
195                 # show up in the form fields. You should add some message asking people to
196                 # fix their entry in whatever way you require.<!--~opt_db_5 {~-->
197         } elseif($edit_id) {
198                 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
199                 $data = db_get_assoc('~table_name~', ~file_name upper~_DB_FIELDS, 'where id=%i', $edit_id);<!--~}~-->
200         } else {
201                 # form not submitted, you can set default values like so:
202                 #$data = array('~always_field~' => 'Yes');
203                 $data = array();
204         }<!--~upload_max {~-->
205
206         tem_set('upload_max_filesize', upload_max_filesize());<!--~}~-->
207
208         tem_set('form', $data);<!--~show_extra_headers {~-->
209         tem_set('extra_headers', 'show');<!--~}~-->
210 }
211
212 ?>