<?php

//////////////////////////////////////////
///  Query class for api & website    
//////////////////////////////////////// 
include_once "/home/steelmin/public_html/inc/config1.php"; 
include_once('helper.php');
define("DEFAULT_NEWS_LIMIT",5); 
define("DEFAULT_TENDERS_LIMIT",5); 
class ApiQuery{
    
    public static function getInsightsRows($memid=0, $source='listing', $resourceType="app", $type='default', $filter=array(), $pageType='post', $limit=DEFAULT_NEWS_LIMIT, $offset=0, $appType='SM'){
		
        $result=array(); 
        $news = array();

		/* $sql = "SELECT n.*, 'Y' as isBookmarked FROM `tbl_news_post` n";
		$sql .= " WHERE n.`id` > 0 ";//and n.`post_id`<>107846 */
		$sql = "SELECT n.*, 
				(select id from tbl_bookmark as b where n.post_id=b.lead_id and user_id=$memid and section='news') as isBookmarked, 
				rm.id  as author_id, rm.RM_email as author_email, rm.imageURL as author_imageURL, rm.facebookId as author_facebookId, rm.twitterId as author_twitterId, rm.whatsappUrl as author_whatsappUrl 
		FROM `tbl_news_post` n 
				left outer join 
				tbl_rel_manager rm
				ON rm.RM_name = n.author_name";
		$sql .= " WHERE n.`id` > 0 ";//and n.`post_id`<>107846
		//RM_email

		if(isset($filter['searchTerms']) && !empty($filter['searchTerms'])) {
			$sql .= " AND (";

			$implode = array();

			$words = explode(' ', trim(preg_replace('/\s+/', ' ', $filter['searchTerms'])));

			foreach ($words as $word) {
				$implode[] = "CONCAT(n.`post_title`, n.`post_content`) LIKE '%" . mysql_real_escape_string($word) . "%'";
			}

			if ($implode) {
				$sql .= " " . implode(" AND ", $implode) . "";
			}

			$sql .= ")";
		}   
		
		if(isset($filter['itemID'])) {
			if(is_array($filter['itemID']))
				$sql .= " AND n.`post_id` IN (" . implode($filter['itemID']) .")";
			else
				$sql .= " AND n.`post_id` = " . (int)$filter['itemID'];
		}
		
		if(isset($filter['commodityIDs'])) {
			$sql .= " AND (";
				$sql .= " `commodityId` IN (";
				$sql .= implode("', '", $filter['commodityIDs']);
				$sql .= ")";
			$sql .= " OR ";	
				$sql .= " `subCommodityId` IN (";
				$sql .= implode("', '", $filter['commodityIDs']);
				$sql .= ")";
			$sql .= ")";
		}
		
		if(isset($filter['regionID'])) {
			$sql .= " AND (";
			foreach($filter['regionID'] as $k=>$regionIDs) {
				if($k>0)
					$sql .= " OR ";
				$sql .= "(regionID like '$regionIDs' or regionID like '9,%' or regionID like '%,9')";
			}
			$sql .= ")";
			//(regionIds like '$regionIDs' or regionIds like '9,%' or regionIds like '%,9')
		}
		
		if(isset($filter['timeline'])) {
			//$sql .= " AND DATE(post_date) = Date('" . $filter['post_date'] . "')";
			switch(strtolower($filter['timeline'])){
				case '1w':
					$sql .= " AND DATE(n.`post_date`) > DATE_SUB(now(), INTERVAL 7 DAYS)";
				break;
				case '2w':
					$sql .= " AND DATE(n.`post_date`) > DATE_SUB(now(), INTERVAL 7 DAYS)";
				break;
				case '2d':
                    $sql .= " AND DATE(n.`post_date`) > DATE_SUB(now(), INTERVAL 2 DAYS)";
				break;
			}
		}
		
		$web_domain = $appType=='CM' ? "coalmint.com" : "steelmint.com";
		$sql .= " AND MATCH(n.`for_smgroup_company`) AGAINST ('";
        if(strpos($web_domain, "coalmint.com") !== false) {
            $sql .= "coalmint.com";
        } else {
			 $sql .= "steelmint.com";
		}
        $sql .= "' IN BOOLEAN MODE)";
		
		
		$orderBY = "";
		
		if($source!='') {//source
			
			switch($source){				
				case 'home':
				break;
				case 'bookmarks':
                    //$sql .= " AND DATE(n.`post_date`) > DATE_SUB(now(), INTERVAL 6 MONTH)";
				break;
				case 'portfolio':
                    //$sql .= " AND DATE(n.`post_date`) > DATE_SUB(now(), INTERVAL 6 MONTH)";
				break;
				case 'relatedInsighs':
                    //$sql .= " AND DATE(n.`post_date`) > DATE_SUB(now(), INTERVAL 6 MONTH)";
				break;
				case 'recent':
                    //$sql .= " AND DATE(n.`post_date`) > DATE_SUB(now(), INTERVAL 6 MONTH)";
				break;
				default: //like listing , mapview and others
					//$sql .= " AND DATE(n.`post_date`) > DATE_SUB(now(), INTERVAL 6 MONTH)";
					$orderBY = " post_date Desc, post_time Desc ";
				break;
			}			
		}
		
		$sort_data = array(
			'n.`post_title`',
			'n.`author_name`',
			'n.`post_date`',
			'n.`created_at`',
			'n.`most_read`',
			't.`tags`',
			'n.`created_at`'
		);

		if (isset($filter['sort']) && in_array($filter['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $filter['sort'];
			if(isset($filter['order']))
				$sql .= " " . $filter['order'];
		}elseif($orderBY!=''){
			$sql .= " ORDER BY " . $orderBY;
		}else {
			$sql .= " ORDER BY n.`created_at` Desc";
		}
		
		$sql .= " LIMIT " . (int)$offset . ", " . (int)$limit;
		
		if(isset($filter['debug']) && $filter['debug'] == 1) {
            echo "<pre>", print_r($filter), $sql, "</pre>";
          
        } 		
            
		$query = mysql_query($sql);
		$region='';$regionCnt = 0;
		$viewMore = array();
		while($row = mysql_fetch_assoc($query)){ 
			
			$result = array();
			$postId = intval($row['post_id']);
			$result['itemId'] = $postId;
			$result['title'] = removeMSWordattributes(html_entity_decode($row['post_title'], ENT_QUOTES));			
			
			if($pageType=='pre'){
				$content = 'hidden';
			}else{
				$content = removeMSWordattributes(html_entity_decode($row['post_content'], ENT_QUOTES));
			
				if(isset($filter['language']) && !empty($filter['language']) && strtolower($filter['language'])!='en'){
					if($row['news_in_'.$filter['language'].'']){
						$news_in_language = html_entity_decode($row['news_in_'.$filter['language'].''], ENT_QUOTES);
						if(strpos($news_in_language, "0997788997788990") !== false){
							list($_news_description, $_news_title) = explode('0997788997788990', $news_in_language);
							$result['post_title'] = $_news_title;
							$content = $_news_description;
						}else{
							$content = (html_entity_decode($news_in_language, ENT_QUOTES));
						}
					}
				}
			}
			
			if($pageType=='post'){
				$breaks = array("\n", "\r","\t");
				$content = str_replace($breaks, " ", $content);
				$content = html_entity_decode($content, ENT_QUOTES);
				$content = preg_replace('/\\\"/im', '"', $content);
				$content = preg_replace('/\\"/im', '"', $content);				
			}

			$post_date = date('M d, Y H:i', strtotime($row['post_date'] . " " . $row['post_time']));
			 
			$result['description'] = $content;			
			$result['thumbnailImageURL'] = str_replace("http://", "https://", $row['small_image']);
			$result['mainImageURL'] = str_replace("http://", "https://", $row['image']);
			$result['videoURL'] = str_replace("http://", "https://", $row['videoURL']);
			$result['thumbnailType'] = $row['videoURL']!=''?'video':'image';
			$result['postedDateTime'] = $post_date;
			
			$result['latitude'] = $row['latitude'];
			$result['longitude'] = $row['longitude'];
			$result['commodityId'] = $row['subCommodityId'];
			$result['regionId'] = end(explode(",",$row['regionId']));
			$result['isBookmarked'] = $row['isBookmarked']!='' ? 'y':'n' ; 
			$result['noOfReads'] = $row['most_read'] ? $row['most_read'] : 0; 
			$result['authorDetails'] = array(
											"authorId"=>$row['author_id'],
											"imageURL"=>$row['author_imageURL'],
											"name"=>$row['author_name'],
											"emailId"=>$row['author_email'],
											"socialAccounts"=>array(
												'facebook'=>$row['author_facebookId'],
												'twitter'=>$row['author_twitterId'],
												'whatsapp'=>$row['author_whatsappUrl']
											)
										);
			
			$re = explode(',',$row['region']);
			
			if($source=='mapview' && in_array($region, $re)!==false && $region!=''){
				//$result['123'] = $regionCnt.'-'.$region.'-'.$row['region'].'--'.in_array($region, explode(',',$re)).'<br>';
				//$viewMore[] = $result;
				//echo '<br>->'.$regionCnt;
				$mainEle[$postId]['viewMore'][] = $result;
			}else{				
				//$regionCnt = 0;
				$region = $row['region'];
				if(count($viewMore)>0){
					$result['viewMore'][] = $viewMore;
					$viewMore = array();
				}
				//$postId = $row['post_id'];
				$mainEle[$postId] = $result;				
			}
			//$news[] = $result;
		}
		
		foreach($mainEle as $ele):
			$news[]=$ele;
		endforeach;
		
		if($type=='default'){
			//json file $news;
		}
		
		//include_once "/home/steelmin/public_html/inc/desconfig.php";
		return $news;
    }

	public static function getTendersRows($memid=0, $source='listing', $resourceType="app", $type='default', $filter=array(), $pageType='post', $limit=DEFAULT_TENDERS_LIMIT, $offset=0, $appType='SM'){
		$result=array(); 
        $tenders = array();
		$currDate = date('Y-m-d');
		$sql = "";				  
		$sql .= " WHERE n.`id` > 0 ";//and n.`post_id`<>107846
		//RM_email
		
		if($source!='') {//source
			
			switch($source){				
				case 'listing':
				break;
				case 'mapView':
                    //$sql .= " AND DATE(n.`post_date`) > DATE_SUB(now(), INTERVAL 6 MONTH)";
				break;
				case 'portfolio':
                    /* $common = new Common();
					$temp = $common->getUserPortfolioV2019($user_id, 'tenders', $appType);		
					 */
					$sqlTC = "select regionId, commodityId from tbl_user_portfolio where user_id=".$memid." and type='tenders' and appType='".$appType."'"; 
					$rsTC = mysql_query($sqlTC)or die(mysql_error().$sqlTC);    
					$temp = mysql_fetch_array($rsTC,MYSQL_ASSOC);	
					
					if($temp['commodityId']){
						$commodityIDs = $temp['commodityId'];
						$filter['commodityID'] = explode(',',$commodityIDs);
					}
					if($temp['regionId']){
						$regionIDs = $temp['regionId'];
						$filter['regionID'] = explode(',',$regionIDs);
					}
					
					if(empty($filter['commodityID']) && empty($filter['regionID'])){
						return $result;
					}
					/* if(empty($filter['commodityID']) && empty($filter['regionID'])){
						return new Response( json_decode(html_entity_decode(json_encode($result) ) ));
					} */
				   //$sql .= " AND DATE(n.`post_date`) > DATE_SUB(now(), INTERVAL 6 MONTH)";
				break;
				case 'home':
                    //$sql .= " AND DATE(n.`post_date`) > DATE_SUB(now(), INTERVAL 6 MONTH)";
				break;
				case 'bookmarks':
                    //$sql .= " AND DATE(n.`post_date`) > DATE_SUB(now(), INTERVAL 6 MONTH)";
				break;
				case 'recent':
                    //$sql .= " AND DATE(n.`post_date`) > DATE_SUB(now(), INTERVAL 6 MONTH)";
				break;
				case 'featured':
                    //$sql .= " AND DATE(n.`post_date`) > DATE_SUB(now(), INTERVAL 6 MONTH)";
				break;
				case 'similar':
                    //$sql .= " AND DATE(n.`post_date`) > DATE_SUB(now(), INTERVAL 6 MONTH)";
				break;
				default: //like listing , mapview and others
					//$sql .= " AND DATE(n.`post_date`) > DATE_SUB(now(), INTERVAL 6 MONTH)";
					$orderBY = " post_date Desc, post_time Desc ";
				break;
			}			
		}
		
		if(isset($filter['searchTerms']) && !empty($filter['searchTerms'])) {
			$sql .= " AND (";

			$implode = array();

			$words = explode(' ', trim(preg_replace('/\s+/', ' ', $filter['searchTerms'])));

			foreach ($words as $word) {
				$implode[] = "REPLACE(UPPER(concat(title, sub_category_all, category_all, company_name, buy_sell,offer_type, country_name, state_name, region, description, remarks, floor_price, qty, grade)), ' ', '') LIKE '%" . mysql_real_escape_string($word) . "%'";
			}

			if ($implode) {
				$sql .= " " . implode(" AND ", $implode) . "";
			}

			$sql .= ")";
		}   
		
		if(isset($filter['itemID'])) {
			if(is_array($filter['itemID']))
				$sql .= " AND n.`id` IN (" . implode($filter['itemID']) .")";
			else
				$sql .= " AND n.`id` = " . (int)$filter['itemID'];
		}
		
		/* if(isset($filter['commodityID'])) {
			$sql .= " AND (";
				$sql .= " `commodityId` IN (";
				$sql .= implode("', '", $filter['commodityID']);
				$sql .= ")";
			$sql .= " OR ";	
				$sql .= " `subCommodityId` IN (";
				$sql .= implode("', '", $filter['commodityID']);
				$sql .= ")";
			$sql .= ")";
		} */
		if(isset($filter['commodityID'])) {
			$sql .= " AND (";
			foreach($filter['commodityID'] as $k=>$commodityID) {
				if($k>0)
					$sql .= " OR ";
				$sql .= "((commodityId like '$commodityID' or commodityId like '$commodityID,%' or commodityId like '%,$commodityID')";
				$sql .= " OR ";
				$sql .= "(subCommodityId like '$commodityID' or subCommodityId like '$commodityID,%' or subCommodityId like '%,$commodityID'))";
			}
			$sql .= ")";
			//(regionIds like '$regionIDs' or regionIds like '9,%' or regionIds like '%,9')
		}
		
		if(isset($filter['regionID'])) {
			$sql .= " AND (";
			foreach($filter['regionID'] as $k=>$regionID) {
				if($k>0)
					$sql .= " OR ";
				$sql .= "(regionID like '$regionID' or regionID like '$regionID,%' or regionID like '%,$regionID')";
			}
			$sql .= ")";
			//(regionIds like '$regionID' or regionIds like '9,%' or regionIds like '%,9')
		}
		
		if(isset($filter['tenderType']) && strtolower($filter['tenderType'])!='all'){
			$sql .= " AND n.`buy_sell` = '" . $filter['tenderType'] ."'";
		}
		
		
		if(isset($filter['company']) && !empty($filter['company'])) {			
			$sql .= " AND `company_name` IN ('";
			$sql .= implode("', '", $filter['company']);
			$sql .= "')";	
		}
		
		if(isset($filter['quantity']) && $filter['quantity']>0) {			
			/* $sql .= " AND `qty` IN ('";
			$sql .= implode("', '", $filter['quantity']);
			$sql .= "')";	 */
		}
		
		if(isset($filter['offerType']) && !empty($filter['offerType'])) {			
			$sql .= " AND `offer_type` IN ('";
			$sql .= implode("', '", $filter['offerType']);
			$sql .= "')";	
		}
		//"status" : "expired/live",
		if(!isset($filter['itemID'])){
			if(isset($filter['status']) && strtolower($filter['status'])=='expired') {
				$sql .= " AND n.due_date < CURDATE() ";
			}else{
				$sql .= " AND n.due_date <> '0000-00-00 00:00:00' AND n.due_date >= CURDATE() ";
			}	
		}
		
		if(isset($filter['openingDate']) && strtolower($filter['openingDate'])!='' && isset($filter['closingDate']) && strtolower($filter['closingDate'])!='' ){
			$openingDate=trim($filter['openingDate']);   
			$closingDate=trim($filter['closingDate']);  
			$sql.=" AND n.opening_date >= '$openingDate 00:01' AND n.closing_date <= '$closingDate 23:59'";
		}elseif(isset($filter['openingDate']) && strtolower($filter['openingDate'])!='' )
        {
            $openingDate=trim($filter['openingDate']);            
            $sql.=" AND n.opening_date like '$openingDate%'";
        }elseif(isset($filter['closingDate']) && strtolower($filter['closingDate'])!='' )
        {
            $closingDate=trim($filter['closingDate']);           
            $sql.=" AND n.closing_date like '$closingDate%'";
        }
		
		if(isset($filter['tagId']) && strtolower($filter['tagId'])!='' )
        {
            $sqlTC = "select query, add_condition from tbl_tender_collection where id=".$filter['tagId']; 
			$rsTC = mysql_query($sqlTC)or die(mysql_error().$sqlTC);    
			$rowTC = mysql_fetch_array($rsTC,MYSQL_ASSOC);
			if($rowTC['query']){
				$ids = qryToCommaSepString($rowTC['query']);
				$sql .= " AND `id` IN ($ids)";	
			}
			
			if($rowTC['add_condition']){
				$sql.=" AND (";
				$sql.=$rowTC['add_condition'];
				$sql .= "')";
			}
        }
		/* 
		$web_domain = $appType=='CM' ? "coalmint.com" : "steelmint.com";
		$sql .= " AND MATCH(n.`for_smgroup_company`) AGAINST ('";
        if(strpos($web_domain, "coalmint.com") !== false) {
            $sql .= "coalmint.com";
        } else {
			 $sql .= "steelmint.com";
		}
        $sql .= "' IN BOOLEAN MODE)"; */
		
		
		$orderBY = "";	
		
		$sort_data = array(
			'n.`opening_date`',
			'n.`due_date`',
			'n.`company_name`',
			'n.`created_at`',
			'n.`f_date`',
			't.`buy_sell`',
			't.`country_name`',
			'n.`qty`'
		);

		if (isset($filter['sort']) && in_array($filter['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $filter['sort'];
			if(isset($filter['order']))
				$sql .= " " . $filter['order'];
		}/* elseif($source=='mapview'){
			$sql .= " ORDER BY `country_name` DESC, n.`due_date` ASC";
		} */elseif($orderBY!=''){
			$sql .= " ORDER BY " . $orderBY;
		}else {
			$sql .= " ORDER BY n.`due_date` ASC";
		}
		
		$sql .= " LIMIT " . (int)$offset . ", " . (int)$limit;
		//(select id from tbl_user_portfolio as b where user_id=$memid and type='tenders' and appType='$appType' and n.subCommodityId limit 1) as isAddedToPortfolio
		 $additional_col =",(select id from tbl_bookmark as b where n.id=b.lead_id and user_id=$memid and section='tenders') as isBookmarked";
		$sql = "select n.ID, n.f_date, n.company_name,
			  n.f_authorised ,
			  n.qty,
			  n.qty_unit,
			  n.grade,
			  DATE_FORMAT(n.opening_date,'%d %b %y') as opening_date,
			  n.opening_date as od,
			  n.closing_date,
			  DATE_FORMAT(n.due_date,'%d %b %y') as due_date,
			  n.due_date as dd,
			  n.pdf_path,
			  replace(n.sub_category, '_', ' ') sub_category,
			  replace(n.sub_category_all, '_', ' ') sub_category_all,
			  category as category_all,
			  category_all as categoryToMatch,
			  n.title,
			  n.memid,
			  n.description ,
			  n.buy_sell,
			  n.imgpath,
			  n.offer_type,
			  ttr.state_name,
			  ttr.country_name,
			  CASE
			  WHEN n.multi_loction = 1 THEN 'Multi Location'
			  WHEN ttr.country_name = 'INDIA' THEN concat(ttr.city_name,'/',ttr.state_name,'/',ttr.country_name)
			  ELSE concat(ttr.city_name, '/', ttr.country_name)
			  END as region,
			  CASE
			  WHEN (n.due_date <> '0000-00-00 00:00:00' AND n.due_date >= '$currDate') THEN 'Live'
			  ELSE 'Expired'
			  END as live_expired,
			  CASE 
				WHEN n.corrigendum <> '' THEN n.corrigendum
				ELSE ''
			  END as corrigendum,
			  CASE 
				WHEN n.contact_person <> '' THEN n.contact_person
				ELSE ''
			  END as contact_person,
			  CASE 
				WHEN n.email <> '' THEN n.email
				ELSE ''
			  END as email,
			  (select name from tbl_designation where id = n.designation) 
			  as designation,
			  DATE_FORMAT(n.publish_date,'%d %b %y') as publish_date
			  ,CASE
				WHEN n.mobile<>'' then concat(n.std_code, n.mobile) 
				ELSE ''
			  END as phone_no,
			  commodityId,
			  subCommodityId,
			  regionIds
			  $additional_col 
			  from tbl_tenders as n
			  left join
			  tbl_tenders_region as ttr
			  on n.id = ttr.tender_id
			  $sql
			  ";
         
		if(isset($filter['debug']) && $filter['debug'] == 1) {
            echo "<pre>", print_r($filter), $sql, "</pre>";          
        }    
		
		$query = mysql_query($sql) or die(mysql_error().$sql);
		$region='';$regionCnt = 0;
		$viewMore = array();
		while($row = mysql_fetch_assoc($query)){ 			
			$result = array();
			$tenderId = intval($row['ID']);
			$result['itemId'] = $tenderId;
			$result['title'] = removeMSWordattributes(html_entity_decode($row['post_title'], ENT_QUOTES));	
			//$row['description'] = removeMSWordattributes(html_entity_decode($row['description'], ENT_QUOTES));	
			$result['description'] = $row['company_name']." :".$row['description'] ;
			$result['type'] = 'image';///needd to change
			$result['thumbnailImage'] = env('TENDER_IMAGE_PREFIX').str_replace(' ', '%20',$row['imgpath']) ;
			$result['imageURL'] = env('TENDER_IMAGE_PREFIX').str_replace(' ', '%20',$row['imgpath']) ;
			$result['thumbnailVideoImage'] = "" ;
			$result['videoUrl'] = "" ;
			$result['commodityID'] = intval(end(explode(",",$row['commodityId'])));
			$result['subCommodityID'] = intval(end(explode(",",$row['subCommodityId'])));
			$result['regionID'] = intval(end(explode(",",$row['regionIds'])));
			$result['latitude'] = (double)$row['latitude'];
			$result['longitude'] = (double)$row['longitude'];
			$result['dueDate'] = date('d, Y',strtotime($row['dd']));
			$result['datePublished'] = date('d, M Y',strtotime($row['publish_date']));
			$result['openingDate'] = date('d, Y',strtotime($row['opening_date']));
			
			//$row['state_name'] = ucwords(strtolower($row['state_name']));
           // $row['country_name'] = ucwords(strtolower($row['country_name']));
			
			//$result['location'] = $row['country_name'].', '.$row['state_name'];
			$result['location'] = str_replace('/', ', ',$row['region']);
			
			$row['pdf_path'] = str_replace(' ', '%20',$row['pdf_path']);
            $row['document'] = $pageType=='pre'?'hidden':"https://www.steelmint.com/images/tenders/" . $row['pdf_path'];
			$result['quantity'] = $row['qty'];
			$result['unit'] = $row['qty_unit'];
			$result['grade'] = $row['grade'];
			$result['size'] = "";
			$result['company'] = $pageType=='pre'?'hidden':$row['company_name'];
			$result['type'] = $row['buy_sell'];
			$result['isBookmarked'] = $row['isBookmarked']!='' ? 'y':'n' ; 
			
			if($memid>0 && $result['commodityID']>0){
				$isAddedToPortfolio = qryToCommaSepString("select id from tbl_user_portfolio as b where user_id=$memid and type='tenders' and appType='$appType' and (commodityId like '".$result['commodityID'].",%' or commodityId like '%,".$result['commodityID']."' or commodityId like '".$result['commodityID']."') limit 1");
				$isAddedToPortfolio = 'y';
			}else
				$isAddedToPortfolio = 'n';
			
			//$result['isAddedToPortfolio'] = $row['isAddedToPortfolio']!='' ? 'y':'n' ;
			$result['isAddedToPortfolio'] = $isAddedToPortfolio;
			$result['contactIds'] = array(
												"name" => $pageType=='pre'?'hidden':$row['contact_person'],
												"phone" => $pageType=='pre'?'hidden':$row['phone_no'],
												"Location" => $row['country_name'].', '.$row['state_name'],
												"email" => $pageType=='pre'?'hidden':$row['email'],
												"designation" => $row['designation'],
												"company" => $pageType=='pre'?'hidden':$row['company_name']
											);
			
			$result['gallery'] = getImagesByType($tenderId, $table_type='tenders');
			//$re = explode(',',$row['region']);
			if($source=='mapview'){
				if(isset($filter['regionID'])){
					if(isset($mainEle[$row['state_name']])){
						$mainEle[$row['state_name']]['viewMore'][] = $result;
					}else{
						$mainEle[$row['state_name']] = $result; 
					}
				}else{
					if(isset($mainEle[$row['country_name']])){
						$mainEle[$row['country_name']]['viewMore'][] = $result;
					}else{
						$mainEle[$row['country_name']] = $result;
					}
				}
			}else{
				$mainEle[$tenderId] = $result;
			}
			/* if($source=='mapview' && ($region==$row['country_name'] || $region!='')){
				//$result['123'] = $regionCnt.'-'.$region.'-'.$row['region'].'--'.in_array($region, explode(',',$re)).'<br>';
				//$viewMore[] = $result;
				echo '<br>->'. $row['country_name'];
				$mainEle[$row['country_name']]['viewMore'][] = $result;
			}else{				
				//$regionCnt = 0;
				$region = $row['country_name'];
				if(count($viewMore)>0){
					$result['viewMore'][] = $viewMore;
					$viewMore = array();
				}
				
				$mainEle[$row['country_name']] = $result;		
				echo '<br>->'. $tenderId;
			} */
		}
		
		
		foreach($mainEle as $ele):
			$tenders[]=$ele;
		endforeach;
		
		if($type=='default'){
			//json file $tenders;
		}
		
		//include_once "/home/steelmin/public_html/inc/desconfig.php";
		return $tenders;
	}
	
	public static function getTendersCollectionRows($type='', $appType='SM'){
		$sqlTC = "select id as tagId, title, imageUrl from tbl_tender_collection where status=1"; 
		$rsTC = mysql_query($sqlTC)or die(mysql_error().$sqlTC); 
		$result = array();
		while($row = mysql_fetch_assoc($rsTC)){
			$row['tagId'] = intval($row['tagId']);
			$result[]=$row;
		}
		
		if($type=='default'){
			//json file $result;
		}
		return $result;
	}
	
	public static function getPricesFilter($section='prices', $tradeType='Domestic', $regionID=0, $this->appType){
		
		switch(strtolower($section)){
			case "prices":
			case "price":
				switch($tradeType){
					case "Domestic":
					case "DOM":
						echo '1';
					break;
					case "Import":
					case "CNF":
						
					break;
					case "Export":
					case "FOB":
						
					break;
				}
			break;
		}
		$sqlTC = "select id as tagId, title, imageUrl from tbl_tender_collection where status=1"; 
		$rsTC = mysql_query($sqlTC)or die(mysql_error().$sqlTC); 
		$result = array();
		while($row = mysql_fetch_assoc($rsTC)){
			$row['tagId'] = intval($row['tagId']);
			$result[]=$row;
		}
		
		if($type=='default'){
			//json file $result;
		}
		return $result;
	}
}

?>