FormSetController.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\FormSet;
  4. use Illuminate\Http\Request;
  5. class FormSetController extends Controller
  6. {
  7. protected $redirect_index = '/admin/FormSet/index';
  8. protected $view_path = 'admin.form-sets.';
  9. protected $pre_uri = '/admin/FormSet/';
  10. protected $model_name = '表单设置';
  11. protected $model;
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. $this->model = new FormSet();
  16. }
  17. public function index()
  18. {
  19. $item = $this->model->first();
  20. if(empty($item)) {
  21. $item = $this->model->createDefault();
  22. }
  23. list($pre_uri, $model, $model_name) = array($this->pre_uri, $this->model, $this->model_name);
  24. return view($this->view_path . 'index', compact('pre_uri', 'model', 'model_name', 'item'));
  25. }
  26. public function update(Request $request)
  27. {
  28. if(!$request->isMethod('POST')) {
  29. return $this->showWarning('访问错误');
  30. }
  31. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id'))) || !is_array($request->input('data'))) {
  32. return $this->showWarning('参数错误');
  33. }
  34. $data = $request->input('data');
  35. $values = collect(['show_below_index', 'text_1_status', 'text_1_need', 'text_2_status', 'text_2_need', 'text_3_status', 'text_3_need', 'text_4_status', 'text_4_need', 'multi_text_status', 'multi_text_need', 'radio_status', 'radio_need', 'checkbox_status', 'checkbox_need']);
  36. foreach($values as $value) {
  37. $data[$value] = isset($data[$value]) ? $data[$value] : 2;
  38. }
  39. if(!$item->update($data)) {
  40. return $this->showWarning('数据库保存失败');
  41. }
  42. return $this->showMessage('操作成功');
  43. }
  44. }