Skip to content

Commit

Permalink
Добавлен альтернытивный вариант настройки виджетов на страницах
Browse files Browse the repository at this point in the history
  • Loading branch information
butschster committed Jun 9, 2016
1 parent 593e49b commit cbe493f
Show file tree
Hide file tree
Showing 9 changed files with 362 additions and 243 deletions.
60 changes: 60 additions & 0 deletions src/Contracts/Model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace KodiCMS\Widgets\Contracts;

use KodiCMS\Widgets\Exceptions\WidgetException;

interface Model
{

/**
* @return string
*/
public function getWidgetClass();

/**
* @return string
*/
public function getType();

/**
* @return \KodiCMS\Widgets\Contracts\Widget|null
* @throws WidgetException
*/
public function toWidget();

/**
* @return bool
*/
public function isWidgetable();

/**
* @return bool
*/
public function isHandler();

/**
* @return bool
*/
public function isRenderable();

/**
* @return bool
*/
public function isCacheable();

/**
* @return bool
*/
public function isClassExists();

/**
* @return bool
*/
public function isCorrupt();

/**
* @return array
*/
public function getLocations();
}
1 change: 0 additions & 1 deletion src/Manager/WidgetManagerDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public function getWidgetsByPage($pageId)
{
$widgets = Widget::whereHas('pages', function ($q) use ($pageId) {
$q->where('pages.id', (int) $pageId);

})->with('related')->get();

return static::buildWidgetCollection($widgets);
Expand Down
88 changes: 34 additions & 54 deletions src/Model/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
use DB;
use KodiCMS\Pages\Model\Page;
use Illuminate\Database\Eloquent\Model;
use KodiCMS\Widgets\Contracts\WidgetManager;
use KodiCMS\Widgets\Contracts\Model as WidgetContract;
use KodiCMS\Widgets\Manager\WidgetManagerDatabase;
use KodiCMS\Widgets\Traits\WidgetStates;
use KodiCMS\Widgets\Widget\Temp as TempWidget;
use KodiCMS\Widgets\Exceptions\WidgetException;
use KodiCMS\Widgets\Manager\WidgetManagerDatabase;

class Widget extends Model
class Widget extends Model implements WidgetContract
{
use WidgetStates;

/**
* @var array
*/
Expand Down Expand Up @@ -48,6 +51,31 @@ class Widget extends Model
*/
protected $widget = null;

/**
* @var WidgetManagerDatabase
*/
protected $manager;

/**
* Widget constructor.
*
* @param array $attributes
*/
public function __construct(array $attributes = [])
{
parent::__construct($attributes);

$this->manager = app('widget.manager');
}

/**
* @return string
*/
public function getWidgetClass()
{
return get_class($this->toWidget());
}

/**
* @param string $template
*/
Expand All @@ -61,7 +89,7 @@ public function setTemplateAttribute($template)
*/
public function getType()
{
foreach (app('widget.manager')->getAvailableTypes() as $group => $types) {
foreach ($this->manager->getAvailableTypes() as $group => $types) {
if (isset($types[$this->type])) {
return $types[$this->type];
}
Expand All @@ -86,7 +114,7 @@ public function toWidget()
return $this->widget;
}

if (! is_null($this->widget = app('widget.manager')->makeWidget($this->type, $this->name, $this->description, $this->settings))) {
if (! is_null($this->widget = $this->manager->makeWidget($this->type, $this->name, $this->description, $this->settings))) {
$this->widget->setId($this->id);

if ($this->isRenderable()) {
Expand All @@ -95,62 +123,14 @@ public function toWidget()

$this->widget->setRalatedWidgets($this->related);
} else {
$this->widget = new TempWidget(app('widget.manager'), $this->name, $this->description);
$this->widget = new TempWidget($this->manager, $this->name, $this->description);
}

static::$cachedWidgets[$this->id] = $this->widget;

return $this->widget;
}

/**
* @return bool
*/
public function isWidgetable()
{
return ($this->exists and app('widget.manager')->isWidgetable(get_class($this->toWidget())));
}

/**
* @return bool
*/
public function isHandler()
{
return app('widget.manager')->isHandler(get_class($this->toWidget()));
}

/**
* @return bool
*/
public function isRenderable()
{
return app('widget.manager')->isRenderable(get_class($this->toWidget()));
}

/**
* @return bool
*/
public function isCacheable()
{
return app('widget.manager')->isCacheable(get_class($this->toWidget()));
}

/**
* @return bool
*/
public function isClassExists()
{
return app('widget.manager')->isClassExists(get_class($this->toWidget()));
}

/**
* @return bool
*/
public function isCorrupt()
{
return app('widget.manager')->isCorrupt(get_class($this->toWidget()));
}

public function scopeFilterByType($query, array $types)
{
if (count($types) > 0) {
Expand Down
10 changes: 9 additions & 1 deletion src/Providers/EventsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,15 @@ public function boot(DispatcherContract $events)

$events->listen('view.page.edit', function ($page) {
if (acl_check('widgets::list') and $page->hasLayout()) {
echo view('widgets::widgets.page.iframe', compact('page'))->render();
// echo view('widgets::widgets.page.iframe', compact('page'))->render();

echo view('widgets::widgets.page.list', [
'page' => $page,
'widgetsCollection' => new PageWidgetCollection(
app('widget.manager'),
$page->id
)
])->render();
}
});

Expand Down
11 changes: 6 additions & 5 deletions src/Providers/ModuleServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ class ModuleServiceProvider extends ServiceProvider
{
public function boot()
{
$this->app->singleton('widget.manager', function () {
return new WidgetManagerDatabase();
});

$this->app->alias('widget.manager', WidgetManager::class);
$this->app['view']->addNamespace('snippets', snippets_path());
}

public function register()
{
$this->app->singleton('widget.manager', function () {
return new WidgetManagerDatabase();
});

$this->app->alias('widget.manager', WidgetManager::class);

$this->registerProviders([
BladeServiceProvider::class,
EventsServiceProvider::class,
Expand Down
1 change: 0 additions & 1 deletion src/Repository/WidgetRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace KodiCMS\Widgets\Repository;

use DB;
use Illuminate\Http\Request;
use KodiCMS\Widgets\Model\Widget;
use KodiCMS\CMS\Repository\BaseRepository;

Expand Down
55 changes: 55 additions & 0 deletions src/Traits/WidgetStates.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace KodiCMS\Widgets\Traits;


trait WidgetStates
{
/**
* @return bool
*/
public function isWidgetable()
{
return ($this->exists and $this->manager->isWidgetable($this->getWidgetClass()));
}

/**
* @return bool
*/
public function isHandler()
{
return $this->manager->isHandler($this->getWidgetClass());
}

/**
* @return bool
*/
public function isRenderable()
{
return $this->manager->isRenderable($this->getWidgetClass());
}

/**
* @return bool
*/
public function isCacheable()
{
return $this->manager->isCacheable($this->getWidgetClass());
}

/**
* @return bool
*/
public function isClassExists()
{
return $this->manager->isClassExists($this->getWidgetClass());
}

/**
* @return bool
*/
public function isCorrupt()
{
return $this->manager->isCorrupt($this->getWidgetClass());
}
}
Loading

0 comments on commit cbe493f

Please sign in to comment.