AddonsSmartFormTemplateColumn.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. namespace addons\SmartForm\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\mall\MallAddons;
  5. use common\models\user\User;
  6. use Yii;
  7. use yii\db\Exception;
  8. use addons\SmartForm\common\enums\SmartFormEnum;
  9. use yii\helpers\Json;
  10. /**
  11. * This is the model class for table "{{%addons_smart_form_template_column}}".
  12. *
  13. * @property int $id
  14. * @property int $mall_id 商城ID
  15. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  16. * @property int $created_at 创建时间
  17. * @property int $updated_at 修改时间
  18. * @property array|null $extra_data 修改时间
  19. */
  20. class AddonsSmartFormTemplateColumn extends \common\models\base\BaseActiveRecord
  21. {
  22. const ADDONS_NAME = 'SmartForm';
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function tableName()
  27. {
  28. return '{{%addons_smart_form_template_column}}';
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['smart_form_template_id','smart_form_components_id','title'], 'required'],
  37. [['smart_form_template_id','smart_form_components_id', 'required', 'limit', 'sort', 'is_export', 'content_type', 'status', 'created_at', 'updated_at'], 'integer'],
  38. [['default'], 'string', 'max' => 255],
  39. [['title','tips'], 'string', 'max' => 100],
  40. [['placeholder'], 'string'],
  41. [['smart_form_components_code'], 'string', 'max' => 30],
  42. [['extra_data'], 'safe'],
  43. ];
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function attributeLabels()
  49. {
  50. return [
  51. 'id' => '主键',
  52. 'smart_form_template_id' => '智能装修表单id',
  53. 'smart_form_components_code' => '智能装修组件标识',
  54. 'smart_form_components_id' => '智能装修组件id',
  55. 'title' => '标题',
  56. 'tips' => '提示语',
  57. 'required' => '是否必填(1:是;2:否)',
  58. 'limit' => '限制长度',
  59. 'show_default' => '显示默认值[0:不显示;1:显示]',
  60. 'is_export' => '是否显示一键导入功能:0:否;1是',
  61. 'content_type' => '文本类型 0:默认 1:姓名 2:手机 3:详细地址',
  62. 'default' => '默认值',
  63. 'sort' => '排序,越小排越前',
  64. 'status' => '状态[0:禁用;1:启用,2:已过期]',
  65. 'created_at' => '添加时间',
  66. 'updated_at' => '修改时间 ',
  67. ];
  68. }
  69. /**
  70. * @method FunctionName
  71. * 保存模板字段
  72. * @author zqh
  73. * @datetime 2022-05-06T02:18:13+0800
  74. * @param array $params [description]
  75. */
  76. public static function createColumn($params=[])
  77. {
  78. # 判断当前模板是否已经有字段
  79. $where['status'] = StatusEnum::ENABLED;
  80. $where['smart_form_template_id'] = $params['template_id'];
  81. $list = self::getOne([$where],true);
  82. if($list){
  83. # 存在则删除
  84. $count = self::deleteAll(['smart_form_template_id' => $params['template_id']]);
  85. }
  86. $template_id = $params['template_id']; # 模板id
  87. unset($params['template_id']);
  88. $insert = [];
  89. # 遍历字段数组
  90. foreach ($params as $ck => $cv) {
  91. $arr = [];
  92. $arr['smart_form_template_id'] = $template_id; # 模板id
  93. $arr['smart_form_components_code'] = $cv['type']; # 组件id
  94. $arr['smart_form_components_id'] = $cv['components_id']; # 组件id
  95. $arr['title'] = $cv['title'] ? $cv['title'] : ''; # 标题
  96. $arr['tips'] = $cv['tips'] ? $cv['tips'] : ''; # 提示语
  97. $arr['placeholder'] = $cv['placeholder'] ? : ''; # 简短提示语
  98. $arr['required'] = $cv['required'] ? $cv['required'] : 0; # 是否必填
  99. $arr['limit'] = $cv['total'] ?? 999; # 限制长度
  100. $arr['status'] = StatusEnum::ENABLED;
  101. $arr['option'] = '';# 下拉选项
  102. $arr['show_default'] = 0;
  103. $arr['is_export'] = $cv['is_export'] ?? 0; # 是否显示一键导入功能
  104. $arr['content_type'] = $cv['content_type'] ?? 0; # 文本类型
  105. $arr['extra_data'] = Json::encode($cv['extra_data'] ?? []);
  106. if (mb_strlen($arr['title']) > 100) {
  107. self::$static_error = '标题长度不能超过100';
  108. return false;
  109. }
  110. if (mb_strlen($arr['tips']) > 100) {
  111. self::$static_error = '提示语长度不能超过100';
  112. return false;
  113. }
  114. # 存在下拉选项
  115. if(isset($cv['chooseList']) && !empty($cv['chooseList'])){
  116. $arr['option']= implode(SmartFormEnum::FIRST_CUTTER,array_column($cv['chooseList'],'title'));
  117. }
  118. $arr['default'] = '';
  119. switch ($cv['type']) {
  120. case 'date-tool': # 日期类型
  121. if(isset($cv['is_show']) && $cv['is_show'])
  122. {
  123. $arr['show_default'] = $cv['is_show'];
  124. if(isset($cv['show_now_day']) && $cv['show_now_day']){
  125. # 显示当天日期
  126. $arr['default'] = 'func' . SmartFormEnum::PARAM_CUTTER . 'showDate' . SmartFormEnum::PARAM_CUTTER . 'today';
  127. }else{
  128. # 自定义日期
  129. $arr['default'] = $cv['date'];
  130. }
  131. }else{
  132. $arr['default'] = '';
  133. }
  134. break;
  135. case 'time-tool': # 时间类型
  136. if(isset($cv['is_show']) && $cv['is_show'])
  137. {
  138. $arr['show_default'] = $cv['is_show'];
  139. if(isset($cv['show_now_time']) && $cv['show_now_time']){
  140. # 显示当天日期
  141. $arr['default'] = 'func' . SmartFormEnum::PARAM_CUTTER . 'showTime' . SmartFormEnum::PARAM_CUTTER . 'now';
  142. }else{
  143. # 自定义日期
  144. $arr['default'] = $cv['time'];
  145. }
  146. }else{
  147. $arr['default'] = '';
  148. }
  149. break;
  150. case 'date-range': # 日期范围类型
  151. # 显示开始日期
  152. if(isset($cv['is_start_show']) && $cv['is_start_show'])
  153. {
  154. $arr['show_default'] = $cv['is_start_show'];
  155. if(isset($cv['show_now_start']) && $cv['show_now_start']){
  156. # 显示当天日期
  157. $arr['default'] = 'func' . SmartFormEnum::PARAM_CUTTER . 'showDate' . SmartFormEnum::PARAM_CUTTER . 'today';
  158. }else{
  159. # 自定义日期
  160. $arr['default'] = $cv['start_date'];
  161. }
  162. }else{
  163. $arr['default'] = '';
  164. $arr['show_default'] = 0;
  165. }
  166. # 显示结束日期
  167. if(isset($cv['is_end_show']) && $cv['is_end_show'])
  168. {
  169. $arr['show_default'] .= SmartFormEnum::FIRST_CUTTER . $cv['is_end_show'];
  170. if(isset($cv['show_now_end']) && $cv['show_now_end']){
  171. # 显示当天日期
  172. $arr['default'] .= SmartFormEnum::DATE_CUTTER . 'func' . SmartFormEnum::PARAM_CUTTER . 'showDate' . SmartFormEnum::PARAM_CUTTER . 'today';
  173. }else{
  174. # 自定义日期
  175. $arr['default'] .= SmartFormEnum::DATE_CUTTER . $cv['end_date'];
  176. }
  177. }else{
  178. $arr['default'] .= SmartFormEnum::DATE_CUTTER . '';
  179. $arr['show_default'] .= SmartFormEnum::FIRST_CUTTER . 0;
  180. }
  181. # 提示语
  182. $arr['tips'] = $cv['startTitle'] . SmartFormEnum::FIRST_CUTTER . $cv['endTitle'];
  183. break;
  184. case 'time-range': # 时间范围类型
  185. # 显示开始日期
  186. if(isset($cv['is_start_show']) && $cv['is_start_show'])
  187. {
  188. $arr['show_default'] = $cv['is_start_show'];
  189. if(isset($cv['show_now_start']) && $cv['show_now_start']){
  190. # 显示当天日期
  191. $arr['default'] = 'func' . SmartFormEnum::PARAM_CUTTER . 'showTime' . SmartFormEnum::PARAM_CUTTER . 'now';
  192. }else{
  193. # 自定义日期
  194. $arr['default'] = $cv['start_time'];
  195. }
  196. }else{
  197. $arr['default'] = '';
  198. $arr['show_default'] = 0;
  199. }
  200. # 显示结束日期
  201. if(isset($cv['is_end_show']) && $cv['is_end_show'])
  202. {
  203. $arr['show_default'] .= SmartFormEnum::FIRST_CUTTER . $cv['is_end_show'];
  204. if(isset($cv['show_now_end']) && $cv['show_now_end']){
  205. # 显示当天日期
  206. $arr['default'] .= SmartFormEnum::DATE_CUTTER . 'func' . SmartFormEnum::PARAM_CUTTER . 'showTime' . SmartFormEnum::PARAM_CUTTER . 'now';
  207. }else{
  208. # 自定义日期
  209. $arr['default'] .= SmartFormEnum::DATE_CUTTER . $cv['end_time'];
  210. }
  211. }else{
  212. $arr['default'] .= SmartFormEnum::DATE_CUTTER . '';
  213. $arr['show_default'] .= SmartFormEnum::FIRST_CUTTER . 0;
  214. }
  215. # 提示语
  216. $arr['tips'] = $cv['startTitle'] . SmartFormEnum::FIRST_CUTTER . $cv['endTitle'];
  217. break;
  218. case 'city-chose': # 省市区
  219. $arr['default'] = $cv['city_type'];
  220. break;
  221. }
  222. $arr['sort'] = $ck;
  223. $arr['created_at'] = time();
  224. $arr['updated_at'] = time();
  225. $insert[] = $arr;
  226. }
  227. # 批量插入数据
  228. # 字段
  229. $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'];
  230. $res = Yii::$app->db->createCommand()->batchInsert(self::tableName(), $field, $insert)->execute();
  231. if ($res == false) {
  232. self::$static_error = '写入模板字段失败';
  233. return false;
  234. }
  235. return $res;
  236. }
  237. }