2011年5月19日 星期四

cweb_grid

class cweb_grid {

    var $column_obj; // 欄位
    var $item_arr; // 項目列
    var $param; // 參數列

    function cweb_grid()
    {

        $this->column_obj = new empty_object;
        $this->item_arr = array();
        $this->param = array();
        
    }


    function _pre_html()
    {
        $param_str = "";
        foreach($this->param as $key => $value )
        {
            $param_str .= " {$key}=\"{$value}\" ";
        }

        $result = "<table " . $param_str . " >";

        return $result;

    }

    function _after_html()
    {
        return "</table>";
    }

    function _arr2obj($row)
    {
        return $row;
    }

    function _proc_row($row)
    {
        $result = "";
        foreach($this->column_obj as $key => $value)
        {
            if( !isset($row->$key) )
                $result .= "<td ></td>";
            else
                $result .= "<td >{$row->$key}</td>";
        }

        $result = "<tr> {$result} </tr>";

        return $result;

    }

    function _proc_column()
    {
        return $this->_proc_row($this->column_obj);

    }

    function _proc_item()
    {

        $result = "";
        foreach($this->item_arr as $item)
        {
            $result .= $this->_proc_row($item);
        }

        return $result;
    }

    function insert_column($key, $text)
    {
        $this->column_obj->$key = $text;
    }

    function insert_item($row)
    {

        $obj = $this->_arr2obj($row);
        $this->item_arr[] = $obj;

    }

    function add_param($key, $value)
    {
        $this->param[$key] = $value;
    }

    function outHtml()
    {
        $result = "";
        
        $result .= $this->_pre_html();

        $result .= $this->_proc_column();

        $result .= $this->_proc_item();

        $result .= $this->_after_html();

        return $result;
    }

}

2011年5月18日 星期三

cweb_link

class cweb_link
{
    var $id;
    var $url;
    var $text;
    var $display;
    function cweb_link($id = "")
    {
        $this->id = $id;
        $this->url = null;
        $this->text = null;
        $this->display = null;
    }

    function insert($url=null,$text=null,$display=true)
    {
        if($url == null || $text == null)
            return;
        $this->url = $url;
        $this->text = $text;
        $this->display = $display;
    }

    function outHtml()
    {
        if($this->display==false)
            return "";

        $html = "<a id='{$this->id}' href='{$this->url}'>{$this->text}</a>";
        if($this->id == "")
            $html = "<a href='{$this->url}'>{$this->text}</a>";
        
        return $html;
    }
}

2011年5月17日 星期二

cweb_select

class cweb_select {
    //put your code here

    var $name;
    var $option;
    var $selected;

    function cweb_select($name = "")
    {
        $this->option = array();
        $this->selected = null;
        $this->name = $name;
    }

    function insert($row /*array or object*/ )
    {
        foreach ($row as $key => $value )
        {
            $this->insertItem($key, $value);
        }
    }

    function insert_only_value($row /*array or object*/ )
    {
        foreach ($row as $value )
        {
            $this->insertItem($value, $value);
        }
    }

    function insertItem($value , $text)
    {
        $this->option[$value] = $text;
    }

    function selected($value)
    {
        $this->selected = $value;
    }

    function getText($value)
    {
        return $this->option[$value];
    }

    function outHtml()
    {
        $result = "";


        foreach( $this->option as $value => $text )
        {
            $selected = "";
            if( $this->selected == $value)
                $selected = " selected";

            $result .= "<option value='{$value}'{$selected}>{$text}</option>";
        }

        $result = "<select name='{$this->name}' >{$result}</select>";

        return $result;
    }
}