首页>建站相关>typecho 1.2.1 给标签添加description说明文字

typecho 1.2.1 给标签添加description说明文字

之前写过一篇为typecho标签添加说明字段的方法,对应的是typecho1.1版本,目前typecho的稳定版本是1.2.1, 因为服务器php版本较低,一直没有去尝试新版。这几天用小皮面板搭建了一个本地的php环境,终于是正经尝试了一下新版本,新版的组织架构变化不大,代码写法上普遍采用了新的通过use导入命名空间的方式,所以如果想为1.2.1版本的标签添加说明,我们还是需要1.2.1版本的对应文件,老版本的文件无法通用。

对应文件的地址

typecho/var/Widget/Metas/Tag/Edit.php

Metas文件夹内另有一个命名为Category的目录,顾名思义,用于处理目录信息。typecho把目录信息与tag信息都放在一张表格里,所以他们处理的方式也极其相似。

完整代码

<?php

namespace Widget\Metas\Tag;

use Typecho\Common;
use Typecho\Db\Exception;
use Typecho\Widget\Helper\Form;
use Widget\Base\Metas;
use Widget\ActionInterface;
use Widget\Notice;

if (!defined('__TYPECHO_ROOT_DIR__')) {
    exit;
}

/**
 * 标签编辑组件
 *
 * @author qining
 * @category typecho
 * @package Widget
 * @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
 * @license GNU General Public License 2.0
 */
class Edit extends Metas implements ActionInterface
{
    /**
     * 入口函数
     */
    public function execute()
    {
        /** 编辑以上权限 */
        $this->user->pass('editor');
    }

    /**
     * 判断标签是否存在
     *
     * @param integer $mid 标签主键
     * @return boolean
     * @throws Exception
     */
    public function tagExists(int $mid): bool
    {
        $tag = $this->db->fetchRow($this->db->select()
            ->from('table.metas')
            ->where('type = ?', 'tag')
            ->where('mid = ?', $mid)->limit(1));

        return (bool)$tag;
    }

    /**
     * 判断标签名称是否存在
     *
     * @param string $name 标签名称
     * @return boolean
     * @throws Exception
     */
    public function nameExists(string $name): bool
    {
        $select = $this->db->select()
            ->from('table.metas')
            ->where('type = ?', 'tag')
            ->where('name = ?', $name)
            ->limit(1);

        if ($this->request->mid) {
            $select->where('mid <> ?', $this->request->filter('int')->mid);
        }

        $tag = $this->db->fetchRow($select);
        return !$tag;
    }

    /**
     * 判断标签名转换到缩略名后是否合法
     *
     * @param string $name 标签名
     * @return boolean
     */
    public function nameToSlug(string $name): bool
    {
        if (empty($this->request->slug)) {
            $slug = Common::slugName($name);
            if (empty($slug) || !$this->slugExists($name)) {
                return false;
            }
        }

        return true;
    }

    /**
     * 判断标签缩略名是否存在
     *
     * @param string $slug 缩略名
     * @return boolean
     * @throws Exception
     */
    public function slugExists(string $slug): bool
    {
        $select = $this->db->select()
            ->from('table.metas')
            ->where('type = ?', 'tag')
            ->where('slug = ?', Common::slugName($slug))
            ->limit(1);

        if ($this->request->mid) {
            $select->where('mid <> ?', $this->request->mid);
        }

        $tag = $this->db->fetchRow($select);
        return !$tag;
    }

    /**
     * 插入标签
     *
     * @throws Exception
     */
    public function insertTag()
    {
        if ($this->form('insert')->validate()) {
            $this->response->goBack();
        }

        /** 取出数据 */
        $tag = $this->request->from('name', 'description', 'slug');
        $tag['type'] = 'tag';
        $tag['slug'] = Common::slugName(empty($tag['slug']) ? $tag['name'] : $tag['slug']);

        /** 插入数据 */
        $tag['mid'] = $this->insert($tag);
        $this->push($tag);

        /** 设置高亮 */
        Notice::alloc()->highlight($this->theId);

        /** 提示信息 */
        Notice::alloc()->set(
            _t('标签 <a href="%s">%s</a> 已经被增加', $this->permalink, $this->name),
            'success'
        );

        /** 转向原页 */
        $this->response->redirect(Common::url('manage-tags.php', $this->options->adminUrl));
    }

    /**
     * 生成表单
     *
     * @param string|null $action 表单动作
     * @return Form
     * @throws Exception
     */
    public function form(?string $action = null): Form
    {
        /** 构建表格 */
        $form = new Form($this->security->getIndex('/action/metas-tag-edit'), Form::POST_METHOD);

        /** 标签名称 */
        $name = new Form\Element\Text(
            'name',
            null,
            null,
            _t('标签名称') . ' *',
            _t('这是标签在站点中显示的名称.可以使用中文,如 "地球".')
        );
        $form->addInput($name);

        /** 标签缩略名 */
        $slug = new Form\Element\Text(
            'slug',
            null,
            null,
            _t('标签缩略名'),
            _t('标签缩略名用于创建友好的链接形式, 如果留空则默认使用标签名称.')
        );
        $form->addInput($slug);

        /** 标签描述 */
        $description = new Form\Element\Text(
            'description',
            null,
            null,
            _t('标签描述'),
            _t('此字段用于给标签添加一段说明文字, 在有的主题中它也会被显示在页面上.')
        );
        $form->addInput($description);

        /** 标签动作 */
        $do = new Form\Element\Hidden('do');
        $form->addInput($do);

        /** 标签主键 */
        $mid = new Form\Element\Hidden('mid');
        $form->addInput($mid);

        /** 提交按钮 */
        $submit = new Form\Element\Submit();
        $submit->input->setAttribute('class', 'btn primary');
        $form->addItem($submit);

        if (isset($this->request->mid) && 'insert' != $action) {
            /** 更新模式 */
            $meta = $this->db->fetchRow($this->select()
                ->where('mid = ?', $this->request->mid)
                ->where('type = ?', 'tag')->limit(1));

            if (!$meta) {
                $this->response->redirect(Common::url('manage-tags.php', $this->options->adminUrl));
            }

            $name->value($meta['name']);
            $slug->value($meta['slug']);
            $description->value($meta['description']);
            $do->value('update');
            $mid->value($meta['mid']);
            $submit->value(_t('编辑标签'));
            $_action = 'update';
        } else {
            $do->value('insert');
            $submit->value(_t('增加标签'));
            $_action = 'insert';
        }

        if (empty($action)) {
            $action = $_action;
        }

        /** 给表单增加规则 */
        if ('insert' == $action || 'update' == $action) {
            $name->addRule('required', _t('必须填写标签名称'));
            $name->addRule([$this, 'nameExists'], _t('标签名称已经存在'));
            $name->addRule([$this, 'nameToSlug'], _t('标签名称无法被转换为缩略名'));
            $name->addRule('xssCheck', _t('请不要标签名称中使用特殊字符'));
            $slug->addRule([$this, 'slugExists'], _t('缩略名已经存在'));
            $slug->addRule('xssCheck', _t('请不要在缩略名中使用特殊字符'));
        }

        if ('update' == $action) {
            $mid->addRule('required', _t('标签主键不存在'));
            $mid->addRule([$this, 'tagExists'], _t('标签不存在'));
        }

        return $form;
    }

    /**
     * 更新标签
     *
     * @throws Exception
     */
    public function updateTag()
    {
        if ($this->form('update')->validate()) {
            $this->response->goBack();
        }

        /** 取出数据 */
        $tag = $this->request->from('name', 'slug', 'description', 'mid');
        $tag['type'] = 'tag';
        $tag['slug'] = Common::slugName(empty($tag['slug']) ? $tag['name'] : $tag['slug']);

        /** 更新数据 */
        $this->update($tag, $this->db->sql()->where('mid = ?', $this->request->filter('int')->mid));
        $this->push($tag);

        /** 设置高亮 */
        Notice::alloc()->highlight($this->theId);

        /** 提示信息 */
        Notice::alloc()->set(
            _t('标签 <a href="%s">%s</a> 已经被更新', $this->permalink, $this->name),
            'success'
        );

        /** 转向原页 */
        $this->response->redirect(Common::url('manage-tags.php', $this->options->adminUrl));
    }

    /**
     * 删除标签
     *
     * @throws Exception
     */
    public function deleteTag()
    {
        $tags = $this->request->filter('int')->getArray('mid');
        $deleteCount = 0;

        if ($tags && is_array($tags)) {
            foreach ($tags as $tag) {
                if ($this->delete($this->db->sql()->where('mid = ?', $tag))) {
                    $this->db->query($this->db->delete('table.relationships')->where('mid = ?', $tag));
                    $deleteCount++;
                }
            }
        }

        /** 提示信息 */
        Notice::alloc()->set(
            $deleteCount > 0 ? _t('标签已经删除') : _t('没有标签被删除'),
            $deleteCount > 0 ? 'success' : 'notice'
        );

        /** 转向原页 */
        $this->response->redirect(Common::url('manage-tags.php', $this->options->adminUrl));
    }

    /**
     * 合并标签
     *
     * @throws Exception
     */
    public function mergeTag()
    {
        if (empty($this->request->merge)) {
            Notice::alloc()->set(_t('请填写需要合并到的标签'), 'notice');
            $this->response->goBack();
        }

        $merge = $this->scanTags($this->request->merge);
        if (empty($merge)) {
            Notice::alloc()->set(_t('合并到的标签名不合法'), 'error');
            $this->response->goBack();
        }

        $tags = $this->request->filter('int')->getArray('mid');

        if ($tags) {
            $this->merge($merge, 'tag', $tags);

            /** 提示信息 */
            Notice::alloc()->set(_t('标签已经合并'), 'success');
        } else {
            Notice::alloc()->set(_t('没有选择任何标签'), 'notice');
        }

        /** 转向原页 */
        $this->response->redirect(Common::url('manage-tags.php', $this->options->adminUrl));
    }

    /**
     * 刷新标签
     *
     * @access public
     * @return void
     * @throws Exception
     */
    public function refreshTag()
    {
        $tags = $this->request->filter('int')->getArray('mid');
        if ($tags) {
            foreach ($tags as $tag) {
                $this->refreshCountByTypeAndStatus($tag, 'post', 'publish');
            }

            // 自动清理标签
            $this->clearTags();

            Notice::alloc()->set(_t('标签刷新已经完成'), 'success');
        } else {
            Notice::alloc()->set(_t('没有选择任何标签'), 'notice');
        }

        /** 转向原页 */
        $this->response->goBack();
    }

    /**
     * 入口函数,绑定事件
     *
     * @access public
     * @return void
     * @throws Exception
     */
    public function action()
    {
        $this->security->protect();
        $this->on($this->request->is('do=insert'))->insertTag();
        $this->on($this->request->is('do=update'))->updateTag();
        $this->on($this->request->is('do=delete'))->deleteTag();
        $this->on($this->request->is('do=merge'))->mergeTag();
        $this->on($this->request->is('do=refresh'))->refreshTag();
        $this->response->redirect($this->options->adminUrl);
    }
}

文件下载

typecho 1.2.1 标签添加description

标签: typecho

移动端可扫我直达哦~

推荐阅读

typecho 2025-04-20

关于typecho主题的目录结构

wordpress的极简主题需要有两个文件,分别是index.php以及style.css,而在typecho中,因为主题的一些配置信息默认被放在了index.php而不是style.css文件中,所以直接往主题包里仍一个index....

建站相关 typecho

typecho 2025-04-20

从零开始做一个 Typecho 主题系列

Typecho是一款轻量级、高效的开源博客程序,由国内开发者团队开发维护。它采用PHP语言编写,支持MySQL、SQLite等多种数据库,安装包体积仅有500KB左右,运行时内存占用极低,却能提供出色的性能表现。这款博客程序最大的特点...

建站相关 typecho

typecho 2025-04-18

Typecho_Db 类以及各方法基础用例

Typecho_Db 是 Typecho 博客系统的数据库操作核心类,提供了数据库连接、查询构建和执行等功能。Typecho_Db 提供了简洁高效的数据库操作接口,是Typecho插件和主题开发中最常用的类之一。主要功能数据库连接管理...

建站相关 typecho

typecho 2025-04-11

Typecho博客系统中的config.inc.php文件

在 Typecho 博客系统中,config.inc.php 是一个核心配置文件,用于存储数据库连接信息和系统关键设置。它通常位于 Typecho 的安装根目录下,在安装过程中自动生成。Typecho 的安装包解压后并不包含这个文件,...

建站相关 typecho

typecho 2025-04-09

Typecho尝试获取主题的一些基础信息

在Typecho的主题一览里是利用了一个循环来遍历主题,遍历主题的时候可以通过activated参数来确定是否是当前主题,这样就可以利用官方提供的函数来获取主题信息了,缺点很明显,需要多使用一个循环,浪费资源。<?php \Wi...

建站相关 typecho

typecho 2025-04-02

typecho模板解析优先级

类似于wordpress ,Typecho的模板系统也遵循特定的优先级规则,当系统寻找模板文件时会按照以下顺序进行查找:模板文件查找优先级主题自定义模板 (最高优先级)usr/themes/[主题名]/[模板文件]例如:usr/the...

建站相关 typecho

typecho 2025-03-27

typecho如何实时更新被修改后的style.css文件

给新的站点“biib.top”加了个友情链接,顺便修改了一下主题的footer背景。但浏览器缓存了站点的css文件,修改后的效果并不实时生效。直接清除浏览器缓存是个简单粗暴的办法,问题在于这个效果也只是针对博主个人,用户可不会没事瞎清...

建站相关 typecho

typecho 2024-04-20

typecho根据标签的slug name信息判断是否输出免责声明

博主是从事机械行业的,工作过程中接触了很多二手老旧的设备,因为是二手设备,不缺胳膊少腿能正常运行已是难得,完善的说明书与售后服务想都不要想了。所以找设备的说明书成了一项附加的工作,总得让设备正常运行起来,偶尔设备有个小病小痛的,也不能...

建站相关 typecho

typecho 2023-11-02

Typecho开发文档-Widget设计文档

什么是WidgetWidget是组成Typecho的最基本元素,除了已经抽象出来的类库外,其它几乎所有的功能都会通过Widget来完成.在实践中我们发现,在博客这种小型但很灵活的系统中实施一些大型框架的思想是不合适的,它会使系统灵活性...

建站相关 typecho

typecho 2023-10-25

Typecho默认路由表一览

路由器(Route)路由器(Route)是Typecho系统中的一个重要组件,类似mod_rewrite的机制,来实现独立的URL和指定的controller/action/params的映射规则.它通过识别诸如http://loca...

建站相关 typecho

typecho 2023-10-21

Typecho自动更新指定文章内容的尝试

曾经在老的博客(wordpress)里尝试并且成功运行过的一个方案,定时去请求某个比如“每天60秒读懂世界”这样的api,获取到数据,然后根据数据更新某一篇博文的内容。因为有“轻微”的强迫症,所以习惯把不用的东西直接“rm -rf”删...

建站相关 typecho