<?php
// Function "thaidate" ทำหน้าที่แปลง
string วันที่ที่อยู่ในรูปแบบ "dd/mm/yyyy" ให้กลายเป็นวันที่ในภาษาไทย
function thaidate($today) {
list($tdate, $tmonth, $tyear) = explode("/", $today);
$tyear += 543;
switch($tmonth) {
case "01":
$tmonth = "ม.ค."; break;
case "02":
$tmonth = "ก.พ."; break;
case "03":
$tmonth = "มี.ค."; break;
case "04":
$tmonth = "เม.ย."; break;
case "05":
$tmonth = "พ.ค."; break;
case "06":
$tmonth = "มิ.ย."; break;
case "07":
$tmonth = "ก.ค."; break;
case "08":
$tmonth = "ส.ค."; break;
case "09":
$tmonth = "ก.ย."; break;
case "10":
$tmonth = "ต.ค."; break;
case "11":
$tmonth = "พ.ย."; break;
case "12":
$tmonth = "ธ.ค."; break;
} // switch
return "$tdate $tmonth $tyear";
} // function
// ส่วนที่กำหนดการทำงานของ ThaiSarnNews Class
class ThaiSarnNews {
// กำหนด property ต่างๆของ ThaiSarnNews Class ไม่ควรจะ
set property เหล่านี้โดยตรง
var $version = "0.9";
var $newsItems = array();
var $curTag = "";
var $itemCount = 0;
var $newsTitle;
var $newsLink;
var $newsDescription;
var $newsSource;
var $newsSourceLink;
var $newsDate;
var $newsCategory;
var $newsKeywords;
var $newsLanguage;
var $xmlLocation = "";
var $newsErrors = "";
var $headingStyle;
var $entryStyle;
var $endingStyle;
var $noNewsMesg;
var $reversedDateStyle;
var $suppressErrors;
// กำหนด Methods ต่างๆของ ThaiSarnNews Class
// Class constructor ซึ่งทำการ initialise ค่า property
ต่างๆ
function ThaiSarnNews() {
$this->newsItems = array();
$this->curTag = "";
$this->itemCount = 0;
$this->newsTitle = array();
$this->newsLink = array();
$this->newsDescription = array();
$this->newsSource = array();
$this->newsSourceLink = array();
$this->newsDate = array();
$this->newsCategory = array();
$this->newsKeywords = array();
$this->newsLanguage = array();
// Set default style
$this->reversedDateStyle = false;
$bgcolor = "#9999cc"; // The color of the table frame.
$fontName = "Ms Sans Serif,Thonburi";
$fontSize = "2";
$tableEntry = "<TR><TD ALIGN=\"left\"
VALIGN=\"bottom\" WIDTH=\"12%\"><FONT face=\"$fontName\"
size=\"$fontSize\"><XMLNews:Date></FONT></TD>";
$tableEntry .= "<TD ALIGN=\"left\" VALIGN=\"bottom\"
WIDTH=\"40%\"><A HREF=\"<XMLNews:Link>\">";
$tableEntry .= "<FONT face=\"$fontName\" size=\"$fontSize\"><XMLNews:Title></FONT></A></TD>";
$tableEntry .= "<TD ALIGN=\"left\" VALIGN=\"bottom\"
WIDTH=\"30%\"><A HREF=\"<XMLNews:SourceLink>\">";
$tableEntry .= "<FONT face=\"$fontName\" size=\"$fontSize\"><XMLNews:Source></FONT></A></TD>";
$tableEntry .= "<TD ALIGN=\"left\" VALIGN=\"bottom\"
WIDTH=\"18%\"><FONT face=\"$fontName\" size=\"$fontSize\"><XMLNews:Category></FONT></TD></TR>";
$noNewsMesg = "<P><FONT face=\"$fontName\"
size=\"2\">ไม่มีข่าวใหม่อยู่ในระบบ</FONT>";
$tableHeading = "<TABLE BORDER=\"0\" WIDTH=\"98%\">";
$tableEnding = "</TABLE>";
$this->headingStyle = $tableHeading;
$this->entryStyle = $tableEntry;
$this->endingStyle = $tableEnding;
$this->noNewsMesg = $noNewsMesg;
}
// Functions for news parser.
function startElement($parser, $name, $attrs)
{
// ถ้า tag เป็น "NEWS" จะมีการ initialize
ค่าต่างๆใน array สำหรับเก็บข้อมูลข่าว
if ($name == "NEWS") {
$this->newsTitle[$this->itemCount] = "";
$this->newsLink[$this->itemCount] = "";
$this->newsDescription[$this->itemCount] = "";
$this->newsSource[$this->itemCount] = "";
$this->newsSourceLink[$this->itemCount] = "";
$this->newsDate[$this->itemCount] = "";
$this->newsCategory[$this->itemCount] = "";
$this->newsKeywords[$this->itemCount] = "";
$this->newsLanguage[$this->itemCount] = "";
}
// จำค่า tag ไว้ในตัวแปล $curTag
$this->curTag = "$name";
}
function endElement($parser, $name) {
// ถ้า tag เป็น "NEWS" จะมีการเพิ่มค่า
itemCount (จำนวนข่าว)
if ($name == "NEWS") { $this->itemCount++;
}
$this->curTag = "";
}
function characterData($parser, $data) {
$titleKey = "TITLE";
$linkKey = "LINK";
$descKey = "DESCRIPTION";
$dateKey = "PUBDATE";
$sourceKey = "SOURCE";
$sourceLinkKey = "SOURCELINK";
$cateKey = "CATEGORY";
$keywKey = "KEYWORD";
$langKey = "LANGUAGE";
// ถ้า tag เป็น "TITLE" ให้นำเอาข้อมูลไปเก็บไว้ใน
array "newsTitle" ในตำแหน่งข่าวปัจจุบัน ฯลฯ
if ($this->curTag == $titleKey) {
$this->newsTitle[$this->itemCount] .= $data;
} elseif ($this->curTag == $linkKey) {
$this->newsLink[$this->itemCount] .= $data;
} elseif ($this->curTag == $descKey) {
$this->newsDescription[$this->itemCount] .= $data;
} elseif ($this->curTag == $dateKey) {
$this->newsDate[$this->itemCount] .= $data;
} elseif ($this->curTag == $sourceKey) {
$this->newsSource[$this->itemCount] .= $data;
} elseif ($this->curTag == $sourceLinkKey) {
$this->newsSourceLink[$this->itemCount] .= $data;
} elseif ($this->curTag == $cateKey) {
$this->newsCategory[$this->itemCount] .= $data;
} elseif ($this->curTag == $keywKey) {
$this->newsKeywords[$this->itemCount] .= $data;
} elseif ($this->curTag == $langKey) {
$this->newsLanguage[$this->itemCount] .= $data;
}
}
// Functions for other parsers.
var $listTag = "";
var $listCount;
var $listArray;
function startListElement($parser, $name, $attrs)
{
if ($name == "CATEGORIES" || $name == "SOURCES") {
$this->listTag = "";
$this->listCount = 0;
$this->listArray = array();
} elseif ($name == "ITEM") {
$this->listTag = "ITEM";
}
}
function endListElement($parser, $name) {
if ($name == "ITEM") { $this->listCount += 1; }
}
function characterListData($parser, $data) {
$this->listArray[$this->listCount] = $data;
}
// Other functions.
function unsuppressErrors () {
$this->suppressErrors = false;
return true;
}
function getErrors () {
return $this->newsErrors;
}
function setLocation($sourceLocation) {
$this->xmlLocation = $sourceLocation;
return true;
}
function reset() {
$this->newsItem = array();
$this->curTag = "";
$this->itemCount = 0;
}
function setExternalLoader ($loadingCmd, $tmpfile)
{
$this->useExternalLoader = true;
$this->externalLoadingCmd = $loadingCmd;
$this->tmpFileLocation = $tmpfile;
return true;
}
function performParsing ($xmlSource, $startHdr,
$endHdr, $chrHdr) {
// 1. ดึงข้อมูลจากไทยสาร: PHP สามารถเปิดไฟล์ที่เป็น
URL ได้โดยใช้คำสั่ง fopen ฯลฯ
if (!($fp = fopen($xmlSource,"r")))
{
$this->newsErrors .= "Cannot open $xmlSource for reading\n";
return false;
}
// 2. ทำการ create parser, เซ็ตให้ object ตัวนี้
ใช้ parser ที่สร้างขึ้นมา และ
// เซ็ต option ให้ parser convert tag ที่อ่านได้จากเอกสารให้เป็น ตัวใหญ่
(capital letters) ให้หมด
$this->xml_parser = xml_parser_create();
xml_set_object($this->xml_parser, &$this);
xml_parser_set_option($this->xml_parser, XML_OPTION_CASE_FOLDING, true);
// 3. ตั้งค่า event handlers สำหรับ tag เปิด tag
ปิด และ character data (ดูคำอธิบาย event-based parsing)
xml_set_element_handler($this->xml_parser,
$startHdr, $endHdr);
xml_set_character_data_handler($this->xml_parser, $chrHdr);
// 4. ทำการ parse เอกสาร XML โดยอ่านข้อมูลทีละ
4096 byte จากไฟล์
while ($data = fread($fp, 4096)) {
if (!xml_parse($this->xml_parser, $data, feof($fp))) {
$this->newsErrors .= "XML error: " . xml_error_string(xml_get_error_code($this->xml_parser))
. " at line " . xml_get_current_line_number($this->xml_parser)
. "\n";
return false;
}
}
// 5. เมื่อทำการ parse เอกสารเสร็จแล้วก็ free
ตัว parser ปิดแฟ้มเอกสารและออกจาก function
xml_parser_free($this->xml_parser);
fclose($fp);
return true;
}
function parse() {
$xmlSource = $this->xmlLocation;
return $this->performParsing ($xmlSource, "startElement",
"endElement", "characterData");
}
function &getSourceList() {
$success = $this->performParsing ("http://www.thaisarn.com/services/distributor/xml_distributor.php?getSourceList=''",
"startListElement", "endListElement", "characterListData");
return $this->listArray;
}
function &getCategoryList() {
$success = $this->performParsing ("http://www.thaisarn.com/services/distributor/xml_distributor.php?getCategoryList=''",
"startListElement", "endListElement", "characterListData");
return $this->listArray;
}
function getParseError() {
return $this->parseError;
}
function getNumNews() {
return $this->itemCount;
}
function getIthNews($i) {
if ($this->reversedDateStyle) {
list($m, $d, $y) = explode("/", $this->newsDate[$i]);
$newsdate = sprintf("%02d",$d) . "/" . sprintf("%02d",$m)
. "/" . $y;
} else {
$newsdate = $this->newsDate[$i];
}
return array(
"title" => $this->newsTitle[$i],
"source" => $this->newsSource[$i],
"link" => $this->newsLink[$i],
"category" => $this->newsCategory[$i],
"description" => $this->newsDescription[$i],
"sourceLink" => $this->newsSourceLink[$i],
"pubdate" => $newsdate,
"keyword" => $this->newsKeywords[$i],
"language" => $this->newsLanguage[$i]
);
}
function setEntryStyle ($str) {
$this->entryStyle = $str;
return true;
}
function setIterationStyle ($str) {
$this->entryStyle = $str;
return true;
}
function setHeadingStyle ($str) {
$this->headingStyle = $str;
return true;
}
function setEndingStyle ($str) {
$this->endingStyle = $str;
return true;
}
function setReversedDateStyle ($bool) {
$this->reversedDateStyle = $bool;
return true;
}
function transformNews () {
$htmlResult = "";
if($this->itemCount == 0)
{
$htmlResult = $this->noNewsMesg;
}
else
{
$htmlResult = $this->headingStyle;
$actualEntryNo = 0;
for($row = 0; $row < ($this->itemCount);
$row++)
{
if ($this->reversedDateStyle)
{
list($m, $d, $y) = explode("/", $this->newsDate[$row]);
$newsdate = sprintf("%02d",$d) . "/" . sprintf("%02d",$m)
. "/" . $y;
$newsdate = thaidate($newsdate);
}
else
{
$newsdate = thaidate($this->newsDate[$row]);
}
$newslink = $this->newsLink[$row];
$newstitle = $this->newsTitle[$row];
$newssource = $this->newsSource[$row];
$newscategory = $this->newsCategory[$row];
$newskeywords = $this->newsKeywords[$row];
$newssourcelink = $this->newsSourceLink[$row];
if ($newstitle != "")
{
$thisEntry = $this->entryStyle;
$thisEntry = str_replace("<XMLNews:Date>", "$newsdate",
$thisEntry);
$thisEntry = str_replace("<XMLNews:Link>", "$newslink",
$thisEntry);
$thisEntry = str_replace("<XMLNews:Title>", "$newstitle",
$thisEntry);
$thisEntry = str_replace("<XMLNews:Source>", "$newssource",
$thisEntry);
$thisEntry = str_replace("<XMLNews:Category>", "$newscategory",
$thisEntry);
$thisEntry = str_replace("<XMLNews:Keyword>", "$newskeywords",
$thisEntry);
if ($newssourcelink != "")
{
$thisEntry = str_replace("<XMLNews:SourceLink>", "$newssourcelink",
$thisEntry);
}
else
{
$thisEntry = str_replace("<XMLNews:SourceLink>", "http://gnews.gits.net.th",
$thisEntry);
}
$htmlResult .= $thisEntry;
$actualEntryNo++;
}
}
$htmlResult .= $this->endingStyle;
if ($actualEntryNo == 0) { $htmlResult = $noNewsMesg;
}
}
return $htmlResult;
}
}
?>
|