SubmitFormForm.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\models\Form;
  9. use app\models\Option;
  10. use yii\base\Model;
  11. class SubmitFormForm extends Model
  12. {
  13. public $store_id;
  14. public $form_list;
  15. public $is_form;
  16. public $form_name;
  17. public function rules()
  18. {
  19. return [
  20. [['form_list'], 'safe'],
  21. [['is_form'], 'integer'],
  22. [['form_name'], 'trim'],
  23. [['form_name'], 'string','max' => 250],
  24. [['form_name'], 'default', 'value' => '我的表单'],
  25. [['is_form'], 'default', 'value' => 0],
  26. ];
  27. }
  28. public function save()
  29. {
  30. if (!$this->validate()) {
  31. return $this->getErrorSummary(false)[0];
  32. }
  33. $t = \Yii::$app->db->beginTransaction();
  34. Form::updateAll(['is_delete' => 1], ['store_id' => $this->store_id]);
  35. if ($this->is_form == 1) {
  36. if (!$this->form_list) {
  37. return [
  38. 'code'=>1,
  39. 'msg'=>'请填写表单设置'
  40. ];
  41. }
  42. }
  43. if ($this->form_list) {
  44. $this->form_list = array_values($this->form_list);
  45. foreach ($this->form_list as $index => $value) {
  46. if (!$value['name']) {
  47. return [
  48. 'code' => 1,
  49. 'msg' => '请输入字段名称'
  50. ];
  51. }
  52. if (in_array($value['type'], ['radio', 'checkbox'])) {
  53. if (!$value['default']) {
  54. return [
  55. 'code' => 1,
  56. 'msg' => '请输入单选或多选的默认值'
  57. ];
  58. }
  59. }
  60. if ($value['id']) {
  61. $form = Form::findOne(['store_id' => $this->store_id, 'id' => $value['id']]);
  62. } else {
  63. $form = new Form();
  64. }
  65. $form->is_delete = 0;
  66. $form->created_at = time();
  67. $form->type = $value['type'];
  68. $form->name = $value['name'];
  69. $form->default = $value['default'];
  70. $form->required = $value['required'] ? $value['required'] : 0;
  71. $form->tip = $value['tip'];
  72. $form->sort = $index;
  73. $form->store_id = $this->store_id;
  74. if(!$form->save()){
  75. $t->rollBack();
  76. return [
  77. 'code' => 1,
  78. 'msg' => $form->errors[0]
  79. ];
  80. }
  81. }
  82. }
  83. $t->commit();
  84. Option::set('is_form', $this->is_form, $this->store_id, 'store');
  85. Option::set('form_name', $this->form_name, $this->store_id, 'store');
  86. return [
  87. 'code' => 0,
  88. 'msg' => '成功'
  89. ];
  90. }
  91. }