3 # This form requires wfpl. See: http://jasonwoof.org/wfpl
5 # This form was initially auto-generated. If you would like to alter the
6 # parameters and generate a new one try this URL:
8 # http://metaform.l/?file_name=admin_files&table_name=files&singular=file&plural=files&opt_email=No&opt_db=Yes&opt_listing=Yes&opt_display=No&opt_pass=Yes&opt_public_form=No&opt_public_display=No&fields=filename+file%0D%0Adescription+textbox&edit=yes
13 # To save results to a database, you'll need to create the files table
14 # (the file admin_files.sql should help with this), and create the file
15 # 'code/db_connect.php' which calls db_connect() see:
16 # code/wfpl/examples/db_connect.php
18 # if you rename any of the database fields, you'll need to update this:
20 define('ADMIN_FILES_DB_FIELDS', 'filename,description');
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'] = 'files/';
26 # emergency backup in case uploaders file name is blank somehow
27 $GLOBALS['filename_file_name'] = uniqid() . getmypid() . '.txt';
30 require_once('code/wfpl/format.php');
31 require_once('code/wfpl/email.php');
32 require_once('code/wfpl/upload.php');
34 function admin_files_get_fields() {
37 $data['description'] = format_oneline(_REQUEST_cut('description'));
39 #header('Content-Type: text/plain');
40 #print_r(array($_REQUEST['filename'], $_FILES['filename']));
42 $filename_filename_tmp = format_filename($_FILES['filename']['name']);
43 if(!$filename_filename_tmp) {
44 $filename_filename_tmp = $GLOBALS['filename_file_name'];
46 if($_FILES['filename'] && $_FILES['filename']['error'] == 0) {
47 $data['filename'] = save_uploaded_file('filename', $GLOBALS['upload_directory'] . $filename_filename_tmp);
49 if(_REQUEST_cut('delete_filename') == 'Yes') {
50 $data['filename'] = '';
52 $data['filename'] = format_path(_REQUEST_cut('old_filename'));
55 unset($_FILES['filename']);
61 function admin_files_main() {
62 if(logged_in_as_admin()) {
63 tem_set('admin_privs');
65 $_REQUEST['url'] = this_url();
69 $id = _REQUEST_cut('edit_id');
71 return admin_files_main_form($id);
74 $id = _REQUEST_cut('admin_files_delete_id');
76 return admin_files_main_delete($id);
79 if(_REQUEST_cut('new')) {
80 return admin_files_main_form();
83 if(_REQUEST_cut('list')) {
84 return admin_files_main_listing();
87 if(isset($_POST['description'])) {
88 return admin_files_main_form();
92 return admin_files_main_listing();
95 function admin_files_main_delete($id) {
96 $fn = db_get_value('files', 'filename', 'where id=%i', $id);
99 db_delete('files', 'where id=%i', $id);
100 message('File deleted.');
102 message("Couldn't find file to delete. Maybe it's already been deleted?");
104 return './admin_files';
107 function admin_files_main_listing() {
108 $listing_rows = db_get_assocs('files', 'id,filename,description', 'order by coalesce(nullif(description, ""), substring(filename, 7)) limit 100');
109 tem_set('listings', $listing_rows);
112 function admin_files_main_form($id = false) {
117 if(isset($_POST['description'])) {
118 $data = admin_files_get_fields();
120 if("you're happy with the POSTed values") {
122 db_update_assoc('files', $data, 'where id=%i', $id);
123 message('File updated.');
125 db_insert_assoc('files', $data);
126 message('File saved.');
128 if($error !== true) {
129 return './admin_files';
132 # otherwise, we display the form again. admin_files_get_fields() has
133 # already put the posted values back into the template engine, so they will
134 # show up in the form fields. You should add some message asking people to
135 # fix their entry in whatever way you require.
137 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
138 $data = db_get_assoc('files', ADMIN_FILES_DB_FIELDS, 'where id=%i', $id);
140 # form not submitted, you can set default values like so:
141 #$data = array('description' => 'Yes');
145 tem_set('upload_max_filesize', upload_max_filesize());
147 tem_set('form', $data);