[1071]XOOPS模組開發
<?php
include "header.php";
require_once XOOPS_ROOT_PATH . '/modules/tadtools/vendor/autoload.php';
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$phpWord->setDefaultFontName('標楷體'); //設定預設字型
$phpWord->setDefaultFontSize(9); //設定預設字型大小
$sectionStyle = array(
// 'orientation' => 'portrait', //landscape(橫);portrait(直)
'marginTop' => \PhpOffice\PhpWord\Shared\Converter::cmToTwip(2.5),
'marginLeft' => \PhpOffice\PhpWord\Shared\Converter::cmToTwip(2.5),
'marginRight' => \PhpOffice\PhpWord\Shared\Converter::cmToTwip(2.5),
'marginBottom' => \PhpOffice\PhpWord\Shared\Converter::cmToTwip(2.5),
// 'colsNum' => 2, //幾欄
// 'pageNumberingStart' => 1, //第幾頁
// 'borderTopSize' => 2, //上邊框
// 'borderTopColor' => '0000FF', //上邊框顏色
// 'lineNumbering' => array('start' => 1, 'increment' => 1), //行號設定
);
$section = $phpWord->addSection($sectionStyle); //建立一個區域
$sn=(int)$_GET['sn'];
if($sn){
$filename=snews_word($sn);
}else{
$filename=snews_word_list();
}
// $cellStyle = array('bgColor' => 'ffffff'); //正常格子
// $cellRowSpan = array('vMerge' => 'restart'); //垂直合併
// $cellContinue = array('vMerge' => 'continue'); //略過
// $cellColSpan = array('gridSpan' => 2); //水平合併
// $tableStyle = array('borderColor' => '000000', 'borderSize' => 6, 'cellMargin' => 80);
// $table = $section->addTable($tableStyle);
// $table->addRow();
// $table->addCell(2000, $cellColSpan)->addText('A');
// $table->addCell(1000, $cellRowSpan)->addText('B');
// $table->addRow();
// $table->addCell(1000, $cellRowSpan)->addText('C');
// $table->addCell(1000, $cellStyle)->addText('D');
// $table->addCell(1000, $cellContinue);
// $table->addRow();
// $table->addCell(1000, $cellContinue);
// $table->addCell(2000, $cellColSpan)->addText('E');
$filename = iconv("UTF-8", "Big5", $filename);
if($_GET['type']=="odt"){
//輸出 odt
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');
header('Content-Type: application/vnd.oasis.opendocument.text');
header("Content-Disposition: attachment;filename={$filename}.odt");
}else{
//輸出 docx
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
header('Content-Type: application/vnd.ms-word');
header("Content-Disposition: attachment;filename={$filename}.docx");
}
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
function snews_word($sn){
global $xoopsDB, $section,$phpWord;
$myts = MyTextSanitizer::getInstance();
$tbl = $xoopsDB->prefix('snews');
$sql = "SELECT * FROM `$tbl` WHERE `sn` = '{$sn}'";
$result = $xoopsDB->query($sql) or web_error($sql);
$snews = $xoopsDB->fetchArray($result);
// $content = $myts->displayTarea($snews['content'], 1, 0, 0, 0, 0);
$word_title= $myts->htmlSpecialChars($snews['title']);
$username = $myts->htmlSpecialChars($snews['username']);
$fontStyle = array('color' => '000000', 'size' => 24, 'bold' => true);
$paragraphStyle = array('align' => 'center', 'valign' => 'center','spaceAfter'=>500);
$phpWord->addTitleStyle(1, $fontStyle, $paragraphStyle); //設定標題N樣式
$section->addTitle( $word_title, 1 ); //新增標題
$fontStyle = array('color' => '000000', 'size' => 14, 'bold' => false);
$paragraphStyle = array('align' => 'center');
$section->addText($username, $fontStyle, $paragraphStyle);
$fontStyle = array('color' => '000000', 'size' => 12, 'bold' => false);
$paragraphStyle = array('align' => 'both');
$images=linkExtractor($snews['content']);
$snews['content']=replace_images($snews['content']);
$content = strip_tags($snews['content']);
$contents = explode("\n",$content);
$i=0;
// die(var_export($contents));
foreach($contents as $content){
// $content = str_replace(array("\n", "\r", " "), '', strip_tags($content));
if(strpos($content,'巷談集圖片')!==false){
$style=array('width'=>600);
$section->addImage( $images[$i], $style );
$i++;
}else{
$content = $myts->htmlSpecialChars($content);
$section->addText($content, $fontStyle, $paragraphStyle);
}
}
$word_title= str_replace(array(" "), '' , $word_title);
return $word_title;
}
function snews_word_list(){
global $xoopsDB, $section,$phpWord;
$topic['1'] = '街巷故事';
$topic['2'] = '市井觀點';
$topic['3'] = '私房知識塾';
$word_title="所有文章";
$fontStyle = array('color' => '000000', 'size' => 24, 'bold' => true);
$paragraphStyle = array('align' => 'center', 'valign' => 'center','spaceAfter'=>500);
$phpWord->addTitleStyle(1, $fontStyle, $paragraphStyle); //設定標題N樣式
$section->addTitle( $word_title, 1 ); //新增標題
$tableStyle = array('borderColor' => '000000', 'borderSize' => 6, 'cellMargin' => 80);
$table = $section->addTable($tableStyle);
$rowStyle = array('cantSplit' => true, 'tblHeader' => true);
$table->addRow(10, $rowStyle);
$cellStyle = array('bgColor' => 'CFCFCF');
$fontStyle = array('size' => 12, 'bold' => true);
$paragraphStyle = array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER);
$table->addCell(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(2), $cellStyle)->addText('分類', $fontStyle, $paragraphStyle);
$table->addCell(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(4), $cellStyle)->addText('日期', $fontStyle, $paragraphStyle);
$table->addCell(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(6), $cellStyle)->addText('標題', $fontStyle, $paragraphStyle);
$table->addCell(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(4), $cellStyle)->addText('作者', $fontStyle, $paragraphStyle);
$myts = MyTextSanitizer::getInstance();
$tbl = $xoopsDB->prefix('snews');
$sql = "SELECT * FROM `$tbl` ORDER BY `topic_sn`, `update_time` DESC";
$result = $xoopsDB->query($sql) or web_error($sql);
$cellStyle = array('bgColor' => 'FFFFFF');
$fontStyle = array('size' => 12, 'bold' => false);
$cellRowSpan = array('vMerge' => 'restart'); //垂直合併
$cellContinue = array('vMerge' => 'continue'); //略過
$cellColSpan = array('gridSpan' => 2); //水平合併
$old_topic_sn = '';
while ($snews = $xoopsDB->fetchArray($result)) {
$topic_sn=$snews['topic_sn'];
$snews['update_time'] = $myts->htmlSpecialChars($snews['update_time']);
$snews['title'] = $myts->htmlSpecialChars($snews['title']);
$snews['username'] = $myts->htmlSpecialChars($snews['username']);
if($topic_sn != $old_topic_sn){
$topicStyle = $cellRowSpan;
}else{
$topicStyle =$cellContinue;
}
$old_topic_sn=$topic_sn;
$table->addRow(10, $rowStyle);
$table->addCell(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(2), $topicStyle)->addText($topic[$topic_sn], $fontStyle);
$table->addCell(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(4), $cellStyle)->addText($snews['update_time'], $fontStyle);
$table->addCell(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(6), $cellStyle)->addText($snews['title'], $fontStyle);
$table->addCell(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(4), $cellStyle)->addText($snews['username'], $fontStyle);
// $all_news[$topic_sn][]=$snews;
}
// foreach($all_news as $topic_sn=>$topic_snews){
// $i=0;
// foreach($topic_snews as $snews){
// $table->addRow(10, $rowStyle);
// $topicStyle = $i?$cellContinue:$cellRowSpan;
// $table->addCell(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(2), $topicStyle)->addText($topic[$topic_sn], $fontStyle);
// $table->addCell(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(4), $cellStyle)->addText($snews['update_time'], $fontStyle);
// $table->addCell(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(6), $cellStyle)->addText($snews['title'], $fontStyle);
// $table->addCell(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(4), $cellStyle)->addText($snews['username'], $fontStyle);
// $i++;
// }
// }
return $word_title;
}
//從網頁語法中抓出圖片連結的函數
function linkExtractor($html){
$linkArray = array();
if (preg_match_all('/<img\s+.*?src=[\"\']?([^\"\' >]*)[\"\']?[^>]*>/i', $html, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
array_push($linkArray, $match[1]);
}
}
return $linkArray;
}
//將網頁語法中的圖片語法取代為自訂文字,
function replace_images($html){
$html = preg_replace('/<img\s+.*?src=[\"\']?([^\"\' >]*)[\"\']?[^>]*>/i', '巷談集圖片', $html);
return $html;
}