調用discuz編輯器再也不是問題了


  前面講了如何開發一個discuz的特殊主題插件,詳情可在此查看discuz特殊主題插件開發步驟和犯的愚蠢錯誤。上一篇文章講解的是一些簡單的開發步驟,不涉及到具體的編碼。網頁編輯器之類的都是系統默認帶過來的,因此也就沒有太多的問題。然而,這些天又折騰了一遍“個人設置”模塊的開發。這些天百度和google了不知道多少遍,發現discuz的官方文檔和二次開發資料少得可憐。調用個編輯器這樣采用的功能官方文檔居然沒有,網友的分享多少也有些不完整;中途差點放棄。最后實在沒辦法兼不甘心,自己啃源碼去。最后的最后,你們也知道了,問題解決了,也就有了這篇文章。解決個調用編輯框的問題居然耗費了我一整天,不可思議。好了,這里記錄下如何調用discuz的編輯器吧,如有誤,請指出。

  環境:discuz! x2.5

  1、模板文件添加內容

<!--{subtemplate home/editor_image_menu}-->
內容:<textarea class="userData" name="content" id="uchome-ttHtmlEditor" style="height: 100%; width: 100%; display: none; border: 0px"></textarea>
<iframe src='home.php?mod=editor&charset={CHARSET}&allowhtml=1&isportal=1' name='uchome-ifrHtmlEditor' id='uchome-ifrHtmlEditor'  scrolling='no' style='width:85%;height:400px;border:1px solid #C5C5C5;position:relative;' border=0 frameborder=0 ></iframe>
<iframe id="uploadframe" name="uploadframe" width="0" height="0" marginwidth="0" frameborder="0" src="about:blank"></iframe>
<input id='submit_editsubmit' class='btn' type='submit' value='提交'  name='editsubmit' onClick='validate(this);'>
<script type="text/javascript" src="static/image/editor/editor_function.js"></script>  
<script type="text/JavaScript">  
function validate(obj) {  
    edit_save();  
    window.onbeforeunload = null;  
    obj.form.submit();  
    return false;  
}
</script>  

  然而這中間需要注意的是:

<!--{subtemplate home/editor_image_menu}-->

  是用於調用圖片上傳功能的,少了的話圖片上傳功能也就用不了了。

<script type="text/javascript" src="static/image/editor/editor_function.js"></script>  
<script type="text/JavaScript">  
function validate(obj) {  
    edit_save();  
    window.onbeforeunload = null;  
    obj.form.submit();  
    return false;  
}
</script>

  這段js是編輯框傳值和校驗用,少了也就沒法傳值了。很多資料到這一步也就完了,此時圖片上傳功能卻無法使用。百度和google了不知道多少遍也沒有找到,資料少得可憐。但是少了圖片上傳功能,這個編輯器也就不完整了;再者編輯框大部分情況下都是需要上傳圖片的。因此,不得不查看源碼,一步步調試了。

  2、查找問題

  上傳圖片的時候,出現以下的現象,然后就毫無反應了。

  於是找來misc.php查看源碼。最后一句是這樣的

require DISCUZ_ROOT.'./source/module/misc/misc_'.$mod.'.php';

  將$mod輸出,發現時swfupload。於是找來misc_swfupload.php文件。第一行的判斷是這樣的。

if((empty($_G['uid']) && $_GET['operation'] != 'upload') || $_POST['hash'] != md5(substr(md5($_G['config']['security']['authkey']), 8).$_G['uid'])) {
    exit();
} 

  於是將分別將$_POST['hash']和md5(substr(md5($_G['config']['security']['authkey']), 8).$_G['uid'])分別輸出來,發現hash是空的,完全對不上。於是找出頁面的hash,在頁面“/template/default/home/editor_image_menu.htm”中找到了如下代碼

<script type="text/javascript">
    var attachUpload = new SWFUpload({
        // Backend Settings
        upload_url: "{$_G[siteurl]}misc.php?mod=swfupload&action=swfupload&operation=<!--{if $_G['basescript'] == 'portal'}-->portal<!--{else}-->album<!--{/if}-->",
        post_params: {"uid" : "$_G[uid]", "hash":"$swfconfig[hash]"<!--{if $_G['basescript'] == 'portal'}-->,"aid":$aid,"catid":$catid<!--{/if}-->},

        // File Upload Settings
        file_size_limit : "$swfconfig[max]",    // 100MB
        <!--{if $_G['basescript'] == 'portal'}-->
        file_types : "$swfconfig[attachexts][ext]",
        file_types_description : "$swfconfig[attachexts][depict]",
        <!--{else}-->
        file_types : "$swfconfig[imageexts][ext]",
        file_types_description : "$swfconfig[imageexts][depict]",
        <!--{/if}-->
        file_upload_limit : 0,
        file_queue_limit : 0,

        // Event Handler Settings (all my handlers are in the Handler.js file)
        swfupload_preload_handler : preLoad,
        swfupload_load_failed_handler : loadFailed,
        file_dialog_start_handler : fileDialogStart,
        file_queued_handler : fileQueued,
        file_queue_error_handler : fileQueueError,
        file_dialog_complete_handler : fileDialogComplete,
        upload_start_handler : uploadStart,
        upload_progress_handler : uploadProgress,
        upload_error_handler : uploadError,
        upload_success_handler : uploadSuccess,
        upload_complete_handler : uploadComplete,

        // Button Settings
        button_image_url : "{IMGDIR}/uploadbutton.png",
        button_placeholder_id : "spanButtonPlaceholder",
        button_width: 100,
        button_height: 25,
        button_cursor:SWFUpload.CURSOR.HAND,
        button_window_mode: "transparent",

        custom_settings : {
            progressTarget : "fsUploadProgress",
            uploadSource: 'portal',
            uploadType: 'attach',
            imgBoxObj: $('attachlist')
            //thumbnail_height: 400,
            //thumbnail_width: 400,
            //thumbnail_quality: 100
        },

        // Debug Settings
        debug: false
    });

</script>

  hash是由$swfconfig[hash]進行賦值的。於是又搜索了一遍源碼,發現了這么兩句。

require_once libfile('function/upload');
$swfconfig = getuploadconfig($_G['uid'], 0, true);

  二話不說,復制過來測試。試了一下,尼瑪的還是不行。於是找呀找呀找,發現是前面調試時的輸出影響了ajax的處理。於是將調試語句去掉,果不其然,可以成功上傳了。一陣欣喜若狂。

 

  對於無法加載相冊之類的,一律如法炮制。最后,其實是php代碼初始化時添加如下代碼即可。

  3、解決問題的代碼

require_once libfile('function/upload');
$swfconfig = getuploadconfig($_G['uid'], 0, true);//編輯框上傳圖片初始化
require_once libfile('function/spacecp');
$albums = getalbums($_G['uid']);//獲取登陸用戶相冊

  兩行代碼,耗費了一整天,我也是醉了。

  最后想說的是,在開發資料不完整或者急缺的情況下;查看源碼也許是解決問題的最快捷的方式。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM