/**
  * @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];
 }
Esempio n. 2
0
<?php

use Pagekit\Widget\Model\Widget;
use Pagekit\Widget\PositionHelper;
use Pagekit\Widget\PositionManager;
use Pagekit\Widget\WidgetManager;
return ['name' => 'system/widget', 'main' => function ($app) {
    $app['widget'] = function ($app) {
        return new WidgetManager($app);
    };
    $app['position'] = function ($app) {
        $positions = new PositionManager($app->config($app['theme']->name));
        foreach ($app['theme']->get('positions', []) as $name => $label) {
            $positions->register($name, $label);
        }
        return $positions;
    };
    $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) {