mirror of
https://github.com/getgrav/grav-plugin-admin.git
synced 2026-01-04 14:50:59 +01:00
Update plugin to Grav 0.9 (DI and Event changes)
This commit is contained in:
81
admin.php
81
admin.php
@@ -1,13 +1,12 @@
|
||||
<?php
|
||||
namespace Grav\Plugin;
|
||||
|
||||
use \Grav\Common\Plugin;
|
||||
use \Grav\Common\Registry;
|
||||
use \Grav\Common\Page\Page;
|
||||
use \Grav\Common\Page\Pages;
|
||||
use \Grav\Common\Filesystem\File;
|
||||
use \Grav\Common\Grav;
|
||||
use \Grav\Common\Uri;
|
||||
use Grav\Common\Plugin;
|
||||
use Grav\Common\Page\Page;
|
||||
use Grav\Common\Page\Pages;
|
||||
use Grav\Common\Filesystem\File;
|
||||
use Grav\Common\Grav;
|
||||
use Grav\Common\Uri;
|
||||
|
||||
class AdminPlugin extends Plugin
|
||||
{
|
||||
@@ -36,12 +35,21 @@ class AdminPlugin extends Plugin
|
||||
*/
|
||||
protected $admin;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public static function getSubscribedEvents() {
|
||||
return [
|
||||
'onPluginsInitialized' => ['onPluginsInitialized', 1000]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize administration plugin if admin path matches.
|
||||
*
|
||||
* Disables system cache.
|
||||
*/
|
||||
public function onAfterInitPlugins()
|
||||
public function onPluginsInitialized()
|
||||
{
|
||||
$route = $this->config->get('plugins.admin.route');
|
||||
|
||||
@@ -49,12 +57,17 @@ class AdminPlugin extends Plugin
|
||||
return;
|
||||
}
|
||||
|
||||
$this->uri = Registry::get('Uri');
|
||||
$this->uri = $this->grav['uri'];
|
||||
$base = '/' . trim($route, '/');
|
||||
|
||||
// Only activate admin if we're inside the admin path.
|
||||
if (substr($this->uri->route(), 0, strlen($base)) == $base) {
|
||||
$this->active = true;
|
||||
$this->enable([
|
||||
'onPagesInitialized' => ['onPagesInitialized', 1000],
|
||||
'onPageInitialized' => ['onPageInitialized', 1000],
|
||||
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 1000],
|
||||
'onTwigSiteVariables' => ['onTwigSiteVariables', 1000]
|
||||
]);
|
||||
|
||||
// Disable system caching.
|
||||
$this->config->set('system.cache.enabled', false);
|
||||
@@ -76,40 +89,31 @@ class AdminPlugin extends Plugin
|
||||
|
||||
// Initialize admin class.
|
||||
require_once __DIR__ . '/classes/admin.php';
|
||||
$this->admin = new Admin($base, $this->template, $this->route);
|
||||
$this->admin = new Admin($this->grav, $base, $this->template, $this->route);
|
||||
|
||||
// And store the class into registry.
|
||||
$registry = Registry::instance();
|
||||
$registry->store('Admin', $this->admin);
|
||||
// And store the class into DI container.
|
||||
$this->grav['admin'] = $this->admin;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets longer path to the home page allowing us to have list of pages when we enter to pages section.
|
||||
*/
|
||||
public function onAfterGetPages()
|
||||
public function onPagesInitialized()
|
||||
{
|
||||
if (!$this->active) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Set original route for the home page.
|
||||
$home = '/' . trim($this->config->get('system.home.alias'), '/');
|
||||
|
||||
/** @var Pages $pages */
|
||||
$pages = Registry::get('Pages');
|
||||
$pages = $this->grav['pages'];
|
||||
$pages->dispatch('/', true)->route($home);
|
||||
}
|
||||
|
||||
/**
|
||||
* Main administration controller.
|
||||
*/
|
||||
public function onAfterGetPage()
|
||||
public function onPageInitialized()
|
||||
{
|
||||
if (!$this->active) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Set page if user hasn't been authorised.
|
||||
if (!$this->admin->authorise()) {
|
||||
$this->template = $this->admin->user ? 'denied' : 'login';
|
||||
@@ -122,7 +126,7 @@ class AdminPlugin extends Plugin
|
||||
$task = !empty($post['task']) ? $post['task'] : $this->uri->param('task');
|
||||
if ($task) {
|
||||
require_once __DIR__ . '/classes/controller.php';
|
||||
$controller = new AdminController($this->template, $task, $this->route, $post);
|
||||
$controller = new AdminController($this->grav, $this->template, $task, $this->route, $post);
|
||||
$success = $controller->execute();
|
||||
$controller->redirect();
|
||||
} elseif ($this->template == 'logs' && $this->route) {
|
||||
@@ -132,39 +136,34 @@ class AdminPlugin extends Plugin
|
||||
}
|
||||
|
||||
/** @var Grav $grav */
|
||||
$grav = Registry::get('Grav');
|
||||
$grav = $this->grav;
|
||||
|
||||
// Finally create admin page.
|
||||
$page = new Page;
|
||||
$page->init(new \SplFileInfo(__DIR__ . "/pages/admin/{$this->template}.md"));
|
||||
$page->slug(basename($this->template));
|
||||
$grav->page = $page;
|
||||
|
||||
unset($grav['page']);
|
||||
$grav['page'] = $page;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add twig paths to plugin templates.
|
||||
*/
|
||||
public function onAfterTwigTemplatesPaths()
|
||||
public function onTwigTemplatePaths()
|
||||
{
|
||||
if (!$this->active) {
|
||||
return;
|
||||
}
|
||||
|
||||
$twig = Registry::get('Twig');
|
||||
$twig = $this->grav['twig'];
|
||||
$twig->twig_paths = array(__DIR__ . '/theme/templates');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set all twig variables for generating output.
|
||||
*/
|
||||
public function onAfterSiteTwigVars()
|
||||
public function onTwigSiteVariables()
|
||||
{
|
||||
if (!$this->active) {
|
||||
return;
|
||||
}
|
||||
|
||||
$theme_url = $this->config->get('system.base_url_relative') . '/user/plugins/' . basename(__DIR__) . '/theme';
|
||||
$twig = Registry::get('Twig');
|
||||
// TODO: use real plugin name instead
|
||||
$theme_url = $this->config->get('system.base_url_relative') . '/user/plugins/admin/theme';
|
||||
$twig = $this->grav['twig'];
|
||||
|
||||
$twig->template = $this->template . '.html.twig';
|
||||
$twig->twig_vars['location'] = $this->template;
|
||||
|
||||
Reference in New Issue
Block a user