Comment.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2017-03-15 21:16
  7. */
  8. namespace common\models;
  9. use Yii;
  10. use yii\behaviors\TimestampBehavior;
  11. use yii\helpers\Html;
  12. /**
  13. * This is the model class for table "{{%comment}}".
  14. *
  15. * @property integer $id
  16. * @property integer $aid
  17. * @property integer $uid
  18. * @property integer $nickname
  19. * @property string $content
  20. * @property string $email
  21. * @property string $website_url
  22. * @property integer $reply_to
  23. * @property string $ip
  24. * @property integer $status
  25. * @property integer $created_at
  26. * @property integer $updated_at
  27. * @property Article $article
  28. */
  29. class Comment extends \yii\db\ActiveRecord
  30. {
  31. const STATUS_INIT = 0;
  32. const STATUS_PASSED = 1;
  33. const STATUS_NOT_PASS = 2;
  34. /**
  35. * @inheritdoc
  36. */
  37. public static function tableName()
  38. {
  39. return '{{%comment}}';
  40. }
  41. public function behaviors()
  42. {
  43. return [
  44. TimestampBehavior::className(),
  45. ];
  46. }
  47. /**
  48. * @inheritdoc
  49. */
  50. public function rules()
  51. {
  52. return [
  53. [['aid', 'uid', 'reply_to', 'status', 'created_at', 'updated_at'], 'integer'],
  54. [['content'], 'required'],
  55. [['content', 'nickname', 'email', 'website_url'], 'string'],
  56. [['ip'], 'string', 'max' => 255],
  57. ];
  58. }
  59. /**
  60. * @inheritdoc
  61. */
  62. public function attributeLabels()
  63. {
  64. return [
  65. 'id' => Yii::t('app', 'ID'),
  66. 'aid' => Yii::t('app', 'Article Id'),
  67. 'uid' => Yii::t('app', 'User Id'),
  68. 'nickname' => Yii::t('app', 'Nickname'),
  69. 'content' => Yii::t('app', 'Comment Message'),
  70. 'reply_to' => Yii::t('app', 'Replay User Id'),
  71. 'ip' => Yii::t('app', 'IP Address'),
  72. 'status' => Yii::t('app', 'Status'),
  73. 'email' => Yii::t('app', 'Email'),
  74. 'website_url' => Yii::t('app', 'Website'),
  75. 'admin_id' => Yii::t('app', 'Admin User Id'),
  76. 'created_at' => Yii::t('app', 'Created At'),
  77. 'updated_at' => Yii::t('app', 'Updated At'),
  78. ];
  79. }
  80. /**
  81. * @return \yii\db\ActiveQuery
  82. */
  83. public function getArticle()
  84. {
  85. return $this->hasOne(Article::className(), ['id' => 'aid']);
  86. }
  87. /**
  88. * @param $id
  89. * @return array
  90. */
  91. public function getCommentByAid($id)
  92. {
  93. $list = self::find()
  94. ->where(['aid' => $id, 'status' => self::STATUS_PASSED])
  95. ->orderBy("id desc,reply_to desc")
  96. ->all();
  97. $newList = [];
  98. foreach ($list as $row) {
  99. $v = $row->getAttributes();
  100. if ($v['reply_to'] == 0) {
  101. $v['sub'] = self::getCommentChildren($list, $v['id']);
  102. $newList[] = $v;
  103. }
  104. }
  105. return $newList;
  106. }
  107. /**
  108. * @param $list
  109. * @param $curId
  110. * @return array
  111. */
  112. public static function getCommentChildren($list, $curId)
  113. {
  114. $subComment = [];
  115. foreach ($list as $v) {
  116. if ($v['reply_to'] == $curId) {
  117. $subComment[] = $v;
  118. }
  119. }
  120. return $subComment;
  121. }
  122. /**
  123. * @inheritdoc
  124. */
  125. public function afterFind()
  126. {
  127. $this->content = str_replace([
  128. ':mrgreen:',
  129. ':razz:',
  130. ':sad:',
  131. ':smile:',
  132. ':oops:',
  133. ':grin:',
  134. ':eek:',
  135. ':???:',
  136. ':cool:',
  137. ':lol:',
  138. ':mad:',
  139. ':twisted:',
  140. ':roll:',
  141. ':wink:',
  142. ':idea:',
  143. ':arrow:',
  144. ':neutral:',
  145. ':cry:',
  146. ':?:',
  147. ':evil:',
  148. ':shock:',
  149. ':!:'
  150. ], [
  151. "<img src='{%URL%}mrgreen{%EXT%}'>",
  152. "<img src='{%URL%}razz{%EXT%}'>",
  153. "<img src='{%URL%}sad{%EXT%}'>",
  154. "<img src='{%URL%}smile{%EXT%}'>",
  155. "<img src='{%URL%}redface{%EXT%}'>",
  156. "<img src='{%URL%}biggrin{%EXT%}'>",
  157. "<img src='{%URL%}surprised{%EXT%}'>",
  158. "<img src='{%URL%}confused{%EXT%}'>",
  159. "<img src='{%URL%}cool{%EXT%}'>",
  160. "<img src='{%URL%}lol{%EXT%}'>",
  161. "<img src='{%URL%}mad{%EXT%}'>",
  162. "<img src='{%URL%}twisted{%EXT%}'>",
  163. "<img src='{%URL%}rolleyes{%EXT%}'>",
  164. "<img src='{%URL%}wink{%EXT%}'>",
  165. "<img src='{%URL%}idea{%EXT%}'>",
  166. "<img src='{%URL%}arrow{%EXT%}'>",
  167. "<img src='{%URL%}neutral{%EXT%}'>",
  168. "<img src='{%URL%}cry{%EXT%}'>",
  169. "<img src='{%URL%}question{%EXT%}'>",
  170. "<img src='{%URL%}evil{%EXT%}'>",
  171. "<img src='{%URL%}eek{%EXT%}'>",
  172. "<img src='{%URL%}exclaim{%EXT%}'>"
  173. ], $this->content);
  174. $this->content = str_replace([
  175. '{%URL%}',
  176. '{%EXT%}'
  177. ], [Yii::$app->params['site']['url'] . 'static/images/smilies/icon_', '.gif'], $this->content);
  178. parent::afterFind();
  179. }
  180. public function afterDelete()
  181. {
  182. $model = Article::findOne($this->aid);
  183. $model->comment_count -= 1;
  184. $model->save(false);
  185. parent::afterDelete();
  186. }
  187. /**
  188. * @inheritdoc
  189. */
  190. public function beforeSave($insert)
  191. {
  192. if ($insert) {
  193. if (Yii::$app->feehi->website_comment) {
  194. if (! Article::find()->where(['id' => $this->aid])->one()['can_comment']) {
  195. $this->addError('content', Yii::t('frontend', 'This article is not allowed to comment'));
  196. return false;
  197. }
  198. if (Yii::$app->feehi->website_comment_need_verify) {
  199. $this->status = self::STATUS_INIT;
  200. } else {
  201. $this->status = self::STATUS_PASSED;
  202. }
  203. $this->ip = Yii::$app->getRequest()->getUserIP();
  204. $this->uid = Yii::$app->getUser()->getIsGuest() ? 0 : Yii::$app->getUser()->getId();
  205. } else {
  206. $this->addError('content', Yii::t('frontend', 'Website closed comment'));
  207. return false;
  208. }
  209. }
  210. $this->nickname = Html::encode($this->nickname);
  211. $this->email = Html::encode($this->email);
  212. if (stripos($this->website_url, 'http://') !== 0 && stripos($this->website_url, 'https://') !== 0) {
  213. $this->website_url = "http://" . $this->website_url;
  214. }
  215. $this->website_url = Html::encode($this->website_url);
  216. $this->content = Html::encode($this->content);
  217. return parent::beforeSave($insert);
  218. }
  219. /**
  220. * @inheritdoc
  221. */
  222. public function afterSave($insert, $changedAttributes)
  223. {
  224. if ($insert) {
  225. $model = Article::findOne($this->aid);
  226. $model->comment_count += 1;
  227. $model->save(false);
  228. }
  229. parent::afterSave($insert, $changedAttributes);
  230. }
  231. }