初始化
This commit is contained in:
74
addons/webman/form/Driver/Config.php
Normal file
74
addons/webman/form/Driver/Config.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace addons\webman\form\Driver;
|
||||
|
||||
|
||||
|
||||
use ExAdmin\ui\component\form\step\StepResult;
|
||||
use ExAdmin\ui\contract\FormAbstract;
|
||||
use ExAdmin\ui\response\Message;
|
||||
use ExAdmin\ui\response\Response;
|
||||
|
||||
|
||||
class Config extends FormAbstract
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* 数据保存
|
||||
* @param array $data
|
||||
* @param mixed $id
|
||||
* @return Message|Response
|
||||
*/
|
||||
public function save(array $data, $id = null)
|
||||
{
|
||||
$result = $this->dispatchEvent('saving',[$this->form]);
|
||||
if ($result instanceof Message) {
|
||||
return $result;
|
||||
}
|
||||
foreach ($data as $field => $value) {
|
||||
admin_sysconf($field, $value);
|
||||
}
|
||||
|
||||
$savedResult = $this->dispatchEvent('saved',[$this->form]);
|
||||
if ($savedResult instanceof Message) {
|
||||
return $savedResult;
|
||||
}
|
||||
if($this->form->isStepfinish()){
|
||||
$result = call_user_func($this->form->getSteps()->getFinish(),new StepResult($this->form,$data, $result, $id));
|
||||
return Response::success($result,'',202);
|
||||
}
|
||||
return message_success(admin_trans('form.save_success'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回唯一标识字段,一般数据库主键自增字段
|
||||
* @return string
|
||||
*/
|
||||
public function getPk(): string
|
||||
{
|
||||
return 'id';
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据
|
||||
* @param string $field 字段
|
||||
* @return mixed
|
||||
*/
|
||||
public function get(string $field = null)
|
||||
{
|
||||
return admin_sysconf($field);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑数据
|
||||
* @param mixed $id
|
||||
* @return mixed
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
// TODO: Implement edit() method.
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
203
addons/webman/form/Driver/Eloquent.php
Normal file
203
addons/webman/form/Driver/Eloquent.php
Normal file
@@ -0,0 +1,203 @@
|
||||
<?php
|
||||
|
||||
namespace addons\webman\form\Driver;
|
||||
|
||||
|
||||
use ExAdmin\ui\component\form\step\StepResult;
|
||||
use ExAdmin\ui\contract\FormAbstract;
|
||||
use ExAdmin\ui\response\Message;
|
||||
use ExAdmin\ui\response\Response;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphOne;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Support\Arr;
|
||||
use support\Db;
|
||||
|
||||
|
||||
/**
|
||||
* @property Model $repository
|
||||
*/
|
||||
class Eloquent extends FormAbstract
|
||||
{
|
||||
|
||||
/**
|
||||
* 编辑数据
|
||||
* @param mixed $id
|
||||
* @return mixed
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
if ($this->trashed()) {
|
||||
$this->data = $this->repository->withTrashed()->find($id);
|
||||
} else {
|
||||
$this->data = $this->repository->find($id);
|
||||
}
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
public function trashed(): bool
|
||||
{
|
||||
return in_array(SoftDeletes::class, class_uses_recursive($this->repository));
|
||||
}
|
||||
|
||||
public function model()
|
||||
{
|
||||
return $this->repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据保存
|
||||
* @param array $data
|
||||
* @return Message|Response
|
||||
*/
|
||||
public function save(array $data, $id = null)
|
||||
{
|
||||
//验证数据
|
||||
$result = $this->form->validator()->check($data, !is_null($id));
|
||||
if ($result instanceof Response) {
|
||||
return $result;
|
||||
}
|
||||
$this->form->input($data);
|
||||
$result = $this->dispatchEvent('saving', [$this->form]);
|
||||
if ($result instanceof Message) {
|
||||
return $result;
|
||||
}
|
||||
$tableField = $this->getTableFields($this->repository->getTable());
|
||||
if (!is_null($id)) {
|
||||
$this->repository = $this->edit($id);
|
||||
}
|
||||
Db::connection($this->repository->getConnectionName())->beginTransaction();
|
||||
try {
|
||||
foreach ($this->form->input() as $field => $value) {
|
||||
if (in_array($field, $tableField)) {
|
||||
$this->repository->setAttribute($field, $value);
|
||||
}
|
||||
}
|
||||
if (!in_array($this->repository::CREATED_AT, $tableField) || !in_array($this->repository::UPDATED_AT, $tableField)) {
|
||||
$this->repository->timestamps = false;
|
||||
}
|
||||
$result = $this->repository->save();
|
||||
foreach ($this->form->input() as $field => $value) {
|
||||
if (method_exists($this->repository, $field)) {
|
||||
$relationMethod = $this->repository->$field();
|
||||
if ($relationMethod instanceof BelongsToMany) {
|
||||
$relationMethod->sync($value);
|
||||
} elseif ($relationMethod instanceof HasOne || $relationMethod instanceof MorphOne || $relationMethod instanceof BelongsTo || $relationMethod instanceof MorphTo) {
|
||||
$model = $this->repository->$field;
|
||||
if (!$model) {
|
||||
$model = $relationMethod->make();
|
||||
}
|
||||
$this->relationSave($model, $value);
|
||||
} elseif ($relationMethod instanceof HasMany || $relationMethod instanceof MorphMany) {
|
||||
$pk = $relationMethod->getModel()->getKeyName();
|
||||
|
||||
$realtionUpdateIds = array_column($value, $pk);
|
||||
if (!empty($this->repository->$field)) {
|
||||
$deleteIds = $this->repository->$field->pluck($pk)->toArray();
|
||||
$deleteIds = array_diff($deleteIds, $realtionUpdateIds);
|
||||
if (count($deleteIds) > 0) {
|
||||
$this->repository->$field()->whereIn($pk, $deleteIds)->delete();
|
||||
}
|
||||
}
|
||||
$foreignKey = $relationMethod->getForeignKeyName();
|
||||
$parentKey = $relationMethod->getParentKey();
|
||||
|
||||
foreach ($value as $key => &$val) {
|
||||
$model = $relationMethod->getModel()->newModelInstance();
|
||||
if (!empty($val[$pk])) {
|
||||
$model = $model->find($val[$pk]);
|
||||
}
|
||||
$val[$foreignKey] = $parentKey;
|
||||
|
||||
$this->relationSave($model, $val);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Db::connection($this->repository->getConnectionName())->commit();
|
||||
} catch (\Exception $exception) {
|
||||
Db::connection($this->repository->getConnectionName())->rollBack();
|
||||
if (config('app.debug')) {
|
||||
throw $exception;
|
||||
}
|
||||
|
||||
}
|
||||
$savedResult = $this->dispatchEvent('saved', [$this->form]);
|
||||
if ($savedResult instanceof Message) {
|
||||
return $savedResult;
|
||||
}
|
||||
if ($this->form->isStepfinish()) {
|
||||
$result = call_user_func($this->form->getSteps()->getFinish(), new StepResult($this->form, $data, $result, $id));
|
||||
return Response::success($result, '', 202);
|
||||
}
|
||||
if ($result) {
|
||||
return message_success(admin_trans('form.save_success'));
|
||||
}
|
||||
return message_error(admin_trans('form.save_fail'));
|
||||
}
|
||||
protected function getTableFields($table){
|
||||
$tableFields = Db::connection($this->repository->getConnectionName())->select('SHOW FULL COLUMNS FROM '.$table);
|
||||
$fields = [];
|
||||
foreach ($tableFields as $tableField){
|
||||
$tableField = json_decode(json_encode($tableField),true);
|
||||
$tableField = array_change_key_case($tableField);
|
||||
$fields[] = $tableField['field'];
|
||||
}
|
||||
return $fields;
|
||||
}
|
||||
protected function relationSave(Model $model, array $data)
|
||||
{
|
||||
$tableField = $this->getTableFields($model->getTable());
|
||||
|
||||
foreach ($data as $field => $value) {
|
||||
if (in_array($field, $tableField)) {
|
||||
$model->$field = $value;
|
||||
}
|
||||
}
|
||||
if (!in_array($model::CREATED_AT, $tableField) || !in_array($model::UPDATED_AT, $tableField)) {
|
||||
$model->timestamps = false;
|
||||
}
|
||||
$model->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回唯一标识字段,一般数据库主键自增字段
|
||||
* @return string
|
||||
*/
|
||||
public function getPk(): string
|
||||
{
|
||||
return $this->repository->getKeyName();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据
|
||||
* @param string $field 字段
|
||||
* @return mixed
|
||||
*/
|
||||
public function get(string $field = null)
|
||||
{
|
||||
if (is_null($field)) {
|
||||
return $this->data->toArray();
|
||||
}
|
||||
$value = Arr::get($this->data, $field);
|
||||
if (method_exists($this->repository, $field)) {
|
||||
$relation = $this->repository->$field();
|
||||
if ($relation instanceof BelongsToMany) {
|
||||
if (empty($value)) {
|
||||
return [];
|
||||
} else {
|
||||
return $value->pluck($relation->getRelatedKeyName());
|
||||
}
|
||||
}
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
19
addons/webman/form/FormManager.php
Normal file
19
addons/webman/form/FormManager.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace addons\webman\form;
|
||||
|
||||
|
||||
use addons\webman\form\Driver\Eloquent;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class FormManager extends \ExAdmin\ui\manager\FormManager
|
||||
{
|
||||
public function setDriver($repository,$component)
|
||||
{
|
||||
parent::setDriver($repository,$component); // TODO: Change the autogenerated stub
|
||||
if ($repository instanceof Model) {
|
||||
$this->driver = new Eloquent();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
16
addons/webman/form/MyEditor.php
Normal file
16
addons/webman/form/MyEditor.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace addons\webman\form;
|
||||
|
||||
use ExAdmin\ui\component\form\Field;
|
||||
|
||||
class MyEditor extends Field
|
||||
{
|
||||
public function jsonSerialize()
|
||||
{
|
||||
$this->attr('html-raw',true)
|
||||
->content(file_get_contents( plugin()->webman->getPath(). '/views/my_editor.vue'));
|
||||
|
||||
return parent::jsonSerialize();
|
||||
}
|
||||
}
|
||||
112
addons/webman/form/Uploader.php
Normal file
112
addons/webman/form/Uploader.php
Normal file
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace addons\webman\form;
|
||||
|
||||
use addons\webman\filesystem\Filesystem;
|
||||
use ExAdmin\ui\contract\UploaderAbstract;
|
||||
use ExAdmin\ui\response\Response;
|
||||
use Intervention\Image\ImageManagerStatic as Image;
|
||||
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||
|
||||
class Uploader extends UploaderAbstract
|
||||
{
|
||||
/**
|
||||
* 写入文件
|
||||
* @param string $filename 文件名
|
||||
* @param $content 文件内容
|
||||
* @return bool
|
||||
*/
|
||||
protected function put(string $filename, $content): bool
|
||||
{
|
||||
return Filesystem::disk($this->disk)->put($filename, $content);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否存在
|
||||
* @param string $path 路径
|
||||
* @return bool
|
||||
*/
|
||||
public function exists(string $path): bool
|
||||
{
|
||||
return Filesystem::disk($this->disk)->exists($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回访问url
|
||||
* @param string $path 路径
|
||||
* @return string
|
||||
*/
|
||||
public function url(string $path): string
|
||||
{
|
||||
|
||||
return Filesystem::disk($this->disk)->url($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* 临时目录
|
||||
* @return string
|
||||
*/
|
||||
protected function tempDirectory(): string
|
||||
{
|
||||
return runtime_path('tmp');
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入文件
|
||||
* @param $content 文件内容
|
||||
* @param $filename 文件名
|
||||
* @return bool|string
|
||||
*/
|
||||
public function putContent($content, $filename)
|
||||
{
|
||||
$path = $this->directory . $filename . '.' . $this->extension;
|
||||
return Filesystem::disk($this->disk)->put($path, $content);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传
|
||||
* @param \Closure $complete
|
||||
* @param bool $exists 判断秒传
|
||||
* @return Response
|
||||
*/
|
||||
public function upload(\Closure $complete = null, bool $exists = false)
|
||||
{
|
||||
return parent::upload(function (UploadedFile $file) {
|
||||
if ($this->form) {
|
||||
$component = $this->form->getImageComponent();
|
||||
if ($component) {
|
||||
$thumbnail = $component->getThumbnail();
|
||||
//图片处理
|
||||
$interventionCall = $component->getInterventionCall();
|
||||
if (count($interventionCall) > 0) {
|
||||
$image = Image::make($file->getRealPath());
|
||||
foreach ($interventionCall as $call) {
|
||||
call_user_func_array([$image, $call['method']], $call['arguments']);
|
||||
}
|
||||
$file = $image->encode(null, null)->getEncoded();
|
||||
}
|
||||
//生成缩略图
|
||||
if (count($thumbnail) > 0) {
|
||||
if ($file instanceof UploadedFile) {
|
||||
$data = $file->getRealPath();
|
||||
$filename = request()->input('identifier');
|
||||
} else {
|
||||
$data = $file;
|
||||
$filename = md5($data);
|
||||
}
|
||||
foreach ($thumbnail as $name => $size) {
|
||||
$image = Image::make($data);
|
||||
list($width, $height) = $size;
|
||||
$content = $image->resize($width, $height)
|
||||
->encode(null, null)
|
||||
->getEncoded();
|
||||
$this->putContent($content, $filename . '-' . $name);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return $file;
|
||||
}, request()->input('type') == 'file'); // TODO: Change the autogenerated stub
|
||||
}
|
||||
}
|
||||
53
addons/webman/form/Validator.php
Normal file
53
addons/webman/form/Validator.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace addons\webman\form;
|
||||
|
||||
|
||||
use ExAdmin\ui\contract\ValidatorAbstract;
|
||||
use ExAdmin\ui\response\Response;
|
||||
|
||||
class Validator extends ValidatorAbstract
|
||||
{
|
||||
/**
|
||||
* 验证
|
||||
* @param array $data 表单数据
|
||||
* @param bool $edit true更新,false新增
|
||||
* @return mixed
|
||||
*/
|
||||
function check(array $data, bool $edit)
|
||||
{
|
||||
$ruleArr = $edit ? $this->updateRule : $this->createRule;
|
||||
$rules = [];
|
||||
$messages = [];
|
||||
foreach ($ruleArr as $field => $row) {
|
||||
$rule = [];
|
||||
if($row instanceof \Closure){
|
||||
$row = call_user_func_array($row,[$data,$this->form]);
|
||||
}
|
||||
foreach ($row as $key => $item) {
|
||||
if (is_numeric($key)) {
|
||||
$rule[] = $item;
|
||||
} else {
|
||||
$rule[] = $key;
|
||||
$index = strpos($key, ':');
|
||||
if ($index !== false) {
|
||||
$key = substr($key, 0, $index);
|
||||
}
|
||||
$messages["{$field}.{$key}"] = $item;
|
||||
}
|
||||
}
|
||||
$rules[$field] = $rule;
|
||||
}
|
||||
$validator = validator($data, $rules, $messages);
|
||||
if ($validator->fails()) {
|
||||
return Response::success($validator->errors()->getMessages(), '', 422);
|
||||
}
|
||||
if($this->form->getSteps()){
|
||||
|
||||
if(!$this->form->isStepfinish()){
|
||||
return Response::success([], '', 201);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user