JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
metaform: php refactor, opt_public_*
[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_cut('~name~')<!--~pulldown_format_extra {~-->, '~name~'<!--~}~-->);<!--~}~--><!--~image_upload {~-->
46
47         if($_FILES['~name~'] && $_FILES['~name~']['error'] == 0) {
48                 $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']<!--~}~-->);
49         } else {
50                 if(_REQUEST_cut('delete_~name~') == 'Yes') {
51                         $data['~name~'] = '';
52                 } else {
53                         $data['~name~'] = format_image_w_h<!--~thumb_w_h {~-->_thumb_w_h<!--~}~-->(_REQUEST_cut('old_~name~'));
54                 }
55         }
56         unset($_FILES['~name~']);<!--~}~--><!--~file_upload {~-->
57
58         $~name~_filename_tmp = $GLOBALS['~name~_file_name'];
59         if(!$~name~_filename_tmp) {
60                 $~name~_filename_tmp = format_filename(_REQUEST_cut('~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_cut('delete_~name~') == 'Yes') {
66                         $data['~name~'] = '';
67                 } else {
68                         $data['~name~'] = format_path(_REQUEST_cut('old_~name~'));
69                 }
70         }
71         unset($_FILES['~name~']);<!--~}~-->
72
73         return $data;
74 }<!--~opt_public_something {~-->
75
76
77 function ~public_file_name~_main() {<!--~opt_public_display {~-->
78         $id = _REQUEST_cut('~public_file_name~_id');
79         if($id) {
80                 return ~file_name~_main_display($id);<!--~opt_public_form unset {~-->
81         } else {
82                 message("Error: Broken Link (~singular cap~ #$id not found)");
83                 return './'; # FIXME where should this go?<!--~}~-->
84         }<!--~}~--><!--~opt_public_form {~-->
85         return ~file_name~_main_form();<!--~}~-->
86 }<!--~}~-->
87
88
89 function ~file_name~_main(<!--~opt_display unset {~--><!--~opt_public_form {~-->$new_only = false<!--~}~--><!--~}~-->) {<!--~opt_pass {~-->
90         if(logged_in_as_admin()) {
91                 tem_set('admin_privs');
92         } else {
93                 $_REQUEST['url'] = this_url();
94                 return 'admin_login';
95         }
96 <!--~}~--><!--~opt_db {~-->
97         $id = _REQUEST_cut('~file_name~_edit_id');
98         if($id) {
99                 return ~file_name~_main_form($id);
100         }
101
102         $id = _REQUEST_cut('~file_name~_delete_id');
103         if($id) {
104                 return ~file_name~_main_delete($id);
105         }
106
107         if(_REQUEST_cut('~file_name~_new')) {
108                 return ~file_name~_main_form();
109         }
110 <!--~}~--><!--~opt_listing {~-->
111         if(_REQUEST_cut('~file_name~_list')) {
112                 return ~file_name~_main_listing();
113         }
114 <!--~}~--><!--~opt_display {~-->
115         $id = _REQUEST_cut('~file_name~_id');
116         if($id) {
117                 return ~file_name~_main_display($id);
118         }
119 <!--~}~--><!--~opt_listing {~-->
120         if(isset($_POST['~always_field~'])) {
121                 return ~file_name~_main_form();
122         }
123
124         # default action:
125         return ~file_name~_main_listing();<!--~}~--><!--~opt_listing unset {~-->
126         return ~file_name~_main_form();<!--~}~-->
127 }<!--~opt_display {~-->
128
129 function ~file_name~_main_display($id) {
130         $data = db_get_assoc('~table_name~', 'id,'.~file_name upper~_DB_FIELDS, 'where id=%i', $id);
131         if(!$data) {
132                 message("Error: Broken Link (~singular cap~ #$id not found)");<!--~opt_public_something {~-->
133                 return './' . $GLOBALS['wfpl_basename'];<!--~}~--><!--~opt_public_something unset {~-->
134                 return './~file_name~';<!--~}~-->
135         }
136         tem_set('display', $data);
137 }<!--~}~--><!--~opt_db {~-->
138
139 function ~file_name~_main_delete($id) {
140         db_delete('~table_name~', 'where id=%i', $id);
141         message('~singular cap~ deleted.');
142         return './~file_name~';
143 }<!--~}~--><!--~opt_listing {~-->
144
145 function ~file_name~_main_listing() {
146         $listing_rows = db_get_assocs('~table_name~', 'id<!--~listing_fields_1 {~-->,~name~<!--~}~-->', 'order by ~always_field~ limit 100');
147         tem_set('listings', $listing_rows);
148 }<!--~}~-->
149
150 function ~file_name~_main_form($id = false) {<!--~pulldowns {~-->
151         pulldown('~name~', ~pulldown_options~);<!--~}~--><!--~pulldowns once_if {~-->
152 <!--~}~--><!--~opt_db {~-->
153         if($id) {
154                 # add hidden field for database id of row we're editing
155                 tem_set('~file_name~_edit_id', $id);
156                 tem_set('editing');
157                 tem_set('edit_msg');
158         } else {
159                 tem_set('new_msg');
160         }
161 <!--~}~-->
162         if(isset($_POST['~always_field~'])) {
163                 $data = ~file_name~_get_fields();
164
165                 if("you're happy with the POSTed values") {<!--~opt_db {~-->
166                         if($id) {
167                                 db_update_assoc('~table_name~', $data, 'where id=%i', $id);
168                                 message('~singular cap~ updated.');
169                         } else {
170                                 db_insert_assoc('~table_name~', $data);
171                                 message('~singular cap~ saved.');
172                         }<!--~}~--><!--~opt_email {~-->
173                         if($GLOBALS['~file_name~_form_recipient'] != 'fixme@example.com') {
174                                 $to = $GLOBALS['~file_name~_form_recipient'];
175                                 $from = 'noreply@~this_domain~';
176                                 $reply_to = $to;
177                                 if(isset($_REQUEST['email']) and valid_email($_REQUEST['email'])) {
178                                         $reply_to = $_REQUEST['email'];
179                                         if($_REQUEST['name'] and ereg('^[a-zA-Z0-9_\' -]*$', $_REQUEST['name']) !== false) {
180                                                 $reply_to = "$_REQUEST[name] <$reply_to>";
181                                         }
182                                 }
183                                 $subject = '~plural cap~ form submitted';
184                                 $email_template = new tem();
185                                 $email_template->load('~file_name~.email.txt');
186                                 $email_template->sets($data);
187                                 $message = $email_template->run();
188                                 $cc = '';
189                                 $bcc = '';
190                                 if(email($from, $to, $subject, $message, $reply_to, $cc, $bcc)) {
191                                         message('Due to an internal error, your message could not be sent. Please try again later.');
192                                         $error = true;
193                                 } else {
194                                         message('Message sent');
195                                 }
196                         }<!--~}~-->
197                         if($error !== true) {<!--~opt_public_something {~-->
198                                 if($GLOBALS['wfpl_basename'] == '~file_name~') {
199                                         return './~file_name~';
200                                 } else {
201                                         # FIXME create this page or change this to go elsewhere
202                                         return './~public_file_name~_thanks';
203                                 }<!--~}~--><!--~opt_public_something unset {~--><!--~opt_db unset {~-->
204                                 # FIXME create this page or change this to go elsewhere<!--~}~-->
205                                 return './~file_name~<!--~opt_db unset {~-->_thanks<!--~}~-->';<!--~}~-->
206                         }
207                 }
208                 # otherwise, we display the form again. ~file_name~_get_fields() has
209                 # already put the posted values back into the template engine, so they will
210                 # show up in the form fields. You should add some message asking people to
211                 # fix their entry in whatever way you require.<!--~opt_db {~-->
212         } elseif($id) {
213                 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
214                 $data = db_get_assoc('~table_name~', ~file_name upper~_DB_FIELDS, 'where id=%i', $id);<!--~}~-->
215         } else {
216                 # form not submitted, you can set default values like so:
217                 #$data = array('~always_field~' => 'Yes');
218                 $data = array();
219         }<!--~upload_max {~-->
220
221         tem_set('upload_max_filesize', upload_max_filesize());<!--~}~-->
222
223         tem_set('form', $data);<!--~show_extra_headers {~-->
224         tem_set('extra_headers');<!--~}~-->
225 }