VoteBehavior.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: NODELOG
  5. * Date: 16/7/23
  6. * Time: 下午9:00
  7. */
  8. namespace common\behaviors;
  9. use common\models\Vote;
  10. use common\models\VoteInfo;
  11. use common\traits\EntityTrait;
  12. use Yii;
  13. use yii\base\Behavior;
  14. use yii\db\ActiveRecord;
  15. class VoteBehavior extends Behavior
  16. {
  17. use EntityTrait;
  18. /**
  19. * @var \yii\db\ActiveRecord
  20. */
  21. public $owner;
  22. public function events()
  23. {
  24. return [
  25. ActiveRecord::EVENT_AFTER_INSERT => 'afterInsert',
  26. ActiveRecord::EVENT_AFTER_UPDATE => 'afterUpdate',
  27. ActiveRecord::EVENT_AFTER_DELETE => 'afterDelete',
  28. ];
  29. }
  30. public function getVoteModel()
  31. {
  32. }
  33. /**
  34. * 当前用户是否顶
  35. * @return bool
  36. */
  37. public function getIsUp()
  38. {
  39. if (!Yii::$app->user->isGuest) {
  40. $userId = Yii::$app->user->id;
  41. $up = Vote::find()->where(['entity' => $this->entity, 'entity_id' => $this->owner->id, 'user_id' => $userId, 'action' => Vote::ACTION_UP])->one();
  42. if ($up) {
  43. return true;
  44. }
  45. }
  46. return false;
  47. }
  48. /**
  49. * 当前用户是否踩
  50. * @return bool
  51. */
  52. public function getIsDown()
  53. {
  54. if (!Yii::$app->user->isGuest) {
  55. $userId = Yii::$app->user->id;
  56. $down = Vote::find()->where(['entity' => $this->entity, 'entity_id' => $this->owner->id, 'user_id' => $userId, 'action' => Vote::ACTION_DOWN])->one();
  57. if ($down) {
  58. return true;
  59. }
  60. }
  61. return false;
  62. }
  63. public function getVoteInfo()
  64. {
  65. return $this->owner->hasOne(VoteInfo::className(), [
  66. 'entity_id' => $this->owner->primaryKey()[0]
  67. ])->where(['entity' => $this->getEntity()]);
  68. }
  69. public function getUpTotal()
  70. {
  71. $model = $this->owner->voteInfo;
  72. if ($model == null) {
  73. return 0;
  74. }
  75. return $model->up;
  76. }
  77. public function getDownTotal()
  78. {
  79. $model = $this->owner->voteInfo;
  80. if ($model == null) {
  81. return 0;
  82. }
  83. return $model->down;
  84. }
  85. public function afterInsert()
  86. {
  87. $model = new VoteInfo();
  88. $model->entity = $this->getEntity();
  89. $model->entity_id = $this->getEntityId();
  90. $model->save();
  91. }
  92. public function afterUpdate()
  93. {
  94. /* @var $model \common\models\VoteInfo */
  95. $model = $this->owner->VoteInfo;
  96. if ($model == null) {
  97. $model = new VoteInfo();
  98. $model->entity = $this->getEntity();
  99. $model->entity_id = $this->getEntityId();
  100. $model->save();
  101. }
  102. }
  103. public function afterDelete()
  104. {
  105. $entity = $this->getEntity();
  106. $entityId = $this->getEntityId();
  107. VoteInfo::deleteAll(['entity' => $entity, 'entity_id' => $entityId]);
  108. Vote::deleteAll(['entity' => $entity, 'entity_id' => $entityId]);
  109. }
  110. }