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" name="newname[]" size="15">'],
    [1,'<input type="text" name="newurl[]" size="50">'],
    [1,'<input type="checkbox" value="1" name="newarticle[]">'],
    [1,'<input type="checkbox" value="1" name="newforum[]">'],
    [1,'<input type="checkbox" value="1" name="newgroup[]">'],
    [1,'<input 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 type="checkbox" name="articleall">'.cplang('misc_relatedlink_extent_article'), '<input type="checkbox" name="forumall">'.cplang('misc_relatedlink_extent_forum'), '<input type="checkbox" name="groupall">'.cplang('misc_relatedlink_extent_group'),'<input 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" name="delete[]" value="'.$link['id'].'" />',
    '<input type="text" name="name['.$link[id].']" value="'.$link['name'].'" size="15" />',
    '<input type="text" name="url['.$link[id].']" value="'.$link['url'].'" size="50" />',
    '<input type="checkbox" value="1" name="article['.$link[id].']" '.($extent[0] ? "checked" : '').'>',
    '<input type="checkbox" value="1" name="forum['.$link[id].']" '.($extent[1] ? "checked" : '').'>',
    '<input type="checkbox" value="1" name="group['.$link[id].']" '.($extent[2] ? "checked" : '').'>',
    '<input type="checkbox" value="1" name="blog['.$link[id].']" '.($extent[3] ? "checked" : '').'>',
    ));
    }
    echo '<tr><td></td><td colspan="6"><div><a href="###">'.$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ì)先做下更新。
    對(duì)應(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ù)中。
    對(duì)應(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ǔ)完以后,緊跟著做了緩存的更新,對(duì)應(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 。
    我們?cè)诳聪虑芭_(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>
    <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)鏈接 在頁面中顯示。