<?php
/**
 * Created by JetBrains PhpStorm.
 * User: user
 * Date: 14-11-3
 * Time: 下午5:39
 * To change this template use File | Settings | File Templates.
 */
class Table_AccountPrivilege
{
    protected $_table = null;

    public function __construct(){
        $this->_table = new DbTable_AccountPrivilege();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance(){
        return new Table_AccountPrivilege();
    }

    /**
     * @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 = "privilege_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 = "privilege_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 = 'privilege_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);
    }

    /**
     * @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["privilege_id"]) && $condition["privilege_id"] != ""){
            $select->where("privilege_id = ?",$condition["privilege_id"]);
        }
        if(isset($condition["privilege_name"]) && $condition["privilege_name"] != ""){
            $select->where("privilege_name = ?",$condition["privilege_name"]);
        }
        if(isset($condition["privilege_name_en"]) && $condition["privilege_name_en"] != ""){
            $select->where("privilege_name_en = ?",$condition["privilege_name_en"]);
        }
        if(isset($condition["privilege_description"]) && $condition["privilege_description"] != ""){
            $select->where("privilege_description = ?",$condition["privilege_description"]);
        }
        if(isset($condition["privilege_slug"]) && $condition["privilege_slug"] != ""){
            $select->where("privilege_slug = ?",$condition["privilege_slug"]);
        }
        if(isset($condition["privilege_link"]) && $condition["privilege_link"] != ""){
            $select->where("privilege_link = ?",$condition["privilege_link"]);
        }
        if(isset($condition["to_show"]) && $condition["to_show"] != ""){
            $select->where("to_show = ?",$condition["to_show"]);
        }
        if(isset($condition["to_assign"]) && $condition["to_assign"] != ""){
            $select->where("to_assign = ?",$condition["to_assign"]);
        }
        if(isset($condition["to_admin"]) && $condition["to_admin"] != ""){
            $select->where("to_admin = ?",$condition["to_admin"]);
        }
        if(isset($condition["module_id"]) && $condition["module_id"] != ""){
            $select->where("module_id = ?",$condition["module_id"]);
        }
        if(isset($condition["sort_id"]) && $condition["sort_id"] != ""){
            $select->where("sort_id = ?",$condition["sort_id"]);
        }
        /*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);
        }
    }

}
