码桶

发现社区成员的开源项目

飞扬 / LB_Down 公开
main
LB_Down/sidebar.php
sidebar.php5.6 KB
<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit;

$sidebarNum = intval($this->options->sidebarNum ?: 8);
?>

<aside class="site-sidebar">
    
    <section class="widget widget-hot">
        <h3 class="widget-title">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <path d="M12 2l2.4 7.4H22l-6.2 4.5 2.4 7.4L12 16.8 5.8 21.3l2.4-7.4L2 9.4h7.6z"/>
            </svg>
            热门下载
        </h3>
        <ol class="hot-list">
            <?php
            $db = \Typecho\Db::get();
            $prefix = $db->getPrefix();
            $adapter = strtolower($db->getAdapterName());
            $isMysql = (false !== strpos($adapter, 'mysql'));

            $orderBy = $isMysql
                ? 'CAST(f.str_value AS UNSIGNED)'
                : '(f.str_value + 0)';

            $q = $isMysql ? '`' : '"';
            $sql = "SELECT c.* FROM {$q}{$prefix}contents{$q} c
                    INNER JOIN {$q}{$prefix}fields{$q} f ON c.cid = f.cid
                    WHERE c.status = 'publish'
                      AND c.type = 'post'
                      AND f.name = 'downloadCount'
                    ORDER BY {$orderBy} DESC
                    LIMIT {$sidebarNum}";

            $hotPosts = $db->fetchAll($sql);

            if ($hotPosts):
                $hotWidget = \Widget\Contents\Post\Recent::alloc();
                foreach ($hotPosts as $i => $post):
                    $hotWidget->reset();
                    $hotWidget->push($post);
                    $thumb = lb_get_thumb($hotWidget);
                    $count = 0;
                    $countRow = $db->fetchObject($db->select('str_value')
                        ->from('table.fields')
                        ->where('cid = ?', $post['cid'])
                        ->where('name = ?', 'downloadCount'));
                    if ($countRow) $count = intval($countRow->str_value);
            ?>
                <li class="hot-item">
                    <span class="hot-rank <?php echo $i < 3 ? 'top' : ''; ?>"><?php echo $i + 1; ?></span>
                    <a href="<?php $hotWidget->permalink(); ?>" class="hot-thumb">
                        <img src="<?php echo htmlspecialchars($thumb); ?>" alt="<?php echo htmlspecialchars($post['title']); ?>"
                             loading="lazy"
                             onerror="this.src='<?php echo lb_theme_url('assets/img/placeholder.svg'); ?>'">
                    </a>
                    <div class="hot-info">
                        <a href="<?php $hotWidget->permalink(); ?>" class="hot-title"><?php echo htmlspecialchars($post['title']); ?></a>
                        <span class="hot-count">
                            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                <path d="M21 15v4a2 2 0 01-2 2H5a2 2 0 01-2-2v-4"/>
                                <polyline points="7 10 12 15 17 10"/>
                                <line x1="12" y1="15" x2="12" y2="3"/>
                            </svg>
                            <?php echo $count; ?>
                        </span>
                    </div>
                </li>
            <?php endforeach; else: ?>
                <li class="empty">暂无数据</li>
            <?php endif; ?>
        </ol>
    </section>

    
    <section class="widget widget-latest">
        <h3 class="widget-title">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <circle cx="12" cy="12" r="10"/>
                <polyline points="12 6 12 12 16 14"/>
            </svg>
            最新更新
        </h3>
        <ul class="latest-list">
            <?php
            $latest = \Widget\Contents\Post\Recent::alloc();
            $latest->setLimit($sidebarNum);
            while ($latest->next()):
                $thumb = lb_get_thumb($latest);
            ?>
                <li class="latest-item">
                    <a href="<?php $latest->permalink(); ?>" class="latest-thumb">
                        <img src="<?php echo htmlspecialchars($thumb); ?>" alt="<?php $latest->title(); ?>"
                             loading="lazy"
                             onerror="this.src='<?php echo lb_theme_url('assets/img/placeholder.svg'); ?>'">
                    </a>
                    <div class="latest-info">
                        <a href="<?php $latest->permalink(); ?>" class="latest-title"><?php $latest->title(); ?></a>
                        <span class="latest-date"><?php $latest->date('m-d'); ?></span>
                    </div>
                </li>
            <?php endwhile; ?>
        </ul>
    </section>

    
    <section class="widget widget-tags">
        <h3 class="widget-title">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <path d="M20.59 13.41l-7.17 7.17a2 2 0 01-2.83 0L2 12V2h10l8.59 8.59a2 2 0 010 2.82z"/>
                <line x1="7" y1="7" x2="7.01" y2="7"/>
            </svg>
            热门标签
        </h3>
        <div class="tag-cloud">
            <?php
            $tags = \Widget\Metas\Tag\Cloud::alloc(null, null, [
                'sort' => 'count',
                'ignoreZeroCount' => true,
                'desc' => true,
                'limit' => 30
            ]);
            if ($tags->have()):
                while ($tags->next()):
            ?>
                <a href="<?php $tags->permalink(); ?>" class="tag-cloud-item"><?php $tags->name(); ?></a>
            <?php
                endwhile;
            else:
            ?>
                <span class="empty">暂无标签</span>
            <?php endif; ?>
        </div>
    </section>
</aside>