<?php
/**
* Created by PhpStorm.
* User: hoyo
* Date: 2015/9/21
* Time: 下午 02:30
*/
namespace Hoyo\Front;
require OS_PATH ."/Component_Back/PHPWord-develop/src/PhpWord/Autoloader.php";
use PhpOffice\PhpWord\Autoloader;
use PhpOffice\PhpWord\Settings;
Autoloader::register();
Settings::loadConfig();
class MySQLStruct
{
var $TN = 'MJAPP_SoundU'; // Material DEVELOP TimeIsMoney
function __construct()
{
$init = new \Hoyo\Init();
$this->init = $init;
//$this->db = $init->PDO();
$PDO = new \PDO("mysql:dbname=". $this->TN .";host=192.168.0.101;charset=utf8", "root", "sly-ha-123");
$PDO->exec("set names utf8"); // php 5.3.6 之前的舊版本
$this->db = $PDO;
$this->smarty = $init->smarty();
$this->LNG = $GLOBALS['L']['L'];
$this->DefaultPage();
}
function DefaultPage()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$phpWord->setDefaultFontSize(11);
$section = $phpWord->addSection(
array('paperSize'=>'A4', 'marginLeft'=>600, 'marginRight'=>600, 'marginTop'=>1100, 'marginBottom'=>1100)
);
$header = array('size' => 16, 'bold' => true);
$sql = "SHOW TABLES";
$pre = $this->db->prepare($sql);
$pre->execute();
while( $row = $pre->fetch() ) {
$TableName = $row['Tables_in_'. $this->TN];
// 資料表說明
$sql = " SHOW TABLE STATUS WHERE name='{$TableName}' " ;
$preTC = $this->db->prepare($sql);
$preTC->execute();
$rowTC = $preTC->fetch();
$Comment = explode( "^", $rowTC['Comment'] );
$TableNameComment = array_shift($Comment);
//$TableComment = $TableNameComment;
//$section->addText(htmlspecialchars( $TableComment, ENT_COMPAT, 'UTF-8'));
// 資料表標題
$TableTitle = $TableName;
if ( strlen($TableNameComment)>0 ) $TableTitle .= ' - '. $TableNameComment;
$section->addText(htmlspecialchars( $TableTitle, ENT_COMPAT, 'UTF-8'), $header );
// 資料表說明
if ( strlen($Comment[0]) >0 ) {
$section->addText(htmlspecialchars('(' . $Comment[0] . ')', ENT_COMPAT, 'UTF-8'));
}
// 表格格現
$styleTable = array('borderSize' => 6, 'borderColor' => '006699', 'cellMargin' => 80);
// 第一行 標題格式
$styleFirstRow = array('borderBottomSize' => 18, 'borderBottomColor' => '0000FF', 'bgColor' => '66BBFF');
// 將表格標題樣式加入
$phpWord->addTableStyle('Fancy Table', $styleTable, $styleFirstRow);
// 標題垂直對齊
$styleCell = array('valign' => 'center');
// 標題文字粗體 水平對齊
$fontStyle = array('bold' => true, 'align' => 'center');
$table = $section->addTable('Fancy Table');
$table->addRow(400);
$table->addCell(500, $styleCell)->addText(htmlspecialchars('#', ENT_COMPAT, 'UTF-8'), $fontStyle);
$table->addCell(3200, $styleCell)->addText(htmlspecialchars('欄位名稱', ENT_COMPAT, 'UTF-8'));
$table->addCell(1400, $styleCell)->addText(htmlspecialchars('型態', ENT_COMPAT, 'UTF-8'));
$table->addCell(1600, $styleCell)->addText(htmlspecialchars('預設值', ENT_COMPAT, 'UTF-8'));
$table->addCell(1800, $styleCell)->addText(htmlspecialchars('鍵值、附加', ENT_COMPAT, 'UTF-8'));
$table->addCell(2100, $styleCell)->addText(htmlspecialchars('說明', ENT_COMPAT, 'UTF-8'));
$i = 1;
$sql = " SHOW FULL FIELDS FROM `{$TableName}` ";
$preTable = $this->db->prepare($sql);
$preTable->execute();
while( $rowTable = $preTable->fetch() ) {
$table->addRow();
$table->addCell()->addText(htmlspecialchars($i, ENT_COMPAT, 'UTF-8'));
$table->addCell()->addText(htmlspecialchars($rowTable['Field'], ENT_COMPAT, 'UTF-8'));
$table->addCell()->addText(htmlspecialchars($rowTable['Type'], ENT_COMPAT, 'UTF-8'));
$table->addCell()->addText(htmlspecialchars($rowTable['Default'], ENT_COMPAT, 'UTF-8'));
// Key & Extra 需要特別處理
$KeyExtra = $table->addCell();
$Key = htmlspecialchars($rowTable['Key'], ENT_COMPAT, 'UTF-8');
$Extra = htmlspecialchars($rowTable['Extra'], ENT_COMPAT, 'UTF-8');
$KeyExtra->addText($Key);
$KeyExtra->addText($Extra);
$table->addCell()->addText(htmlspecialchars($rowTable['Comment'], ENT_COMPAT, 'UTF-8'));
$i++; // # 序號 方便和 phpMyAdmin 核對
}
// 下一頁 (一個資料表一頁)
$section->addPageBreak();
}
// 儲存 Word
// Word2007, ODText, HTML
$phpWord->save('/tmp/'. $this->TN .'.docx', 'Word2007');
$this->smarty->assign('LNG', $this->LNG);
//echo __CLASS__;
$this->smarty->assign('Content', 'Page/'.__CLASS__.'.tpl');
$this->smarty->display('Layout_index.tpl');
}
}