2016-10-29 18:54:10 +08:00
|
|
|
<?php
|
|
|
|
|
2019-07-02 22:22:05 +08:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-10-29 18:54:10 +08:00
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use Event;
|
|
|
|
use Closure;
|
2017-01-02 14:37:16 +08:00
|
|
|
use App\Events;
|
2019-07-03 16:19:13 +08:00
|
|
|
use Notification;
|
|
|
|
use App\Notifications;
|
2019-08-21 11:11:44 +08:00
|
|
|
use Illuminate\Support\Arr;
|
2019-06-28 23:47:30 +08:00
|
|
|
use Illuminate\Support\Str;
|
2016-10-29 18:54:10 +08:00
|
|
|
|
|
|
|
class Hook
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Add an item to menu.
|
|
|
|
*
|
2019-08-15 15:02:40 +08:00
|
|
|
* @param string $category 'user' or 'admin' or 'explore'
|
2016-10-29 18:54:10 +08:00
|
|
|
* @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
|
2019-06-29 00:07:40 +08:00
|
|
|
* 'icon' => 'fa-book', # font-awesome icon
|
|
|
|
* 'new-tab' => false, # open the link in new tab or not, false by default
|
2016-10-29 18:54:10 +08:00
|
|
|
* ]
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-07-02 22:22:05 +08:00
|
|
|
public static function addMenuItem(string $category, int $position, array $menu): void
|
2016-10-29 18:54:10 +08:00
|
|
|
{
|
2019-06-28 23:47:30 +08:00
|
|
|
$class = 'App\Events\Configure'.Str::title($category).'Menu';
|
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
|
|
|
*/
|
2019-07-02 22:22:05 +08:00
|
|
|
public static function addRoute(Closure $callback): void
|
2016-10-29 18:54:10 +08:00
|
|
|
{
|
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
|
|
|
|
2019-08-15 15:02:40 +08:00
|
|
|
public static function registerPluginTransScripts(string $name, $pages = ['*'], $priority = 999): void
|
2017-01-02 14:37:16 +08:00
|
|
|
{
|
2019-08-15 15:02:40 +08:00
|
|
|
Event::listen(Events\RenderingFooter::class, function ($event) use ($name, $pages) {
|
2018-08-07 13:17:28 +08:00
|
|
|
foreach ($pages as $pattern) {
|
2019-08-15 15:02:40 +08:00
|
|
|
if (! 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-08-15 15:02:40 +08:00
|
|
|
$basepath = config('plugins.url') ?: url('plugins').'/'.$name.'/';
|
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
|
|
|
}
|
|
|
|
|
2019-07-02 22:22:05 +08:00
|
|
|
public static function addStyleFileToPage($urls, $pages = ['*'], $priority = 1): void
|
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-08-15 15:02:40 +08:00
|
|
|
if (! 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);
|
|
|
|
}
|
|
|
|
|
2019-07-02 22:22:05 +08:00
|
|
|
public static function addScriptFileToPage($urls, $pages = ['*'], $priority = 1): void
|
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-08-15 15:02:40 +08:00
|
|
|
if (! 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);
|
|
|
|
}
|
2019-07-02 23:34:27 +08:00
|
|
|
|
|
|
|
public static function addUserBadge(string $text, $color = 'primary'): void
|
|
|
|
{
|
|
|
|
Event::listen(
|
|
|
|
Events\RenderingBadges::class,
|
|
|
|
function (Events\RenderingBadges $event) use ($text, $color) {
|
|
|
|
$event->badges[] = [$text, $color];
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2019-07-03 16:19:13 +08:00
|
|
|
|
|
|
|
public static function sendNotification($users, string $title, $content = ''): void
|
|
|
|
{
|
2019-08-21 11:11:44 +08:00
|
|
|
Notification::send(Arr::wrap($users), new Notifications\SiteMessage($title, $content));
|
2019-07-03 16:19:13 +08:00
|
|
|
}
|
2019-07-03 19:33:08 +08:00
|
|
|
|
|
|
|
public static function pushMiddleware($middleware)
|
|
|
|
{
|
|
|
|
app()->make('Illuminate\Contracts\Http\Kernel')->pushMiddleware($middleware);
|
|
|
|
}
|
2016-10-29 18:54:10 +08:00
|
|
|
}
|