mirror of
https://github.com/getgrav/grav-plugin-admin.git
synced 2025-11-05 04:46:03 +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
|
<?php
|
||||||
namespace Grav\Plugin;
|
namespace Grav\Plugin;
|
||||||
|
|
||||||
use \Grav\Common\Plugin;
|
use Grav\Common\Plugin;
|
||||||
use \Grav\Common\Registry;
|
use Grav\Common\Page\Page;
|
||||||
use \Grav\Common\Page\Page;
|
use Grav\Common\Page\Pages;
|
||||||
use \Grav\Common\Page\Pages;
|
use Grav\Common\Filesystem\File;
|
||||||
use \Grav\Common\Filesystem\File;
|
use Grav\Common\Grav;
|
||||||
use \Grav\Common\Grav;
|
use Grav\Common\Uri;
|
||||||
use \Grav\Common\Uri;
|
|
||||||
|
|
||||||
class AdminPlugin extends Plugin
|
class AdminPlugin extends Plugin
|
||||||
{
|
{
|
||||||
@@ -36,12 +35,21 @@ class AdminPlugin extends Plugin
|
|||||||
*/
|
*/
|
||||||
protected $admin;
|
protected $admin;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public static function getSubscribedEvents() {
|
||||||
|
return [
|
||||||
|
'onPluginsInitialized' => ['onPluginsInitialized', 1000]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize administration plugin if admin path matches.
|
* Initialize administration plugin if admin path matches.
|
||||||
*
|
*
|
||||||
* Disables system cache.
|
* Disables system cache.
|
||||||
*/
|
*/
|
||||||
public function onAfterInitPlugins()
|
public function onPluginsInitialized()
|
||||||
{
|
{
|
||||||
$route = $this->config->get('plugins.admin.route');
|
$route = $this->config->get('plugins.admin.route');
|
||||||
|
|
||||||
@@ -49,12 +57,17 @@ class AdminPlugin extends Plugin
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->uri = Registry::get('Uri');
|
$this->uri = $this->grav['uri'];
|
||||||
$base = '/' . trim($route, '/');
|
$base = '/' . trim($route, '/');
|
||||||
|
|
||||||
// Only activate admin if we're inside the admin path.
|
// Only activate admin if we're inside the admin path.
|
||||||
if (substr($this->uri->route(), 0, strlen($base)) == $base) {
|
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.
|
// Disable system caching.
|
||||||
$this->config->set('system.cache.enabled', false);
|
$this->config->set('system.cache.enabled', false);
|
||||||
@@ -76,40 +89,31 @@ class AdminPlugin extends Plugin
|
|||||||
|
|
||||||
// Initialize admin class.
|
// Initialize admin class.
|
||||||
require_once __DIR__ . '/classes/admin.php';
|
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.
|
// And store the class into DI container.
|
||||||
$registry = Registry::instance();
|
$this->grav['admin'] = $this->admin;
|
||||||
$registry->store('Admin', $this->admin);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets longer path to the home page allowing us to have list of pages when we enter to pages section.
|
* 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.
|
// Set original route for the home page.
|
||||||
$home = '/' . trim($this->config->get('system.home.alias'), '/');
|
$home = '/' . trim($this->config->get('system.home.alias'), '/');
|
||||||
|
|
||||||
/** @var Pages $pages */
|
/** @var Pages $pages */
|
||||||
$pages = Registry::get('Pages');
|
$pages = $this->grav['pages'];
|
||||||
$pages->dispatch('/', true)->route($home);
|
$pages->dispatch('/', true)->route($home);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main administration controller.
|
* Main administration controller.
|
||||||
*/
|
*/
|
||||||
public function onAfterGetPage()
|
public function onPageInitialized()
|
||||||
{
|
{
|
||||||
if (!$this->active) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set page if user hasn't been authorised.
|
// Set page if user hasn't been authorised.
|
||||||
if (!$this->admin->authorise()) {
|
if (!$this->admin->authorise()) {
|
||||||
$this->template = $this->admin->user ? 'denied' : 'login';
|
$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');
|
$task = !empty($post['task']) ? $post['task'] : $this->uri->param('task');
|
||||||
if ($task) {
|
if ($task) {
|
||||||
require_once __DIR__ . '/classes/controller.php';
|
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();
|
$success = $controller->execute();
|
||||||
$controller->redirect();
|
$controller->redirect();
|
||||||
} elseif ($this->template == 'logs' && $this->route) {
|
} elseif ($this->template == 'logs' && $this->route) {
|
||||||
@@ -132,39 +136,34 @@ class AdminPlugin extends Plugin
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @var Grav $grav */
|
/** @var Grav $grav */
|
||||||
$grav = Registry::get('Grav');
|
$grav = $this->grav;
|
||||||
|
|
||||||
// Finally create admin page.
|
// Finally create admin page.
|
||||||
$page = new Page;
|
$page = new Page;
|
||||||
$page->init(new \SplFileInfo(__DIR__ . "/pages/admin/{$this->template}.md"));
|
$page->init(new \SplFileInfo(__DIR__ . "/pages/admin/{$this->template}.md"));
|
||||||
$page->slug(basename($this->template));
|
$page->slug(basename($this->template));
|
||||||
$grav->page = $page;
|
|
||||||
|
unset($grav['page']);
|
||||||
|
$grav['page'] = $page;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add twig paths to plugin templates.
|
* Add twig paths to plugin templates.
|
||||||
*/
|
*/
|
||||||
public function onAfterTwigTemplatesPaths()
|
public function onTwigTemplatePaths()
|
||||||
{
|
{
|
||||||
if (!$this->active) {
|
$twig = $this->grav['twig'];
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$twig = Registry::get('Twig');
|
|
||||||
$twig->twig_paths = array(__DIR__ . '/theme/templates');
|
$twig->twig_paths = array(__DIR__ . '/theme/templates');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set all twig variables for generating output.
|
* Set all twig variables for generating output.
|
||||||
*/
|
*/
|
||||||
public function onAfterSiteTwigVars()
|
public function onTwigSiteVariables()
|
||||||
{
|
{
|
||||||
if (!$this->active) {
|
// TODO: use real plugin name instead
|
||||||
return;
|
$theme_url = $this->config->get('system.base_url_relative') . '/user/plugins/admin/theme';
|
||||||
}
|
$twig = $this->grav['twig'];
|
||||||
|
|
||||||
$theme_url = $this->config->get('system.base_url_relative') . '/user/plugins/' . basename(__DIR__) . '/theme';
|
|
||||||
$twig = Registry::get('Twig');
|
|
||||||
|
|
||||||
$twig->template = $this->template . '.html.twig';
|
$twig->template = $this->template . '.html.twig';
|
||||||
$twig->twig_vars['location'] = $this->template;
|
$twig->twig_vars['location'] = $this->template;
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ use Grav\Common\User\Authentication;
|
|||||||
use Grav\Common\Filesystem\File;
|
use Grav\Common\Filesystem\File;
|
||||||
use Grav\Common\Grav;
|
use Grav\Common\Grav;
|
||||||
use Grav\Common\Plugins;
|
use Grav\Common\Plugins;
|
||||||
use Grav\Common\Registry;
|
|
||||||
use Grav\Common\Session;
|
use Grav\Common\Session;
|
||||||
use Grav\Common\Themes;
|
use Grav\Common\Themes;
|
||||||
use Grav\Common\Uri;
|
use Grav\Common\Uri;
|
||||||
@@ -16,6 +15,11 @@ use Grav\Common\Data;
|
|||||||
|
|
||||||
class Admin
|
class Admin
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var Grav
|
||||||
|
*/
|
||||||
|
public $grav;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Uri $uri
|
* @var Uri $uri
|
||||||
*/
|
*/
|
||||||
@@ -64,18 +68,20 @@ class Admin
|
|||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*
|
*
|
||||||
|
* @param Grav $grav
|
||||||
* @param string $base
|
* @param string $base
|
||||||
* @param string $location
|
* @param string $location
|
||||||
* @param string $route
|
* @param string $route
|
||||||
*/
|
*/
|
||||||
public function __construct($base, $location, $route)
|
public function __construct(Grav $grav, $base, $location, $route)
|
||||||
{
|
{
|
||||||
|
$this->grav = $grav;
|
||||||
$this->base = $base;
|
$this->base = $base;
|
||||||
$this->location = $location;
|
$this->location = $location;
|
||||||
$this->route = $route;
|
$this->route = $route;
|
||||||
|
|
||||||
/** @var Uri uri */
|
/** @var Uri uri */
|
||||||
$this->uri = Registry::get('Uri');
|
$this->uri = $this->grav['uri'];
|
||||||
|
|
||||||
// TODO: add session timeout into configuration
|
// TODO: add session timeout into configuration
|
||||||
$this->session = new Session\Session(1800, $this->uri->rootUrl(false) . $base);
|
$this->session = new Session\Session(1800, $this->uri->rootUrl(false) . $base);
|
||||||
@@ -148,7 +154,7 @@ class Admin
|
|||||||
$this->user = $this->session->user = $user;
|
$this->user = $this->session->user = $user;
|
||||||
|
|
||||||
/** @var Grav $grav */
|
/** @var Grav $grav */
|
||||||
$grav = Registry::get('Grav');
|
$grav = $this->grav;
|
||||||
$grav->redirect($this->uri->route());
|
$grav->redirect($this->uri->route());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -285,7 +291,7 @@ class Admin
|
|||||||
*/
|
*/
|
||||||
public function themes()
|
public function themes()
|
||||||
{
|
{
|
||||||
return Themes::all();
|
return $this->grav['themes']->all();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -324,7 +330,7 @@ class Admin
|
|||||||
protected function getPage($path)
|
protected function getPage($path)
|
||||||
{
|
{
|
||||||
/** @var Pages $pages */
|
/** @var Pages $pages */
|
||||||
$pages = Registry::get('Pages');
|
$pages = $this->grav['pages'];
|
||||||
|
|
||||||
if ($path && $path[0] != '/') {
|
if ($path && $path[0] != '/') {
|
||||||
$path = "/{$path}";
|
$path = "/{$path}";
|
||||||
@@ -371,6 +377,6 @@ class Admin
|
|||||||
*/
|
*/
|
||||||
public static function route()
|
public static function route()
|
||||||
{
|
{
|
||||||
return dirname('/' . Registry::get('Admin')->route);
|
return dirname('/' . Grav::instance()['admin']->route);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ namespace Grav\Plugin;
|
|||||||
|
|
||||||
use Grav\Common\Config;
|
use Grav\Common\Config;
|
||||||
use Grav\Common\Filesystem\Folder;
|
use Grav\Common\Filesystem\Folder;
|
||||||
use Grav\Common\Registry;
|
use Grav\Common\Grav;
|
||||||
use Grav\Common\Themes;
|
use Grav\Common\Themes;
|
||||||
use Grav\Common\Uri;
|
use Grav\Common\Uri;
|
||||||
use Grav\Common\Data;
|
use Grav\Common\Data;
|
||||||
@@ -11,6 +11,11 @@ use Grav\Common\Page;
|
|||||||
|
|
||||||
class AdminController
|
class AdminController
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var Grav
|
||||||
|
*/
|
||||||
|
public $grav;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
@@ -47,18 +52,20 @@ class AdminController
|
|||||||
protected $redirectCode;
|
protected $redirectCode;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param Grav $grav
|
||||||
* @param string $view
|
* @param string $view
|
||||||
* @param string $task
|
* @param string $task
|
||||||
* @param string $route
|
* @param string $route
|
||||||
* @param array $post
|
* @param array $post
|
||||||
*/
|
*/
|
||||||
public function __construct($view, $task, $route, $post)
|
public function __construct(Grav $grav, $view, $task, $route, $post)
|
||||||
{
|
{
|
||||||
|
$this->grav = $grav;
|
||||||
$this->view = $view;
|
$this->view = $view;
|
||||||
$this->task = $task ? $task : 'display';
|
$this->task = $task ? $task : 'display';
|
||||||
$this->post = $this->getPost($post);
|
$this->post = $this->getPost($post);
|
||||||
$this->route = $route;
|
$this->route = $route;
|
||||||
$this->admin = Registry::get('Admin');
|
$this->admin = $this->grav['admin'];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -97,8 +104,7 @@ class AdminController
|
|||||||
$path = trim(substr($this->redirect, 0, strlen($base)) == $base
|
$path = trim(substr($this->redirect, 0, strlen($base)) == $base
|
||||||
? substr($this->redirect, strlen($base)) : $this->redirect, '/');
|
? substr($this->redirect, strlen($base)) : $this->redirect, '/');
|
||||||
|
|
||||||
$grav = Registry::get('Grav');
|
$this->grav->redirect($base . '/' . preg_replace('|/+|', '/', $path), $this->redirectCode);
|
||||||
$grav->redirect($base . '/' . preg_replace('|/+|', '/', $path), $this->redirectCode);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -170,7 +176,7 @@ class AdminController
|
|||||||
|
|
||||||
// Force configuration reload and save.
|
// Force configuration reload and save.
|
||||||
/** @var Config $config */
|
/** @var Config $config */
|
||||||
$config = Registry::get('Config');
|
$config = $this->grav['config'];
|
||||||
$config->reload()->save();
|
$config->reload()->save();
|
||||||
|
|
||||||
// TODO: find out why reload and save doesn't always update the object itself (and remove this workaround).
|
// TODO: find out why reload and save doesn't always update the object itself (and remove this workaround).
|
||||||
@@ -193,7 +199,7 @@ class AdminController
|
|||||||
// Special handler for pages data.
|
// Special handler for pages data.
|
||||||
if ($this->view == 'pages') {
|
if ($this->view == 'pages') {
|
||||||
/** @var Page\Pages $pages */
|
/** @var Page\Pages $pages */
|
||||||
$pages = Registry::get('Pages');
|
$pages = $this->grav['pages'];
|
||||||
|
|
||||||
// Find new parent page in order to build the path.
|
// Find new parent page in order to build the path.
|
||||||
$route = !isset($data['route']) ? dirname($this->admin->route) : $data['route'];
|
$route = !isset($data['route']) ? dirname($this->admin->route) : $data['route'];
|
||||||
@@ -267,7 +273,7 @@ class AdminController
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
/** @var Page\Pages $pages */
|
/** @var Page\Pages $pages */
|
||||||
$pages = Registry::get('Pages');
|
$pages = $this->grav['pages'];
|
||||||
$data = $this->post;
|
$data = $this->post;
|
||||||
|
|
||||||
// Find new parent page in order to build the path.
|
// Find new parent page in order to build the path.
|
||||||
@@ -336,7 +342,7 @@ class AdminController
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @var Uri $uri */
|
/** @var Uri $uri */
|
||||||
$uri = Registry::get('Uri');
|
$uri = $this->grav['uri'];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$page = $this->admin->page();
|
$page = $this->admin->page();
|
||||||
|
|||||||
Reference in New Issue
Block a user