Example #1
0
 /**
  * @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())]]];
 }
 /**
  * @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];
 }