FEATURE: prepare widget for displaying menu on onepage templates

This commit is contained in:
Michal Kváček
2016-09-25 01:35:03 +02:00
parent a37767c2e8
commit 09151e97d1
8 changed files with 246 additions and 24 deletions
@@ -0,0 +1,55 @@
<?php
namespace Templates\Base\OnePage\Widgets\OnePageMenu;
use App\Models\Widget;
use App\Widgets\Contracts\WidgetRenderer;
use App\Widgets\NonInstallableWidget;
use stdClass;
class OnePageMenu extends NonInstallableWidget
{
/**
* Name getter
*
* @return string
*/
public static function getName()
{
return 'template::widget/menu.name';
}
/**
* Getter for description
*
* @return string
*/
public static function getDescription()
{
return 'template::widget/menu.description';
}
/**
* Getter for presenter/renderer of this widget
*
* @return WidgetRenderer
*/
public static function getPresenter()
{
return Presenter::class;
}
/**
* @param Widget $widget
* @param stdClass $data
*
* @return mixed
*/
public static function save(Widget $widget, stdClass $data)
{
$widgetSettings = ['containerClass' => isset($data->containerClass) ? $data->containerClass : ''];
$widget->settings = json_encode($widgetSettings);
return $widget;
}
}
@@ -0,0 +1,65 @@
<?php
namespace Templates\Base\OnePage\Widgets\OnePageMenu;
use App\Models\Widget;
use App\Models\WidgetType;
use App\Widgets\Contracts\WidgetRenderer;
use Templates\Base\OnePage\Handlers\OnePage;
class Presenter implements WidgetRenderer
{
/**
* @var string path to template
*/
public static $template;
/**
* Method used for displaying widget.
*
* This method renders some HTML code for displaying.
*
* @param object $settings
*
* @return mixed
* @throws \ErrorException
*/
public static function render($settings)
{
if (!self::$template) {
throw new \ErrorException('Cannot render menu without template! Please, set Presenter::$template variable before rendering.');
}
$pages = OnePage::getPagesOnHomepage();
return view(self::$template, ['pages' => $pages])->render();
}
/**
* Display form for creating new instance of widget
*
* @param WidgetType $type
*
* @return mixed
*/
public static function renderAddForm(WidgetType $type)
{
return view('template::admin.widgets.onePageMenu.new', [
'type' => $type,
'widget' => new Widget(),
'settings' => new \stdClass()
]);
}
/**
* Display form for editing already existing widget
*
* @param Widget $widget
*
* @return mixed
*/
public static function renderEditForm(Widget $widget)
{
$settings = json_decode($widget->settings);
return view('template::admin.widgets.onePageMenu.edit', ['widget' => $widget, 'settings' => $settings]);
}
}