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