作者 Karson

新增插件卸载时删除相关数据表功能

... ... @@ -8,6 +8,7 @@ use think\addons\AddonException;
use think\addons\Service;
use think\Cache;
use think\Config;
use think\Db;
use think\Exception;
/**
... ... @@ -19,6 +20,7 @@ use think\Exception;
class Addon extends Backend
{
protected $model = null;
protected $noNeedRight = ['get_table_list'];
public function _initialize()
{
... ... @@ -144,14 +146,26 @@ class Addon extends Backend
{
$name = $this->request->post("name");
$force = (int)$this->request->post("force");
$droptables = (int)$this->request->post("droptables");
if (!$name) {
$this->error(__('Parameter %s can not be empty', 'name'));
}
if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
$this->error(__('Addon name incorrect'));
}
//只有开启调试且为超级管理员才允许删除相关数据库
$tables = [];
if ($droptables && Config::get("app_debug") && $this->auth->isSuperAdmin()) {
$tables = get_addon_tables($name);
}
try {
Service::uninstall($name, $force);
if ($tables) {
//删除插件关联表
foreach ($tables as $index => $table) {
Db::execute("DROP TABLE IF EXISTS `{$table}`");
}
}
$this->success(__('Uninstall successful'));
} catch (AddonException $e) {
$this->result($e->getData(), $e->getCode(), __($e->getMessage()));
... ... @@ -367,4 +381,14 @@ class Addon extends Backend
$callback = $this->request->get('callback') ? "jsonp" : "json";
return $callback($result);
}
/**
* 获取插件相关表
*/
public function get_table_list()
{
$name = $this->request->post("name");
$tables = get_addon_tables($name);
$this->success('', null, ['tables' => $tables]);
}
}
... ...
... ... @@ -90,4 +90,10 @@ return [
'Addon already exists' => '上传的插件已经存在',
'Unable to open the zip file' => '无法打开ZIP文件',
'Unable to extract the file' => '无法解压ZIP文件',
'Are you sure you want to unstall %s?' => '确认卸载插件《%s》?',
'Delete all the addon file and cannot be recovered!' => '卸载将会删除所有插件文件且不可找回!!!',
'Delete all the addon database and cannot be recovered!' => '删除所有插件相关数据表且不可找回!!!',
'Please backup important data manually before uninstall!' => '如有重要数据请备份后再操作!!!',
'The following data tables will be deleted' => '以下插件数据表将会被删除',
'The Addon did not create a data table' => '插件未创建任何数据表',
];
... ...
... ... @@ -53,11 +53,13 @@
position: absolute;
box-shadow: 0px 0px 2px #f11414;
}
.form-userinfo .breadcrumb {
margin-bottom:10px;
margin-bottom: 10px;
}
.btn-toggle {
padding:0;
padding: 0;
}
</style>
<div class="panel panel-default panel-intro">
... ... @@ -205,6 +207,17 @@
<%}%>
</div>
</script>
<script id="uninstalltpl" type="text/html">
<div class="">
<div class=""><%=__("Are you sure you want to unstall %s?", addon['title'])%>
<p class="text-danger">{:__('Delete all the addon file and cannot be recovered!')} </p>
{if config('app_debug')}
<p class="text-danger"><input type="checkbox" name="droptables" id="droptables" data-name="<%=addon['name']%>"/> {:__('Delete all the addon database and cannot be recovered!')} </p>
{/if}
<p class="text-danger">{:__('Please backup important data manually before uninstall!')}</p>
</div>
</div>
</script>
<script id="conflicttpl" type="text/html">
<div class="alert alert-dismissable alert-danger">
<button type="button" class="close" data-dismiss="alert">×</button>
... ...
... ... @@ -196,6 +196,26 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function
table.bootstrapTable('refresh', {url: $(this).data("url"), pageNumber: 1});
return false;
});
var tables = [];
$(document).on("click", "#droptables", function () {
if ($(this).prop("checked")) {
Fast.api.ajax({
url: "addon/get_table_list",
async: false,
data: {name: $(this).data("name")}
}, function (data) {
tables = data.tables;
return false;
});
var html;
html = tables.length > 0 ? '<div class="alert alert-warning-light droptablestips" style="max-width:480px;max-height:300px;overflow-y: auto;">' + __('The following data tables will be deleted') + ':<br>' + tables.join("<br>") + '</div>'
: '<div class="alert alert-warning-light droptablestips">' + __('The Addon did not create a data table') + '</div>';
$(html).insertAfter($(this).closest("p"));
} else {
$(".droptablestips").remove();
}
$(window).resize();
});
// 会员信息
$(document).on("click", ".btn-userinfo", function () {
... ... @@ -350,10 +370,10 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function
});
};
var uninstall = function (name, force) {
var uninstall = function (name, force, droptables) {
Fast.api.ajax({
url: 'addon/uninstall',
data: {name: name, force: force ? 1 : 0}
data: {name: name, force: force ? 1 : 0, droptables: droptables ? 1 : 0}
}, function (data, ret) {
delete Config['addons'][name];
Layer.closeAll();
... ... @@ -372,7 +392,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function
},
yes: function () {
uninstall(name, true);
uninstall(name, true, droptables);
}
});
... ... @@ -466,8 +486,9 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function
Layer.alert(__('Please disable addon first'), {icon: 7});
return false;
}
Layer.confirm(__('Uninstall tips', Config['addons'][name].title), function () {
uninstall(name, false);
Template.helper("__", __);
Layer.confirm(Template("uninstalltpl", {addon: Config['addons'][name]}), function (index, layero) {
uninstall(name, false, $("input[name='droptables']", layero).prop("checked"));
});
});
... ...