mirror of
https://github.com/fruithost/Panel.git
synced 2026-12-01 12:55:04 +09:00
Adding Ajax API, fix some module handlings, add new load method for modules
This commit is contained in:
1 parent
ecc278de48
commit
6723246bf2
7 files changed
+109
-30
No files matched your search
@@ -138,6 +138,10 @@
|
||||
return;
|
||||
}
|
||||
|
||||
if(method_exists($module->getInstance(), 'load')) {
|
||||
$module->getInstance()->load();
|
||||
}
|
||||
|
||||
if(!method_exists($module->getInstance(), 'content')) {
|
||||
$this->template->display('error/module_empty', [
|
||||
'module' => $module,
|
||||
@@ -169,6 +173,39 @@
|
||||
|
||||
$this->template->display('login');
|
||||
});
|
||||
|
||||
$this->router->addRoute('/ajax', function() {
|
||||
if(!Auth::isLoggedIn()) {
|
||||
header('HTTP/1.1 403 Forbidden');
|
||||
require_once(dirname(PATH) . '/placeholder/errors/403.php');
|
||||
exit();
|
||||
}
|
||||
|
||||
if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest') {
|
||||
header('HTTP/1.1 405 Method Not Allowed');
|
||||
require_once(dirname(PATH) . '/placeholder/errors/405.php');
|
||||
exit();
|
||||
}
|
||||
|
||||
// Fire modals
|
||||
if(isset($_POST['modal']) && !empty($_POST['modal'])) {
|
||||
$modals = $this->getHooks()->applyFilter('modals', []);
|
||||
|
||||
foreach($modals AS $modal) {
|
||||
if($modal->getName() === $_POST['modal']) {
|
||||
$callback = $modal->getCallback('save');
|
||||
$result = call_user_func_array($callback, [ $_POST ]);
|
||||
|
||||
if(is_bool($result)) {
|
||||
print json_encode($result);
|
||||
return;
|
||||
}
|
||||
|
||||
print $result;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
$this->router->run();
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
private $title = null;
|
||||
private $content = null;
|
||||
private $buttons = [];
|
||||
private $callbacks = [];
|
||||
|
||||
public function __construct($name, $title, $content) {
|
||||
$this->name = $name;
|
||||
@@ -16,6 +17,20 @@
|
||||
}
|
||||
}
|
||||
|
||||
public function getCallback($name) {
|
||||
if(!isset($this->callbacks[$name])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->callbacks[$name];
|
||||
}
|
||||
|
||||
public function onSave($method) {
|
||||
$this->callbacks['save'] = $method;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getName() {
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
private $path = null;
|
||||
private $info = null;
|
||||
private $instance = null;
|
||||
private $enabled = false;
|
||||
|
||||
public function __construct($path) {
|
||||
$this->path = $path;
|
||||
@@ -23,6 +24,14 @@
|
||||
return $this->instance;
|
||||
}
|
||||
|
||||
public function isEnabled() {
|
||||
return $this->enabled;
|
||||
}
|
||||
|
||||
public function setEnabled($state) {
|
||||
$this->enabled = $state;
|
||||
}
|
||||
|
||||
public function init($core) {
|
||||
$script = sprintf('%s%smodule.php', $this->path, DS);
|
||||
|
||||
|
||||
@@ -14,6 +14,10 @@
|
||||
/* Override Me */
|
||||
}
|
||||
|
||||
public function load() {
|
||||
/* Override Me */
|
||||
}
|
||||
|
||||
public function getCore() {
|
||||
return $this->core;
|
||||
}
|
||||
|
||||
@@ -7,6 +7,11 @@
|
||||
|
||||
public function __construct($core) {
|
||||
$this->core = $core;
|
||||
$enabled = [];
|
||||
|
||||
foreach(Database::fetch('SELECT `name` FROM `fh_modules` WHERE `state`=\'ENABLED\'') AS $entry) {
|
||||
$enabled[] = $entry->name;
|
||||
}
|
||||
|
||||
foreach(new \DirectoryIterator($this->getPath()) AS $info) {
|
||||
if($info->isDot()) {
|
||||
@@ -14,8 +19,10 @@
|
||||
}
|
||||
|
||||
$path = sprintf('%s%s%s', $this->getPath(), DS, $info->getFilename());
|
||||
$module = new Module($path);
|
||||
$module->setEnabled(in_array(basename($path), $enabled));
|
||||
|
||||
$this->addModule(basename($path), new Module($path));
|
||||
$this->addModule(basename($path), $module);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +31,10 @@
|
||||
}
|
||||
|
||||
public function addModule($name, $module) {
|
||||
if(!$module->isEnabled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->modules[$name] = $module->init($this->core);
|
||||
}
|
||||
|
||||
|
||||
@@ -18,8 +18,8 @@
|
||||
|
||||
$this->core->getHooks()->addAction('html_head', [ $this, 'head_robots' ]);
|
||||
$this->core->getHooks()->addAction('html_head', [ $this, 'head_scripts' ]);
|
||||
$this->core->getHooks()->addAction('html_foot', [ $this, 'foot_scripts' ]);
|
||||
$this->core->getHooks()->addAction('html_foot', [ $this, 'foot_modals' ]);
|
||||
$this->core->getHooks()->addAction('html_foot', [ $this, 'foot_scripts' ]);
|
||||
|
||||
$this->files->addStylesheet('bootstrap', $this->url('css/bootstrap/bootstrap.min.css'), '4.3.1');
|
||||
$this->files->addStylesheet('jquery-ui', $this->url('css/jquery-ui/jquery-ui.css'), '1.12.1');
|
||||
|
||||
@@ -63,40 +63,43 @@
|
||||
foreach($modals AS $modal) {
|
||||
?>
|
||||
<div class="modal fade" id="<?php print $modal->getName(); ?>" tabindex="-1" role="dialog" aria-labelledby="<?php print $modal->getName(); ?>" aria-hidden="true">
|
||||
<div class="modal-dialog" role="document">
|
||||
<div class="modal-dialog modal-dialog-centered" role="document">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title"><?php print $modal->getTitle(); ?></h5>
|
||||
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
|
||||
<span aria-hidden="true">×</span>
|
||||
</button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<?php print $modal->getContent($template); ?>
|
||||
</div>
|
||||
<div class="modal-footer text-center">
|
||||
<?php
|
||||
$buttons = $modal->getButtons();
|
||||
|
||||
foreach($buttons AS $button) {
|
||||
if(is_array($button)) {
|
||||
foreach($button AS $entry) {
|
||||
<form method="post" class="ajax" action="<?php print $this->url('/ajax'); ?>">
|
||||
<input type="hidden" name="modal" value="<?php print $modal->getName(); ?>" />
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title"><?php print $modal->getTitle(); ?></h5>
|
||||
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
|
||||
<span aria-hidden="true">×</span>
|
||||
</button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<?php print $modal->getContent($template); ?>
|
||||
</div>
|
||||
<div class="modal-footer text-center">
|
||||
<?php
|
||||
$buttons = $modal->getButtons();
|
||||
|
||||
foreach($buttons AS $button) {
|
||||
if(is_array($button)) {
|
||||
foreach($button AS $entry) {
|
||||
if($entry->isDismissable()) {
|
||||
printf('<button type="button" name="%1$s" data-dismiss="modal" class="btn %3$s">%2$s</button>', $entry->getName(), $entry->getLabel(), $entry->getClasses(false));
|
||||
} else {
|
||||
printf('<button type="submit" name="%1$s" class="btn %3$s">%2$s</button>', $entry->getName(), $entry->getLabel(), $entry->getClasses(false));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if($entry->isDismissable()) {
|
||||
printf('<button type="button" name="%1$s" data-dismiss="modal" class="btn %3$s">%2$s</button>', $entry->getName(), $entry->getLabel(), $entry->getClasses(false));
|
||||
printf('<button type="button" name="%1$s" data-dismiss="modal" class="btn %3$s">%2$s</button>', $button->getName(), $button->getLabel(), $button->getClasses(false));
|
||||
} else {
|
||||
printf('<button type="button" name="%1$s" class="btn %3$s">%2$s</button>', $entry->getName(), $entry->getLabel(), $entry->getClasses(false));
|
||||
printf('<button type="submit" name="%1$s" class="btn %3$s">%2$s</button>', $button->getName(), $button->getLabel(), $button->getClasses(false));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if($entry->isDismissable()) {
|
||||
printf('<button type="button" name="%1$s" data-dismiss="modal" class="btn %3$s">%2$s</button>', $button->getName(), $button->getLabel(), $button->getClasses(false));
|
||||
} else {
|
||||
printf('<button type="button" name="%1$s" class="btn %3$s">%2$s</button>', $button->getName(), $button->getLabel(), $button->getClasses(false));
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
?>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user