FEATURE: change directory structure
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
namespace App\CMS\Widgets\HtmlCode;
|
||||
|
||||
use App\CMS\Contracts\Widgets\PublicWidget;
|
||||
use App\CMS\Widgets\Contracts\WidgetRenderer;
|
||||
use App\CMS\Widgets\Exceptions\MissingParameterException;
|
||||
use App\Models\Widgets\Widget;
|
||||
|
||||
class HtmlCode implements PublicWidget
|
||||
{
|
||||
/**
|
||||
* Name getter
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getName()
|
||||
{
|
||||
return 'widgets/htmlCode.widgetName';
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for description
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getDescription()
|
||||
{
|
||||
return 'widgets/htmlCode.widgetDescription';
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for presenter/renderer of this widget
|
||||
*
|
||||
* @return WidgetRenderer
|
||||
*/
|
||||
public static function getPresenter()
|
||||
{
|
||||
return Presenters\HtmlCode::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Widget $widget
|
||||
* @param \stdClass $data
|
||||
*
|
||||
* @return Widget
|
||||
* @throws MissingParameterException
|
||||
*/
|
||||
public static function save(Widget $widget, \stdClass $data)
|
||||
{
|
||||
if (!isset($data->code) || empty($data->code)) {
|
||||
throw new MissingParameterException('code');
|
||||
}
|
||||
|
||||
$widgetSettings = [
|
||||
'code' => $data->code,
|
||||
'containerClass' => isset($data->containerClass) ? $data->containerClass : ''
|
||||
];
|
||||
|
||||
$widget->settings = json_encode($widgetSettings);
|
||||
|
||||
return $widget;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
namespace App\CMS\Widgets\HtmlCode\Presenters;
|
||||
|
||||
use App\Models\Widgets\Widget;
|
||||
use App\Models\Widgets\WidgetType;
|
||||
use App\CMS\Contracts\Widgets\WidgetRenderer;
|
||||
|
||||
class HtmlCode 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;
|
||||
$code = '
|
||||
<div class="html-code-widget ' . $widget->container_class . '">
|
||||
' . $settings->code . '
|
||||
</div>';
|
||||
|
||||
return $code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display form for creating new instance of widget
|
||||
*
|
||||
* @param WidgetType $type
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function renderAddForm(WidgetType $type)
|
||||
{
|
||||
return view('admin.widgets.htmlCode.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('admin.widgets.htmlCode.edit', ['widget' => $widget, 'settings' => $settings]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
namespace App\CMS\Widgets\Menu\Extensions;
|
||||
|
||||
use App\CMS\Widgets\Contracts\MenuExtensionHandler;
|
||||
|
||||
class URL implements MenuExtensionHandler {
|
||||
public function getPossibleValues()
|
||||
{
|
||||
return '*';
|
||||
}
|
||||
|
||||
public function getId()
|
||||
{
|
||||
return 'url';
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return trans('widgets/menu.extensionURL');
|
||||
}
|
||||
|
||||
public function getDescription()
|
||||
{
|
||||
return trans('widgets/menu.extensionURLDescription');
|
||||
}
|
||||
|
||||
public function getFormExtensionTemplate()
|
||||
{
|
||||
return 'admin.widgets.menu.extensions.url';
|
||||
}
|
||||
|
||||
public function save()
|
||||
{
|
||||
// TODO: Implement save() method.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
namespace App\CMS\Widgets\Menu;
|
||||
|
||||
use App\CMS\Contracts\Widgets\PublicWidget;
|
||||
use App\CMS\Widgets\Contracts\WidgetRenderer;
|
||||
use App\CMS\Widgets\Exceptions\MissingParameterException;
|
||||
use App\Models\Content\Content;
|
||||
use App\Models\Widgets\Widget;
|
||||
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
|
||||
|
||||
class Menu implements PublicWidget
|
||||
{
|
||||
/**
|
||||
* Name getter
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getName()
|
||||
{
|
||||
return 'widgets/menu.widgetName';
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for description
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getDescription()
|
||||
{
|
||||
return 'widgets/menu.widgetDescription';
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for presenter/renderer of this widget
|
||||
*
|
||||
* @return WidgetRenderer
|
||||
*/
|
||||
public static function getPresenter()
|
||||
{
|
||||
return Presenters\Menu::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \App\Models\Widget $widget
|
||||
* @param \stdClass $data
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function save(Widget $widget, \stdClass $data)
|
||||
{
|
||||
if (!isset($data->settings) || empty($data->settings) || !json_decode($data->settings)) {
|
||||
throw new MissingParameterException('settings');
|
||||
}
|
||||
|
||||
$links = json_decode($data->settings);
|
||||
$pages = self::selectContentPages(array_keys((array)$links));
|
||||
|
||||
foreach ($links as $link) {
|
||||
if (!isset($pages[$link->id])) {
|
||||
unset($links->{$link->id});
|
||||
}
|
||||
}
|
||||
|
||||
$settings = [
|
||||
'links' => $links,
|
||||
'containerClass' => $data->containerClass
|
||||
];
|
||||
|
||||
$widget->settings = json_encode($settings);
|
||||
|
||||
return $widget;
|
||||
}
|
||||
|
||||
public static function selectContentPages($ids)
|
||||
{
|
||||
$allowedStatuses = [Content::STATUS_PUBLIC];
|
||||
|
||||
if (\Auth::user()) {
|
||||
$allowedStatuses[] = Content::STATUS_PROTECTED;
|
||||
}
|
||||
|
||||
$pages = Content::where('language', LaravelLocalization::getCurrentLocale())
|
||||
->whereIn('id', $ids)
|
||||
->whereIn('status', $allowedStatuses)
|
||||
->get();
|
||||
$used = [];
|
||||
foreach ($pages as $page) {
|
||||
$used[$page->id] = $page;
|
||||
}
|
||||
|
||||
return $used;
|
||||
}
|
||||
}
|
||||
@@ -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