| 1 |
<?php
|
| 2 |
|
| 3 |
function comment_upload_comment($op, $comment) {
|
| 4 |
static $file;
|
| 5 |
$cid = is_object($comment) ? $comment->cid : $comment['cid'];
|
| 6 |
switch ($op) {
|
| 7 |
case 'form post':
|
| 8 |
return form_file(t('Attachment'), 'file', 50, $file || ($cid && _comment_upload_load_file($cid)) ? t('You already have a file uploaded, if you upload another it\'ll overwrite the current one.') :'');
|
| 9 |
break;
|
| 10 |
case 'form param';
|
| 11 |
return array('enctype' => 'multipart/form-data');
|
| 12 |
case 'validate':
|
| 13 |
if ($file = file_check_upload('file')) {
|
| 14 |
$file = file_save_upload('file');
|
| 15 |
}
|
| 16 |
break;
|
| 17 |
case 'insert':
|
| 18 |
if ($file) {
|
| 19 |
file_save_upload($file, $file->filename);
|
| 20 |
$fid = db_next_id('{files}_fid');
|
| 21 |
db_query("INSERT INTO {files} (fid, nid, filename, filepath, filemime, filesize) VALUES (%d, %d, '%s', '%s', '%s', %d)", $fid, 0, $file->filename, $file->filename, $file->filemime, $file->filesize);
|
| 22 |
db_query("INSERT INTO {comment_files} (cid, fid) VALUES (%d, %d)", $cid, $fid);
|
| 23 |
}
|
| 24 |
break;
|
| 25 |
case 'update':
|
| 26 |
// admin comment edit does not call validate, so we need to $file = file_check_upload('file')
|
| 27 |
if ($file || ($file = file_check_upload('file'))) {
|
| 28 |
file_save_upload($file, $file->filename);
|
| 29 |
$fid = _comment_upload_delete($cid);
|
| 30 |
db_query("UPDATE {files} SET filename = '%s', filepath = '%s', filemime = '%s', filesize = %d WHERE fid = %d", $file->filename, $file->filename, $file->filemime, $file->filesize, $fid);
|
| 31 |
}
|
| 32 |
break;
|
| 33 |
case 'delete':
|
| 34 |
_comment_upload_delete($cid, TRUE);
|
| 35 |
break;
|
| 36 |
case 'view':
|
| 37 |
if ($file) {
|
| 38 |
$file_view = $file; // this is for preview
|
| 39 |
}
|
| 40 |
else {
|
| 41 |
$file_view = _comment_upload_load_file($cid);
|
| 42 |
}
|
| 43 |
if ($file_view) {
|
| 44 |
return '<div class="comment_attachment"><a href="'. check_url(file_create_url($file_view->filepath)) .'">'. check_plain($file_view->filename) .' ('. format_size($file_view->filesize) .')</a></div>';
|
| 45 |
}
|
| 46 |
break;
|
| 47 |
}
|
| 48 |
}
|
| 49 |
|
| 50 |
function _comment_upload_load_file($cid) {
|
| 51 |
return db_fetch_object(db_query('SELECT * FROM {files} f INNER JOIN {comment_files} cf ON f.fid = cf.fid WHERE cf.cid = %d', $cid));
|
| 52 |
}
|
| 53 |
|
| 54 |
function _comment_upload_delete($cid, $db = FALSE) {
|
| 55 |
if ($file = _comment_upload_load_file($cid)) {
|
| 56 |
file_delete($file->filepath);
|
| 57 |
if ($db) {
|
| 58 |
db_query('DELETE FROM {files} WHERE fid = %d', $file->fid);
|
| 59 |
db_query('DELETE FROM {comment_files} WHERE cid = %d', $cid);
|
| 60 |
}
|
| 61 |
return $file->fid;
|
| 62 |
}
|
| 63 |
}
|