2016-10-29 18:54:10 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use Event;
|
|
|
|
use Closure;
|
2017-01-02 14:37:16 +08:00
|
|
|
use App\Events;
|
2016-10-29 18:54:10 +08:00
|
|
|
|
|
|
|
class Hook
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Add an item to menu.
|
|
|
|
*
|
|
|
|
* @param string $category 'user' or 'admin'
|
|
|
|
* @param int $position Where to insert the given item, start from 0.
|
|
|
|
* @param array $menu e.g.
|
|
|
|
* [
|
|
|
|
* 'title' => 'Title', # will be translated by translator
|
|
|
|
* 'link' => 'user/config', # route link
|
|
|
|
* 'icon' => 'fa-book' # font-awesome icon
|
|
|
|
* ]
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function addMenuItem($category, $position, array $menu)
|
|
|
|
{
|
2018-08-07 10:59:15 +08:00
|
|
|
$class = $category == 'user' ? Events\ConfigureUserMenu::class : Events\ConfigureAdminMenu::class;
|
2016-11-17 17:32:12 +08:00
|
|
|
|
2018-08-07 10:59:15 +08:00
|
|
|
Event::listen($class, function ($event) use ($menu, $position, $category) {
|
2016-10-29 18:54:10 +08:00
|
|
|
$new = [];
|
|
|
|
|
|
|
|
$offset = 0;
|
2016-11-17 17:32:12 +08:00
|
|
|
foreach ($event->menu[$category] as $item) {
|
2018-02-16 17:31:04 +08:00
|
|
|
// Push new menu items at the given position
|
2016-10-29 18:54:10 +08:00
|
|
|
if ($offset == $position) {
|
|
|
|
$new[] = $menu;
|
|
|
|
}
|
|
|
|
|
|
|
|
$new[] = $item;
|
|
|
|
$offset++;
|
|
|
|
}
|
|
|
|
|
2018-08-21 14:43:46 +08:00
|
|
|
if ($position >= $offset) {
|
|
|
|
$new[] = $menu;
|
|
|
|
}
|
|
|
|
|
2016-11-17 17:32:12 +08:00
|
|
|
$event->menu[$category] = $new;
|
2016-10-29 18:54:10 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-05 09:31:36 +08:00
|
|
|
* Add routes. A router instance will be passed to the given callback.
|
2016-10-29 18:54:10 +08:00
|
|
|
*
|
|
|
|
* @param Closure $callback
|
|
|
|
*/
|
|
|
|
public static function addRoute(Closure $callback)
|
|
|
|
{
|
2018-08-07 10:59:15 +08:00
|
|
|
Event::listen(Events\ConfigureRoutes::class, function ($event) use ($callback) {
|
2016-10-29 18:54:10 +08:00
|
|
|
return call_user_func($callback, $event->router);
|
|
|
|
});
|
|
|
|
}
|
2017-01-02 14:37:16 +08:00
|
|
|
|
2018-08-07 13:17:28 +08:00
|
|
|
public static function registerPluginTransScripts($id, $pages = ['*'], $priority = 999)
|
2017-01-02 14:37:16 +08:00
|
|
|
{
|
2018-08-07 13:17:28 +08:00
|
|
|
Event::listen(Events\RenderingFooter::class, function ($event) use ($id, $pages) {
|
|
|
|
foreach ($pages as $pattern) {
|
2019-03-02 22:58:37 +08:00
|
|
|
if (! app('request')->is($pattern)) {
|
2018-08-07 13:17:28 +08:00
|
|
|
continue;
|
2019-03-02 22:58:37 +08:00
|
|
|
}
|
2017-01-02 14:37:16 +08:00
|
|
|
|
2018-08-07 13:17:28 +08:00
|
|
|
// We will determine current locale in the event callback,
|
|
|
|
// otherwise the locale is not properly detected.
|
2019-03-21 19:45:52 +08:00
|
|
|
$basepath = config('plugins.url') ?: url('plugins').'/'.$id.'/';
|
2018-08-07 13:17:28 +08:00
|
|
|
$relative = 'lang/'.config('app.locale').'/locale.js';
|
|
|
|
|
2019-03-21 19:45:52 +08:00
|
|
|
$event->addContent(
|
|
|
|
'<script src="'.$basepath.$relative.'"></script>'
|
|
|
|
);
|
2018-08-07 13:17:28 +08:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}, $priority);
|
2017-01-02 14:37:16 +08:00
|
|
|
}
|
|
|
|
|
2017-01-08 16:05:54 +08:00
|
|
|
public static function addStyleFileToPage($urls, $pages = ['*'], $priority = 1)
|
2017-01-02 14:37:16 +08:00
|
|
|
{
|
2018-08-07 10:59:15 +08:00
|
|
|
Event::listen(Events\RenderingHeader::class, function ($event) use ($urls, $pages) {
|
2017-01-08 16:05:54 +08:00
|
|
|
foreach ($pages as $pattern) {
|
2019-03-02 22:58:37 +08:00
|
|
|
if (! app('request')->is($pattern)) {
|
2017-01-08 16:05:54 +08:00
|
|
|
continue;
|
2019-03-02 22:58:37 +08:00
|
|
|
}
|
2017-01-08 16:05:54 +08:00
|
|
|
|
|
|
|
foreach ((array) $urls as $url) {
|
|
|
|
$event->addContent("<link rel=\"stylesheet\" href=\"$url\">");
|
|
|
|
}
|
2017-01-08 16:12:28 +08:00
|
|
|
|
|
|
|
return;
|
2017-01-02 14:37:16 +08:00
|
|
|
}
|
|
|
|
}, $priority);
|
|
|
|
}
|
|
|
|
|
2017-01-08 16:05:54 +08:00
|
|
|
public static function addScriptFileToPage($urls, $pages = ['*'], $priority = 1)
|
2017-01-02 14:37:16 +08:00
|
|
|
{
|
2018-08-07 10:59:15 +08:00
|
|
|
Event::listen(Events\RenderingFooter::class, function ($event) use ($urls, $pages) {
|
2017-01-08 16:05:54 +08:00
|
|
|
foreach ($pages as $pattern) {
|
2019-03-02 22:58:37 +08:00
|
|
|
if (! app('request')->is($pattern)) {
|
2017-01-08 16:05:54 +08:00
|
|
|
continue;
|
2019-03-02 22:58:37 +08:00
|
|
|
}
|
2017-01-08 16:05:54 +08:00
|
|
|
|
|
|
|
foreach ((array) $urls as $url) {
|
|
|
|
$event->addContent("<script src=\"$url\"></script>");
|
|
|
|
}
|
2017-01-08 16:12:28 +08:00
|
|
|
|
|
|
|
return;
|
2017-01-02 14:37:16 +08:00
|
|
|
}
|
|
|
|
}, $priority);
|
|
|
|
}
|
2016-10-29 18:54:10 +08:00
|
|
|
}
|