]> _ Git - cubist_cms-back.git/commitdiff
wip #3157 @3
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 21 Oct 2019 16:13:36 +0000 (18:13 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 21 Oct 2019 16:13:36 +0000 (18:13 +0200)
src/app/Console/Commands/GenerateCommand.php
src/app/CubistCrudPanel.php
src/app/CubistCrudRouter.php [new file with mode: 0644]
src/app/Magic/Controllers/CubistMagicController.php
src/app/Magic/Models/CubistMagicAbstractModel.php
src/resources/cubistmagic/routes.stub
src/resources/views/buttons/bulk_offline.blade.php [new file with mode: 0644]
src/resources/views/buttons/bulk_publish.blade.php [new file with mode: 0644]

index 72eef3a1a6090f55cc69fdc0a31d998b60a12fb6..f6f4f8516347535bedabea746f4301b73a7fc737 100644 (file)
@@ -61,13 +61,8 @@ class GenerateCommand extends CubistCommand
         $route = $model->getOption('route', $model->getOption('name'));
         $controller = $model->getControllerClass();
 
-        $this->_routes[] = 'CRUD::resource(\'' . $route . '\', \'' . $controller . '\')->with(function () {
-        Route::match([\'post\'], \'' . $route . '/{id}/media\', \'' . $controller . '@uploadMedia\');
-        Route::match([\'delete\'], \'' . $route . '/{id}/media/{mediaId}\', \'' . $controller . '@deleteMedia\');
-        Route::match([\'post\'], \'' . $route . '/{id}/media/reorder\', \'' . $controller . '@reorderMedia\');
-    });';
+        $this->_routes[] = 'new \Cubist\Backpack\CubistCrudRouter(\'' . $route . '\', \'' . $controller . '\', []);';
     }
-
 }
 
 
index f31cdc4801379c29b169d4a9fedc4d142f008c9d..746ce59b6c33d3eab251f1853d86516d821538cc 100644 (file)
@@ -3,14 +3,14 @@
 namespace Cubist\Backpack;
 
 use Backpack\CRUD\CrudPanel;
-use Cubist\Backpack\app\Magic\Models\CubistMagicModel;
+use Cubist\Backpack\app\Magic\Models\CubistMagicAbstractModel;
 
 /**
- * @property CubistMagicModel $model
+ * @property CubistMagicAbstractModel $model
  */
 
 /**
- * @property CubistMagicModel $entry
+ * @property CubistMagicAbstractModel $entry
  */
 class CubistCrudPanel extends CrudPanel
 {
diff --git a/src/app/CubistCrudRouter.php b/src/app/CubistCrudRouter.php
new file mode 100644 (file)
index 0000000..a587a07
--- /dev/null
@@ -0,0 +1,30 @@
+<?php
+
+
+namespace Cubist\Backpack;
+
+use Backpack\CRUD\CrudRouter;
+
+use Illuminate\Support\Facades\Route;
+
+class CubistCrudRouter extends CrudRouter
+{
+    public function __construct($name, $controller, $options)
+    {
+        parent::__construct($name, $controller, $options);
+
+        Route::post($this->name . '/bulk-publish', [
+            'as' => 'crud.' . $this->name . '.bulkPublish',
+            'uses' => $this->controller . '@bulkPublish',
+        ]);
+
+        Route::post($this->name . '/bulk-offline', [
+            'as' => 'crud.' . $this->name . '.bulkOffline',
+            'uses' => $this->controller . '@bulkOffline',
+        ]);
+
+        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 110a75ebc14eaf133679fb2de6a89e52472580b9..e880e1e015c5eed40319c6b4c69ac8a6f9879314 100644 (file)
@@ -11,6 +11,7 @@ use Cubist\Backpack\app\Magic\Models\CubistMagicAbstractModel;
 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\Cache;
@@ -81,10 +82,13 @@ class CubistMagicController extends CubistCrudController
         $this->_postSetModel();
         if ($this->_bulk) {
             $this->crud->enableBulkActions();
+            $this->crud->addButton('bottom', 'bulk_publish', 'view', CubistBackpackServiceProvider::NAMESPACE . '::buttons.bulk_publish', 'begining');
+            $this->crud->addButton('bottom', 'bulk_offline', 'view', CubistBackpackServiceProvider::NAMESPACE . '::buttons.bulk_offline', 'begining');
             if ($this->_clonable) {
                 $this->crud->addButton('bottom', 'bulk_clone', 'view', 'crud::buttons.bulk_clone', 'beginning');
             }
             $this->crud->addBulkDeleteButton();
+
         }
         $this->crud->setRoute(config('backpack.base.route_prefix') . '/' . $this->_routeURL);
         $this->crud->setEntityNameStrings($this->_singular, $this->_plural);
@@ -248,6 +252,31 @@ class CubistMagicController extends CubistCrudController
         return parent::create();
     }
 
+    protected function _bulkChangeOnlineStatus($status)
+    {
+        $this->crud->hasAccessOrFail('update');
+
+        $entries = $this->request->input('entries');
+        foreach ($entries as $key => $id) {
+            if ($entry = $this->crud->model->find($id)) {
+                /** @var $entry CubistMagicAbstractModel */
+                $entry->setAttribute('status', $status);
+            }
+        }
+
+        return $entries;
+    }
+
+    public function bulkPublish()
+    {
+        return $this->_bulkChangeOnlineStatus(true);
+    }
+
+    public function bulkOffline()
+    {
+        return $this->_bulkChangeOnlineStatus(false);
+    }
+
     public function saveReorder()
     {
         $this->_forgetCache();
index 74f4ed7ddd48ae37a2a9fbf06fd85ecc8a61bf42..d59e88ae658c20ae6d7d820841b877cb364abb17 100644 (file)
@@ -171,6 +171,7 @@ class CubistMagicAbstractModel extends Model implements HasMedia
                 return Str::slug(implode('-', $slug));
             }
         }
+        return Str::slug($this->getAttribute('id'));
     }
 
     public function getOption($key, $default = null)
index f9bac56e31d4898ee39652daf7a0ed45d859cdbb..b6008f635b9c56d57f2c882c98e34c3e751303c3 100644 (file)
@@ -5,4 +5,4 @@ Route::group([
     'namespace'  => 'App\Http\Controllers\Admin',
 ], function () { // custom admin routes
     _CUSTOM_
-}); // this should be the absolute last line of this file
+});
diff --git a/src/resources/views/buttons/bulk_offline.blade.php b/src/resources/views/buttons/bulk_offline.blade.php
new file mode 100644 (file)
index 0000000..5393c41
--- /dev/null
@@ -0,0 +1,58 @@
+@if ($crud->hasAccess('update') && $crud->bulk_actions)
+       <a href="javascript:void(0)" onclick="bulkOfflineEntries(this)" class="btn btn-sm btn-default bulk-button"><i class="fa fa-times-circle"></i> Put offline</a>
+@endif
+
+@push('after_scripts')
+<script>
+       if (typeof bulkOfflineEntries != 'function') {
+         function bulkOfflineEntries(button) {
+
+             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 put offline these :number entries?";
+             message = message.replace(":number", crud.checkedItems.length);
+
+             // show confirm message
+             if (confirm(message) == true) {
+                       var ajax_calls = [];
+                       var clone_route = "{{ url($crud->route) }}/bulk-offline";
+
+                               // submit an AJAX delete call
+                               $.ajax({
+                                       url: clone_route,
+                                       type: 'POST',
+                                       data: { entries: crud.checkedItems },
+                                       success: function(result) {
+                                         // Show an alert with the result
+                                         new PNotify({
+                                             title: "Entries offline",
+                                             text: crud.checkedItems.length+" entries have been put offline.",
+                                             type: "success"
+                                         });
+
+                                         crud.checkedItems = [];
+                                         crud.table.ajax.reload();
+                                       },
+                                       error: function(result) {
+                                         // Show an alert with the result
+                                         new PNotify({
+                                             title: "Publishing failed",
+                                             text: "One or more entries could not be put offline. Please try again.",
+                                             type: "warning"
+                                         });
+                                       }
+                               });
+             }
+      }
+       }
+</script>
+@endpush
diff --git a/src/resources/views/buttons/bulk_publish.blade.php b/src/resources/views/buttons/bulk_publish.blade.php
new file mode 100644 (file)
index 0000000..911d8ea
--- /dev/null
@@ -0,0 +1,58 @@
+@if ($crud->hasAccess('update') && $crud->bulk_actions)
+       <a href="javascript:void(0)" onclick="bulkPublishEntries(this)" class="btn btn-sm btn-default bulk-button"><i class="fa fa-check-circle"></i> Publish</a>
+@endif
+
+@push('after_scripts')
+<script>
+       if (typeof bulkPublishEntries != 'function') {
+         function bulkPublishEntries(button) {
+
+             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 publish these :number entries?";
+             message = message.replace(":number", crud.checkedItems.length);
+
+             // show confirm message
+             if (confirm(message) == true) {
+                       var ajax_calls = [];
+                       var publish_route = "{{ url($crud->route) }}/bulk-publish";
+
+                               // submit an AJAX delete call
+                               $.ajax({
+                                       url: publish_route,
+                                       type: 'POST',
+                                       data: { entries: crud.checkedItems },
+                                       success: function(result) {
+                                         // Show an alert with the result
+                                         new PNotify({
+                                             title: "Entries published",
+                                             text: crud.checkedItems.length+" entries have been published.",
+                                             type: "success"
+                                         });
+
+                                         crud.checkedItems = [];
+                                         crud.table.ajax.reload();
+                                       },
+                                       error: function(result) {
+                                         // Show an alert with the result
+                                         new PNotify({
+                                             title: "Publishing failed",
+                                             text: "One or more entries could not be published. Please try again.",
+                                             type: "warning"
+                                         });
+                                       }
+                               });
+             }
+      }
+       }
+</script>
+@endpush