-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
306 additions
and
19 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,67 @@ | ||
import {Errors} from "laravel-nova"; | ||
|
||
Nova.booting((Vue, router, store) => { | ||
Vue.component('index-nova-tab-translatable', require('./components/IndexField')) | ||
Vue.component('detail-nova-tab-translatable', require('./components/DetailField')) | ||
Vue.component('form-nova-tab-translatable', require('./components/FormField')) | ||
Vue.component('index-nova-tab-translatable', require('./components/IndexField')) | ||
Vue.component('detail-nova-tab-translatable', require('./components/DetailField')) | ||
Vue.component('form-nova-tab-translatable', require('./components/FormField')) | ||
|
||
const BaseFormFileField = Vue.options.components["form-file-field"]; | ||
const CustomFormFileField = BaseFormFileField.extend({ | ||
methods: { | ||
async removeFile() { | ||
this.uploadErrors = new Errors() | ||
|
||
const { | ||
resourceName, | ||
resourceId, | ||
relatedResourceName, | ||
relatedResourceId, | ||
viaRelationship, | ||
} = this | ||
const attribute = this.field.attribute | ||
|
||
|
||
|
||
const uri = | ||
this.viaRelationship && | ||
this.relatedResourceName && | ||
this.relatedResourceId | ||
? `/nova-api/kongulov/nova-tab-translatable/${resourceName}/${resourceId}/${relatedResourceName}/${relatedResourceId}/field/${attribute}?viaRelationship=${viaRelationship}` | ||
: `/nova-api/kongulov/nova-tab-translatable/${resourceName}/${resourceId}/field/${attribute}` | ||
|
||
try { | ||
await Nova.request().delete(uri) | ||
this.closeRemoveModal() | ||
this.deleted = true | ||
this.$emit('file-deleted') | ||
Nova.success(this.__('The file was deleted!')) | ||
} catch (error) { | ||
this.closeRemoveModal() | ||
|
||
if (error.response.status == 422) { | ||
this.uploadErrors = new Errors(error.response.data.errors) | ||
} | ||
} | ||
}, | ||
} | ||
}) | ||
Vue.component('form-file-field', CustomFormFileField) | ||
|
||
const BaseDetailFileField = Vue.options.components["detail-file-field"]; | ||
const CustomDetailFileField = BaseDetailFileField.extend({ | ||
methods: { | ||
download() { | ||
const { resourceName, resourceId } = this | ||
const attribute = this.field.attribute | ||
|
||
let link = document.createElement('a') | ||
link.href = `/nova-api/kongulov/nova-tab-translatable/${resourceName}/${resourceId}/download/${attribute}` | ||
link.download = 'download' | ||
document.body.appendChild(link) | ||
link.click() | ||
document.body.removeChild(link) | ||
}, | ||
} | ||
}) | ||
Vue.component('detail-file-field', CustomDetailFileField) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Route; | ||
|
||
// Fields... | ||
Route::get('/{resource}/{resourceId}/download/{field}', 'FieldDownloadController@show'); | ||
Route::delete('/{resource}/{resourceId}/field/{field}', 'FieldDestroyController@handle'); | ||
Route::delete('/{resource}/{resourceId}/{relatedResource}/{relatedResourceId}/field/{field}', 'PivotFieldDestroyController@handle'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Kongulov\NovaTabTranslatable\Http\Controllers; | ||
|
||
use Illuminate\Routing\Controller; | ||
use Laravel\Nova\Http\Requests\NovaRequest; | ||
|
||
class FieldDestroyController extends Controller | ||
{ | ||
/** | ||
* Delete the file at the given field. | ||
* | ||
* @param \Laravel\Nova\Http\Requests\NovaRequest $request | ||
* @return \Illuminate\Http\Response | ||
* @throws \Illuminate\Auth\Access\AuthorizationException | ||
*/ | ||
public function handle(NovaRequest $request): \Illuminate\Http\Response | ||
{ | ||
$resource = $request->findResourceOrFail(); | ||
|
||
$explode = explode('_', $request->field); | ||
$locale = last($explode); | ||
$fieldNameArray = array_slice($explode, 1, -1); | ||
$fieldName = implode('_', $fieldNameArray); | ||
|
||
if (!in_array($fieldName, $resource->translatable)) abort(404); | ||
|
||
$resource->authorizeToUpdate($request); | ||
$model = $resource->model(); | ||
|
||
$model->forgetTranslation($fieldName, $locale); | ||
$model->timestamps = false; | ||
$model->save(); | ||
|
||
return response(['success' => true]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace Kongulov\NovaTabTranslatable\Http\Controllers; | ||
|
||
use Illuminate\Http\Response; | ||
use Illuminate\Routing\Controller; | ||
use Illuminate\Support\Facades\Storage; | ||
use Kongulov\NovaTabTranslatable\NovaTabTranslatable; | ||
use Laravel\Nova\Http\Requests\NovaRequest; | ||
use Symfony\Component\HttpFoundation\BinaryFileResponse; | ||
|
||
class FieldDownloadController extends Controller | ||
{ | ||
/** | ||
* Download the given field's contents. | ||
* | ||
* @param \Laravel\Nova\Http\Requests\NovaRequest $request | ||
* @return BinaryFileResponse | ||
* @throws \Illuminate\Auth\Access\AuthorizationException | ||
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException | ||
*/ | ||
public function show(NovaRequest $request) | ||
{ | ||
$resource = $request->findResourceOrFail(); | ||
|
||
$explode = explode('_', $request->field); | ||
$locale = last($explode); | ||
$fieldNameArray = array_slice($explode, 1, -1); | ||
$fieldName = implode('_', $fieldNameArray); | ||
|
||
if (!in_array($fieldName, $resource->translatable)) abort(404); | ||
|
||
$resource->authorizeToView($request); | ||
$model = $resource->model(); | ||
$value = $model->getTranslation($fieldName, $locale); | ||
|
||
$tabs = $resource->updateFields($request)->whereInstanceOf(NovaTabTranslatable::class); | ||
|
||
$field = false; | ||
|
||
foreach ($tabs as $tab) { | ||
$field = collect($tab->data)->first(function($field) use ($request){ | ||
return isset($field->attribute) && | ||
$field->attribute == $request->field; | ||
}); | ||
} | ||
|
||
if (!$field) abort(404); | ||
|
||
$disk = $field->getStorageDisk(); | ||
|
||
if (!Storage::disk($disk)->exists($value)) abort(404); | ||
|
||
$path = Storage::disk($disk)->get($value); | ||
|
||
return response()->download($path); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Kongulov\NovaTabTranslatable\Http\Controllers; | ||
|
||
use Illuminate\Routing\Controller; | ||
use Laravel\Nova\DeleteField; | ||
use Laravel\Nova\Http\Requests\PivotFieldDestroyRequest; | ||
use Laravel\Nova\Nova; | ||
|
||
class PivotFieldDestroyController extends Controller | ||
{ | ||
/** | ||
* Delete the file at the given field. | ||
* | ||
* @param \Laravel\Nova\Http\Requests\PivotFieldDestroyRequest $request | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function handle(PivotFieldDestroyRequest $request) | ||
{ | ||
abort(404); | ||
|
||
$request->authorizeForAttachment(); | ||
|
||
DeleteField::forRequest( | ||
$request, $request->findFieldOrFail(), | ||
$pivot = $request->findPivotModel() | ||
)->save(); | ||
|
||
Nova::actionEvent()->forAttachedResourceUpdate( | ||
$request, $request->findModelOrFail(), $pivot | ||
)->save(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters