LaravelRUS / SleepingOwlAdmin

🦉 Administrative interface builder for Laravel (Laravel admin)
http://sleepingowladmin.ru/
MIT License
805 stars 217 forks source link

Questions about SleepingOwlAdmin #41

Closed avifatal closed 8 years ago

avifatal commented 8 years ago

Hi, I want to try and use this product, I have some questions if I may.

  1. Can I write my own functionality (interceptions) in Before/After crud operations? for example, I want to send an email when a row was been deleted.
  2. Can I intercept and write client side functionality?
  3. Can I add custom operation in the grid near "update" and "delete" icons? such as disable and run my own functionality?
  4. Can custom filters be saved for later use?

Thanks!

butschster commented 8 years ago

Hi,

You can use custom controller for 1 and 3 questions

Example

AdminSection::registerModel(Contact3::class, function (ModelConfiguration $model) {

     $model->setControllerClass(\App\Http\Controller\MyCustomContact3Controller::class);

     $model->onDisplay(function () { ... });

});

And then you will create controller

class MyCustomContact3Controller extends \SleepingOwl\Admin\Http\Controllers\AdminController 
{
    public function deleteDestroy(ModelConfiguration $model, $id)
    {

       $item = $model->getRepository()->find($id);

        if (is_null($item) || ! $model->isDeletable($item)) {
            abort(404);
        }

        $model->fireDelete($id);
        $model->getRepository()->delete($id);

        ...

       // Your code

        ...

        return redirect()->back()->with('success_message', $model->getMessageOnDelete());
    }
}

Could you explain more about 2 and 4 questions

avifatal commented 8 years ago

Hi, thanks for your time and for your quick answer!

  1. Lets say that I want on change to in a field to throw an alert, I know I can do it via native JS, the question is, is there any formal SDK for that? thanks
  2. About the filters, I need to learn it first. forget it
  3. your example I assume intercept delete operation. How can create custom operation from client to server, for example I want in customer grid to have a button near the delete and update that will call to the server and execute custom function, for example: CloseAllTasks() or whatever I want to do in the server.
  4. Can you release real life example as opensource app developed with SleepingOwlAdmin? The demo app is good, but I assume it is not covering all.
  5. Is there any API for calling in the client for grid / forms manually? for example, when I edit customer, I want to show in the same screen all of its related calls (some related mode/entity).

Thanks again. Avi

butschster commented 8 years ago
  1. A simpler way is use model events
class News extends Model {

     protected static function boot()
     {
        parent::boot();

        static::deleting(function (News  $news) {
              Mail::send(....);
        });
     }
}

But it is possitble to add events (callbacks) to controller CRUD methods.

Like this

$model->beforeCreate(...);
$model->afterCreate(...);

$model->beforeUpdate(...);
$model->afterUpdate(...);

$model->beforeSave(...);
$model->afterSave(...);

$model->beforeDelete(...);
$model->afterDelet(...);

3.

your example I assume intercept delete operation. How can create custom operation from client to server, for example I want in customer grid to have a button near the delete and update that will call to the server and execute custom function, for example: CloseAllTasks() or whatever I want to do in the server.

Display

// Display
$model->onDisplay(function () {
    $display = AdminDisplay::table()
        ->setActions([
            AdminColumn::action('CloseAllTasks', 'Export')
                ->setIcon('fa fa-share')
                ->setAction(route('admin.news.export'))
                ->usePost()
        ])
        ->setColumns([
            AdminColumn::checkbox(), // Required for actions working
            ...
            AdminColumn::link('title')->setLabel('Title'),
            ...
        ]);

    return $display;
});

Route

Route::post('/news/export', ['as' => 'admin.news.export', function () {
    return new \Illuminate\Http\JsonResponse([
        'title' => 'Congratulation! You exported news.',
        'news' => App\Model\News5::whereIn('id', Request::get('id', []))->get()
    ]);
}]);

https://github.com/LaravelRUS/SleepingOwlAdmin/releases/tag/4.17.88-beta

4 Yes. We plane to create a few demo apps with different functionality. 5 API is planned for the near future.

avifatal commented 8 years ago

Thank you! all is good, just about question 5. Embedding now grid in a form is impossible?

butschster commented 8 years ago

Hi.

Now you can register events for section:

Example

// Registering event from section class
AdminSection::registerModel(News::class, function (ModelConfiguration $model) {
    ...
    $model->updating(function(ModelConfiguration $model, Model $item) {
        Mail::send(...);
    });
    ...
});

// Registering from ServisProvider or bootstrap.php
app('events')->listen("sleeping_owl.section.creating: App\Model\News", function() {
      // do something
});

// or

Event::listen("sleeping_owl.section.creating: App\Model\News", function() {
      // do something
});