1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00

Fix for deleting documents

This commit is contained in:
Hillel Coren 2016-05-26 19:02:53 +03:00
parent 0da3741b56
commit 2043621ac9
5 changed files with 69 additions and 18 deletions

View File

@ -14,6 +14,7 @@ use App\Ninja\Repositories\DocumentRepository;
use App\Http\Requests\DocumentRequest;
use App\Http\Requests\CreateDocumentRequest;
use App\Http\Requests\UpdateDocumentRequest;
class DocumentController extends BaseController
{
@ -26,20 +27,20 @@ class DocumentController extends BaseController
$this->documentRepo = $documentRepo;
}
public function get(DocumentRequest $request)
{
return static::getDownloadResponse($request->entity());
}
public static function getDownloadResponse($document){
$direct_url = $document->getDirectUrl();
if($direct_url){
return redirect($direct_url);
}
$stream = $document->getStream();
if($stream){
$headers = [
'Content-Type' => Document::$types[$document->type]['mime'],
@ -54,59 +55,59 @@ class DocumentController extends BaseController
$response = Response::make($document->getRaw(), 200);
$response->header('content-type', Document::$types[$document->type]['mime']);
}
return $response;
}
public function getPreview(DocumentRequest $request)
{
$document = $request->entity();
if(empty($document->preview)){
return Response::view('error', array('error'=>'Preview does not exist!'), 404);
}
$direct_url = $document->getDirectPreviewUrl();
if($direct_url){
return redirect($direct_url);
}
$previewType = pathinfo($document->preview, PATHINFO_EXTENSION);
$response = Response::make($document->getRawPreview(), 200);
$response->header('content-type', Document::$types[$previewType]['mime']);
return $response;
}
public function getVFSJS(DocumentRequest $request, $publicId, $name)
{
$document = $request->entity();
if(substr($name, -3)=='.js'){
$name = substr($name, 0, -3);
}
if(!$document->isPDFEmbeddable()){
return Response::view('error', array('error'=>'Image does not exist!'), 404);
}
$content = $document->preview?$document->getRawPreview():$document->getRaw();
$content = 'ninjaAddVFSDoc('.json_encode(intval($publicId).'/'.strval($name)).',"'.base64_encode($content).'")';
$response = Response::make($content, 200);
$response->header('content-type', 'text/javascript');
$response->header('cache-control', 'max-age=31536000');
return $response;
}
public function postUpload(CreateDocumentRequest $request)
{
if (!Utils::hasFeature(FEATURE_DOCUMENTS)) {
return;
}
$result = $this->documentRepo->upload(Input::all()['file'], $doc_array);
if(is_string($result)){
return Response::json([
'error' => $result,
@ -120,4 +121,11 @@ class DocumentController extends BaseController
], 200);
}
}
public function delete(UpdateDocumentRequest $request)
{
$request->entity()->delete();
return RESULT_SUCCESS;
}
}

View File

@ -0,0 +1,26 @@
<?php namespace App\Http\Requests;
class UpdateDocumentRequest extends DocumentRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return $this->user()->can('edit', $this->entity());
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
];
}
}

View File

@ -156,6 +156,7 @@ Route::group(['middleware' => 'auth:user'], function() {
Route::get('documents/js/{documents}/{filename}', 'DocumentController@getVFSJS');
Route::get('documents/preview/{documents}/{filename?}', 'DocumentController@getPreview');
Route::post('document', 'DocumentController@postUpload');
Route::delete('documents/{documents}', 'DocumentController@delete');
Route::get('quotes/create/{client_id?}', 'QuoteController@create');
Route::get('quotes/{invoices}/clone', 'InvoiceController@cloneInvoice');

View File

@ -243,6 +243,7 @@
},
acceptedFiles:{!! json_encode(implode(',',\App\Models\Document::$allowedMimes)) !!},
addRemoveLinks:true,
dictRemoveFileConfirmation:"{{trans('texts.are_you_sure')}}",
@foreach(['default_message', 'fallback_message', 'fallback_text', 'file_too_big', 'invalid_file_type', 'response_error', 'cancel_upload', 'cancel_upload_confirmation', 'remove_file'] as $key)
"dict{{strval($key)}}":"{{trans('texts.dropzone_'.Utils::toClassCase($key))}}",
@endforeach
@ -392,6 +393,13 @@
function handleDocumentRemoved(file){
model.removeDocument(file.public_id);
$.ajax({
url: '{{ '/documents/' }}' + file.public_id,
type: 'DELETE',
success: function(result) {
// Do something with the result
}
});
}
function handleDocumentUploaded(file, response){

View File

@ -1016,6 +1016,7 @@
},
acceptedFiles:{!! json_encode(implode(',',\App\Models\Document::$allowedMimes)) !!},
addRemoveLinks:true,
dictRemoveFileConfirmation:"{{trans('texts.are_you_sure')}}",
@foreach(['default_message', 'fallback_message', 'fallback_text', 'file_too_big', 'invalid_file_type', 'response_error', 'cancel_upload', 'cancel_upload_confirmation', 'remove_file'] as $key)
"dict{{Utils::toClassCase($key)}}":"{{trans('texts.dropzone_'.$key)}}",
@endforeach
@ -1459,6 +1460,13 @@
function handleDocumentRemoved(file){
model.invoice().removeDocument(file.public_id);
refreshPDF(true);
$.ajax({
url: '{{ '/documents/' }}' + file.public_id,
type: 'DELETE',
success: function(result) {
// Do something with the result
}
});
}
function handleDocumentUploaded(file, response){