审查视图

application/admin/model/cms/Fields.php 4.7 KB
景龙 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 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 128 129 130 131 132 133 134 135 136 137 138 139 140 141
<?php

namespace app\admin\model\cms;

use addons\cms\library\Alter;
use app\common\model\Config;
use think\Exception;
use think\exception\PDOException;
use think\Model;

class Fields extends Model
{

    // 表名
    protected $name = 'cms_fields';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    // 追加属性
    protected $append = [
        'status_text',
        'content_list',
    ];
    protected static $listField = ['select', 'selects', 'checkbox', 'radio', 'array'];

    public function setError($error)
    {
        $this->error = $error;
    }

    protected static function init()
    {
        $beforeUpdateCallback = function ($row) {

        };

        $afterInsertCallback = function ($row) {
            //为了避免引起更新的事件回调,这里采用直接执行SQL的写法
            $row->query($row->fetchSql(true)->update(['id' => $row['id'], 'weigh' => $row['id']]));
            $field = $row['model_id'] ? 'model_id' : 'diyform_id';
            $model = $row['model_id'] ? Modelx::get($row[$field]) : Diyform::get($row[$field]);
            if ($model) {
                $sql = Alter::instance()
                    ->setTable($model['table'])
                    ->setName($row['name'])
                    ->setLength($row['length'])
                    ->setContent($row['content'])
                    ->setDecimals($row['decimals'])
                    ->setDefaultvalue($row['defaultvalue'])
                    ->setComment($row['title'])
                    ->setType($row['type'])
                    ->getAddSql();
                try {
                    db()->query($sql);
                    $fields = Fields::where($field, $model['id'])->field('name')->column('name');
                    $model->fields = implode(',', $fields);
                    $model->save();
                } catch (PDOException $e) {
                    $row->getQuery()->where('id', $row->id)->delete();
                    throw new Exception($e->getMessage());
                }
            }
        };
        $afterUpdateCallback = function ($row) {
            $field = $row['model_id'] ? 'model_id' : 'diyform_id';
            $model = $row['model_id'] ? Modelx::get($row[$field]) : Diyform::get($row[$field]);
            if ($model) {
                $alter = Alter::instance();
                if (isset($row['oldname']) && $row['oldname'] != $row['name']) {
                    $alter->setOldname($row['oldname']);
                }
                $sql = $alter
                    ->setTable($model['table'])
                    ->setName($row['name'])
                    ->setLength($row['length'])
                    ->setContent($row['content'])
                    ->setDecimals($row['decimals'])
                    ->setDefaultvalue($row['defaultvalue'])
                    ->setComment($row['title'])
                    ->setType($row['type'])
                    ->getModifySql();
                db()->query($sql);
                $fields = Fields::where($field, $model['id'])->field('name')->column('name');
                $model->fields = implode(',', $fields);
                $model->save();
            }
        };

        self::beforeInsert($beforeUpdateCallback);
        self::beforeUpdate($beforeUpdateCallback);

        self::afterInsert($afterInsertCallback);
        self::afterUpdate($afterUpdateCallback);

        self::afterDelete(function ($row) {
            $field = $row['model_id'] ? 'model_id' : 'diyform_id';
            $model = $row['model_id'] ? Modelx::get($row[$field]) : Diyform::get($row[$field]);
            if ($model) {
                $sql = Alter::instance()
                    ->setTable($model['table'])
                    ->setName($row['name'])
                    ->getDropSql();
                try {
                    db()->query($sql);
                } catch (PDOException $e) {

                }
            }
        });
    }

    public function getContentListAttr($value, $data)
    {
        return in_array($data['type'], self::$listField) ? Config::decode($data['content']) : $data['content'];
    }

    public function getStatusList()
    {
        return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : $data['status'];
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function model()
    {
        return $this->belongsTo('Modelx', 'model_id')->setEagerlyType(0);
    }

    public function diyform()
    {
        return $this->belongsTo('Diyform', 'diyform_id')->setEagerlyType(0);
    }

}