CommentBehavior.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: NODELOG
  5. * Date: 2016/12/19
  6. * Time: 下午4:20
  7. */
  8. namespace common\behaviors;
  9. use common\models\Comment;
  10. use common\models\CommentInfo;
  11. use common\traits\EntityTrait;
  12. use Yii;
  13. use yii\base\Behavior;
  14. use yii\db\ActiveRecord;
  15. use yii\validators\Validator;
  16. class CommentBehavior extends Behavior
  17. {
  18. use EntityTrait;
  19. /**
  20. * @var \yii\db\ActiveRecord
  21. */
  22. public $owner;
  23. public $defaultStatus = 1;
  24. public function events()
  25. {
  26. return [
  27. ActiveRecord::EVENT_INIT => 'addRules',
  28. ActiveRecord::EVENT_AFTER_INSERT => 'afterInsert',
  29. ActiveRecord::EVENT_AFTER_UPDATE => 'afterUpdate',
  30. ActiveRecord::EVENT_AFTER_DELETE => 'afterDelete',
  31. ];
  32. }
  33. public function addRules()
  34. {
  35. $this->addRule('commentEnabled', 'in', ['range' => [0, 1]]);
  36. }
  37. public function addRule($attributes, $validator, $options = [])
  38. {
  39. $validators = $this->owner->getValidators();
  40. $validators->append(Validator::createValidator($validator, $this->owner, (array) $attributes, $options));
  41. }
  42. public function afterInsert()
  43. {
  44. if ($this->_commentEnabled != null) {
  45. $status = $this->_commentEnabled;
  46. if ($status == $this->defaultStatus) {
  47. return;
  48. }
  49. $model = new CommentInfo();
  50. $model->entity = $this->getEntity();
  51. $model->entity_id = $this->getEntityId();
  52. $model->status = $status;
  53. $model->save();
  54. }
  55. }
  56. public function afterUpdate()
  57. {
  58. if ($this->_commentEnabled != null) {
  59. $status = $this->_commentEnabled;
  60. /* @var $model \common\models\CommentInfo */
  61. $model = $this->owner->commentInfo;
  62. if ($model == null) {
  63. if ($status == $this->defaultStatus) {
  64. return;
  65. }
  66. $model = new CommentInfo();
  67. $model->entity = $this->getEntity();
  68. $model->entity_id = $this->getEntityId();
  69. $model->status = $status;
  70. $model->save();
  71. } else {
  72. $model->status = $status;
  73. $model->save();
  74. }
  75. }
  76. }
  77. public function afterDelete()
  78. {
  79. $entity = $this->getEntity();
  80. $entityId = $this->getEntityId();
  81. CommentInfo::deleteAll(['entity' => $entity, 'entity_id' => $entityId]);
  82. Comment::deleteAll(['entity' => $entity, 'entity_id' => $entityId]);
  83. }
  84. public function getCommentInfo()
  85. {
  86. return $this->owner->hasOne(CommentInfo::className(), [
  87. 'entity_id' => $this->owner->primaryKey()[0]
  88. ])->where(['entity' => $this->getEntity()]);
  89. }
  90. public function getCommentTotal()
  91. {
  92. $model = $this->owner->commentInfo;
  93. if ($model == null) {
  94. return 0;
  95. }
  96. return $model->total;
  97. }
  98. private $_commentEnabled;
  99. public function getCommentEnabled()
  100. {
  101. if ($this->_commentEnabled != null) {
  102. return $this->_commentEnabled;
  103. }
  104. $model = $this->owner->commentInfo;
  105. if ($model == null || $model->status === null) {
  106. return $this->defaultStatus;
  107. }
  108. return $model->status;
  109. }
  110. public function setCommentEnabled($commentEnabled)
  111. {
  112. $this->_commentEnabled = $commentEnabled;
  113. }
  114. }