<table><tr><td>
<form method="POST" action="">
<input type="hidden" name="msg" value="#?msg?#">
主旨:<input type="text" name="title" size="80"></p>
<p>內容:<br>
<textarea rows="5" name="description" cols="80"></textarea></p>
<p><input type="submit" value="送出" name="B1"><input type="reset" value="重新設定" name="B2"></p>
</form>
</td></tr></table>
//
class CuTestTaskForm extends CuPage
{
function OnProcess()
{
$this->title->Set("value", "哦哦更爽的");
$this->msg->Set("value", "這樣行嗎?");
}
}
$page = new CuTestTaskForm;
$filename = "resource/Task.form.html";
$page->LoadFile($filename);
$nIndex = $page->GetObjectIndex("msg");
$this->debug->debug($nIndex, "msg nIndex");
$this->assertTrue($nIndex == 5, "msg nIndex from CuPageGetObjectIndex");
$msg = $page->GetObject("msg");
$msg->Set("value", "抓出來的是考備版");
$this->debug->debug($msg, "CuPage::GetObject 抓出來的是考備版");
$page->SetObject($msg); // 再蓋回去囉
$this->debug->innerText($page->OnHTML(), "CuPage->SetObject 再蓋回去囉");
$page->OnProcess();
$this->debug->innerText($page->OnHTML(), "from CuPage::OnHTML");
// CuPage.php
/************************************************
* 類別名稱: CuPage
* 類別說明: 將HTML 的 form 載入後轉為PHP控制物件 CuWebControl
* 現在只支援 <INPUT >
************************************************/
class CuPage
{
var $m_Control;
var $m_Index;
function CuPage()
{
$this->m_Control = array();
$this->m_Index = 0;
}
/************************************************
* 函式名稱: LoadFile
* 函式說明: 載入html檔
************************************************/
function LoadFile($filename)
{
$html = LoadFile("resource/Task.form.html");
$this->ExplodeForm($html);
$this->CombineControl();
}
/************************************************
* 函式名稱: ExplodeForm
* 函式說明: 將Html Form 轉為 控制項陣列
************************************************/
function ExplodeForm($html)
{
$html_array = explode("<", $html);
foreach( $html_array as $key => $value )
{
if( ereg( "^input", $value ) )
{
list($webcode, $str) = explode(">", $value);
$object_Attribute = explode(" ", $webcode);
$this->CreateWebControl($object_Attribute);
$this->CreateHTMLString( $str );
}
else if( $value != "" )
{
$this->CreateHTMLString( "<" . $value );
}
}
}
/************************************************
* 函式名稱: CombineControl
* 函式說明: 叫用這個函式, 來派發form 物件到類別內
************************************************/
function CombineControl()
{
foreach( $this->m_Control as $key => $value )
{
if( $value->Attribute["name"] == "" )
continue;
$name = $value->Attribute["name"];
$this->$name = &$this->m_Control[$key];
}
}
/************************************************
* 函式名稱: OnProcess
* 函式說明: 子類別繼承這個函式後對表單的操作函式
************************************************/
function OnProcess()
{
}
/************************************************
* 函式名稱: AddObject
* 函式說明: 將CuWebControl加入CuPage陣列
************************************************/
function AddObject($object)
{
$this->m_Index = $this->m_Index + 1;
$this->m_Control[$this->m_Index] = $object;
}
/************************************************
* 函式名稱: SetObject
* 函式說明: 將CuWebControl加入CuPage陣列, 這裡會掃name參數, 找到一樣的就蓋掉, 找不到就轉叫 AddObject
************************************************/
function SetObject($object)
{
$index = $this->GetObjectIndex($object->Get("name"));
if( $index == 0 )
$this->AddObject($object);
else
$this->m_Control[$index] = $object;
}
/************************************************
* 函式名稱: GetObjectIndex
* 函式說明: 依名稱尋找索引值
************************************************/
function GetObjectIndex($name)
{
foreach( $this->m_Control as $key => $value )
{
if( $name == $value->Attribute["name"] )
return $key;
}
}
/************************************************
* 函式名稱: GetObject
* 函式說明: 依名稱尋找元件 -- 這裡傳回的是copy版,
************************************************/
function GetObject($name)
{
foreach( $this->m_Control as $key => $value )
{
if( $name == $value->Attribute["name"] )
return $this->m_Control[$key];
}
}
/************************************************
* 函式名稱: CreateWebControl
* 函式說明: 將處理好一半的HTML碼轉為CuWebControl, 並且叫用SetObject
************************************************/
function CreateWebControl($html_tag_array)
{
$Control = new CuWebControl;
foreach($html_tag_array as $Attribute_key => $Attribute_value)
{
list($key, $value) = explode("=", $Attribute_value);
$value = split("\"", $value);
$Control->Set($key, $value[1]);
}
$this->AddObject($Control);
}
/************************************************
* 函式名稱: CreateHTMLString
* 函式說明: 建構HTML字串元件除了可以轉換的 FORM INPUT 外, 其他的文字直接建文字元件
************************************************/
function CreateHTMLString($string)
{
if( $string == "" )
return;
$Control = new CuHTMLString();
$Control->OnAdd( $string );
$this->AddObject($Control);
}
function OnHTML()
{
$html = "";
foreach( $this->m_Control as $key => $value )
{
if( $value != null )
$html .= $value->OnHTML();
}
return $html;
}
}
Form 的載入一般都直接使用HTML並且替換字串, 在這裡將form.html當做外部介面設定檔, 將 input 生成為可控制類別, 使用類別操控的方式來處理表單, 可以很方便的操作內容元件,
上面拿 tag input 來做範例.
沒有留言:
張貼留言