<?php 
class Zll extends Model
{
	function query($query)
		{
			$query=$this->db->query($query);
			return $query->result_array();
		}
	function gUpdate($tableName,$where,$qData)
		{	
			foreach($where as $col=>$val)
				$this->db->where($col,$val);		
			$this->db->update($tableName,$qData);		
		}

	function gInsert($tableName,$qData)
		{
			$this->db->insert($tableName, $qData); 
			return $ID = $this->db->insert_id();	
		}
	function createUserAccount($data)
		{
				$this->db->insert('users',$data);
				$insertID = $this->db->insert_id();
				$data1['userID'] = $insertID;
				$data1['user_name'] = md5($data['username']);
				$this->db->insert('validateuser',$data1);
				return $insertID;
		}	
	function gDelete($tableName,$where)
		{
			foreach($where as $col=>$val)
				$this->db->where($col,$val);		
			$this->db->delete($tableName); 
		}
	function gLike($tableName,$where)
		{
			foreach($where as $col=>$val)
				$this->db->like($col,$val);		
			$this->db->insert($tableName); 
		}
	
	function gCountAll($tableName,$where=NULL)
		{
			if(is_array($where))
				foreach($where as $col=>$val)
					$this->db->where($col,$val);		
			return $this->db->count_all_results($tableName);		
		}
	
	function gTruncate($tableName)
		{
			$this->db->truncate($tableName); 
		}

	function gSelectStar($tableName,$obf='',$ob='')
		{
			if($obf)
				$this->db->order_by($obf, $ob); 
			$query = $this->db->get($tableName); 
			return $query->result_array();	

		}
	function getpaymentDetail($iID)
		{	
				$query=$this->db->query("select * from payment where iID='$iID'");
				return $query->result_array();
		}
	function gSelectWhere($tableName,$where)
		{
			$query = $this->db->get_where($tableName,$where); 
			return $query->result_array();	
		}
	//function confirmTable($uID)
//		{
//			$query = $this->db->query("Select * from booktable where uID = '$uID'  ");
//		}
	function gSelectWhereOr($tableName,$where,$orderby=NULL)
		{
			if(is_array($orderby))
				foreach($orderby as $col=>$val)
					$this->db->order_by($col,$val);		

			if(is_array($where))
				foreach($where as $col=>$val)
					$this->db->or_where($col,$val);		

		
			$query = $this->db->get_where($tableName); 
			return $query->result_array();	
		}
	function gSelectWhereLimit($tableName,$where,$offset,$per_page,$orderby=NULL)
		{
			if(is_array($orderby))
				foreach($orderby as $col=>$val)
					$this->db->order_by($col,$val);		
			
			if(is_array($where))
				$query = $this->db->get_where($tableName,$where,$per_page,$offset); 
			else
				$query = $this->db->get($tableName,$per_page,$offset); 
			return $query->result_array();	
		}
	function getuserbidstat($userID)
		{
			$result = NULL;
			$userbids = $this->db->dbprefix('userbids');
			$item = $this->db->dbprefix('auctions');
			$query=$this->db->query("select * from $userbids as UB, $item as I where UB.userID = '$userID' and I.iID = UB.iID group by UB.iID");
			$res = $query->result_array();
			foreach($res as $product)
				{					
					$this->db->where('iID', $product['iID']);
					$this->db->where('userID', $userID);
					$this->db->from('userbids');
					$count = $this->db->count_all_results();
					$itemName = $product['iName'];
					$result[$itemName]=$count."|".$product['iID'];
				}
			return $result;
		}
	function getItemBidStat($iID)
		{
			$result = NULL;
			$userbids = $this->db->dbprefix('userbids');
			$users = $this->db->dbprefix('users');
			$query=$this->db->query("select * from $userbids as UB,$users as U where iID = '$iID' and U.userID = UB.userID group by UB.userID");
			$res = $query->result_array();
			foreach($res as $bidinf)
				{					
					$userID = $bidinf['userID'];
					$username = $bidinf['username'];
					$query1=$this->db->query("select * from $userbids where iID = '$iID' and userID = '$userID'");
					$bids = $query1->num_rows;
					$result[$username]=$bids;					
				}
			return $result;
		}
	function countTicketCount($status)
		{
			$var = NULL;
			if($status=='Open') $var = " and ticketStatus = 'Open' ";
			if($status=='Closed') $var = " and ticketStatus = 'Closed' ";
			if($status=='On-Hold') $var = " and ticketStatus = 'On Hold' ";

			$ticket = $this->db->dbprefix('ticket');
			$users = $this->db->dbprefix('users');
			$query=$this->db->query("select * from $ticket as T, $users as U where T.userID = U.userID $var");
			return $query->num_rows;
		}	
	function getTickets($status,$offset,$per_page)
		{
			$var = NULL;
			if($status=='Open') $var = " and ticketStatus = 'Open' ";
			if($status=='Closed') $var = " and ticketStatus = 'Closed' ";
			if($status=='On-Hold') $var = " and ticketStatus = 'On Hold' ";
			
			$ticket = $this->db->dbprefix('ticket');
			$users = $this->db->dbprefix('users');
			$query=$this->db->query("select * from $ticket as T, $users as U where T.userID = U.userID $var order by ticketID desc limit $offset,$per_page");
			return $query->result_array();
		}	
	function getTicketInfo($ticketID)
		{
			$ticket = $this->db->dbprefix('ticket');
			$users = $this->db->dbprefix('users');
			$query=$this->db->query("select * from $ticket as T, $users as U where T.userID = U.userID and T.ticketID = '$ticketID' ");
			$ret['ticketInfo'] = $query->result_array();		
			$where['ticketID'] = $ticketID;	
			$ret['replies'] = $this->gSelectWhere('reply',$where);			
			return $ret;
		}	
	function getliveauction($iIDs)
		{
			$T = $this->currtime();
			$auctions = $this->db->dbprefix('auctions');
			$query=$this->db->query("select iID,tickTime,iStatus,iCurrBid,iHigestBidder,rev,lastBidTime,winner,pTimeLeft,bBids,minBid,totBids,bot,totBids,br,setBr,incrBy,fet,bbs,bidder_list,iName,starttime from $auctions $iIDs order by iID desc ");
			return $query->result_array();		
		}	

	function getallliveauctions($status=NULL,$offset,$per_page)
		{	
			if($status==-1)
				$status = " where (iCurrBid < rev) ";
			elseif($status!='all')
				$status = " where (iStatus = '2' or iStatus = '0') and (iCurrBid >= rev) ";
			else
				$status = " ";

			$auctions = $this->db->dbprefix('auctions');
			"select * from $auctions $status order by iID desc limit $offset,$per_page";
			$query=$this->db->query("select * from $auctions $status order by iID desc limit $offset,$per_page ");
			return $query->result_array();		
		}	
	function countcatauctions($catID,$subID)
		{
			$status = NULL;
			if($catID)
				$status = " and category = '$catID' ";
			else
				$status .= " and subcategory = '$subID' ";
				
			$auctions = $this->db->dbprefix('auctions');
			$query=$this->db->query("select * from $auctions where (iStatus = '0' or iStatus = '2') $status ");
			return $query->num_rows();		
		}
	function getallcategoryauctions($catID,$subID,$offset,$per_page)
		{	
			$status = NULL;
			if($catID)
				$status = " and category = '$catID' ";
			else
				$status .= " and subcategory = '$subID' ";
				
			$auctions = $this->db->dbprefix('auctions');
			$query=$this->db->query("select * from $auctions where (iStatus = '0' or iStatus = '2') $status order by iID desc limit $offset,$per_page ");
			return $query->result_array();		
		}	

	function bulk_action($act)
		{
			$auctions = $this->db->dbprefix('auctions');
			if($act == 'resume')
				$query=$this->db->query("update $auctions  set iStatus ='0',lastBidTime = '".$this->currtime()."',pTimeLeft='0' where (iStatus ='0' or iStatus ='2')");
			else
				$query=$this->db->query("update $auctions  set iStatus ='2' where (iStatus ='0' or iStatus ='2')");
		}
	function grabStatInfo($ID)
		{
			$newsletters = $this->db->dbprefix('newsletters');
			$sublist = $this->db->dbprefix('sublist');
			$users = $this->db->dbprefix('users');
			$query=$this->db->query("select * from $sublist as SL,$users as U where SL.userID = U.userID and SL.ID = '$ID'");
			return $query->result_array();		
		}
	function getPayInfo($payID)
		{
			$country = $this->db->dbprefix('country');
			$auctions = $this->db->dbprefix('auctions');
			$payment = $this->db->dbprefix('payment');
			$query=$this->db->query("select * from $auctions as AUC,$payment as PAY, $country as C where AUC.iID = PAY.iID and C.countryID = PAY.country and PAY.payID = '$payID'");
			return $query->result_array();		
		}
	function countWatchList($userID)
		{
			$auctions = $this->db->dbprefix('auctions');
			$watchlist = $this->db->dbprefix('watchlist');
			$query=$this->db->query("select * from $auctions as A,$watchlist as W where W.userID = '$userID'and A.iID = W.iID  ");
			return $query->num_rows;		
		}
	function watchList($userID,$offset,$per_page)
		{
			$auctions = $this->db->dbprefix('auctions');
			$watchlist = $this->db->dbprefix('watchlist');
			$query=$this->db->query("select * from $auctions as A,$watchlist as W where W.userID = '$userID'and A.iID = W.iID limit $offset,$per_page");
			return $query->result_array();		
		}
	function email($to,$from,$fromName,$subject,$body)
		{
			$this->load->library('email');
			$config['charset'] = 'UTF-8';
			$config['mailtype'] = 'html';
			$config['wordwrap'] = TRUE;
			$this->email->initialize($config);
			$this->email->from($from, $fromName);
			$this->email->to($to);
			$this->email->subject($subject);
			$this->email->message($body);
			$this->email->send();		
		}
	function setwinner($iHigestBidder,$itemID)
		{
			$auctions = $this->db->dbprefix('auctions');
			$query=$this->db->query("update $auctions set winner = '$iHigestBidder', iStatus='1' , bot='0' where iID = '$itemID'");			
		}
	function return_user_bids($arr)
		{			
			$users = $this->db->dbprefix('users');
			$bidbutlers = $this->db->dbprefix('bidbutlers');
			$auctions = $this->db->dbprefix('auctions');
			if(is_array($arr) && count($arr))
				foreach($arr as $username=>$bids)
					{	
						$new_string = NULL;
						$ex = explode("-",$bids);
						$bbs = $ex[2];
						$iID = $ex[3];
						$query=$this->db->query("update $users set bids = bids+".$ex[0]." where userID = '$username'");	
						$query=$this->db->query("delete from $bidbutlers where bbID = '".$ex[1]."'");							
								$ex1 = explode("#",$bbs);
								foreach($ex1 as $r1)
									{
										$ex2 = explode("|",$r1);
										if($ex2[0])
										{
											$bb_userID = $ex2[0];
											if($bb_userID == $username) {}
											else $new_string .= $r1."#";																				
										}
									}
						$query=$this->db->query("update $auctions set bbs = '$new_string' where iID = '$iID'");		
					}
		}
	function getUserBidButlers($userID)
		{
			$auctions = $this->db->dbprefix('auctions');
			$bidbutlers = $this->db->dbprefix('bidbutlers');
			$query=$this->db->query("select * from $bidbutlers as B,$auctions as A where B.iID = A.iID and B.userID = '$userID'  ");			
			return $query->result_array();		
		}
	function seourl($p)
		{
			$pattern[0] = '/\s/i';
			$pattern[1] = '/\'/i';
			$pattern[2] = '/"/i';
			$pattern[3] = '/\(/i';
			$pattern[4] = '/\)/i';
			$pattern[5] = '/\]/i';
			$pattern[6] = '/\[/i';
			$pattern[7] = '/\#/';
			$pattern[8] = '/\./';
			$pattern[9] = '/\&/';
			$pattern[10] = '/\//';
			$pattern[11] = '/\\\/';
			$pattern[12] = '/`/';
			$replacement[0] = '-';
			$replacement[1] = '';
			$replacement[2] = '';
			$replacement[3] = '';
			$replacement[4] = '';
			$replacement[5] = '';
			$replacement[6] = '';
			$replacement['7']='';
			$replacement['8']='';
			$replacement['9']='and';
			$replacement['10']='-';
			$replacement['11']='-';
			$replacement['12']='';
			return preg_replace($pattern, $replacement, $p);
		}
	function checkImg($path)
		{
					if(!file_exists(getcwd()."/items/".$path) && !is_dir(getcwd()."/items/".$path))
						return 'noimage.gif';
					else
						return $path;
		
		}		
	function getactiveauctions()
		{
			$auctions = $this->db->dbprefix('auctions');
			$query=$this->db->query("select * from $auctions where fet = 'No' and (iStatus='0' or iStatus='2') order by rand() limit 5 ");			
			return $query->result_array();		
		}
	function getfeaturedauctions()
		{
			$auctions = $this->db->dbprefix('auctions');
			$query=$this->db->query("select * from $auctions where fet = 'Yes' and (iStatus='0' or iStatus='2') order by rand() limit 3  ");			
			return $query->result_array();		
		}
	function prodcutCategory()
		{
			$returnArray = NULL;
			$category = $this->db->dbprefix('category');
			$query=$this->db->query("select * from $category where pID = '0' order by catName  ");			
			foreach($query->result_array() as $row)
				{
					$catID = $row['catID'];
					$catName = $row['catName'];
					$url = $this->seourl($row['catName']."-$catID|0");
					$returnArray[$url] = $catName;
						$query_sub=$this->db->query("select * from $category where pID = '$row[catID]' order by catName  ");			
							foreach($query_sub->result_array() as $row_sub)
								{									
									$catID_sub = $row_sub['catID'];
									$catName_sub = " &nbsp; -- ".$row_sub['catName'];
									$url = $this->seourl($row['catName']."-".rtrim($row_sub['catName'],' ')."-$catID|$catID_sub");
									$returnArray[$url] = $catName_sub;
								}
				}
			return $returnArray;
		}
	function countActiveSearch($str)
		{		
			$auctions = $this->db->dbprefix('auctions');
			$query=$this->db->query("select * from $auctions where iName like '%$str%' or iDesc like '%$str%' ");			
			return $query->num_rows;		
		}
	function getActiveSearch($str,$offset,$per_page)
		{		
			$auctions = $this->db->dbprefix('auctions');
			$query=$this->db->query("select * from $auctions where iName like '%$str%' or iDesc like '%$str%' limit $offset,$per_page ");			
			return $query->result_array();		
		}
	function copyItem($iID)
		{
			$auctions = $this->db->dbprefix('auctions');
			$where['iID'] = $iID;
			$query=$this->db->query("select * from $auctions where iID = '$iID' ");
			foreach($query->result_array() as $row)
				{
					$in['iID'] = $row['iID'];
					$in['bbs'] = $row['bbs'];
					$in['iName'] = $row['iName'];
					$in['iDesc'] = $row['iDesc'];
					$in['category'] = $row['category'];
					$in['subcategory'] = $row['subcategory'];
					$in['iOrgPrice'] = $row['iOrgPrice'];
					$in['iStartTime'] = $row['iStartTime'];
					$in['iExpTime'] = $row['iExpTime'];
					$in['iCurrBid'] = $row['iCurrBid'];
					$in['iHigestBidder'] = $row['iHigestBidder'];
					$in['tickTime'] = $row['tickTime'];
					$in['winner'] = $row['winner'];
					$in['iStatus'] = $row['iStatus'];
					$in['lastBidTime'] = $row['lastBidTime'];
					$in['pauseAuction'] = $row['pauseAuction'];
					$in['pTimeLeft'] = $row['pTimeLeft'];
					$in['minBid'] = $row['minBid'];
					$in['totBids'] = $row['totBids'];
					$in['bBids'] = $row['bBids'];
					$in['bot'] = $row['bot'];
					$in['br'] = $row['br'];
					$in['setBr'] = $row['setBr'];
					$in['fet'] = $row['fet'];
					$in['img1'] = $row['img1'];
					$in['thumb1'] = $row['thumb1'];
					$in['img2'] = $row['img2'];
					$in['thumb2'] = $row['thumb2'];
					$in['img3'] = $row['img3'];
					$in['thumb3'] = $row['thumb3'];
					$in['img4'] = $row['img4'];
					$in['thumb4'] = $row['thumb4'];
					$in['penny'] = $row['penny'];
					$in['incrBy'] = $row['incrBy'];
					$in['img5'] = $row['img5'];
					$in['thumb5'] = $row['thumb5'];
					$in['paid'] = $row['paid'];
					$this->gInsert('dead_auctions',$in);
					$this->gDelete('auctions',$where);
				
				}
		}
	function getPaidPayment($iID)
		{
			$dead_auctions = $this->db->dbprefix('auctions');
			$username = $this->session->userdata('username');
			$payment = $this->db->dbprefix('payment');
			$query=$this->db->query("select * from $dead_auctions as DA, $payment as P where DA.iID = P.iID and DA.iID = '$iID' ");			
			return $query->result_array();		
		}
	function countPaidItem()
		{
			$dead_auctions = $this->db->dbprefix('auctions');
			$payment = $this->db->dbprefix('payment');
			$query=$this->db->query("select * from $dead_auctions as DA, $payment as P where DA.iID = P.iID");			
			return $query->num_rows;		
		}
	function countPayment($status)
		{
			$dead_auctions = $this->db->dbprefix('auctions');
			$payment = $this->db->dbprefix('payment');
			$query=$this->db->query("select * from $dead_auctions as DA, $payment as P where DA.iID = P.iID and DA.iID = P.iID");			
			return $query->num_rows;		
		}
	function getPayments($status,$offset,$per_page )
		{
			$ext = NULL;
			if($status)
				$ext = " and status = 'Delivered' ";
			else
				$ext = " and status = 'Paid' ";
			$dead_auctions = $this->db->dbprefix('auctions');
			$payment = $this->db->dbprefix('payment');
			//echo "select * from $dead_auctions as DA, $payment as P where DA.iID = P.iID $ext limit $offset,$per_page";
			$query=$this->db->query("select * from $dead_auctions as DA, $payment as P where DA.iID = P.iID $ext limit $offset,$per_page");			
			return $query->result_array();		
		}
	function get_last_won_auction()
		{
			$dead_auctions = $this->db->dbprefix('auctions');			
			$query=$this->db->query("select * from $dead_auctions order by iID desc limit 1");			
			return $query->result_array();		
		}
	function countCartProduct($userID,$iID)
		{
			$rowauction = $this->db->dbprefix('rowauction');			
			$cart = $this->db->dbprefix('cart');			
			$query=$this->db->query("select * from $rowauction as R, $cart as C where C.rowID = R.rowID and C.userID = '$userID' and C.rowID = '$iID' and orderID = '0' ");			
			return $query->num_rows();		
		}
	function getCartProduct($userID)
		{
			$rowauction = $this->db->dbprefix('rowauction');			
			$cart = $this->db->dbprefix('cart');			
			$query=$this->db->query("select * from $rowauction as R, $cart as C where C.rowID = R.rowID and C.userID = '$userID' and orderID = 0 order by cartID desc ");			
			return $query->result_array();		
		}	
	function getOrderItems($orderID)
		{
			$rowauction = $this->db->dbprefix('rowauction');			
			$cart = $this->db->dbprefix('cart');			
			$query=$this->db->query("select * from $rowauction as R, $cart as C where C.rowID = R.rowID and C.orderID = '$orderID'  order by cartID desc ");			
			return $query->result_array();		
		}
			
	function countliveauction()
		{
			$time = $this->currtime();
			$auctions = $this->db->dbprefix('auctions');
			$query=$this->db->query("select * from $auctions where (iStatus='0' or iStatus='2') and starttime <= '$time'");			
			return $query->num_rows();				
		}
	function countfutureauction()
		{
			$time = $this->currtime();
			$auctions = $this->db->dbprefix('auctions');
			$query=$this->db->query("select * from $auctions where (iStatus='0' or iStatus='2') and starttime > '$time'");			
			return $query->num_rows();				
		}

	function geliveauctions($offset,$per_page)
		{
			$time = $this->currtime();
			$auctions = $this->db->dbprefix('auctions');
			
			$query=$this->db->query("select * from $auctions where iCurrBid >= rev and starttime <= '$time' and (iStatus='0' or iStatus='2') order by apriority asc,iID asc   ");			
			return $query->result_array();				
		}
	function getRevAuctions($offset,$per_page)
		{
			$time = $this->currtime();
			$auctions = $this->db->dbprefix('auctions');
			$query=$this->db->query("select * from $auctions where iCurrBid < rev and starttime <= '$time' order by apriority asc,iID asc  ");			
			return $query->result_array();				
		}
	function getfutureauctions($offset,$per_page)
		{
			$time = $this->currtime() ;
			$auctions = $this->db->dbprefix('auctions');
			$query=$this->db->query("select * from $auctions where (iStatus='0' or iStatus='2') and starttime > '$time' order by apriority asc,iID asc limit $offset,$per_page  ");			
			return $query->result_array();				
		}
	function get_related_acution($category)
		{
			$auctions = $this->db->dbprefix('auctions');
			$query=$this->db->query("select * from $auctions where iStatus='0' or iStatus='2' and category = '$category' limit 4  ");			
			return $query->result_array();				
		}
	function getmaxuser()
		{
			$users = $this->db->dbprefix('users');
			$query=$this->db->query("select * from $users");			
			return $query->num_rows();				
		}
	function count_referrals($userID)
		{
			$users = $this->db->dbprefix('users');
			$query=$this->db->query("select * from $auctions where (iStatus='0' or iStatus='2') and starttime <= '$time' order by apriority asc,iID asc limit $offset,$per_page  ");			
			return $query->result_array();				
		}
	function checkUsername($username,$mailID)
		{
			$users = $this->db->dbprefix('users');
			$query=$this->db->query("select * from $users where mailID = '$mailID' or username = '$username' ");			
			if($query->num_rows())
				{
					return FALSE;
				}
			else
				{
					$bots = $this->db->dbprefix('bots');
					$flag = 0;
					$query=$this->db->query("select * from $bots");			
					foreach($query->result_array() as $row)
						{
							$bn = $row['botNames'];
							$exp = explode("|",$bn);							
							foreach($exp as $bname)
							{
								if($username == $bname)
									return FALSE;
							}
						}					
				}
			return TRUE;	
		}
	function checkBotname($botnames)
		{
			$bts = explode("|",$botnames);		
			$users = $this->db->dbprefix('users');
			$query=$this->db->query("select username from $users");			
			if($query->num_rows())
				{
					foreach($query->result_array() as $row)
						{
							foreach($bts as $botName)
								if($botName == $row['username'])
									return $row['username'];
						}
				}
			return TRUE;	
		}
	function countryName($countryID)
		{
			$country = $this->db->dbprefix('country');
			$query=$this->db->query("select * from $country where countryID = '$countryID'");			
			foreach($query->result_array() as $row)
				return $row['countryName'];
		}
	function getOrdersCount($para)
		{
			if($para!='all')
				$wh = " and orderStatus = '$para'";
			else
				$wh = NULL;			
			$users = $this->db->dbprefix('users');
			$orders = $this->db->dbprefix('orders');
			$query=$this->db->query("select * from $users as U,$orders as O where U.userID = O.userID $wh ");			
			return $query->num_rows();				
		}
	function getOrders($para,$offset,$per_page)
		{
			if($para!='all')
				$wh = " and orderStatus = '$para'";
			else
				$wh = NULL;			
			$users = $this->db->dbprefix('users');
			$orders = $this->db->dbprefix('orders');
			$query=$this->db->query("select * from $users as U,$orders as O where U.userID = O.userID $wh order by orderID desc limit $offset,$per_page  ");			
			return $query->result_array();				
		}
	function getSingleOrders($orderID)
		{		
			$users = $this->db->dbprefix('users');
			$orders = $this->db->dbprefix('orders');
			$query=$this->db->query("select * from $users as U,$orders as O where U.userID = O.userID and O.orderID = '$orderID'");			
			return $query->result_array();				
		}
	function get_user_ticket_data($ticketID)
		{
			$users = $this->db->dbprefix('users');
			$ticket = $this->db->dbprefix('ticket');
			$query=$this->db->query("select * from $users as U, $ticket as T where T.ticketID='$ticketID' and T.userID = U.userID ");			
			return $query->result_array();				
		}
	function check_active_bb($userID)
		{
			$bidbutlers = $this->db->dbprefix('bidbutlers');
			$auctions = $this->db->dbprefix('auctions');
			$query=$this->db->query("select * from $bidbutlers as B, $auctions as A where B.userID='$userID' and A.iID = B.iID and (A.iStatus = 0 or iStatus = 2)");
			return $query->result_array();				
		}
	function incr_price($iID)
		{
			$auctions = $this->db->dbprefix('auctions');
			$query=$this->db->query("update $auctions set iCurrBid = iCurrBid+0.01,bid4=1 where iID = '$iID' and bid4=0");
		}
	function getUsername($userID)
		{
			$users = $this->db->dbprefix('users');
			$query=$this->db->query("select username from $users where userID = '$userID' ");			
			$my_array = $query->result_array();				
			return $my_array[0]['username'];
		}
	function check_active_bb_by_item($userID,$iID)
		{
			$bidbutlers = $this->db->dbprefix('bidbutlers');
			$auctions = $this->db->dbprefix('auctions');
			$query=$this->db->query("select * from $bidbutlers as B, $auctions as A where B.userID='$userID' and B.iID = '$iID' and A.iID = B.iID and (A.iStatus = 0 or iStatus = 2)");
			return $query->result_array();				
		}
	function get_peel_ads()
		{
			$peelads = $this->db->dbprefix('peelads');
			$query=$this->db->query("select * from $peelads where peelStatus = '1' order by rand() limit 1");
			return $query->result_array();				
		}
	function get_sginup_data($start,$end)
		{
			$users = $this->db->dbprefix('users');						
			$query=$this->db->query("select * from $users where memberSince between '$start' and '$end'");
			return $query->result_array();				
		}
	function latest10LiveAuction()
		{
			$auctions = $this->db->dbprefix('auctions');						
			$query=$this->db->query("select * from $auctions where (iStatus = '0' or iStatus = '2') limit 10");
			return $query->result_array();				
		}
	function latest10LiveEnded()
		{
			$auctions = $this->db->dbprefix('auctions');						
			$query=$this->db->query("select * from $auctions where iStatus = '1' limit 10 ");
			return $query->result_array();				
		}
	function searchUser($qs)
		{
			$users = $this->db->dbprefix('users');						
			$query=$this->db->query("select * from $users $qs ");
			return $query->result_array();				
		}
	function count_all_bids($userID)
		{
			$userbids = $this->db->dbprefix('userbids');						
			$auctions = $this->db->dbprefix('auctions');						
			$query=$this->db->query("select * from $userbids as UB, $auctions as A where UB.userID = '$userID' and A.iID = UB.iID");
			return $query->num_rows();				
		}
	function get_all_bids($userID,$offset,$per_page)
		{
			$userbids = $this->db->dbprefix('userbids');						
			$auctions = $this->db->dbprefix('auctions');						
			$query=$this->db->query("select * from $userbids as UB, $auctions as A where UB.userID = '$userID' and A.iID = UB.iID  order by ubID desc limit $offset,$per_page");
			return $query->result_array();				
		}
	function ad_user_bids($bidpack,$userID,$method,$txid)
		{
			$conf = $this->gSelectStar('siteconfiguration');
			$where = NULL;
			$where['userID'] = $userID; 
			$userdata = $this->gSelectWhere('users',$where);
			
			$where = NULL;
			$where['id'] = 12; 
			$mail_data = $this->gSelectWhere('emailtemplate',$where);
			
			$where = NULL;
			$where['bpID'] = $bidpack; 
			$bidata = $this->gSelectWhere('bidpack',$where);
			
			
			$P[0] = '/{USERNAME}/';
			$P[1] = '/{BIDPACK}/';
			$P[2] = '/{BIDS}/';
			$P[3] = '/{BONUSBIDS}/';
			$P[4] = '/{TOTALBIDS}/';
	
			$R[0] = $userdata[0]['username'];
			$R[1] = $bidata[0]['name'];
			$R[2] = $bidata[0]['bids'];
			$R[3] = $bidata[0]['bonus'];
			$R[4] = $bidata[0]['bids'] + $bidata[0]['bonus'];
			
			$mail_body  = preg_replace($P,$R,$mail_data[0]['details']);
			$send_mail = $this->email($userdata[0]['mailID'],$mail_data[0]['fromID'],$mail_data[0]['fromName'],$mail_data[0]['subject'],$mail_body);
	
			$userbids = $userdata[0]['bids'];
			$newbids = $bidata[0]['bids'];
			$bonus = $bidata[0]['bonus'];
			$bpName = $bidata[0]['name'];
			$bprice = $bidata[0]['price'];
			$newbids = $userbids + $newbids + $bonus;
			
			$where = NULL;		
			$where['userID']= $userID;
			$updata['bids'] = $newbids;
			$update = $this->gUpdate('users',$where,$updata);
			
			$insData['userID'] = $userID;
			$insData['details'] = "Purches bid pack: $bpName";
			$insData['amount'] = number_format(($bprice/$usd_inr),2);
			$insData['totalBids'] = $newbids;
			$insData['purTime'] = $this->currtime();
			$insData['packBids'] = $bidata[0]['bids'];
			$insData['bonusBids'] = $bonus;
			$ins = $this->gInsert('purches_history',$insData);
		}	
	function getBids($userID)	
		{
			$wh['userID'] = $userID;
			$x = $this->gSelectWhere('users',$wh);
			foreach($x as $row)
				return $row['bids'];
		}
	function payref($userID)
		{
			$wh['userID'] = $userID;
			$wh['paidStatus'] =0;
			$x = $this->gSelectWhere('userref',$wh);
			foreach($x as $row)
				{
					$ref = $row['pUserID'];
					$refID = $row['refID'];
					$wxy['refID'] = $refID;
					$nd['paidStatus'] = $this->currtime();
					$this->gUpdate('userref',$wxy,$nd);
					
					$wx['userID'] = $userID;
					$userdata = $this->gSelectWhere('users',$wx);
					$site_info = $this->gSelectStar('siteconfiguration');
					$free_ref_bids = $site_info[0]['freerefbids'];
					$free_reg_bids = $site_info[0]['freeregbids'];
					
					$where = NULL;
					$where['userID'] = $ref;
					$ref_user = $this->gSelectWhere('users',$where);
					$new_user_bids = $ref_user[0]['bids'] + $free_ref_bids;
					$nb['bids'] = $new_user_bids;					
					$update_user_bids = $this->gUpdate('users',$where,$nb);
					
					$where = NULL;
					$where['id'] = 5;
					$web = $this->gSelectWhere('emailtemplate',$where);
					$webPage = $web[0]['details'];
					$fromName = $web[0]['fromName'];
					$fromID = $web[0]['fromID'];
					$replyTo = $web[0]['replyTo'];
					$Subject = $web[0]['subject'];
		
					$patterns = $replacements = NULL;
					$patterns[0] = '/{USERNAME}/';
					$patterns[1] = '/{FREE_BIDS}/';
					$patterns[2] = '/{REFF_USERNAME}/';
					$replacements[0] = $ref_user[0]['username'];
					$replacements[1] = $free_ref_bids;
					$replacements[2] = $userdata[0]['username'];
					$details = preg_replace($patterns, $replacements, $webPage);
					$TO = $ref_user[0]['mailID'];
					$this->email($TO,$fromID,$fromName,$Subject,$details);

					$insData['userID'] = $ref;
					$insData['details'] = "Referrals free bids for user :".$userdata[0]['username'];
					$insData['amount'] = 0;
					$insData['totalBids'] = $free_ref_bids;
					$insData['purTime'] = $this->currtime();
					$insData['packBids'] = $free_ref_bids;
					$insData['bonusBids'] = 0;
					$ins = $this->gInsert('purches_history',$insData);
				}
		}
	function getUserWonAuctions($username,$start,$end)
		{
			//winner
			$auctions = $this->db->dbprefix('auctions');
			$query=$this->db->query("select * from $auctions where winner = '$username' and lastBidTime between $end and $start ");
			//echo "select * from $auctions where winner = '$username' and lastBidTime between $end and $start";
			return $query->num_rows();				
			
		}
	function currtime()
		{
			$currtime = time();;
			include(getcwd()."/system/application/config/config.php");
			$time_correction = $config['time_correction'];
			$ex = explode(":",$time_correction);
			$h = $ex[0];
			$m = $ex[1];
			$hs = 3600 * $h;
			$ms = 60 * $m;
			return $currtime = time() + ($hs + $ms);
		}
		
	function check_dup_username($uname)	
		{
			$query=$this->db->query("select * from users where username = '$uname' ");
			return $query->num_rows();				
		}
		
	function check_dup_email($email,$userid)	
		{
			$query=$this->db->query("select * from users where email = '$email' AND id!='$userid'");
			return $query->num_rows();				
		}
	
	function check_dup_number($number,$userid)	
		{
			$query=$this->db->query("select * from users where number = '$number'  AND id!='$userid' ");
			return $query->num_rows();				
		}
		
}	
?>