145 lines
3.8 KiB
PHP
145 lines
3.8 KiB
PHP
<?php
|
|
namespace App\Widgets\Menu\Presenters;
|
|
|
|
use App\Models\Content;
|
|
use App\Models\Widget;
|
|
use App\Models\WidgetType;
|
|
use App\Widgets\Contracts\WidgetRenderer;
|
|
use Illuminate\Support\Facades\App;
|
|
use Illuminate\Support\Facades\Request;
|
|
use Illuminate\Support\Facades\Route;
|
|
|
|
class Menu implements WidgetRenderer
|
|
{
|
|
protected static function selectContentPages($ids)
|
|
{
|
|
$pages = Content::where('status', '!=', Content::STATUS_DELETED)
|
|
->whereIn('id', $ids)
|
|
->get();
|
|
$used = [];
|
|
|
|
foreach ($pages as $page) {
|
|
$used[$page->id] = $page;
|
|
}
|
|
|
|
return $used;
|
|
}
|
|
|
|
/**
|
|
* Method used for displaying widget.
|
|
*
|
|
* This method renders some HTML code for displaying.
|
|
*
|
|
* @param object $settings
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public static function render($settings)
|
|
{
|
|
$links = (array)$settings->links;
|
|
|
|
$pageIds = array_keys($links);
|
|
|
|
// get pages
|
|
$pages = self::selectContentPages($pageIds);
|
|
|
|
// sort links using "order"
|
|
uasort($links, function ($a, $b) {
|
|
return $a->order - $b->order;
|
|
});
|
|
|
|
$route = Request::route()
|
|
->getName();
|
|
$parameters = Route::current()
|
|
->parameters();
|
|
|
|
$code = '<ul class="menu ' . $settings->containerClass . '">';
|
|
|
|
foreach ($links as $pageId => $link) {
|
|
if (!isset($pages[$pageId])) {
|
|
continue;
|
|
}
|
|
$page = $pages[$pageId];
|
|
|
|
$active = false;
|
|
if ($route == 'page.content' && isset($parameters['url']) && $parameters['url'] == $page->url) {
|
|
$active = true;
|
|
}
|
|
|
|
$code .= '<li';
|
|
if ($active) {
|
|
$code .= ' class="is-active active"';
|
|
}
|
|
$code .= '><a href="' . route('page.content', ['url' => $page->url]) . '">' . e($page->title) . '</a></li>';
|
|
}
|
|
|
|
$code .= "</ul>";
|
|
|
|
return $code;
|
|
}
|
|
|
|
/**
|
|
* Display form for creating new instance of widget
|
|
*
|
|
* @param WidgetType $type
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public static function renderAddForm(WidgetType $type)
|
|
{
|
|
$extensions = App::make('registerMenuExtension')
|
|
->get();
|
|
|
|
$widget = new Widget();
|
|
$content = new Content();
|
|
|
|
// get list of all pages
|
|
$pages = $content->getOverview()
|
|
->get();
|
|
|
|
$widget->settingsObj = new \stdClass();
|
|
$widget->settingsObj->links = new \stdClass();
|
|
|
|
return view('admin.widgets.menu.new', [
|
|
'type' => $type,
|
|
'widget' => $widget,
|
|
'pages' => $pages,
|
|
'extensions' => $extensions
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Display form for editing already existing widget
|
|
*
|
|
* @param Widget $widget
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public static function renderEditForm(Widget $widget)
|
|
{
|
|
// $extensions = App::make('registerMenuExtension')
|
|
// ->get();
|
|
$content = new Content();
|
|
|
|
// get list of all pages
|
|
$pages = $content->getOverview()
|
|
->get();
|
|
|
|
// make object from JSON
|
|
$widget->settingsObj = json_decode($widget->settings);
|
|
if (!isset($widget->settingsObj->links)) {
|
|
$links = new \stdClass();
|
|
} else {
|
|
$links = (array)$widget->settingsObj->links;
|
|
}
|
|
|
|
// sort links using "order"
|
|
uasort($links, function ($a, $b) {
|
|
return $a->order - $b->order;
|
|
});
|
|
|
|
$widget->settingsObj->links = (object)$links;
|
|
|
|
return view('admin.widgets.menu.edit', ['widget' => $widget, 'pages' => $pages]);
|
|
}
|
|
} |