From: Vincent Vanwaelscappel Date: Tue, 22 Oct 2019 17:54:02 +0000 (+0200) Subject: wip #3158 @1.5 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=5bf46bd9fcab4b74493dc030541d2c864c2f4b1e;p=cubist_cms-back.git wip #3158 @1.5 --- diff --git a/src/app/Console/Commands/LocaleCopy.php b/src/app/Console/Commands/LocaleCopy.php index 0f503d4..caf23a6 100644 --- a/src/app/Console/Commands/LocaleCopy.php +++ b/src/app/Console/Commands/LocaleCopy.php @@ -38,6 +38,6 @@ class LocaleCopy extends CubistCommand return; } /** @var CubistMagicTranslatableModel $model */ - $model->copyTranslations($this->argument('from'), $this->argument('to'), $this->option('overwrite')); + $model->copyAllTranslations($this->argument('from'), $this->argument('to'), $this->option('overwrite')); } } diff --git a/src/app/CubistCrudRouter.php b/src/app/CubistCrudRouter.php index a587a07..e2ca24a 100644 --- a/src/app/CubistCrudRouter.php +++ b/src/app/CubistCrudRouter.php @@ -23,6 +23,11 @@ class CubistCrudRouter extends CrudRouter 'uses' => $this->controller . '@bulkOffline', ]); + Route::post($this->name . '/bulk-translations-replace', [ + 'as' => 'crud.' . $this->name . '.bulkTranslationsReplace', + 'uses' => $this->controller . '@bulkTranslationsReplace', + ]); + Route::match(['post'], $this->name . '/{id}/media', $this->controller . 'CrudController@uploadMedia'); Route::match(['delete'], $this->name . '/{id}/media/{mediaId}', $this->controller . 'CrudController@deleteMedia'); Route::match(['post'], $this->name . '/{id}/media/reorder', $this->controller . 'CrudController@reorderMedia'); diff --git a/src/app/Magic/Controllers/CubistMagicController.php b/src/app/Magic/Controllers/CubistMagicController.php index bf47273..c2ad3f5 100644 --- a/src/app/Magic/Controllers/CubistMagicController.php +++ b/src/app/Magic/Controllers/CubistMagicController.php @@ -3,17 +3,20 @@ namespace Cubist\Backpack\app\Magic\Controllers; +use Composer\Console\Application; use Cubist\Backpack\app\Http\Controllers\CubistCrudController; use Backpack\CRUD\CrudTrait; use Cubist\Backpack\app\Magic\Fields\Field; use Cubist\Backpack\app\Magic\Menu\Menu; use Cubist\Backpack\app\Magic\Models\CubistMagicAbstractModel; +use Cubist\Backpack\app\Magic\Models\CubistMagicTranslatableModel; use Cubist\Backpack\app\Magic\Requests\CubistMagicRequest; use Cubist\Backpack\app\Magic\Requests\CubistMagicStoreRequest; use Cubist\Backpack\app\Magic\Requests\CubistMagicUpdateRequest; use Cubist\Backpack\CubistBackpackServiceProvider; use Gaspertrix\Backpack\DropzoneField\Traits\HandleAjaxMedia; use Illuminate\Support\Arr; +use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Redirect; @@ -80,6 +83,9 @@ class CubistMagicController extends CubistCrudController */ $this->crud->setModel($this->_modelNamespace); $this->_postSetModel(); + + $model = $this->getModelInstance(); + if ($this->_bulk) { $this->crud->enableBulkActions(); $this->crud->addButton('bottom', 'bulk_publish', 'view', CubistBackpackServiceProvider::NAMESPACE . '::buttons.bulk_publish', 'begining'); @@ -87,9 +93,14 @@ class CubistMagicController extends CubistCrudController if ($this->_clonable) { $this->crud->addButton('bottom', 'bulk_clone', 'view', 'crud::buttons.bulk_clone', 'beginning'); } - $this->crud->addBulkDeleteButton(); + if ($model->translationEnabled()) { + $this->crud->addButton('bottom', 'bulk_translate', 'view', CubistBackpackServiceProvider::NAMESPACE . '::buttons.bulk_translate', 'begining'); + } + $this->crud->addBulkDeleteButton(); } + + $this->crud->setRoute(config('backpack.base.route_prefix') . '/' . $this->_routeURL); $this->crud->setEntityNameStrings($this->_singular, $this->_plural); @@ -100,7 +111,6 @@ class CubistMagicController extends CubistCrudController |-------------------------------------------------------------------------- */ - $model = $this->getModelInstance(); $this->crud->addColumn(['name' => $model->getPrimaryKey(), 'type' => 'number', 'label' => "#", 'searchLogic' => 'text']); @@ -279,6 +289,23 @@ class CubistMagicController extends CubistCrudController return $this->_bulkChangeOnlineStatus(0); } + public function bulkTranslationsReplace() + { + $this->crud->hasAccessOrFail('update'); + + $locale = $this->request->input('locale'); + $entries = $this->request->input('entries'); + foreach ($entries as $key => $id) { + if ($entry = $this->crud->model->find($id)) { + /** @var $entry CubistMagicTranslatableModel */ + $entry->copyTranslations($locale, App::getLocale(), true); + } + } + $this->_forgetCache(); + + return $entries; + } + public function saveReorder() { $this->_forgetCache(); diff --git a/src/app/Magic/Models/CMSPage.php b/src/app/Magic/Models/CMSPage.php index 1a816c8..b437c0a 100644 --- a/src/app/Magic/Models/CMSPage.php +++ b/src/app/Magic/Models/CMSPage.php @@ -243,4 +243,12 @@ class CMSPage extends CubistMagicNestedModel } return $class; } + + public function copyTranslations($from, $to, $overwrite = false) + { + if ($this instanceof CMSPage) { + $this->useTemplate(); + } + parent::copyTranslations($from, $to, $overwrite); // TODO: Change the autogenerated stub + } } diff --git a/src/app/Magic/Models/CubistMagicTranslatableModel.php b/src/app/Magic/Models/CubistMagicTranslatableModel.php index 0c7ea1c..74a2c7e 100644 --- a/src/app/Magic/Models/CubistMagicTranslatableModel.php +++ b/src/app/Magic/Models/CubistMagicTranslatableModel.php @@ -76,38 +76,36 @@ class CubistMagicTranslatableModel extends CubistMagicAbstractModel }); } - public function copyTranslations($from, $to, $overwrite = false) + public function copyAllTranslations($from, $to, $overwrite = false) { $i = 0; foreach (static::all() as $item) { //echo 'instance ' . $item->id . ' ' . implode(', ', $item->translatable) . "\n"; - if ($item instanceof CMSPage) { - $item->useTemplate(); - } - - /** @var $item self */ - - foreach ($item->translatable as $translatable) { - if (!$item->hasTranslation($translatable, $from)) { - //echo 'no translation for ' . $translatable . "\n"; - continue; - } - - $fake = in_array($translatable, $item->fakeColumns); - $hasTranslation = $item->hasTranslation($translatable, $to); - $destTranslation = $item->getTranslation($translatable, $from); - if (!$overwrite && $hasTranslation && $fake) { - $destTranslation = array_merge($destTranslation, $item->getTranslation($translatable, $to)); - } - $item->setTranslation($translatable, $to, $destTranslation); + $item->copyTranslations($from, $to, $overwrite); - } - $item->save(); $i++; } echo 'copy translation of ' . $i . ' instances of ' . get_class($this) . ' from ' . $from . ' to ' . $to . "\n"; } + public function copyTranslations($from, $to, $overwrite = false) + { + foreach ($this->translatable as $translatable) { + if (!$this->hasTranslation($translatable, $from)) { + continue; + } + $fake = in_array($translatable, $this->fakeColumns); + $hasTranslation = $this->hasTranslation($translatable, $to); + $destTranslation = $this->getTranslation($translatable, $from); + if (!$overwrite && $hasTranslation && $fake) { + $destTranslation = array_merge($destTranslation, $this->getTranslation($translatable, $to)); + } + $this->setTranslation($translatable, $to, $destTranslation); + + } + $this->save(); + } + public function update(array $attributes = [], array $options = []) { return $this->updateTranslations($this->_prepareData($attributes), $options); diff --git a/src/resources/views/buttons/bulk_translate.blade.php b/src/resources/views/buttons/bulk_translate.blade.php new file mode 100644 index 0000000..520a28f --- /dev/null +++ b/src/resources/views/buttons/bulk_translate.blade.php @@ -0,0 +1,81 @@ +@if ($crud->hasAccess('update') && $crud->bulk_actions) + +
+ {{ __('Traductions') }} + + +
+@endif + + +@push('after_scripts') + +@endpush +