ProtocolController.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace addons\TinyShop\merchant\modules\common\controllers;
  3. use Yii;
  4. use addons\TinyShop\common\enums\ProtocolNameEnum;
  5. use addons\TinyShop\common\models\common\Protocol;
  6. use addons\TinyShop\merchant\controllers\BaseController;
  7. /**
  8. * 协议管理
  9. *
  10. * Class ProtocolController
  11. * @package addons\TinyShop\merchant\modules\common\controllers
  12. * @author jianyan74 <751393839@qq.com>
  13. */
  14. class ProtocolController extends BaseController
  15. {
  16. /**
  17. * @var Protocol
  18. */
  19. public $modelClass = Protocol::class;
  20. /**
  21. * @return string
  22. */
  23. public function actionIndex()
  24. {
  25. return $this->render($this->action->id, [
  26. 'protocolNameMap' => ProtocolNameEnum::getMap()
  27. ]);
  28. }
  29. /**
  30. * 编辑/创建
  31. *
  32. * @return mixed
  33. */
  34. public function actionEdit()
  35. {
  36. $name = Yii::$app->request->get('name', null);
  37. $model = $this->findModelByName($name);
  38. $model->title = ProtocolNameEnum::getValue($name);
  39. $model->name = $name;
  40. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  41. return $this->referrer();
  42. }
  43. return $this->render($this->action->id, [
  44. 'model' => $model,
  45. ]);
  46. }
  47. /**
  48. * 返回模型
  49. *
  50. * @param $id
  51. * @return \yii\db\ActiveRecord
  52. */
  53. protected function findModelByName($name)
  54. {
  55. /* @var $model \yii\db\ActiveRecord */
  56. if (empty($name) || empty($model = $this->modelClass::find()
  57. ->where(['name' => $name])
  58. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  59. ->one())
  60. ) {
  61. $model = new $this->modelClass;
  62. return $model->loadDefaultValues();
  63. }
  64. return $model;
  65. }
  66. }
粤ICP备19079148号