線上書籍

Home

[1071]XOOPS模組開發

一、 輸出靜態HTML檔
  1. 匯出功能主要是靠 header 來定義文件的檔頭,進而憑空生出檔案(匯出記得關除錯)。 header("Content-type: text/html"); header("Content-Disposition: attachment; filename=檔名"); echo 主要內容; exit;
  2. 利用mime-type即可將文件偽裝成各種檔案(特別是文字檔): http://www.freeformatter.com/mime-types-list.html
  3. 可利用 tadtools/tad_function.php 中的 html5() 來套入HTML5頁面: html5($content = "", $ui = false, $bootstrap = true, $bootstrap_version = 3, $use_jquery = true, $container='container')
  4. 下載時,IE可能會變成亂碼檔名,可用 iconv("UTF-8","Big5",$檔名) ,將檔名轉成Big5編碼即可。(但若遇到檔名有特殊字的,就會變成缺字了)
  5. 若是要把檔案存在主機上,做成靜態頁面,可用 file_put_contents ($檔名, $檔案內容); file_put_contents(XOOPS_ROOT_PATH . "/uploads/snews/snews_{$sn}.html", $html);
  6. 若是要讀出存在主機上的檔案,可用file_get_contents ($檔名); $html=file_get_contents(XOOPS_ROOT_PATH . "/uploads/snews/snews_{$sn}.html");
二、 輸出成json
  1. 同HTML,將mime-type改為application/json 即可
  2. 如果是要給其他程式線上介接,一般不需要用header()來指定格式,直接echo即可。
  3. 以PHP而言,利用json_encode($陣列, JSON_UNESCAPED_UNICODE); 就可把陣列轉換為json格式(JSON_UNESCAPED_UNICODE亦可用256取代,讓中文不被編碼) $json = json_encode($all, JSON_UNESCAPED_UNICODE); file_put_contents(XOOPS_ROOT_PATH . "/uploads/snews/focus.json", $json);
三、 讀入json
  1. 利用json_decode($json,true)就可以把json轉換為PHP陣列。
  2. 擷取遠端的json檔案可以利用file_get_contents ($檔名) 來抓取! $json = file_get_contents(XOOPS_URL . "/uploads/snews/focus.json"); $focus = json_decode($json, true);
四、 模組區塊(Blocks)設定
  1. 先將xoops_version.php中的區塊設定取消註解,並依序填入資料。 $modversion['blocks'] = array(); $i = 1; $modversion['blocks'][$i]['file'] = "snews_focus.php"; $modversion['blocks'][$i]['name'] = _MI_SNEWS_FOCUS; $modversion['blocks'][$i]['description'] = _MI_SNEWS_FOCUS_DESC; $modversion['blocks'][$i]['show_func'] = "snews_focus"; $modversion['blocks'][$i]['template'] = "snews_focus.tpl"; $modversion['blocks'][$i]['edit_func'] = "snews_focus_edit"; $modversion['blocks'][$i]['options'] = "1";
  2. 若需要第二組區塊設定,在$i++下方,將七個設定陣列再複製一份來修改即可。
  3. 接著依據file 的設定值在blocks目錄下建立區塊檔案,如:blocks/snews_focus.php
  4. 裡面至少要有一個區塊主函數。主函數的名稱必須和show_func 設定值一樣,例如: <?php function snews_focus($options = ""){ } function snews_focus_edit($options = ""){ }
  5. 區塊主函數若要使用區塊的設定值,可以直接引入$options參數,如:$options[0]
  6. 區塊的設定值來自xoops_version.php中的options 設定值,根據「|」拆開後,第一個值就是$options[0],第二個值就是$options[1]依此類推。 function snews_focus($options = "") { $json = file_get_contents(XOOPS_URL . "/uploads/snews/focus.json"); $focus = json_decode($json, true); $rand_focus = array_rand($focus, $options[0]); if (is_array($rand_focus)) { foreach ($rand_focus as $k) { $block[] = $focus[$k]; } } else { $block[] = $focus[$rand_focus]; } return $block; }
  7. array_rand($陣列,數量)用來隨機取得陣列索引,若指定數量為1,則傳回數字;若指定數量大於1,則傳回陣列。
  8. 將最後內容結果return即可,可以是陣列,也可以是單一值,變數名稱不拘。
  9. 若是有使用區塊的設定值,就要有編輯函數。
  10. 編輯函數其實就是一個網頁表單而已,只不過不需要<form></form>。 function snews_focus_edit($options = "") { $form = "顯示文章數:<input type='text' name='options[0]' value='{$options[0]}'>"; return $form; }
  11. 主函數的目的僅在於從資料庫抓出資料,送到區塊樣板中,故需在template項目設定樣板名稱,如:snews_focus.tpl,樣板放在「templates/blocks」下。
  12. 區塊的樣版檔一律收到樣板標籤<{$block}>,不管顯示函數傳回的變數名稱為何。 <div class="container"> <{foreach from=$block item=focus}> <div class="row"> <div class="col-sm-3"> <img src="<{$focus.cover}>" class="img-responsive img-rounded"> </div> <div class="col-sm-9"> <h3><{$focus.title}></h3><{$focus.content2}> </div> </div> <{/foreach}> <div class="text-right"> <a href="<{$xoops_url}>/modules/snews/focus.php">[閱讀更多]</a> </div> </div>
  13. 區塊中若有連結,需注意必須使用「絕對位置」,如<{$xoops_url}>
五、微調中文文字數的裁切函數 function word_cut($string, $limit, $pad = "...") { $len = mb_strlen($string, 'UTF-8'); if ($len <= $limit) { return $string; } //先找出裁切後的字串有多少英文字 $tmp_content = mb_substr($string, 0, $limit, 'UTF-8'); preg_match_all('/(\w)/', $tmp_content, $match); $eng = count($match[1]); $add = round($eng / 2, 0); $limit += $add; $string = mb_substr($string, 0, $limit, 'UTF-8'); return $string . $pad; }

本週範例