mirror of
https://gitee.com/ulthon/ulthon_information.git
synced 2026-03-03 16:24:28 +08:00
基本完成新的编辑器;
This commit is contained in:
@@ -15,195 +15,204 @@ use think\Request;
|
||||
|
||||
class Post extends Common
|
||||
{
|
||||
/**
|
||||
* 显示资源列表
|
||||
*
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
//
|
||||
/**
|
||||
* 显示资源列表
|
||||
*
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
//
|
||||
|
||||
$list = ModelPost::with(['categorys.category','tags.tag'])
|
||||
->where('type',$this->request->param('type',1))
|
||||
->order('id desc')
|
||||
->paginate();
|
||||
$list = ModelPost::with(['categorys.category', 'tags.tag'])
|
||||
->where('type', $this->request->param('type', 1))
|
||||
->order('id desc')
|
||||
->paginate();
|
||||
|
||||
View::assign('list', $list);
|
||||
View::assign('list', $list);
|
||||
|
||||
return View::fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示创建资源表单页.
|
||||
*
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
//
|
||||
|
||||
return View::fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存新建的资源
|
||||
*
|
||||
* @param \think\Request $request
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function save(Request $request)
|
||||
{
|
||||
//
|
||||
$post_data = $request->post();
|
||||
|
||||
$post_data['uid'] = uniqid();
|
||||
|
||||
$categorys = [];
|
||||
$tags = [];
|
||||
if (isset($post_data['categorys'])) {
|
||||
$categorys = $post_data['categorys'];
|
||||
unset($post_data['categorys']);
|
||||
}
|
||||
if (isset($post_data['tags'])) {
|
||||
$tags = $post_data['tags'];
|
||||
unset($post_data['tags']);
|
||||
return View::fetch();
|
||||
}
|
||||
|
||||
$model_post = ModelPost::create($post_data);
|
||||
/**
|
||||
* 显示创建资源表单页.
|
||||
*
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
//
|
||||
|
||||
foreach ($categorys as $category) {
|
||||
PostCategory::create([
|
||||
'post_id' => $model_post->id,
|
||||
'category_id' => $category
|
||||
]);
|
||||
}
|
||||
foreach ($tags as $tag) {
|
||||
PostTag::create([
|
||||
'post_id' => $model_post->id,
|
||||
'tag_id' => $tag
|
||||
]);
|
||||
return View::fetch();
|
||||
}
|
||||
|
||||
return $this->success('添加成功',url('index',['type'=>$this->request->param('type')]));
|
||||
}
|
||||
/**
|
||||
* 保存新建的资源
|
||||
*
|
||||
* @param \think\Request $request
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function save(Request $request)
|
||||
{
|
||||
//
|
||||
$post_data = $request->post();
|
||||
|
||||
/**
|
||||
* 显示指定的资源
|
||||
*
|
||||
* @param int $id
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function read($id)
|
||||
{
|
||||
//
|
||||
}
|
||||
$post_data['uid'] = uniqid();
|
||||
|
||||
/**
|
||||
* 显示编辑资源表单页.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
//
|
||||
$categorys = [];
|
||||
$tags = [];
|
||||
if (isset($post_data['categorys'])) {
|
||||
$categorys = $post_data['categorys'];
|
||||
unset($post_data['categorys']);
|
||||
}
|
||||
if (isset($post_data['tags'])) {
|
||||
$tags = $post_data['tags'];
|
||||
unset($post_data['tags']);
|
||||
}
|
||||
|
||||
$model_post = ModelPost::find($id);
|
||||
$model_post = ModelPost::create($post_data);
|
||||
|
||||
foreach ($categorys as $category) {
|
||||
PostCategory::create([
|
||||
'post_id' => $model_post->id,
|
||||
'category_id' => $category
|
||||
]);
|
||||
}
|
||||
foreach ($tags as $tag) {
|
||||
PostTag::create([
|
||||
'post_id' => $model_post->id,
|
||||
'tag_id' => $tag
|
||||
]);
|
||||
}
|
||||
|
||||
View::assign('post', $model_post);
|
||||
|
||||
return View::fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存更新的资源
|
||||
*
|
||||
* @param \think\Request $request
|
||||
* @param int $id
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
//
|
||||
$post_data = $request->post();
|
||||
|
||||
$model_post = ModelPost::find($id);
|
||||
|
||||
$categorys = [];
|
||||
$tags = [];
|
||||
if (isset($post_data['categorys'])) {
|
||||
$categorys = $post_data['categorys'];
|
||||
unset($post_data['categorys']);
|
||||
}
|
||||
if (isset($post_data['tags'])) {
|
||||
$tags = $post_data['tags'];
|
||||
unset($post_data['tags']);
|
||||
return $this->success('添加成功', url('index', ['type' => $this->request->param('type')]));
|
||||
}
|
||||
|
||||
$model_post->save($post_data);
|
||||
|
||||
$old_category_list = PostCategory::where('post_id', $id)->select();
|
||||
$old_category_id_list = array_column((array)$old_category_list, 'id');
|
||||
$old_tag_list = PostTag::where('post_id', $id)->select();
|
||||
$old_tag_id_list = array_column((array)$old_tag_list, 'id');
|
||||
|
||||
// 旧的有新的没有
|
||||
foreach ($old_category_list as $model_category) {
|
||||
if (!in_array($model_category->id, $categorys)) {
|
||||
$model_category->delete();
|
||||
}
|
||||
}
|
||||
foreach ($old_tag_list as $model_tag) {
|
||||
if (!in_array($model_tag->id, $tags)) {
|
||||
$model_tag->delete();
|
||||
}
|
||||
/**
|
||||
* 显示指定的资源
|
||||
*
|
||||
* @param int $id
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function read($id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示编辑资源表单页.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
//
|
||||
|
||||
// 旧的没有新的有
|
||||
foreach ($categorys as $category) {
|
||||
if (!in_array($category, $old_category_id_list)) {
|
||||
$model_post = ModelPost::find($id);
|
||||
|
||||
PostCategory::create([
|
||||
'post_id' => $model_post->id,
|
||||
'category_id' => $category
|
||||
]);
|
||||
}
|
||||
|
||||
View::assign('post', $model_post);
|
||||
|
||||
return View::fetch();
|
||||
}
|
||||
|
||||
foreach ($tags as $tag) {
|
||||
if (!in_array($tag, $old_tag_id_list)) {
|
||||
public function editContent($id)
|
||||
{
|
||||
$model_post = ModelPost::find($id);
|
||||
|
||||
PostTag::create([
|
||||
'post_id' => $model_post->id,
|
||||
'tag_id' => $tag
|
||||
]);
|
||||
}
|
||||
|
||||
View::assign('post', $model_post);
|
||||
return View::fetch();
|
||||
}
|
||||
|
||||
return $this->success('保存成功', url('index',['type'=>$model_post->getData('type')]));
|
||||
}
|
||||
/**
|
||||
* 保存更新的资源
|
||||
*
|
||||
* @param \think\Request $request
|
||||
* @param int $id
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
//
|
||||
$post_data = $request->post();
|
||||
|
||||
/**
|
||||
* 删除指定资源
|
||||
*
|
||||
* @param int $id
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
//
|
||||
$model_post = ModelPost::find($id);
|
||||
|
||||
$model_post = ModelPost::find($id);
|
||||
$categorys = [];
|
||||
$tags = [];
|
||||
if (isset($post_data['categorys'])) {
|
||||
$categorys = $post_data['categorys'];
|
||||
unset($post_data['categorys']);
|
||||
}
|
||||
if (isset($post_data['tags'])) {
|
||||
$tags = $post_data['tags'];
|
||||
unset($post_data['tags']);
|
||||
}
|
||||
|
||||
$model_post->delete();
|
||||
$model_post->save($post_data);
|
||||
|
||||
PostCategory::where('post_id',$id)->delete();
|
||||
$old_category_list = PostCategory::where('post_id', $id)->select();
|
||||
$old_category_id_list = array_column((array)$old_category_list, 'id');
|
||||
$old_tag_list = PostTag::where('post_id', $id)->select();
|
||||
$old_tag_id_list = array_column((array)$old_tag_list, 'id');
|
||||
|
||||
PostTag::where('post_id',$id)->delete();
|
||||
// 旧的有新的没有
|
||||
foreach ($old_category_list as $model_category) {
|
||||
if (!in_array($model_category->id, $categorys)) {
|
||||
$model_category->delete();
|
||||
}
|
||||
}
|
||||
foreach ($old_tag_list as $model_tag) {
|
||||
if (!in_array($model_tag->id, $tags)) {
|
||||
$model_tag->delete();
|
||||
}
|
||||
}
|
||||
|
||||
return json_message();
|
||||
}
|
||||
|
||||
// 旧的没有新的有
|
||||
foreach ($categorys as $category) {
|
||||
if (!in_array($category, $old_category_id_list)) {
|
||||
|
||||
PostCategory::create([
|
||||
'post_id' => $model_post->id,
|
||||
'category_id' => $category
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($tags as $tag) {
|
||||
if (!in_array($tag, $old_tag_id_list)) {
|
||||
|
||||
PostTag::create([
|
||||
'post_id' => $model_post->id,
|
||||
'tag_id' => $tag
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->success('保存成功', url('index', ['type' => $model_post->getData('type')]));
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除指定资源
|
||||
*
|
||||
* @param int $id
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
//
|
||||
|
||||
$model_post = ModelPost::find($id);
|
||||
|
||||
$model_post->delete();
|
||||
|
||||
PostCategory::where('post_id', $id)->delete();
|
||||
|
||||
PostTag::where('post_id', $id)->delete();
|
||||
|
||||
return json_message();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user