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'] = 'uploads/';
26 $GLOBALS['filename_file_name'] = uniqid() . getmypid() . '.txt'; # comment this out to use uploader's filename
29 require_once('code/wfpl/format.php');
30 require_once('code/wfpl/email.php');
31 require_once('code/wfpl/upload.php');
33 function admin_files_get_fields() {
36 $data['description'] = format_oneline(_REQUEST_cut('description'));
38 $filename_filename_tmp = $GLOBALS['filename_file_name'];
39 if(!$filename_filename_tmp) {
40 $filename_filename_tmp = format_filename(_REQUEST_cut('filename'));
42 if($_FILES['filename'] && $_FILES['filename']['error'] == 0) {
43 $data['filename'] = save_uploaded_file('filename', $GLOBALS['upload_directory'] . $filename_filename_tmp);
45 if(_REQUEST_cut('delete_filename') == 'Yes') {
46 $data['filename'] = '';
48 $data['filename'] = format_path(_REQUEST_cut('old_filename'));
51 unset($_FILES['filename']);
57 function admin_files_main() {
58 if(logged_in_as_admin()) {
59 tem_set('admin_privs');
61 $_REQUEST['url'] = this_url();
65 $id = _REQUEST_cut('edit_id');
67 return admin_files_main_form($id);
70 $id = _REQUEST_cut('admin_files_delete_id');
72 return admin_files_main_delete($id);
75 if(_REQUEST_cut('new')) {
76 return admin_files_main_form();
79 if(_REQUEST_cut('list')) {
80 return admin_files_main_listing();
83 if(isset($_POST['description'])) {
84 return admin_files_main_form();
88 return admin_files_main_listing();
91 function admin_files_main_delete($id) {
92 db_delete('files', 'where id=%i', $id);
93 message('File deleted.');
94 return './admin_files';
97 function admin_files_main_listing() {
98 $listing_rows = db_get_assocs('files', 'id,filename,description', 'order by description limit 100');
99 tem_set('listings', $listing_rows);
102 function admin_files_main_form($id = false) {
107 if(isset($_POST['description'])) {
108 $data = admin_files_get_fields();
110 if("you're happy with the POSTed values") {
112 db_update_assoc('files', $data, 'where id=%i', $id);
113 message('File updated.');
115 db_insert_assoc('files', $data);
116 message('File saved.');
118 if($error !== true) {
119 return './admin_files';
122 # otherwise, we display the form again. admin_files_get_fields() has
123 # already put the posted values back into the template engine, so they will
124 # show up in the form fields. You should add some message asking people to
125 # fix their entry in whatever way you require.
127 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
128 $data = db_get_assoc('files', ADMIN_FILES_DB_FIELDS, 'where id=%i', $id);
130 # form not submitted, you can set default values like so:
131 #$data = array('description' => 'Yes');
135 tem_set('upload_max_filesize', upload_max_filesize());
137 tem_set('form', $data);