JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
added admin_files, s/this_host/$host/, etc
[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 # code/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('code/wfpl/format.php');
31 require_once('code/wfpl/email.php');
32 require_once('code/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         db_delete('files', 'where id=%i', $id);
97         message('File deleted.');
98         return './admin_files';
99 }
100
101 function admin_files_main_listing() {
102         $listing_rows = db_get_assocs('files', 'id,filename,description', 'order by coalesce(nullif(description, ""), substring(filename, 7)) limit 100');
103         tem_set('listings', $listing_rows);
104 }
105
106 function admin_files_main_form($id = false) {
107         if($id) {
108                 tem_set('id', $id);
109         }
110
111         if(isset($_POST['description'])) {
112                 $data = admin_files_get_fields();
113
114                 if("you're happy with the POSTed values") {
115                         if($id) {
116                                 db_update_assoc('files', $data, 'where id=%i', $id);
117                                 message('File updated.');
118                         } else {
119                                 db_insert_assoc('files', $data);
120                                 message('File saved.');
121                         }
122                         if($error !== true) {
123                                 return './admin_files';
124                         }
125                 }
126                 # otherwise, we display the form again. admin_files_get_fields() has
127                 # already put the posted values back into the template engine, so they will
128                 # show up in the form fields. You should add some message asking people to
129                 # fix their entry in whatever way you require.
130         } elseif($id) {
131                 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
132                 $data = db_get_assoc('files', ADMIN_FILES_DB_FIELDS, 'where id=%i', $id);
133         } else {
134                 # form not submitted, you can set default values like so:
135                 #$data = array('description' => 'Yes');
136                 $data = array();
137         }
138
139         tem_set('upload_max_filesize', upload_max_filesize());
140
141         tem_set('form', $data);
142 }