EntityController.php 3.7 KB
Newer Older
孙建华 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
/**
 * @author  Eddy <cumtsjh@163.com>
 */

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\EntityRequest;
use App\Repository\Admin\EntityRepository;
use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Illuminate\View\View;
use Illuminate\Support\Facades\Log;
16
use App\Exceptions\CreateTableException;
孙建华 已提交
17 18 19

class EntityController extends Controller
{
20 21 22
    protected $formNames = [
        'name', 'table_name', 'description', 'is_internal', 'enable_comment', 'is_show_content_manage'
    ];
孙建华 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

    public function __construct()
    {
        parent::__construct();

        $this->breadcrumb[] = ['title' => '模型列表', 'url' => route('admin::entity.index')];
    }

    /**
     * 模型管理-模型列表
     *
     */
    public function index()
    {
        $this->breadcrumb[] = ['title' => '模型列表', 'url' => ''];
        return view('admin.entity.index', ['breadcrumb' => $this->breadcrumb]);
    }

    /**
     * 模型列表数据接口
     *
     * @param Request $request
     * @return array
     */
    public function list(Request $request)
    {
        $perPage = (int) $request->get('limit', 50);
        $condition = $request->only($this->formNames);

        $data = EntityRepository::list($perPage, $condition);

        return $data;
    }

    /**
     * 模型管理-新增模型
     *
     */
    public function create()
    {
        $this->breadcrumb[] = ['title' => '新增模型', 'url' => ''];
        return view('admin.entity.add', ['breadcrumb' => $this->breadcrumb]);
    }

    /**
     * 模型管理-保存模型
     *
     * @param EntityRequest $request
     * @return array
     */
    public function save(EntityRequest $request)
    {
        try {
76 77
            $createDB = $request->post('is_modify_db', false);
            EntityRepository::add($request->only($this->formNames), $createDB);
孙建华 已提交
78 79 80
            return [
                'code' => 0,
                'msg' => '新增成功',
孙建华 已提交
81
                'redirect' => true
孙建华 已提交
82
            ];
83 84 85 86 87 88
        } catch (CreateTableException $e) {
            return [
                'code' => 2,
                'msg' => '新增失败:创建数据库表失败,数据表已存在或其它原因',
                'redirect' => false
            ];
孙建华 已提交
89 90 91 92
        } catch (QueryException $e) {
            return [
                'code' => 1,
                'msg' => '新增失败:' . (Str::contains($e->getMessage(), 'Duplicate entry') ? '当前模型已存在' : '其它错误'),
孙建华 已提交
93
                'redirect' => false
孙建华 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
            ];
        }
    }

    /**
     * 模型管理-编辑模型
     *
     * @param int $id
     * @return View
     */
    public function edit($id)
    {
        $this->breadcrumb[] = ['title' => '编辑模型', 'url' => ''];

        $model = EntityRepository::find($id);
        return view('admin.entity.add', ['id' => $id, 'model' => $model, 'breadcrumb' => $this->breadcrumb]);
    }

    /**
     * 模型管理-更新模型
     *
     * @param EntityRequest $request
     * @param int $id
     * @return array
     */
    public function update(EntityRequest $request, $id)
    {
        $data = $request->only($this->formNames);
        unset($data['table_name']);
        try {
            EntityRepository::update($id, $data);
            return [
                'code' => 0,
                'msg' => '编辑成功',
孙建华 已提交
128
                'redirect' => true
孙建华 已提交
129 130 131 132 133 134
            ];
        } catch (QueryException $e) {
            Log::error($e);
            return [
                'code' => 1,
                'msg' => '编辑失败:' . (Str::contains($e->getMessage(), 'Duplicate entry') ? '当前模型已存在' : '其它错误'),
孙建华 已提交
135
                'redirect' => false
孙建华 已提交
136 137 138 139
            ];
        }
    }
}