diff --git a/app/Http/Controllers/DocumentController.php b/app/Http/Controllers/DocumentController.php index d597ba0044..e4ea605d57 100644 --- a/app/Http/Controllers/DocumentController.php +++ b/app/Http/Controllers/DocumentController.php @@ -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; + } } diff --git a/app/Http/Requests/UpdateDocumentRequest.php b/app/Http/Requests/UpdateDocumentRequest.php new file mode 100644 index 0000000000..3a950c9f34 --- /dev/null +++ b/app/Http/Requests/UpdateDocumentRequest.php @@ -0,0 +1,26 @@ +user()->can('edit', $this->entity()); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array + */ + public function rules() + { + return [ + + ]; + } +} diff --git a/app/Http/routes.php b/app/Http/routes.php index 84ec2552bb..446179ae23 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -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'); diff --git a/resources/views/expenses/edit.blade.php b/resources/views/expenses/edit.blade.php index aadfacc526..d8c5c62d36 100644 --- a/resources/views/expenses/edit.blade.php +++ b/resources/views/expenses/edit.blade.php @@ -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){ diff --git a/resources/views/invoices/edit.blade.php b/resources/views/invoices/edit.blade.php index d17a3ba25c..4f6940645a 100644 --- a/resources/views/invoices/edit.blade.php +++ b/resources/views/invoices/edit.blade.php @@ -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){