## 批量新增栏目
View: 1. 在index.html / add.html / edit.html 的顶部添加[批量添加分类]的操作按钮
> \themes\admin_simpleboot3\portal\admin_category\index.html
> \themes\admin_simpleboot3\portal\admin_category\add.html
> \themes\admin_simpleboot3\portal\admin_category\edit.html
<ul class="nav nav-tabs">
<li class="active"><a href="{:url('AdminCategory/index')}">分类管理</a></li>
<li><a href="{:url('AdminCategory/add')}">添加分类</a></li>
<li><a href="{:url('AdminCategory/addMore')}">批量添加分类</a></li>
</ul>
2. 当前路径新增模板页 add_more.html 主要是把input改成 textarea ; 去掉别名和图片(批量新增不好处理);表单提交到addPostMore 处理
<include file="public@header"/>
</head>
<body>
<div class="wrap js-check-wrap">
<ul class="nav nav-tabs">
<li><a href="{:url('AdminCategory/index')}">分类管理</a></li>
<li><a href="{:url('AdminCategory/add')}">添加分类</a></li>
<li class="active"><a href="{:url('AdminCategory/addMore')}">批量添加分类</a></li>
</ul>
<div class="row margin-top-20">
<div class="col-md-2">
<div class="list-group">
<a class="list-group-item" href="#A" data-toggle="tab">基本属性</a>
<a class="list-group-item" href="#B" data-toggle="tab">SEO设置</a>
<a class="list-group-item" href="#C" data-toggle="tab">模板设置</a>
</div>
</div>
<div class="col-md-6">
<form class="js-ajax-form" action="{:url('AdminCategory/addPostMore')}" method="post">
<div class="tab-content">
<div class="tab-pane active" id="A">
<div class="form-group">
<label for="input-parent"><span class="form-required">*</span>上级</label>
<div>
<select class="form-control" name="parent_id" id="input-parent">
<option value="0">作为一级分类</option>
{$categories_tree}
</select>
</div>
</div>
<div class="form-group">
<label for="input-name"><span class="form-required">*</span>分类名称</label>
<div>
<textarea name="name" class="form-control" id="input-name" rows="10"></textarea>
[一个栏目一行]
</div>
</div>
</div>
<div class="tab-pane" id="B">
<div class="form-group">
<label for="input-seo_title">SEO标题</label>
<div>
<input type="text" class="form-control" id="input-seo_title" name="seo_title">
</div>
</div>
<div class="form-group">
<label for="input-seo_keywords">SEO关键字</label>
<div>
<input type="text" class="form-control" id="input-seo_keywords" name="seo_keywords">
</div>
</div>
<div class="form-group">
<label for="input-seo_description">SEO描述</label>
<div>
<textarea class="form-control" name="seo_description"
id="input-seo_description"></textarea>
</div>
</div>
</div>
<div class="tab-pane" id="C">
<div class="form-group">
<label for="input-list_tpl"><span class="form-required">*</span>列表模板</label>
<div>
<select class="form-control" name="list_tpl" id="input-list_tpl">
<!--<option value="list">portal/list.html(默认)</option>-->
<foreach name="list_theme_files" item="vo">
<php>$value=preg_replace('/^portal\//','',$vo['file']);</php>
<option value="{$value}">{$vo.name} {$vo.file}.html</option>
</foreach>
</select>
</div>
</div>
<div class="form-group">
<label for="input-one_tpl"><span class="form-required">*</span>文章模板</label>
<div>
<select class="form-control" name="one_tpl" id="input-one_tpl">
<!--<option value="article">portal/article.html(默认)</option>-->
<foreach name="article_theme_files" item="vo">
<php>$value=preg_replace('/^portal\//','',$vo['file']);</php>
<option value="{$value}">{$vo.name} {$vo.file}.html</option>
</foreach>
</select>
</div>
</div>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary js-ajax-submit">{:lang('ADD')}</button>
<a class="btn btn-default" href="{:url('AdminCategory/index')}">{:lang('BACK')}</a>
</div>
</form>
</div>
</div>
</div>
<script type="text/javascript" src="__STATIC__/js/admin.js"></script>
</body>
</html>
Controller:实现批量添加分类方法 addPostMore
> \app\portal\controller\AdminCategoryController.php
public function addPostMore()
{
$portalCategoryModel = new PortalCategoryModel();
$data = $this->request->param();
$result = $this->validate($data, 'PortalCategory');
if ($result !== true) {
$this->error($result);
}
//把多个栏目名称拆分成数组
$cname = trim($data['name']);
$cname_arr = explode("\r\n", $cname);
$datas = [];
foreach ($cname_arr as $key => $value) {
$datas[$key]['parent_id'] = $data['parent_id'];
$datas[$key]['name'] = $value;
$datas[$key]['list_tpl'] =$data['list_tpl'];
$datas[$key]['one_tpl'] = $data['one_tpl'];
$datas[$key]['more']['thumbnail'] = '';
}
//批量新增数据,并返回新增数据结果集(toArray()把对象转换成数组)
$insertids[] = $portalCategoryModel->allowField(true)->saveAll($datas)->toArray();
$ids=[];
foreach ($insertids[0] as $key => $value) {
//这里判断 parent_id 是否为0
if( $insertids[0][$key]['parent_id'] == '0' ){
$ids[$key]['id'] = $insertids[0][$key]['id'];
$ids[$key]['path'] = '0-' . $insertids[0][$key]['id'];
}else{
// 根据parent_id查询当前数据的path
$parentPath = $portalCategoryModel->where('id', intval($insertids[0][$key]['parent_id']))->value('path');
$ids[$key]['id'] = $insertids[0][$key]['id'];
$ids[$key]['path'] = $parentPath."-".$insertids[0][$key]['id'];
}
}
//批量更新数据
$portalCategoryModel->allowField(true)->saveAll($ids);
$this->success('添加成功!', url('AdminCategory/index'));
}