<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+
// | Common database model class. It use for common functions             |
// | File name : common.php                                               |
// | PHP version >= 5.2                                                   |
// | Created On 4 January 2012                                                   |
// +----------------------------------------------------------------------+
// | Author: Jinson Mathew <jinson.m@armiasystems.com>              |
// +----------------------------------------------------------------------+
// | Copyrights Armia Systems ? 2010                                      |
// | All rights reserved                                                  |
// +------------------------------------------------------


class ModelCommon extends BaseModel
{
	public function __construct()
	{
		parent::__construct();
	}
 
 

	/*
	Function to insert the values to table
	*/
	function addFields($table,$postedArray)
	{
		return $this->insert($this->tablePrefix.$table, $postedArray);
	}
	
	/*
	Function to update the table details
	*/
	function updateFields($table,$postedArray,$condition)
	{
		return $this->update($this->tablePrefix.$table, $postedArray,$condition);
	}

        /*
	Function to update the Forum table details
	*/
	function updateForumFields($table,$postedArray,$condition)
	{
		return $this->update($table,$postedArray,$condition);
	}
	
	/*
	Common function to check the existance of an item
	*/
	function checkExists($table,$field,$where)
    {
        if($where!='')
                $where= ' WHERE '.$where;
    	 $query = "SELECT count(".$field.") as cnt
    	             FROM ".$this->tablePrefix .$table.$where ;
         //echo $query;
				  
        $res = $this->execute($query);
    	return $this->fetchOne($res);
    }
	
	/*
	Common function to return the row fields
	*/
	function selectRow($table,$field,$where)
    {
		if($where!='')
			$where= ' WHERE '.$where;
    	 $query = "SELECT ".$field."
    	          FROM ".$this->tablePrefix .$table.$where ;
        // echo $query;
        $res = $this->execute($query);
    	return $this->fetchOne($res);
    }

	/*
	Common function to return the row fields
	*/
	function selectRecord($table,$field,$order='',$where='')
    {
           
		if($where!='')
			$where= ' WHERE '.$where;
    	 $query = "SELECT ".$field."
    	          FROM ".$this->tablePrefix .$table." ".$where." ".$order ;
	
        $res = $this->execute($query);
    	return $this->fetchRow($res);
    }

	/*
 	Common function to return the resultset details
	*/
	function selectResult($table,$field,$order='',$where='')
        {
             
             if($order!='')
             $order= ''.$order;
             $query = "SELECT ".$field."
                         FROM ".$this->tablePrefix .$table." ".$where." ".$order ;
             //echo $query;
             $res = $this->execute($query);
             return $this->fetchAll($res);
        }

	/*
	Function to delete the  keywords of a Brands
	*/
	function deleteRecord($table,$where)
	{
        $query = "DELETE FROM ".$this->tablePrefix  .$table.'  WHERE '.$where ;
        $res = $this->execute($query);

	}
	
	/*
	Function to execute a query to delete a records
	*/
	function deleteQuery($query)
	{
         $res = $this->execute($query);
         
	}
	

	/*
	Function to delete the  keywords of a Brands
	*/
	function selectQuery($query)
	{
		if($query != '')
		{
	        $res = $this->execute($query);
			return $this->fetchAll($res);
			 
		}
	}
        function getDatas($selFields = '*',$table1='',$table2='',$join='',$where='', $groupby = '',$orderby='',$limit='',$pagination='')
	{
  		$query 	= 'SELECT '.$selFields.' FROM '.$this->tablePrefix. $table1 ;
                if($join!='')
		     $query.='  ' . $join .' '.$this->tablePrefix. $table2;
                if($where!='')
		     $query.=' WHERE '.$where;
		if($groupby		!='')
                    $query.=' GROUP BY '.$groupby;
		if($orderby!='')
                    $query.=' ORDER BY '.$orderby.' ';
//                if($limit!='')
//                    $query.=' LIMIT '.$limit;
//                else if($pagination!='')
//                {
//                    $query	= 	$this->dopaging($query,'',PAGE_LIST_COUNT);
//                }
                if($pagination!='')
                {
                    if($pagination==1)
                        $query.=' LIMIT 0,'.PAGE_LIST_COUNT;
                    else
                    $query.=' LIMIT '.($pagination-1)*PAGE_LIST_COUNT.",".PAGE_LIST_COUNT;
                }
               else {
                   $query.=' LIMIT 0,'.PAGE_LIST_COUNT;
               }
               //echo $query;
             
              
                $res = $this->execute($query);
                return $this->fetchAll($res);
  	}
         //function getDataCount($table='',$selFields = '*',$where='')
        function getDataCount($selFields = '*',$table1='',$table2='',$join='',$where='', $groupby = '')
	{
            $query 	= 'SELECT COUNT('.$selFields.') AS cnt FROM '.$this->tablePrefix. $table1 .'  ' . $join .' '.$this->tablePrefix. $table2;
            if($where!='')
		     $query.=' WHERE '.$where;
            if($groupby		!='')
                $query.=' GROUP BY '.$groupby;
            //$query 	= 'SELECT COUNT('.$selFields.') AS cnt FROM ' . $this->tablePrefix . $table .' ' .$where;
            //echo "333". $query; //die;
            return $this->fetchOne($this->execute($query));
	}

         public function getListFields($table,$field,$order='',$where='')
         {
             if($order!='')
             $order= ''.$order;
              if($where!='')
             $where = 'WHERE '.$where;
             $query = "SELECT ".$field."
                         FROM ".$this->tablePrefix .$table.' '.$where.' '.$order ;
             $res = $this->execute($query);
             
             return $this->fetchAll($res);
         }

         public function getTopList($table,$field,$order='',$where='')
         {
             if($order!='')
             $order= ''.$order;
//              if($where!='')
//             $where = 'WHERE '.$where;
             $query = "SELECT ".$field."
                         FROM ".$this->tablePrefix .$table.' '.$where.' '.$order ;            
             $res = $this->execute($query);
             return $this->fetchAll($res);
         }
	/*
	function to get all the datas without pagination
	*/
	function getAllData($selFields = '*',$table='',$join = '' ,$where = '', $groupby = '',$sort_order='DESC',$sort_filed='')
	{

   		$query 	= 'SELECT '.$selFields.' FROM '.$this->tablePrefix. $table .'  ' . $join .' '.$where;
 		if($groupby		!='')
                    $query.=' GROUP BY '.$groupby;
		if($sort_filed		!='')
                    $query.=' ORDER BY '.$sort_filed.' '.$sort_order.' ';

		 //echo  $query;
         $res    =	$this->execute($query);
         return $this->fetchAll($res);
 	}

        /*Common function to return all the resultset details
	*/
	function selectAllResult($table,$field,$order='',$where='')
        {

             if($order!='')
             $order= ''.$order;
             $query = "SELECT ".$field."
                         FROM ".$this->tablePrefix .$table." ".$where." ".$order ;
             //echo $query;exit;
             $res = $this->execute($query);
             return $this->fetchAll($res);
        }

}
?>