ArticleForm.php 3.4 KB

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