Skip to content

Commit

Permalink
关注
Browse files Browse the repository at this point in the history
  • Loading branch information
xutl committed Nov 19, 2017
1 parent 9c387e9 commit dc2878d
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 5 deletions.
8 changes: 4 additions & 4 deletions frontend/views/question/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class="fa fa-database"></i> 追加悬赏</a></li>
</ul>
</div>

<?= \yuncms\comment\frontend\widgets\Comment::widget(['source_type' => 'question', 'source_id' => $model->id, 'hide_cancel' => false]) ?>
<?= \yuncms\comment\frontend\widgets\Comment::widget(['model_class' => 'question', 'model_id' => $model->id, 'hide_cancel' => false]) ?>

<!-- 分享
<div class="mb-10">
Expand Down Expand Up @@ -103,7 +103,7 @@ class="fa fa-thumbs-o-up"></i>
</div>

<!-- 评论 -->
<?= \yuncms\question\frontend\widgets\Comment::widget(['source_type' => 'answer', 'source_id' => $bestAnswer->id, 'hide_cancel' => false]) ?>
<?= \yuncms\comment\frontend\widgets\Comment::widget(['model_class' => 'answer', 'model_id' => $bestAnswer->id, 'hide_cancel' => false]) ?>
<!-- 评论结束 -->
<div class="media user-info border-top">
<div class="media-left">
Expand Down Expand Up @@ -187,7 +187,7 @@ class="text-muted"> - <?= $bestAnswer->user->profile->city ?></span>
<ul class="widget-action list-unstyled">
<!-- 关注部分 -->
<li>
<?php if (!Yii::$app->user->isGuest && Yii::$app->user->identity->isFollowed($model, $model->id)): ?>
<?php if (!Yii::$app->user->isGuest && $model->isFollowed(Yii::$app->user->getId())): ?>
<button type="button" data-target="follow-button" class="btn btn-success btn-sm active"
data-source_type="question" data-source_id="<?= $model->id ?>" data-show_num="true"
data-toggle="tooltip" data-placement="right" title="" data-original-title="关注后将获得更新提醒">
Expand All @@ -204,7 +204,7 @@ class="text-muted"> - <?= $bestAnswer->user->profile->city ?></span>
</li>
<!-- 收藏部分 -->
<li>
<?php if (!Yii::$app->user->isGuest && Yii::$app->user->identity->isCollected($model, $model->id)): ?>
<?php if (!Yii::$app->user->isGuest && $model->isCollected(Yii::$app->user->getId())): ?>
<button type="button" data-target="collect-button" class="btn btn-default btn-sm active"
data-source_type="question" data-source_id="<?= $model->id ?>" data-show_num="true"
data-toggle="tooltip" data-placement="right" title="" data-original-title="关注后将获得更新提醒">
Expand Down
33 changes: 32 additions & 1 deletion models/Question.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
* @property integer $answers
* @property integer $views
* @property integer $votes
* @property integer $followers
* @property integer $collections
* @property integer $status
* @property integer $created_at
* @property integer $updated_at
Expand Down Expand Up @@ -189,7 +191,36 @@ public function getUser()
*/
public function getCollections()
{
return $this->hasMany(Collection::className(), ['model_id' => 'id'])->onCondition(['model' => static::className()]);
return $this->hasMany(Collection::className(), ['model_id' => 'id'])->onCondition(['model_class' => static::className()]);
}

/**
* 是否已经收藏过
* @param int $user_id
* @return bool
*/
public function isCollected($user_id)
{
return $this->getCollections()->andWhere(['user_id' => $user_id])->exists();
}

/**
* Collection Relation
* @return \yii\db\ActiveQueryInterface
*/
public function getAttentions()
{
return $this->hasMany(QuestionAttention::className(), ['model_id' => 'id'])->onCondition(['model_class' => static::className()]);
}

/**
* 是否已关注
* @param int $user_id
* @return mixed
*/
public function isFollowed($user_id)
{
return $this->getAttentions()->andWhere(['user_id' => $user_id])->exists();
}

/**
Expand Down
47 changes: 47 additions & 0 deletions models/QuestionAttention.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* @link http://www.tintsoft.com/
* @copyright Copyright (c) 2012 TintSoft Technology Co. Ltd.
* @license http://www.tintsoft.com/license/
*/
namespace yuncms\question\models;

use Yii;
use yuncms\collection\models\CollectionQuery;

/**
* Class Support
* @property string $subject
* @package yuncms\question\models
*/
class QuestionAttention extends \yuncms\attention\models\Attention
{
const TYPE = 'sixiang\question\models\Question';

/**
* @return void
*/
public function init()
{
$this->model_class = self::TYPE;
parent::init();
}

/**
* @return CollectionQuery
*/
public static function find()
{
return new CollectionQuery(get_called_class(), ['model_class' => self::TYPE, 'tableName' => self::tableName()]);
}

/**
* @param bool $insert
* @return bool
*/
public function beforeSave($insert)
{
$this->model_class = self::TYPE;
return parent::beforeSave($insert);
}
}

0 comments on commit dc2878d

Please sign in to comment.