inetis.listswitch
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.6 KiB

3 years ago
  1. <?php namespace Inetis\ListSwitch;
  2. use Backend\Classes\Controller;
  3. use Event;
  4. use Flash;
  5. use System\Classes\PluginBase;
  6. /**
  7. * listSwitch Plugin Information File
  8. */
  9. class Plugin extends PluginBase
  10. {
  11. /**
  12. * Returns information about this plugin.
  13. *
  14. * @return array
  15. */
  16. public function pluginDetails()
  17. {
  18. return [
  19. 'name' => 'inetis.listswitch::lang.inetis.plugin.name',
  20. 'description' => 'inetis.listswitch::lang.inetis.plugin.description',
  21. 'author' => 'inetis',
  22. 'icon' => 'icon-toggle-on',
  23. 'homepage' => 'https://github.com/inetis-ch/oc-ListSwitch',
  24. ];
  25. }
  26. /**
  27. * Register custom list type
  28. *
  29. * @return array
  30. */
  31. public function registerListColumnTypes()
  32. {
  33. return [
  34. 'inetis-list-switch' => [ListSwitchField::class, 'render'],
  35. ];
  36. }
  37. /**
  38. * Boot method, called right before the request route.
  39. *
  40. * @return array
  41. */
  42. public function boot()
  43. {
  44. Event::listen('backend.list.extendColumns', function ($widget) {
  45. /** @var \Backend\Widgets\Lists $widget */
  46. foreach ($widget->config->columns as $name => $config) {
  47. if (empty($config['type']) || $config['type'] !== 'inetis-list-switch') {
  48. continue;
  49. }
  50. // Store field config here, before that unofficial fields was removed
  51. ListSwitchField::storeFieldConfig($name, $config);
  52. $widget->addColumns([
  53. $name => array_merge($config, [
  54. 'clickable' => false,
  55. ]),
  56. ]);
  57. }
  58. });
  59. /**
  60. * Switch a boolean value of a model field
  61. * @return void
  62. */
  63. Controller::extend(function ($controller) {
  64. /** @var Controller $controller */
  65. $controller->addDynamicMethod('index_onSwitchInetisListField', function () use ($controller) {
  66. $field = post('field');
  67. $id = post('id');
  68. $modelClass = post('model');
  69. if (empty($field) || empty($id) || empty($modelClass)) {
  70. Flash::error('Following parameters are required : id, field, model');
  71. return;
  72. }
  73. $model = new $modelClass;
  74. $item = $model::find($id);
  75. $item->{$field} = !$item->{$field};
  76. $item->save();
  77. return $controller->listRefresh($controller->primaryDefinition);
  78. });
  79. });
  80. }
  81. }