JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
clean up my urls
[wfpl.git] / metaform / template.php
1 <?php
2
3 # This form requires wfpl. See: http://sametwice.com/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
18 #
19 # if you rename any of the database fields, you'll need to update this:
20 define('~file_name upper~_DB_FIELDS', '~db_fields~');
21 ~}~~upload_settings {~
22 # Set this to the path to your uploads directory. It can be relative to the
23 # location of this script. IT MUST END WITH A SLASH
24 $GLOBALS['upload_directory'] = 'uploads/';
25 ~}~~file_settings {~
26 $GLOBALS['~name~_file_name'] = uniqid() . getmypid() . '.txt'; # comment this out to use uploader's filename
27 ~}~~image_settings {~
28 $GLOBALS['~name~_max_width'] = '400';
29 $GLOBALS['~name~_max_height'] = '400';~thumb_settings {~
30 $GLOBALS['~name~_thumb_max_width'] = '70';
31 $GLOBALS['~name~_thumb_max_height'] = '70';
32 $GLOBALS['~name~_file_name'] = uniqid() . getmypid() . '.jpg'; # comment this out to use uploader's filename
33 ~}~~}~
34
35 require_once(__DIR__.'/'.'inc/wfpl/format.php');~opt_email {~
36 require_once(__DIR__.'/'.'inc/wfpl/email.php');~}~~uploads_include {~
37 require_once(__DIR__.'/'.'inc/wfpl/upload.php');~}~
38
39 $GLOBALS['~file_name~_field_to_caption'] = array(~name_to_caption {~
40         '~name~' => '~caption phpsq~'~ sep {~,~}~~}~
41 );
42
43 function ~file_name~_get_fields() {
44         $data = array();
45 ~formats {~
46         $data['~name~'] = format_~format~(_REQUEST_cut('~name~')~pulldown_format_extra {~, '~name~'~}~);~}~~image_upload {~
47
48         if ($_FILES['~name~'] && $_FILES['~name~']['error'] == 0) {
49                 $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']~}~);
50         } else {
51                 if (_REQUEST_cut('delete_~name~') == 'Yes') {
52                         $data['~name~'] = '';
53                 } else {
54                         $data['~name~'] = format_image_w_h~thumb_w_h {~_thumb_w_h~}~(_REQUEST_cut('old_~name~'));
55                 }
56         }
57         unset($_FILES['~name~']);~}~~file_upload {~
58
59         $~name~_filename_tmp = $GLOBALS['~name~_file_name'];
60         if (!$~name~_filename_tmp) {
61                 $~name~_filename_tmp = format_filename(_REQUEST_cut('~name~'));
62         }
63         if ($_FILES['~name~'] && $_FILES['~name~']['error'] == 0) {
64                 $data['~name~'] = save_uploaded_file('~name~', $GLOBALS['upload_directory'] . $~name~_filename_tmp);
65         } else {
66                 if (_REQUEST_cut('delete_~name~') == 'Yes') {
67                         $data['~name~'] = '';
68                 } else {
69                         $data['~name~'] = format_path(_REQUEST_cut('old_~name~'));
70                 }
71         }
72         unset($_FILES['~name~']);~}~
73
74         return $data;
75 }~opt_public_something {~
76
77
78 function ~public_file_name~_main() {~opt_public_display {~
79         $id = _REQUEST_cut('id');
80         if ($id) {
81                 return ~file_name~_main_display($id);~opt_public_form unset {~
82         } else {
83                 message("Error: Broken Link (~singular cap~ #$id not found)");
84                 return './'; # FIXME where should this go?~}~
85         }~}~~opt_public_form {~
86         return ~file_name~_main_form();~}~
87 }~}~
88
89
90 function ~file_name~_main() {~opt_pass {~
91         if (logged_in_as_admin()) {
92                 tem_set('admin_privs');
93         } else {
94                 $_REQUEST['url'] = this_url();
95                 return 'admin_login';
96         }
97 ~}~~opt_db {~
98         $id = _REQUEST_cut('edit_id');
99         if ($id) {
100                 return ~file_name~_main_form($id);
101         }
102
103         $id = _REQUEST_cut('~file_name~_delete_id');
104         if ($id) {
105                 return ~file_name~_main_delete($id);
106         }
107
108         if (_REQUEST_cut('new')) {
109                 return ~file_name~_main_form();
110         }
111 ~}~~opt_listing {~
112         if (_REQUEST_cut('list')) {
113                 return ~file_name~_main_listing();
114         }
115 ~}~~opt_display {~
116         $id = _REQUEST_cut('id');
117         if ($id) {
118                 return ~file_name~_main_display($id);
119         }
120 ~}~~opt_listing {~
121         if (_REQUEST_cut('download_csv')) {
122                 return ~file_name~_csv_download();
123         }
124
125         if (isset($_POST['~always_field~'])) {
126                 return ~file_name~_main_form();
127         }
128
129         # default action:
130         return ~file_name~_main_listing();~}~~opt_listing unset {~
131         return ~file_name~_main_form();~}~
132 }~opt_display {~
133
134 function ~file_name~_main_display($id) {
135         $data = db_get_assoc('~table_name~', 'id,'.~file_name upper~_DB_FIELDS, 'where id=%i', $id);
136         if (!$data) {
137                 message("Error: Broken Link (~singular cap~ #$id not found)");~opt_public_something {~
138                 return './' . $GLOBALS['wfpl_basename'];~}~~opt_public_something unset {~
139                 return './~file_name~';~}~
140         }
141         tem_set('display', $data);
142 }~}~~opt_db {~
143
144 function ~file_name~_main_delete($id) {
145         db_delete('~table_name~', 'where id=%i', $id);
146         message('~singular cap~ deleted.');
147         return './~file_name~';
148 }~}~~opt_listing {~
149
150 function ~file_name~_csv_download() {
151         require_once(__DIR__.'/'.'inc/wfpl/csv.php');
152         $rows = db_get_rows('~table_name~', 'id,'.~file_name upper~_DB_FIELDS, 'order by id');
153         $fields = explode(',', 'id,'.~file_name upper~_DB_FIELDS);
154         $header = array();
155         foreach ($fields as $field) {
156                 if (isset($GLOBALS['~file_name~_field_to_caption'][$field])) {
157                         $header[] = $GLOBALS['~file_name~_field_to_caption'][$field];
158                 } else {
159                         $header[] = $field;
160                 }
161         }
162         array_unshift($rows, $header);
163         array2d_to_csv_download($rows, '~file_name~.csv');
164 }
165
166 function ~file_name~_main_listing() {
167         $data = array();
168         $desc = '';
169         $sort = _REQUEST_cut('sort');
170         if ($sort && substr($sort, 0, 1) === '-') {
171                 $sort = substr($sort, 1);
172                 $desc = ' DESC ';
173         } else {
174                 $data["sorting-by-$sort"] = '-';
175         }
176         $legal_sorts = explode(',', ~file_name upper~_DB_FIELDS);
177         if (!$sort || !in_array($sort, $legal_sorts)) {
178                 $sort = '~always_field~';
179         }
180
181         $data['rows'] = db_get_assocs('~table_name~', 'id~listing_fields_1 {~,~name~~}~', "order by $sort $desc limit 1000");
182         tem_set('listings', $data);
183 }~}~
184
185 function ~file_name~_main_form($id = false) {~pulldowns {~
186         pulldown('~name~', ~pulldown_options~);~}~~has_pulldowns {~
187 ~}~~opt_db {~
188         if ($id) {
189                 tem_set('id', $id);
190         }
191 ~}~
192         if (isset($_POST['~always_field~'])) {
193                 $data = ~file_name~_get_fields();
194
195                 if ("you're happy with the POSTed values") {~opt_db {~
196                         if ($id) {
197                                 db_update_assoc('~table_name~', $data, 'where id=%i', $id);
198                                 message('~singular cap~ updated.');
199                         } else {
200                                 db_insert_assoc('~table_name~', $data);
201                                 message('~singular cap~ saved.');
202                         }~}~~opt_email {~
203                         if ($GLOBALS['~file_name~_form_recipient'] != 'fixme@example.com') {
204                                 $to = $GLOBALS['~file_name~_form_recipient'];
205                                 $from = 'noreply@~this_domain~';
206                                 $reply_to = $to;
207                                 if (isset($data['email']) and valid_email($data['email'])) {
208                                         $reply_to = $data['email'];
209                                         if ($data['name'] and ereg('^[a-zA-Z0-9_\' -]*$', $data['name']) !== false) {
210                                                 $reply_to = "$data[name] <$reply_to>";
211                                         }
212                                 }
213                                 $subject = '~plural cap~ form submitted';
214                                 $email_template = new tem();
215                                 $email_template->load('~public_file_name~.email.txt');
216                                 $email_template->sets($data);
217                                 $message = $email_template->run();
218                                 $cc = '';
219                                 $bcc = '';
220                                 if (email($from, $to, $subject, $message, $reply_to, $cc, $bcc)) {
221                                         message('Due to an internal error, your message could not be sent. Please try again later.');
222                                         $error = true;
223                                 } else {
224                                         message('Message sent');
225                                 }
226                         }~}~
227                         if ($error !== true) {~opt_public_something {~
228                                 if ($GLOBALS['wfpl_basename'] == '~file_name~') {
229                                         return './~file_name~';
230                                 } else {
231                                         # FIXME create this page or change this to go elsewhere
232                                         return './~public_file_name~_thanks';
233                                 }~}~~opt_public_something unset {~~opt_db unset {~
234                                 # FIXME create this page or change this to go elsewhere~}~
235                                 return './~file_name~~opt_db unset {~_thanks~}~';~}~
236                         }
237                 }
238                 # otherwise, we display the form again. We've got the form field
239                 # values in $data and will put those back in the filds below. You
240                 # should add some message asking people to fix their entry in
241                 # whatever way you require.~opt_db {~
242         } elseif ($id) {
243                 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
244                 $data = db_get_assoc('~table_name~', ~file_name upper~_DB_FIELDS, 'where id=%i', $id);~}~
245         } else {
246                 # form not submitted, you can set default values like so:
247                 #$data = array('~always_field~' => 'Yes');
248                 $data = array();
249         }~upload_max {~
250
251         tem_set('$upload_max_filesize', upload_max_filesize());~}~
252
253         tem_set('form', $data);~show_extra_headers {~
254         tem_set('$head');~}~
255 }