136 lines
3.7 KiB
PHP
136 lines
3.7 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 App\Widgets\Menu\Menu as WidgetMenu;
|
|
use Illuminate\Support\Facades\App;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Request;
|
|
use Illuminate\Support\Facades\Route;
|
|
|
|
class Menu implements WidgetRenderer
|
|
{
|
|
/**
|
|
* Method used for displaying widget.
|
|
*
|
|
* This method renders some HTML code for displaying.
|
|
*
|
|
* @param Widget $widget
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public static function render(Widget $widget)
|
|
{
|
|
$settings = $widget->settings;
|
|
$links = (array)$settings->links;
|
|
$pageIds = array_keys($links);
|
|
|
|
// get pages
|
|
$pages = WidgetMenu::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 ' . $widget->container_class . '"';
|
|
|
|
if ($widget->container_id)
|
|
$code .= ' id="'.e($widget->container_id).'"';
|
|
|
|
$code .= '>';
|
|
|
|
foreach ($links as $pageId => $link) {
|
|
if (!isset($pages[$pageId])) {
|
|
continue;
|
|
}
|
|
|
|
$page = $pages[$pageId];
|
|
$active = ($page->is_homepage && $route == 'page.index') || ($route == 'page.content' && isset($parameters['url']) && $parameters['url'] == $page->url);
|
|
$route = $page->is_homepage ? route('page.index') : route('page.content', ['url' => $page->url]);
|
|
|
|
$code .= '<li';
|
|
if ($active) {
|
|
$code .= ' class="is-active active"';
|
|
}
|
|
$code .= '><a href="' . $route . '">' . 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 = $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]);
|
|
}
|
|
} |