cms first commit
This commit is contained in:
58
catch/cms/model/events/ArticlesEvent.php
Normal file
58
catch/cms/model/events/ArticlesEvent.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
namespace catchAdmin\cms\model\events;
|
||||
|
||||
use catchAdmin\cms\model\Articles;
|
||||
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)
|
||||
{
|
||||
$model->category_id = $model->category_id[0];
|
||||
|
||||
$model->images = implode(',', $model->images);
|
||||
|
||||
$model->tags = implode(',', $model->tags);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 插入后
|
||||
*
|
||||
* @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)
|
||||
{
|
||||
$tags = Utils::stringToArrayBy($model->tags);
|
||||
|
||||
$tagModel = new Tags;
|
||||
$existTags = $tagModel->whereIn('name', $tags)->select()->toArray();
|
||||
$existTagsName = array_column($existTags, 'name');
|
||||
|
||||
$tagsIds = [];
|
||||
foreach ($tags as $tag) {
|
||||
if (! in_array($tag, $existTagsName)) {
|
||||
$tagsIds[] = $tagModel->createBy([
|
||||
'name' => $tag
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$model->tags()->attach(array_merge(array_column($existTags, 'id'), $tagsIds));
|
||||
}
|
||||
}
|
83
catch/cms/model/events/CategoryEvent.php
Normal file
83
catch/cms/model/events/CategoryEvent.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | Catch-CMS Design On 2020
|
||||
// +----------------------------------------------------------------------
|
||||
// | CatchAdmin [Just Like ~ ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: JaguarJack [ njphper@gmail.com ]
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace catchAdmin\cms\model\events;
|
||||
|
||||
use catcher\exceptions\FailedException;
|
||||
use catcher\Utils;
|
||||
|
||||
trait CategoryEvent
|
||||
{
|
||||
/**
|
||||
* 插入前
|
||||
*
|
||||
* @time 2021年03月03日
|
||||
* @param \think\Model $category
|
||||
* @return void
|
||||
*/
|
||||
public static function onBeforeInsert($category): void
|
||||
{
|
||||
$category->data(self::changeData($category->getData()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新前
|
||||
*
|
||||
* @time 2021年03月03日
|
||||
* @param \think\Model $category
|
||||
* @return mixed|void
|
||||
*/
|
||||
public static function onBeforeUpdate($category)
|
||||
{
|
||||
// $category->data(self::changeData($category->getData()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除前
|
||||
*
|
||||
* @time 2021年03月03日
|
||||
* @param $category
|
||||
* @return void
|
||||
*/
|
||||
public static function onBeforeDelete($category)
|
||||
{
|
||||
if ($category->hasNextLevel()) {
|
||||
throw new FailedException('存在下级栏目, 无法删除');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新Data
|
||||
*
|
||||
* @time 2021年03月03日
|
||||
* @param $data
|
||||
* @return mixed
|
||||
*/
|
||||
protected static function changeData($data)
|
||||
{
|
||||
/**
|
||||
if (isset($data['parent_id'])) {
|
||||
$parentId = $data['parent_id'];
|
||||
|
||||
if (!is_array($parentId)) {
|
||||
// $parentId = Utils::dealWithFormArrayString($parentId);
|
||||
}
|
||||
|
||||
$parentId = count($parentId) ? array_pop($parentId) : 0;
|
||||
|
||||
$data['parent_id'] = $parentId;
|
||||
}
|
||||
|
||||
return $data;*/
|
||||
}
|
||||
}
|
167
catch/cms/model/events/ModelFieldsEvent.php
Normal file
167
catch/cms/model/events/ModelFieldsEvent.php
Normal file
@@ -0,0 +1,167 @@
|
||||
<?php
|
||||
namespace catchAdmin\cms\model\events;
|
||||
|
||||
use catchAdmin\cms\exceptions\ColumnException;
|
||||
use catchAdmin\cms\model\Models;
|
||||
use catchAdmin\cms\support\Table;
|
||||
use catchAdmin\cms\support\TableColumn;
|
||||
use catchAdmin\cms\tables\Model;
|
||||
use catcher\exceptions\FailedException;
|
||||
|
||||
trait ModelFieldsEvent
|
||||
{
|
||||
protected static $modelTableName = null;
|
||||
|
||||
/**
|
||||
* 插入前
|
||||
*
|
||||
* @time 2021年03月08日
|
||||
* @param $modelFields
|
||||
* @return void
|
||||
*/
|
||||
public static function onBeforeInsert($modelFields)
|
||||
{
|
||||
$tableName = self::getModelTableName($modelFields->getData('model_id'));
|
||||
|
||||
if ($tableName && Table::hasColumn($tableName, $modelFields->getData('name'))) {
|
||||
throw new ColumnException(sprintf('Column [%s] already exist in Table [%s]', $modelFields->getData('name'), $tableName));
|
||||
}
|
||||
|
||||
$modelFields->data(self::changeData($modelFields->getData()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新后
|
||||
*
|
||||
* @time 2021年03月08日
|
||||
* @param $modelFields
|
||||
* @return void
|
||||
*/
|
||||
public static function onBeforeUpdate($modelFields)
|
||||
{
|
||||
$modelFields->data(self::changeData($modelFields->getData(), true));
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入之后
|
||||
*
|
||||
* @time 2021年03月08日
|
||||
* @param $modelFields
|
||||
* @return void
|
||||
*/
|
||||
public static function onAfterInsert($modelFields)
|
||||
{
|
||||
if ($modelFields->getKey()) {
|
||||
try {
|
||||
$tableName = self::getModelTableName($modelFields->getData('model_id'));
|
||||
|
||||
if ($tableName) {
|
||||
Table::addColumn($tableName, (new TableColumn($modelFields->getData()))->get());
|
||||
}
|
||||
|
||||
self::addIndexForField($tableName, $modelFields->getData('name'), $modelFields->getData('is_index'));
|
||||
} catch (\Exception $e) {
|
||||
$modelFields->delete();
|
||||
throw new FailedException($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新之后
|
||||
*
|
||||
* @time 2021年04月30日
|
||||
* @param \think\Model $modelFields
|
||||
* @return void
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
*/
|
||||
public static function onAfterUpdate(\think\Model $modelFields)
|
||||
{
|
||||
$field = $modelFields->find($modelFields->getWhere());
|
||||
|
||||
self::addIndexForField(self::getModelTableName($field['model_id']), $field['name'], $field['is_index']);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 添加索引
|
||||
*
|
||||
* @time 2021年04月30日
|
||||
* @param $table
|
||||
* @param string $column
|
||||
* @param $isIndex
|
||||
* @return void
|
||||
*/
|
||||
protected static function addIndexForField($table, string $column, $isIndex)
|
||||
{
|
||||
if (! Table::isIndex($table, $column) && $isIndex == self::IS_INDEX) {
|
||||
Table::addIndex($table, $column);
|
||||
}
|
||||
|
||||
if (Table::isIndex($table, $column) && $isIndex == self::NOT_INDEX) {
|
||||
Table::dropIndex($table, $column);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除字段
|
||||
*
|
||||
* @time 2021年03月08日
|
||||
* @param $modelFields
|
||||
* @return void
|
||||
*/
|
||||
public static function onAfterDelete($modelFields)
|
||||
{
|
||||
$tableName = self::getModelTableName($modelFields->getData('model_id'));
|
||||
|
||||
$columnName = $modelFields->getData('name');
|
||||
|
||||
if (Table::hasColumn($tableName, $columnName)) {
|
||||
Table::dropColumn($tableName, $columnName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验
|
||||
*
|
||||
* @time 2021年03月08日
|
||||
* @param $data
|
||||
* @param bool $update
|
||||
* @return mixed
|
||||
*/
|
||||
protected static function changeData($data, $update = false)
|
||||
{
|
||||
if (isset($data['type']) && !in_array($data['type'], ['string', 'int'])) {
|
||||
$data['length'] = 0;
|
||||
}
|
||||
|
||||
// 更新不会校验
|
||||
if (!$update) {
|
||||
if (Table::hasColumn(self::getModelTableName($data['model_id']), $data['name'])) {
|
||||
throw new ColumnException(sprintf('Column [%s] already exist in Table [%s]', $data['name'], self::getModelTableName($data['model_id'])));
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取模型关联的表
|
||||
*
|
||||
* @time 2021年03月08日
|
||||
* @param $modelId
|
||||
* @return mixed|null
|
||||
*/
|
||||
protected static function getModelTableName($modelId)
|
||||
{
|
||||
if (self::$modelTableName) {
|
||||
return self::$modelTableName;
|
||||
}
|
||||
|
||||
self::$modelTableName = Models::where('id', $modelId)->value('table_name');
|
||||
|
||||
return self::$modelTableName;
|
||||
}
|
||||
}
|
52
catch/cms/model/events/ModelsEvent.php
Normal file
52
catch/cms/model/events/ModelsEvent.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | Catch-CMS Design On 2020
|
||||
// +----------------------------------------------------------------------
|
||||
// | CatchAdmin [Just Like ~ ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: JaguarJack [ njphper@gmail.com ]
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace catchAdmin\cms\model\events;
|
||||
|
||||
use catchAdmin\cms\support\Table;
|
||||
use catcher\exceptions\FailedException;
|
||||
|
||||
trait ModelsEvent
|
||||
{
|
||||
/**
|
||||
* 插入前
|
||||
*
|
||||
* @time 2021年03月03日
|
||||
* @param \think\Model $model
|
||||
* @return void
|
||||
*/
|
||||
public static function onBeforeInsert($model): void
|
||||
{
|
||||
if (!Table::exist($model->getData('table_name'))) {
|
||||
throw new FailedException('模型关联的表【' .$model->getData('table_name'). '】不存在');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新前
|
||||
*
|
||||
* @time 2021年03月03日
|
||||
* @param \think\Model $model
|
||||
* @return void
|
||||
*/
|
||||
public static function onBeforeUpdate($model): void
|
||||
{
|
||||
$data = $model->getData();
|
||||
|
||||
$tableName = $data['table_name'] ?? null;
|
||||
|
||||
if ($tableName && !Table::exist($model->getData('table_name'))) {
|
||||
throw new FailedException('模型关联的表【' .$model->getData('table_name'). '】不存在');
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user