## 批量復制文章(用我的方式來完成之前框架未做好功能)
由于經常新增后臺門戶功能,所以看到了之前沒有做好的功能之一:批量復制文章,個人覺得這個功能很好,所以決定要實現它!
View: 搜索 '批量復制' 發現代碼是加了注釋的,先把注釋去掉:
> \themes\admin_simpleboot3\portal\admin_article\index.html
<button class="btn btn-primary btn-sm js-articles-copy" type="button">批量復制</button>
在頁面底部發現批量復制的腳本,思路是先勾選文章,得到IDS,然后傳到copy頁面實現(彈窗);
在280行左右,5.1版的默認發送的URL失效了,這里重新組成URL,順利實現彈窗功能,同時增加關閉事件,關閉后刷新表格。
ids = ids.join(',');
url = '{:url("AdminArticle/copy")}?ids='+ ids ;
art.dialog.open(url, {
//art.dialog.open("__ROOT__/index.php?g=portal&m=AdminArticle&a=copy&ids=" + ids, {
title: "批量復制",
width: "400px",
close: function(){
reloadPage(window);
}
});
> \themes\admin_simpleboot3\portal\admin_article\copy.html
copy.html頁面:發現這個頁面也是沒有完善好的,估計之前的作者(小夏)沒時間弄,我把完成的代碼貼出來吧。頁面主要調用控制器傳過來的IDS以及選擇的欄目樹,有這兩個條件就可以實現復制功能了。然后form把數據提交到copySave處理提交數據。
<include file="public@header"/>
</head>
<body>
<div class="wrap">
<form method="post" class="js-ajax-form" action="{:url('AdminArticle/copySave')}">
<div style="width: 350px">
<label style="width: 350px">分類:</label>
<select name="term_id" class="form-control" style="width: 280px;float: left;">
{$terms_tree}
</select>
<input type="hidden" name="ids" value="{$ids}">
<button class="btn btn-primary js-ajax-submit" type="submit" style="float: left;">復制</button>
</div>
</form>
</div>
<script src="__STATIC__/js/admin.js"></script>
</body>
</html>
Controller: 完善copy()方法(這里沒有用鉤子來實現功能),以及新增的copySave()方法。
> \app\portal\controller\AdminArticleController.php
public function copy()
{
$param = $this->request->param();
$categoryId = $this->request->param('category', 0, 'intval');
//獲取欄目
$portalCategoryModel = new PortalCategoryModel();
$categoryTree = $portalCategoryModel->adminCategoryTree($categoryId);
$this->assign('terms_tree', $categoryTree);
//獲取IDS
$ids = $this->request->param('ids');
$this->assign('ids', $ids);
return $this->fetch();
}
/**
* 復制數據
* @return [json] [返回狀態]
*/
public function copySave()
{
$param = $this->request->param();
$term_id = $this->request->param('term_id');
$ids = $this->request->param('ids');
//獲取數據集
$portalPostModel = new PortalPostModel();
$info =$portalPostModel->where('id','in',$ids)->select()->toArray();
//處理數組去主鍵
以及 發布時間(系統設定新增默認是未發布,所以沒有發布時間)
foreach ($info as &$arr) {
unset($arr['id']);
unset($arr['published_time']);
}
//1,批量插入數據
$arrlist = $portalPostModel->allowField(true)->saveAll($info)->toArray();
//獲取自增ID
foreach ($arrlist as &$arr) {
$newids[]= $arr['id'];
}
//2,更新發布狀態
$portalPostModel->where('id', 'in', $newids)->update(['post_status' => 1,'update_time'=>time(),'published_time'=>time()]);
//3,向關系表新增相關數據
foreach ($arrlist as $k => $v) {
$sz[$k]['category_id'] = $term_id;
$sz[$k]['post_id'] = $v['id'];
}
Db::name('portalCategoryPost')->insertAll($sz);
$this->success("復制成功!", '');
}
這里copySave()沒有通過模型的關系來更新文章目錄關系表。
最后一部操作:復制文章成功后,處理返回的是 admin.js 的 callback
> \static\js\admin.js
220行左右的 callback 增加 art.dialog.close() 關閉方法,完成一系列操作過程!寫的比較菜,望指點!
callback: {
afterClose: function () {
Wind.use('artDialog', 'iframeTools', function () {
art.dialog.close();
});
if ($btn.data('refresh') == undefined || $btn.data('refresh')) {
if ($btn.data('success_refresh')) {
var successRefreshCallback = $btn.data('success_refresh');
window[successRefreshCallback](data, statusText, xhr, $form);
return;
} else {
_refresh();
}
}
}
}