JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla admin_files from metaform
[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'] = 'uploads/';
25
26 $GLOBALS['filename_file_name'] = uniqid() . getmypid() . '.txt'; # comment this out to use uploader's filename
27
28
29 require_once('code/wfpl/format.php');
30 require_once('code/wfpl/email.php');
31 require_once('code/wfpl/upload.php');
32
33 function admin_files_get_fields() {
34         $data = array();
35
36         $data['description'] = format_oneline(_REQUEST_cut('description'));
37
38         $filename_filename_tmp = $GLOBALS['filename_file_name'];
39         if(!$filename_filename_tmp) {
40                 $filename_filename_tmp = format_filename(_REQUEST_cut('filename'));
41         }
42         if($_FILES['filename'] && $_FILES['filename']['error'] == 0) {
43                 $data['filename'] = save_uploaded_file('filename', $GLOBALS['upload_directory'] . $filename_filename_tmp);
44         } else {
45                 if(_REQUEST_cut('delete_filename') == 'Yes') {
46                         $data['filename'] = '';
47                 } else {
48                         $data['filename'] = format_path(_REQUEST_cut('old_filename'));
49                 }
50         }
51         unset($_FILES['filename']);
52
53         return $data;
54 }
55
56
57 function admin_files_main() {
58         if(logged_in_as_admin()) {
59                 tem_set('admin_privs');
60         } else {
61                 $_REQUEST['url'] = this_url();
62                 return 'admin_login';
63         }
64
65         $id = _REQUEST_cut('edit_id');
66         if($id) {
67                 return admin_files_main_form($id);
68         }
69
70         $id = _REQUEST_cut('admin_files_delete_id');
71         if($id) {
72                 return admin_files_main_delete($id);
73         }
74
75         if(_REQUEST_cut('new')) {
76                 return admin_files_main_form();
77         }
78
79         if(_REQUEST_cut('list')) {
80                 return admin_files_main_listing();
81         }
82
83         if(isset($_POST['description'])) {
84                 return admin_files_main_form();
85         }
86
87         # default action:
88         return admin_files_main_listing();
89 }
90
91 function admin_files_main_delete($id) {
92         db_delete('files', 'where id=%i', $id);
93         message('File deleted.');
94         return './admin_files';
95 }
96
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);
100 }
101
102 function admin_files_main_form($id = false) {
103         if($id) {
104                 tem_set('id', $id);
105         }
106
107         if(isset($_POST['description'])) {
108                 $data = admin_files_get_fields();
109
110                 if("you're happy with the POSTed values") {
111                         if($id) {
112                                 db_update_assoc('files', $data, 'where id=%i', $id);
113                                 message('File updated.');
114                         } else {
115                                 db_insert_assoc('files', $data);
116                                 message('File saved.');
117                         }
118                         if($error !== true) {
119                                 return './admin_files';
120                         }
121                 }
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.
126         } elseif($id) {
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);
129         } else {
130                 # form not submitted, you can set default values like so:
131                 #$data = array('description' => 'Yes');
132                 $data = array();
133         }
134
135         tem_set('upload_max_filesize', upload_max_filesize());
136
137         tem_set('form', $data);
138 }