/** * @Route("/{id}", methods="DELETE", requirements={"id"="\d+"}) * @Request({"id": "int"}, csrf=true) */ public function deleteAction($id) { if (!($widget = Widget::find($id))) { App::abort(404, 'Widget not found.'); } $widget->delete(); return ['message' => 'success']; }
/** * @Request({"id": "int", "type": "string"}) */ public function editAction($id = 0, $type = null) { if (!$id) { $widget = Widget::create(['type' => $type]); } else { if (!($widget = Widget::find($id))) { App::abort(404, 'Widget not found.'); } } return ['$view' => ['title' => __('Widgets'), 'name' => 'system/widget/edit.php'], '$data' => ['widget' => $widget, 'config' => ['menus' => App::menu(), 'nodes' => array_values(Node::query()->get()), 'roles' => array_values(Role::findAll()), 'types' => array_values(App::widget()->all()), 'positions' => array_values(App::position()->all())]]]; }
/** * @Route(methods="POST") * @Request({"ids": "int[]"}, csrf=true) */ public function copyAction($ids = []) { foreach ($ids as $id) { if ($widget = Widget::find((int) $id)) { $copy = clone $widget; $copy->id = null; $copy->status = 0; $copy->title = $widget->title . ' - ' . __('Copy'); $copy->save(); } } return ['message' => 'success', 'positions' => array_values(App::position()->all())]; }
/** * @param string|null $position * @return Widget[] */ protected function getWidgets($position) { static $widgets, $positions = []; if (null === $widgets) { $widgets = Widget::where(['status' => 1])->get(); } if (!($pos = $this->positions->get($position))) { return []; } if (!isset($positions[$position])) { $positions[$position] = []; foreach ($pos['assigned'] as $id) { if (!isset($widgets[$id]) or !($widget = $widgets[$id]) or !$widget->hasAccess(App::user()) or $nodes = $widget->nodes and !in_array(App::node()->id, $nodes) or !($type = App::widget($widget->type))) { continue; } $result = $type->render($widget); $widget->set('result', $result); $positions[$position][] = $widget; } } return $positions[$position]; }
/** * @param string $id * @param array $data * @return Widget */ protected function create($id, $data) { $widget = new Widget(); $widget->setId($id); $widget->setType($data['type']); $widget->setSettings($data); return $widget; }
}; $app['module']->addLoader(function ($module) use($app) { if (isset($module['widgets'])) { $app['widget']->register($module['widgets'], $module['path']); } return $module; }); $app->extend('view', function ($view) use($app) { return $view->addHelper(new PositionHelper($app['position'])); }); }, 'autoload' => ['Pagekit\\Widget\\' => 'src'], 'routes' => ['/site/widget' => ['name' => '@site/widget', 'controller' => 'Pagekit\\Widget\\Controller\\WidgetController'], '/api/site/widget' => ['name' => '@site/api/widget', 'controller' => 'Pagekit\\Widget\\Controller\\WidgetApiController']], 'resources' => ['system/widget:' => '', 'views:system/widget' => 'views'], 'permissions' => ['system: manage widgets' => ['title' => 'Manage widgets']], 'menu' => ['site: widgets' => ['label' => 'Widgets', 'parent' => 'site', 'url' => '@site/widget', 'access' => 'system: manage widgets', 'active' => '@site/widget(/edit)?', 'priority' => 20]], 'config' => ['widget' => ['positions' => [], 'config' => [], 'defaults' => []]], 'events' => ['boot' => function ($event, $app) { Widget::defineProperty('position', function () use($app) { return $app['position']->find($this->id); }, true); Widget::defineProperty('theme', function () use($app) { $config = $app['theme']->config('_widgets.' . $this->id, []); $default = $app['theme']->get('widget', []); return array_replace_recursive($default, $config); }, true); }, 'view.scripts' => function ($event, $scripts) { $scripts->register('widgets', 'system/widget:app/bundle/widgets.js', 'vue'); }, 'model.widget.init' => function ($event, $widget) use($app) { if ($type = $app->widget($widget->type)) { $widget->data = array_replace_recursive($type->get('defaults', []), $widget->data ?: []); } }, 'model.widget.saved' => function ($event, $widget) use($app) { $app['position']->assign($widget->position, $widget->id); $app->config($app['theme']->name)->set('_widgets.' . $widget->id, $widget->theme); }, 'model.role.deleted' => function ($event, $role) { Widget::removeRole($role); }]];
/** * @Route(methods="POST") * @Request({"ids": "int[]"}, csrf=true) */ public function copyAction($ids = []) { foreach ($ids as $id) { if ($widget = Widget::find((int) $id)) { $copy = clone $widget; $copy->id = null; $copy->status = 0; $copy->title = $widget->title . ' - ' . __('Copy'); $copy->save(); } } return ['message' => 'success']; }