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'));
}
}
'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');
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;
*/
$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');
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);
|--------------------------------------------------------------------------
*/
- $model = $this->getModelInstance();
$this->crud->addColumn(['name' => $model->getPrimaryKey(), 'type' => 'number', 'label' => "#", 'searchLogic' => 'text']);
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();
}
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
+ }
}
});
}
- 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);
--- /dev/null
+@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
+