Comment.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. namespace common\models;
  3. use backend\models\search\SearchModelTrait;
  4. use common\behaviors\VoteBehavior;
  5. use common\modules\user\behaviors\UserBehavior;
  6. use common\modules\user\models\User;
  7. use yii\behaviors\BlameableBehavior;
  8. use yii\behaviors\TimestampBehavior;
  9. use yii\helpers\Html;
  10. use common\behaviors\UserBehaviorBehavior;
  11. use Yii;
  12. /**
  13. * This is the model class for table "{{%comment}}".
  14. *
  15. * @property int $id
  16. * @property int $user_id
  17. * @property string $user_ip
  18. * @property string $content
  19. * @property string $entity
  20. * @property int $entity_id
  21. * @property int $parent_id
  22. * @property int $reply_uid
  23. * @property int $created_at
  24. * @property Comment $parent
  25. */
  26. class Comment extends \yii\db\ActiveRecord
  27. {
  28. use SearchModelTrait;
  29. const PAGE_SIZE = 10;
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public static function tableName()
  34. {
  35. return '{{%comment}}';
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function rules()
  41. {
  42. return [
  43. [['entity', 'entity_id', 'content'], 'required'],
  44. [['entity_id', 'user_id', 'parent_id', 'is_top', 'parent_id', 'reply_uid'], 'integer'],
  45. [['content'], 'string'],
  46. ['content', 'setReplyUid'],
  47. ];
  48. }
  49. public function setReplyUid($attribute)
  50. {
  51. if (preg_match('/@(\S+?)\s/', $this->$attribute, $matches) > 0) {
  52. $replyUserName = $matches[1];
  53. $replyUserId = User::find()->select('id')->where(['username' => $replyUserName])->scalar();
  54. $this->reply_uid = $replyUserId;
  55. }
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function attributeLabels()
  61. {
  62. return [
  63. 'id' => 'ID',
  64. 'entity' => '来源',
  65. 'entity_id' => '来源ID',
  66. 'user_id' => '评论人',
  67. 'user_ip' => 'IP',
  68. 'content' => '内容',
  69. 'up' => '顶',
  70. 'down' => '踩',
  71. 'is_top' => '是否置顶',
  72. 'status' => '状态',
  73. 'created_at' => '创建时间',
  74. 'updated_at' => '更新时间',
  75. 'parent_id' => '父评论'
  76. ];
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function behaviors()
  82. {
  83. return [
  84. TimestampBehavior::className(),
  85. [
  86. 'class' => BlameableBehavior::className(),
  87. 'createdByAttribute' => 'user_id',
  88. 'updatedByAttribute' => false
  89. ],
  90. VoteBehavior::className(),
  91. UserBehavior::className(),
  92. [
  93. 'class' => UserBehaviorBehavior::className(),
  94. 'eventName' => [self::EVENT_AFTER_INSERT],
  95. 'name' => 'comment',
  96. 'rule' => [
  97. 'cycle' => 24,
  98. 'max' => 10,
  99. 'counter' => 5,
  100. ],
  101. 'content' => '{user.username}在{extra.time}评论',
  102. 'data' => [
  103. 'extra' => [
  104. 'time' => date('Y-m-d H:i:s')
  105. ]
  106. ]
  107. ]
  108. ];
  109. }
  110. /**
  111. * 获取所有子评论.
  112. *
  113. * @return \yii\db\ActiveQuery
  114. */
  115. public function getSons()
  116. {
  117. return $this->hasMany(self::className(), ['parent_id' => 'id'])->where(['status' => 1]);
  118. }
  119. public function getParent()
  120. {
  121. return $this->hasOne(self::className(), ['id' => 'parent_id']);
  122. }
  123. public function transactions()
  124. {
  125. return [
  126. self::SCENARIO_DEFAULT => self::OP_ALL,
  127. ];
  128. }
  129. public static function process($data)
  130. {
  131. preg_match('/@(\S+?)\s/', $data, $matches);
  132. if (!empty($matches)) {
  133. $replyUserName = $matches[1];
  134. $replyUserId = User::find()->select('id')->where(['username' => $replyUserName])->scalar();
  135. $data = preg_replace('/(@\S+?\s)/', Html::a('$1', ['/user/default/index', 'id' => $replyUserId]), $data);
  136. }
  137. return $data;
  138. }
  139. public function beforeSave($insert)
  140. {
  141. if (parent::beforeSave($insert)) {
  142. if ($insert == true) {
  143. if ($this->parent_id) {
  144. $this->reply_uid = $this->parent->user_id;
  145. }
  146. $this->user_ip = Yii::$app->getRequest()->getUserIP();
  147. return true;
  148. }
  149. return true;
  150. }
  151. return false;
  152. }
  153. public function afterSave($insert, $changedAttributes)
  154. {
  155. parent::afterSave($insert, $changedAttributes);
  156. if ($insert) {
  157. $this->updateCommentTotal();
  158. return true;
  159. }
  160. return true;
  161. }
  162. public function afterDelete()
  163. {
  164. parent::afterDelete();
  165. $this->updateCommentTotal();
  166. }
  167. public function updateCommentTotal()
  168. {
  169. $model = CommentInfo::find()->where(['entity' => $this->entity, 'entity_id' => $this->entity_id])->one();
  170. $total = Comment::activeCount($this->entity, $this->entity_id);
  171. if($model == null && $total != 0) {
  172. $model = new CommentInfo();
  173. $model->entity =$this->entity;
  174. $model->entity_id = $this->entity_id;
  175. $model->total =$total;
  176. $model->save();
  177. } else {
  178. $model->total = $total;
  179. $model->save();
  180. }
  181. }
  182. public static function activeCount($entity, $entity_id = NULL)
  183. {
  184. return self::find()->where([
  185. 'entity' => $entity,
  186. 'entity_id' => $entity_id,
  187. 'status' => 1
  188. ])->count();
  189. }
  190. /**
  191. * 关联实体列表
  192. * @return array
  193. * @author nodelog
  194. */
  195. public static function getEntityList()
  196. {
  197. return [
  198. 'clock' => Clock::className(),//朋友圈
  199. 'goods' => Goods::className(),//商品
  200. 'article' => Article::className(),//文章
  201. ];
  202. }
  203. }