网站首页  汉语字词  英语词汇  考试资料  写作素材  旧版资料

请输入您要查询的考试资料:

 

标题 discuz x2新添加关联链接
内容
    在 x2.0 中增加了关联链接,可以在指定范围内把 指定的文字 加上链接。
    在 后台->运营->关联链接 处设置。
    这里可以直接 添加、删除、更新 关联链接,并且可以选择该链接分别在 文章、论坛主题、群组主题、日志 中是否启用。
    我们分析下这个代码的执行过程。
    首先这个功能的路径是 /admin.php?action=misc&operation=relatedlink ,
    我们根据这个链接可以定位到代码在 /source/admincp/admincp_misc.php 中,打开这个文件,搜 relatedlink
    } elseif($operation == 'relatedlink') {
    if(!submitcheck('linksubmit')) {
    ?>
    <script type=text/javascript>
    var rowtypedata = [
    [
    [1,'', 'td25'],
    [1,'<input type=text class=txt name=newname[] size=15>'],
    [1,'<input type=text name=newurl[] size=50>'],
    [1,'<input class=checkbox type=checkbox value=1 name=newarticle[]>'],
    [1,'<input class=checkbox type=checkbox value=1 name=newforum[]>'],
    [1,'<input class=checkbox type=checkbox value=1 name=newgroup[]>'],
    [1,'<input class=checkbox type=checkbox value=1 name=newblog[]>']
    ]
    ]
    </script>
    <?php
    shownav('extended', 'misc_relatedlink');
    showsubmenu('nav_misc_relatedlink');
    /*search={misc_relatedlink:action=misc&operation=relatedlink}*/
    showtips('misc_relatedlink_tips');
    /*search*/
    showformheader('misc&operation=relatedlink');
    showtableheader();
    showsubtitle(array('', 'misc_relatedlink_edit_name', 'misc_relatedlink_edit_url', '<input class=checkbox type=checkbox name=articleall>'.cplang('misc_relatedlink_extent_article'), '<input class=checkbox type=checkbox name=forumall>'.cplang('misc_relatedlink_extent_forum'), '<input class=checkbox type=checkbox name=groupall>'.cplang('misc_relatedlink_extent_group'),'<input class=checkbox type=checkbox name=blogall>'.cplang('misc_relatedlink_extent_blog')));
    $query = db::query(select * from .db::table('common_relatedlink'). order by id desc);
    while($link = db::fetch($query)) {
    $extent = sprintf('%04b', $link['extent']); 
    showtablerow('', array('class=td25', '', '', 'class=td26', 'class=td26', 'class=td26', ''), array(
    '<input type=checkbox class=checkbox name=delete[] value='.$link['id'].' />',
    '<input type=text class=txt name=name['.$link[id].'] value='.$link['name'].' size=15 />',
    '<input type=text name=url['.$link[id].'] value='.$link['url'].' size=50 />',
    '<input class=checkbox type=checkbox value=1 name=article['.$link[id].'] '.($extent[0] ? checked : '').'>',
    '<input class=checkbox type=checkbox value=1 name=forum['.$link[id].'] '.($extent[1] ? checked : '').'>',
    '<input class=checkbox type=checkbox value=1 name=group['.$link[id].'] '.($extent[2] ? checked : '').'>',
    '<input class=checkbox type=checkbox value=1 name=blog['.$link[id].'] '.($extent[3] ? checked : '').'>',
    ));
    }
    echo '<tr><td></td><td colspan=6><div><a href=### class=addtr>'.$lang['misc_relatedlink_add'].'</a></div></td></tr>';
    showsubmit('linksubmit', 'submit', 'del');
    showtablefooter();
    showformfooter();
    } else {
    if($_g['gp_delete']) {
    db::delete('common_relatedlink', id in (.dimplode($_g['gp_delete']).));
    }
    if(is_array($_g['gp_name'])) {
    foreach($_g['gp_name'] as $id => $val) {
    $extent_str = intval($_g['gp_article'][$id]).intval($_g['gp_forum'][$id]).intval($_g['gp_group'][$id]).intval($_g['gp_blog'][$id]); 
    $extent_str = intval($extent_str, '2'); 
    db::update('common_relatedlink', array(
    'name' => $_g['gp_name'][$id],
    'url' => $_g['gp_url'][$id],
    'extent' => $extent_str,
    ), array(
    'id' => $id,
    ));
    }
    }
    if(is_array($_g['gp_newname'])) {
    foreach($_g['gp_newname'] as $key => $value) {
    if($value) {
    $extent_str = intval($_g['gp_newarticle'][$key]).intval($_g['gp_newforum'][$key]).intval($_g['gp_newgroup'][$key]).intval($_g['gp_newblog'][$key]); 
    $extent_str = intval($extent_str, '2'); 
    db::insert('common_relatedlink', array(
    'name' => $value,
    'url' => $_g['gp_newurl'][$key],
    'extent' => $extent_str,
    ));
    }
    }
    }
    updatecache('relatedlink');
    cpmsg('relatedlink_succeed', 'action=misc&operation=relatedlink', 'succeed');
    } 当直接打开这个页面的时候,就是显示默认的已经存在的关联链接。
    当点击 提交 的时候,会做三个处理。
    1.删除处理
    如果提交之前,把某些关联链接前的 删除 勾打上的话,那么这里会先处理 删除 的操作。
    代码为:
    if($_g['gp_delete']) {
    db::delete('common_relatedlink', id in (.dimplode($_g['gp_delete']).));
    }
    2.更新操作
    如果在操作之前,已经存在的关联链接被修改过,那么在提交的时候,这些链接会先做下更新。
    对应的代码为:
    if(is_array($_g['gp_name'])) {
    foreach($_g['gp_name'] as $id => $val) {
    $extent_str = intval($_g['gp_article'][$id]).intval($_g['gp_forum'][$id]).intval($_g['gp_group'][$id]).intval($_g['gp_blog'][$id]);
    $extent_str = intval($extent_str, '2');
    db::update('common_relatedlink', array(
    'name' => $_g['gp_name'][$id],
    'url' => $_g['gp_url'][$id],
    'extent' => $extent_str,
    ), array(
    'id' => $id,
    ));
    }
    }
    3.新添加的操作
    在提交前,如果有新添加的管链链接,则会执行相应的代码插入到数据中。
    对应的代码为:
    if(is_array($_g['gp_newname'])) {
    foreach($_g['gp_newname'] as $key => $value) {
    if($value) {
    $extent_str = intval($_g['gp_newarticle'][$key]).intval($_g['gp_newforum'][$key]).intval($_g['gp_newgroup'][$key]).intval($_g['gp_newblog'][$key]);
    $extent_str = intval($extent_str, '2');
    db::insert('common_relatedlink', array(
    'name' => $value,
    'url' => $_g['gp_newurl'][$key],
    'extent' => $extent_str,
    ));
    }
    }
    } 需要注意的时候,不管是更新还是新添加, 关联链接 在进入数据库之前,关于在那些模块启用的地方,都用了二进制形式来控制在那里显示,然后再变为 10 进制存的。
    存储完以后,紧跟着做了缓存的更新,对应的代码是:
    updatecache('relatedlink'); 关于的缓存的更新,需要查看 /source/function/function_cache.php
    然后调用了 /source/function/cache/cache_relatedlink.php
    function build_cache_relatedlink() {
    global $_g;
    $data = array();
    $query = db::query(select * from .db::table('common_relatedlink'));
    while($link = db::fetch($query)) {
    if(substr($link['url'], 0, 7) != 'http://') {
    $link['url'] = 'http://'.$link['url'];
    }
    $data[] = $link;
    }
    save_syscache('relatedlink', $data);
    }
    从这里能看到,最后缓存存到了 pre_common_syscache 中,其中 cname 就是 relatedlink 。
    我们在看下前台发帖子等时候,使用我们刚刚添加的 关联链接的情况。
    当我们查看帖子的时候,执行的文件是 /source/module/forum/forum_viewthread.php 文件。
    在这个文件中,先得到设置在帖子中显示的关联链接,相应的代码是:
    if(!defined('in_archiver')) {
    $post['message'] = discuzcode($post['message'], $post['smileyoff'], $post['bbcodeoff'], $post['htmlon'] & 1, $_g['forum']['allowsmilies'], $_g['forum']['allowbbcode'], ($_g['forum']['allowimgcode'] && $_g['setting']['showimages'] ? 1 : 0), $_g['forum']['allowhtml'], ($_g['forum']['jammer'] && $post['authorid'] != $_g['uid'] ? 1 : 0), 0, $post['authorid'], $_g['cache']['usergroups'][$post['groupid']]['allowmediacode'] && $_g['forum']['allowmediacode'], $post['pid']);
    if($post['first']) {
    if(!$_g['forum_thread']['isgroup']) {
    $_g['relatedlinks'] = getrelatedlink('forum');
    } else {
    $_g['relatedlinks'] = getrelatedlink('group');
    }
    }
    }
    把得到的 关联链接 存放到了全局变量 $_g 中,然后在 模板文件 中使用。
    显示帖子的时候调用的模板文件是:/template/default/forum/viewthread.htm 文件。
    这个文件相关的代码为:
    <!--{if $_g['relatedlinks']}-->
    <div style=display: none>
    <ul>
    <!--{loop $_g['relatedlinks'] $key $link}-->
    <li><a id=relatedlink_$key href=$link[url]>$link[name]</a></li>
    <!--{/loop}-->
    </ul>
    </div>
    <script type=text/javascript>relatedlinks('postmessage_$_g[forum_firstpid]');</script>
    <!--{/if}-->
    然后执行了 js 的 relatedlinks 函数,该函数在 /static/js/common.js
    通过这个文件中的 js 方法,使得 关联链接 在页面中显示。
随便看

 

在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。

 

Copyright © 2002-2024 cuapp.net All Rights Reserved
更新时间:2025/5/14 21:51:21