Example #1
0
 /**
  * 获取最近的 {$limit} 条评论
  * @param int $limit
  * @param bool|false $refresh
  * @param int $size
  * @return array|mixed|null
  */
 public static function getRecentComments($limit = 10, $refresh = false, $size = 40)
 {
     $cache_key = '__recentComments';
     $limit = intval($limit) ? intval($limit) : 10;
     if ($refresh) {
         $comments = null;
     } else {
         $comments = Yii::$app->cache->get($cache_key);
     }
     if (empty($comments)) {
         /* @var self[] $post_comments */
         $post_comments = self::find()->with('post')->limit($limit)->all();
         $ids = $avatars = $urls = array();
         foreach ($post_comments as $comment) {
             $ids[$comment->pid] = $comment->pid;
             if (!isset($avatars[$comment->id])) {
                 $avatars[$comment->id] = XUtils::getAvatar($comment->email, $size);
             }
         }
         foreach ($post_comments as $comment) {
             $comments[] = ['id' => $comment->id, 'author' => $comment->author, 'author_url' => $comment->url, 'pid' => $comment->pid, 'post_url' => $comment->post->getUrl(true), 'content' => $comment->content, 'create_time' => $comment->create_time, 'email' => $comment->email, 'avatar' => $avatars[$comment->id], 'title' => $comment->post ? $comment->post->title : ''];
         }
         $dp = new DbDependency();
         $dp->sql = (new Query())->select('MAX(update_time)')->from(self::tableName())->createCommand()->rawSql;
         Yii::$app->cache->set($cache_key, $comments, 3600, $dp);
     }
     return $comments;
 }
use app\components\XUtils;
use app\models\User;
use app\modules\admin\widgets\Menu;
/**
 * @var View $this
 * @var User $current_user
 */
$current_url = Url::current();
$current_user = Yii::$app->user->identity;
?>
<aside class="main-sidebar">
    <section class="sidebar">
        <div class="user-panel">
            <div class="pull-left image">
                <img src="<?php 
echo XUtils::getAvatar($current_user->email);
?>
" class="img-circle" alt="<?php 
echo $current_user->nickname;
?>
">
            </div>
            <div class="pull-left info">
                <p><?php 
echo $current_user->nickname;
?>
</p>
                <a href="#"><i class="fa fa-circle text-success"></i> Online</a>
            </div>
        </div>
        <?php