2011年6月20日 星期一

crecord

class crecord extends crecord_NullObject {

    var $model;
    var $table;
    var $row;

    var $id;

    function crecord()
    {
        $this->model = NULL;
        $this->table = "";

        $this->_common_init();
    }

    function _common_init( $id = NULL )
    {
        $this->row = NULL;
        $this->id = $id;

        if( $id != NULL )
            $this->load($id);
    }

    function init( &$model , $table, $id = NULL)
    {
        $this->model = &$model;
        $this->table = $table;

        $this->_common_table();
        $this->load($id);
    }

    function equal($record)
    {
        foreach($record->row as $key => $value)
        {

            if( $this->getField($key) != $value )
                    return false;
        }
        return true;
    }


    function reuse($id = NULL)
    {
        $this->_common_init($id);
    }

    function loadlast( $key = null , $value = null)
    {
        $id = $this->model->select_max('id', $key, $value);
        $this->load($id);
    }


    function _common_table()
    {
        $this->model->init($this->table);
    }

    function load($id = NULL)
    {
         return $this->loadwithfield("id", $id);
    }

    function loadwithfield($field, $value)
    {
        $this->_common_table();
        $this->reuse();

        if( $field == NULL || $value == NULL )
            return;

        $row = $this->model->select_field_row($field,$value);

        return $this->_init_load_field($row);

    }

    function _init_load_field($row)
    {
        if( $row == NULL )
        {
            $this->id = NULL;
            return;
        }

        $this->id = $row->id;
        $this->setrow($row);

        return $this->row;
    }

    function load2($field ='id',$value=NULL)
    {
        return $this->loadwithfield($field, $value);

    }

    function is_update()
    {
        return $this->id != NULL;
    }

    function save()
    {
        if( $this->row == NULL )
            return;

        $this->_common_table();
        if( $this->id == NULL )
        {
           $this->id = $this->model->insert($this->row);
           return $this->id;
        }
        else
        {
           $this->model->update($this->id, $this->row);
           return $this->id;
        }

    }

    function delete($id = null)
    {
        $this->_common_table();


        if( $id != null )
            $this->load($id);

        if( $this->id == NULL )
            return;

        $this->model->delete($this->id);
        return $this->id;
    }

    function getField($field)
    {
        if( $field == "id" )
        {
            return $this->id;
        }

        switch( gettype($this->row) )
        {
            case "array":
                return $this->row[$field];

            case "object":
            {
                if( isset($this->row->$field) )
                    return $this->row->$field;
            }

            default:
                return;
        }

        if( is_object($this->row))
                return $this->row->$field;

        if( is_array($this->row))
                return $this->row[$field];
    }


    function setField($field, $value)
    {
        $type = gettype($this->row);
        switch( $type)
        {
            case "array":
                return $this->row[$field] = $value;
            case "object":
            default:
                return $this->row->$field = $value;
        }
    }

    function getrow()
    {
        $row = $this->row;
        if( $row != NULL )
            $row->id = $this->id;
        return $row;
    }

    function setrow($row)
    {
        $this->_tranArr2Obj ($row);
    }

    function _tranArr2Obj($array_row)
    {
        foreach($array_row as $key => $value )
        {
            if( $key == "id" )
                continue;
            $this->setField($key, $value);
        }
    }

    function create_owner()
    {
        return new crecord;
    }

    function isNull()
    {
        return "FALSE";
    }
    
}

mymodel_db

class mymodel_db extends model{

    var $table;

    function mymodel_db(){
        parent::model();

        $this->table = "";
    }

    function init($table)
    {
        $this->table = $table;
    }

    function select($id)
    {
        $row = $this->select_field_row("id", $id);
        return $row;
    }

    function select_query($field, $value)
    {
        $this->db->flush_cache();
        $this->db->where($field , $value);
        
        $query = $this->db->get($this->table);
        return $query;

    }


    function select_field_row($field, $value)
    {
        $query = $this->select_query($field, $value);
        return $query->row();

    }


    function select2($field,$value)
    {

        return $this->select_field_row($field, $value);
    }

    function delete($id){
        $this->db->flush_cache();
        $this->db->where("id",$id);
        $this->db->delete($this->table);

    }

    function insert($row)
    {
        $this->db->flush_cache();
        $this->db->insert($this->table,$row);
        $id = $this->db->insert_id();

        return $id;

    }

    function update($id, $row)
    {
        $this->db->flush_cache();
        $this->db->where("id",$id);
        $this->db->update($this->table,$row);
    }

    function selectall()
    {
        $this->db->flush_cache();
        $query = $this->db->get($this->table);
        return $query;
    }

    function select_all_idTOarr()
    {
        $id_arr = array();
        $this->db->flush_cache();
        $query = $this->db->get($this->table);
        foreach($query->result() as $row)
        {
            $id_arr[] = $row->id;
        }
        return $id_arr;
    }

    function like_query($field, $value)
    {
        $this->db->flush_cache();
        $this->db->like($field , $value);
        $query = $this->db->get($this->table);
        return $query;
    }

    function list_fields()
    {
        $result = $this->db->list_fields($this->table);
        return $result;
    }
}

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;
    }
}