| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- <?php
- namespace addons\SmartForm\common\models;
- use common\enums\StatusEnum;
- use common\models\mall\MallAddons;
- use common\models\user\User;
- use Yii;
- use yii\db\Exception;
- use addons\SmartForm\common\enums\SmartFormEnum;
- use yii\helpers\Json;
- /**
- * This is the model class for table "{{%addons_smart_form_template_column}}".
- *
- * @property int $id
- * @property int $mall_id 商城ID
- * @property int|null $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- * @property array|null $extra_data 修改时间
- */
- class AddonsSmartFormTemplateColumn extends \common\models\base\BaseActiveRecord
- {
- const ADDONS_NAME = 'SmartForm';
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_smart_form_template_column}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['smart_form_template_id','smart_form_components_id','title'], 'required'],
- [['smart_form_template_id','smart_form_components_id', 'required', 'limit', 'sort', 'is_export', 'content_type', 'status', 'created_at', 'updated_at'], 'integer'],
- [['default'], 'string', 'max' => 255],
- [['title','tips'], 'string', 'max' => 100],
- [['placeholder'], 'string'],
- [['smart_form_components_code'], 'string', 'max' => 30],
- [['extra_data'], 'safe'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => '主键',
- 'smart_form_template_id' => '智能装修表单id',
- 'smart_form_components_code' => '智能装修组件标识',
- 'smart_form_components_id' => '智能装修组件id',
- 'title' => '标题',
- 'tips' => '提示语',
- 'required' => '是否必填(1:是;2:否)',
- 'limit' => '限制长度',
- 'show_default' => '显示默认值[0:不显示;1:显示]',
- 'is_export' => '是否显示一键导入功能:0:否;1是',
- 'content_type' => '文本类型 0:默认 1:姓名 2:手机 3:详细地址',
- 'default' => '默认值',
- 'sort' => '排序,越小排越前',
- 'status' => '状态[0:禁用;1:启用,2:已过期]',
- 'created_at' => '添加时间',
- 'updated_at' => '修改时间 ',
- ];
- }
- /**
- * @method FunctionName
- * 保存模板字段
- * @author zqh
- * @datetime 2022-05-06T02:18:13+0800
- * @param array $params [description]
- */
- public static function createColumn($params=[])
- {
- # 判断当前模板是否已经有字段
- $where['status'] = StatusEnum::ENABLED;
- $where['smart_form_template_id'] = $params['template_id'];
- $list = self::getOne([$where],true);
- if($list){
- # 存在则删除
- $count = self::deleteAll(['smart_form_template_id' => $params['template_id']]);
- }
- $template_id = $params['template_id']; # 模板id
- unset($params['template_id']);
- $insert = [];
- # 遍历字段数组
- foreach ($params as $ck => $cv) {
- $arr = [];
- $arr['smart_form_template_id'] = $template_id; # 模板id
- $arr['smart_form_components_code'] = $cv['type']; # 组件id
- $arr['smart_form_components_id'] = $cv['components_id']; # 组件id
- $arr['title'] = $cv['title'] ? $cv['title'] : ''; # 标题
- $arr['tips'] = $cv['tips'] ? $cv['tips'] : ''; # 提示语
- $arr['placeholder'] = $cv['placeholder'] ? : ''; # 简短提示语
- $arr['required'] = $cv['required'] ? $cv['required'] : 0; # 是否必填
- $arr['limit'] = $cv['total'] ?? 999; # 限制长度
- $arr['status'] = StatusEnum::ENABLED;
- $arr['option'] = '';# 下拉选项
- $arr['show_default'] = 0;
- $arr['is_export'] = $cv['is_export'] ?? 0; # 是否显示一键导入功能
- $arr['content_type'] = $cv['content_type'] ?? 0; # 文本类型
- $arr['extra_data'] = Json::encode($cv['extra_data'] ?? []);
- if (mb_strlen($arr['title']) > 100) {
- self::$static_error = '标题长度不能超过100';
- return false;
- }
- if (mb_strlen($arr['tips']) > 100) {
- self::$static_error = '提示语长度不能超过100';
- return false;
- }
- # 存在下拉选项
- if(isset($cv['chooseList']) && !empty($cv['chooseList'])){
- $arr['option']= implode(SmartFormEnum::FIRST_CUTTER,array_column($cv['chooseList'],'title'));
- }
- $arr['default'] = '';
- switch ($cv['type']) {
- case 'date-tool': # 日期类型
-
- if(isset($cv['is_show']) && $cv['is_show'])
- {
- $arr['show_default'] = $cv['is_show'];
- if(isset($cv['show_now_day']) && $cv['show_now_day']){
- # 显示当天日期
- $arr['default'] = 'func' . SmartFormEnum::PARAM_CUTTER . 'showDate' . SmartFormEnum::PARAM_CUTTER . 'today';
- }else{
- # 自定义日期
- $arr['default'] = $cv['date'];
- }
- }else{
- $arr['default'] = '';
- }
- break;
- case 'time-tool': # 时间类型
-
- if(isset($cv['is_show']) && $cv['is_show'])
- {
- $arr['show_default'] = $cv['is_show'];
- if(isset($cv['show_now_time']) && $cv['show_now_time']){
- # 显示当天日期
- $arr['default'] = 'func' . SmartFormEnum::PARAM_CUTTER . 'showTime' . SmartFormEnum::PARAM_CUTTER . 'now';
- }else{
- # 自定义日期
- $arr['default'] = $cv['time'];
- }
- }else{
- $arr['default'] = '';
- }
- break;
- case 'date-range': # 日期范围类型
- # 显示开始日期
- if(isset($cv['is_start_show']) && $cv['is_start_show'])
- {
- $arr['show_default'] = $cv['is_start_show'];
- if(isset($cv['show_now_start']) && $cv['show_now_start']){
- # 显示当天日期
- $arr['default'] = 'func' . SmartFormEnum::PARAM_CUTTER . 'showDate' . SmartFormEnum::PARAM_CUTTER . 'today';
- }else{
- # 自定义日期
- $arr['default'] = $cv['start_date'];
- }
- }else{
- $arr['default'] = '';
- $arr['show_default'] = 0;
- }
- # 显示结束日期
- if(isset($cv['is_end_show']) && $cv['is_end_show'])
- {
- $arr['show_default'] .= SmartFormEnum::FIRST_CUTTER . $cv['is_end_show'];
- if(isset($cv['show_now_end']) && $cv['show_now_end']){
- # 显示当天日期
- $arr['default'] .= SmartFormEnum::DATE_CUTTER . 'func' . SmartFormEnum::PARAM_CUTTER . 'showDate' . SmartFormEnum::PARAM_CUTTER . 'today';
- }else{
- # 自定义日期
- $arr['default'] .= SmartFormEnum::DATE_CUTTER . $cv['end_date'];
- }
- }else{
- $arr['default'] .= SmartFormEnum::DATE_CUTTER . '';
- $arr['show_default'] .= SmartFormEnum::FIRST_CUTTER . 0;
- }
- # 提示语
- $arr['tips'] = $cv['startTitle'] . SmartFormEnum::FIRST_CUTTER . $cv['endTitle'];
- break;
- case 'time-range': # 时间范围类型
- # 显示开始日期
- if(isset($cv['is_start_show']) && $cv['is_start_show'])
- {
- $arr['show_default'] = $cv['is_start_show'];
- if(isset($cv['show_now_start']) && $cv['show_now_start']){
- # 显示当天日期
- $arr['default'] = 'func' . SmartFormEnum::PARAM_CUTTER . 'showTime' . SmartFormEnum::PARAM_CUTTER . 'now';
- }else{
- # 自定义日期
- $arr['default'] = $cv['start_time'];
- }
- }else{
- $arr['default'] = '';
- $arr['show_default'] = 0;
- }
- # 显示结束日期
- if(isset($cv['is_end_show']) && $cv['is_end_show'])
- {
- $arr['show_default'] .= SmartFormEnum::FIRST_CUTTER . $cv['is_end_show'];
- if(isset($cv['show_now_end']) && $cv['show_now_end']){
- # 显示当天日期
- $arr['default'] .= SmartFormEnum::DATE_CUTTER . 'func' . SmartFormEnum::PARAM_CUTTER . 'showTime' . SmartFormEnum::PARAM_CUTTER . 'now';
- }else{
- # 自定义日期
- $arr['default'] .= SmartFormEnum::DATE_CUTTER . $cv['end_time'];
- }
- }else{
- $arr['default'] .= SmartFormEnum::DATE_CUTTER . '';
- $arr['show_default'] .= SmartFormEnum::FIRST_CUTTER . 0;
- }
- # 提示语
- $arr['tips'] = $cv['startTitle'] . SmartFormEnum::FIRST_CUTTER . $cv['endTitle'];
- break;
- case 'city-chose': # 省市区
- $arr['default'] = $cv['city_type'];
- break;
- }
- $arr['sort'] = $ck;
- $arr['created_at'] = time();
- $arr['updated_at'] = time();
- $insert[] = $arr;
- }
- # 批量插入数据
- # 字段
- $field = ['smart_form_template_id','smart_form_components_code','smart_form_components_id','title','tips','placeholder','required','limit','status','option','show_default','is_export','content_type', 'extra_data','default','sort','created_at','updated_at'];
- $res = Yii::$app->db->createCommand()->batchInsert(self::tableName(), $field, $insert)->execute();
- if ($res == false) {
- self::$static_error = '写入模板字段失败';
- return false;
- }
- return $res;
- }
-
- }
|