ArticleForm.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace common\models\forms;
  3. use common\helpers\Date;
  4. use common\components\Model;
  5. use common\helpers\Form;
  6. use common\models\Article;
  7. use common\models\ArticleCategory;
  8. use yii\base\Exception;
  9. /**
  10. * Login form
  11. */
  12. class ArticleForm extends Model
  13. {
  14. public $id;
  15. public $title;
  16. public $cid;
  17. public $sort;
  18. public $content;
  19. public $countryId;
  20. /**
  21. * @inheritdoc
  22. */
  23. public function rules()
  24. {
  25. return [
  26. [['id', 'title', 'cid', 'content'], 'trim'],
  27. [['id', 'title', 'cid', 'content', 'countryId'], 'required'],
  28. [['countryId'], 'string'],
  29. [['id'], 'exist', 'targetClass'=>Article::class, 'targetAttribute'=>'ID'],
  30. [['cid'], 'exist', 'targetClass'=>ArticleCategory::class, 'targetAttribute'=>'ID'],
  31. ];
  32. }
  33. public function attributeLabels()
  34. {
  35. return [
  36. 'id' => 'ID',
  37. 'title' => 'Title',//标题
  38. 'cid' => 'Type', // 分类
  39. 'content' => 'Content', // 内容
  40. 'sort' => 'Sort', // 排序
  41. 'countryId' => 'Country', // 国家
  42. ];
  43. }
  44. /**
  45. * 指定校验场景
  46. * @return array
  47. */
  48. public function scenarios()
  49. {
  50. $parentScenarios = parent::scenarios();
  51. $customScenarios = [
  52. 'add' => ['title', 'cid', 'content', 'sort', 'countryId'],
  53. 'edit' => ['id','title', 'cid', 'content', 'sort', 'countryId'],
  54. 'sort' => ['id', 'sort'],
  55. ];
  56. return array_merge($parentScenarios, $customScenarios);
  57. }
  58. /**
  59. * 申请
  60. * @return Article|null
  61. * @throws \yii\db\Exception
  62. */
  63. public function edit()
  64. {
  65. if(!$this->validate()){
  66. return null;
  67. }
  68. $db = \Yii::$app->db;
  69. $transaction = $db->beginTransaction();
  70. try {
  71. if($this->scenario == 'add'){
  72. $model = new Article();
  73. $model->CREATED_AT = Date::nowTime();
  74. } elseif($this->scenario == 'edit') {
  75. $model = Article::findOne(['ID'=>$this->id]);
  76. } elseif($this->scenario == 'sort') {
  77. $model = Article::findOne(['ID'=>$this->id]);
  78. } else {
  79. $this->addError('edit', \Yii::t('ctx', 'sceneDoesNotExist'));// 提交场景不存在
  80. return null;
  81. }
  82. $model->TITLE = $this->title;
  83. $model->CID = $this->cid;
  84. $model->CONTENT = '';
  85. $model->STATUS = 1;
  86. $model->SORT = $this->sort;
  87. $model->COUNTRY_ID = $this->countryId;
  88. if(!$model->save()){
  89. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  90. }
  91. // 暂时把内容写入到文件中
  92. $path = \Yii::getAlias('@common/runtime/articleContent/').$model->ID;
  93. file_put_contents($path, $this->content);
  94. $transaction->commit();
  95. } catch (Exception $e) {
  96. $transaction->rollBack();
  97. $this->addError('edit', $e->getMessage());
  98. return null;
  99. }
  100. return $model;
  101. }
  102. /**
  103. * 排序
  104. * @return Article
  105. */
  106. public function sortTo(): ?Article
  107. {
  108. if (!$this->validate()) {
  109. return null;
  110. }
  111. $model = Article::findOne(['ID' => $this->id]);
  112. $model->SORT = $this->sort;
  113. if (!$model->save()) {
  114. $this->addError('sortTo', Form::formatErrorsForApi($model->getErrors()));
  115. return null;
  116. }
  117. return $model;
  118. }
  119. }