]> _ Git - cubist_cms-back.git/commitdiff
wip #3158 @1.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 22 Oct 2019 17:54:02 +0000 (19:54 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 22 Oct 2019 17:54:02 +0000 (19:54 +0200)
src/app/Console/Commands/LocaleCopy.php
src/app/CubistCrudRouter.php
src/app/Magic/Controllers/CubistMagicController.php
src/app/Magic/Models/CMSPage.php
src/app/Magic/Models/CubistMagicTranslatableModel.php
src/resources/views/buttons/bulk_translate.blade.php [new file with mode: 0644]

index 0f503d41eea9d3a8de0253239c92a6808f2bec14..caf23a68a78be8ea990547aa8cdf68a4f347d67f 100644 (file)
@@ -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'));
     }
 }
index a587a07bbc1690c4258f97665f4936af844ab9b2..e2ca24a73a5b95b82229973784151f163d4815ca 100644 (file)
@@ -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');
index bf47273658ca5c73f8ddd9f3f4253a4910bfc43c..c2ad3f57edb4c017e69fa318644c5d6a9567463c 100644 (file)
@@ -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();
index 1a816c84b0cc41cdbf205baa6c2684fbe035c4bd..b437c0aa013adc7228a4326c0dff4574fff9e2f0 100644 (file)
@@ -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
+    }
 }
index 0c7ea1c747074d5062ae753e31ab23628ca24285..74a2c7ed501e163d8be792e9c890450c05e45292 100644 (file)
@@ -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 (file)
index 0000000..520a28f
--- /dev/null
@@ -0,0 +1,81 @@
+@if ($crud->hasAccess('update') && $crud->bulk_actions)
+    <!-- Edit button group -->
+    <div class="btn-group">
+        <a href="javascript:void(0)" class="btn btn-sm btn-default bulk-button"><i
+                class="fa fa-globe"></i> {{ __('Traductions') }}</a>
+        <button type="button" class="btn btn-sm btn-default bulk-button dropdown-toggle" data-toggle="dropdown"
+                aria-haspopup="true"
+                aria-expanded="false">
+            <span class="caret"></span>
+            <span class="sr-only">Toggle Dropdown</span>
+        </button>
+        <ul class="dropdown-menu dropdown-menu-right">
+            <li class="dropdown-header">{{ __('Remplacer les contenus ":locale" par', ['locale'=> $crud->model->getAvailableLocales()[App::getLocale()]]) }}
+                :
+            </li>
+            @foreach ($crud->model->getAvailableLocales() as $key => $locale)
+                @if(App::isLocale($key))
+                    @continue
+                @endif
+                <li><a href="javascript:void(0)" onclick="bulkReplaceTranslations(this,'{{ $key }}')">{{ $locale }}</a>
+                </li>
+            @endforeach
+        </ul>
+    </div>
+@endif
+
+
+@push('after_scripts')
+    <script>
+        if (typeof bulkReplaceTranslations != 'function') {
+            function bulkReplaceTranslations(button, locale) {
+
+                if (typeof crud.checkedItems === 'undefined' || crud.checkedItems.length == 0) {
+                    new PNotify({
+                        title: "{{ trans('backpack::crud.bulk_no_entries_selected_title') }}",
+                        text: "{{ trans('backpack::crud.bulk_no_entries_selected_message') }}",
+                        type: "warning"
+                    });
+
+                    return;
+                }
+
+                var message = "Are you sure you want to replace the translations of these :number entries?";
+                message = message.replace(":number", crud.checkedItems.length);
+
+                // show confirm message
+                if (confirm(message) == true) {
+                    var ajax_calls = [];
+                    var route = "{{ url($crud->route) }}/bulk-translations-replace";
+
+                    // submit an AJAX delete call
+                    $.ajax({
+                        url: route,
+                        type: 'POST',
+                        data: {entries: crud.checkedItems, locale: locale},
+                        success: function (result) {
+                            // Show an alert with the result
+                            new PNotify({
+                                title: "Entries updated",
+                                text: crud.checkedItems.length + " entries have been updated.",
+                                type: "success"
+                            });
+
+                            crud.checkedItems = [];
+                            crud.table.ajax.reload();
+                        },
+                        error: function (result) {
+                            // Show an alert with the result
+                            new PNotify({
+                                title: "Update failed",
+                                text: "One or more entries could not be updated. Please try again.",
+                                type: "warning"
+                            });
+                        }
+                    });
+                }
+            }
+        }
+    </script>
+@endpush
+