discuz x2新添加關(guān)聯(lián)鏈接

字號(hào):


    在 x2.0 中增加了關(guān)聯(lián)鏈接,可以在指定范圍內(nèi)把 指定的文字 加上鏈接。
    在 后臺(tái)->運(yùn)營->關(guān)聯(lián)鏈接 處設(shè)置。
    這里可以直接 添加、刪除、更新 關(guān)聯(lián)鏈接,并且可以選擇該鏈接分別在 文章、論壇主題、群組主題、日志 中是否啟用。
    我們分析下這個(gè)代碼的執(zhí)行過程。
    首先這個(gè)功能的路徑是 /admin.php?action=misc&operation=relatedlink ,
    我們根據(jù)這個(gè)鏈接可以定位到代碼在 /source/admincp/admincp_misc.php 中,打開這個(gè)文件,搜 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');
    } 當(dāng)直接打開這個(gè)頁面的時(shí)候,就是顯示默認(rèn)的已經(jīng)存在的關(guān)聯(lián)鏈接。
    當(dāng)點(diǎn)擊 提交 的時(shí)候,會(huì)做三個(gè)處理。
    1.刪除處理
    如果提交之前,把某些關(guān)聯(lián)鏈接前的 刪除 勾打上的話,那么這里會(huì)先處理 刪除 的操作。
    代碼為:
    if($_g['gp_delete']) {
    db::delete('common_relatedlink', id in (.dimplode($_g['gp_delete']).));
    }
    2.更新操作
    如果在操作之前,已經(jīng)存在的關(guān)聯(lián)鏈接被修改過,那么在提交的時(shí)候,這些鏈接會(huì)先做下更新。
    對應(yīng)的代碼為:
    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.新添加的操作
    在提交前,如果有新添加的管鏈鏈接,則會(huì)執(zhí)行相應(yīng)的代碼插入到數(shù)據(jù)中。
    對應(yīng)的代碼為:
    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,
    ));
    }
    }
    } 需要注意的時(shí)候,不管是更新還是新添加, 關(guān)聯(lián)鏈接 在進(jìn)入數(shù)據(jù)庫之前,關(guān)于在那些模塊啟用的地方,都用了二進(jìn)制形式來控制在那里顯示,然后再變?yōu)?10 進(jìn)制存的。
    存儲(chǔ)完以后,緊跟著做了緩存的更新,對應(yīng)的代碼是:
    updatecache('relatedlink'); 關(guān)于的緩存的更新,需要查看 /source/function/function_cache.php
    然后調(diào)用了 /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 。
    我們在看下前臺(tái)發(fā)帖子等時(shí)候,使用我們剛剛添加的 關(guān)聯(lián)鏈接的情況。
    當(dāng)我們查看帖子的時(shí)候,執(zhí)行的文件是 /source/module/forum/forum_viewthread.php 文件。
    在這個(gè)文件中,先得到設(shè)置在帖子中顯示的關(guān)聯(lián)鏈接,相應(yīng)的代碼是:
    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');
    }
    }
    }
    把得到的 關(guān)聯(lián)鏈接 存放到了全局變量 $_g 中,然后在 模板文件 中使用。
    顯示帖子的時(shí)候調(diào)用的模板文件是:/template/default/forum/viewthread.htm 文件。
    這個(gè)文件相關(guān)的代碼為:
    <!--{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}-->
    然后執(zhí)行了 js 的 relatedlinks 函數(shù),該函數(shù)在 /static/js/common.js
    通過這個(gè)文件中的 js 方法,使得 關(guān)聯(lián)鏈接 在頁面中顯示。