JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
ba0e507ae01f99f8534300ad6a328a2a2efef8dd
[wfpl-cms.git] / admin_files.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 # 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
9
10
11 # SETUP
12
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 # inc/wfpl/examples/db_connect.php
17 #
18 # if you rename any of the database fields, you'll need to update this:
19
20 define('ADMIN_FILES_DB_FIELDS', 'filename,description');
21
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/';
25
26 # emergency backup in case uploaders file name is blank somehow
27 $GLOBALS['filename_file_name'] = uniqid() . getmypid() . '.txt';
28
29
30 require_once('inc/wfpl/format.php');
31 require_once('inc/wfpl/email.php');
32 require_once('inc/wfpl/upload.php');
33
34 function admin_files_get_fields() {
35         $data = array();
36
37         $data['description'] = format_oneline(_REQUEST_cut('description'));
38
39         #header('Content-Type: text/plain');
40         #print_r(array($_REQUEST['filename'], $_FILES['filename']));
41         #exit();
42         $filename_filename_tmp = format_filename($_FILES['filename']['name']);
43         if(!$filename_filename_tmp) {
44                 $filename_filename_tmp = $GLOBALS['filename_file_name'];
45         }
46         if($_FILES['filename'] && $_FILES['filename']['error'] == 0) {
47                 $data['filename'] = save_uploaded_file('filename', $GLOBALS['upload_directory'] . $filename_filename_tmp);
48         } else {
49                 if(_REQUEST_cut('delete_filename') == 'Yes') {
50                         $data['filename'] = '';
51                 } else {
52                         $data['filename'] = format_path(_REQUEST_cut('old_filename'));
53                 }
54         }
55         unset($_FILES['filename']);
56
57         return $data;
58 }
59
60
61 function admin_files_main() {
62         if(logged_in_as_admin()) {
63                 tem_set('admin_privs');
64         } else {
65                 $_REQUEST['url'] = this_url();
66                 return 'admin_login';
67         }
68
69         $id = _REQUEST_cut('edit_id');
70         if($id) {
71                 return admin_files_main_form($id);
72         }
73
74         $id = _REQUEST_cut('admin_files_delete_id');
75         if($id) {
76                 return admin_files_main_delete($id);
77         }
78
79         if(_REQUEST_cut('new')) {
80                 return admin_files_main_form();
81         }
82
83         if(_REQUEST_cut('list')) {
84                 return admin_files_main_listing();
85         }
86
87         if(isset($_POST['description'])) {
88                 return admin_files_main_form();
89         }
90
91         # default action:
92         return admin_files_main_listing();
93 }
94
95 function admin_files_main_delete($id) {
96         $fn = db_get_value('files', 'filename', 'where id=%i', $id);
97         if ($fn) {
98                 unlink($fn);
99                 db_delete('files', 'where id=%i', $id);
100                 message('File deleted.');
101         } else {
102                 message("Couldn't find file to delete. Maybe it's already been deleted?");
103         }
104         return './admin_files';
105 }
106
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);
110 }
111
112 function admin_files_main_form($id = false) {
113         if($id) {
114                 tem_set('id', $id);
115         }
116
117         if(isset($_POST['description'])) {
118                 $data = admin_files_get_fields();
119
120                 if("you're happy with the POSTed values") {
121                         if($id) {
122                                 db_update_assoc('files', $data, 'where id=%i', $id);
123                                 message('File updated.');
124                         } else {
125                                 db_insert_assoc('files', $data);
126                                 message('File saved.');
127                         }
128                         if($error !== true) {
129                                 return './admin_files';
130                         }
131                 }
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.
136         } elseif($id) {
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);
139         } else {
140                 # form not submitted, you can set default values like so:
141                 #$data = array('description' => 'Yes');
142                 $data = array();
143         }
144
145         tem_set('upload_max_filesize', upload_max_filesize());
146
147         tem_set('form', $data);
148 }