
<?php
require_once("ajax_preload.php");      
      
if ($httpreferer){
  if ($ajax_db->getErrorCode()==1){
    die;
  }else{
  	/* Paging */
  	$sLimit = "";
  	if ( isset( $_GET['iDisplayStart'] ) )
  	{
  		$sLimit = "LIMIT ".mysql_real_escape_string( $_GET['iDisplayStart'] ).", ".
  			mysql_real_escape_string( $_GET['iDisplayLength'] );
  	}
  	
  	/* Ordering */
  	if ( isset( $_GET['iSortCol_0'] ) )
  	{
  		$sOrder = "ORDER BY  ";
  		for ( $i=0 ; $i<mysql_real_escape_string( $_GET['iSortingCols'] ) ; $i++ )
  		{
  			$sOrder .= fnColumnToField(mysql_real_escape_string( $_GET['iSortCol_'.$i] ))."
  			 	".mysql_real_escape_string( $_GET['sSortDir_'.$i] ) .", ";
  		}
  		$sOrder = substr_replace( $sOrder, "", -2 );
  	}
  	
  
  	$sWhere = "";
  	
  	if ( $_GET['sSearch'] != "" ){
  	  $re=getLikeHibajelConts($ajax_db,$_GET['sSearch']); 
  	  if ($re==1){
    		$sWhere = " WHERE (pg_gepek.gepszam LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%')";  		                    		                
  		}                  	
    }
  	     	
  	$sQuery = "SELECT SQL_CALC_FOUND_ROWS 
                id_pg_hibanaplo, pg_hibanaplo.hibajelenseg,	pg_hibanaplo.hibajavitasa,	pg_hibanaplo.ellenorzes, pg_gepek.gepszam, pg_hibanaplo.storno
          		 FROM pg_hibanaplo
          		 JOIN pg_gepek
                  ON pg_gepek.id_pg_gepek=pg_hibanaplo.id_pg_gepek   
               ".$sWhere." ".$sOrder." ".$sLimit;
      
  	$rResult = $ajax_db->getAll($sQuery);
  	
  	$sQuery = "
  		SELECT FOUND_ROWS()
  	";

    $aResultFilterTotal = $ajax_db->getAll($sQuery);
    $iFilteredTotal = $aResultFilterTotal[0][0];

 	  $sQuery = "
  		SELECT COUNT(pg_hibanaplo.id_pg_hibanaplo)
  		FROM pg_hibanaplo
  	";  	
  	
    $aResultTotal = $ajax_db->getAll($sQuery);
    $iTotal = $aResultTotal[0][0];      
      	
  	$sOutput = '{';
  	$sOutput .= '"sEcho": '.intval($_GET['sEcho']).', ';
  	$sOutput .= '"iTotalRecords": '.$iTotal.', ';
  	$sOutput .= '"iTotalDisplayRecords": '.$iFilteredTotal.', ';
  	$sOutput .= '"aaData": [ ';
    if ($rResult[0][0]!=''){
    	foreach ($rResult as &$aRow) {
  
        $hjel=getHibajelConts($ajax_db,$aRow["id_pg_hibanaplo"], '3');
        $hjav=getHibajelConts($ajax_db,$aRow["id_pg_hibanaplo"], '1');
        $ell=getHibajelConts($ajax_db,$aRow["id_pg_hibanaplo"], '2');
        $alkatr=getAlkatreszConts($ajax_db,$aRow["id_pg_hibanaplo"]);
        if ($aRow["storno"]){
          $function='<img width=\"16\" height=\"16\" alt=\"\" src=\"'.$config->getValue("media_path","").'/ikon_minusz.png\">'.$aRow["id_pg_hibanaplo"];
        }else{
          $function='<a href=\"javascript:void(0);\" onclick=\"TorolJavitasForm(); getHiddenAjaxData(\''.$config->getValue("AjaxData","").'/get_hibanaplo.php\',\'hibanaplo_id='.$aRow["id_pg_hibanaplo"].'\',\'hiddenajaxdata\',\''.$config->getValue("loading_gif","").'\',runFromHibanaplo(\'hiddenajaxdata\'));\"><img width=\"16\" height=\"16\" alt=\"\" src=\"'.$config->getValue("media_path","").'/ikon_szerkeszt.png\">'.$aRow["id_pg_hibanaplo"].'</a>';
        }
    		$sOutput .= "["; 		                  
    		$sOutput .= '"'.$function.'",';
    		$sOutput .= '"'.addslashes(iconv_iso88592_utf8(substr($aRow['gepszam'],-4))).'",';
    		$sOutput .= '"'.addslashes(iconv_iso88592_utf8($aRow['hibajelenseg'])).'<br />'.$hjel.'",';
    		$sOutput .= '"'.addslashes(iconv_iso88592_utf8($aRow['hibajavitasa'])).'<br />'.$hjav.'",';
        $sOutput .= '"'.addslashes(iconv_iso88592_utf8($aRow['ellenorzes'])).'<br />'.$hjav.'",';    				
    		$sOutput .= '"'.$alkatr.'"';
    		$sOutput .= "],";
    	}
  	}
  	$sOutput = substr_replace( $sOutput, "", -1 );
    $sOutput .= '] }';
  	$content.=$sOutput;
	  
    $ajax_page->AppendTag("content",  $content);    
    $ajax_page->out();
  }
}

	function fnColumnToField( $i ){
		if ( $i == 0 )
			return "pg_hibanaplo.id_pg_hibanaplo";			
		else if ( $i == 1 )
			return "pg_gepek.gepszam";	      
		else if ( $i == 2 )
      return "pg_hibanaplo.hibajelenseg";		      	   
		else if ( $i == 3 )
      return "pg_hibanaplo.hibajavitasa";			   
		else if ( $i == 4 )
		  return "pg_hibanaplo.ellenorzes";
                        		
	}
