<?php
class Table_OrderAddressBook
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_OrderAddressBook();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_OrderAddressBook();
    }

    /**
     * @param $row
     * @return mixed
     */
    public function add($row)
    {
        return $this->_table->insert($row);
    }


    /**
     * @param $row
     * @param $value
     * @param string $field
     * @return mixed
     */
    public function update($row, $value, $field = "oab_id")
    {
        $where = $this->_table->getAdapter()->quoteInto("{$field}= ?", $value);
        return $this->_table->update($row, $where);
    }

    /**
     * @param $value
     * @param string $field
     * @return mixed
     */
    public function delete($value, $field = "oab_id")
    {
        $where = $this->_table->getAdapter()->quoteInto("{$field}= ?", $value);
        return $this->_table->delete($where);
    }

    /**
     * @param $value
     * @param string $field
     * @param string $colums
     * @return mixed
     */
    public function getByField($value, $field = 'oab_id', $colums = "*")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $colums);
        $select->where("{$field} = ?", $value);
        return $this->_table->getAdapter()->fetchRow($select);
    }
    
    public function getByWhere($where, $colums = "*")
    {
    	$select = $this->_table->getAdapter()->select();
    	$table = $this->_table->info('name');
    	$select->from($table, $colums);
    	foreach($where as $field=>$value) {
    		$select->where("{$field} = ?", $value);
    	}
    	return $this->_table->getAdapter()->fetchRow($select);
    }

    public function getLeftJoinByField($value, $field = 'oab_id', $colums = "*")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $colums);
        $select->joinLeft('country','country.country_id='.$table.'.oab_country_id',array('country_code','country_name_en'));
        $select->where("{$field} = ?", $value);
        return $this->_table->getAdapter()->fetchRow($select);
    }

    public function getAll()
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, "*");
        return $this->_table->getAdapter()->fetchAll($select);
    }

    /**
     * @param array $condition
     * @param string $type
     * @param int $pageSize
     * @param int $page
     * @param string $orderBy
     * @return array|string
     */
    public function getByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->where("1 =?", 1);
        /*CONDITION_START*/
        
        if(isset($condition["order_id"]) && $condition["order_id"] != ""){
            $select->where("order_id = ?",$condition["order_id"]);
        }
        if(isset($condition["order_code"]) && $condition["order_code"] != ""){
            $select->where("order_code = ?",$condition["order_code"]);
        }
        if(isset($condition["oab_firstname"]) && $condition["oab_firstname"] != ""){
            $select->where("oab_firstname = ?",$condition["oab_firstname"]);
        }
        if(isset($condition["oab_lastname"]) && $condition["oab_lastname"] != ""){
            $select->where("oab_lastname = ?",$condition["oab_lastname"]);
        }
        if(isset($condition["oab_company"]) && $condition["oab_company"] != ""){
            $select->where("oab_company = ?",$condition["oab_company"]);
        }
        if(isset($condition["oab_country_id"]) && $condition["oab_country_id"] != ""){
            $select->where("oab_country_id = ?",$condition["oab_country_id"]);
        }
        if(isset($condition["oab_zone_id"]) && $condition["oab_zone_id"] != ""){
            $select->where("oab_zone_id = ?",$condition["oab_zone_id"]);
        }
        if(isset($condition["oab_postcode"]) && $condition["oab_postcode"] != ""){
            $select->where("oab_postcode = ?",$condition["oab_postcode"]);
        }
        if(isset($condition["oab_state"]) && $condition["oab_state"] != ""){
            $select->where("oab_state = ?",$condition["oab_state"]);
        }
        if(isset($condition["oab_city"]) && $condition["oab_city"] != ""){
            $select->where("oab_city = ?",$condition["oab_city"]);
        }
        if(isset($condition["oab_suburb"]) && $condition["oab_suburb"] != ""){
            $select->where("oab_suburb = ?",$condition["oab_suburb"]);
        }
        if(isset($condition["oab_street_address1"]) && $condition["oab_street_address1"] != ""){
            $select->where("oab_street_address1 = ?",$condition["oab_street_address1"]);
        }
        if(isset($condition["oab_street_address2"]) && $condition["oab_street_address2"] != ""){
            $select->where("oab_street_address2 = ?",$condition["oab_street_address2"]);
        }
        if(isset($condition["oab_phone"]) && $condition["oab_phone"] != ""){
            $select->where("oab_phone = ?",$condition["oab_phone"]);
        }
        if(isset($condition["oab_cell_phone"]) && $condition["oab_cell_phone"] != ""){
            $select->where("oab_cell_phone = ?",$condition["oab_cell_phone"]);
        }
        if(isset($condition["oab_fax"]) && $condition["oab_fax"] != ""){
            $select->where("oab_fax = ?",$condition["oab_fax"]);
        }
        if(isset($condition["oab_email"]) && $condition["oab_email"] != ""){
            $select->where("oab_email = ?",$condition["oab_email"]);
        }
        if(isset($condition["oab_note"]) && $condition["oab_note"] != ""){
            $select->where("oab_note = ?",$condition["oab_note"]);
        }
        /*CONDITION_END*/
        if ('count(*)' == $type) {
            return $this->_table->getAdapter()->fetchOne($select);
        } else {
            if (!empty($orderBy)) {
                $select->order($orderBy);
            }
            if ($pageSize > 0 and $page > 0) {
                $start = ($page - 1) * $pageSize;
                $select->limit($pageSize, $start);
            }
            $sql = $select->__toString();
            return $this->_table->getAdapter()->fetchAll($sql);
        }
    }
}