catchAdmin/catch/cms/model/events/ArticlesEvent.php

132 lines
3.2 KiB
PHP
Raw Normal View History

2021-05-22 11:02:45 +08:00
<?php
namespace catchAdmin\cms\model\events;
use catchAdmin\cms\model\Articles;
2021-05-23 20:35:21 +08:00
use catchAdmin\cms\model\BaseModel;
2021-05-22 11:02:45 +08:00
use catchAdmin\cms\model\Tags;
use catcher\exceptions\FailedException;
use catcher\Utils;
trait ArticlesEvent
{
/**
* 插入前
*
* @time 2021年05月21日
* @param Articles $model
* @return void
*/
public static function onBeforeInsert(Articles $model)
{
2021-05-23 20:35:21 +08:00
self::beforeChangeData($model);
2021-05-22 11:02:45 +08:00
}
/**
* 插入后
*
* @time 2021年05月21日
* @param Articles $model
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return void
*/
public static function onAfterInsert(Articles $model)
{
2021-05-23 20:35:21 +08:00
$model->attachTags(self::getTagsId($model));
}
/**
*
* @auth CatchAdmin
* @time 2021年05月22日
* @param Articles $model
* @return void
*/
public static function onBeforeUpdate(Articles $model)
{
self::beforeChangeData($model);
}
/**
*
* @auth CatchAdmin
* @time 2021年05月22日
* @param Articles $model
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return void
*/
public static function onAfterUpdate(Articles $model)
{
$data = $model->getData();
if (isset($data['tags'])) {
$tagIds = self::getTagsId($model);
2021-05-22 11:02:45 +08:00
2021-05-23 20:35:21 +08:00
$article = $model->where($model->getWhere())->find();
2021-05-22 11:02:45 +08:00
2021-05-23 20:35:21 +08:00
$article->detachTags($article->tag()->select()->column('id'));
$article->attachTags(self::getTagsId($model));
}
}
/**
* 插入前
*
* @auth CatchAdmin
* @time 2021年05月22日
* @param $model
* @return void
*/
protected static function beforeChangeData($model)
{
$data = $model->getData();
if (isset($data['category_id'])) {
$model->category_id = $model->category_id[count($model->category_id) - 1];
}
if (isset($data['images'])) {
$model->images = empty($model->images) ? '' : implode(',', $model->images);
}
if (isset($data['tags'])) {
$model->tags = empty($model->tags) ? '' : implode(',', $model->tags);
}
}
/**
* 插入后
*
* @auth CatchAdmin
* @time 2021年05月22日
* @param $model
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return array
*/
protected static function getTagsId($model): array
{
$tags = Utils::stringToArrayBy($model->tags);
$tagIds = [];
2021-05-22 11:02:45 +08:00
foreach ($tags as $tag) {
2021-05-23 20:35:21 +08:00
$tagModel = Tags::where('name', $tag)->findOrEmpty();
if ($tagModel->isEmpty()) {
$tagIds[] = $tagModel->storeBy([
2021-05-22 11:02:45 +08:00
'name' => $tag
]);
}
}
2021-05-23 20:35:21 +08:00
return array_merge(Tags::whereIn('name', $tags)->column('id'), $tagIds);
2021-05-22 11:02:45 +08:00
}
}