| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581 |
- <?php
- namespace addons\SmartForm\common\models;
- use addons\SmartForm\common\models\AddonsSmartFormTemplateColumn; # 模板字段
- use addons\SmartForm\common\models\AddonsSmartFormTemplateUserForm; # 用户表单
- use addons\SmartForm\common\models\AddonsSmartFormDecorationForm; # 关联表单
- use common\enums\StatusEnum;
- use common\models\mall\MallAddons;
- use common\models\user\User;
- use addons\SmartForm\common\enums\SmartFormEnum;
- use common\models\diy\DiyPage;
- use Yii;
- use yii\db\Exception;
- use yii\helpers\Json;
- /**
- * This is the model class for table "{{%addons_smart_form_template}}".
- *
- * @property int $id
- * @property int $mall_id 商城ID
- * @property int|null $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- * @property int $is_repeat 修改时间
- */
- class AddonsSmartFormTemplate extends \common\models\base\BaseActiveRecord
- {
- const ADDONS_NAME = 'SmartForm';
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_smart_form_template}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id','title'], 'required'],
- [['expire_time', 'mall_id', 'jump_type', 'status', 'link_status', 'diy_page_id', 'created_at', 'updated_at','show_header', 'is_repeat'], 'integer'],
- [['title'], 'string', 'max' => 150],
- [['jump_link','header'], 'string', 'max' => 250],
- ];
- }
- /**
- * @method getColumns
- * 关联模型 - 表单字段
- * @author zqh
- * @datetime 2022-05-06T13:59:55+0800
- * @return [type] [description]
- */
- public function getColumns()
- {
- return $this->hasMany(AddonsSmartFormTemplateColumn::class, ['smart_form_template_id' => 'id'])->orderBy('sort asc');
- // ->indexBy('smart_form_components_code');
- }
- /**
- * @method getColumnsCodeInfo
- * 关联模型 - 表单字段code
- * @author zqh
- * @datetime 2022-05-07T21:21:19+0800
- * @return [type] [description]
- */
- public function getColumnsCodeInfo()
- {
- }
- /**
- * @method getColumnsInfo
- * 关联模型 - 表单字段-方便处理提交表单
- * @author zqh
- * @datetime 2022-05-06T13:59:55+0800
- * @return [type] [description]
- */
- public function getColumnsInfo()
- {
- return $this->hasMany(AddonsSmartFormTemplateColumn::class, ['smart_form_template_id' => 'id'])
- ->indexBy('id');
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => '主键',
- 'mall_id' => '商城id',
- 'diy_page_id' => '使用页面id',
- 'title' => '标题',
- 'show_header' => '开启头部背景',
- 'header' => '头部背景',
- 'expire_time' => '过期时间[-1:永久有效]',
- 'status' => '状态[-1:删除;0:禁用;1:启用,2:已过期]',
- 'link_status' => '开启跳转[0:关闭;1:开启]',
- 'jump_type' => '跳转类型[0:跳转链接;1:跳转二维码]',
- 'jump_link' => '跳转链接',
- 'created_at' => '添加时间',
- 'updated_at' => '更新时间',
- ];
- }
- /**
- * @method getTplInfo
- * 通过模板id获取详情
- * @author zqh
- * @datetime 2022-05-06T14:00:34+0800
- * @param [type] $template_id [description]
- * @return [type] [description]
- */
- public static function getTplInfo($template_id=0)
- {
- if(!$template_id){
- self::$static_error = "模板不存在";
- return false;
- }
- $info = AddonsSmartFormTemplate::getOne([['id' => $template_id]], true, [
- 'columns'=> function ($query) {
- }
- ]);
- if(!$info){
- self::$static_error = "模板不存在";
- return false;
- }
- if($info['columns']){
- # 存在模板字段
- $columns = $info['columns'];
- $classname = new SmartFormEnum();
- foreach ($columns as $k => $v) {
- if(strpos($v['tips'],SmartFormEnum::FIRST_CUTTER) !== false){
- $info['columns'][$k]['tips'] = explode(SmartFormEnum::FIRST_CUTTER,$v['tips']);
- }
- if(strpos($v['show_default'],SmartFormEnum::FIRST_CUTTER) !== false){
- $info['columns'][$k]['show_default'] = explode(SmartFormEnum::FIRST_CUTTER,$v['show_default']);
- }
- if(strpos($v['option'],SmartFormEnum::FIRST_CUTTER) !== false){
- $info['columns'][$k]['option'] = explode(SmartFormEnum::FIRST_CUTTER,$v['option']);
- }
- $info['columns'][$k]['extra_data'] = Json::decode(!empty($v['extra_data']) ? $v['extra_data'] : '[]');
- if(strpos($v['default'],SmartFormEnum::DATE_CUTTER) !== false){
- $default = [];
- $default = explode(SmartFormEnum::DATE_CUTTER,$v['default']);
- foreach ($default as $kd => $kv) {
- if(strpos($kv,SmartFormEnum::PARAM_CUTTER) !== false){
- list($f,$func,$p) = explode(SmartFormEnum::PARAM_CUTTER,$kv);
- $default[$kd] = call_user_func(array($classname, $func),$p);
- $info['columns'][$k]['show_now_start'][$kd] = 1;
- }else{
- $default[$kd] = $kv;
- $info['columns'][$k]['show_now_start'][$kd] = 0;
- }
- }
- $info['columns'][$k]['default'] = $default;
- }else{
- if($v['default']){
- if(strpos($v['default'],SmartFormEnum::PARAM_CUTTER) !== false){
- list($f,$func,$p) = explode(SmartFormEnum::PARAM_CUTTER,$v['default']);
- $default = call_user_func(array($classname, $func),$p);
- $info['columns'][$k]['show_now_start'] = 1;
- $info['columns'][$k]['default'] = $default;
- }else{
- $default = $v['default'];
- $info['columns'][$k]['show_now_start'] = 0;
- }
- }
- }
- }
- }
- return $info;
- }
- /**
- * @method createTemplate
- * 新增模板
- * @author zqh
- * @datetime 2022-05-05T16:48:46+0800
- * @param [type] $params [description]
- * @return [type] [description]
- */
- public static function createTemplate($params)
- {
- # 模板基础信息 begin
- $form = $params['form'];
- $column_info = $params['toolList'];
- $where['mall_id'] = $params['mall_id'];
- $where['title'] = $form['title'];
- $where['status'] = StatusEnum::ENABLED;
- # 判断当前标题是否已经存在
- $find = self::getOne([$where],true);
- if($find && !$form['id']){
- self::$static_error = "模板名称已经使用,请先删除或修改该模板";
- return false;
- }
- if ($form['id']) {
- $tplModel = self::findOne(['and',['id'=>$form['id'],'mall_id'=>$params['mall_id']]]);
- if (empty($tplModel)){
- self::$static_error = "记录不存在或已删除";
- return false;
- }
- }else{
- $tplModel = new self();
- $tplModel->loadDefaultValues();
- $tplModel->mall_id = $params['mall_id'];
- }
- $create['mall_id'] = $params['mall_id'];# 商城id
- $create['show_header'] = $form['show_heard'];# 开启头部背景
- $create['header'] = $form['first_img'];# 头部背景
- $create['title'] = $form['title'];# 标题
- $create['is_repeat'] = intval($form['is_repeat']);# 是否可重复
- if($form['time_type'] == -1){
- $create['expire_time'] = $form['time_type'];# 过期时间[-1:永久有效]
- }else{
- $create['expire_time'] = strtotime($form['date']);
- }
-
- $create['link_status'] = $form['link_status']; # 开启跳转[0:关闭;1:开启]
- $create['jump_type'] = $form['jump_type']; # 跳转类型[0:跳转链接;1:跳转二维码]
- if($form['jump_type']=='1'){
- $create['jump_link'] = $form['qcard_img']; # 跳转二维码
- }else{
- $create['jump_link'] = $form['link']; # 跳转链接
- }
-
- // $create['success_link'] = $form['success_link']; # 跳转链接
- if(!$tplModel->load($create,'') || !$tplModel->save()){
- self::$static_error = $tplModel->getErrorMessage();
- return false;
- }
- # 模板基础信息 end
- # 模板字段 begin
-
- $column_info['template_id'] = $tplModel->id;
- $columnStatus = AddonsSmartFormTemplateColumn::createColumn($column_info);
- if(!$columnStatus){
- $AddonsSmartFormTemplateColumnModel = new AddonsSmartFormTemplateColumn();
- self::$static_error = $AddonsSmartFormTemplateColumnModel->getErrorMessage();
- return false;
- }
- # 模板字段 end
- return true;
- }
- /**
- * @method copyTpl
- * 复制模板
- * @author zqh
- * @datetime 2022-05-06T16:43:48+0800
- * @return [type] [description]
- */
- public static function copyTpl($template_id = 0)
- {
- if(!$template_id){
- self::$static_error = "模板不存在";
- return false;
- }
- # 复制主表
- $tplInfo = AddonsSmartFormTemplate::findOne($template_id);
- if(!$tplInfo){
- self::$static_error = '复制对象不存在';
- return false;
- }
- $tplInfo2 = new AddonsSmartFormTemplate;
- $tplInfo2->attributes = $tplInfo->attributes; // 这里需要 rules 支持
- $tplInfo2->created_at = time();
- $tplInfo2->updated_at = time();
- $new = $tplInfo2->save();
- if(!$new) {
- self::$static_error = '复制模板失败';
- return false;
- }
- # 复制明细字段
- $list = AddonsSmartFormTemplateColumn::find()->where(['smart_form_template_id'=>$template_id])->asArray()->all();
- if(empty($list)){
- self::$static_error = '复制对象不存在';
- return false;
- }
- foreach ($list as $k => $v) {
- unset($v['id']);
- $data = [];
- $data["smart_form_template_id"] = $tplInfo2->id;
- $data["smart_form_components_code"] = $v["smart_form_components_code"];
- $data["smart_form_components_id"] = $v["smart_form_components_id"];
- $data["title"] = $v["title"];
- $data["tips"] = $v["tips"];
- $data["placeholder"] = $v["placeholder"];
- $data["required"] = $v["required"];
- $data["limit"] = $v["limit"];
- $data["option"] = $v["option"];
- $data["show_default"] = $v["show_default"];
- $data["default"] = $v["default"];
- $data["is_export"] = $v["is_export"];
- $data["content_type"] = $v["content_type"];
- $data["sort"] = $v["sort"];
- $data["status"] = $v["status"];
- $data["created_at"] = time();
- $data["updated_at"] = time();
- $rows[] = $data;
- }
-
- // 生成数据
- $field = ['smart_form_template_id','smart_form_components_code','smart_form_components_id','title','tips','placeholder','required','limit','option','show_default','default','is_export','content_type','sort','status','created_at','updated_at'];
- $s2 = Yii::$app->db->createCommand()->batchInsert(AddonsSmartFormTemplateColumn::tableName(), $field, $rows)->execute();
- if(!$s2){
- self::$static_error = '复制字段失败';
- return false;
- }
- return $tplInfo2->id;
- }
-
- /**
- * @method del
- * 删除
- * @author zqh
- * @datetime 2022-05-07T21:23:13+0800
- * @param [type] $params [description]
- * @return [type] [description]
- */
- public static function del($params)
- {
- try{
- $trans = Yii::$app->db->beginTransaction();
- $model = self::findOne(['id'=>$params['id']]);
- if($model){
- $s = $s1 = $s2 = true;
- $model->status = StatusEnum::DELETE;
- $s = $model->save();
- if(!$s){
- self::$static_error = '删除表单失败';
- return false;
- }
- # 删除 表单字段
- $model1 = AddonsSmartFormTemplateColumn::findOne(['smart_form_template_id'=>$model->id,'status'=>StatusEnum::ENABLED]);
- if($model1)
- $s1 = AddonsSmartFormTemplateColumn::updateAll(['status'=>StatusEnum::DELETE],['smart_form_template_id'=>$model->id]);
- if(!$s1){
- self::$static_error = '删除表单字段失败';
- return false;
- }
- # 删除 用户提交表单
- $model2 = AddonsSmartFormTemplateUserForm::findOne(['smart_form_template_id'=>$model->id,'status'=>StatusEnum::ENABLED]);
- if($model2)
- $s2 = AddonsSmartFormTemplateUserForm::updateAll(['status'=>StatusEnum::DELETE],['smart_form_template_id'=>$model->id]);
- if( !$s2){
- self::$static_error = '删除用户提交表单失败';
- return false;
- }
- # 删除页面的引入
- # 查找若有引入的页面
- # 删除引入表单
- $list = AddonsSmartFormDecorationForm::find()->where(['smart_form_template_id'=>$model->id,'status'=>StatusEnum::ENABLED])->asArray()->all();
- $s3 = true;
- if(!empty($list)){
- $s3 = AddonsSmartFormDecorationForm::updateAll(['status'=>StatusEnum::DELETE],['smart_form_template_id'=>$model->id]);
- }
- if(!$s3){
- self::$static_error = '删除引入表单失败';
- return false;
- }
- # 删除页面引入
- $s4 = true;
- if(!empty($list)){
- foreach ($list as $k1 => $v1) {
- $diyInfo = DiyPage::findOne(['id' => $v1['diy_page_id']]);
- if($diyInfo['content']){
- $content = json_decode($diyInfo['content'],true);
- $has = false;
- foreach ($content as $k => $v) {
- if($v['funcListItem']['type']=='smart-form'){
- unset($content[$k]);
- $has = true;
- }
- }
- if($has){
- $content = array_values($content);
- $diyInfo['updated_at'] = time();
- $diyInfo['content'] = json_encode($content,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
- $s4 = $diyInfo->save();
- }else{
- $s4 = true;
- }
- if(!$s4){
- self::$static_error = '删除引入页面内容失败';
- return false;
- }
- }
- }
- }
- if(!$s4){
- self::$static_error = '删除引入页面内容失败';
- return false;
- }
- }
- $trans->commit();
- return true;
- }catch (\Exception $e){
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * @method getDiyList
- * 装修选择列表
- * @author zqh
- * @datetime 2022-05-08T07:31:35+0800
- * @param [type] $mall_id [description]
- * @param [type] $post [description]
- * @return [type] [description]
- */
- public static function getDiyList($mall_id,$post)
- {
- $params['select'] = 'id,mall_id,diy_page_id,title,show_header,header,expire_time,status,link_status,jump_type,jump_link,created_at';// 查询字段
- isset($post['keyword']) && $params['where'][] = ['like','title',$post['keyword']];
- $params['where'][] = ['=','status',StatusEnum::ENABLED];
- $params['where'][] = ['mall_id' => $mall_id];
- $params['with'] = ['columns'=> function ($query) {
- $query->where(['status' => StatusEnum::ENABLED])
- ->select('id,smart_form_template_id,smart_form_components_code,smart_form_components_id,title,tips,placeholder,required,limit,option,default,show_default,is_export,content_type')->orderBy('sort asc');
- }];
- $params['limit'] = 15;
- $params['order'] = 'created_at desc';
- $list = AddonsSmartFormTemplate::listPage($params,false,true);
- if($list['list']){
- foreach ($list['list'] as $k11 => $v11) {
-
-
- # 存在模板字段
- $columns = $list['list'][$k11]['columns'];
- $classname = new SmartFormEnum();
- foreach ($columns as $k => $v) {
- if(strpos($v['show_default'],SmartFormEnum::FIRST_CUTTER) !== false){
- $list['list'][$k11]['columns'][$k]['show_default'] = explode(SmartFormEnum::FIRST_CUTTER,$v['show_default']);
- }
- if(strpos($v['tips'],SmartFormEnum::FIRST_CUTTER) !== false){
- $list['list'][$k11]['columns'][$k]['tips'] = explode(SmartFormEnum::FIRST_CUTTER,$v['tips']);
- }
- if(strpos($v['option'],SmartFormEnum::FIRST_CUTTER) !== false){
- $option = [];
- $option = explode(SmartFormEnum::FIRST_CUTTER,$v['option']);
- $list['list'][$k11]['columns'][$k]['option'] = $option;
- foreach ($option as $k1 => $v1) {
- $list['list'][$k11]['columns'][$k]['chooseList'][$k1]['title'] = $v1;
- }
- }
- if(strpos($v['default'],SmartFormEnum::DATE_CUTTER) !== false){
- $default = [];
- $default = explode(SmartFormEnum::DATE_CUTTER,$v['default']);
- foreach ($default as $kd => $kv) {
- if(strpos($kv,SmartFormEnum::PARAM_CUTTER) !== false){
- list($f,$func,$p) = explode(SmartFormEnum::PARAM_CUTTER,$kv);
- $default[$kd] = call_user_func(array($classname, $func),$p);
- $list['list'][$k11]['columns'][$k]['show_now_start'][$kd] = 1;
- }else{
- $default[$kd] = $kv;
- $list['list'][$k11]['columns'][$k]['show_now_start'][$kd] = 0;
- }
- }
- $list['list'][$k11]['columns'][$k]['default'] = $default;
- }else{
- if($v['default']){
- if(strpos($v['default'],SmartFormEnum::PARAM_CUTTER) !== false){
- list($f,$func,$p) = explode(SmartFormEnum::PARAM_CUTTER,$v['default']);
- $default = call_user_func(array($classname, $func),$p);
- $list['list'][$k11]['columns'][$k]['show_now_start'] = 1;
- $list['list'][$k11]['columns'][$k]['default'] = $default;
- }else{
- $default = $v['default'];
- $list['list'][$k11]['columns'][$k]['show_now_start'] = 0;
- }
- }
- }
- }
- }
- }
- return $list;
- }
- /**
- * 获取商品的优惠券设置
- * @Author: lun
- * @DateTime: 2022/5/12 0012 14:49
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $params
- * @return bool|string[]
- */
- public static function getGoodSetting($params)
- {
- // 后台页面组件展示
- if ($params['type'] == 'show_component') {
- return [
- 'component' => 'com-smartform-goods',
- 'path' => Yii::$app->basePath . '/../addons/SmartForm/backend/views/components/',
- 'show_address' => 'header'
- ];
- return true;
- } else {
- try {
- return true;
- } catch (Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- }
- }
|