FEATURE: change directory structure
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
namespace App\CMS\Widgets\Menu\Presenters;
|
||||
|
||||
use App\CMS\Contracts\Widgets\WidgetRenderer;
|
||||
use App\CMS\Widgets\Menu\Menu as WidgetMenu;
|
||||
use App\Models\Content\Content;
|
||||
use App\Models\Widgets\Widget;
|
||||
use App\Models\Widgets\WidgetType;
|
||||
use Illuminate\Support\Facades\App;
|
||||
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)
|
||||
{
|
||||
$content = new Content();
|
||||
|
||||
// get list of all pages
|
||||
$pages = $content->getOverview()
|
||||
->get();
|
||||
|
||||
// make object from JSON
|
||||
if (!isset($widget->settings->links)) {
|
||||
$links = new \stdClass();
|
||||
} else {
|
||||
$links = (array)$widget->settings->links;
|
||||
}
|
||||
|
||||
// sort links using "order"
|
||||
uasort($links, function ($a, $b) {
|
||||
return $a->order - $b->order;
|
||||
});
|
||||
|
||||
$widget->settings->links = (object)$links;
|
||||
|
||||
return view('admin.widgets.menu.edit', ['widget' => $widget, 'pages' => $pages]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user